Пример #1
0
 def test_connect_but_no_creator(self):
     with self.assertRaises(ValueError) as ctx:
         socks._SocksMachine(
             'CONNECT',
             u'foo.bar',
         )
     self.assertTrue(
         "create_connection function required" in str(ctx.exception))
Пример #2
0
 def test_connect_but_no_creator(self):
     with self.assertRaises(ValueError) as ctx:
         socks._SocksMachine(
             'CONNECT', u'foo.bar',
         )
     self.assertTrue(
         "create_connection function required" in str(ctx.exception)
     )
Пример #3
0
    def test_end_to_end_wrong_version(self):
        dis = []

        def on_disconnect(error_message):
            dis.append(error_message)

        sm = socks._SocksMachine('RESOLVE',
                                 u'meejah.ca',
                                 443,
                                 on_disconnect=on_disconnect)
        sm.connection()

        sm.feed_data(b'\x06')
        sm.feed_data(b'\x00')

        # we should have sent the request to the server, and nothing
        # else (because we disconnected)
        data = BytesIO()
        sm.send_data(data.write)
        self.assertEqual(
            b'\x05\x01\x00',
            data.getvalue(),
        )
        self.assertEqual(1, len(dis))
        self.assertEqual("Expected version 5, got 6", dis[0])
Пример #4
0
    def test_resolve_with_reply(self):
        # kurt: most things use (hsot, port) tuples, this probably
        # should too
        sm = socks._SocksMachine('RESOLVE', u'meejah.ca', 443)
        sm.connection()
        sm.version_reply(0x00)

        # make sure the state-machine wanted to send out the correct
        # request.
        data = BytesIO()
        sm.send_data(data.write)
        self.assertEqual(
            b'\x05\x01\x00'
            b'\x05\xf0\x00\x03\tmeejah.ca\x00\x00',
            data.getvalue(),
        )

        # now feed it a reply (but not enough to parse it yet!)
        d = sm.when_done()
        # ...we have to send at least 8 bytes, but NOT the entire hostname
        sm.feed_data(b'\x05\x00\x00\x03')
        sm.feed_data(b'\x06meeja')
        self.assertTrue(not d.called)
        # now send the rest, checking the buffering in _parse_domain_name_reply
        sm.feed_data(b'h\x00\x00')
        self.assertTrue(d.called)
        answer = yield d
        # XXX answer *should* be not-bytes, though I think
        self.assertEqual(b'meejah', answer)
Пример #5
0
    def test_resolve_with_reply(self):
        # kurt: most things use (hsot, port) tuples, this probably
        # should too
        sm = socks._SocksMachine('RESOLVE', u'meejah.ca', 443)
        sm.connection()
        sm.version_reply(0x00)

        # make sure the state-machine wanted to send out the correct
        # request.
        data = BytesIO()
        sm.send_data(data.write)
        self.assertEqual(
            b'\x05\x01\x00'
            b'\x05\xf0\x00\x03\tmeejah.ca\x00\x00',
            data.getvalue(),
        )

        # now feed it a reply (but not enough to parse it yet!)
        d = sm.when_done()
        # ...we have to send at least 8 bytes, but NOT the entire hostname
        sm.feed_data(b'\x05\x00\x00\x03')
        sm.feed_data(b'\x06meeja')
        self.assertTrue(not d.called)
        # now send the rest, checking the buffering in _parse_domain_name_reply
        sm.feed_data(b'h\x00\x00')
        self.assertTrue(d.called)
        answer = yield d
        # XXX answer *should* be not-bytes, though I think
        self.assertEqual(b'meejah', answer)
Пример #6
0
    def test_resolve(self):
        # kurt: most things use (hsot, port) tuples, this probably
        # should too
        sm = socks._SocksMachine('RESOLVE', u'meejah.ca', 443)
        sm.connection()
        sm.version_reply(0x00)

        data = BytesIO()
        sm.send_data(data.write)
        self.assertEqual(
            b'\x05\x01\x00'
            b'\x05\xf0\x00\x03\tmeejah.ca\x00\x00',
            data.getvalue(),
        )
Пример #7
0
    def test_resolve(self):
        # kurt: most things use (hsot, port) tuples, this probably
        # should too
        sm = socks._SocksMachine('RESOLVE', u'meejah.ca', 443)
        sm.connection()
        sm.version_reply(0x00)

        data = BytesIO()
        sm.send_data(data.write)
        self.assertEqual(
            b'\x05\x01\x00'
            b'\x05\xf0\x00\x03\tmeejah.ca\x00\x00',
            data.getvalue(),
        )
Пример #8
0
    def test_end_to_end_success(self):
        sm = socks._SocksMachine('RESOLVE', u'meejah.ca', 443)
        sm.connection()

        sm.feed_data(b'\x05')
        sm.feed_data(b'\x00')

        # now we check we got the right bytes out the other side
        data = BytesIO()
        sm.send_data(data.write)
        self.assertEqual(
            b'\x05\x01\x00'
            b'\x05\xf0\x00\x03\tmeejah.ca\x00\x00',
            data.getvalue(),
        )
