mls_test.go raw
1 package mls
2
3 import (
4 "encoding"
5 "encoding/hex"
6 "encoding/json"
7 "fmt"
8 "os"
9 "testing"
10
11 "golang.org/x/crypto/cryptobyte"
12 )
13
14 type testBytes []byte
15
16 var _ encoding.TextUnmarshaler = (*testBytes)(nil)
17
18 func (out *testBytes) UnmarshalText(text []byte) error {
19 *out = make([]byte, hex.DecodedLen(len(text)))
20 _, err := hex.Decode(*out, text)
21 return err
22 }
23
24 func (tb testBytes) ByteString() *cryptobyte.String {
25 s := cryptobyte.String(tb)
26 return &s
27 }
28
29 func loadTestVector(t *testing.T, filename string, v interface{}) {
30 f, err := os.Open(filename)
31 if err != nil {
32 t.Fatalf("failed to open test vector %q: %v", filename, err)
33 }
34 defer f.Close()
35
36 if err := json.NewDecoder(f).Decode(v); err != nil {
37 t.Fatalf("failed to load test vector %q: %v", filename, err)
38 }
39 }
40
41 type deserializationTest struct {
42 VLBytesHeader testBytes `json:"vlbytes_header"`
43 Length uint32 `json:"length"`
44 }
45
46 func TestDeserialization(t *testing.T) {
47 var tests []deserializationTest
48 loadTestVector(t, "testdata/deserialization.json", &tests)
49
50 for i, tc := range tests {
51 t.Run(fmt.Sprintf("[%v]", i), func(t *testing.T) {
52 var length uint32
53 s := tc.VLBytesHeader.ByteString()
54 if !readVarint(s, &length) {
55 t.Fatalf("readVarint() = false")
56 } else if !s.Empty() {
57 t.Errorf("byte string should be empty after readVarint()")
58 }
59 if length != tc.Length {
60 t.Errorf("got %v, want %v", length, tc.Length)
61 }
62 })
63 }
64 }
65