|
17 | 17 | package core
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "bytes" |
20 | 21 | "math/big"
|
21 | 22 | "testing"
|
| 23 | + |
| 24 | + "github.com/ethereum/go-ethereum/common/hexutil" |
22 | 25 | )
|
23 | 26 |
|
| 27 | +func TestBytesPadding(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + Type string |
| 30 | + Input []byte |
| 31 | + Output []byte // nil => error |
| 32 | + }{ |
| 33 | + { |
| 34 | + // Fail on wrong length |
| 35 | + Type: "bytes20", |
| 36 | + Input: []byte{}, |
| 37 | + Output: nil, |
| 38 | + }, |
| 39 | + { |
| 40 | + Type: "bytes1", |
| 41 | + Input: []byte{1}, |
| 42 | + Output: []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 43 | + }, |
| 44 | + { |
| 45 | + Type: "bytes1", |
| 46 | + Input: []byte{1, 2}, |
| 47 | + Output: nil, |
| 48 | + }, |
| 49 | + { |
| 50 | + Type: "bytes7", |
| 51 | + Input: []byte{1, 2, 3, 4, 5, 6, 7}, |
| 52 | + Output: []byte{1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, |
| 53 | + }, |
| 54 | + { |
| 55 | + Type: "bytes32", |
| 56 | + Input: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}, |
| 57 | + Output: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}, |
| 58 | + }, |
| 59 | + { |
| 60 | + Type: "bytes32", |
| 61 | + Input: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33}, |
| 62 | + Output: nil, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + d := TypedData{} |
| 67 | + for i, test := range tests { |
| 68 | + val, err := d.EncodePrimitiveValue(test.Type, test.Input, 1) |
| 69 | + if test.Output == nil { |
| 70 | + if err == nil { |
| 71 | + t.Errorf("test %d: expected error, got no error (result %x)", i, val) |
| 72 | + } |
| 73 | + } else { |
| 74 | + if err != nil { |
| 75 | + t.Errorf("test %d: expected no error, got %v", i, err) |
| 76 | + } |
| 77 | + if len(val) != 32 { |
| 78 | + t.Errorf("test %d: expected len 32, got %d", i, len(val)) |
| 79 | + } |
| 80 | + if !bytes.Equal(val, test.Output) { |
| 81 | + t.Errorf("test %d: expected %x, got %x", i, test.Output, val) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestParseBytes(t *testing.T) { |
| 88 | + for i, tt := range []struct { |
| 89 | + v interface{} |
| 90 | + exp []byte |
| 91 | + }{ |
| 92 | + {"0x", []byte{}}, |
| 93 | + {"0x1234", []byte{0x12, 0x34}}, |
| 94 | + {[]byte{12, 34}, []byte{12, 34}}, |
| 95 | + {hexutil.Bytes([]byte{12, 34}), []byte{12, 34}}, |
| 96 | + {"1234", nil}, // not a proper hex-string |
| 97 | + {"0x01233", nil}, // nibbles should be rejected |
| 98 | + {"not a hex string", nil}, |
| 99 | + {15, nil}, |
| 100 | + {nil, nil}, |
| 101 | + } { |
| 102 | + out, ok := parseBytes(tt.v) |
| 103 | + if tt.exp == nil { |
| 104 | + if ok || out != nil { |
| 105 | + t.Errorf("test %d: expected !ok, got ok = %v with out = %x", i, ok, out) |
| 106 | + } |
| 107 | + continue |
| 108 | + } |
| 109 | + if !ok { |
| 110 | + t.Errorf("test %d: expected ok got !ok", i) |
| 111 | + } |
| 112 | + if !bytes.Equal(out, tt.exp) { |
| 113 | + t.Errorf("test %d: expected %x got %x", i, tt.exp, out) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
24 | 118 | func TestParseInteger(t *testing.T) {
|
25 | 119 | for i, tt := range []struct {
|
26 | 120 | t string
|
|
0 commit comments