There are a few unknown fields, represented as is for now. Signed-off-by: Yonah <contrib@gensokyo.uk>
104 lines
2.8 KiB
Go
104 lines
2.8 KiB
Go
package streamdata_test
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"reflect"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"git.gensokyo.uk/yonah/streamdata"
|
|
)
|
|
|
|
func TestIdent(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
testCases := []struct {
|
|
name string
|
|
data string
|
|
ident *streamdata.Ident
|
|
err error
|
|
}{
|
|
{"sample0", "2717837930-430126387-fd9c9877-00e1-47e3-867b-916cd1e6b90c", &streamdata.Ident{
|
|
Serial: 2717837930,
|
|
Channel: 430126387,
|
|
Data: [streamdata.IdentFFLen]byte{
|
|
0xfd, 0x9c, 0x98, 0x77,
|
|
0x00, 0xe1,
|
|
0x47, 0xe3,
|
|
0x86, 0x7b,
|
|
0x91, 0x6c, 0xd1, 0xe6, 0xb9, 0x0c,
|
|
},
|
|
}, nil},
|
|
|
|
{"sample1", "2719457116-430126387-8f8dbbd4-26e7-4fbe-bc3b-fb71c93b5c39", &streamdata.Ident{
|
|
Serial: 2719457116,
|
|
Channel: 430126387,
|
|
Data: [streamdata.IdentFFLen]byte{
|
|
0x8f, 0x8d, 0xbb, 0xd4,
|
|
0x26, 0xe7,
|
|
0x4f, 0xbe,
|
|
0xbc, 0x3b,
|
|
0xfb, 0x71, 0xc9, 0x3b, 0x5c, 0x39,
|
|
},
|
|
}, nil},
|
|
|
|
{"sample2", "2721992950-430126387-08ad437f-3599-43d6-9934-83f31265e9d0", &streamdata.Ident{
|
|
Serial: 2721992950,
|
|
Channel: 430126387,
|
|
Data: [streamdata.IdentFFLen]byte{
|
|
0x08, 0xad, 0x43, 0x7f,
|
|
0x35, 0x99,
|
|
0x43, 0xd6,
|
|
0x99, 0x34,
|
|
0x83, 0xf3, 0x12, 0x65, 0xe9, 0xd0,
|
|
},
|
|
}, nil},
|
|
|
|
{"field count", "", nil, streamdata.IdentFieldError(1)},
|
|
{"invalid serial", "\x00------", nil, strconv.ErrSyntax},
|
|
{"invalid channel", "0-\x00-----", nil, strconv.ErrSyntax},
|
|
{"short ff0", "0-0-ff----", nil, &streamdata.IdentFFError{Got: 1, Want: 4}},
|
|
{"long ff1", "0-0-deadbeef-fefefe---", nil, &streamdata.IdentFFError{Got: 3, Want: 2}},
|
|
{"short ff2", "0-0-deadbeef-fefe-fd--", nil, &streamdata.IdentFFError{Got: 1, Want: 2}},
|
|
{"long ff3", "0-0-deadbeef-fefe-fdfd-fcfcfcfc-", nil, &streamdata.IdentFFError{Got: 4, Want: 2}},
|
|
{"short ff4", "0-0-deadbeef-fefe-fdfd-fcfc-cafe", nil, &streamdata.IdentFFError{Got: 2, Want: 6}},
|
|
{"invalid ff", "0-0-deadbeef-fefe-fdfd-fcfc-meow", nil, hex.InvalidByteError('m')},
|
|
}
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("decode", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
var got streamdata.Ident
|
|
if err := got.UnmarshalText([]byte(tc.data)); !reflect.DeepEqual(err, tc.err) {
|
|
t.Errorf("UnmarshalText: error = %v, want %v", err, tc.err)
|
|
}
|
|
|
|
if tc.err == nil && !reflect.DeepEqual(&got, tc.ident) {
|
|
t.Errorf("UnmarshalText: %#v, want %#v", got, *tc.ident)
|
|
}
|
|
})
|
|
|
|
if tc.err != nil {
|
|
return
|
|
}
|
|
|
|
t.Run("encode", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if got := tc.ident.String(); got != tc.data {
|
|
t.Fatalf("String: %s, want %s", got, tc.data)
|
|
}
|
|
|
|
if got, err := tc.ident.MarshalText(); err != nil {
|
|
t.Fatalf("MarshalText: error = %v", err)
|
|
} else if string(got) != tc.data {
|
|
t.Errorf("MarshalText: %s, want %s", string(got), tc.data)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|