rlp_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. "math/big"
  20. "reflect"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/rlp"
  23. )
  24. func TestNonInterfaceSlice(t *testing.T) {
  25. vala := []string{"value1", "value2", "value3"}
  26. valb := []interface{}{"value1", "value2", "value3"}
  27. resa := Encode(vala)
  28. resb := Encode(valb)
  29. if !bytes.Equal(resa, resb) {
  30. t.Errorf("expected []string & []interface{} to be equal")
  31. }
  32. }
  33. func TestRlpValueEncoding(t *testing.T) {
  34. val := EmptyValue()
  35. val.AppendList().Append(byte(1)).Append(byte(2)).Append(byte(3))
  36. val.Append("4").AppendList().Append(byte(5))
  37. res, err := rlp.EncodeToBytes(val)
  38. if err != nil {
  39. t.Fatalf("encode error: %v", err)
  40. }
  41. exp := Encode([]interface{}{[]interface{}{1, 2, 3}, "4", []interface{}{5}})
  42. if bytes.Compare(res, exp) != 0 {
  43. t.Errorf("expected %x, got %x", exp, res)
  44. }
  45. }
  46. func TestValueSlice(t *testing.T) {
  47. val := []interface{}{
  48. "value1",
  49. "valeu2",
  50. "value3",
  51. }
  52. value := NewValue(val)
  53. splitVal := value.SliceFrom(1)
  54. if splitVal.Len() != 2 {
  55. t.Error("SliceFrom: Expected len", 2, "got", splitVal.Len())
  56. }
  57. splitVal = value.SliceTo(2)
  58. if splitVal.Len() != 2 {
  59. t.Error("SliceTo: Expected len", 2, "got", splitVal.Len())
  60. }
  61. splitVal = value.SliceFromTo(1, 3)
  62. if splitVal.Len() != 2 {
  63. t.Error("SliceFromTo: Expected len", 2, "got", splitVal.Len())
  64. }
  65. }
  66. func TestLargeData(t *testing.T) {
  67. data := make([]byte, 100000)
  68. enc := Encode(data)
  69. value := NewValueFromBytes(enc)
  70. if value.Len() != len(data) {
  71. t.Error("Expected data to be", len(data), "got", value.Len())
  72. }
  73. }
  74. func TestValue(t *testing.T) {
  75. value := NewValueFromBytes([]byte("\xcd\x83dog\x83god\x83cat\x01"))
  76. if value.Get(0).Str() != "dog" {
  77. t.Errorf("expected '%v', got '%v'", value.Get(0).Str(), "dog")
  78. }
  79. if value.Get(3).Uint() != 1 {
  80. t.Errorf("expected '%v', got '%v'", value.Get(3).Uint(), 1)
  81. }
  82. }
  83. func TestEncode(t *testing.T) {
  84. strRes := "\x83dog"
  85. bytes := Encode("dog")
  86. str := string(bytes)
  87. if str != strRes {
  88. t.Errorf("Expected %q, got %q", strRes, str)
  89. }
  90. sliceRes := "\xcc\x83dog\x83god\x83cat"
  91. strs := []interface{}{"dog", "god", "cat"}
  92. bytes = Encode(strs)
  93. slice := string(bytes)
  94. if slice != sliceRes {
  95. t.Error("Expected %q, got %q", sliceRes, slice)
  96. }
  97. intRes := "\x82\x04\x00"
  98. bytes = Encode(1024)
  99. if string(bytes) != intRes {
  100. t.Errorf("Expected %q, got %q", intRes, bytes)
  101. }
  102. }
  103. func TestDecode(t *testing.T) {
  104. single := []byte("\x01")
  105. b, _ := Decode(single, 0)
  106. if b.(uint8) != 1 {
  107. t.Errorf("Expected 1, got %q", b)
  108. }
  109. str := []byte("\x83dog")
  110. b, _ = Decode(str, 0)
  111. if bytes.Compare(b.([]byte), []byte("dog")) != 0 {
  112. t.Errorf("Expected dog, got %q", b)
  113. }
  114. slice := []byte("\xcc\x83dog\x83god\x83cat")
  115. res := []interface{}{"dog", "god", "cat"}
  116. b, _ = Decode(slice, 0)
  117. if reflect.DeepEqual(b, res) {
  118. t.Errorf("Expected %q, got %q", res, b)
  119. }
  120. }
  121. func TestEncodeDecodeBigInt(t *testing.T) {
  122. bigInt := big.NewInt(1391787038)
  123. encoded := Encode(bigInt)
  124. value := NewValueFromBytes(encoded)
  125. if value.BigInt().Cmp(bigInt) != 0 {
  126. t.Errorf("Expected %v, got %v", bigInt, value.BigInt())
  127. }
  128. }
  129. func TestEncodeDecodeBytes(t *testing.T) {
  130. bv := NewValue([]interface{}{[]byte{1, 2, 3, 4, 5}, []byte{6}})
  131. b, _ := rlp.EncodeToBytes(bv)
  132. val := NewValueFromBytes(b)
  133. if !bv.Cmp(val) {
  134. t.Errorf("Expected %#v, got %#v", bv, val)
  135. }
  136. }
  137. func TestEncodeZero(t *testing.T) {
  138. b, _ := rlp.EncodeToBytes(NewValue(0))
  139. exp := []byte{0xc0}
  140. if bytes.Compare(b, exp) == 0 {
  141. t.Error("Expected", exp, "got", b)
  142. }
  143. }
  144. func BenchmarkEncodeDecode(b *testing.B) {
  145. for i := 0; i < b.N; i++ {
  146. bytes := Encode([]interface{}{"dog", "god", "cat"})
  147. Decode(bytes, 0)
  148. }
  149. }