summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozdevice/tests/test_socket_connection.py
blob: 11827375465f75920afbd8f2e11ff2e58354eb38 (plain)
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
#!/usr/bin/env python

import mozunit
import pytest
from conftest import random_tcp_port


@pytest.fixture(params=["tcp:{}".format(random_tcp_port()) for _ in range(5)])
def select_test_port(request):
    """Generate a list of ports to be used for testing."""
    yield request.param


def test_list_socket_connections_reverse(mock_adb_object):
    assert [("['reverse',", "'--list']")] == mock_adb_object.list_socket_connections(
        "reverse"
    )


def test_list_socket_connections_forward(mock_adb_object):
    assert [("['forward',", "'--list']")] == mock_adb_object.list_socket_connections(
        "forward"
    )


def test_create_socket_connection_reverse(
    mock_adb_object, select_test_port, redirect_stdout_and_assert
):
    _expected = "['reverse', '{0}', '{0}']".format(select_test_port)
    redirect_stdout_and_assert(
        mock_adb_object.create_socket_connection,
        direction="reverse",
        local=select_test_port,
        remote=select_test_port,
        text=_expected,
    )


def test_create_socket_connection_forward(
    mock_adb_object, select_test_port, redirect_stdout_and_assert
):
    _expected = "['forward', '{0}', '{0}']".format(select_test_port)
    redirect_stdout_and_assert(
        mock_adb_object.create_socket_connection,
        direction="forward",
        local=select_test_port,
        remote=select_test_port,
        text=_expected,
    )


def test_create_socket_connection_forward_adb_assigned_port(
    mock_adb_object, select_test_port
):
    result = mock_adb_object.create_socket_connection(
        direction="forward", local="tcp:0", remote=select_test_port
    )
    assert isinstance(result, int) and result == 7777


def test_remove_socket_connections_reverse(mock_adb_object, redirect_stdout_and_assert):
    _expected = "['reverse', '--remove-all']"
    redirect_stdout_and_assert(
        mock_adb_object.remove_socket_connections, direction="reverse", text=_expected
    )


def test_remove_socket_connections_forward(mock_adb_object, redirect_stdout_and_assert):
    _expected = "['forward', '--remove-all']"
    redirect_stdout_and_assert(
        mock_adb_object.remove_socket_connections, direction="forward", text=_expected
    )


def test_legacy_forward(mock_adb_object, select_test_port, redirect_stdout_and_assert):
    _expected = "['forward', '{0}', '{0}']".format(select_test_port)
    redirect_stdout_and_assert(
        mock_adb_object.forward,
        local=select_test_port,
        remote=select_test_port,
        text=_expected,
    )


def test_legacy_forward_adb_assigned_port(mock_adb_object, select_test_port):
    result = mock_adb_object.forward(local="tcp:0", remote=select_test_port)
    assert isinstance(result, int) and result == 7777


def test_legacy_reverse(mock_adb_object, select_test_port, redirect_stdout_and_assert):
    _expected = "['reverse', '{0}', '{0}']".format(select_test_port)
    redirect_stdout_and_assert(
        mock_adb_object.reverse,
        local=select_test_port,
        remote=select_test_port,
        text=_expected,
    )


def test_validate_port_invalid_prefix(mock_adb_object):
    with pytest.raises(ValueError):
        mock_adb_object._validate_port("{}".format("invalid"), is_local=True)


@pytest.mark.xfail
def test_validate_port_non_numerical_port_identifier(mock_adb_object):
    with pytest.raises(AttributeError):
        mock_adb_object._validate_port(
            "{}".format("tcp:this:is:not:a:number"), is_local=True
        )


def test_validate_port_identifier_length_short(mock_adb_object):
    with pytest.raises(ValueError):
        mock_adb_object._validate_port("{}".format("tcp"), is_local=True)


def test_validate_direction(mock_adb_object):
    with pytest.raises(ValueError):
        mock_adb_object._validate_direction("{}".format("bad direction"))


if __name__ == "__main__":
    mozunit.main()