topic_test.go 4.6 KB

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