github.com/status-im/status-go@v1.1.0/wakuv2/common/topic_test.go (about) 1 // Copyright 2019 The Waku Library Authors. 2 // 3 // The Waku library is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Lesser General Public License as published by 5 // the Free Software Foundation, either version 3 of the License, or 6 // (at your option) any later version. 7 // 8 // The Waku library is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty off 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Lesser General Public License for more details. 12 // 13 // You should have received a copy of the GNU Lesser General Public License 14 // along with the Waku library. If not, see <http://www.gnu.org/licenses/>. 15 // 16 // This software uses the go-ethereum library, which is licensed 17 // under the GNU Lesser General Public Library, version 3 or any later. 18 19 package common 20 21 import ( 22 "encoding/json" 23 "testing" 24 25 "github.com/stretchr/testify/require" 26 ) 27 28 var topicStringTests = []struct { 29 topic TopicType 30 str string 31 }{ 32 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, str: "0x00000000"}, 33 {topic: TopicType{0x00, 0x7f, 0x80, 0xff}, str: "0x007f80ff"}, 34 {topic: TopicType{0xff, 0x80, 0x7f, 0x00}, str: "0xff807f00"}, 35 {topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, str: "0xf26e7779"}, 36 } 37 38 func TestTopicSet(t *testing.T) { 39 40 tSet := NewTopicSet([]TopicType{{0x00, 0x00, 0x00, 0x00}, {0x00, 0x7f, 0x80, 0xff}}) 41 topics := tSet.ContentTopics() 42 require.Equal(t, len(topics), 2) 43 } 44 45 func TestTopicString(t *testing.T) { 46 for i, tst := range topicStringTests { 47 s := tst.topic.String() 48 if s != tst.str { 49 t.Fatalf("failed test %d: have %s, want %s.", i, s, tst.str) 50 } 51 } 52 } 53 54 var bytesToTopicTests = []struct { 55 data []byte 56 topic TopicType 57 }{ 58 {topic: TopicType{0x8f, 0x9a, 0x2b, 0x7d}, data: []byte{0x8f, 0x9a, 0x2b, 0x7d}}, 59 {topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte{0x00, 0x7f, 0x80, 0xff}}, 60 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00, 0x00}}, 61 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00}}, 62 {topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte{0x01}}, 63 {topic: TopicType{0x00, 0xfe, 0x00, 0x00}, data: []byte{0x00, 0xfe}}, 64 {topic: TopicType{0xea, 0x1d, 0x43, 0x00}, data: []byte{0xea, 0x1d, 0x43}}, 65 {topic: TopicType{0x6f, 0x3c, 0xb0, 0xdd}, data: []byte{0x6f, 0x3c, 0xb0, 0xdd, 0x0f, 0x00, 0x90}}, 66 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{}}, 67 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: nil}, 68 } 69 70 var unmarshalTestsGood = []struct { 71 topic TopicType 72 data []byte 73 }{ 74 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x00000000"`)}, 75 {topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte(`"0x007f80ff"`)}, 76 {topic: TopicType{0xff, 0x80, 0x7f, 0x00}, data: []byte(`"0xff807f00"`)}, 77 {topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, data: []byte(`"0xf26e7779"`)}, 78 } 79 80 var unmarshalTestsBad = []struct { 81 topic TopicType 82 data []byte 83 }{ 84 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x000000"`)}, 85 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x0000000"`)}, 86 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x000000000"`)}, 87 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x0000000000"`)}, 88 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"000000"`)}, 89 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0000000"`)}, 90 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"000000000"`)}, 91 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0000000000"`)}, 92 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"abcdefg0"`)}, 93 } 94 95 var unmarshalTestsUgly = []struct { 96 topic TopicType 97 data []byte 98 }{ 99 {topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte(`"0x00000001"`)}, 100 } 101 102 func TestBytesToTopic(t *testing.T) { 103 for i, tst := range bytesToTopicTests { 104 top := BytesToTopic(tst.data) 105 if top != tst.topic { 106 t.Fatalf("failed test %d: have %v, want %v.", i, t, tst.topic) 107 } 108 } 109 } 110 111 func TestUnmarshalTestsGood(t *testing.T) { 112 for i, tst := range unmarshalTestsGood { 113 var top TopicType 114 err := json.Unmarshal(tst.data, &top) 115 if err != nil { 116 t.Errorf("failed test %d. input: %v. err: %v", i, tst.data, err) 117 } else if top != tst.topic { 118 t.Errorf("failed test %d: have %v, want %v.", i, t, tst.topic) 119 } 120 } 121 } 122 123 func TestUnmarshalTestsBad(t *testing.T) { 124 // in this test UnmarshalJSON() is supposed to fail 125 for i, tst := range unmarshalTestsBad { 126 var top TopicType 127 err := json.Unmarshal(tst.data, &top) 128 if err == nil { 129 t.Fatalf("failed test %d. input: %v.", i, tst.data) 130 } 131 } 132 } 133 134 func TestUnmarshalTestsUgly(t *testing.T) { 135 // in this test UnmarshalJSON() is NOT supposed to fail, but result should be wrong 136 for i, tst := range unmarshalTestsUgly { 137 var top TopicType 138 err := json.Unmarshal(tst.data, &top) 139 if err != nil { 140 t.Errorf("failed test %d. input: %v.", i, tst.data) 141 } else if top == tst.topic { 142 t.Errorf("failed test %d: have %v, want %v.", i, top, tst.topic) 143 } 144 } 145 }