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