test_results.py 10.5 KB
Newer Older
Matt Traudt's avatar
Matt Traudt committed
1
from unittest.mock import patch
2
from sbws.globals import RESULT_VERSION
Matt Traudt's avatar
Matt Traudt committed
3
4
5
6
7
8
9
from sbws.lib.resultdump import Result
from sbws.lib.resultdump import ResultSuccess
from sbws.lib.resultdump import ResultError
from sbws.lib.resultdump import ResultErrorAuth
from sbws.lib.resultdump import ResultErrorCircuit
from sbws.lib.resultdump import ResultErrorStream
from sbws.lib.resultdump import _ResultType
10
from tests.unit.globals import monotonic_time
Matt Traudt's avatar
Matt Traudt committed
11
12


Matt Traudt's avatar
Matt Traudt committed
13
14
@patch('time.time')
def test_Result(time_mock):
Matt Traudt's avatar
Matt Traudt committed
15
16
17
18
    '''
    A standard Result should not be convertible to a string because Result.type
    is not implemented.
    '''
Matt Traudt's avatar
Matt Traudt committed
19
    time_mock.side_effect = monotonic_time()
Matt Traudt's avatar
Matt Traudt committed
20
21
22
    fp1 = 'A' * 40
    fp2 = 'Z' * 40
    circ = [fp1, fp2]
Matt Traudt's avatar
Matt Traudt committed
23
    dest_url = 'http://example.com/sbws.bin'
Matt Traudt's avatar
Matt Traudt committed
24
    scanner_nick = 'sbwsscanner'
Matt Traudt's avatar
Matt Traudt committed
25
26
27
    nick = 'Mooooooo'
    relay_ip = '169.254.100.1'
    relay = Result.Relay(fp1, nick, relay_ip)
Matt Traudt's avatar
Matt Traudt committed
28
    r = Result(relay, circ, dest_url, scanner_nick)
Matt Traudt's avatar
Matt Traudt committed
29
30
31
32
33
34
35
36
37
38
39
40
41
    try:
        str(r)
    except NotImplementedError:
        pass
    else:
        assert None, 'Should have failed'


def test_Result_from_dict_bad_version():
    '''
    The first thing that is checked is the version field, and a wrong one
    should return None
    '''
42
    d = {'version': RESULT_VERSION + 1}
Matt Traudt's avatar
Matt Traudt committed
43
44
45
46
47
48
49
50
51
    r = Result.from_dict(d)
    assert r is None


def test_Result_from_dict_bad_type():
    '''
    If the result type string doesn't match any of the known types, then it
    should throw NotImplementedError
    '''
52
    d = {'version': RESULT_VERSION, 'type': 'NotARealType'}
Matt Traudt's avatar
Matt Traudt committed
53
54
55
56
57
58
59
60
    try:
        Result.from_dict(d)
    except NotImplementedError as e:
        assert str(e) == 'Unknown result type NotARealType'
    else:
        assert None, 'Should have failed'


Matt Traudt's avatar
Matt Traudt committed
61
62
@patch('time.time')
def test_ResultSuccess(time_mock):
Matt Traudt's avatar
Matt Traudt committed
63
    t = 2000
Matt Traudt's avatar
Matt Traudt committed
64
    time_mock.side_effect = monotonic_time(start=t)
Matt Traudt's avatar
Matt Traudt committed
65
66
67
    fp1 = 'A' * 40
    fp2 = 'Z' * 40
    circ = [fp1, fp2]
Matt Traudt's avatar
Matt Traudt committed
68
    dest_url = 'http://example.com/sbws.bin'
Matt Traudt's avatar
Matt Traudt committed
69
    scanner_nick = 'sbwsscanner'
Matt Traudt's avatar
Matt Traudt committed
70
71
72
73
74
    nick = 'Mooooooo'
    relay_ip = '169.254.100.1'
    relay = Result.Relay(fp1, nick, relay_ip)
    rtts = [5, 25]
    downloads = [{'duration': 4, 'amount': 40}]
Matt Traudt's avatar
Matt Traudt committed
75
76
    r1 = ResultSuccess(rtts, downloads, relay, circ, dest_url, scanner_nick)
    r2 = ResultSuccess(rtts, downloads, relay, circ, dest_url, scanner_nick,
Matt Traudt's avatar
Matt Traudt committed
77
78
79
80
81
82
                       t=t)
    assert r1.downloads == downloads
    assert r1.rtts == rtts
    assert r1.nickname == nick
    assert r1.time == t
    assert r1.fingerprint == fp1
