2018-10-15 22:01:44 -04:00
|
|
|
import os
|
|
|
|
import requests
|
2018-10-11 20:28:47 -04:00
|
|
|
import socks
|
|
|
|
import zipfile
|
2019-05-24 20:59:04 -04:00
|
|
|
import tempfile
|
2018-10-11 20:28:47 -04:00
|
|
|
from PyQt5 import QtCore, QtTest
|
|
|
|
from .GuiBaseTest import GuiBaseTest
|
|
|
|
|
|
|
|
class GuiShareTest(GuiBaseTest):
|
2018-10-11 21:18:34 -04:00
|
|
|
# Persistence tests
|
2019-05-22 23:55:31 -04:00
|
|
|
def have_same_password(self, password):
|
|
|
|
'''Test that we have the same password'''
|
|
|
|
self.assertEqual(self.gui.share_mode.server_status.web.password, password)
|
2018-10-11 21:18:34 -04:00
|
|
|
|
2018-10-11 20:28:47 -04:00
|
|
|
# Share-specific tests
|
2018-10-16 20:57:21 -04:00
|
|
|
|
|
|
|
def file_selection_widget_has_files(self, num=2):
|
|
|
|
'''Test that the number of items in the list is as expected'''
|
|
|
|
self.assertEqual(self.gui.share_mode.server_status.file_selection.get_num_files(), num)
|
2018-10-11 20:28:47 -04:00
|
|
|
|
2019-05-22 23:55:31 -04:00
|
|
|
|
2018-10-15 22:01:44 -04:00
|
|
|
def deleting_all_files_hides_delete_button(self):
|
2018-10-11 20:28:47 -04:00
|
|
|
'''Test that clicking on the file item shows the delete button. Test that deleting the only item in the list hides the delete button'''
|
|
|
|
rect = self.gui.share_mode.server_status.file_selection.file_list.visualItemRect(self.gui.share_mode.server_status.file_selection.file_list.item(0))
|
|
|
|
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.file_list.viewport(), QtCore.Qt.LeftButton, pos=rect.center())
|
|
|
|
# Delete button should be visible
|
|
|
|
self.assertTrue(self.gui.share_mode.server_status.file_selection.delete_button.isVisible())
|
2018-10-15 22:01:44 -04:00
|
|
|
# Click delete, delete button should still be visible since we have one more file
|
2018-10-11 20:28:47 -04:00
|
|
|
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.delete_button, QtCore.Qt.LeftButton)
|
2018-10-15 22:01:44 -04:00
|
|
|
|
|
|
|
rect = self.gui.share_mode.server_status.file_selection.file_list.visualItemRect(self.gui.share_mode.server_status.file_selection.file_list.item(0))
|
|
|
|
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.file_list.viewport(), QtCore.Qt.LeftButton, pos=rect.center())
|
|
|
|
self.assertTrue(self.gui.share_mode.server_status.file_selection.delete_button.isVisible())
|
|
|
|
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.delete_button, QtCore.Qt.LeftButton)
|
|
|
|
|
|
|
|
# No more files, the delete button should be hidden
|
2018-10-11 20:28:47 -04:00
|
|
|
self.assertFalse(self.gui.share_mode.server_status.file_selection.delete_button.isVisible())
|
|
|
|
|
2019-05-22 23:55:31 -04:00
|
|
|
|
2018-10-11 20:28:47 -04:00
|
|
|
def add_a_file_and_delete_using_its_delete_widget(self):
|
|
|
|
'''Test that we can also delete a file by clicking on its [X] widget'''
|
|
|
|
self.gui.share_mode.server_status.file_selection.file_list.add_file('/etc/hosts')
|
|
|
|
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.file_list.item(0).item_button, QtCore.Qt.LeftButton)
|
2018-10-16 20:57:21 -04:00
|
|
|
self.file_selection_widget_has_files(0)
|
2018-10-11 20:28:47 -04:00
|
|
|
|
2019-05-22 23:55:31 -04:00
|
|
|
|
2019-09-02 00:22:59 -04:00
|
|
|
def file_selection_widget_read_files(self):
|
2018-10-11 20:28:47 -04:00
|
|
|
'''Re-add some files to the list so we can share'''
|
|
|
|
self.gui.share_mode.server_status.file_selection.file_list.add_file('/etc/hosts')
|
|
|
|
self.gui.share_mode.server_status.file_selection.file_list.add_file('/tmp/test.txt')
|
2018-10-16 20:57:21 -04:00
|
|
|
self.file_selection_widget_has_files(2)
|
2018-10-11 20:28:47 -04:00
|
|
|
|
2018-10-15 22:01:44 -04:00
|
|
|
|
|
|
|
def add_large_file(self):
|
|
|
|
'''Add a large file to the share'''
|
|
|
|
size = 1024*1024*155
|
|
|
|
with open('/tmp/large_file', 'wb') as fout:
|
|
|
|
fout.write(os.urandom(size))
|
|
|
|
self.gui.share_mode.server_status.file_selection.file_list.add_file('/tmp/large_file')
|
2019-05-22 23:55:31 -04:00
|
|
|
|
2018-10-15 22:01:44 -04:00
|
|
|
|
2018-10-11 20:28:47 -04:00
|
|
|
def add_delete_buttons_hidden(self):
|
|
|
|
'''Test that the add and delete buttons are hidden when the server starts'''
|
|
|
|
self.assertFalse(self.gui.share_mode.server_status.file_selection.add_button.isVisible())
|
|
|
|
self.assertFalse(self.gui.share_mode.server_status.file_selection.delete_button.isVisible())
|
|
|
|
|
2019-05-22 23:55:31 -04:00
|
|
|
|
2018-10-11 20:28:47 -04:00
|
|
|
def download_share(self, public_mode):
|
|
|
|
'''Test that we can download the share'''
|
2019-05-24 20:59:04 -04:00
|
|
|
url = "http://127.0.0.1:{}/download".format(self.gui.app.port)
|
2018-10-11 20:28:47 -04:00
|
|
|
if public_mode:
|
2019-05-24 20:59:04 -04:00
|
|
|
r = requests.get(url)
|
2018-10-11 20:28:47 -04:00
|
|
|
else:
|
2019-05-24 20:59:04 -04:00
|
|
|
r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.share_mode.server_status.web.password))
|
|
|
|
|
|
|
|
tmp_file = tempfile.NamedTemporaryFile()
|
|
|
|
with open(tmp_file.name, 'wb') as f:
|
|
|
|
f.write(r.content)
|
|
|
|
|
|
|
|
zip = zipfile.ZipFile(tmp_file.name)
|
2018-10-11 20:28:47 -04:00
|
|
|
QtTest.QTest.qWait(2000)
|
|
|
|
self.assertEqual('onionshare', zip.read('test.txt').decode('utf-8'))
|
|
|
|
|
2019-09-02 21:19:42 -04:00
|
|
|
def individual_file_is_viewable_or_not(self, public_mode, stay_open):
|
|
|
|
'''Test whether an individual file is viewable (when in stay_open mode) and that it isn't (when not in stay_open mode)'''
|
|
|
|
url = "http://127.0.0.1:{}".format(self.gui.app.port)
|
|
|
|
download_file_url = "http://127.0.0.1:{}/test.txt".format(self.gui.app.port)
|
|
|
|
if public_mode:
|
|
|
|
r = requests.get(url)
|
|
|
|
else:
|
|
|
|
r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.share_mode.server_status.web.password))
|
|
|
|
|
|
|
|
if stay_open:
|
|
|
|
self.assertTrue('a href="test.txt"' in r.text)
|
|
|
|
|
|
|
|
if public_mode:
|
|
|
|
r = requests.get(download_file_url)
|
|
|
|
else:
|
|
|
|
r = requests.get(download_file_url, auth=requests.auth.HTTPBasicAuth('onionshare', self.gui.share_mode.server_status.web.password))
|
|
|
|
|
|
|
|
tmp_file = tempfile.NamedTemporaryFile()
|
|
|
|
with open(tmp_file.name, 'wb') as f:
|
|
|
|
f.write(r.content)
|
|
|
|
|
|
|
|
with open(tmp_file.name, 'r') as f:
|
|
|
|
self.assertEqual('onionshare', f.read())
|
|
|
|
else:
|
|
|
|
self.assertFalse('a href="/test.txt"' in r.text)
|
|
|
|
self.download_share(public_mode)
|
|
|
|
|
|
|
|
QtTest.QTest.qWait(2000)
|
|
|
|
|
2019-05-24 16:38:41 -04:00
|
|
|
def hit_401(self, public_mode):
|
|
|
|
'''Test that the server stops after too many 401s, or doesn't when in public_mode'''
|
|
|
|
url = "http://127.0.0.1:{}/".format(self.gui.app.port)
|
2018-10-15 22:01:44 -04:00
|
|
|
|
|
|
|
for _ in range(20):
|
2019-05-24 16:38:41 -04:00
|
|
|
password_guess = self.gui.common.build_password()
|
2019-05-24 20:59:04 -04:00
|
|
|
r = requests.get(url, auth=requests.auth.HTTPBasicAuth('onionshare', password_guess))
|
2018-10-15 22:01:44 -04:00
|
|
|
|
|
|
|
# A nasty hack to avoid the Alert dialog that blocks the rest of the test
|
|
|
|
if not public_mode:
|
2018-10-16 20:57:21 -04:00
|
|
|
QtCore.QTimer.singleShot(1000, self.accept_dialog)
|
2018-10-15 22:01:44 -04:00
|
|
|
|
|
|
|
# In public mode, we should still be running (no rate-limiting)
|
|
|
|
if public_mode:
|
|
|
|
self.web_server_is_running()
|
|
|
|
# In non-public mode, we should be shut down (rate-limiting)
|
|
|
|
else:
|
|
|
|
self.web_server_is_stopped()
|
|
|
|
|
|
|
|
|
2018-10-11 20:28:47 -04:00
|
|
|
def add_button_visible(self):
|
|
|
|
'''Test that the add button should be visible'''
|
|
|
|
self.assertTrue(self.gui.share_mode.server_status.file_selection.add_button.isVisible())
|
|
|
|
|
|
|
|
|
2018-10-15 22:01:44 -04:00
|
|
|
# 'Grouped' tests follow from here
|
|
|
|
|
2018-10-11 20:28:47 -04:00
|
|
|
def run_all_share_mode_setup_tests(self):
|
|
|
|
"""Tests in share mode prior to starting a share"""
|
|
|
|
self.click_mode(self.gui.share_mode)
|
2018-10-15 22:01:44 -04:00
|
|
|
self.file_selection_widget_has_files()
|
2018-10-11 20:28:47 -04:00
|
|
|
self.history_is_not_visible(self.gui.share_mode)
|
|
|
|
self.click_toggle_history(self.gui.share_mode)
|
|
|
|
self.history_is_visible(self.gui.share_mode)
|
2018-10-15 22:01:44 -04:00
|
|
|
self.deleting_all_files_hides_delete_button()
|
2018-10-11 20:28:47 -04:00
|
|
|
self.add_a_file_and_delete_using_its_delete_widget()
|
2019-09-02 00:22:59 -04:00
|
|
|
self.file_selection_widget_read_files()
|
2018-10-11 20:28:47 -04:00
|
|
|
|
2019-05-22 23:55:31 -04:00
|
|
|
|
2018-10-15 22:01:44 -04:00
|
|
|
def run_all_share_mode_started_tests(self, public_mode, startup_time=2000):
|
2018-10-11 20:28:47 -04:00
|
|
|
"""Tests in share mode after starting a share"""
|
|
|
|
self.server_working_on_start_button_pressed(self.gui.share_mode)
|
|
|
|
self.server_status_indicator_says_starting(self.gui.share_mode)
|
|
|
|
self.add_delete_buttons_hidden()
|
|
|
|
self.settings_button_is_hidden()
|
2018-10-15 22:01:44 -04:00
|
|
|
self.server_is_started(self.gui.share_mode, startup_time)
|
|
|
|
self.web_server_is_running()
|
2019-05-22 23:55:31 -04:00
|
|
|
self.have_a_password(self.gui.share_mode, public_mode)
|
2018-10-11 20:28:47 -04:00
|
|
|
self.url_description_shown(self.gui.share_mode)
|
2018-10-15 22:01:44 -04:00
|
|
|
self.have_copy_url_button(self.gui.share_mode, public_mode)
|
2018-10-11 20:28:47 -04:00
|
|
|
self.server_status_indicator_says_started(self.gui.share_mode)
|
|
|
|
|
2019-05-22 23:55:31 -04:00
|
|
|
|
2018-10-11 20:28:47 -04:00
|
|
|
def run_all_share_mode_download_tests(self, public_mode, stay_open):
|
|
|
|
"""Tests in share mode after downloading a share"""
|
2018-10-12 03:53:03 -04:00
|
|
|
self.web_page(self.gui.share_mode, 'Total size', public_mode)
|
2018-10-11 20:28:47 -04:00
|
|
|
self.download_share(public_mode)
|
|
|
|
self.history_widgets_present(self.gui.share_mode)
|
|
|
|
self.server_is_stopped(self.gui.share_mode, stay_open)
|
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.share_mode, stay_open)
|
|
|
|
self.add_button_visible()
|
|
|
|
self.server_working_on_start_button_pressed(self.gui.share_mode)
|
2018-10-15 22:01:44 -04:00
|
|
|
self.server_is_started(self.gui.share_mode)
|
2018-10-11 20:28:47 -04:00
|
|
|
self.history_indicator(self.gui.share_mode, public_mode)
|
|
|
|
|
2019-09-02 21:19:42 -04:00
|
|
|
def run_all_share_mode_individual_file_download_tests(self, public_mode, stay_open):
|
|
|
|
"""Tests in share mode after downloading a share"""
|
|
|
|
self.web_page(self.gui.share_mode, 'Total size', public_mode)
|
|
|
|
self.individual_file_is_viewable_or_not(public_mode, stay_open)
|
|
|
|
self.history_widgets_present(self.gui.share_mode)
|
|
|
|
self.server_is_stopped(self.gui.share_mode, stay_open)
|
|
|
|
self.web_server_is_stopped()
|
|
|
|
self.server_status_indicator_says_closed(self.gui.share_mode, stay_open)
|
|
|
|
self.add_button_visible()
|
|
|
|
self.server_working_on_start_button_pressed(self.gui.share_mode)
|
|
|
|
self.server_is_started(self.gui.share_mode)
|
|
|
|
self.history_indicator(self.gui.share_mode, public_mode)
|
2019-05-22 23:55:31 -04:00
|
|
|
|
2018-10-11 20:28:47 -04:00
|
|
|
def run_all_share_mode_tests(self, public_mode, stay_open):
|
|
|
|
"""End-to-end share tests"""
|
|
|
|
self.run_all_share_mode_setup_tests()
|
|
|
|
self.run_all_share_mode_started_tests(public_mode)
|
|
|
|
self.run_all_share_mode_download_tests(public_mode, stay_open)
|
|
|
|
|
|
|
|
|
2019-09-02 21:19:42 -04:00
|
|
|
def run_all_share_mode_individual_file_tests(self, public_mode, stay_open):
|
|
|
|
"""Tests in share mode when viewing an individual file"""
|
|
|
|
self.run_all_share_mode_setup_tests()
|
|
|
|
self.run_all_share_mode_started_tests(public_mode)
|
|
|
|
self.run_all_share_mode_individual_file_download_tests(public_mode, stay_open)
|
|
|
|
|
2018-10-15 22:01:44 -04:00
|
|
|
def run_all_large_file_tests(self, public_mode, stay_open):
|
|
|
|
"""Same as above but with a larger file"""
|
|
|
|
self.run_all_share_mode_setup_tests()
|
|
|
|
self.add_large_file()
|
|
|
|
self.run_all_share_mode_started_tests(public_mode, startup_time=15000)
|
|
|
|
self.assertTrue(self.gui.share_mode.filesize_warning.isVisible())
|
|
|
|
self.server_is_stopped(self.gui.share_mode, stay_open)
|
|
|
|
self.web_server_is_stopped()
|
|
|
|
self.server_status_indicator_says_closed(self.gui.share_mode, stay_open)
|
|
|
|
|
|
|
|
|
2018-10-11 21:18:34 -04:00
|
|
|
def run_all_share_mode_persistent_tests(self, public_mode, stay_open):
|
2019-05-22 23:55:31 -04:00
|
|
|
"""Same as end-to-end share tests but also test the password is the same on multiple shared"""
|
2018-10-11 21:18:34 -04:00
|
|
|
self.run_all_share_mode_setup_tests()
|
|
|
|
self.run_all_share_mode_started_tests(public_mode)
|
2019-05-22 23:55:31 -04:00
|
|
|
password = self.gui.share_mode.server_status.web.password
|
2018-10-11 21:18:34 -04:00
|
|
|
self.run_all_share_mode_download_tests(public_mode, stay_open)
|
2019-05-22 23:55:31 -04:00
|
|
|
self.have_same_password(password)
|
2018-10-11 21:18:34 -04:00
|
|
|
|
|
|
|
|
2018-10-11 20:28:47 -04:00
|
|
|
def run_all_share_mode_timer_tests(self, public_mode):
|
|
|
|
"""Auto-stop timer tests in share mode"""
|
|
|
|
self.run_all_share_mode_setup_tests()
|
|
|
|
self.set_timeout(self.gui.share_mode, 5)
|
|
|
|
self.run_all_share_mode_started_tests(public_mode)
|
2019-03-25 00:05:54 -04:00
|
|
|
self.autostop_timer_widget_hidden(self.gui.share_mode)
|
2018-10-11 20:28:47 -04:00
|
|
|
self.server_timed_out(self.gui.share_mode, 10000)
|
2018-10-15 22:01:44 -04:00
|
|
|
self.web_server_is_stopped()
|
2018-10-11 20:28:47 -04:00
|
|
|
|
2019-03-25 00:28:31 -04:00
|
|
|
def run_all_share_mode_autostart_timer_tests(self, public_mode):
|
|
|
|
"""Auto-start timer tests in share mode"""
|
2019-03-04 18:28:27 -05:00
|
|
|
self.run_all_share_mode_setup_tests()
|
2019-03-25 00:28:31 -04:00
|
|
|
self.set_autostart_timer(self.gui.share_mode, 5)
|
2019-03-04 18:28:27 -05:00
|
|
|
self.server_working_on_start_button_pressed(self.gui.share_mode)
|
2019-03-25 00:28:31 -04:00
|
|
|
self.autostart_timer_widget_hidden(self.gui.share_mode)
|
2019-03-04 18:28:27 -05:00
|
|
|
self.server_status_indicator_says_scheduled(self.gui.share_mode)
|
2019-03-05 00:59:13 -05:00
|
|
|
self.web_server_is_stopped()
|
2019-03-04 18:28:27 -05:00
|
|
|
self.scheduled_service_started(self.gui.share_mode, 7000)
|
|
|
|
self.web_server_is_running()
|
2018-10-16 20:57:21 -04:00
|
|
|
|
2019-03-25 00:05:54 -04:00
|
|
|
def run_all_share_mode_autostop_autostart_mismatch_tests(self, public_mode):
|
2019-03-11 00:55:17 -04:00
|
|
|
"""Auto-stop timer tests in share mode"""
|
|
|
|
self.run_all_share_mode_setup_tests()
|
2019-03-25 00:28:31 -04:00
|
|
|
self.set_autostart_timer(self.gui.share_mode, 15)
|
2019-03-11 00:55:17 -04:00
|
|
|
self.set_timeout(self.gui.share_mode, 5)
|
|
|
|
QtCore.QTimer.singleShot(4000, self.accept_dialog)
|
|
|
|
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.server_button, QtCore.Qt.LeftButton)
|
|
|
|
self.server_is_stopped(self.gui.share_mode, False)
|
|
|
|
|
2018-10-16 20:57:21 -04:00
|
|
|
def run_all_share_mode_unreadable_file_tests(self):
|
|
|
|
'''Attempt to share an unreadable file'''
|
|
|
|
self.run_all_share_mode_setup_tests()
|
|
|
|
QtCore.QTimer.singleShot(1000, self.accept_dialog)
|
|
|
|
self.gui.share_mode.server_status.file_selection.file_list.add_file('/tmp/nonexistent.txt')
|
|
|
|
self.file_selection_widget_has_files(2)
|