mirror of
https://github.com/luxfi/coreth.git
synced 2026-10-30 17:00:51 +00:00
59 lines
1.9 KiB
Go
59 lines
1.9 KiB
Go
// Copyright (C) 2019-2025, Lux Industries, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package message
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"math/rand"
|
|
"testing"
|
|
|
|
"github.com/luxfi/geth/common"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// TestMarshalCodeRequest asserts that the structure or serialization logic hasn't changed, primarily to
|
|
// ensure compatibility with the network.
|
|
func TestMarshalCodeRequest(t *testing.T) {
|
|
codeRequest := CodeRequest{
|
|
Hashes: []common.Hash{common.BytesToHash([]byte("some code pls"))},
|
|
}
|
|
|
|
base64CodeRequest := "AAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAHNvbWUgY29kZSBwbHM="
|
|
|
|
codeRequestBytes, err := Codec.Marshal(Version, codeRequest)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, base64CodeRequest, base64.StdEncoding.EncodeToString(codeRequestBytes))
|
|
|
|
var c CodeRequest
|
|
_, err = Codec.Unmarshal(codeRequestBytes, &c)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, codeRequest.Hashes, c.Hashes)
|
|
}
|
|
|
|
// TestMarshalCodeResponse asserts that the structure or serialization logic hasn't changed, primarily to
|
|
// ensure compatibility with the network.
|
|
func TestMarshalCodeResponse(t *testing.T) {
|
|
// generate some random code data
|
|
// use local random generator for deterministic results (rand.Seed is deprecated in Go 1.20+)
|
|
rng := rand.New(rand.NewSource(1)) //nolint:gosec
|
|
codeData := make([]byte, 50)
|
|
_, err := rng.Read(codeData)
|
|
assert.NoError(t, err)
|
|
|
|
codeResponse := CodeResponse{
|
|
Data: [][]byte{codeData},
|
|
}
|
|
|
|
base64CodeResponse := "AAAAAAABAAAAMlL9/AchgmVPFj9fD5piHXKVZsdNEAN8TXu7BAfR4sZJgYVa2GgdDYbR6R4AFnk5y2aU"
|
|
|
|
codeResponseBytes, err := Codec.Marshal(Version, codeResponse)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, base64CodeResponse, base64.StdEncoding.EncodeToString(codeResponseBytes))
|
|
|
|
var c CodeResponse
|
|
_, err = Codec.Unmarshal(codeResponseBytes, &c)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, codeResponse.Data, c.Data)
|
|
}
|