2019-11-08 03:44:21 -05:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import tempfile
|
2020-08-21 16:42:20 -04:00
|
|
|
import sys
|
2019-11-08 03:44:21 -05:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2020-10-13 01:40:55 -04:00
|
|
|
from onionshare_cli import common, settings
|
2019-11-08 03:44:21 -05:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def settings_obj(sys_onionshare_dev_mode, platform_linux):
|
|
|
|
_common = common.Common()
|
|
|
|
_common.version = "DUMMY_VERSION_1.2.3"
|
|
|
|
return settings.Settings(_common)
|
|
|
|
|
|
|
|
|
|
|
|
class TestSettings:
|
|
|
|
def test_init(self, settings_obj):
|
|
|
|
expected_settings = {
|
|
|
|
"version": "DUMMY_VERSION_1.2.3",
|
|
|
|
"connection_type": "bundled",
|
|
|
|
"control_port_address": "127.0.0.1",
|
|
|
|
"control_port_port": 9051,
|
|
|
|
"socks_address": "127.0.0.1",
|
|
|
|
"socks_port": 9050,
|
|
|
|
"socket_file_path": "/var/run/tor/control",
|
|
|
|
"auth_type": "no_auth",
|
|
|
|
"auth_password": "",
|
|
|
|
"use_autoupdate": True,
|
|
|
|
"autoupdate_timestamp": None,
|
2021-10-27 00:06:05 -04:00
|
|
|
"bridges_enabled": False,
|
|
|
|
"bridges_type": "built-in",
|
|
|
|
"bridges_builtin_pt": "obfs4",
|
|
|
|
"bridges_moat": "",
|
|
|
|
"bridges_custom": "",
|
2021-11-24 01:55:47 -05:00
|
|
|
"bridges_builtin": {},
|
2019-11-08 03:44:21 -05:00
|
|
|
"persistent_tabs": [],
|
2021-10-15 12:21:58 -04:00
|
|
|
"theme": 0,
|
2019-11-08 03:44:21 -05:00
|
|
|
}
|
|
|
|
for key in settings_obj._settings:
|
|
|
|
# Skip locale, it will not always default to the same thing
|
|
|
|
if key != "locale":
|
|
|
|
assert settings_obj._settings[key] == settings_obj.default_settings[key]
|
|
|
|
assert settings_obj._settings[key] == expected_settings[key]
|
|
|
|
|
|
|
|
def test_fill_in_defaults(self, settings_obj):
|
|
|
|
del settings_obj._settings["version"]
|
|
|
|
settings_obj.fill_in_defaults()
|
|
|
|
assert settings_obj._settings["version"] == "DUMMY_VERSION_1.2.3"
|
|
|
|
|
2019-11-08 12:31:22 -05:00
|
|
|
def test_load(self, temp_dir, settings_obj):
|
2019-11-08 03:44:21 -05:00
|
|
|
custom_settings = {
|
|
|
|
"version": "CUSTOM_VERSION",
|
|
|
|
"socks_port": 9999,
|
|
|
|
"use_stealth": True,
|
|
|
|
}
|
2021-12-02 00:01:32 -05:00
|
|
|
tmp_file, tmp_file_path = tempfile.mkstemp(dir=temp_dir.name)
|
2019-11-08 03:44:21 -05:00
|
|
|
with open(tmp_file, "w") as f:
|
|
|
|
json.dump(custom_settings, f)
|
|
|
|
settings_obj.filename = tmp_file_path
|
|
|
|
settings_obj.load()
|
|
|
|
|
|
|
|
assert settings_obj._settings["version"] == "CUSTOM_VERSION"
|
|
|
|
assert settings_obj._settings["socks_port"] == 9999
|
|
|
|
assert settings_obj._settings["use_stealth"] is True
|
|
|
|
|
|
|
|
os.remove(tmp_file_path)
|
|
|
|
assert os.path.exists(tmp_file_path) is False
|
|
|
|
|
2019-11-08 12:31:22 -05:00
|
|
|
def test_save(self, monkeypatch, temp_dir, settings_obj):
|
2019-11-08 03:44:21 -05:00
|
|
|
settings_filename = "default_settings.json"
|
2021-12-02 00:01:32 -05:00
|
|
|
new_temp_dir = tempfile.mkdtemp(dir=temp_dir.name)
|
2019-11-08 12:31:22 -05:00
|
|
|
settings_path = os.path.join(new_temp_dir, settings_filename)
|
2019-11-08 03:44:21 -05:00
|
|
|
settings_obj.filename = settings_path
|
|
|
|
settings_obj.save()
|
|
|
|
with open(settings_path, "r") as f:
|
|
|
|
settings = json.load(f)
|
|
|
|
|
|
|
|
assert settings_obj._settings == settings
|
|
|
|
|
|
|
|
os.remove(settings_path)
|
|
|
|
assert os.path.exists(settings_path) is False
|
|
|
|
|
|
|
|
def test_get(self, settings_obj):
|
|
|
|
assert settings_obj.get("version") == "DUMMY_VERSION_1.2.3"
|
|
|
|
assert settings_obj.get("connection_type") == "bundled"
|
|
|
|
assert settings_obj.get("control_port_address") == "127.0.0.1"
|
|
|
|
assert settings_obj.get("control_port_port") == 9051
|
|
|
|
assert settings_obj.get("socks_address") == "127.0.0.1"
|
|
|
|
assert settings_obj.get("socks_port") == 9050
|
|
|
|
assert settings_obj.get("socket_file_path") == "/var/run/tor/control"
|
|
|
|
assert settings_obj.get("auth_type") == "no_auth"
|
|
|
|
assert settings_obj.get("auth_password") == ""
|
|
|
|
assert settings_obj.get("use_autoupdate") is True
|
|
|
|
assert settings_obj.get("autoupdate_timestamp") is None
|
|
|
|
assert settings_obj.get("autoupdate_timestamp") is None
|
2021-10-27 00:06:05 -04:00
|
|
|
assert settings_obj.get("bridges_enabled") is False
|
|
|
|
assert settings_obj.get("bridges_type") == "built-in"
|
|
|
|
assert settings_obj.get("bridges_builtin_pt") == "obfs4"
|
|
|
|
assert settings_obj.get("bridges_moat") == ""
|
|
|
|
assert settings_obj.get("bridges_custom") == ""
|
2019-11-08 03:44:21 -05:00
|
|
|
|
|
|
|
def test_set_version(self, settings_obj):
|
|
|
|
settings_obj.set("version", "CUSTOM_VERSION")
|
|
|
|
assert settings_obj._settings["version"] == "CUSTOM_VERSION"
|
|
|
|
|
|
|
|
def test_set_control_port_port(self, settings_obj):
|
|
|
|
settings_obj.set("control_port_port", 999)
|
|
|
|
assert settings_obj._settings["control_port_port"] == 999
|
|
|
|
|
|
|
|
settings_obj.set("control_port_port", "NON_INTEGER")
|
|
|
|
assert settings_obj._settings["control_port_port"] == 9051
|
|
|
|
|
|
|
|
def test_set_socks_port(self, settings_obj):
|
|
|
|
settings_obj.set("socks_port", 888)
|
|
|
|
assert settings_obj._settings["socks_port"] == 888
|
|
|
|
|
|
|
|
settings_obj.set("socks_port", "NON_INTEGER")
|
|
|
|
assert settings_obj._settings["socks_port"] == 9050
|
|
|
|
|
2020-08-21 16:42:20 -04:00
|
|
|
@pytest.mark.skipif(sys.platform != "Darwin", reason="requires Darwin")
|
2019-11-08 03:44:21 -05:00
|
|
|
def test_filename_darwin(self, monkeypatch, platform_darwin):
|
|
|
|
obj = settings.Settings(common.Common())
|
|
|
|
assert obj.filename == os.path.expanduser(
|
|
|
|
"~/Library/Application Support/OnionShare-testdata/onionshare.json"
|
|
|
|
)
|
|
|
|
|
2021-09-01 23:01:30 -04:00
|
|
|
@pytest.mark.skipif(sys.platform != "linux", reason="requires Linux")
|
2019-11-08 03:44:21 -05:00
|
|
|
def test_filename_linux(self, monkeypatch, platform_linux):
|
|
|
|
obj = settings.Settings(common.Common())
|
|
|
|
assert obj.filename == os.path.expanduser(
|
|
|
|
"~/.config/onionshare-testdata/onionshare.json"
|
|
|
|
)
|
|
|
|
|
2020-08-21 16:42:20 -04:00
|
|
|
@pytest.mark.skipif(sys.platform != "win32", reason="requires Windows")
|
|
|
|
def test_filename_windows(self, monkeypatch, platform_windows):
|
|
|
|
obj = settings.Settings(common.Common())
|
|
|
|
assert obj.filename == os.path.expanduser(
|
|
|
|
"~\\AppData\\Roaming\\OnionShare-testdata\\onionshare.json"
|
|
|
|
)
|
|
|
|
|
2019-11-08 03:44:21 -05:00
|
|
|
def test_set_custom_bridge(self, settings_obj):
|
|
|
|
settings_obj.set(
|
2021-10-27 00:07:38 -04:00
|
|
|
"bridges_custom",
|
2019-11-08 03:44:21 -05:00
|
|
|
"Bridge 45.3.20.65:9050 21300AD88890A49C429A6CB9959CFD44490A8F6E",
|
|
|
|
)
|
|
|
|
assert (
|
2021-10-27 00:07:38 -04:00
|
|
|
settings_obj._settings["bridges_custom"]
|
2019-11-08 03:44:21 -05:00
|
|
|
== "Bridge 45.3.20.65:9050 21300AD88890A49C429A6CB9959CFD44490A8F6E"
|
|
|
|
)
|