mirror of
https://github.com/onionshare/onionshare.git
synced 2025-01-25 22:15:57 -05:00
Merge branch 'unit-tests' of https://github.com/ecmendenhall/onionshare into ecmendenhall-unit-tests
This commit is contained in:
commit
9ffa0937d2
15
README.md
15
README.md
@ -58,3 +58,18 @@ Since OnionShare is a command line program, and using it involves copying and pa
|
|||||||
127.0.0.1 - - [22/May/2014 11:31:02] "GET /912d927863347b7b97f7a268a4210694/download HTTP/1.1" 200 -
|
127.0.0.1 - - [22/May/2014 11:31:02] "GET /912d927863347b7b97f7a268a4210694/download HTTP/1.1" 200 -
|
||||||
127.0.0.1 - - [22/May/2014 11:31:14] "GET /912d927863347b7b97f7a268a4210694/download HTTP/1.1" 200 -
|
127.0.0.1 - - [22/May/2014 11:31:14] "GET /912d927863347b7b97f7a268a4210694/download HTTP/1.1" 200 -
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
|
||||||
|
Onionshare includes [nose](https://nose.readthedocs.org/en/latest/) unit tests. First,
|
||||||
|
|
||||||
|
```sh
|
||||||
|
sudo pip install nose
|
||||||
|
```
|
||||||
|
|
||||||
|
Then run
|
||||||
|
|
||||||
|
```sh
|
||||||
|
nosetests test
|
||||||
|
```
|
||||||
|
|
||||||
|
in the onionshare project directory to run them.
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from onionshare import *
|
from onionshare import *
|
||||||
|
import tempfile
|
||||||
|
|
||||||
def test_get_platform_returns_env_var():
|
def test_get_platform_returns_env_var():
|
||||||
"get_platform() returns ONIONSHARE_PLATFORM from the environment"
|
"get_platform() returns ONIONSHARE_PLATFORM from the environment"
|
||||||
@ -6,10 +7,7 @@ def test_get_platform_returns_env_var():
|
|||||||
assert get_platform() == 'TI-83+'
|
assert get_platform() == 'TI-83+'
|
||||||
|
|
||||||
def test_get_platform_returns_platform_system():
|
def test_get_platform_returns_platform_system():
|
||||||
"""
|
"get_platform() returns platform.system() when ONIONSHARE_PLATFORM is not defined"
|
||||||
get_platform() returns platform.system() when
|
|
||||||
ONIONSHARE_PLATFORM is not defined
|
|
||||||
"""
|
|
||||||
os.environ.pop('ONIONSHARE_PLATFORM', None)
|
os.environ.pop('ONIONSHARE_PLATFORM', None)
|
||||||
onionshare.platform.system = lambda: 'Sega Saturn'
|
onionshare.platform.system = lambda: 'Sega Saturn'
|
||||||
assert get_platform() == 'Sega Saturn'
|
assert get_platform() == 'Sega Saturn'
|
||||||
@ -22,10 +20,7 @@ def test_tails_appends_to_path():
|
|||||||
assert sys.path[-1][-13:] == '/../tails/lib'
|
assert sys.path[-1][-13:] == '/../tails/lib'
|
||||||
|
|
||||||
def test_get_hidden_service_dir_windows_with_temp():
|
def test_get_hidden_service_dir_windows_with_temp():
|
||||||
"""
|
"get_hidden_service_dir() uses a directory from the Windows environment when defined"
|
||||||
get_hidden_service_dir() uses a temporary directory from the
|
|
||||||
Windows environment when defined
|
|
||||||
"""
|
|
||||||
onionshare.platform.system = lambda: 'Windows'
|
onionshare.platform.system = lambda: 'Windows'
|
||||||
os.environ['Temp'] = "C:\Internet Explorer\Secrets"
|
os.environ['Temp'] = "C:\Internet Explorer\Secrets"
|
||||||
expected_path = "C:/Internet Explorer/Secrets/onionshare_hidden_service_port"
|
expected_path = "C:/Internet Explorer/Secrets/onionshare_hidden_service_port"
|
||||||
@ -64,10 +59,27 @@ def test_tails_open_port():
|
|||||||
onionshare.tails_open_port('port')
|
onionshare.tails_open_port('port')
|
||||||
|
|
||||||
expected_call = [
|
expected_call = [
|
||||||
'/sbin/iptables', '-I', 'OUTPUT',
|
'/sbin/iptables', '-I', 'OUTPUT',
|
||||||
'-o', 'lo', '-p',
|
'-o', 'lo', '-p',
|
||||||
'tcp', '--dport', 'port', '-j', 'ACCEPT'
|
'tcp', '--dport', 'port', '-j', 'ACCEPT'
|
||||||
]
|
]
|
||||||
|
actual_call = mock_subprocess.last_call_args()
|
||||||
|
assert actual_call == expected_call
|
||||||
|
|
||||||
|
def test_tails_close_port():
|
||||||
|
"tails_close_port() calls iptables with REJECT arg"
|
||||||
|
onionshare.get_platform = lambda: 'Tails'
|
||||||
|
onionshare.strings = {'closing_hole': ''}
|
||||||
|
|
||||||
|
mock_subprocess = MockSubprocess()
|
||||||
|
onionshare.subprocess = mock_subprocess
|
||||||
|
onionshare.tails_close_port('port')
|
||||||
|
|
||||||
|
expected_call = [
|
||||||
|
'/sbin/iptables', '-I', 'OUTPUT',
|
||||||
|
'-o', 'lo', '-p',
|
||||||
|
'tcp', '--dport', 'port', '-j', 'REJECT'
|
||||||
|
]
|
||||||
actual_call = mock_subprocess.last_call_args()
|
actual_call = mock_subprocess.last_call_args()
|
||||||
assert actual_call == expected_call
|
assert actual_call == expected_call
|
||||||
|
|
||||||
@ -81,26 +93,8 @@ def test_load_strings_loads_other_languages():
|
|||||||
"load_strings() loads other languages in different locales"
|
"load_strings() loads other languages in different locales"
|
||||||
locale.getdefaultlocale = lambda: ('fr_FR', 'UTF-8')
|
locale.getdefaultlocale = lambda: ('fr_FR', 'UTF-8')
|
||||||
load_strings("fr")
|
load_strings("fr")
|
||||||
print onionshare.strings
|
|
||||||
assert onionshare.strings['calculating_sha1'] == "Calculer un hachage SHA-1."
|
assert onionshare.strings['calculating_sha1'] == "Calculer un hachage SHA-1."
|
||||||
|
|
||||||
def test_tails_close_port():
|
|
||||||
"tails_close_port() calls iptables with REJECT arg"
|
|
||||||
onionshare.get_platform = lambda: 'Tails'
|
|
||||||
onionshare.strings = {'closing_hole': ''}
|
|
||||||
|
|
||||||
mock_subprocess = MockSubprocess()
|
|
||||||
onionshare.subprocess = mock_subprocess
|
|
||||||
onionshare.tails_close_port('port')
|
|
||||||
|
|
||||||
expected_call = [
|
|
||||||
'/sbin/iptables', '-I', 'OUTPUT',
|
|
||||||
'-o', 'lo', '-p',
|
|
||||||
'tcp', '--dport', 'port', '-j', 'REJECT'
|
|
||||||
]
|
|
||||||
actual_call = mock_subprocess.last_call_args()
|
|
||||||
assert actual_call == expected_call
|
|
||||||
|
|
||||||
def test_generate_slug_length():
|
def test_generate_slug_length():
|
||||||
"generates a 32-character slug"
|
"generates a 32-character slug"
|
||||||
assert len(slug) == 32
|
assert len(slug) == 32
|
||||||
@ -117,3 +111,41 @@ def test_generate_slug_characters():
|
|||||||
def test_starts_with_empty_strings():
|
def test_starts_with_empty_strings():
|
||||||
"creates an empty strings dict by default"
|
"creates an empty strings dict by default"
|
||||||
assert strings == {}
|
assert strings == {}
|
||||||
|
|
||||||
|
def test_choose_port_returns_a_port_number():
|
||||||
|
"choose_port() returns a port number"
|
||||||
|
assert 1024 <= choose_port() <= 65535
|
||||||
|
|
||||||
|
def test_choose_port_returns_an_open_port():
|
||||||
|
"choose_port() returns an open port"
|
||||||
|
port = choose_port()
|
||||||
|
socket.socket().bind(("127.0.0.1", port))
|
||||||
|
|
||||||
|
def write_tempfile(text):
|
||||||
|
tempdir = tempfile.mkdtemp()
|
||||||
|
path = tempdir + "/test-file.txt"
|
||||||
|
with open(path, "w") as f:
|
||||||
|
f.write(text)
|
||||||
|
f.close()
|
||||||
|
return path
|
||||||
|
|
||||||
|
def test_filehash_returns_correct_hash():
|
||||||
|
"file_crunching() returns correct hash"
|
||||||
|
|
||||||
|
text = """
|
||||||
|
If you want a picture of the future, imagine a boot stamping on an
|
||||||
|
encrypted, redundant, distributed filesystem -- forever.
|
||||||
|
"""
|
||||||
|
tempfile = write_tempfile(text)
|
||||||
|
filehash, _ = file_crunching(tempfile)
|
||||||
|
|
||||||
|
assert filehash == 'bc004fe72e6530a545570b4c6ce76bcb78ea526b'
|
||||||
|
|
||||||
|
def test_filehash_returns_correct_size():
|
||||||
|
"file_crunching() returns correct size"
|
||||||
|
|
||||||
|
text = "AUSCANNZUKUS has always been at war with Eastasia."
|
||||||
|
tempfile = write_tempfile(text)
|
||||||
|
_, filesize = file_crunching(tempfile)
|
||||||
|
|
||||||
|
assert filesize == 50
|
||||||
|
Loading…
x
Reference in New Issue
Block a user