Skip to content

Commit 0cc7616

Browse files
committed
test: Add more tests for bootstrap.
Also fixed some incorrect types in udp/tcp port getters.
1 parent 9c7efc6 commit 0cc7616

File tree

10 files changed

+79
-17
lines changed

10 files changed

+79
-17
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ COPY test /build/test
4444
RUN . /path/to/venv/bin/activate \
4545
&& coverage run -m unittest discover -v -p "*_test.py"
4646
RUN . /path/to/venv/bin/activate \
47-
&& coverage report -m --fail-under=67
47+
&& coverage report -m --fail-under=70

pytox/common.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ class PytoxException(Exception):
1010

1111

1212
class ApiException(PytoxException):
13+
code: enum.Enum
14+
1315
def __init__(self, err: enum.Enum):
1416
super().__init__(err.name)
15-
self.error = err
17+
self.code = err
1618

1719

1820
class LengthException(PytoxException):

pytox/toxcore/tox.pyx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,10 @@ cdef class Tox_Ptr:
473473
def bootstrap(self, host: str, port: int, public_key: bytes) -> bool:
474474
common.check_len("public_key", public_key, tox_public_key_size())
475475
cdef Tox_Err_Bootstrap err = TOX_ERR_BOOTSTRAP_OK
476-
return tox_bootstrap(self._get(), host.encode("utf-8"), port, public_key, &err)
476+
cdef bool res = tox_bootstrap(self._get(), host.encode("utf-8"), port, public_key, &err)
477+
if err:
478+
raise ApiException(Tox_Err_Bootstrap(err))
479+
return res
477480

478481
@property
479482
def iteration_interval(self) -> int:
@@ -515,15 +518,15 @@ cdef class Tox_Ptr:
515518
@property
516519
def udp_port(self) -> int:
517520
cdef Tox_Err_Get_Port err = TOX_ERR_GET_PORT_OK
518-
cdef bool res = tox_self_get_udp_port(self._get(), &err)
521+
cdef uint16_t res = tox_self_get_udp_port(self._get(), &err)
519522
if err:
520523
raise ApiException(Tox_Err_Get_Port(err))
521524
return res
522525

523526
@property
524527
def tcp_port(self) -> int:
525528
cdef Tox_Err_Get_Port err = TOX_ERR_GET_PORT_OK
526-
cdef bool res = tox_self_get_tcp_port(self._get(), &err)
529+
cdef uint16_t res = tox_self_get_tcp_port(self._get(), &err)
527530
if err:
528531
raise ApiException(Tox_Err_Get_Port(err))
529532
return res

test/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
load("@rules_python//python:defs.bzl", "py_test")
22
load("//third_party/python:build_defs.bzl", "mypy_test")
33

4-
TESTS = glob(["**/*.py"])
4+
TESTS = glob(["**/*_test.py"])
55

