Ejemplo n.º 1
0
def test_log(mock_stderr, mock_stdout):
    sshuttle.helpers.log("message")
    sshuttle.helpers.log("abc")
    sshuttle.helpers.log("message 1\n")
    sshuttle.helpers.log("message 2\nline2\nline3\n")
    sshuttle.helpers.log("message 3\nline2\nline3")
    assert mock_stdout.mock_calls == [
        call.flush(),
        call.flush(),
        call.flush(),
        call.flush(),
        call.flush(),
    ]
    assert mock_stderr.mock_calls == [
        call.write('prefix: message'),
        call.flush(),
        call.write('prefix: abc'),
        call.flush(),
        call.write('prefix: message 1\n'),
        call.flush(),
        call.write('prefix: message 2\n'),
        call.write('---> line2\n'),
        call.write('---> line3\n'),
        call.flush(),
        call.write('prefix: message 3\n'),
        call.write('---> line2\n'),
        call.write('---> line3\n'),
        call.flush(),
    ]
Ejemplo n.º 2
0
def test_log(mock_stderr, mock_stdout):
    sshuttle.helpers.log("message")
    sshuttle.helpers.log("abc")
    sshuttle.helpers.log("message 1\n")
    sshuttle.helpers.log("message 2\nline2\nline3\n")
    sshuttle.helpers.log("message 3\nline2\nline3")
    assert mock_stdout.mock_calls == [
        call.flush(),
        call.flush(),
        call.flush(),
        call.flush(),
        call.flush(),
    ]
    assert mock_stderr.mock_calls == [
        call.write('prefix: message\n'),
        call.flush(),
        call.write('prefix: abc\n'),
        call.flush(),
        call.write('prefix: message 1\n'),
        call.flush(),
        call.write('prefix: message 2\n'),
        call.write('    line2\n'),
        call.write('    line3\n'),
        call.flush(),
        call.write('prefix: message 3\n'),
        call.write('    line2\n'),
        call.write('    line3\n'),
        call.flush(),
    ]
Ejemplo n.º 3
0
def test_main(mock_get_method, mock_setup_daemon, mock_rewrite_etc_hosts):
    stdin, stdout = setup_daemon()
    mock_setup_daemon.return_value = stdin, stdout

    mock_get_method("not_auto").name = "test"
    mock_get_method.reset_mock()

    sshuttle.firewall.main("not_auto", False)

    assert mock_rewrite_etc_hosts.mock_calls == [
        call({'1.2.3.3': 'existing'}, 1024),
        call({}, 1024),
    ]

    assert stdout.mock_calls == [
        call.write('READY test\n'),
        call.flush(),
        call.write('STARTED\n'),
        call.flush()
    ]
    assert mock_setup_daemon.mock_calls == [call()]
    assert mock_get_method.mock_calls == [
        call('not_auto'),
        call().setup_firewall(
            1024, 1026, [(10, u'2404:6800:4004:80c::33')], 10,
            [(10, 64, False, u'2404:6800:4004:80c::', 0, 0),
             (10, 128, True, u'2404:6800:4004:80c::101f', 80, 80)], True,
            None),
        call().setup_firewall(1025, 1027, [(2, u'1.2.3.33')], 2,
                              [(2, 24, False, u'1.2.3.0', 8000, 9000),
                               (2, 32, True, u'1.2.3.66', 8080, 8080)], True,
                              None),
        call().restore_firewall(1024, 10, True, None),
        call().restore_firewall(1025, 2, True, None),
    ]
Ejemplo n.º 4
0
def test_debug3(mock_stderr, mock_stdout):
    sshuttle.helpers.debug3("message")
    assert mock_stdout.mock_calls == [
        call.flush(),
    ]
    assert mock_stderr.mock_calls == [
        call.write('prefix: message'),
        call.flush(),
    ]
Ejemplo n.º 5
0
def test_debug3(mock_stderr, mock_stdout):
    sshuttle.helpers.debug3("message")
    assert mock_stdout.mock_calls == [
        call.flush(),
    ]
    assert mock_stderr.mock_calls == [
        call.write('prefix: message\n'),
        call.flush(),
    ]
