File: test_stream.py

package info (click to toggle)
python-txi2p-tahoe 0.3.7-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 448 kB
  • sloc: python: 3,757; makefile: 163; sh: 3
file content (324 lines) | stat: -rw-r--r-- 13,098 bytes parent folder | download | duplicates (3)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# Copyright (c) str4d <str4d@mail.i2p>
# See COPYING for details.

try:
    # Python 3
    from unittest.mock import Mock
except:
    # Python 2 (library)
    from mock import Mock
import os
import twisted
from twisted.internet import defer
from twisted.python.versions import Version
from twisted.test import proto_helpers
from twisted.trial import unittest

from txi2p.address import I2PAddress
from txi2p.sam import stream
from txi2p.test.util import TEST_B64, FakeFactory
from .util import SAMProtocolTestMixin, SAMFactoryTestMixin

if twisted.version < Version('twisted', 12, 3, 0):
    skipSRO = 'TestCase.successResultOf() requires twisted 12.3 or newer'
else:
    skipSRO = None


class TestStreamConnectProtocol(SAMProtocolTestMixin, unittest.TestCase):
    protocol = stream.StreamConnectProtocol

    def test_streamConnectAfterHello(self):
        fac, proto = self.makeProto()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.dest = 'bar'
        proto.transport.clear()
        proto.dataReceived(b'HELLO REPLY RESULT=OK VERSION=3.1\n')
        self.assertEquals(
            b'STREAM CONNECT ID=foo DESTINATION=bar SILENT=false\n',
            proto.transport.value())

    def test_streamConnectWithPortAfterHello(self):
        fac, proto = self.makeProto()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.dest = 'bar'
        fac.port = 80
        proto.transport.clear()
        proto.dataReceived(b'HELLO REPLY RESULT=OK VERSION=3.1\n')
        self.assertEquals(
            b'STREAM CONNECT ID=foo DESTINATION=bar SILENT=false TO_PORT=80\n',
            proto.transport.value())

    def test_streamConnectWithLocalPortAfterHello(self):
        fac, proto = self.makeProto()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.dest = 'bar'
        fac.localPort = 34444
        proto.transport.clear()
        proto.dataReceived(b'HELLO REPLY RESULT=OK VERSION=3.1\n')
        self.assertEquals(
            b'STREAM CONNECT ID=foo DESTINATION=bar SILENT=false FROM_PORT=34444\n',
            proto.transport.value())

    def test_namingLookupAfterHello(self):
        fac, proto = self.makeProto()
        fac.dest = None
        fac.host = 'spam.i2p'
        proto.transport.clear()
        proto.dataReceived(b'HELLO REPLY RESULT=OK VERSION=3.1\n')
        self.assertEquals(
            b'NAMING LOOKUP NAME=spam.i2p\n',
            proto.transport.value())

    def test_namingLookupReturnsError(self):
        fac, proto = self.makeProto()
        fac.dest = None
        fac.host = 'spam.i2p'
        proto.transport.clear()
        proto.dataReceived(b'HELLO REPLY RESULT=OK VERSION=3.1\n')
        proto.transport.clear()
        proto.dataReceived(b'NAMING REPLY RESULT=KEY_NOT_FOUND NAME=spam.i2p MESSAGE="foo bar baz"\n')
        fac.resultNotOK.assert_called_with('KEY_NOT_FOUND', 'foo bar baz')

    def test_streamConnectAfterNamingLookup(self):
        fac, proto = self.makeProto()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.dest = None
        fac.host = 'spam.i2p'
        proto.transport.clear()
        proto.dataReceived(b'HELLO REPLY RESULT=OK VERSION=3.1\n')
        proto.transport.clear()
        proto.dataReceived(b'NAMING REPLY RESULT=OK NAME=spam.i2p VALUE=bar\n')
        self.assertEquals(
            b'STREAM CONNECT ID=foo DESTINATION=bar SILENT=false\n',
            proto.transport.value())

    def test_streamConnectReturnsError(self):
        fac, proto = self.makeProto()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.dest = 'bar'
        proto.transport.clear()
        proto.dataReceived(b'HELLO REPLY RESULT=OK VERSION=3.1\n')
        proto.transport.clear()
        proto.dataReceived(b'STREAM STATUS RESULT=I2P_ERROR MESSAGE="foo bar baz"\n')
        fac.resultNotOK.assert_called_with('I2P_ERROR', 'foo bar baz')

    def test_streamConnectionEstablishedAfterStreamConnect(self):
        fac, proto = self.makeProto()
        fac.streamConnectionEstablished = Mock()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.dest = 'bar'
        proto.transport.clear()
        proto.dataReceived(b'HELLO REPLY RESULT=OK VERSION=3.1\n')
        proto.transport.clear()
        proto.dataReceived(b'STREAM STATUS RESULT=OK\n')
        fac.streamConnectionEstablished.assert_called_with(proto.receiver)


