123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- // Copyright 2019 The go-ethereum Authors
- // This file is part of the go-ethereum library.
- //
- // The go-ethereum library is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Lesser General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // The go-ethereum library is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Lesser General Public License for more details.
- //
- // You should have received a copy of the GNU Lesser General Public License
- // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
- package core
- import (
- "bytes"
- "math/big"
- "testing"
- "github.com/ethereum/go-ethereum/common/hexutil"
- )
- func TestBytesPadding(t *testing.T) {
- tests := []struct {
- Type string
- Input []byte
- Output []byte // nil => error
- }{
- {
- // Fail on wrong length
- Type: "bytes20",
- Input: []byte{},
- Output: nil,
- },
- {
- Type: "bytes1",
- Input: []byte{1},
- 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},
- },
- {
- Type: "bytes1",
- Input: []byte{1, 2},
- Output: nil,
- },
- {
- Type: "bytes7",
- Input: []byte{1, 2, 3, 4, 5, 6, 7},
- 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},
- },
- {
- Type: "bytes32",
- 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},
- 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},
- },
- {
- Type: "bytes32",
- 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},
- Output: nil,
- },
- }
- d := TypedData{}
- for i, test := range tests {
- val, err := d.EncodePrimitiveValue(test.Type, test.Input, 1)
- if test.Output == nil {
- if err == nil {
- t.Errorf("test %d: expected error, got no error (result %x)", i, val)
- }
- } else {
- if err != nil {
- t.Errorf("test %d: expected no error, got %v", i, err)
- }
- if len(val) != 32 {
- t.Errorf("test %d: expected len 32, got %d", i, len(val))
- }
- if !bytes.Equal(val, test.Output) {
- t.Errorf("test %d: expected %x, got %x", i, test.Output, val)
- }
- }
- }
- }
- func TestParseBytes(t *testing.T) {
- for i, tt := range []struct {
- v interface{}
- exp []byte
- }{
- {"0x", []byte{}},
- {"0x1234", []byte{0x12, 0x34}},
- {[]byte{12, 34}, []byte{12, 34}},
- {hexutil.Bytes([]byte{12, 34}), []byte{12, 34}},
- {"1234", nil}, // not a proper hex-string
- {"0x01233", nil}, // nibbles should be rejected
- {"not a hex string", nil},
- {15, nil},
- {nil, nil},
- } {
- out, ok := parseBytes(tt.v)
- if tt.exp == nil {
- if ok || out != nil {
- t.Errorf("test %d: expected !ok, got ok = %v with out = %x", i, ok, out)
- }
- continue
- }
- if !ok {
- t.Errorf("test %d: expected ok got !ok", i)
- }
- if !bytes.Equal(out, tt.exp) {
- t.Errorf("test %d: expected %x got %x", i, tt.exp, out)
- }
- }
- }
- func TestParseInteger(t *testing.T) {
- for i, tt := range []struct {
- t string
- v interface{}
- exp *big.Int
- }{
- {"uint32", "-123", nil},
- {"int32", "-123", big.NewInt(-123)},
- {"uint32", "0xff", big.NewInt(0xff)},
- {"int8", "0xffff", nil},
- } {
- res, err := parseInteger(tt.t, tt.v)
- if tt.exp == nil && res == nil {
- continue
- }
- if tt.exp == nil && res != nil {
- t.Errorf("test %d, got %v, expected nil", i, res)
- continue
- }
- if tt.exp != nil && res == nil {
- t.Errorf("test %d, got '%v', expected %v", i, err, tt.exp)
- continue
- }
- if tt.exp.Cmp(res) != 0 {
- t.Errorf("test %d, got %v expected %v", i, res, tt.exp)
- }
- }
- }
|