File: test_tractorrc.py

package info (click to toggle)
tractor 5.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 248 kB
  • sloc: python: 1,489; xml: 71; makefile: 4; sh: 2
file content (120 lines) | stat: -rw-r--r-- 3,484 bytes parent folder | download
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
# Released under GPLv3+ License
# Danial Behzadi <dani.behzi@ubuntu.com>, 2024.
"""
unit tests for tractorrc
"""

import unittest
from unittest.mock import patch
from tractor import tractorrc


class GetExitLines(unittest.TestCase):
    """
    test case for _get_exit_lines
    """

    @patch("tractor.db.get_val", return_value="ww")
    def test_auto(self, *_):
        """
        auto exit node
        """
        self.assertEqual(tractorrc._get_exit_lines(), "")

    @patch("tractor.db.get_val", return_value="us")
    def test_us(self, *_):
        """
        specific exit node
        """
        self.assertEqual(
            tractorrc._get_exit_lines(), "ExitNodes {us}\nStrictNodes 1\n"
        )


class FillBridgeLines(unittest.TestCase):
    """
    test case for _fill_bridge_lines
    """

    @patch("tractor.db.get_val", return_value="obfs4proxy")
    def test_obfs(self, *_):
        """
        obfs lines
        """
        self.assertEqual(
            tractorrc._fill_bridge_lines("obfs4", ["line1", "line2"]),
            "UseBridges 1\n"
            + "ClientTransportPlugin meek_lite,obfs2,obfs3,obfs4,scramblesuit"
            + ",webtunnel exec obfs4proxy\n"
            + "Bridge line1\n"
            + "Bridge line2\n",
        )

    @patch("tractor.db.get_val", return_value="snowflake")
    def test_snowflake(self, *_):
        """
        obfs lines
        """
        self.assertEqual(
            tractorrc._fill_bridge_lines("snowflake", ["line1", "line2"]),
            "UseBridges 1\n"
            + "ClientTransportPlugin snowflake exec snowflake "
            + "-ice stun:stun.antisip.com:347,stun:stun.epygi.com:3478,"
            + "stun:stun.uls.co.za:3478,stun:stun.voipgate.com:3478,"
            + "stun:stun.mixvoip.com:3478,stun:stun.nextcloud.com:3478,"
            + "stun:stun.bethesda.net:3478,stun:stun.nextcloud.com:443\n"
            + "Bridge line1\n"
            + "Bridge line2\n",
        )

    def test_bad_type(self, *_):
        """
        bad bridge typr
        """
        with self.assertRaises(ValueError):
            tractorrc._fill_bridge_lines(0, "line")


class GetBridgeLines(unittest.TestCase):
    """
    test case for _get_bridge_lines
    """

    @patch("tractor.db.get_val", return_value=2)
    @patch("tractor.bridges.relevant_lines", return_value=None)
    def test_no_bridge(self, *_):
        """
        No relevant bridges found
        """
        with self.assertRaises(EnvironmentError):
            tractorrc._get_bridge_lines()


class Create(unittest.TestCase):
    """
    test case for create
    """

    @patch("tractor.tractorrc._get_upstream_line", return_value="")
    @patch("tractor.db.get_val", return_value="none")
    @patch("os.chmod")
    def test_local(self, *_):
        """
        local connection
        """
        tmpdir, path = tractorrc.create()
        self.assertTrue(tmpdir.startswith("/tmp/"))
        self.assertTrue(path.endswith("/tractorrc"))

    @patch("tractor.tractorrc._get_bridge_lines", return_value=None)
    @patch("tractor.tractorrc._get_upstream_line", return_value="")
    @patch("tractor.bridges.relevant_lines", return_value=None)
    @patch("tractor.db.get_val", return_value=True)
    @patch("os.chmod")
    def test_network(self, *_):
        """
        listen on network
        """
        tmpdir, path = tractorrc.create()
        self.assertTrue(tmpdir.startswith("/tmp/"))
        self.assertTrue(path.endswith("/tractorrc"))