class TestStreamConnectFactory(SAMFactoryTestMixin, unittest.TestCase):
    factory = stream.StreamConnectFactory
    blankFactoryArgs = (None, Mock(), '', '')

    def test_streamConnectionEstablished(self):
        mreactor = proto_helpers.MemoryReactor()
        wrappedFactory = FakeFactory()
        session = Mock()
        fac, proto = self.makeProto(wrappedFactory, session, 'spam.i2p', 'foo')
        # Shortcut to end of SAM stream connect protocol
        proto.receiver.currentRule = 'State_connect'
        proto._parser._setupInterp()
        proto.dataReceived(b'STREAM STATUS RESULT=OK\n')
        session.addStream.assert_called_with(proto.receiver)
        streamProto = self.successResultOf(fac.deferred)
        self.assertEqual(proto.receiver.wrappedProto, streamProto)
    test_streamConnectionEstablished.skip = skipSRO


class TestStreamAcceptProtocol(SAMProtocolTestMixin, unittest.TestCase):
    protocol = stream.StreamAcceptProtocol

    def test_streamAcceptAfterHello(self):
        fac, proto = self.makeProto()
        fac.session = Mock()
        fac.session.id = 'foo'
        proto.transport.clear()
        proto.dataReceived(b'HELLO REPLY RESULT=OK VERSION=3.1\n')
        self.assertEquals(
            b'STREAM ACCEPT ID=foo SILENT=false\n',
            proto.transport.value())

    def test_streamAcceptReturnsError(self):
        fac, proto = self.makeProto()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.forwardPort = 1337
        proto.transport.clear()
        proto.dataReceived(b'HELLO REPLY RESULT=OK VERSION=3.1\n')
        proto.transport.clear()
        proto.dataReceived(b'STREAM STATUS RESULT=I2P_ERROR MESSAGE="foo bar baz"\n')
        fac.resultNotOK.assert_called_with('I2P_ERROR', 'foo bar baz')

    def test_streamAcceptEstablishedAfterStreamAccept(self):
        fac, proto = self.makeProto()
        fac.streamAcceptEstablished = Mock()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.forwardPort = 1337
        proto.transport.clear()
        proto.dataReceived(b'HELLO REPLY RESULT=OK VERSION=3.1\n')
        proto.transport.clear()
        proto.dataReceived(b'STREAM STATUS RESULT=OK\n')
        fac.streamAcceptEstablished.assert_called_with(proto.receiver)

    def test_streamAcceptIncomingAfterPeerAddress(self):
        fac, proto = self.makeProto()
        fac.streamAcceptIncoming = Mock()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.forwardPort = 1337
        # Shortcut to end of SAM stream accept protocol
        proto.receiver.currentRule = 'State_readData'
        proto._parser._setupInterp()
        proto.dataReceived(('%s FROM_PORT=34444 TO_PORT=0\n' % TEST_B64).encode('utf-8'))
        fac.streamAcceptIncoming.assert_called_with(proto.receiver)
        self.assertEquals(
            I2PAddress(TEST_B64, port=34444),
            proto.receiver.peer)

    def test_peerDataWrapped_allAtOnce(self):
        fac, proto = self.makeProto()
        fac.streamAcceptIncoming = Mock()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.forwardPort = 1337
        proto.receiver.wrappedProto = proto_helpers.AccumulatingProtocol()
        # Shortcut to end of SAM stream accept protocol
        proto.receiver.currentRule = 'State_readData'
        proto._parser._setupInterp()
        proto.dataReceived(('%s FROM_PORT=34444 TO_PORT=0\nEgg and spam' % TEST_B64).encode('utf-8'))
        self.assertEquals(b'Egg and spam', proto.receiver.wrappedProto.data)

    def test_peerDataWrapped_twoParts(self):
        fac, proto = self.makeProto()
        fac.streamAcceptIncoming = Mock()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.forwardPort = 1337
        proto.receiver.wrappedProto = proto_helpers.AccumulatingProtocol()
        # Shortcut to end of SAM stream accept protocol
        proto.receiver.currentRule = 'State_readData'
        proto._parser._setupInterp()
        proto.dataReceived(('%s FROM_PORT=34444 TO_PORT=0\nEgg a' % TEST_B64).encode('utf-8'))
        self.assertEquals(b'Egg a', proto.receiver.wrappedProto.data)
        proto.dataReceived(b'nd spam')
        self.assertEquals(b'Egg and spam', proto.receiver.wrappedProto.data)

    def test_peerDataWrapped_threeParts(self):
        fac, proto = self.makeProto()
        fac.streamAcceptIncoming = Mock()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.forwardPort = 1337
        proto.receiver.wrappedProto = proto_helpers.AccumulatingProtocol()
        # Shortcut to end of SAM stream accept protocol
        proto.receiver.currentRule = 'State_readData'
        proto._parser._setupInterp()
        proto.dataReceived(('%s FROM_PORT=34444 T' % TEST_B64).encode('utf-8'))
        proto.dataReceived(b'O_PORT=0\nEgg a')
        self.assertEquals(b'Egg a', proto.receiver.wrappedProto.data)
        proto.dataReceived(b'nd spam')
        self.assertEquals(b'Egg and spam', proto.receiver.wrappedProto.data)


