bytes_test.go 5.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright 2014 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum 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.
//
// go-ethereum 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 go-ethereum.  If not, see <http://www.gnu.org/licenses/>.

obscuren's avatar
obscuren committed
17
package common
obscuren's avatar
obscuren committed
18 19

import (
20 21 22
	"bytes"
	"testing"

23
	checker "gopkg.in/check.v1"
obscuren's avatar
obscuren committed
24 25
)

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
type BytesSuite struct{}

var _ = checker.Suite(&BytesSuite{})

func (s *BytesSuite) TestByteString(c *checker.C) {
	var data Bytes
	data = []byte{102, 111, 111}
	exp := "foo"
	res := data.String()

	c.Assert(res, checker.Equals, exp)
}

/*
func (s *BytesSuite) TestDeleteFromByteSlice(c *checker.C) {
	data := []byte{1, 2, 3, 4}
	slice := []byte{1, 2, 3, 4}
	exp := []byte{1, 4}
	res := DeleteFromByteSlice(data, slice)

	c.Assert(res, checker.DeepEquals, exp)
}

*/
func (s *BytesSuite) TestNumberToBytes(c *checker.C) {
	// data1 := int(1)
	// res1 := NumberToBytes(data1, 16)
	// c.Check(res1, checker.Panics)

	var data2 float64 = 3.141592653
	exp2 := []byte{0xe9, 0x38}
	res2 := NumberToBytes(data2, 16)
	c.Assert(res2, checker.DeepEquals, exp2)
}

func (s *BytesSuite) TestBytesToNumber(c *checker.C) {
	datasmall := []byte{0xe9, 0x38, 0xe9, 0x38}
	datalarge := []byte{0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38}

	var expsmall uint64 = 0xe938e938
	var explarge uint64 = 0x0

	ressmall := BytesToNumber(datasmall)
	reslarge := BytesToNumber(datalarge)

	c.Assert(ressmall, checker.Equals, expsmall)
	c.Assert(reslarge, checker.Equals, explarge)

}

func (s *BytesSuite) TestReadVarInt(c *checker.C) {
	data8 := []byte{1, 2, 3, 4, 5, 6, 7, 8}
	data4 := []byte{1, 2, 3, 4}
	data2 := []byte{1, 2}
	data1 := []byte{1}

	exp8 := uint64(72623859790382856)
	exp4 := uint64(16909060)
	exp2 := uint64(258)
	exp1 := uint64(1)

	res8 := ReadVarInt(data8)
	res4 := ReadVarInt(data4)
	res2 := ReadVarInt(data2)
	res1 := ReadVarInt(data1)

	c.Assert(res8, checker.Equals, exp8)
	c.Assert(res4, checker.Equals, exp4)
	c.Assert(res2, checker.Equals, exp2)
	c.Assert(res1, checker.Equals, exp1)
}

func (s *BytesSuite) TestCopyBytes(c *checker.C) {
	data1 := []byte{1, 2, 3, 4}
	exp1 := []byte{1, 2, 3, 4}
	res1 := CopyBytes(data1)
	c.Assert(res1, checker.DeepEquals, exp1)
}

func (s *BytesSuite) TestIsHex(c *checker.C) {
	data1 := "a9e67e"
	exp1 := false
	res1 := IsHex(data1)
	c.Assert(res1, checker.DeepEquals, exp1)

	data2 := "0xa9e67e00"
	exp2 := true
	res2 := IsHex(data2)
	c.Assert(res2, checker.DeepEquals, exp2)

}

func (s *BytesSuite) TestParseDataString(c *checker.C) {
	res1 := ParseData("hello", "world", "0x0106")
	data := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000"
	exp1 := Hex2Bytes(data)
	c.Assert(res1, checker.DeepEquals, exp1)
}

func (s *BytesSuite) TestParseDataBytes(c *checker.C) {
	data1 := []byte{232, 212, 165, 16, 0}
	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}

	res1 := ParseData(data1)
	c.Assert(res1, checker.DeepEquals, exp1)

}

func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
	val1 := []byte{1, 2, 3, 4}
	exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}

	res1 := LeftPadBytes(val1, 8)
	res2 := LeftPadBytes(val1, 2)

	c.Assert(res1, checker.DeepEquals, exp1)
	c.Assert(res2, checker.DeepEquals, val1)
}

func (s *BytesSuite) TestFormatData(c *checker.C) {
	data1 := ""
	data2 := "0xa9e67e00"
	data3 := "a9e67e"
	data4 := "\"a9e67e00\""

	// exp1 := []byte{}
	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}
	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}
	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}

	res1 := FormatData(data1)
	res2 := FormatData(data2)
	res3 := FormatData(data3)
	res4 := FormatData(data4)

	c.Assert(res1, checker.IsNil)
	c.Assert(res2, checker.DeepEquals, exp2)
	c.Assert(res3, checker.DeepEquals, exp3)
	c.Assert(res4, checker.DeepEquals, exp4)
}

func (s *BytesSuite) TestRightPadBytes(c *checker.C) {
	val := []byte{1, 2, 3, 4}
	exp := []byte{1, 2, 3, 4, 0, 0, 0, 0}

	resstd := RightPadBytes(val, 8)
	resshrt := RightPadBytes(val, 2)

	c.Assert(resstd, checker.DeepEquals, exp)
	c.Assert(resshrt, checker.DeepEquals, val)
}

func (s *BytesSuite) TestLeftPadString(c *checker.C) {
	val := "test"
	exp := "\x30\x30\x30\x30" + val

	resstd := LeftPadString(val, 8)
	resshrt := LeftPadString(val, 2)

	c.Assert(resstd, checker.Equals, exp)
	c.Assert(resshrt, checker.Equals, val)
}

func (s *BytesSuite) TestRightPadString(c *checker.C) {
	val := "test"
	exp := val + "\x30\x30\x30\x30"

	resstd := RightPadString(val, 8)
	resshrt := RightPadString(val, 2)

	c.Assert(resstd, checker.Equals, exp)
	c.Assert(resshrt, checker.Equals, val)
obscuren's avatar
obscuren committed
198
}
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

func TestFromHex(t *testing.T) {
	input := "0x01"
	expected := []byte{1}
	result := FromHex(input)
	if bytes.Compare(expected, result) != 0 {
		t.Errorf("Expected % x got % x", expected, result)
	}
}

func TestFromHexOddLength(t *testing.T) {
	input := "0x1"
	expected := []byte{1}
	result := FromHex(input)
	if bytes.Compare(expected, result) != 0 {
		t.Errorf("Expected % x got % x", expected, result)
	}
}