1 // Copyright 2014 The go-ethereum Authors
2 // This file is part of the go-ethereum library.
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.
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.
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/>.
23 checker "gopkg.in/check.v1"
26 type BytesSuite struct{}
28 var _ = checker.Suite(&BytesSuite{})
30 func (s *BytesSuite) TestNumberToBytes(c *checker.C) {
32 // res1 := NumberToBytes(data1, 16)
33 // c.Check(res1, checker.Panics)
35 var data2 float64 = 3.141592653
36 exp2 := []byte{0xe9, 0x38}
37 res2 := NumberToBytes(data2, 16)
38 c.Assert(res2, checker.DeepEquals, exp2)
41 func (s *BytesSuite) TestBytesToNumber(c *checker.C) {
42 datasmall := []byte{0xe9, 0x38, 0xe9, 0x38}
43 datalarge := []byte{0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38}
45 var expsmall uint64 = 0xe938e938
46 var explarge uint64 = 0x0
48 ressmall := BytesToNumber(datasmall)
49 reslarge := BytesToNumber(datalarge)
51 c.Assert(ressmall, checker.Equals, expsmall)
52 c.Assert(reslarge, checker.Equals, explarge)
56 func (s *BytesSuite) TestReadVarInt(c *checker.C) {
57 data8 := []byte{1, 2, 3, 4, 5, 6, 7, 8}
58 data4 := []byte{1, 2, 3, 4}
62 exp8 := uint64(72623859790382856)
63 exp4 := uint64(16909060)
67 res8 := ReadVarInt(data8)
68 res4 := ReadVarInt(data4)
69 res2 := ReadVarInt(data2)
70 res1 := ReadVarInt(data1)
72 c.Assert(res8, checker.Equals, exp8)
73 c.Assert(res4, checker.Equals, exp4)
74 c.Assert(res2, checker.Equals, exp2)
75 c.Assert(res1, checker.Equals, exp1)
78 func (s *BytesSuite) TestCopyBytes(c *checker.C) {
79 data1 := []byte{1, 2, 3, 4}
80 exp1 := []byte{1, 2, 3, 4}
81 res1 := CopyBytes(data1)
82 c.Assert(res1, checker.DeepEquals, exp1)
85 func (s *BytesSuite) TestIsHex(c *checker.C) {
89 c.Assert(res1, checker.DeepEquals, exp1)
94 c.Assert(res2, checker.DeepEquals, exp2)
98 func (s *BytesSuite) TestParseDataString(c *checker.C) {
99 res1 := ParseData("hello", "world", "0x0106")
100 data := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000"
101 exp1 := Hex2Bytes(data)
102 c.Assert(res1, checker.DeepEquals, exp1)
105 func (s *BytesSuite) TestParseDataBytes(c *checker.C) {
106 data1 := []byte{232, 212, 165, 16, 0}
107 exp1 := []byte{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, 232, 212, 165, 16, 0}
109 res1 := ParseData(data1)
110 c.Assert(res1, checker.DeepEquals, exp1)
114 func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
115 val1 := []byte{1, 2, 3, 4}
116 exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}
118 res1 := LeftPadBytes(val1, 8)
119 res2 := LeftPadBytes(val1, 2)
121 c.Assert(res1, checker.DeepEquals, exp1)
122 c.Assert(res2, checker.DeepEquals, val1)
125 func (s *BytesSuite) TestFormatData(c *checker.C) {
127 data2 := "0xa9e67e00"
129 data4 := "\"a9e67e00\""
132 exp2 := []byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0xa9, 0xe6, 0x7e, 00}
133 exp3 := []byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00}
134 exp4 := []byte{0x61, 0x39, 0x65, 0x36, 0x37, 0x65, 0x30, 0x30, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00}
136 res1 := FormatData(data1)
137 res2 := FormatData(data2)
138 res3 := FormatData(data3)
139 res4 := FormatData(data4)
141 c.Assert(res1, checker.IsNil)
142 c.Assert(res2, checker.DeepEquals, exp2)
143 c.Assert(res3, checker.DeepEquals, exp3)
144 c.Assert(res4, checker.DeepEquals, exp4)
147 func (s *BytesSuite) TestRightPadBytes(c *checker.C) {
148 val := []byte{1, 2, 3, 4}
149 exp := []byte{1, 2, 3, 4, 0, 0, 0, 0}
151 resstd := RightPadBytes(val, 8)
152 resshrt := RightPadBytes(val, 2)
154 c.Assert(resstd, checker.DeepEquals, exp)
155 c.Assert(resshrt, checker.DeepEquals, val)
158 func (s *BytesSuite) TestLeftPadString(c *checker.C) {
160 exp := "\x30\x30\x30\x30" + val
162 resstd := LeftPadString(val, 8)
163 resshrt := LeftPadString(val, 2)
165 c.Assert(resstd, checker.Equals, exp)
166 c.Assert(resshrt, checker.Equals, val)
169 func (s *BytesSuite) TestRightPadString(c *checker.C) {
171 exp := val + "\x30\x30\x30\x30"
173 resstd := RightPadString(val, 8)
174 resshrt := RightPadString(val, 2)
176 c.Assert(resstd, checker.Equals, exp)
177 c.Assert(resshrt, checker.Equals, val)
180 func TestFromHex(t *testing.T) {
182 expected := []byte{1}
183 result := FromHex(input)
184 if bytes.Compare(expected, result) != 0 {
185 t.Errorf("Expected % x got % x", expected, result)
189 func TestFromHexOddLength(t *testing.T) {
191 expected := []byte{1}
192 result := FromHex(input)
193 if bytes.Compare(expected, result) != 0 {
194 t.Errorf("Expected % x got % x", expected, result)