Ejemplo n.º 6
0
    def test_dump_condensed_debug_msg_no_exc(self, mock_stdout, mock_stderr):
        """
        Tests for dumping error msg to stdout and stderr.

        Args/kwargs:
            `mock_stdout`
                Mocked stdout from a decorator.
            `mock_stderr`
                Mocked stderr from a decorator.
        """
        # Make sure there is no exc_info from previous
        # exception handling
        sys.exc_clear()

        # Dump debug msg to stdout
        stdout_msg = '<dump_condensed_debug_msg header stdout>'
        dump_condensed_debug_msg(stdout_msg, sys.stdout)
        stdout_call = list(mock_stdout.mock_calls)[0]
        self.assertTrue(stdout_msg in str(stdout_call))
        self.assertEqual(mock_stdout.mock_calls[-1], call.flush())

        # Dump debug msg to stderr
        stderr_msg = '<dump_condensed_debug_msg header stderr>'
        dump_condensed_debug_msg(stderr_msg, sys.stderr)
        stderr_call = list(mock_stderr.mock_calls)[0]
        self.assertTrue(stderr_msg in str(stderr_call))
        self.assertEqual(mock_stderr.mock_calls[-1], call.flush())

        mock_stderr.reset_mock()
        mock_stdout.reset_mock()

        # Test while handling exception
        try:
            raise ValueError('ValueError msg')
        except ValueError:
            # Dump exception debug msg to stdout
            exc_stdout_msg = re.compile(
                r"\\nstdout\\n\\nCONDENSED DEBUG INFO:.+\[.+@.+\] "
                r"ValueError:.+ValueError msg.+raise ValueError"
                r"\('ValueError msg'\).+test_common_helpers.+`")
            dump_condensed_debug_msg(header='stdout', stream=sys.stdout)
            stdout_call = str(list(mock_stdout.mock_calls)[0])
            self.assertRegexpMatches(text=stdout_call,
                                     expected_regexp=exc_stdout_msg)
            self.assertEqual(mock_stdout.mock_calls[-1], call.flush())

            # Dump exception debug msg to stderr
            exc_stderr_msg = re.compile(
                r"\\nstderr\\n\\nCONDENSED DEBUG INFO:.+\[.+@.+\] "
                r"ValueError:.+ValueError msg.+raise ValueError"
                r"\('ValueError msg'\).+test_common_helpers.+`")
            dump_condensed_debug_msg(header='stderr', stream=sys.stderr)
            stderr_call = str(list(mock_stderr.mock_calls)[0])
            self.assertRegexpMatches(text=stderr_call,
                                     expected_regexp=exc_stderr_msg)
            self.assertEqual(mock_stderr.mock_calls[-1], call.flush())
Ejemplo n.º 7
0
def test_main(mock_get_method, mock_setup_daemon):
    stdin, stdout = setup_daemon()
    mock_setup_daemon.return_value = stdin, stdout

    if not os.path.isdir("tmp"):
        os.mkdir("tmp")

    sshuttle.firewall.main("test", False)

    with open("tmp/hosts") as f:
        line = f.readline()
        s = line.split()
        assert s == ['1.2.3.3', 'existing']

        line = f.readline()
        assert line == ""

    stdout.mock_calls == [
        call.write('READY test\n'),
        call.flush(),
        call.write('STARTED\n'),
        call.flush()
    ]
    mock_setup_daemon.mock_calls == [call()]
    mock_get_method.mock_calls == [
        call('test'),
        call().setup_firewall(
            1024, 1026,
            [(10, u'2404:6800:4004:80c::33')],
            10,
            [(10, 64, False, u'2404:6800:4004:80c::'),
                (10, 128, True, u'2404:6800:4004:80c::101f')],
            True),
        call().setup_firewall(
            1025, 1027,
            [(2, u'1.2.3.33')],
            2,
            [(2, 24, False, u'1.2.3.0'), (2, 32, True, u'1.2.3.66')],
            True),
        call().setup_firewall()(),
        call().setup_firewall(1024, 0, [], 10, [], True),
        call().setup_firewall(1025, 0, [], 2, [], True),
    ]
Ejemplo n.º 8
0
def test_main(mock_get_method, mock_setup_daemon, mock_rewrite_etc_hosts):
    stdin, stdout = setup_daemon()
    mock_setup_daemon.return_value = stdin, stdout

    mock_get_method("not_auto").name = "test"
    mock_get_method.reset_mock()

    sshuttle.firewall.main("not_auto", False)

    assert mock_rewrite_etc_hosts.mock_calls == [
        call({'1.2.3.3': 'existing'}, 1024),
        call({}, 1024),
    ]

    assert stdout.mock_calls == [
        call.write('READY test\n'),
        call.flush(),
        call.write('STARTED\n'),
        call.flush()
    ]
    assert mock_setup_daemon.mock_calls == [call()]
    assert mock_get_method.mock_calls == [
        call('not_auto'),
        call().setup_firewall(
            1024, 1026,
            [(AF_INET6, u'2404:6800:4004:80c::33')],
            AF_INET6,
            [(AF_INET6, 64, False, u'2404:6800:4004:80c::', 0, 0),
                (AF_INET6, 128, True, u'2404:6800:4004:80c::101f', 80, 80)],
            True,
            None),
        call().setup_firewall(
            1025, 1027,
            [(AF_INET, u'1.2.3.33')],
            AF_INET,
            [(AF_INET, 24, False, u'1.2.3.0', 8000, 9000),
                (AF_INET, 32, True, u'1.2.3.66', 8080, 8080)],
            True,
            None),
        call().restore_firewall(1024, AF_INET6, True, None),
        call().restore_firewall(1025, AF_INET, True, None),
    ]
