topic_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2016 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 whisperv5
  17. import "testing"
  18. var topicStringTests = []struct {
  19. topic TopicType
  20. str string
  21. }{
  22. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, str: "0x00000000"},
  23. {topic: TopicType{0x00, 0x7f, 0x80, 0xff}, str: "0x007f80ff"},
  24. {topic: TopicType{0xff, 0x80, 0x7f, 0x00}, str: "0xff807f00"},
  25. {topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, str: "0xf26e7779"},
  26. }
  27. func TestTopicString(t *testing.T) {
  28. for i, tst := range topicStringTests {
  29. s := tst.topic.String()
  30. if s != tst.str {
  31. t.Fatalf("failed test %d: have %s, want %s.", i, s, tst.str)
  32. }
  33. }
  34. }
  35. var bytesToTopicTests = []struct {
  36. data []byte
  37. topic TopicType
  38. }{
  39. {topic: TopicType{0x8f, 0x9a, 0x2b, 0x7d}, data: []byte{0x8f, 0x9a, 0x2b, 0x7d}},
  40. {topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte{0x00, 0x7f, 0x80, 0xff}},
  41. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00, 0x00}},
  42. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00}},
  43. {topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte{0x01}},
  44. {topic: TopicType{0x00, 0xfe, 0x00, 0x00}, data: []byte{0x00, 0xfe}},
  45. {topic: TopicType{0xea, 0x1d, 0x43, 0x00}, data: []byte{0xea, 0x1d, 0x43}},
  46. {topic: TopicType{0x6f, 0x3c, 0xb0, 0xdd}, data: []byte{0x6f, 0x3c, 0xb0, 0xdd, 0x0f, 0x00, 0x90}},
  47. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{}},
  48. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: nil},
  49. }
  50. func TestBytesToTopic(t *testing.T) {
  51. for i, tst := range bytesToTopicTests {
  52. top := BytesToTopic(tst.data)
  53. if top != tst.topic {
  54. t.Fatalf("failed test %d: have %v, want %v.", i, t, tst.topic)
  55. }
  56. }
  57. }
  58. var unmarshalTestsGood = []struct {
  59. topic TopicType
  60. data []byte
  61. }{
  62. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0x00000000")},
  63. {topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte("0x007f80ff")},
  64. {topic: TopicType{0xff, 0x80, 0x7f, 0x00}, data: []byte("0xff807f00")},
  65. {topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, data: []byte("0xf26e7779")},
  66. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("00000000")},
  67. {topic: TopicType{0x00, 0x80, 0x01, 0x00}, data: []byte("00800100")},
  68. {topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte("007f80ff")},
  69. {topic: TopicType{0xff, 0x80, 0x7f, 0x00}, data: []byte("ff807f00")},
  70. {topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, data: []byte("f26e7779")},
  71. }
  72. var unmarshalTestsBad = []struct {
  73. topic TopicType
  74. data []byte
  75. }{
  76. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0x000000")},
  77. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0x0000000")},
  78. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0x000000000")},
  79. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0x0000000000")},
  80. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("000000")},
  81. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0000000")},
  82. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("000000000")},
  83. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0000000000")},
  84. {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("abcdefg0")},
  85. }
  86. var unmarshalTestsUgly = []struct {
  87. topic TopicType
  88. data []byte
  89. }{
  90. {topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte("00000001")},
  91. }
  92. func TestUnmarshalTestsGood(t *testing.T) {
  93. for i, tst := range unmarshalTestsGood {
  94. var top TopicType
  95. err := top.UnmarshalJSON(tst.data)
  96. if err != nil {
  97. t.Fatalf("failed test %d. input: %v.", i, tst.data)
  98. } else if top != tst.topic {
  99. t.Fatalf("failed test %d: have %v, want %v.", i, t, tst.topic)
  100. }
  101. }
  102. }
  103. func TestUnmarshalTestsBad(t *testing.T) {
  104. // in this test UnmarshalJSON() is supposed to fail
  105. for i, tst := range unmarshalTestsBad {
  106. var top TopicType
  107. err := top.UnmarshalJSON(tst.data)
  108. if err == nil {
  109. t.Fatalf("failed test %d. input: %v.", i, tst.data)
  110. }
  111. }
  112. }
  113. func TestUnmarshalTestsUgly(t *testing.T) {
  114. // in this test UnmarshalJSON() is NOT supposed to fail, but result should be wrong
  115. for i, tst := range unmarshalTestsUgly {
  116. var top TopicType
  117. err := top.UnmarshalJSON(tst.data)
  118. if err != nil {
  119. t.Fatalf("failed test %d. input: %v.", i, tst.data)
  120. } else if top == tst.topic {
  121. t.Fatalf("failed test %d: have %v, want %v.", i, top, tst.topic)
  122. }
  123. }
  124. }