Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add tests for all options setters/getters. #117

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ COPY test /build/test
RUN . /path/to/venv/bin/activate \
&& coverage run -m unittest discover -v -p "*_test.py"
RUN . /path/to/venv/bin/activate \
&& coverage report -m --fail-under=65
&& coverage report -m --fail-under=67
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
53 changes: 51 additions & 2 deletions test/tox_options_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,60 @@ 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