This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
forked from NullHypothesis/zoossh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric_test.go
246 lines (200 loc) · 5.77 KB
/
generic_test.go
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Tests functions from "generic.go".
package zoossh
import (
"net"
"os"
"testing"
)
// Test the function ParseUnknownFile() on the input /dev/zero.
func TestParseUnknownFileZero(t *testing.T) {
_, err := ParseUnknownFile("/dev/zero")
if err == nil {
t.Errorf("ParseUnknownFile() failed to reject /dev/zero.")
}
}
// Test the function ParseUnknownFile().
func TestParseUnknownFile(t *testing.T) {
// Only run this test if the consensus file is there.
if _, err := os.Stat(consensusFile); os.IsNotExist(err) {
t.Skipf("skipping because of missing %s", consensusFile)
}
_, err := ParseUnknownFile(consensusFile)
if err != nil {
t.Errorf("ParseUnknownFile() failed to parse %s.", consensusFile)
}
}
func TestInterfaces(t *testing.T) {
testFingerprint := Fingerprint("9695DFC35FFEB861329B9F1AB04C46397020CE31")
if _, err := os.Stat(consensusFile); os.IsNotExist(err) {
t.Skipf("skipping because of missing %s", consensusFile)
}
if _, err := os.Stat(serverDescriptorFile); os.IsNotExist(err) {
t.Skipf("skipping because of missing %s", serverDescriptorFile)
}
consensus, err := ParseUnknownFile(consensusFile)
if err != nil {
t.Fatal(err)
}
descriptors, err := ParseUnknownFile(serverDescriptorFile)
if err != nil {
t.Fatal(err)
}
// Test the GetObject() function.
obj1, found := consensus.GetObject(testFingerprint)
if found != true {
t.Error("Could not find existing router status in consensus.")
}
obj2, found := descriptors.GetObject(testFingerprint)
if found != true {
t.Error("Could not find existing descriptor in descriptor set.")
}
// Test the GetFingerprint() function.
if obj1.GetFingerprint() != testFingerprint {
t.Error("Failed to retrieve correct fingerprint.")
}
if obj2.GetFingerprint() != testFingerprint {
t.Error("Failed to retrieve correct fingerprint.")
}
// Test the Length() function.
if consensus.Length() != numRouterStatuses {
t.Error("Failed to determine consensus length.")
}
if descriptors.Length() != 763 {
t.Error("Failed to determine descriptor set length.")
}
// Test the Iterate() function.
counter := 0
for _ = range consensus.Iterate(nil) {
counter += 1
}
if counter != consensus.Length() {
t.Error("Failed to iterate over all router statuses in consensus.")
}
counter = 0
for _ = range descriptors.Iterate(nil) {
counter += 1
}
if counter != descriptors.Length() {
t.Error("Failed to iterate over all descriptors in descriptor set.")
}
// Test the Merge() function.
prevLength := consensus.Length()
consensus.Merge(consensus)
if consensus.Length() != prevLength {
t.Error("Consensus merge with itself caused unexpected length.")
}
prevLength = descriptors.Length()
descriptors.Merge(descriptors)
if descriptors.Length() != prevLength {
t.Error("Descriptors merge with itself caused unexpected length.")
}
}
func TestIsEmpty(t *testing.T) {
filter := NewObjectFilter()
if !filter.IsEmpty() {
t.Error("Empty filter apparently not empty.")
}
filter.AddIPAddr(net.IP("1.2.3.4"))
if filter.IsEmpty() {
t.Error("Populated filter apparently empty.")
}
}
func TestFilterGetterSetter(t *testing.T) {
filter := NewObjectFilter()
fpr := Fingerprint("9B94CD0B7B8057EAF21BA7F023B7A1C8CA9CE645")
ipAddrs := net.IP("1.2.3.4")
nickname := "dummy-relay-nickname"
exists := filter.HasFingerprint(fpr)
if exists {
t.Error("Non-existing fingerprint apparently in filter.")
}
filter.AddFingerprint(fpr)
exists = filter.HasFingerprint(fpr)
if !exists {
t.Error("Existing fingerprint apparently not in filter.")
}
exists = filter.HasIPAddr(ipAddrs)
if exists {
t.Error("Non-existing IP address apparently in filter.")
}
filter.AddIPAddr(ipAddrs)
exists = filter.HasIPAddr(ipAddrs)
if !exists {
t.Error("Existing IP address apparently not in filter.")
}
exists = filter.HasNickname(nickname)
if exists {
t.Error("Non-existing nickname apparently in filter.")
}
filter.AddNickname(nickname)
exists = filter.HasNickname(nickname)
if !exists {
t.Error("Existing nickname apparently not in filter.")
}
}
func TestConsensusFiltering(t *testing.T) {
if _, err := os.Stat(consensusFile); os.IsNotExist(err) {
t.Skipf("skipping because of missing %s", consensusFile)
}
consensus, err := ParseConsensusFile(consensusFile)
if err != nil {
t.Fatal(err)
}
filter := NewObjectFilter()
filter.AddFingerprint(Fingerprint("9B94CD0B7B8057EAF21BA7F023B7A1C8CA9CE645"))
filter.AddFingerprint(Fingerprint("CCEF02AA454C0AB0FE1AC68304F6D8C4220C1912"))
count := 0
for _ = range consensus.Iterate(filter) {
count++
}
if count != 2 {
t.Error("Didn't filter correct amount of relays.")
}
count = 0
for _ = range consensus.Iterate(nil) {
count++
}
if count != numRouterStatuses {
t.Error("Processed unexpected number of router statuses.")
}
count = 0
for _ = range consensus.Iterate(NewObjectFilter()) {
count++
}
if count != numRouterStatuses {
t.Error("Processed unexpected number of router statuses.")
}
}
func TestDescriptorFiltering(t *testing.T) {
if _, err := os.Stat(serverDescriptorFile); os.IsNotExist(err) {
t.Skipf("skipping because of missing %s", serverDescriptorFile)
}
descriptors, err := ParseDescriptorFile(serverDescriptorFile)
if err != nil {
t.Fatal(err)
}
filter := NewObjectFilter()
filter.AddNickname("leenuts")
filter.AddNickname("manningsnowden2")
count := 0
for _ = range descriptors.Iterate(filter) {
count++
}
if count != 2 {
t.Error("Didn't filter correct amount of relays.")
}
count = 0
for _ = range descriptors.Iterate(nil) {
count++
}
if count != numServerDescriptors {
t.Error("Processed unexpected number of router descriptors.", count)
}
count = 0
for _ = range descriptors.Iterate(NewObjectFilter()) {
count++
}
if count != numServerDescriptors {
t.Error("Processed unexpected number of router descriptors.", count)
}
}