Matt Traudt's avatar
Matt Traudt committed
83
    assert r1.scanner == scanner_nick
Matt Traudt's avatar
Matt Traudt committed
84
85
86
    assert r1.type == _ResultType.Success
    assert r1.address == relay_ip
    assert r1.circ == circ
Matt Traudt's avatar
Matt Traudt committed
87
    assert r1.dest_url == dest_url
88
    assert r1.version == RESULT_VERSION
Matt Traudt's avatar
Matt Traudt committed
89
90
91
    assert str(r1) == str(r2)


Matt Traudt's avatar
Matt Traudt committed
92
93
@patch('time.time')
def test_ResultSuccess_from_dict(time_mock):
Matt Traudt's avatar
Matt Traudt committed
94
    t = 2000
Matt Traudt's avatar
Matt Traudt committed
95
    time_mock.side_effect = monotonic_time(start=t)
Matt Traudt's avatar
Matt Traudt committed
96
97
98
    fp1 = 'A' * 40
    fp2 = 'Z' * 40
    circ = [fp1, fp2]
Matt Traudt's avatar
Matt Traudt committed
99
    dest_url = 'http://example.com/sbws.bin'
Matt Traudt's avatar
Matt Traudt committed
100
    scanner_nick = 'sbwsscanner'
Matt Traudt's avatar
Matt Traudt committed
101
102
103
104
105
    nick = 'Mooooooo'
    relay_ip = '169.254.100.1'
    relay = Result.Relay(fp1, nick, relay_ip)
    rtts = [5, 25]
    downloads = [{'duration': 4, 'amount': 40}]
Matt Traudt's avatar
Matt Traudt committed
106
    r1 = ResultSuccess(rtts, downloads, relay, circ, dest_url, scanner_nick)
Matt Traudt's avatar
Matt Traudt committed
107
108
109
    d = {
        'rtts': rtts, 'downloads': downloads, 'fingerprint': fp1,
        'nickname': nick, 'address': relay_ip, 'circ': circ,
Matt Traudt's avatar
Matt Traudt committed
110
        'dest_url': dest_url, 'scanner': scanner_nick,
111
        'version': RESULT_VERSION, 'type': _ResultType.Success, 'time': t,
Matt Traudt's avatar
Matt Traudt committed
112
113
114
115
116
117
118
    }
    r2 = Result.from_dict(d)
    assert isinstance(r1, ResultSuccess)
    assert isinstance(r2, ResultSuccess)
    assert str(r1) == str(r2)


Matt Traudt's avatar
Matt Traudt committed
119
120
@patch('time.time')
def test_ResultError(time_mock):
Matt Traudt's avatar
Matt Traudt committed
121
    t = 2000
Matt Traudt's avatar
Matt Traudt committed
122
    time_mock.side_effect = monotonic_time(start=t)
Matt Traudt's avatar
Matt Traudt committed
123
124
125
    fp1 = 'A' * 40
    fp2 = 'Z' * 40
    circ = [fp1, fp2]
Matt Traudt's avatar
Matt Traudt committed
126
    dest_url = 'http://example.com/sbws.bin'
Matt Traudt's avatar
Matt Traudt committed
127
    scanner_nick = 'sbwsscanner'
Matt Traudt's avatar
Matt Traudt committed
128
129
130
131
    nick = 'Mooooooo'
    relay_ip = '169.254.100.1'
    relay = Result.Relay(fp1, nick, relay_ip)
    msg = 'aaaaayyyyyy bb'
Matt Traudt's avatar
Matt Traudt committed
132
133
    r1 = ResultError(relay, circ, dest_url, scanner_nick, msg=msg)
    r2 = ResultError(relay, circ, dest_url, scanner_nick, msg=msg, t=t)
Matt Traudt's avatar
Matt Traudt committed
134
135
136
137
    assert r1.msg == msg
    assert r1.nickname == nick
    assert r1.time == t
    assert r1.fingerprint == fp1
Matt Traudt's avatar
Matt Traudt committed
138
    assert r1.scanner == scanner_nick
Matt Traudt's avatar
Matt Traudt committed
139
140
141
    assert r1.type == _ResultType.Error
    assert r1.address == relay_ip
    assert r1.circ == circ
Matt Traudt's avatar
Matt Traudt committed
142
    assert r1.dest_url == dest_url
143
    assert r1.version == RESULT_VERSION
Matt Traudt's avatar
Matt Traudt committed
144
145
146
    assert str(r1) == str(r2)