Пример #9
0
    def test_end_to_end_success(self):
        sm = socks._SocksMachine('RESOLVE', u'meejah.ca', 443)
        sm.connection()

        sm.feed_data(b'\x05')
        sm.feed_data(b'\x00')

        # now we check we got the right bytes out the other side
        data = BytesIO()
        sm.send_data(data.write)
        self.assertEqual(
            b'\x05\x01\x00'
            b'\x05\xf0\x00\x03\tmeejah.ca\x00\x00',
            data.getvalue(),
        )
Пример #10
0
    def test_connect(self):
        sm = socks._SocksMachine(
            'CONNECT', u'1.2.3.4', 443,
            create_connection=lambda a, p: None,
        )
        sm.connection()
        sm.version_reply(0x00)

        data = BytesIO()
        sm.send_data(data.write)
        self.assertEqual(
            b'\x05\x01\x00'
            b'\x05\x01\x00\x01\x01\x02\x03\x04\x01\xbb',
            data.getvalue(),
        )
Пример #11
0
    def test_resolve_ptr(self):
        sm = socks._SocksMachine('RESOLVE_PTR', u'1.2.3.4', 443)
        sm.connection()
        sm.version_reply(0x00)

        data = BytesIO()
        sm.send_data(data.write)
        self.assertEqual(
            b'\x05\x01\x00'
            b'\x05\xf1\x00\x01\x01\x02\x03\x04\x00\x00',
            data.getvalue(),
        )
        sm.feed_data(b'\x05\x00\x00\x01\x00\x01\x02\xff\x12\x34')
        addr = yield sm.when_done()
        self.assertEqual('0.1.2.255', addr)
Пример #12
0
    def test_resolve_ptr(self):
        sm = socks._SocksMachine('RESOLVE_PTR', u'1.2.3.4', 443)
        sm.connection()
        sm.version_reply(0x00)

        data = BytesIO()
        sm.send_data(data.write)
        self.assertEqual(
            b'\x05\x01\x00'
            b'\x05\xf1\x00\x01\x01\x02\x03\x04\x00\x00',
            data.getvalue(),
        )
        sm.feed_data(
            b'\x05\x00\x00\x01\x00\x01\x02\xff\x12\x34'
        )
        addr = yield sm.when_done()
        self.assertEqual('0.1.2.255', addr)
Пример #13
0
    def test_end_to_end_connect_and_relay(self):
        sm = socks._SocksMachine(
            'CONNECT', u'1.2.3.4', 443,
            create_connection=lambda a, p: None,
        )
        sm.connection()

        sm.feed_data(b'\x05')
        sm.feed_data(b'\x00')
        sm.feed_data(b'some relayed data')

        # now we check we got the right bytes out the other side
        data = BytesIO()
        sm.send_data(data.write)
        self.assertEqual(
            b'\x05\x01\x00'
            b'\x05\x01\x00\x01\x01\x02\x03\x04\x01\xbb',
            data.getvalue(),
        )
Пример #14
0
    def test_end_to_end_connection_refused(self):
        dis = []

        def on_disconnect(error_message):
            dis.append(error_message)
        sm = socks._SocksMachine(
            'CONNECT', u'1.2.3.4', 443,
            on_disconnect=on_disconnect,
            create_connection=lambda a, p: None,
        )
        sm.connection()

        sm.feed_data(b'\x05')
        sm.feed_data(b'\x00')

        # reply with 'connection refused'
        sm.feed_data(b'\x05\x05\x00\x01\x00\x00\x00\x00\xff\xff')

        self.assertEqual(1, len(dis))
        self.assertEqual(socks.ConnectionRefusedError.message, dis[0])
Пример #15
0
    def test_end_to_end_wrong_version(self):
        dis = []

        def on_disconnect(error_message):
            dis.append(error_message)
        sm = socks._SocksMachine('RESOLVE', u'meejah.ca', 443, on_disconnect=on_disconnect)
        sm.connection()

        sm.feed_data(b'\x06')
        sm.feed_data(b'\x00')

        # we should have sent the request to the server, and nothing
        # else (because we disconnected)
        data = BytesIO()
        sm.send_data(data.write)
        self.assertEqual(
            b'\x05\x01\x00',
            data.getvalue(),
        )
        self.assertEqual(1, len(dis))
        self.assertEqual("Expected version 5, got 6", dis[0])
