bytes_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2014 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. 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) TestNumberToBytes(c *checker.C) {
  25. // data1 := int(1)
  26. // res1 := NumberToBytes(data1, 16)
  27. // c.Check(res1, checker.Panics)
  28. var data2 float64 = 3.141592653
  29. exp2 := []byte{0xe9, 0x38}
  30. res2 := NumberToBytes(data2, 16)
  31. c.Assert(res2, checker.DeepEquals, exp2)
  32. }
  33. func (s *BytesSuite) TestBytesToNumber(c *checker.C) {
  34. datasmall := []byte{0xe9, 0x38, 0xe9, 0x38}
  35. datalarge := []byte{0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38}
  36. var expsmall uint64 = 0xe938e938
  37. var explarge uint64 = 0x0
  38. ressmall := BytesToNumber(datasmall)
  39. reslarge := BytesToNumber(datalarge)
  40. c.Assert(ressmall, checker.Equals, expsmall)
  41. c.Assert(reslarge, checker.Equals, explarge)
  42. }
  43. func (s *BytesSuite) TestReadVarInt(c *checker.C) {
  44. data8 := []byte{1, 2, 3, 4, 5, 6, 7, 8}
  45. data4 := []byte{1, 2, 3, 4}
  46. data2 := []byte{1, 2}
  47. data1 := []byte{1}
  48. exp8 := uint64(72623859790382856)
  49. exp4 := uint64(16909060)
  50. exp2 := uint64(258)
  51. exp1 := uint64(1)
  52. res8 := ReadVarInt(data8)
  53. res4 := ReadVarInt(data4)
  54. res2 := ReadVarInt(data2)
  55. res1 := ReadVarInt(data1)
  56. c.Assert(res8, checker.Equals, exp8)
  57. c.Assert(res4, checker.Equals, exp4)
  58. c.Assert(res2, checker.Equals, exp2)
  59. c.Assert(res1, checker.Equals, exp1)
  60. }
  61. func (s *BytesSuite) TestCopyBytes(c *checker.C) {
  62. data1 := []byte{1, 2, 3, 4}
  63. exp1 := []byte{1, 2, 3, 4}
  64. res1 := CopyBytes(data1)
  65. c.Assert(res1, checker.DeepEquals, exp1)
  66. }
  67. func (s *BytesSuite) TestIsHex(c *checker.C) {
  68. data1 := "a9e67e"
  69. exp1 := false
  70. res1 := IsHex(data1)
  71. c.Assert(res1, checker.DeepEquals, exp1)
  72. data2 := "0xa9e67e00"
  73. exp2 := true
  74. res2 := IsHex(data2)
  75. c.Assert(res2, checker.DeepEquals, exp2)
  76. }
  77. func (s *BytesSuite) TestParseDataString(c *checker.C) {
  78. res1 := ParseData("hello", "world", "0x0106")
  79. data := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000"
  80. exp1 := Hex2Bytes(data)
  81. c.Assert(res1, checker.DeepEquals, exp1)
  82. }
  83. func (s *BytesSuite) TestParseDataBytes(c *checker.C) {
  84. data1 := []byte{232, 212, 165, 16, 0}
  85. 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}
  86. res1 := ParseData(data1)
  87. c.Assert(res1, checker.DeepEquals, exp1)
  88. }
  89. func (s *BytesSuite) TestLeftPadBytes(c *checker.C) {
  90. val1 := []byte{1, 2, 3, 4}
  91. exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4}
  92. res1 := LeftPadBytes(val1, 8)
  93. res2 := LeftPadBytes(val1, 2)
  94. c.Assert(res1, checker.DeepEquals, exp1)
  95. c.Assert(res2, checker.DeepEquals, val1)
  96. }
  97. func (s *BytesSuite) TestFormatData(c *checker.C) {
  98. data1 := ""
  99. data2 := "0xa9e67e00"
  100. data3 := "a9e67e"
  101. data4 := "\"a9e67e00\""
  102. // exp1 := []byte{}
  103. 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}
  104. 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}
  105. 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}
  106. res1 := FormatData(data1)
  107. res2 := FormatData(data2)
  108. res3 := FormatData(data3)
  109. res4 := FormatData(data4)
  110. c.Assert(res1, checker.IsNil)
  111. c.Assert(res2, checker.DeepEquals, exp2)
  112. c.Assert(res3, checker.DeepEquals, exp3)
  113. c.Assert(res4, checker.DeepEquals, exp4)
  114. }
  115. func (s *BytesSuite) TestRightPadBytes(c *checker.C) {
  116. val := []byte{1, 2, 3, 4}
  117. exp := []byte{1, 2, 3, 4, 0, 0, 0, 0}
  118. resstd := RightPadBytes(val, 8)
  119. resshrt := RightPadBytes(val, 2)
  120. c.Assert(resstd, checker.DeepEquals, exp)
  121. c.Assert(resshrt, checker.DeepEquals, val)
  122. }
  123. func (s *BytesSuite) TestLeftPadString(c *checker.C) {
  124. val := "test"
  125. exp := "\x30\x30\x30\x30" + val
  126. resstd := LeftPadString(val, 8)
  127. resshrt := LeftPadString(val, 2)
  128. c.Assert(resstd, checker.Equals, exp)
  129. c.Assert(resshrt, checker.Equals, val)
  130. }
  131. func (s *BytesSuite) TestRightPadString(c *checker.C) {
  132. val := "test"
  133. exp := val + "\x30\x30\x30\x30"
  134. resstd := RightPadString(val, 8)
  135. resshrt := RightPadString(val, 2)
  136. c.Assert(resstd, checker.Equals, exp)
  137. c.Assert(resshrt, checker.Equals, val)
  138. }
  139. func TestFromHex(t *testing.T) {
  140. input := "0x01"
  141. expected := []byte{1}
  142. result := FromHex(input)
  143. if !bytes.Equal(expected, result) {
  144. t.Errorf("Expected % x got % x", expected, result)
  145. }
  146. }
  147. func TestFromHexOddLength(t *testing.T) {
  148. input := "0x1"
  149. expected := []byte{1}
  150. result := FromHex(input)
  151. if !bytes.Equal(expected, result) {
  152. t.Errorf("Expected % x got % x", expected, result)
  153. }
  154. }