Skip to content

Commit

Permalink
test: Add tests for all options setters/getters.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 26, 2024
1 parent 724b9d9 commit 0cb9bbb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
1 change: 1 addition & 0 deletions pytox/toxcore/tox.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class Tox_Options_Ptr:
proxy_host: Any
proxy_port: Any
proxy_type: Any
savedata_data: Any
savedata_type: Any
start_port: Any
tcp_port: Any
Expand Down
18 changes: 16 additions & 2 deletions pytox/toxcore/tox.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# cython: language_level=3, linetrace=True
from libc.string cimport memcpy
from pytox import common
from types import TracebackType
from typing import TypeVar
Expand Down Expand Up @@ -264,11 +265,16 @@ cdef class Tox_Options_Ptr:

@property
def proxy_host(self) -> str:
return str(tox_options_get_proxy_host(self._get()))
return tox_options_get_proxy_host(self._get()).decode("utf-8")

@proxy_host.setter
def proxy_host(self, proxy_host: str):
tox_options_set_proxy_host(self._get(), proxy_host)
cdef size_t size = len(proxy_host) + 1
proxy_host_bytes = proxy_host.encode("utf-8") + b"\0"
cdef const char *proxy_host_chars = proxy_host_bytes
cdef char *data = <char*> malloc(size * sizeof(char)) # LEAK!
memcpy(data, proxy_host_chars, size)
tox_options_set_proxy_host(self._get(), data)

@property
def proxy_port(self) -> int:
Expand Down Expand Up @@ -318,6 +324,14 @@ cdef class Tox_Options_Ptr:
def savedata_type(self, savedata_type: Tox_Savedata_Type):
tox_options_set_savedata_type(self._get(), savedata_type)

@property
def savedata_data(self) -> bytes:
raise Exception("Not implemented") # TODO(iphydf): Implement

@savedata_data.setter
def savedata_data(self, savedata_data: bytes):
tox_options_set_savedata_data(self._get(), savedata_data, len(savedata_data))

@property
def experimental_thread_safety(self) -> bool:
return tox_options_get_experimental_thread_safety(self._get())
Expand Down
54 changes: 52 additions & 2 deletions test/tox_options_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,61 @@ def test_options(self) -> None:
opts.ipv6_enabled = False
self.assertFalse(opts.ipv6_enabled)

self.assertTrue(opts.udp_enabled)
opts.udp_enabled = False
self.assertFalse(opts.udp_enabled)

self.assertTrue(opts.local_discovery_enabled)
opts.local_discovery_enabled = False
self.assertFalse(opts.local_discovery_enabled)

self.assertTrue(opts.dht_announcements_enabled)
opts.dht_announcements_enabled = False
self.assertFalse(opts.dht_announcements_enabled)

self.assertTrue(opts.hole_punching_enabled)
opts.hole_punching_enabled = False
self.assertFalse(opts.hole_punching_enabled)

self.assertEqual(opts.proxy_type, c.TOX_PROXY_TYPE_NONE)
opts.proxy_type = c.TOX_PROXY_TYPE_SOCKS5
self.assertEqual(opts.proxy_type, c.TOX_PROXY_TYPE_SOCKS5)

opts.proxy_host = "localhost"
self.assertEqual(opts.proxy_host, "localhost")

opts.proxy_port = 1234
self.assertEqual(opts.proxy_port, 1234)

opts.start_port = 1235
self.assertEqual(opts.start_port, 1235)

opts.end_port = 1236
self.assertEqual(opts.end_port, 1236)

opts.tcp_port = 1237
self.assertEqual(opts.tcp_port, 1237)

opts.savedata_type = c.TOX_SAVEDATA_TYPE_TOX_SAVE
self.assertEqual(opts.savedata_type, c.TOX_SAVEDATA_TYPE_TOX_SAVE)

# Can't test whether it works, but at least we can test that it doesn't crash.
opts.savedata_data = b"test"

self.assertFalse(opts.experimental_thread_safety)
opts.experimental_thread_safety = True
self.assertTrue(opts.experimental_thread_safety)

self.assertFalse(opts.experimental_groups_persistence)
opts.experimental_groups_persistence = True
self.assertTrue(opts.experimental_groups_persistence)


def test_use_after_free(self) -> None:
with c.Tox_Options_Ptr() as opts:
saved_opts = opts
pass
with self.assertRaises(common.UseAfterFreeException):
print(saved_opts.ipv6_enabled)
print(opts.ipv6_enabled)


if __name__ == "__main__":
Expand Down

0 comments on commit 0cb9bbb

Please sign in to comment.