Matt Traudt's avatar
Matt Traudt committed
147
148
@patch('time.time')
def test_ResultError_from_dict(time_mock):
Matt Traudt's avatar
Matt Traudt committed
149
    t = 2000
Matt Traudt's avatar
Matt Traudt committed
150
    time_mock.side_effect = monotonic_time(start=t)
Matt Traudt's avatar
Matt Traudt committed
151
152
153
    fp1 = 'A' * 40
    fp2 = 'Z' * 40
    circ = [fp1, fp2]
Matt Traudt's avatar
Matt Traudt committed
154
    dest_url = 'http://example.com/sbws.bin'
Matt Traudt's avatar
Matt Traudt committed
155
    scanner_nick = 'sbwsscanner'
Matt Traudt's avatar
Matt Traudt committed
156
157
158
159
    nick = 'Mooooooo'
    relay_ip = '169.254.100.1'
    relay = Result.Relay(fp1, nick, relay_ip)
    msg = 'aaaaayyyyyy bb'
Matt Traudt's avatar
Matt Traudt committed
160
    r1 = ResultError(relay, circ, dest_url, scanner_nick, msg=msg)
Matt Traudt's avatar
Matt Traudt committed
161
162
163
    d = {
        'msg': msg, 'fingerprint': fp1,
        'nickname': nick, 'address': relay_ip, 'circ': circ,
Matt Traudt's avatar
Matt Traudt committed
164
        'dest_url': dest_url, 'scanner': scanner_nick,
165
        'version': RESULT_VERSION, 'type': _ResultType.Error, 'time': t,
Matt Traudt's avatar
Matt Traudt committed
166
167
168
169
170
171
172
    }
    r2 = Result.from_dict(d)
    assert isinstance(r1, ResultError)
    assert isinstance(r2, ResultError)
    assert str(r1) == str(r2)


Matt Traudt's avatar
Matt Traudt committed
173
174
@patch('time.time')
def test_ResultErrorCircuit(time_mock):
Matt Traudt's avatar
Matt Traudt committed
175
    t = 2000
Matt Traudt's avatar
Matt Traudt committed
176
    time_mock.side_effect = monotonic_time(start=t)
Matt Traudt's avatar
Matt Traudt committed
177
178
179
    fp1 = 'A' * 40
    fp2 = 'Z' * 40
    circ = [fp1, fp2]
Matt Traudt's avatar
Matt Traudt committed
180
    dest_url = 'http://example.com/sbws.bin'
Matt Traudt's avatar
Matt Traudt committed
181
    scanner_nick = 'sbwsscanner'
Matt Traudt's avatar
Matt Traudt committed
182
183
184
185
    nick = 'Mooooooo'
    relay_ip = '169.254.100.1'
    relay = Result.Relay(fp1, nick, relay_ip)
    msg = 'aaaaayyyyyy bb'
Matt Traudt's avatar
Matt Traudt committed
186
187
    r1 = ResultErrorCircuit(relay, circ, dest_url, scanner_nick, msg=msg)
    r2 = ResultErrorCircuit(relay, circ, dest_url, scanner_nick, msg=msg,
Matt Traudt's avatar
Matt Traudt committed
188
189
190
191
192
                            t=t)
    assert r1.msg == msg
    assert r1.nickname == nick
    assert r1.time == t
    assert r1.fingerprint == fp1
Matt Traudt's avatar
Matt Traudt committed
193
    assert r1.scanner == scanner_nick
Matt Traudt's avatar
Matt Traudt committed
194
195
196
    assert r1.type == _ResultType.ErrorCircuit
    assert r1.address == relay_ip
    assert r1.circ == circ
Matt Traudt's avatar
Matt Traudt committed
197
    assert r1.dest_url == dest_url
198
    assert r1.version == RESULT_VERSION
Matt Traudt's avatar
Matt Traudt committed
199
200
201
    assert str(r1) == str(r2)


Matt Traudt's avatar
Matt Traudt committed
202
203
@patch('time.time')
def test_ResultErrorCircuit_from_dict(time_mock):
Matt Traudt's avatar
Matt Traudt committed
204
    t = 2000
Matt Traudt's avatar
Matt Traudt committed
205
    time_mock.side_effect = monotonic_time(start=t)
Matt Traudt's avatar
Matt Traudt committed
206
207
208
    fp1 = 'A' * 40
    fp2 = 'Z' * 40
    circ = [fp1, fp2]
Matt Traudt's avatar
Matt Traudt committed
209
    dest_url = 'http://example.com/sbws.bin'
Matt Traudt's avatar
Matt Traudt committed
210
    scanner_nick = 'sbwsscanner'
