github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/whisper/whisperv5/topic_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:51</date> 10 //</624342689495650304> 11 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 // 25 // 26 // 27 28 package whisperv5 29 30 import ( 31 "encoding/json" 32 "testing" 33 ) 34 35 var topicStringTests = []struct { 36 topic TopicType 37 str string 38 }{ 39 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, str: "0x00000000"}, 40 {topic: TopicType{0x00, 0x7f, 0x80, 0xff}, str: "0x007f80ff"}, 41 {topic: TopicType{0xff, 0x80, 0x7f, 0x00}, str: "0xff807f00"}, 42 {topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, str: "0xf26e7779"}, 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 // 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 // 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 } 146