github.com/aswedchain/aswed@v1.0.1/signer/core/signed_data_internal_test.go (about) 1 // Copyright 2019 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library 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 Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package core 18 19 import ( 20 "bytes" 21 "math/big" 22 "testing" 23 24 "github.com/aswedchain/aswed/common/hexutil" 25 ) 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 118 func TestParseInteger(t *testing.T) { 119 for i, tt := range []struct { 120 t string 121 v interface{} 122 exp *big.Int 123 }{ 124 {"uint32", "-123", nil}, 125 {"int32", "-123", big.NewInt(-123)}, 126 {"uint32", "0xff", big.NewInt(0xff)}, 127 {"int8", "0xffff", nil}, 128 } { 129 res, err := parseInteger(tt.t, tt.v) 130 if tt.exp == nil && res == nil { 131 continue 132 } 133 if tt.exp == nil && res != nil { 134 t.Errorf("test %d, got %v, expected nil", i, res) 135 continue 136 } 137 if tt.exp != nil && res == nil { 138 t.Errorf("test %d, got '%v', expected %v", i, err, tt.exp) 139 continue 140 } 141 if tt.exp.Cmp(res) != 0 { 142 t.Errorf("test %d, got %v expected %v", i, res, tt.exp) 143 } 144 } 145 }