Matt Traudt's avatar
Matt Traudt committed
211
212
213
214
    nick = 'Mooooooo'
    relay_ip = '169.254.100.1'
    relay = Result.Relay(fp1, nick, relay_ip)
    msg = 'aaaaayyyyyy bb'
Matt Traudt's avatar
Matt Traudt committed
215
    r1 = ResultErrorCircuit(relay, circ, dest_url, scanner_nick, msg=msg)
Matt Traudt's avatar
Matt Traudt committed
216
217
218
    d = {
        'msg': msg, 'fingerprint': fp1,
        'nickname': nick, 'address': relay_ip, 'circ': circ,
Matt Traudt's avatar
Matt Traudt committed
219
        'dest_url': dest_url, 'scanner': scanner_nick,
220
        'version': RESULT_VERSION, 'type': _ResultType.ErrorCircuit, 'time': t,
Matt Traudt's avatar
Matt Traudt committed
221
222
223
224
225
226
227
    }
    r2 = Result.from_dict(d)
    assert isinstance(r1, ResultErrorCircuit)
    assert isinstance(r2, ResultErrorCircuit)
    assert str(r1) == str(r2)


Matt Traudt's avatar
Matt Traudt committed
228
229
@patch('time.time')
def test_ResultErrorStream(time_mock):
Matt Traudt's avatar
Matt Traudt committed
230
    t = 2000
Matt Traudt's avatar
Matt Traudt committed
231
    time_mock.side_effect = monotonic_time(start=t)
Matt Traudt's avatar
Matt Traudt committed
232
233
234
    fp1 = 'A' * 40
    fp2 = 'Z' * 40
    circ = [fp1, fp2]
Matt Traudt's avatar
Matt Traudt committed
235
    dest_url = 'http://example.com/sbws.bin'
Matt Traudt's avatar
Matt Traudt committed
236
    scanner_nick = 'sbwsscanner'
Matt Traudt's avatar
Matt Traudt committed
237
238
239
240
    nick = 'Mooooooo'
    relay_ip = '169.254.100.1'
    relay = Result.Relay(fp1, nick, relay_ip)
    msg = 'aaaaayyyyyy bb'
Matt Traudt's avatar
Matt Traudt committed
241
242
    r1 = ResultErrorStream(relay, circ, dest_url, scanner_nick, msg=msg)
    r2 = ResultErrorStream(relay, circ, dest_url, scanner_nick, msg=msg,
Matt Traudt's avatar
Matt Traudt committed
243
244
245
246
247
                           t=t)
    assert r1.msg == msg
    assert r1.nickname == nick
    assert r1.time == t
    assert r1.fingerprint == fp1
Matt Traudt's avatar
Matt Traudt committed
248
    assert r1.scanner == scanner_nick
Matt Traudt's avatar
Matt Traudt committed
249
250
251
    assert r1.type == _ResultType.ErrorStream
    assert r1.address == relay_ip
    assert r1.circ == circ
Matt Traudt's avatar
Matt Traudt committed
252
    assert r1.dest_url == dest_url
253
    assert r1.version == RESULT_VERSION
Matt Traudt's avatar
Matt Traudt committed
254
255
256
    assert str(r1) == str(r2)


Matt Traudt's avatar
Matt Traudt committed
257
258
@patch('time.time')
def test_ResultErrorStream_from_dict(time_mock):
Matt Traudt's avatar
Matt Traudt committed
259
    t = 2000
Matt Traudt's avatar
Matt Traudt committed
260
    time_mock.side_effect = monotonic_time(start=t)
Matt Traudt's avatar
Matt Traudt committed
261
262
263
    fp1 = 'A' * 40
    fp2 = 'Z' * 40
    circ = [fp1, fp2]
Matt Traudt's avatar
Matt Traudt committed
264
    dest_url = 'http://example.com/sbws.bin'
Matt Traudt's avatar
Matt Traudt committed
265
    scanner_nick = 'sbwsscanner'
Matt Traudt's avatar
Matt Traudt committed
266
267
268
269
    nick = 'Mooooooo'
    relay_ip = '169.254.100.1'
    relay = Result.Relay(fp1, nick, relay_ip)
    msg = 'aaaaayyyyyy bb'
Matt Traudt's avatar
Matt Traudt committed
270
    r1 = ResultErrorStream(relay, circ, dest_url, scanner_nick, msg=msg)
