github.com/prysmaticlabs/prysm@v1.4.4/validator/slashing-protection/local/standard-protection-format/helpers_test.go (about) 1 package interchangeformat 2 3 import ( 4 "fmt" 5 "math" 6 "reflect" 7 "testing" 8 9 types "github.com/prysmaticlabs/eth2-types" 10 ) 11 12 func Test_uint64FromString(t *testing.T) { 13 tests := []struct { 14 name string 15 str string 16 want uint64 17 wantErr bool 18 }{ 19 { 20 name: "Overflow uint64 gets MaxUint64", 21 str: "29348902839048290384902839048290384902938748278934789273984728934789273894798273498", 22 want: math.MaxUint64, 23 wantErr: true, 24 }, 25 { 26 name: "Max Uint64 works", 27 str: "18446744073709551615", 28 want: math.MaxUint64, 29 wantErr: false, 30 }, 31 { 32 name: "Negative number fails", 33 str: "-3", 34 wantErr: true, 35 }, 36 { 37 name: "Junk fails", 38 str: "alksjdkjasd", 39 wantErr: true, 40 }, 41 { 42 name: "0 works", 43 str: "0", 44 want: 0, 45 }, 46 { 47 name: "Normal uint64 works", 48 str: "23980", 49 want: 23980, 50 }, 51 } 52 for _, tt := range tests { 53 t.Run(fmt.Sprintf("Uint64/%s", tt.name), func(t *testing.T) { 54 got, err := Uint64FromString(tt.str) 55 if (err != nil) != tt.wantErr { 56 t.Errorf("Uint64FromString() error = %v, wantErr %v", err, tt.wantErr) 57 return 58 } 59 if got != tt.want { 60 t.Errorf("Uint64FromString() got = %v, want %v", got, tt.want) 61 } 62 }) 63 t.Run(fmt.Sprintf("Epoch/%s", tt.name), func(t *testing.T) { 64 got, err := EpochFromString(tt.str) 65 if (err != nil) != tt.wantErr { 66 t.Errorf("EpochFromString() error = %v, wantErr %v", err, tt.wantErr) 67 return 68 } 69 if got != types.Epoch(tt.want) { 70 t.Errorf("EpochFromString() got = %v, want %v", got, tt.want) 71 } 72 }) 73 t.Run(fmt.Sprintf("Slot/%s", tt.name), func(t *testing.T) { 74 got, err := SlotFromString(tt.str) 75 if (err != nil) != tt.wantErr { 76 t.Errorf("SlotFromString() error = %v, wantErr %v", err, tt.wantErr) 77 return 78 } 79 if got != types.Slot(tt.want) { 80 t.Errorf("SlotFromString() got = %v, want %v", got, tt.want) 81 } 82 }) 83 } 84 } 85 86 func Test_pubKeyFromHex(t *testing.T) { 87 tests := []struct { 88 name string 89 str string 90 want [48]byte 91 wantErr bool 92 }{ 93 { 94 name: "Empty value fails due to wrong length", 95 str: "", 96 wantErr: true, 97 }, 98 { 99 name: "Junk fails", 100 str: "alksjdkjasd", 101 wantErr: true, 102 }, 103 { 104 name: "Empty value with 0x prefix fails due to wrong length", 105 str: "0x", 106 wantErr: true, 107 }, 108 { 109 name: "Works with 0x prefix and good public key", 110 str: "0xb845089a1457f811bfc000588fbb4e713669be8ce060ea6be3c6ece09afc3794106c91ca73acda5e5457122d58723bed", 111 want: [48]byte{184, 69, 8, 154, 20, 87, 248, 17, 191, 192, 0, 88, 143, 187, 78, 113, 54, 105, 190, 140, 224, 96, 234, 107, 227, 198, 236, 224, 154, 252, 55, 148, 16, 108, 145, 202, 115, 172, 218, 94, 84, 87, 18, 45, 88, 114, 59, 237}, 112 }, 113 { 114 name: "Works without 0x prefix and good public key", 115 str: "b845089a1457f811bfc000588fbb4e713669be8ce060ea6be3c6ece09afc3794106c91ca73acda5e5457122d58723bed", 116 want: [48]byte{184, 69, 8, 154, 20, 87, 248, 17, 191, 192, 0, 88, 143, 187, 78, 113, 54, 105, 190, 140, 224, 96, 234, 107, 227, 198, 236, 224, 154, 252, 55, 148, 16, 108, 145, 202, 115, 172, 218, 94, 84, 87, 18, 45, 88, 114, 59, 237}, 117 }, 118 { 119 name: "0x prefix and wrong length public key fails", 120 str: "0xb845089a1457f811bfc000588fbb4e713669be8", 121 wantErr: true, 122 }, 123 } 124 for _, tt := range tests { 125 t.Run(tt.name, func(t *testing.T) { 126 got, err := PubKeyFromHex(tt.str) 127 if (err != nil) != tt.wantErr { 128 t.Errorf("PubKeyFromHex() error = %v, wantErr %v", err, tt.wantErr) 129 return 130 } 131 if !reflect.DeepEqual(got, tt.want) { 132 t.Errorf("PubKeyFromHex() got = %v, want %v", got, tt.want) 133 } 134 }) 135 } 136 } 137 138 func Test_rootFromHex(t *testing.T) { 139 tests := []struct { 140 name string 141 str string 142 want [32]byte 143 wantErr bool 144 }{ 145 { 146 name: "Empty value fails due to wrong length", 147 str: "", 148 wantErr: true, 149 }, 150 { 151 name: "Junk fails", 152 str: "alksjdkjasd", 153 wantErr: true, 154 }, 155 { 156 name: "Empty value with 0x prefix fails due to wrong length", 157 str: "0x", 158 wantErr: true, 159 }, 160 { 161 name: "Works with 0x prefix and good root", 162 str: "0x4ff6f743a43f3b4f95350831aeaf0a122a1a392922c45d804280284a69eb850b", 163 want: [32]byte{79, 246, 247, 67, 164, 63, 59, 79, 149, 53, 8, 49, 174, 175, 10, 18, 42, 26, 57, 41, 34, 196, 93, 128, 66, 128, 40, 74, 105, 235, 133, 11}, 164 }, 165 { 166 name: "Works without 0x prefix and good root", 167 str: "4ff6f743a43f3b4f95350831aeaf0a122a1a392922c45d804280284a69eb850b", 168 want: [32]byte{79, 246, 247, 67, 164, 63, 59, 79, 149, 53, 8, 49, 174, 175, 10, 18, 42, 26, 57, 41, 34, 196, 93, 128, 66, 128, 40, 74, 105, 235, 133, 11}, 169 }, 170 { 171 name: "0x prefix and wrong length root fails", 172 str: "0xb845089a14", 173 wantErr: true, 174 }, 175 } 176 for _, tt := range tests { 177 t.Run(tt.name, func(t *testing.T) { 178 got, err := RootFromHex(tt.str) 179 if (err != nil) != tt.wantErr { 180 t.Errorf("rootFromHex() error = %v, wantErr %v", err, tt.wantErr) 181 return 182 } 183 if !reflect.DeepEqual(got, tt.want) { 184 t.Errorf("rootFromHex() got = %v, want %v", got, tt.want) 185 } 186 }) 187 } 188 } 189 190 func Test_rootToHexString(t *testing.T) { 191 mockRoot := [32]byte{1} 192 tests := []struct { 193 name string 194 root []byte 195 want string 196 wantErr bool 197 }{ 198 { 199 name: "nil roots return empty string", 200 root: nil, 201 want: "", 202 wantErr: false, 203 }, 204 { 205 name: "len(root) == 0 returns empty string", 206 root: make([]byte, 0), 207 want: "", 208 wantErr: false, 209 }, 210 { 211 name: "non-empty root with incorrect size returns error", 212 root: make([]byte, 20), 213 want: "", 214 wantErr: true, 215 }, 216 { 217 name: "non-empty root with correct size returns expected value", 218 root: mockRoot[:], 219 want: "0x0100000000000000000000000000000000000000000000000000000000000000", 220 wantErr: false, 221 }, 222 } 223 for _, tt := range tests { 224 t.Run(tt.name, func(t *testing.T) { 225 got, err := rootToHexString(tt.root) 226 if (err != nil) != tt.wantErr { 227 t.Errorf("rootToHexString() error = %v, wantErr %v", err, tt.wantErr) 228 return 229 } 230 if got != tt.want { 231 t.Errorf("rootToHexString() got = %v, want %v", got, tt.want) 232 } 233 }) 234 } 235 } 236 237 func Test_pubKeyToHexString(t *testing.T) { 238 mockPubKey := [48]byte{1} 239 tests := []struct { 240 name string 241 pubKey []byte 242 want string 243 wantErr bool 244 }{ 245 { 246 name: "nil pubkey should return error", 247 pubKey: nil, 248 want: "", 249 wantErr: true, 250 }, 251 { 252 name: "empty pubkey should return error", 253 pubKey: make([]byte, 0), 254 want: "", 255 wantErr: true, 256 }, 257 { 258 name: "wrong length pubkey should return error", 259 pubKey: make([]byte, 3), 260 want: "", 261 wantErr: true, 262 }, 263 { 264 name: "non-empty pubkey with correct size returns expected value", 265 pubKey: mockPubKey[:], 266 want: "0x010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", 267 wantErr: false, 268 }, 269 } 270 for _, tt := range tests { 271 t.Run(tt.name, func(t *testing.T) { 272 got, err := pubKeyToHexString(tt.pubKey) 273 if (err != nil) != tt.wantErr { 274 t.Errorf("pubKeyToHexString() error = %v, wantErr %v", err, tt.wantErr) 275 return 276 } 277 if got != tt.want { 278 t.Errorf("pubKeyToHexString() got = %v, want %v", got, tt.want) 279 } 280 }) 281 } 282 }