yggdrasil-go/src/core/version_test.go

36 lines
853 B
Go
Raw Normal View History

2023-03-18 12:14:32 +00:00
package core
import (
2023-06-18 20:28:14 +01:00
"bytes"
2023-03-18 12:14:32 +00:00
"crypto/ed25519"
"crypto/rand"
2023-03-18 12:14:32 +00:00
"reflect"
"testing"
)
func TestVersionRoundtrip(t *testing.T) {
for _, test := range []*version_metadata{
{majorVer: 1},
{majorVer: 256},
{majorVer: 2, minorVer: 4},
{majorVer: 2, minorVer: 257},
{majorVer: 258, minorVer: 259},
{majorVer: 3, minorVer: 5, priority: 6},
{majorVer: 260, minorVer: 261, priority: 7},
} {
// Generate a random public key for each time, since it is
// a required field.
test.publicKey = make(ed25519.PublicKey, ed25519.PublicKeySize)
_, _ = rand.Read(test.publicKey)
2023-03-18 12:14:32 +00:00
2023-06-18 20:28:14 +01:00
encoded := bytes.NewBuffer(test.encode())
2023-03-18 12:14:32 +00:00
decoded := &version_metadata{}
if !decoded.decode(encoded) {
t.Fatalf("failed to decode")
}
if !reflect.DeepEqual(test, decoded) {
t.Fatalf("round-trip failed\nwant: %+v\n got: %+v", test, decoded)
}
}
}