rlp_test.go 3.6 KB

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