github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/whisper/whisperv5/topic_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 // 10 // 11 // 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 25 package whisperv5 26 27 import ( 28 "encoding/json" 29 "testing" 30 ) 31 32 var topicStringTests = []struct { 33 topic TopicType 34 str string 35 }{ 36 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, str: "0x00000000"}, 37 {topic: TopicType{0x00, 0x7f, 0x80, 0xff}, str: "0x007f80ff"}, 38 {topic: TopicType{0xff, 0x80, 0x7f, 0x00}, str: "0xff807f00"}, 39 {topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, str: "0xf26e7779"}, 40 } 41 42 func TestTopicString(t *testing.T) { 43 for i, tst := range topicStringTests { 44 s := tst.topic.String() 45 if s != tst.str { 46 t.Fatalf("failed test %d: have %s, want %s.", i, s, tst.str) 47 } 48 } 49 } 50 51 var bytesToTopicTests = []struct { 52 data []byte 53 topic TopicType 54 }{ 55 {topic: TopicType{0x8f, 0x9a, 0x2b, 0x7d}, data: []byte{0x8f, 0x9a, 0x2b, 0x7d}}, 56 {topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte{0x00, 0x7f, 0x80, 0xff}}, 57 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00, 0x00}}, 58 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00}}, 59 {topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte{0x01}}, 60 {topic: TopicType{0x00, 0xfe, 0x00, 0x00}, data: []byte{0x00, 0xfe}}, 61 {topic: TopicType{0xea, 0x1d, 0x43, 0x00}, data: []byte{0xea, 0x1d, 0x43}}, 62 {topic: TopicType{0x6f, 0x3c, 0xb0, 0xdd}, data: []byte{0x6f, 0x3c, 0xb0, 0xdd, 0x0f, 0x00, 0x90}}, 63 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{}}, 64 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: nil}, 65 } 66 67 var unmarshalTestsGood = []struct { 68 topic TopicType 69 data []byte 70 }{ 71 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x00000000"`)}, 72 {topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte(`"0x007f80ff"`)}, 73 {topic: TopicType{0xff, 0x80, 0x7f, 0x00}, data: []byte(`"0xff807f00"`)}, 74 {topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, data: []byte(`"0xf26e7779"`)}, 75 } 76 77 var unmarshalTestsBad = []struct { 78 topic TopicType 79 data []byte 80 }{ 81 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x000000"`)}, 82 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x0000000"`)}, 83 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x000000000"`)}, 84 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0x0000000000"`)}, 85 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"000000"`)}, 86 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0000000"`)}, 87 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"000000000"`)}, 88 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"0000000000"`)}, 89 {topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte(`"abcdefg0"`)}, 90 } 91 92 var unmarshalTestsUgly = []struct { 93 topic TopicType 94 data []byte 95 }{ 96 {topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte(`"0x00000001"`)}, 97 } 98 99 func TestBytesToTopic(t *testing.T) { 100 for i, tst := range bytesToTopicTests { 101 top := BytesToTopic(tst.data) 102 if top != tst.topic { 103 t.Fatalf("failed test %d: have %v, want %v.", i, t, tst.topic) 104 } 105 } 106 } 107 108 func TestUnmarshalTestsGood(t *testing.T) { 109 for i, tst := range unmarshalTestsGood { 110 var top TopicType 111 err := json.Unmarshal(tst.data, &top) 112 if err != nil { 113 t.Errorf("failed test %d. input: %v. err: %v", i, tst.data, err) 114 } else if top != tst.topic { 115 t.Errorf("failed test %d: have %v, want %v.", i, t, tst.topic) 116 } 117 } 118 } 119 120 func TestUnmarshalTestsBad(t *testing.T) { 121 // 122 for i, tst := range unmarshalTestsBad { 123 var top TopicType 124 err := json.Unmarshal(tst.data, &top) 125 if err == nil { 126 t.Fatalf("failed test %d. input: %v.", i, tst.data) 127 } 128 } 129 } 130 131 func TestUnmarshalTestsUgly(t *testing.T) { 132 // 133 for i, tst := range unmarshalTestsUgly { 134 var top TopicType 135 err := json.Unmarshal(tst.data, &top) 136 if err != nil { 137 t.Errorf("failed test %d. input: %v.", i, tst.data) 138 } else if top == tst.topic { 139 t.Errorf("failed test %d: have %v, want %v.", i, top, tst.topic) 140 } 141 } 142 }