@@ -29,110 +29,112 @@ import (
2929// a string that is greater than the max 5-bit fixmap size
3030const gtFixMapString = "12345678901234567890123456789012"
3131
32+ var parseVoteTestCases = []struct {
33+ obj any
34+ errContains string
35+ }{
36+ // vote
37+ {map [string ]string {"a" : "1" , "b" : "2" },
38+ "expected fixed map size 3 for unauthenticatedVote, got 2" },
39+ {map [string ]any {"a" : 1 , "b" : 2 , "c" : 3 },
40+ "unexpected field in unauthenticatedVote" },
41+ {[]int {1 , 2 , 3 },
42+ "reading map for unauthenticatedVote" },
43+ {map [string ]string {"a" : "1" , "b" : "2" , "c" : "3" , "d" : "4" , "e" : "5" , "f" : "6" , "g" : "7" },
44+ "expected fixed map size 3 for unauthenticatedVote, got 7" },
45+ {map [string ]string {gtFixMapString : "1" , "b" : "2" , "c" : "3" },
46+ "reading key for unauthenticatedVote" },
47+
48+ // cred
49+ {map [string ]string {"cred" : "1" , "d" : "2" , "e" : "3" },
50+ "reading map for UnauthenticatedCredential" },
51+ {map [string ]any {"cred" : map [string ]int {"pf" : 1 , "q" : 2 }, "d" : "2" , "e" : "3" },
52+ "expected fixed map size 1 for UnauthenticatedCredential, got 2" },
53+ {map [string ]any {"cred" : map [string ]int {gtFixMapString : 1 }, "d" : "2" , "e" : "3" },
54+ "reading key for UnauthenticatedCredential" },
55+ {map [string ]any {"cred" : map [string ]string {"invalid" : "1" }, "r" : "2" , "sig" : "3" },
56+ "unexpected field in UnauthenticatedCredential" },
57+ {map [string ]any {"cred" : map [string ]any {"pf" : []byte {1 , 2 , 3 }}, "r" : "2" , "sig" : "3" },
58+ "reading pf" },
59+
60+ // rawVote
61+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : []int {1 , 2 , 3 }, "sig" : "3" },
62+ "reading map for rawVote" },
63+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]string {}, "sig" : "3" },
64+ "expected fixmap size for rawVote 1 <= cnt <= 5, got 0" },
65+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]string {"a" : "1" , "b" : "2" , "c" : "3" , "d" : "4" , "e" : "5" , "f" : "6" }, "sig" : "3" },
66+ "expected fixmap size for rawVote 1 <= cnt <= 5, got 6" },
67+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]string {gtFixMapString : "1" }, "sig" : "3" },
68+ "reading key for rawVote" },
69+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]string {"invalid" : "1" }, "sig" : "3" },
70+ "unexpected field in rawVote" },
71+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"per" : "not-a-number" }, "sig" : "3" },
72+ "reading per" },
73+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : "not-a-number" }, "sig" : "3" },
74+ "reading rnd" },
75+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"step" : "not-a-number" }, "sig" : "3" },
76+ "reading step" },
77+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : "not-a-map" }, "sig" : "3" },
78+ "reading map for proposalValue" },
79+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"snd" : []int {1 , 2 , 3 }}, "sig" : "3" },
80+ "reading snd" },
81+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]string {"snd" : "1" }, "sig" : []int {1 , 2 , 3 }},
82+ "reading snd: expected bin8 length 32" },
83+
84+ // proposalValue
85+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : map [string ]string {"invalid" : "1" }}, "sig" : "3" },
86+ "unexpected field in proposalValue" },
87+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : map [string ]string {gtFixMapString : "1" }}, "sig" : "3" },
88+ "reading key for proposalValue" },
89+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : map [string ]any {"dig" : []int {1 , 2 , 3 }}}, "sig" : "3" },
90+ "reading dig" },
91+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : map [string ]any {"encdig" : []int {1 , 2 , 3 }}}, "sig" : "3" },
92+ "reading encdig" },
93+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : map [string ]any {"oper" : "not-a-number" }}, "sig" : "3" },
94+ "reading oper" },
95+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : map [string ]any {"oprop" : []int {1 , 2 , 3 }}}, "sig" : "3" },
96+ "reading oprop" },
97+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : map [string ]any {"a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 , "e" : 5 }}, "sig" : "3" },
98+ "expected fixmap size for proposalValue 1 <= cnt <= 4, got 5" },
99+
100+ // sig
101+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : []int {1 , 2 , 3 }},
102+ "reading map for OneTimeSignature" },
103+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {}},
104+ "expected fixed map size 6 for OneTimeSignature, got 0" },
105+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {"p" : []int {1 }}},
106+ "expected fixed map size 6 for OneTimeSignature, got 1" },
107+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
108+ gtFixMapString : "1" , "a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 , "e" : 5 }},
109+ "reading key for OneTimeSignature" },
110+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
111+ "a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 , "e" : 5 , "f" : 6 }},
112+ "unexpected field in OneTimeSignature" },
113+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
114+ "p" : []int {1 }, "p1s" : [64 ]byte {}, "p2" : [32 ]byte {}, "p2s" : [64 ]byte {}, "ps" : [64 ]byte {}, "s" : [64 ]byte {}}},
115+ "reading p: expected bin8 length 32" },
116+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
117+ "p" : [32 ]byte {}, "p1s" : []int {1 }, "p2" : [32 ]byte {}, "p2s" : [64 ]byte {}, "ps" : [64 ]byte {}, "s" : [64 ]byte {}}},
118+ "reading p1s: expected bin8 length 64" },
119+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
120+ "p" : [32 ]byte {}, "p1s" : [64 ]byte {}, "p2" : []int {1 }, "p2s" : [64 ]byte {}, "ps" : [64 ]byte {}, "s" : [64 ]byte {}}},
121+ "reading p2: expected bin8 length 32" },
122+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
123+ "p" : [32 ]byte {}, "p1s" : [64 ]byte {}, "p2" : [32 ]byte {}, "p2s" : []int {1 }, "ps" : [64 ]byte {}, "s" : [64 ]byte {}}},
124+ "reading p2s: expected bin8 length 64" },
125+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
126+ "p" : [32 ]byte {}, "p1s" : [64 ]byte {}, "p2" : [32 ]byte {}, "p2s" : [64 ]byte {}, "ps" : []int {1 }, "s" : [64 ]byte {}}},
127+ "reading ps: expected bin8 length 64" },
128+ {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
129+ "p" : [32 ]byte {}, "p1s" : [64 ]byte {}, "p2" : [32 ]byte {}, "p2s" : [64 ]byte {}, "ps" : [64 ]byte {}, "s" : []int {1 }}},
130+ "reading s: expected bin8 length 64" },
131+ }
132+
32133// TestParseVoteErrors tests error cases of the parseVote function
33134func TestParseVoteErrors (t * testing.T ) {
34135 partitiontest .PartitionTest (t )
35136
36- for _ , tc := range []struct {
37- obj any
38- errContains string
39- }{
40- // vote
41- {map [string ]string {"a" : "1" , "b" : "2" },
42- "expected fixed map size 3 for unauthenticatedVote, got 2" },
43- {map [string ]any {"a" : 1 , "b" : 2 , "c" : 3 },
44- "unexpected field in unauthenticatedVote" },
45- {[]int {1 , 2 , 3 },
46- "reading map for unauthenticatedVote" },
47- {map [string ]string {"a" : "1" , "b" : "2" , "c" : "3" , "d" : "4" , "e" : "5" , "f" : "6" , "g" : "7" },
48- "expected fixed map size 3 for unauthenticatedVote, got 7" },
49- {map [string ]string {gtFixMapString : "1" , "b" : "2" , "c" : "3" },
50- "reading key for unauthenticatedVote" },
51-
52- // cred
53- {map [string ]string {"cred" : "1" , "d" : "2" , "e" : "3" },
54- "reading map for UnauthenticatedCredential" },
55- {map [string ]any {"cred" : map [string ]int {"pf" : 1 , "q" : 2 }, "d" : "2" , "e" : "3" },
56- "expected fixed map size 1 for UnauthenticatedCredential, got 2" },
57- {map [string ]any {"cred" : map [string ]int {gtFixMapString : 1 }, "d" : "2" , "e" : "3" },
58- "reading key for UnauthenticatedCredential" },
59- {map [string ]any {"cred" : map [string ]string {"invalid" : "1" }, "r" : "2" , "sig" : "3" },
60- "unexpected field in UnauthenticatedCredential" },
61- {map [string ]any {"cred" : map [string ]any {"pf" : []byte {1 , 2 , 3 }}, "r" : "2" , "sig" : "3" },
62- "reading pf" },
63-
64- // rawVote
65- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : []int {1 , 2 , 3 }, "sig" : "3" },
66- "reading map for rawVote" },
67- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]string {}, "sig" : "3" },
68- "expected fixmap size for rawVote 1 <= cnt <= 5, got 0" },
69- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]string {"a" : "1" , "b" : "2" , "c" : "3" , "d" : "4" , "e" : "5" , "f" : "6" }, "sig" : "3" },
70- "expected fixmap size for rawVote 1 <= cnt <= 5, got 6" },
71- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]string {gtFixMapString : "1" }, "sig" : "3" },
72- "reading key for rawVote" },
73- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]string {"invalid" : "1" }, "sig" : "3" },
74- "unexpected field in rawVote" },
75- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"per" : "not-a-number" }, "sig" : "3" },
76- "reading per" },
77- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : "not-a-number" }, "sig" : "3" },
78- "reading rnd" },
79- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"step" : "not-a-number" }, "sig" : "3" },
80- "reading step" },
81- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : "not-a-map" }, "sig" : "3" },
82- "reading map for proposalValue" },
83- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"snd" : []int {1 , 2 , 3 }}, "sig" : "3" },
84- "reading snd" },
85- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]string {"snd" : "1" }, "sig" : []int {1 , 2 , 3 }},
86- "reading snd: expected bin8 length 32" },
87-
88- // proposalValue
89- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : map [string ]string {"invalid" : "1" }}, "sig" : "3" },
90- "unexpected field in proposalValue" },
91- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : map [string ]string {gtFixMapString : "1" }}, "sig" : "3" },
92- "reading key for proposalValue" },
93- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : map [string ]any {"dig" : []int {1 , 2 , 3 }}}, "sig" : "3" },
94- "reading dig" },
95- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : map [string ]any {"encdig" : []int {1 , 2 , 3 }}}, "sig" : "3" },
96- "reading encdig" },
97- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : map [string ]any {"oper" : "not-a-number" }}, "sig" : "3" },
98- "reading oper" },
99- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : map [string ]any {"oprop" : []int {1 , 2 , 3 }}}, "sig" : "3" },
100- "reading oprop" },
101- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"prop" : map [string ]any {"a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 , "e" : 5 }}, "sig" : "3" },
102- "expected fixmap size for proposalValue 1 <= cnt <= 4, got 5" },
103-
104- // sig
105- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : []int {1 , 2 , 3 }},
106- "reading map for OneTimeSignature" },
107- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {}},
108- "expected fixed map size 6 for OneTimeSignature, got 0" },
109- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {"p" : []int {1 }}},
110- "expected fixed map size 6 for OneTimeSignature, got 1" },
111- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
112- gtFixMapString : "1" , "a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 , "e" : 5 }},
113- "reading key for OneTimeSignature" },
114- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
115- "a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 , "e" : 5 , "f" : 6 }},
116- "unexpected field in OneTimeSignature" },
117- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
118- "p" : []int {1 }, "p1s" : [64 ]byte {}, "p2" : [32 ]byte {}, "p2s" : [64 ]byte {}, "ps" : [64 ]byte {}, "s" : [64 ]byte {}}},
119- "reading p: expected bin8 length 32" },
120- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
121- "p" : [32 ]byte {}, "p1s" : []int {1 }, "p2" : [32 ]byte {}, "p2s" : [64 ]byte {}, "ps" : [64 ]byte {}, "s" : [64 ]byte {}}},
122- "reading p1s: expected bin8 length 64" },
123- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
124- "p" : [32 ]byte {}, "p1s" : [64 ]byte {}, "p2" : []int {1 }, "p2s" : [64 ]byte {}, "ps" : [64 ]byte {}, "s" : [64 ]byte {}}},
125- "reading p2: expected bin8 length 32" },
126- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
127- "p" : [32 ]byte {}, "p1s" : [64 ]byte {}, "p2" : [32 ]byte {}, "p2s" : []int {1 }, "ps" : [64 ]byte {}, "s" : [64 ]byte {}}},
128- "reading p2s: expected bin8 length 64" },
129- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
130- "p" : [32 ]byte {}, "p1s" : [64 ]byte {}, "p2" : [32 ]byte {}, "p2s" : [64 ]byte {}, "ps" : []int {1 }, "s" : [64 ]byte {}}},
131- "reading ps: expected bin8 length 64" },
132- {map [string ]any {"cred" : map [string ]any {"pf" : crypto.VrfProof {1 }}, "r" : map [string ]any {"rnd" : 1 }, "sig" : map [string ]any {
133- "p" : [32 ]byte {}, "p1s" : [64 ]byte {}, "p2" : [32 ]byte {}, "p2s" : [64 ]byte {}, "ps" : [64 ]byte {}, "s" : []int {1 }}},
134- "reading s: expected bin8 length 64" },
135- } {
137+ for _ , tc := range parseVoteTestCases {
136138 mock := & mockCompressWriter {}
137139 var buf []byte
138140 // protocol.Encode and protocol.EncodeReflect encode keys in alphabetical order
@@ -143,22 +145,47 @@ func TestParseVoteErrors(t *testing.T) {
143145 }
144146 err := parseVote (buf , mock )
145147 require .Error (t , err )
146- t .Logf ("For test with %T, got error: %v, expected to contain: %v" , tc .obj , err , tc .errContains )
147148 require .Contains (t , err .Error (), tc .errContains )
148149 }
149150}
150151
152+ // TestParseEncodeStaticSteps asserts that table entries for step:1, step:2, step:3 are encoded
153+ func TestParseEncodeStaticSteps (t * testing.T ) {
154+ partitiontest .PartitionTest (t )
155+ v := agreement.UnauthenticatedVote {}
156+ v .Cred .Proof [0 ] = 1 // not empty
157+ v .R .Round = 1
158+ v .Sig .PK [0 ] = 1 // not empty
159+
160+ for i := 1 ; i <= 3 ; i ++ {
161+ var expectedStaticIdx uint8
162+ switch i {
163+ case 1 :
164+ v .R .Step = 1
165+ expectedStaticIdx = staticIdxStepVal1Field
166+ case 2 :
167+ v .R .Step = 2
168+ expectedStaticIdx = staticIdxStepVal2Field
169+ case 3 :
170+ v .R .Step = 3
171+ expectedStaticIdx = staticIdxStepVal3Field
172+ }
173+
174+ msgpbuf := protocol .Encode (& v )
175+ w := & mockCompressWriter {}
176+ err := parseVote (msgpbuf , w )
177+ require .NoError (t , err )
178+ require .Contains (t , w .writes , expectedStaticIdx )
179+ }
180+ }
181+
151182// mockCompressWriter implements compressWriter for testing
152183type mockCompressWriter struct { writes []any }
153184
154- func (m * mockCompressWriter ) writeStatic (idx uint8 ) { m .writes = append (m .writes , idx ) }
155-
185+ func (m * mockCompressWriter ) writeStatic (idx uint8 ) { m .writes = append (m .writes , idx ) }
156186func (m * mockCompressWriter ) writeLiteralBin64 (val [64 ]byte ) { m .writes = append (m .writes , val ) }
157-
158187func (m * mockCompressWriter ) writeLiteralBin80 (val [80 ]byte ) { m .writes = append (m .writes , val ) }
159-
160188func (m * mockCompressWriter ) writeDynamicBin32 (val [32 ]byte ) { m .writes = append (m .writes , val ) }
161-
162189func (m * mockCompressWriter ) writeDynamicVaruint (valBytes []byte ) error {
163190 m .writes = append (m .writes , valBytes )
164191 return nil
0 commit comments