class TestStreamAcceptFactory(SAMFactoryTestMixin, unittest.TestCase):
    factory = stream.StreamAcceptFactory
    blankFactoryArgs = (Mock(), Mock(), Mock())

    def test_streamAcceptEstablished(self):
        mreactor = proto_helpers.MemoryReactor()
        session = Mock()
        listeningPort = Mock()
        fac, proto = self.makeProto(Mock(), session, listeningPort)
        # Shortcut to end of SAM stream accept protocol
        proto.receiver.currentRule = 'State_accept'
        proto._parser._setupInterp()
        proto.dataReceived(b'STREAM STATUS RESULT=OK\n')
        session.addStream.assert_called_with(proto.receiver)
        listeningPort.addAccept.assert_called_with(proto.receiver)
    test_streamAcceptEstablished.skip = skipSRO

    def test_streamAcceptIncoming(self):
        mreactor = proto_helpers.MemoryReactor()
        wrappedFactory = FakeFactory()
        session = Mock()
        listeningPort = Mock()
        fac, proto = self.makeProto(wrappedFactory, session, listeningPort)
        # Shortcut to end of SAM stream accept protocol
        proto.receiver.currentRule = 'State_readData'
        proto._parser._setupInterp()
        proto.dataReceived(('%s FROM_PORT=34444 TO_PORT=0\n' % TEST_B64).encode('utf-8'))
        listeningPort.removeAccept.assert_called_with(proto.receiver)
        self.assertEqual(wrappedFactory.proto, proto.receiver.wrappedProto)
    test_streamAcceptEstablished.skip = skipSRO


class TestStreamForwardProtocol(SAMProtocolTestMixin, unittest.TestCase):
    protocol = stream.StreamForwardProtocol

    def test_streamForwardAfterHello(self):
        fac, proto = self.makeProto()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.forwardPort = 1337
        proto.transport.clear()
        proto.dataReceived(b'HELLO REPLY RESULT=OK VERSION=3.1\n')
        self.assertEquals(
            b'STREAM FORWARD ID=foo PORT=1337 SILENT=false\n',
            proto.transport.value())

    def test_streamForwardReturnsError(self):
        fac, proto = self.makeProto()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.forwardPort = 1337
        proto.transport.clear()
        proto.dataReceived(b'HELLO REPLY RESULT=OK VERSION=3.1\n')
        proto.transport.clear()
        proto.dataReceived(b'STREAM STATUS RESULT=I2P_ERROR MESSAGE="foo bar baz"\n')
        fac.resultNotOK.assert_called_with('I2P_ERROR', 'foo bar baz')

    def test_streamForwardEstablishedAfterStreamForward(self):
        fac, proto = self.makeProto()
        fac.streamForwardEstablished = Mock()
        fac.session = Mock()
        fac.session.id = 'foo'
        fac.forwardPort = 1337
        proto.transport.clear()
        proto.dataReceived(b'HELLO REPLY RESULT=OK VERSION=3.1\n')
        proto.transport.clear()
        proto.dataReceived(b'STREAM STATUS RESULT=OK\n')
        fac.streamForwardEstablished.assert_called_with(proto.receiver)


class TestStreamForwardFactory(SAMFactoryTestMixin, unittest.TestCase):
    factory = stream.StreamForwardFactory
    blankFactoryArgs = (Mock(), '')

    def test_streamForwardEstablished(self):
        mreactor = proto_helpers.MemoryReactor()
        session = Mock()
        fac, proto = self.makeProto(session, '')
        # Shortcut to end of SAM stream forward protocol
        proto.receiver.currentRule = 'State_forward'
        proto._parser._setupInterp()
        proto.dataReceived(b'STREAM STATUS RESULT=OK\n')
        session.addStream.assert_called_with(proto.receiver)
        streamProto = self.successResultOf(fac.deferred)
        self.assertEqual(proto.receiver, streamProto)
    test_streamForwardEstablished.skip = skipSRO