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