From b70300012d12edf65f9ce62135f02403c8854d2b Mon Sep 17 00:00:00 2001 From: Simone Mosciatti Date: Tue, 8 Mar 2022 13:39:16 +0100 Subject: [PATCH] test encoding and decoding of arrays --- ua/decode_test.go | 98 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) diff --git a/ua/decode_test.go b/ua/decode_test.go index 0a3334a3..e461798f 100644 --- a/ua/decode_test.go +++ b/ua/decode_test.go @@ -20,6 +20,10 @@ type B struct { S []*A } +type C struct { + A [2]int32 +} + func TestCodec(t *testing.T) { tests := []struct { name string @@ -112,6 +116,30 @@ func TestCodec(t *testing.T) { 0x67, 0x45, 0x00, 0x00, }, }, + { + name: "[2]uint32", + v: &struct{ V [2]uint32 }{[2]uint32{0x1234, 0x4567}}, + b: []byte{ + // length + 0x02, 0x00, 0x00, 0x00, + // elem 1 + 0x34, 0x12, 0x00, 0x00, + // elem 2 + 0x67, 0x45, 0x00, 0x00, + }, + }, + { + name: "[2]uint32{1}", + v: &struct{ V [2]uint32 }{[2]uint32{1}}, + b: []byte{ + // length + 0x02, 0x00, 0x00, 0x00, + // elem 1 + 0x01, 0x00, 0x00, 0x00, + // zero element of the array + 0x00, 0x00, 0x00, 0x00, + }, + }, { name: "string", v: &struct{ V string }{"abc"}, @@ -180,6 +208,17 @@ func TestCodec(t *testing.T) { 0x03, 0x00, 0x00, 0x00, }, }, + { + name: "[2]uint32", + v: &struct{ V [2]uint32 }{[2]uint32{}}, + b: []byte{ + // length + 0x02, 0x00, 0x00, 0x00, + // values + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + }, + }, { name: "[]*A", v: &struct{ V []*A }{ @@ -206,7 +245,7 @@ func TestCodec(t *testing.T) { }, }, b: []byte{ - // B.A.N + // B.A.V 0x34, 0x12, 0x00, 0x00, // B.A.S == nil 0xff, 0xff, 0xff, 0xff, @@ -222,7 +261,7 @@ func TestCodec(t *testing.T) { }, }, b: []byte{ - // B.A.N + // B.A.V 0x90, 0x78, 0x00, 0x00, // len(B.A.S) 0x02, 0x00, 0x00, 0x00, @@ -232,6 +271,45 @@ func TestCodec(t *testing.T) { 0x67, 0x45, 0x00, 0x00, }, }, + { + name: "&C", + v: &C{A: [2]int32{1, 2}}, + b: []byte{ + // len(C.A) + 0x02, 0x00, 0x00, 0x00, + // C.A[0] + 0x01, 0x00, 0x00, 0x00, + // C.A[1] + 0x02, 0x00, 0x00, 0x00, + }, + }, + { + name: "[3]C", + v: &struct{ V [3]C }{[3]C{ + {}, + {A: [2]int32{7}}, + {A: [2]int32{0, 9}}, + }}, + b: []byte{ + // len(V) + 0x03, 0x00, 0x00, 0x00, + // len(V[0]) + 0x02, 0x00, 0x00, 0x00, + // V[0] + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + // len(V[1]) + 0x02, 0x00, 0x00, 0x00, + // V[1] + 0x07, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + // len(V[2]) + 0x02, 0x00, 0x00, 0x00, + // V[2] + 0x00, 0x00, 0x00, 0x00, + 0x09, 0x00, 0x00, 0x00, + }, + }, } for _, tt := range tests { @@ -266,3 +344,19 @@ func TestCodec(t *testing.T) { }) } } + +func TestFailDecodeArray(t *testing.T) { + b := []byte{ + // len + 0x03, 0x00, 0x00, 0x00, + // Values + 0x00, 0x00, 0x00, 0x00, // 0 + 0x00, 0x00, 0x00, 0x00, // 1 + 0x07, 0x00, 0x00, 0x00, // 7 + } + var a [2]int32 + _, err := Decode(b, &a) + if err == nil { + t.Fatalf("was expecting error for tryig to decode a stream of bytes with length 3 into an array of size 2") + } +}