Пример #16
0
    def test_end_to_end_successful_relay(self):
        class Proto(object):
            data = b''
            lost = []

            def dataReceived(self, d):
                self.data = self.data + d

            def connectionLost(self, reason):
                self.lost.append(reason)

        the_proto = Proto()
        dis = []

        def on_disconnect(error_message):
            dis.append(error_message)

        sm = socks._SocksMachine(
            'CONNECT',
            u'1.2.3.4',
            443,
            on_disconnect=on_disconnect,
            create_connection=lambda a, p: the_proto,
        )
        sm.connection()

        sm.feed_data(b'\x05')
        sm.feed_data(b'\x00')

        # reply with success, port 0x1234
        sm.feed_data(b'\x05\x00\x00\x01\x00\x00\x00\x00\x12\x34')

        # now some data that should get relayed
        sm.feed_data(b'this is some relayed data')
        # should *not* have disconnected
        self.assertEqual(0, len(dis))
        self.assertTrue(the_proto.data, b"this is some relayed data")
        sm.disconnected(socks.SocksError("it's fine"))
        self.assertEqual(1, len(Proto.lost))
        self.assertTrue("it's fine" in str(Proto.lost[0]))
Пример #17
0
    def test_end_to_end_successful_relay(self):

        class Proto(object):
            data = b''
            lost = []

            def dataReceived(self, d):
                self.data = self.data + d

            def connectionLost(self, reason):
                self.lost.append(reason)

        the_proto = Proto()
        dis = []

        def on_disconnect(error_message):
            dis.append(error_message)
        sm = socks._SocksMachine(
            'CONNECT', u'1.2.3.4', 443,
            on_disconnect=on_disconnect,
            create_connection=lambda a, p: the_proto,
        )
        sm.connection()

        sm.feed_data(b'\x05')
        sm.feed_data(b'\x00')

        # reply with success, port 0x1234
        sm.feed_data(b'\x05\x00\x00\x01\x00\x00\x00\x00\x12\x34')

        # now some data that should get relayed
        sm.feed_data(b'this is some relayed data')
        # should *not* have disconnected
        self.assertEqual(0, len(dis))
        self.assertTrue(the_proto.data, b"this is some relayed data")
        sm.disconnected(socks.SocksError("it's fine"))
        self.assertEqual(1, len(Proto.lost))
        self.assertTrue("it's fine" in str(Proto.lost[0]))
Пример #18
0
    def test_unknown_response_type(self):
        # kurt: most things use (hsot, port) tuples, this probably
        # should too
        sm = socks._SocksMachine('RESOLVE', u'meejah.ca', 443)
        sm.connection()
        # don't actually support username/password (which is version 0x02) yet
        # sm.version_reply(0x02)
        sm.version_reply(0)

        # make sure the state-machine wanted to send out the correct
        # request.
        data = BytesIO()
        sm.send_data(data.write)
        self.assertEqual(
            b'\x05\x01\x00'
            b'\x05\xf0\x00\x03\tmeejah.ca\x00\x00',
            data.getvalue(),
        )

        sm.feed_data(b'\x05\x00\x00\xaf\x00\x00\x00\x00')
        with self.assertRaises(socks.SocksError) as ctx:
            yield sm.when_done()
        self.assertTrue('Unexpected response type 175' in str(ctx.exception))
Пример #19
0
    def test_unknown_response_type(self):
        # kurt: most things use (hsot, port) tuples, this probably
        # should too
        sm = socks._SocksMachine('RESOLVE', u'meejah.ca', 443)
        sm.connection()
        # don't actually support username/password (which is version 0x02) yet
        # sm.version_reply(0x02)
        sm.version_reply(0)

        # make sure the state-machine wanted to send out the correct
        # request.
        data = BytesIO()
        sm.send_data(data.write)
        self.assertEqual(
            b'\x05\x01\x00'
            b'\x05\xf0\x00\x03\tmeejah.ca\x00\x00',
            data.getvalue(),
        )

        sm.feed_data(b'\x05\x00\x00\xaf\x00\x00\x00\x00')
        with self.assertRaises(socks.SocksError) as ctx:
            yield sm.when_done()
        self.assertTrue('Unexpected response type 175' in str(ctx.exception))
Пример #20
0
 def test_illegal_request(self):
     with self.assertRaises(ValueError) as ctx:
         socks._SocksMachine('FOO_RESOLVE', u'meejah.ca', 443)
     self.assertTrue(
         'Unknown request type' in str(ctx.exception)
     )
Пример #21
0
 def test_illegal_host(self):
     with self.assertRaises(ValueError) as ctx:
         socks._SocksMachine('RESOLVE', 1234, 443)
     self.assertTrue("'host' must be" in str(ctx.exception))
Пример #22
0
 def test_illegal_request(self):
     with self.assertRaises(ValueError) as ctx:
         socks._SocksMachine('FOO_RESOLVE', u'meejah.ca', 443)
     self.assertTrue('Unknown request type' in str(ctx.exception))
Пример #23
0
 def test_illegal_host(self):
     with self.assertRaises(ValueError) as ctx:
         socks._SocksMachine('RESOLVE', 1234, 443)
     self.assertTrue(
         "'host' must be" in str(ctx.exception)
     )