github.com/ylsgit/go-ethereum@v1.6.5/whisper/whisperv5/topic_test.go (about) 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 17 package whisperv5 18 19 import "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 31 func TestTopicString(t *testing.T) { 32 for i, tst := range topicStringTests { 33 s := tst.topic.String() 34 if s != tst.str { 35 t.Fatalf("failed test %d: have %s, want %s.", i, s, tst.str) 36 } 37 } 38 } 39 40 var bytesToTopicTests = []struct { 41 data []byte 42 topic TopicType 43 }{ 44 {topic: TopicType{0x8f, 0x9a, 0x2b, 0x7d}, data: []byte{0x8f, 0x9a, 0x2b, 0x7d}}, 45 {topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte{0x00, 0x7f, 0x80, 0xff}}, 46 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00, 0x00}}, 47 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00}}, 48 {topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte{0x01}}, 49 {topic: TopicType{0x00, 0xfe, 0x00, 0x00}, data: []byte{0x00, 0xfe}}, 50 {topic: TopicType{0xea, 0x1d, 0x43, 0x00}, data: []byte{0xea, 0x1d, 0x43}}, 51 {topic: TopicType{0x6f, 0x3c, 0xb0, 0xdd}, data: []byte{0x6f, 0x3c, 0xb0, 0xdd, 0x0f, 0x00, 0x90}}, 52 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{}}, 53 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: nil}, 54 } 55 56 func TestBytesToTopic(t *testing.T) { 57 for i, tst := range bytesToTopicTests { 58 top := BytesToTopic(tst.data) 59 if top != tst.topic { 60 t.Fatalf("failed test %d: have %v, want %v.", i, t, tst.topic) 61 } 62 } 63 } 64 65 var unmarshalTestsGood = []struct { 66 topic TopicType 67 data []byte 68 }{ 69 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0x00000000")}, 70 {topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte("0x007f80ff")}, 71 {topic: TopicType{0xff, 0x80, 0x7f, 0x00}, data: []byte("0xff807f00")}, 72 {topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, data: []byte("0xf26e7779")}, 73 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("00000000")}, 74 {topic: TopicType{0x00, 0x80, 0x01, 0x00}, data: []byte("00800100")}, 75 {topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte("007f80ff")}, 76 {topic: TopicType{0xff, 0x80, 0x7f, 0x00}, data: []byte("ff807f00")}, 77 {topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, data: []byte("f26e7779")}, 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("00000001")}, 100 } 101 102 func TestUnmarshalTestsGood(t *testing.T) { 103 for i, tst := range unmarshalTestsGood { 104 var top TopicType 105 err := top.UnmarshalJSON(tst.data) 106 if err != nil { 107 t.Fatalf("failed test %d. input: %v.", i, tst.data) 108 } else if top != tst.topic { 109 t.Fatalf("failed test %d: have %v, want %v.", i, t, tst.topic) 110 } 111 } 112 } 113 114 func TestUnmarshalTestsBad(t *testing.T) { 115 // in this test UnmarshalJSON() is supposed to fail 116 for i, tst := range unmarshalTestsBad { 117 var top TopicType 118 err := top.UnmarshalJSON(tst.data) 119 if err == nil { 120 t.Fatalf("failed test %d. input: %v.", i, tst.data) 121 } 122 } 123 } 124 125 func TestUnmarshalTestsUgly(t *testing.T) { 126 // in this test UnmarshalJSON() is NOT supposed to fail, but result should be wrong 127 for i, tst := range unmarshalTestsUgly { 128 var top TopicType 129 err := top.UnmarshalJSON(tst.data) 130 if err != nil { 131 t.Fatalf("failed test %d. input: %v.", i, tst.data) 132 } else if top == tst.topic { 133 t.Fatalf("failed test %d: have %v, want %v.", i, top, tst.topic) 134 } 135 } 136 }