66
[py_test(
77
name = src[:-3],

test/auto_tests/auto_test.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ class TestTox(core.Tox_Ptr):
4848
conferences: dict[int, ConferenceInfo]
4949

5050
def __init__(self, index: int) -> None:
51-
super(TestTox, self).__init__()
51+
with core.Tox_Options_Ptr() as options:
52+
options.local_discovery_enabled = False
53+
super(TestTox, self).__init__(options)
5254
self.index = index
5355
self.friends = collections.defaultdict(FriendInfo)
5456
self.conferences = collections.defaultdict(ConferenceInfo)
@@ -176,6 +178,9 @@ def _iterate(self, max_iterate: int, cond: Callable[[], bool]) -> None:
176178
self.fail(f"condition not met after {max_iterate} iterations")
177179

178180
def _wait_for_self_online(self) -> None:
181+
self.tox2.bootstrap("127.0.0.1", self.tox1.udp_port, self.tox1.dht_id)
182+
self.tox3.bootstrap("127.0.0.1", self.tox1.udp_port, self.tox1.dht_id)
183+
179184
def is_online() -> bool:
180185
return bool(
181186
self.tox1.connection_status == core.TOX_CONNECTION_NONE
@@ -226,7 +231,7 @@ def test_friend_by_public_key(self) -> None:
226231
with self.assertRaises(core.ApiException) as ex:
227232
# We're not our own friend.
228233
self.tox1.friend_by_public_key(self.tox1.public_key)
229-
self.assertEqual(ex.exception.error,
234+
self.assertEqual(ex.exception.code,
230235
core.TOX_ERR_FRIEND_BY_PUBLIC_KEY_NOT_FOUND)
231236

232237
def test_send_message(self) -> None:

test/auto_tests/self_connection_status_test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ class TestTox(core.Tox_Ptr):
2424
friends: dict[int, FriendInfo]
2525

2626
def __init__(self, index: int) -> None:
27-
super(TestTox, self).__init__()
27+
with core.Tox_Options_Ptr() as options:
28+
options.local_discovery_enabled = False
29+
super(TestTox, self).__init__(options)
2830
self.index = index
2931
self.friends = collections.defaultdict(FriendInfo)
3032

@@ -64,6 +66,9 @@ def _iterate(self, max_iterate: int, cond: Callable[[], bool]) -> None:
6466
self.fail(f"condition not met after {max_iterate} iterations")
6567

6668
def _wait_for_self_online(self) -> None:
69+
self.tox2.bootstrap("127.0.0.1", self.tox1.udp_port, self.tox1.dht_id)
70+
self.tox3.bootstrap("127.0.0.1", self.tox1.udp_port, self.tox1.dht_id)
71+
6772
def is_online() -> bool:
6873
return bool(
6974
self.tox1.connection_status == core.TOX_CONNECTION_NONE

test/tox_options_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def test_options(self) -> None:
5151

5252
# Can't test whether it works, but at least we can test that it doesn't crash.
5353
opts.savedata_data = b"test"
54+
with self.assertRaises(Exception):
55+
# not implemented
56+
print(opts.savedata_data)
5457

5558
self.assertFalse(opts.experimental_thread_safety)
5659
opts.experimental_thread_safety = True

test/tox_test.py

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ class ToxTest(unittest.TestCase):
99
def test_version(self) -> None:
1010
self.assertEqual(len(c.VERSION.split(".")), 3)
1111

12-
def test_options(self) -> None:
13-
opts = c.Tox_Options_Ptr()
14-
self.assertTrue(opts.ipv6_enabled)
15-
opts.ipv6_enabled = False
16-
self.assertFalse(opts.ipv6_enabled)
17-
1812
def test_use_after_free(self) -> None:
1913
opts = c.Tox_Options_Ptr()
2014
with c.Tox_Ptr(opts) as tox:
@@ -31,11 +25,26 @@ def test_pass_none(self) -> None:
3125
with c.Tox_Ptr(None):
3226
pass
3327

28+
def test_pass_invalid_options(self) -> None:
29+
opts = c.Tox_Options_Ptr()
30+
opts.proxy_type = c.TOX_PROXY_TYPE_SOCKS5
31+
opts.proxy_host = "invalid-host"
32+
opts.proxy_port = 1234
33+
with self.assertRaises(c.ApiException) as e:
34+
c.Tox_Ptr(opts)
35+
self.assertEqual(e.exception.code, c.TOX_ERR_NEW_PROXY_BAD_HOST)
36+
3437
def test_address(self) -> None:
3538
opts = c.Tox_Options_Ptr()
3639
with c.Tox_Ptr(opts) as tox:
3740
self.assertEqual(tox.address, tox.address)
3841

42+
def test_nospam(self) -> None:
43+
with c.Tox_Ptr(None) as tox:
44+
tox.nospam = 0x12345678
45+
self.assertEqual(tox.nospam, 0x12345678)
46+
self.assertEqual(tox.address[-6:-2].hex(), "12345678")
47+
3948
def test_public_key_is_address_prefix(self) -> None:
4049
opts = c.Tox_Options_Ptr()
4150
with c.Tox_Ptr(opts) as tox:
@@ -89,6 +98,17 @@ def test_friend_add(self) -> None:
8998
with self.assertRaises(common.LengthException):
9099
tox2.friend_add(tox1.public_key, b"oh no!")
91100

101+
def test_invalid_bootstrap(self) -> None:
102+
with c.Tox_Ptr() as tox:
103+
with self.assertRaises(c.ApiException) as e:
104+
tox.bootstrap("invalid-host", 1234, bytes(c.PUBLIC_KEY_SIZE))
105+
self.assertEqual(e.exception.code, c.TOX_ERR_BOOTSTRAP_BAD_HOST)
106+
107+
def test_bootstrap_checks_key_length(self) -> None:
108+
with c.Tox_Ptr() as tox:
109+
with self.assertRaises(common.LengthException):
110+
tox.bootstrap("localhost", 1234, bytes(c.PUBLIC_KEY_SIZE - 1))
111+
92112
def test_friend_delete(self) -> None:
93113
with c.Tox_Ptr() as tox1:
94114
with c.Tox_Ptr() as tox2:
@@ -98,6 +118,30 @@ def test_friend_delete(self) -> None:
98118
# Deleting again: we don't have that friend anymore.
99119
tox1.friend_delete(0)
100120

121+
def test_udp_port_fails_when_udp_disabled(self) -> None:
122+
with c.Tox_Options_Ptr() as opts:
123+
opts.udp_enabled = False
124+
with c.Tox_Ptr(opts) as tox:
125+
with self.assertRaises(c.ApiException) as e:
126+
print(tox.udp_port)
127+
self.assertEqual(e.exception.code,
128+
c.TOX_ERR_GET_PORT_NOT_BOUND)
129+
130+
def test_tcp_port_fails_when_tcp_disabled(self) -> None:
131+
with c.Tox_Options_Ptr() as opts:
132+
opts.tcp_port = 0
133+
with c.Tox_Ptr(opts) as tox:
134+
with self.assertRaises(c.ApiException) as e:
135+
print(tox.tcp_port)
136+
self.assertEqual(e.exception.code,
137+
c.TOX_ERR_GET_PORT_NOT_BOUND)
138+
139+
def test_tcp_port(self) -> None:
140+
with c.Tox_Options_Ptr() as opts:
141+
opts.tcp_port = 1234
142+
with c.Tox_Ptr(opts) as tox:
143+
self.assertEqual(tox.tcp_port, 1234)
144+
101145

102146
if __name__ == "__main__":
103147
unittest.main()

test/toxav_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class AvTest(unittest.TestCase):
99
def test_version(self) -> None:
1010
with self.assertRaises(av.ApiException) as ex:
1111
av.Toxav_Ptr(cast(tox.Tox_Ptr, None))
12-
self.assertEqual(ex.exception.error, av.TOXAV_ERR_NEW_NULL)
12+
self.assertEqual(ex.exception.code, av.TOXAV_ERR_NEW_NULL)
1313

1414

1515
if __name__ == "__main__":

test/toxencryptsave_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_encrypt_decrypt_with_wrong_salt(self) -> None:
2222
with c.Tox_Pass_Key_Ptr(b"hello", b"b" * 32) as pk2:
2323
with self.assertRaises(c.ApiException) as ex:
2424
pk2.decrypt(pk1.encrypt(b"hello world"))
25-
self.assertEqual(ex.exception.error.name,
25+
self.assertEqual(ex.exception.code.name,
2626
c.TOX_ERR_DECRYPTION_FAILED.name)
2727

2828
def test_salt_too_small(self) -> None:

0 commit comments

Comments
 (0)