2019-09-04 20:46:28 +02:00
|
|
|
package tlv_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-10-31 05:20:29 +01:00
|
|
|
"reflect"
|
2019-09-04 20:46:28 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/tlv"
|
|
|
|
)
|
|
|
|
|
2019-10-31 05:20:29 +01:00
|
|
|
type parsedTypeTest struct {
|
2019-11-10 12:06:49 +01:00
|
|
|
name string
|
|
|
|
encode []tlv.Type
|
|
|
|
decode []tlv.Type
|
|
|
|
expParsedTypes tlv.TypeSet
|
2019-10-31 05:20:29 +01:00
|
|
|
}
|
|
|
|
|
2019-09-04 20:46:28 +02:00
|
|
|
// TestParsedTypes asserts that a Stream will properly return the set of types
|
|
|
|
// that it encounters when the type is known-and-decoded or unknown-and-ignored.
|
|
|
|
func TestParsedTypes(t *testing.T) {
|
|
|
|
const (
|
2019-11-10 12:06:49 +01:00
|
|
|
knownType = 1
|
|
|
|
unknownType = 3
|
|
|
|
secondKnownType = 4
|
2019-09-04 20:46:28 +02:00
|
|
|
)
|
|
|
|
|
2019-10-31 05:20:29 +01:00
|
|
|
tests := []parsedTypeTest{
|
|
|
|
{
|
2019-11-10 12:06:49 +01:00
|
|
|
name: "known and unknown",
|
2019-10-31 05:20:29 +01:00
|
|
|
encode: []tlv.Type{knownType, unknownType},
|
|
|
|
decode: []tlv.Type{knownType},
|
2019-11-10 12:06:49 +01:00
|
|
|
expParsedTypes: tlv.TypeSet{
|
|
|
|
unknownType: false,
|
|
|
|
knownType: true,
|
|
|
|
},
|
2019-10-31 05:20:29 +01:00
|
|
|
},
|
|
|
|
{
|
2019-11-10 12:06:49 +01:00
|
|
|
name: "known and missing known",
|
|
|
|
encode: []tlv.Type{knownType},
|
|
|
|
decode: []tlv.Type{knownType, secondKnownType},
|
|
|
|
expParsedTypes: tlv.TypeSet{
|
|
|
|
knownType: true,
|
|
|
|
},
|
2019-10-31 05:20:29 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
test := test
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
testParsedTypes(t, test)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testParsedTypes(t *testing.T, test parsedTypeTest) {
|
|
|
|
encRecords := make([]tlv.Record, 0, len(test.encode))
|
|
|
|
for _, typ := range test.encode {
|
|
|
|
encRecords = append(
|
|
|
|
encRecords, tlv.MakePrimitiveRecord(typ, new(uint64)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
decRecords := make([]tlv.Record, 0, len(test.decode))
|
|
|
|
for _, typ := range test.decode {
|
|
|
|
decRecords = append(
|
|
|
|
decRecords, tlv.MakePrimitiveRecord(typ, new(uint64)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a stream that will encode the test's set of types.
|
|
|
|
encStream := tlv.MustNewStream(encRecords...)
|
2019-09-04 20:46:28 +02:00
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := encStream.Encode(&b); err != nil {
|
|
|
|
t.Fatalf("unable to encode stream: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-10-31 05:20:29 +01:00
|
|
|
// Create a stream that will parse a subset of the test's types.
|
|
|
|
decStream := tlv.MustNewStream(decRecords...)
|
2019-09-04 20:46:28 +02:00
|
|
|
|
|
|
|
parsedTypes, err := decStream.DecodeWithParsedTypes(
|
|
|
|
bytes.NewReader(b.Bytes()),
|
|
|
|
)
|
2019-11-10 12:06:49 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error decoding: %v", err)
|
2019-09-04 20:46:28 +02:00
|
|
|
}
|
2019-11-10 12:06:49 +01:00
|
|
|
if !reflect.DeepEqual(parsedTypes, test.expParsedTypes) {
|
|
|
|
t.Fatalf("error mismatch on parsed types")
|
2019-09-04 20:46:28 +02:00
|
|
|
}
|
|
|
|
}
|