|
| 1 | +// Copyright (C) 2019-2025 Algorand, Inc. |
| 2 | +// This file is part of go-algorand |
| 3 | +// |
| 4 | +// go-algorand is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as |
| 6 | +// published by the Free Software Foundation, either version 3 of the |
| 7 | +// License, or (at your option) any later version. |
| 8 | +// |
| 9 | +// go-algorand is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with go-algorand. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package vpack |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/algorand/go-algorand/agreement" |
| 23 | + "github.com/algorand/go-algorand/crypto" |
| 24 | + "github.com/algorand/go-algorand/protocol" |
| 25 | + "github.com/algorand/go-algorand/test/partitiontest" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | +) |
| 28 | + |
| 29 | +// a string that is greater than the max 5-bit fixmap size |
| 30 | +const gtFixMapString = "12345678901234567890123456789012" |
| 31 | + |
| 32 | +// TestParseVoteErrors tests error cases of the parseVote function |
| 33 | +func TestParseVoteErrors(t *testing.T) { |
| 34 | + partitiontest.PartitionTest(t) |
| 35 | + |
| 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 | + } { |
| 136 | + mock := &mockCompressWriter{} |
| 137 | + var buf []byte |
| 138 | + // protocol.Encode and protocol.EncodeReflect encode keys in alphabetical order |
| 139 | + if v, ok := tc.obj.(*agreement.UnauthenticatedVote); ok { |
| 140 | + buf = protocol.Encode(v) |
| 141 | + } else { |
| 142 | + buf = protocol.EncodeReflect(tc.obj) |
| 143 | + } |
| 144 | + err := parseVote(buf, mock) |
| 145 | + require.Error(t, err) |
| 146 | + t.Logf("For test with %T, got error: %v, expected to contain: %v", tc.obj, err, tc.errContains) |
| 147 | + require.Contains(t, err.Error(), tc.errContains) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// mockCompressWriter implements compressWriter for testing |
| 152 | +type mockCompressWriter struct{ writes []any } |
| 153 | + |
| 154 | +func (m *mockCompressWriter) writeStatic(idx uint8) { m.writes = append(m.writes, idx) } |
| 155 | + |
| 156 | +func (m *mockCompressWriter) writeLiteralBin64(val [64]byte) { m.writes = append(m.writes, val) } |
| 157 | + |
| 158 | +func (m *mockCompressWriter) writeLiteralBin80(val [80]byte) { m.writes = append(m.writes, val) } |
| 159 | + |
| 160 | +func (m *mockCompressWriter) writeDynamicBin32(val [32]byte) { m.writes = append(m.writes, val) } |
| 161 | + |
| 162 | +func (m *mockCompressWriter) writeDynamicVaruint(valBytes []byte) error { |
| 163 | + m.writes = append(m.writes, valBytes) |
| 164 | + return nil |
| 165 | +} |
0 commit comments