Matt Traudt's avatar
Matt Traudt committed
271
272
273
    d = {
        'msg': msg, 'fingerprint': fp1,
        'nickname': nick, 'address': relay_ip, 'circ': circ,
Matt Traudt's avatar
Matt Traudt committed
274
        'dest_url': dest_url, 'scanner': scanner_nick,
275
        'version': RESULT_VERSION, 'type': _ResultType.ErrorStream, 'time': t,
Matt Traudt's avatar
Matt Traudt committed
276
277
278
279
280
281
282
    }
    r2 = Result.from_dict(d)
    assert isinstance(r1, ResultErrorStream)
    assert isinstance(r2, ResultErrorStream)
    assert str(r1) == str(r2)


Matt Traudt's avatar
Matt Traudt committed
283
284
@patch('time.time')
def test_ResultErrorAuth(time_mock):
Matt Traudt's avatar
Matt Traudt committed
285
    t = 2000
Matt Traudt's avatar
Matt Traudt committed
286
    time_mock.side_effect = monotonic_time(start=t)
Matt Traudt's avatar
Matt Traudt committed
287
288
289
    fp1 = 'A' * 40
    fp2 = 'Z' * 40
    circ = [fp1, fp2]
Matt Traudt's avatar
Matt Traudt committed
290
    dest_url = 'http://example.com/sbws.bin'
Matt Traudt's avatar
Matt Traudt committed
291
    scanner_nick = 'sbwsscanner'
Matt Traudt's avatar
Matt Traudt committed
292
293
294
295
    nick = 'Mooooooo'
    relay_ip = '169.254.100.1'
    relay = Result.Relay(fp1, nick, relay_ip)
    msg = 'aaaaayyyyyy bb'
Matt Traudt's avatar
Matt Traudt committed
296
297
    r1 = ResultErrorAuth(relay, circ, dest_url, scanner_nick, msg=msg)
    r2 = ResultErrorAuth(relay, circ, dest_url, scanner_nick, msg=msg,
Matt Traudt's avatar
Matt Traudt committed
298
299
300
301
302
                         t=t)
    assert r1.msg == msg
    assert r1.nickname == nick
    assert r1.time == t
    assert r1.fingerprint == fp1
Matt Traudt's avatar
Matt Traudt committed
303
    assert r1.scanner == scanner_nick
Matt Traudt's avatar
Matt Traudt committed
304
305
306
    assert r1.type == _ResultType.ErrorAuth
    assert r1.address == relay_ip
    assert r1.circ == circ
Matt Traudt's avatar
Matt Traudt committed
307
    assert r1.dest_url == dest_url
308
    assert r1.version == RESULT_VERSION
Matt Traudt's avatar
Matt Traudt committed
309
310
311
    assert str(r1) == str(r2)


Matt Traudt's avatar
Matt Traudt committed
312
313
@patch('time.time')
def test_ResultErrorAuth_from_dict(time_mock):
Matt Traudt's avatar
Matt Traudt committed
314
    t = 2000
Matt Traudt's avatar
Matt Traudt committed
315
    time_mock.side_effect = monotonic_time(start=t)
Matt Traudt's avatar
Matt Traudt committed
316
317
318
    fp1 = 'A' * 40
    fp2 = 'Z' * 40
    circ = [fp1, fp2]
Matt Traudt's avatar
Matt Traudt committed
319
    dest_url = 'http://example.com/sbws.bin'
Matt Traudt's avatar
Matt Traudt committed
320
    scanner_nick = 'sbwsscanner'
Matt Traudt's avatar
Matt Traudt committed
321
322
323
324
    nick = 'Mooooooo'
    relay_ip = '169.254.100.1'
    relay = Result.Relay(fp1, nick, relay_ip)
    msg = 'aaaaayyyyyy bb'
Matt Traudt's avatar
Matt Traudt committed
325
    r1 = ResultErrorAuth(relay, circ, dest_url, scanner_nick, msg=msg)
Matt Traudt's avatar
Matt Traudt committed
326
327
328
    d = {
        'msg': msg, 'fingerprint': fp1,
        'nickname': nick, 'address': relay_ip, 'circ': circ,
Matt Traudt's avatar
Matt Traudt committed
329
        'dest_url': dest_url, 'scanner': scanner_nick,
330
        'version': RESULT_VERSION, 'type': _ResultType.ErrorAuth, 'time': t,
Matt Traudt's avatar
Matt Traudt committed
331
332
333
334
335
    }
    r2 = Result.from_dict(d)
    assert isinstance(r1, ResultErrorAuth)
    assert isinstance(r2, ResultErrorAuth)
    assert str(r1) == str(r2)