2018-10-11 20:28:47 -04:00
|
|
|
import os
|
|
|
|
import requests
|
2018-10-26 18:08:55 -04:00
|
|
|
from datetime import datetime, timedelta
|
2018-10-16 20:57:21 -04:00
|
|
|
from PyQt5 import QtCore, QtTest
|
2018-10-11 20:28:47 -04:00
|
|
|
from .GuiBaseTest import GuiBaseTest
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
|
2018-10-11 20:28:47 -04:00
|
|
|
class GuiReceiveTest(GuiBaseTest):
|
2019-10-13 00:01:25 -04:00
|
|
|
def upload_file(
|
|
|
|
self,
|
|
|
|
public_mode,
|
|
|
|
file_to_upload,
|
|
|
|
expected_basename,
|
|
|
|
identical_files_at_once=False,
|
|
|
|
):
|
|
|
|
"""Test that we can upload the file"""
|
2019-05-24 20:59:04 -04:00
|
|
|
|
|
|
|
# Wait 2 seconds to make sure the filename, based on timestamp, isn't accidentally reused
|
|
|
|
QtTest.QTest.qWait(2000)
|
|
|
|
|
2019-10-13 00:01:25 -04:00
|
|
|
files = {"file[]": open(file_to_upload, "rb")}
|
2019-10-20 13:46:14 -04:00
|
|
|
url = f"http://127.0.0.1:{self.gui.app.port}/upload"
|
2019-05-24 20:59:04 -04:00
|
|
|
if public_mode:
|
2019-05-24 16:38:41 -04:00
|
|
|
r = requests.post(url, files=files)
|
2019-05-24 21:07:57 -04:00
|
|
|
if identical_files_at_once:
|
|
|
|
# Send a duplicate upload to test for collisions
|
|
|
|
r = requests.post(url, files=files)
|
2018-10-11 20:28:47 -04:00
|
|
|
else:
|
2019-10-13 00:01:25 -04:00
|
|
|
r = requests.post(
|
|
|
|
url,
|
|
|
|
files=files,
|
|
|
|
auth=requests.auth.HTTPBasicAuth(
|
|
|
|
"onionshare", self.gui.receive_mode.web.password
|
|
|
|
),
|
|
|
|
)
|
2019-05-24 21:07:57 -04:00
|
|
|
if identical_files_at_once:
|
|
|
|
# Send a duplicate upload to test for collisions
|
2019-10-13 00:01:25 -04:00
|
|
|
r = requests.post(
|
|
|
|
url,
|
|
|
|
files=files,
|
|
|
|
auth=requests.auth.HTTPBasicAuth(
|
|
|
|
"onionshare", self.gui.receive_mode.web.password
|
|
|
|
),
|
|
|
|
)
|
2019-05-24 20:59:04 -04:00
|
|
|
|
2018-10-11 20:28:47 -04:00
|
|
|
QtTest.QTest.qWait(2000)
|
|
|
|
|
2019-05-24 20:59:04 -04:00
|
|
|
# Make sure the file is within the last 10 seconds worth of fileames
|
2018-10-26 18:08:55 -04:00
|
|
|
exists = False
|
|
|
|
now = datetime.now()
|
|
|
|
for i in range(10):
|
|
|
|
date_dir = now.strftime("%Y-%m-%d")
|
2019-03-07 20:54:10 -05:00
|
|
|
if identical_files_at_once:
|
|
|
|
time_dir = now.strftime("%H.%M.%S-1")
|
|
|
|
else:
|
|
|
|
time_dir = now.strftime("%H.%M.%S")
|
2019-10-13 00:01:25 -04:00
|
|
|
receive_mode_dir = os.path.join(
|
|
|
|
self.gui.common.settings.get("data_dir"), date_dir, time_dir
|
|
|
|
)
|
2018-10-26 18:08:55 -04:00
|
|
|
expected_filename = os.path.join(receive_mode_dir, expected_basename)
|
|
|
|
if os.path.exists(expected_filename):
|
|
|
|
exists = True
|
|
|
|
break
|
|
|
|
now = now - timedelta(seconds=1)
|
|
|
|
|
|
|
|
self.assertTrue(exists)
|
|
|
|
|
|
|
|
def upload_file_should_fail(self, public_mode):
|
2019-10-13 00:01:25 -04:00
|
|
|
"""Test that we can't upload the file when permissions are wrong, and expected content is shown"""
|
|
|
|
files = {"file[]": open("/tmp/test.txt", "rb")}
|
2019-10-20 13:46:14 -04:00
|
|
|
url = f"http://127.0.0.1:{self.gui.app.port}/upload"
|
2019-05-24 20:59:04 -04:00
|
|
|
if public_mode:
|
2019-05-24 16:38:41 -04:00
|
|
|
r = requests.post(url, files=files)
|
2018-10-15 22:01:44 -04:00
|
|
|
else:
|
2019-10-13 00:01:25 -04:00
|
|
|
r = requests.post(
|
|
|
|
url,
|
|
|
|
files=files,
|
|
|
|
auth=requests.auth.HTTPBasicAuth(
|
|
|
|
"onionshare", self.gui.receive_mode.web.password
|
|
|
|
),
|
|
|
|
)
|
2018-10-15 22:01:44 -04:00
|
|
|
|
2018-10-16 20:57:21 -04:00
|
|
|
QtCore.QTimer.singleShot(1000, self.accept_dialog)
|
2019-10-13 00:01:25 -04:00
|
|
|
self.assertTrue("Error uploading, please inform the OnionShare user" in r.text)
|
2018-10-15 22:01:44 -04:00
|
|
|
|
|
|
|
def upload_dir_permissions(self, mode=0o755):
|
2019-10-13 00:01:25 -04:00
|
|
|
"""Manipulate the permissions on the upload dir in between tests"""
|
|
|
|
os.chmod("/tmp/OnionShare", mode)
|
2018-10-15 22:01:44 -04:00
|
|
|
|
2019-05-24 20:59:04 -04:00
|
|
|
def try_without_auth_in_non_public_mode(self):
|
2019-10-20 13:46:14 -04:00
|
|
|
r = requests.post(f"http://127.0.0.1:{self.gui.app.port}/upload")
|
2019-05-24 20:59:04 -04:00
|
|
|
self.assertEqual(r.status_code, 401)
|
2019-10-20 13:46:14 -04:00
|
|
|
r = requests.get(f"http://127.0.0.1:{self.gui.app.port}/close")
|
2019-05-24 20:59:04 -04:00
|
|
|
self.assertEqual(r.status_code, 401)
|
2018-10-17 00:21:04 -04:00
|
|
|
|
2018-10-15 22:01:44 -04:00
|
|
|
# 'Grouped' tests follow from here
|
|
|
|
|
|
|
|
def run_all_receive_mode_setup_tests(self, public_mode):
|
2019-10-13 00:01:25 -04:00
|
|
|
"""Set up a share in Receive mode and start it"""
|
2018-10-11 20:28:47 -04:00
|
|
|
self.click_mode(self.gui.receive_mode)
|
|
|
|
self.history_is_not_visible(self.gui.receive_mode)
|
|
|
|
self.click_toggle_history(self.gui.receive_mode)
|
|
|
|
self.history_is_visible(self.gui.receive_mode)
|
|
|
|
self.server_working_on_start_button_pressed(self.gui.receive_mode)
|
|
|
|
self.server_status_indicator_says_starting(self.gui.receive_mode)
|
|
|
|
self.settings_button_is_hidden()
|
2018-10-15 22:01:44 -04:00
|
|
|
self.server_is_started(self.gui.receive_mode)
|
|
|
|
self.web_server_is_running()
|
2019-05-22 23:55:31 -04:00
|
|
|
self.have_a_password(self.gui.receive_mode, public_mode)
|
2018-10-11 20:28:47 -04:00
|
|
|
self.url_description_shown(self.gui.receive_mode)
|
2018-10-15 22:01:44 -04:00
|
|
|
self.have_copy_url_button(self.gui.receive_mode, public_mode)
|
2018-10-11 20:28:47 -04:00
|
|
|
self.server_status_indicator_says_started(self.gui.receive_mode)
|
2019-10-13 00:01:25 -04:00
|
|
|
self.web_page(
|
|
|
|
self.gui.receive_mode,
|
|
|
|
"Select the files you want to send, then click",
|
|
|
|
public_mode,
|
|
|
|
)
|
2018-10-15 22:01:44 -04:00
|
|
|
|
2019-05-24 20:59:04 -04:00
|
|
|
def run_all_receive_mode_tests(self, public_mode):
|
2019-10-13 00:01:25 -04:00
|
|
|
"""Upload files in receive mode and stop the share"""
|
2018-10-15 22:01:44 -04:00
|
|
|
self.run_all_receive_mode_setup_tests(public_mode)
|
2018-10-17 00:21:04 -04:00
|
|
|
if not public_mode:
|
2019-05-24 20:59:04 -04:00
|
|
|
self.try_without_auth_in_non_public_mode()
|
2019-10-13 00:01:25 -04:00
|
|
|
self.upload_file(public_mode, "/tmp/test.txt", "test.txt")
|
2018-10-11 20:28:47 -04:00
|
|
|
self.history_widgets_present(self.gui.receive_mode)
|
|
|
|
self.counter_incremented(self.gui.receive_mode, 1)
|
2019-10-13 00:01:25 -04:00
|
|
|
self.upload_file(public_mode, "/tmp/test.txt", "test.txt")
|
2018-10-11 20:28:47 -04:00
|
|
|
self.counter_incremented(self.gui.receive_mode, 2)
|
2019-10-13 00:01:25 -04:00
|
|
|
self.upload_file(public_mode, "/tmp/testdir/test", "test")
|
2018-10-17 00:21:04 -04:00
|
|
|
self.counter_incremented(self.gui.receive_mode, 3)
|
2019-10-13 00:01:25 -04:00
|
|
|
self.upload_file(public_mode, "/tmp/testdir/test", "test")
|
2018-10-17 00:21:04 -04:00
|
|
|
self.counter_incremented(self.gui.receive_mode, 4)
|
2019-03-07 20:54:10 -05:00
|
|
|
# Test uploading the same file twice at the same time, and make sure no collisions
|
2019-10-13 00:01:25 -04:00
|
|
|
self.upload_file(public_mode, "/tmp/test.txt", "test.txt", True)
|
2019-03-07 20:54:10 -05:00
|
|
|
self.counter_incremented(self.gui.receive_mode, 6)
|
2019-09-08 22:19:39 -04:00
|
|
|
self.history_indicator(self.gui.receive_mode, public_mode, "2")
|
2018-10-11 20:28:47 -04:00
|
|
|
self.server_is_stopped(self.gui.receive_mode, False)
|
2018-10-15 22:01:44 -04:00
|
|
|
self.web_server_is_stopped()
|
2018-10-11 20:28:47 -04:00
|
|
|
self.server_status_indicator_says_closed(self.gui.receive_mode, False)
|
|
|
|
self.server_working_on_start_button_pressed(self.gui.receive_mode)
|
2018-10-15 22:01:44 -04:00
|
|
|
self.server_is_started(self.gui.receive_mode)
|
2019-09-08 22:19:39 -04:00
|
|
|
self.history_indicator(self.gui.receive_mode, public_mode, "2")
|
2018-10-11 20:28:47 -04:00
|
|
|
|
2019-05-24 20:59:04 -04:00
|
|
|
def run_all_receive_mode_unwritable_dir_tests(self, public_mode):
|
2019-10-13 00:01:25 -04:00
|
|
|
"""Attempt to upload (unwritable) files in receive mode and stop the share"""
|
2018-10-15 22:01:44 -04:00
|
|
|
self.run_all_receive_mode_setup_tests(public_mode)
|
|
|
|
self.upload_dir_permissions(0o400)
|
2018-10-26 18:08:55 -04:00
|
|
|
self.upload_file_should_fail(public_mode)
|
2018-10-15 22:01:44 -04:00
|
|
|
self.server_is_stopped(self.gui.receive_mode, True)
|
|
|
|
self.web_server_is_stopped()
|
|
|
|
self.server_status_indicator_says_closed(self.gui.receive_mode, False)
|
|
|
|
self.upload_dir_permissions(0o755)
|
|
|
|
|
|
|
|
def run_all_receive_mode_timer_tests(self, public_mode):
|
|
|
|
"""Auto-stop timer tests in receive mode"""
|
|
|
|
self.run_all_receive_mode_setup_tests(public_mode)
|
|
|
|
self.set_timeout(self.gui.receive_mode, 5)
|
2019-03-25 00:05:54 -04:00
|
|
|
self.autostop_timer_widget_hidden(self.gui.receive_mode)
|
2018-10-15 22:01:44 -04:00
|
|
|
self.server_timed_out(self.gui.receive_mode, 15000)
|
|
|
|
self.web_server_is_stopped()
|
2019-09-09 02:35:05 -04:00
|
|
|
|
|
|
|
def run_all_clear_all_button_tests(self, public_mode):
|
|
|
|
"""Test the Clear All history button"""
|
|
|
|
self.run_all_receive_mode_setup_tests(public_mode)
|
2019-10-13 00:01:25 -04:00
|
|
|
self.upload_file(public_mode, "/tmp/test.txt", "test.txt")
|
2019-09-09 02:35:05 -04:00
|
|
|
self.history_widgets_present(self.gui.receive_mode)
|
|
|
|
self.clear_all_history_items(self.gui.receive_mode, 0)
|
2019-10-13 00:01:25 -04:00
|
|
|
self.upload_file(public_mode, "/tmp/test.txt", "test.txt")
|
2019-09-09 02:35:05 -04:00
|
|
|
self.clear_all_history_items(self.gui.receive_mode, 2)
|