aboutsummaryrefslogtreecommitdiffstats
path: root/whisper/topic_test.go
blob: 22ee060966dccd72c2ff9396394e13f0ea25a3a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package whisper

import (
    "bytes"
    "testing"
)

var topicCreationTests = []struct {
    data []byte
    hash [4]byte
}{
    {hash: [4]byte{0xc5, 0xd2, 0x46, 0x01}, data: nil},
    {hash: [4]byte{0xc5, 0xd2, 0x46, 0x01}, data: []byte{}},
    {hash: [4]byte{0x8f, 0x9a, 0x2b, 0x7d}, data: []byte("test name")},
}

func TestTopicCreation(t *testing.T) {
    // Create the topics individually
    for i, tt := range topicCreationTests {
        topic := NewTopic(tt.data)
        if bytes.Compare(topic[:], tt.hash[:]) != 0 {
            t.Errorf("binary test %d: hash mismatch: have %v, want %v.", i, topic, tt.hash)
        }
    }
    for i, tt := range topicCreationTests {
        topic := NewTopicFromString(string(tt.data))
        if bytes.Compare(topic[:], tt.hash[:]) != 0 {
            t.Errorf("textual test %d: hash mismatch: have %v, want %v.", i, topic, tt.hash)
        }
    }
    // Create the topics in batches
    binaryData := make([][]byte, len(topicCreationTests))
    for i, tt := range topicCreationTests {
        binaryData[i] = tt.data
    }
    textualData := make([]string, len(topicCreationTests))
    for i, tt := range topicCreationTests {
        textualData[i] = string(tt.data)
    }

    topics := NewTopics(binaryData...)
    for i, tt := range topicCreationTests {
        if bytes.Compare(topics[i][:], tt.hash[:]) != 0 {
            t.Errorf("binary batch test %d: hash mismatch: have %v, want %v.", i, topics[i], tt.hash)
        }
    }
    topics = NewTopicsFromStrings(textualData...)
    for i, tt := range topicCreationTests {
        if bytes.Compare(topics[i][:], tt.hash[:]) != 0 {
            t.Errorf("textual batch test %d: hash mismatch: have %v, want %v.", i, topics[i], tt.hash)
        }
    }
}

var topicMatcherCreationTest = struct {
    binary  [][][]byte
    textual [][]string
    matcher []map[[4]byte]struct{}
}{
    binary: [][][]byte{
        [][]byte{},
        [][]byte{
            []byte("Topic A"),
        },
        [][]byte{
            []byte("Topic B1"),
            []byte("Topic B2"),
            []byte("Topic B3"),
        },
    },
    textual: [][]string{
        []string{},
        []string{"Topic A"},
        []string{"Topic B1", "Topic B2", "Topic B3"},
    },
    matcher: []map[[4]byte]struct{}{
        map[[4]byte]struct{}{},
        map[[4]byte]struct{}{
            [4]byte{0x25, 0xfc, 0x95, 0x66}: struct{}{},
        },
        map[[4]byte]struct{}{
            [4]byte{0x93, 0x6d, 0xec, 0x09}: struct{}{},
            [4]byte{0x25, 0x23, 0x34, 0xd3}: struct{}{},
            [4]byte{0x6b, 0xc2, 0x73, 0xd1}: struct{}{},
        },
    },
}

func TestTopicMatcherCreation(t *testing.T) {
    test := topicMatcherCreationTest

    matcher := newTopicMatcherFromBinary(test.binary...)
    for i, cond := range matcher.conditions {
        for topic, _ := range cond {
            if _, ok := test.matcher[i][topic]; !ok {
                t.Errorf("condition %d; extra topic found: 0x%x", i, topic[:])
            }
        }
    }
    for i, cond := range test.matcher {
        for topic, _ := range cond {
            if _, ok := matcher.conditions[i][topic]; !ok {
                t.Errorf("condition %d; topic not found: 0x%x", i, topic[:])
            }
        }
    }

    matcher = newTopicMatcherFromStrings(test.textual...)
    for i, cond := range matcher.conditions {
        for topic, _ := range cond {
            if _, ok := test.matcher[i][topic]; !ok {
                t.Errorf("condition %d; extra topic found: 0x%x", i, topic[:])
            }
        }
    }
    for i, cond := range test.matcher {
        for topic, _ := range cond {
            if _, ok := matcher.conditions[i][topic]; !ok {
                t.Errorf("condition %d; topic not found: 0x%x", i, topic[:])
            }
        }
    }
}

var topicMatcherTests = []struct {
    filter [][]string
    topics []string
    match  bool
}{
    // Empty topic matcher should match everything
    {
        filter: [][]string{},
        topics: []string{},
        match:  true,
    },
    {
        filter: [][]string{},
        topics: []string{"a", "b", "c"},
        match:  true,
    },
    // Fixed topic matcher should match strictly, but only prefix
    {
        filter: [][]string{[]string{"a"}, []string{"b"}},
        topics: []string{"a"},
        match:  false,
    },
    {
        filter: [][]string{[]string{"a"}, []string{"b"}},
        topics: []string{"a", "b"},
        match:  true,
    },
    {
        filter: [][]string{[]string{"a"}, []string{"b"}},
        topics: []string{"a", "b", "c"},
        match:  true,
    },
    // Multi-matcher should match any from a sub-group
    {
        filter: [][]string{[]string{"a1", "a2"}},
        topics: []string{"a"},
        match:  false,
    },
    {
        filter: [][]string{[]string{"a1", "a2"}},
        topics: []string{"a1"},
        match:  true,
    },
    {
        filter: [][]string{[]string{"a1", "a2"}},
        topics: []string{"a2"},
        match:  true,
    },
    // Wild-card condition should match anything
    {
        filter: [][]string{[]string{}, []string{"b"}},
        topics: []string{"a"},
        match:  false,
    },
    {
        filter: [][]string{[]string{}, []string{"b"}},
        topics: []string{"a", "b"},
        match:  true,
    },
    {
        filter: [][]string{[]string{}, []string{"b"}},
        topics: []string{"b", "b"},
        match:  true,
    },
}

func TestTopicMatcher(t *testing.T) {
    for i, tt := range topicMatcherTests {
        topics := NewTopicsFromStrings(tt.topics...)

        matcher := newTopicMatcherFromStrings(tt.filter...)
        if match := matcher.Matches(topics); match != tt.match {
            t.Errorf("test %d: match mismatch: have %v, want %v", i, match, tt.match)
        }
    }
}