bytes_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package common
  17. import (
  18. "bytes"
  19. "testing"
  20. checker "gopkg.in/check.v1"
  21. )
  22. type BytesSuite struct{}
  23. var _ = checker.Suite(&BytesSuite{})
  24. func (s *BytesSuite) TestByteString(c *checker.C) {
  25. var data Bytes
  26. data = []byte{102, 111, 111}
  27. exp := "foo"
  28. res := data.String()
  29. c.Assert(res, checker.Equals, exp)
  30. }
  31. /*
  32. func (s *BytesSuite) TestDeleteFromByteSlice(c *checker.C) {
  33. data := []byte{1, 2, 3, 4}
  34. slice := []byte{1, 2, 3, 4}
  35. exp := []byte{1, 4}
  36. res := DeleteFromByteSlice(data, slice)
  37. c.Assert(res, checker.DeepEquals, exp)
  38. }
  39. */
  40. func (s *BytesSuite) TestNumberToBytes(c *checker.C) {
  41. // data1 := int(1)
  42. // res1 := NumberToBytes(data1, 16)
  43. // c.Check(res1, checker.Panics)
  44. var data2 float64 = 3.141592653
  45. exp2 := []byte{0xe9, 0x38}
  46. res2 := NumberToBytes(data2, 16)
  47. c.Assert(res2, checker.DeepEquals, exp2)
  48. }
  49. func (s *BytesSuite) TestBytesToNumber(c *checker.C) {
  50. datasmall := []byte{0xe9, 0x38, 0xe9, 0x38}
  51. datalarge := []byte{0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38}
  52. var expsmall uint64 = 0xe938e938
  53. var explarge uint64 = 0x0
  54. ressmall := BytesToNumber(datasmall)
  55. reslarge := BytesToNumber(datalarge)
  56. c.Assert(ressmall, checker.Equals, expsmall)
  57. c.Assert(reslarge, checker.Equals, explarge)
  58. }
  59. func (s *BytesSuite) TestReadVarInt(c *checker.C) {
  60. data8 := []byte{1, 2, 3, 4, 5, 6, 7, 8}
  61. data4 := []byte{1, 2, 3, 4}
  62. data2 := []byte{1, 2}
  63. data1 := []byte{1}
  64. exp8 := uint64(72623859790382856)
  65. exp4 := uint64(16909060)
  66. exp2 := uint64(258)
  67. exp1 := uint64(1)
  68. res8 := ReadVarInt(data8)
  69. res4 := ReadVarInt(data4)
  70. res2 := ReadVarInt(data2)
  71. 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)
  76. }
  77. func (s *BytesSuite) TestCopyBytes(c *checker.C) {
  78. data1 := []byte{1, 2, 3, 4}
  79. exp1 := []byte{1, 2, 3, 4}
  80. res1 := CopyBytes(data1)
  81. c.Assert(res1, checker.DeepEquals, exp1)
  82. }
  83. func (s *BytesSuite) TestIsHex(c *checker.C) {
  84. data1 := "a9e67e"
  85. exp1 := false
  86. res1 := IsHex(data1)
  87. c.Assert(res1, checker.DeepEquals, exp1)
  88. data2 := "0xa9e67e00"
  89. exp2 := true
  90. res2 := IsHex(data2)
  91. c.Assert(res2, checker.DeepEquals, exp2)
  92. }
  93. func (s *BytesSuite) TestParseDataString(c *checker.C) {
  94. res1 := ParseData("hello", "world", "0x0106")
  95. data := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000"
  96. exp1 := Hex2Bytes(data)
  97. c.Assert(res1, checker.DeepEquals, exp1)
  98. }
  99. func (s *BytesSuite) TestParseDataBytes(c *checker.C) {
  100. data1 := []byte{232, 212, 165, 16, 0}
  101. 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}
  102. res1 := ParseData(data1)
  103. c.Assert(res1, checker.DeepEquals, exp1)
  104. }
  105. func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
  106. val1 := []byte{1, 2, 3, 4}
  107. exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}
  108. res1 := LeftPadBytes(val1, 8)
  109. res2 := LeftPadBytes(val1, 2)
  110. c.Assert(res1, checker.DeepEquals, exp1)
  111. c.Assert(res2, checker.DeepEquals, val1)
  112. }
  113. func (s *BytesSuite) TestFormatData(c *checker.C) {
  114. data1 := ""
  115. data2 := "0xa9e67e00"
  116. data3 := "a9e67e"
  117. data4 := "\"a9e67e00\""
  118. // exp1 := []byte{}
  119. 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}
  120. 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}
  121. 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}
  122. res1 := FormatData(data1)
  123. res2 := FormatData(data2)
  124. res3 := FormatData(data3)
  125. res4 := FormatData(data4)
  126. c.Assert(res1, checker.IsNil)
  127. c.Assert(res2, checker.DeepEquals, exp2)
  128. c.Assert(res3, checker.DeepEquals, exp3)
  129. c.Assert(res4, checker.DeepEquals, exp4)
  130. }
  131. func (s *BytesSuite) TestRightPadBytes(c *checker.C) {
  132. val := []byte{1, 2, 3, 4}
  133. exp := []byte{1, 2, 3, 4, 0, 0, 0, 0}
  134. resstd := RightPadBytes(val, 8)
  135. resshrt := RightPadBytes(val, 2)
  136. c.Assert(resstd, checker.DeepEquals, exp)
  137. c.Assert(resshrt, checker.DeepEquals, val)
  138. }
  139. func (s *BytesSuite) TestLeftPadString(c *checker.C) {
  140. val := "test"
  141. exp := "\x30\x30\x30\x30" + val
  142. resstd := LeftPadString(val, 8)
  143. resshrt := LeftPadString(val, 2)
  144. c.Assert(resstd, checker.Equals, exp)
  145. c.Assert(resshrt, checker.Equals, val)
  146. }
  147. func (s *BytesSuite) TestRightPadString(c *checker.C) {
  148. val := "test"
  149. exp := val + "\x30\x30\x30\x30"
  150. resstd := RightPadString(val, 8)
  151. resshrt := RightPadString(val, 2)
  152. c.Assert(resstd, checker.Equals, exp)
  153. c.Assert(resshrt, checker.Equals, val)
  154. }
  155. func TestFromHex(t *testing.T) {
  156. input := "0x01"
  157. expected := []byte{1}
  158. result := FromHex(input)
  159. if bytes.Compare(expected, result) != 0 {
  160. t.Errorf("Expected % x got % x", expected, result)
  161. }
  162. }
  163. func TestFromHexOddLength(t *testing.T) {
  164. input := "0x1"
  165. expected := []byte{1}
  166. result := FromHex(input)
  167. if bytes.Compare(expected, result) != 0 {
  168. t.Errorf("Expected % x got % x", expected, result)
  169. }
  170. }