Ejemplo n.º 9
0
def test_main(mock_get_method, mock_setup_daemon):
    stdin, stdout = setup_daemon()
    mock_setup_daemon.return_value = stdin, stdout

    if not os.path.isdir("tmp"):
        os.mkdir("tmp")

    sshuttle.firewall.main("test", False)

    with open("tmp/hosts") as f:
        line = f.readline()
        s = line.split()
        assert s == ['1.2.3.3', 'existing']

        line = f.readline()
        assert line == ""

    stdout.mock_calls == [
        call.write('READY test\n'),
        call.flush(),
        call.write('STARTED\n'),
        call.flush()
    ]
    mock_setup_daemon.mock_calls == [call()]
    mock_get_method.mock_calls == [
        call('test'),
        call().setup_firewall(1024, 1026, [(10, u'2404:6800:4004:80c::33')],
                              10,
                              [(10, 64, False, u'2404:6800:4004:80c::'),
                               (10, 128, True, u'2404:6800:4004:80c::101f')],
                              True),
        call().setup_firewall(1025, 1027, [(2, u'1.2.3.33')], 2,
                              [(2, 24, False, u'1.2.3.0'),
                               (2, 32, True, u'1.2.3.66')], True),
        call().setup_firewall()(),
        call().setup_firewall(1024, 0, [], 10, [], True),
        call().setup_firewall(1025, 0, [], 2, [], True),
    ]
Ejemplo n.º 10
0
def test_firewall_command_darwin(mock_pf_get_dev, mock_ioctl, mock_stdout):
    method = get_method('pf')
    assert not method.firewall_command("somthing")

    command = "QUERY_PF_NAT %d,%d,%s,%d,%s,%d\n" % (
        AF_INET, socket.IPPROTO_TCP, "127.0.0.1", 1025, "127.0.0.2", 1024)
    assert method.firewall_command(command)

    assert mock_pf_get_dev.mock_calls == [call()]
    assert mock_ioctl.mock_calls == [
        call(mock_pf_get_dev(), 0xc0544417, ANY),
    ]
    assert mock_stdout.mock_calls == [
        call.write('QUERY_PF_NAT_SUCCESS 0.0.0.0,0\n'),
        call.flush(),
    ]
Ejemplo n.º 11
0
def test_firewall_command_darwin(mock_pf_get_dev, mock_ioctl, mock_stdout):
    method = get_method('pf')
    assert not method.firewall_command("somthing")

    command = "QUERY_PF_NAT %d,%d,%s,%d,%s,%d\n" % (
        socket.AF_INET, socket.IPPROTO_TCP,
        "127.0.0.1", 1025, "127.0.0.2", 1024)
    assert method.firewall_command(command)

    assert mock_pf_get_dev.mock_calls == [call()]
    assert mock_ioctl.mock_calls == [
        call(mock_pf_get_dev(), 0xc0544417, ANY),
    ]
    assert mock_stdout.mock_calls == [
        call.write('QUERY_PF_NAT_SUCCESS 0.0.0.0,0\n'),
        call.flush(),
    ]
 def test_warp_pointer(self, mock_rand):
     mock_warp = MagicMock()
     dummy_core = MagicMock(WarpPointer=mock_warp)
     mock_flush = MagicMock()
     dummy_connection = MagicMock(core=dummy_core, flush=mock_flush)
     mock_holder = MagicMock()
     mock_holder.attach_mock(
         self.mock_get_window_geometry,
         'get_window_geometry'
     )
     mock_holder.attach_mock(
         mock_warp,
         'WarpPointer'
     )
     mock_holder.attach_mock(
         mock_rand,
         'randint'
     )
     mock_holder.attach_mock(
         mock_flush,
         'flush'
     )
     self.mover.move_to_random_position_in_window(
         dummy_connection,
         self.WINDOW
     )
     mock_holder.assert_has_calls([
         call.get_window_geometry(dummy_connection, self.WINDOW),
         call.randint(0, self.WIDTH),
         call.randint(0, self.HEIGHT),
         call.WarpPointer(
             Atom._None,
             self.WINDOW,
             0,
             0,
             0,
             0,
             self.RANDOM_INT,
             self.RANDOM_INT
         ),
         call.flush()
     ])