More coverage such as 404 ratelimit, large file tests. Standardise some method naming conventions and other fixes/cleanup

This commit is contained in:
Miguel Jacq 2018-10-16 13:01:44 +11:00
parent ed224f0388
commit cc49589080
No known key found for this signature in database
GPG key ID: EEA4341C6D97A0B6
20 changed files with 360 additions and 87 deletions

View file

@ -26,6 +26,13 @@ class GuiBaseTest(object):
testfile.write('onionshare') testfile.write('onionshare')
testfile.close() testfile.close()
# Create a test dir and files
if not os.path.exists('/tmp/testdir'):
testdir = os.mkdir('/tmp/testdir')
testfile = open('/tmp/testdir/test.txt', 'w')
testfile.write('onionshare')
testfile.close()
common = Common() common = Common()
common.settings = Settings(common) common.settings = Settings(common)
common.define_css() common.define_css()
@ -46,14 +53,17 @@ class GuiBaseTest(object):
web = Web(common, False, True) web = Web(common, False, True)
open('/tmp/{}.json'.format(settings_filename), 'w').write(json.dumps(test_settings)) open('/tmp/{}.json'.format(settings_filename), 'w').write(json.dumps(test_settings))
gui = OnionShareGui(common, testonion, qtapp, app, ['/tmp/test.txt'], '/tmp/{}.json'.format(settings_filename), True) gui = OnionShareGui(common, testonion, qtapp, app, ['/tmp/test.txt', '/tmp/testdir'], '/tmp/{}.json'.format(settings_filename), True)
return gui return gui
@staticmethod @staticmethod
def tear_down(): def tear_down():
'''Clean up after tests'''
try: try:
os.remove('/tmp/test.txt') os.remove('/tmp/test.txt')
os.remove('/tmp/largefile')
shutil.rmtree('/tmp/OnionShare') shutil.rmtree('/tmp/OnionShare')
shutil.rmtree('/tmp/testdir')
except: except:
pass pass
@ -73,6 +83,11 @@ class GuiBaseTest(object):
self.assertTrue(self.gui.settings_button.isVisible()) self.assertTrue(self.gui.settings_button.isVisible())
def settings_button_is_hidden(self):
'''Test that the settings button is hidden when the server starts'''
self.assertFalse(self.gui.settings_button.isVisible())
def server_status_bar_is_visible(self): def server_status_bar_is_visible(self):
'''Test that the status bar is visible''' '''Test that the status bar is visible'''
self.assertTrue(self.gui.status_bar.isVisible()) self.assertTrue(self.gui.status_bar.isVisible())
@ -152,22 +167,17 @@ class GuiBaseTest(object):
def server_status_indicator_says_starting(self, mode): def server_status_indicator_says_starting(self, mode):
'''Test that the Server Status indicator shows we are Starting''' '''Test that the Server Status indicator shows we are Starting'''
self.assertEquals(mode.server_status_label.text(), strings._('gui_status_indicator_share_working')) self.assertEqual(mode.server_status_label.text(), strings._('gui_status_indicator_share_working'))
def settings_button_is_hidden(self): def server_is_started(self, mode, startup_time=2000):
'''Test that the settings button is hidden when the server starts'''
self.assertFalse(self.gui.settings_button.isVisible())
def a_server_is_started(self, mode):
'''Test that the server has started''' '''Test that the server has started'''
QtTest.QTest.qWait(2000) QtTest.QTest.qWait(startup_time)
# Should now be in SERVER_STARTED state # Should now be in SERVER_STARTED state
self.assertEqual(mode.server_status.status, 2) self.assertEqual(mode.server_status.status, 2)
def a_web_server_is_running(self): def web_server_is_running(self):
'''Test that the web server has started''' '''Test that the web server has started'''
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -187,17 +197,24 @@ class GuiBaseTest(object):
self.assertTrue(mode.server_status.url_description.isVisible()) self.assertTrue(mode.server_status.url_description.isVisible())
def have_copy_url_button(self, mode): def have_copy_url_button(self, mode, public_mode):
'''Test that the Copy URL button is shown''' '''Test that the Copy URL button is shown and that the clipboard is correct'''
self.assertTrue(mode.server_status.copy_url_button.isVisible()) self.assertTrue(mode.server_status.copy_url_button.isVisible())
QtTest.QTest.mouseClick(mode.server_status.copy_url_button, QtCore.Qt.LeftButton)
clipboard = self.gui.qtapp.clipboard()
if public_mode:
self.assertEqual(clipboard.text(), 'http://127.0.0.1:{}'.format(self.gui.app.port))
else:
self.assertEqual(clipboard.text(), 'http://127.0.0.1:{}/{}'.format(self.gui.app.port, mode.server_status.web.slug))
def server_status_indicator_says_started(self, mode): def server_status_indicator_says_started(self, mode):
'''Test that the Server Status indicator shows we are started''' '''Test that the Server Status indicator shows we are started'''
if type(mode) == ReceiveMode: if type(mode) == ReceiveMode:
self.assertEquals(mode.server_status_label.text(), strings._('gui_status_indicator_receive_started')) self.assertEqual(mode.server_status_label.text(), strings._('gui_status_indicator_receive_started'))
if type(mode) == ShareMode: if type(mode) == ShareMode:
self.assertEquals(mode.server_status_label.text(), strings._('gui_status_indicator_share_started')) self.assertEqual(mode.server_status_label.text(), strings._('gui_status_indicator_share_started'))
def web_page(self, mode, string, public_mode): def web_page(self, mode, string, public_mode):
@ -237,17 +254,17 @@ class GuiBaseTest(object):
def counter_incremented(self, mode, count): def counter_incremented(self, mode, count):
'''Test that the counter has incremented''' '''Test that the counter has incremented'''
self.assertEquals(mode.history.completed_count, count) self.assertEqual(mode.history.completed_count, count)
def server_is_stopped(self, mode, stay_open): def server_is_stopped(self, mode, stay_open):
'''Test that the server stops when we click Stop''' '''Test that the server stops when we click Stop'''
if type(mode) == ReceiveMode or (type(mode) == ShareMode and stay_open): if type(mode) == ReceiveMode or (type(mode) == ShareMode and stay_open):
QtTest.QTest.mouseClick(mode.server_status.server_button, QtCore.Qt.LeftButton) QtTest.QTest.mouseClick(mode.server_status.server_button, QtCore.Qt.LeftButton)
self.assertEquals(mode.server_status.status, 0) self.assertEqual(mode.server_status.status, 0)
def web_service_is_stopped(self): def web_server_is_stopped(self):
'''Test that the web server also stopped''' '''Test that the web server also stopped'''
QtTest.QTest.qWait(2000) QtTest.QTest.qWait(2000)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -259,12 +276,35 @@ class GuiBaseTest(object):
def server_status_indicator_says_closed(self, mode, stay_open): def server_status_indicator_says_closed(self, mode, stay_open):
'''Test that the Server Status indicator shows we closed''' '''Test that the Server Status indicator shows we closed'''
if type(mode) == ReceiveMode: if type(mode) == ReceiveMode:
self.assertEquals(self.gui.receive_mode.server_status_label.text(), strings._('gui_status_indicator_receive_stopped')) self.assertEqual(self.gui.receive_mode.server_status_label.text(), strings._('gui_status_indicator_receive_stopped'))
if type(mode) == ShareMode: if type(mode) == ShareMode:
if stay_open: if stay_open:
self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('gui_status_indicator_share_stopped')) self.assertEqual(self.gui.share_mode.server_status_label.text(), strings._('gui_status_indicator_share_stopped'))
else: else:
self.assertEquals(self.gui.share_mode.server_status_label.text(), strings._('closing_automatically')) self.assertEqual(self.gui.share_mode.server_status_label.text(), strings._('closing_automatically'))
# Auto-stop timer tests
def set_timeout(self, mode, timeout):
'''Test that the timeout can be set'''
timer = QtCore.QDateTime.currentDateTime().addSecs(timeout)
mode.server_status.shutdown_timeout.setDateTime(timer)
self.assertTrue(mode.server_status.shutdown_timeout.dateTime(), timer)
def timeout_widget_hidden(self, mode):
'''Test that the timeout widget is hidden when share has started'''
self.assertFalse(mode.server_status.shutdown_timeout_container.isVisible())
def server_timed_out(self, mode, wait):
'''Test that the server has timed out after the timer ran out'''
QtTest.QTest.qWait(wait)
# We should have timed out now
self.assertEqual(mode.server_status.status, 0)
# 'Grouped' tests follow from here
def run_all_common_setup_tests(self): def run_all_common_setup_tests(self):
self.gui_loaded() self.gui_loaded()

View file

@ -15,7 +15,39 @@ class GuiReceiveTest(GuiBaseTest):
QtTest.QTest.qWait(2000) QtTest.QTest.qWait(2000)
self.assertTrue(os.path.isfile(expected_file)) self.assertTrue(os.path.isfile(expected_file))
def run_all_receive_mode_tests(self, public_mode, receive_allow_receiver_shutdown): def upload_file_should_fail(self, public_mode, expected_file):
'''Test that we can't upload the file when permissions are wrong, and expected content is shown'''
files = {'file[]': open('/tmp/test.txt', 'rb')}
if not public_mode:
path = 'http://127.0.0.1:{}/{}/upload'.format(self.gui.app.port, self.gui.receive_mode.web.slug)
else:
path = 'http://127.0.0.1:{}/upload'.format(self.gui.app.port)
response = requests.post(path, files=files)
# A nasty hack to avoid the Alert dialog that blocks the rest of the test
self.gui.qtapp.exit()
self.assertTrue('Error uploading, please inform the OnionShare user' in response.text)
def upload_dir_permissions(self, mode=0o755):
'''Manipulate the permissions on the upload dir in between tests'''
os.chmod('/tmp/OnionShare', mode)
def run_receive_mode_sender_closed_tests(self, public_mode):
'''Test that the share can be stopped by the sender in receive mode'''
if not public_mode:
path = 'http://127.0.0.1:{}/{}/close'.format(self.gui.app.port, self.gui.receive_mode.web.slug)
else:
path = 'http://127.0.0.1:{}/close'.format(self.gui.app.port)
response = requests.post(path)
self.server_is_stopped(self.gui.receive_mode, False)
self.web_server_is_stopped()
self.server_status_indicator_says_closed(self.gui.receive_mode, False)
# 'Grouped' tests follow from here
def run_all_receive_mode_setup_tests(self, public_mode):
'''Set up a share in Receive mode and start it'''
self.click_mode(self.gui.receive_mode) self.click_mode(self.gui.receive_mode)
self.history_is_not_visible(self.gui.receive_mode) self.history_is_not_visible(self.gui.receive_mode)
self.click_toggle_history(self.gui.receive_mode) self.click_toggle_history(self.gui.receive_mode)
@ -23,13 +55,17 @@ class GuiReceiveTest(GuiBaseTest):
self.server_working_on_start_button_pressed(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.server_status_indicator_says_starting(self.gui.receive_mode)
self.settings_button_is_hidden() self.settings_button_is_hidden()
self.a_server_is_started(self.gui.receive_mode) self.server_is_started(self.gui.receive_mode)
self.a_web_server_is_running() self.web_server_is_running()
self.have_a_slug(self.gui.receive_mode, public_mode) self.have_a_slug(self.gui.receive_mode, public_mode)
self.url_description_shown(self.gui.receive_mode) self.url_description_shown(self.gui.receive_mode)
self.have_copy_url_button(self.gui.receive_mode) self.have_copy_url_button(self.gui.receive_mode, public_mode)
self.server_status_indicator_says_started(self.gui.receive_mode) self.server_status_indicator_says_started(self.gui.receive_mode)
self.web_page(self.gui.receive_mode, 'Select the files you want to send, then click', public_mode) self.web_page(self.gui.receive_mode, 'Select the files you want to send, then click', public_mode)
def run_all_receive_mode_tests(self, public_mode, receive_allow_receiver_shutdown):
'''Upload files in receive mode and stop the share'''
self.run_all_receive_mode_setup_tests(public_mode)
self.upload_file(public_mode, '/tmp/OnionShare/test.txt') self.upload_file(public_mode, '/tmp/OnionShare/test.txt')
self.history_widgets_present(self.gui.receive_mode) self.history_widgets_present(self.gui.receive_mode)
self.counter_incremented(self.gui.receive_mode, 1) self.counter_incremented(self.gui.receive_mode, 1)
@ -37,9 +73,26 @@ class GuiReceiveTest(GuiBaseTest):
self.counter_incremented(self.gui.receive_mode, 2) self.counter_incremented(self.gui.receive_mode, 2)
self.history_indicator(self.gui.receive_mode, public_mode) self.history_indicator(self.gui.receive_mode, public_mode)
self.server_is_stopped(self.gui.receive_mode, False) self.server_is_stopped(self.gui.receive_mode, False)
self.web_service_is_stopped() self.web_server_is_stopped()
self.server_status_indicator_says_closed(self.gui.receive_mode, False) self.server_status_indicator_says_closed(self.gui.receive_mode, False)
self.server_working_on_start_button_pressed(self.gui.receive_mode) self.server_working_on_start_button_pressed(self.gui.receive_mode)
self.a_server_is_started(self.gui.receive_mode) self.server_is_started(self.gui.receive_mode)
self.history_indicator(self.gui.receive_mode, public_mode) self.history_indicator(self.gui.receive_mode, public_mode)
def run_all_receive_mode_unwritable_dir_tests(self, public_mode, receive_allow_receiver_shutdown):
'''Attempt to upload (unwritable) files in receive mode and stop the share'''
self.run_all_receive_mode_setup_tests(public_mode)
self.upload_dir_permissions(0o400)
self.upload_file_should_fail(public_mode, '/tmp/OnionShare/test.txt')
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)
self.timeout_widget_hidden(self.gui.receive_mode)
self.server_timed_out(self.gui.receive_mode, 15000)
self.web_server_is_stopped()

View file

@ -1,29 +1,11 @@
import os
import requests
import socks import socks
import zipfile import zipfile
from PyQt5 import QtCore, QtTest from PyQt5 import QtCore, QtTest
from .GuiBaseTest import GuiBaseTest from .GuiBaseTest import GuiBaseTest
class GuiShareTest(GuiBaseTest): class GuiShareTest(GuiBaseTest):
# Auto-stop timer tests
def set_timeout(self, mode, timeout):
'''Test that the timeout can be set'''
timer = QtCore.QDateTime.currentDateTime().addSecs(timeout)
mode.server_status.shutdown_timeout.setDateTime(timer)
self.assertTrue(mode.server_status.shutdown_timeout.dateTime(), timer)
def timeout_widget_hidden(self, mode):
'''Test that the timeout widget is hidden when share has started'''
self.assertFalse(mode.server_status.shutdown_timeout_container.isVisible())
def server_timed_out(self, mode, wait):
'''Test that the server has timed out after the timer ran out'''
QtTest.QTest.qWait(wait)
# We should have timed out now
self.assertEqual(mode.server_status.status, 0)
# Persistence tests # Persistence tests
def have_same_slug(self, slug): def have_same_slug(self, slug):
'''Test that we have the same slug''' '''Test that we have the same slug'''
@ -31,19 +13,26 @@ class GuiShareTest(GuiBaseTest):
# Share-specific tests # Share-specific tests
def file_selection_widget_has_a_file(self): def file_selection_widget_has_files(self):
'''Test that the number of files in the list is 1''' '''Test that the number of items in the list is 2'''
self.assertEqual(self.gui.share_mode.server_status.file_selection.get_num_files(), 1) self.assertEqual(self.gui.share_mode.server_status.file_selection.get_num_files(), 2)
def deleting_only_file_hides_delete_button(self): def deleting_all_files_hides_delete_button(self):
'''Test that clicking on the file item shows the delete button. Test that deleting the only item in the list hides the delete button''' '''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)) 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()) 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 # Delete button should be visible
self.assertTrue(self.gui.share_mode.server_status.file_selection.delete_button.isVisible()) self.assertTrue(self.gui.share_mode.server_status.file_selection.delete_button.isVisible())
# Click delete, and since there's no more files, the delete button should be hidden # Click delete, delete button should still be visible since we have one more file
QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.delete_button, QtCore.Qt.LeftButton) QtTest.QTest.mouseClick(self.gui.share_mode.server_status.file_selection.delete_button, QtCore.Qt.LeftButton)
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
self.assertFalse(self.gui.share_mode.server_status.file_selection.delete_button.isVisible()) self.assertFalse(self.gui.share_mode.server_status.file_selection.delete_button.isVisible())
@ -61,6 +50,14 @@ class GuiShareTest(GuiBaseTest):
self.assertEqual(self.gui.share_mode.server_status.file_selection.get_num_files(), 2) self.assertEqual(self.gui.share_mode.server_status.file_selection.get_num_files(), 2)
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')
def add_delete_buttons_hidden(self): def add_delete_buttons_hidden(self):
'''Test that the add and delete buttons are hidden when the server starts''' '''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.add_button.isVisible())
@ -95,35 +92,56 @@ class GuiShareTest(GuiBaseTest):
QtTest.QTest.qWait(2000) QtTest.QTest.qWait(2000)
self.assertEqual('onionshare', zip.read('test.txt').decode('utf-8')) self.assertEqual('onionshare', zip.read('test.txt').decode('utf-8'))
def hit_404(self, public_mode):
'''Test that the server stops after too many 404s, or doesn't when in public_mode'''
bogus_path = '/gimme'
url = "http://127.0.0.1:{}/{}".format(self.gui.app.port, bogus_path)
for _ in range(20):
r = requests.get(url)
# A nasty hack to avoid the Alert dialog that blocks the rest of the test
if not public_mode:
self.gui.qtapp.exit()
# 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()
def add_button_visible(self): def add_button_visible(self):
'''Test that the add button should be visible''' '''Test that the add button should be visible'''
self.assertTrue(self.gui.share_mode.server_status.file_selection.add_button.isVisible()) self.assertTrue(self.gui.share_mode.server_status.file_selection.add_button.isVisible())
# 'Grouped' tests follow from here
def run_all_share_mode_setup_tests(self): def run_all_share_mode_setup_tests(self):
"""Tests in share mode prior to starting a share""" """Tests in share mode prior to starting a share"""
self.click_mode(self.gui.share_mode) self.click_mode(self.gui.share_mode)
self.file_selection_widget_has_a_file() self.file_selection_widget_has_files()
self.history_is_not_visible(self.gui.share_mode) self.history_is_not_visible(self.gui.share_mode)
self.click_toggle_history(self.gui.share_mode) self.click_toggle_history(self.gui.share_mode)
self.history_is_visible(self.gui.share_mode) self.history_is_visible(self.gui.share_mode)
self.deleting_only_file_hides_delete_button() self.deleting_all_files_hides_delete_button()
self.add_a_file_and_delete_using_its_delete_widget() self.add_a_file_and_delete_using_its_delete_widget()
self.file_selection_widget_readd_files() self.file_selection_widget_readd_files()
def run_all_share_mode_started_tests(self, public_mode): def run_all_share_mode_started_tests(self, public_mode, startup_time=2000):
"""Tests in share mode after starting a share""" """Tests in share mode after starting a share"""
self.server_working_on_start_button_pressed(self.gui.share_mode) self.server_working_on_start_button_pressed(self.gui.share_mode)
self.server_status_indicator_says_starting(self.gui.share_mode) self.server_status_indicator_says_starting(self.gui.share_mode)
self.add_delete_buttons_hidden() self.add_delete_buttons_hidden()
self.settings_button_is_hidden() self.settings_button_is_hidden()
self.a_server_is_started(self.gui.share_mode) self.server_is_started(self.gui.share_mode, startup_time)
self.a_web_server_is_running() self.web_server_is_running()
self.have_a_slug(self.gui.share_mode, public_mode) self.have_a_slug(self.gui.share_mode, public_mode)
self.url_description_shown(self.gui.share_mode) self.url_description_shown(self.gui.share_mode)
self.have_copy_url_button(self.gui.share_mode) self.have_copy_url_button(self.gui.share_mode, public_mode)
self.server_status_indicator_says_started(self.gui.share_mode) self.server_status_indicator_says_started(self.gui.share_mode)
@ -133,11 +151,11 @@ class GuiShareTest(GuiBaseTest):
self.download_share(public_mode) self.download_share(public_mode)
self.history_widgets_present(self.gui.share_mode) self.history_widgets_present(self.gui.share_mode)
self.server_is_stopped(self.gui.share_mode, stay_open) self.server_is_stopped(self.gui.share_mode, stay_open)
self.web_service_is_stopped() self.web_server_is_stopped()
self.server_status_indicator_says_closed(self.gui.share_mode, stay_open) self.server_status_indicator_says_closed(self.gui.share_mode, stay_open)
self.add_button_visible() self.add_button_visible()
self.server_working_on_start_button_pressed(self.gui.share_mode) self.server_working_on_start_button_pressed(self.gui.share_mode)
self.a_server_is_started(self.gui.share_mode) self.server_is_started(self.gui.share_mode)
self.history_indicator(self.gui.share_mode, public_mode) self.history_indicator(self.gui.share_mode, public_mode)
@ -148,6 +166,17 @@ class GuiShareTest(GuiBaseTest):
self.run_all_share_mode_download_tests(public_mode, stay_open) self.run_all_share_mode_download_tests(public_mode, stay_open)
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)
def run_all_share_mode_persistent_tests(self, public_mode, stay_open): def run_all_share_mode_persistent_tests(self, public_mode, stay_open):
"""Same as end-to-end share tests but also test the slug is the same on multiple shared""" """Same as end-to-end share tests but also test the slug is the same on multiple shared"""
self.run_all_share_mode_setup_tests() self.run_all_share_mode_setup_tests()
@ -164,5 +193,5 @@ class GuiShareTest(GuiBaseTest):
self.run_all_share_mode_started_tests(public_mode) self.run_all_share_mode_started_tests(public_mode)
self.timeout_widget_hidden(self.gui.share_mode) self.timeout_widget_hidden(self.gui.share_mode)
self.server_timed_out(self.gui.share_mode, 10000) self.server_timed_out(self.gui.share_mode, 10000)
self.web_service_is_stopped() self.web_server_is_stopped()

View file

@ -1,4 +1,5 @@
import json import json
import os
import requests import requests
import socks import socks
@ -24,6 +25,13 @@ class TorGuiBaseTest(GuiBaseTest):
testfile.write('onionshare') testfile.write('onionshare')
testfile.close() testfile.close()
# Create a test dir and files
if not os.path.exists('/tmp/testdir'):
testdir = os.mkdir('/tmp/testdir')
testfile = open('/tmp/testdir/test.txt', 'w')
testfile.write('onionshare')
testfile.close()
common = Common() common = Common()
common.settings = Settings(common) common.settings = Settings(common)
common.define_css() common.define_css()
@ -45,7 +53,7 @@ class TorGuiBaseTest(GuiBaseTest):
web = Web(common, False, False) web = Web(common, False, False)
open('/tmp/{}.json'.format(settings_filename), 'w').write(json.dumps(test_settings)) open('/tmp/{}.json'.format(settings_filename), 'w').write(json.dumps(test_settings))
gui = OnionShareGui(common, testonion, qtapp, app, ['/tmp/test.txt'], '/tmp/{}.json'.format(settings_filename), False) gui = OnionShareGui(common, testonion, qtapp, app, ['/tmp/test.txt', '/tmp/testdir'], '/tmp/{}.json'.format(settings_filename), False)
return gui return gui
def history_indicator(self, mode, public_mode): def history_indicator(self, mode, public_mode):
@ -91,12 +99,6 @@ class TorGuiBaseTest(GuiBaseTest):
QtTest.QTest.mouseClick(mode.toggle_history, QtCore.Qt.LeftButton) QtTest.QTest.mouseClick(mode.toggle_history, QtCore.Qt.LeftButton)
self.assertFalse(mode.toggle_history.indicator_label.isVisible()) self.assertFalse(mode.toggle_history.indicator_label.isVisible())
def a_server_is_started(self, mode):
'''Test that the server has started (overriding from local tests to wait for longer)'''
QtTest.QTest.qWait(45000)
# Should now be in SERVER_STARTED state
self.assertEqual(mode.server_status.status, 2)
def have_an_onion_service(self): def have_an_onion_service(self):
'''Test that we have a valid Onion URL''' '''Test that we have a valid Onion URL'''
self.assertRegex(self.gui.app.onion_host, r'[a-z2-7].onion') self.assertRegex(self.gui.app.onion_host, r'[a-z2-7].onion')
@ -127,6 +129,17 @@ class TorGuiBaseTest(GuiBaseTest):
self.assertTrue(string in f.read()) self.assertTrue(string in f.read())
f.close() f.close()
def have_copy_url_button(self, mode, public_mode):
'''Test that the Copy URL button is shown and that the clipboard is correct'''
self.assertTrue(mode.server_status.copy_url_button.isVisible())
QtTest.QTest.mouseClick(mode.server_status.copy_url_button, QtCore.Qt.LeftButton)
clipboard = self.gui.qtapp.clipboard()
if public_mode:
self.assertEqual(clipboard.text(), 'http://{}'.format(self.gui.app.onion_host))
else:
self.assertEqual(clipboard.text(), 'http://{}/{}'.format(self.gui.app.onion_host, mode.server_status.web.slug))
def cancel_the_share(self, mode): def cancel_the_share(self, mode):
'''Test that we can cancel this share before it's started up ''' '''Test that we can cancel this share before it's started up '''
self.server_working_on_start_button_pressed(self.gui.share_mode) self.server_working_on_start_button_pressed(self.gui.share_mode)
@ -138,13 +151,15 @@ class TorGuiBaseTest(GuiBaseTest):
QtTest.QTest.mouseRelease(mode.server_status.server_button, QtCore.Qt.LeftButton) QtTest.QTest.mouseRelease(mode.server_status.server_button, QtCore.Qt.LeftButton)
self.assertEqual(mode.server_status.status, 0) self.assertEqual(mode.server_status.status, 0)
self.server_is_stopped(self.gui.share_mode, False) self.server_is_stopped(self.gui.share_mode, False)
self.web_service_is_stopped() self.web_server_is_stopped()
# Stealth tests # Stealth tests
def copy_have_hidserv_auth_button(self, mode): def copy_have_hidserv_auth_button(self, mode):
'''Test that the Copy HidservAuth button is shown''' '''Test that the Copy HidservAuth button is shown'''
self.assertTrue(mode.server_status.copy_hidservauth_button.isVisible()) self.assertTrue(mode.server_status.copy_hidservauth_button.isVisible())
clipboard = self.gui.qtapp.clipboard()
self.assertRegex(clipboard.text(), r'HidServAuth %s [a-zA-Z1-9]' % self.gui.app.onion_host)
def hidserv_auth_string(self): def hidserv_auth_string(self):
'''Test the validity of the HidservAuth string''' '''Test the validity of the HidservAuth string'''

View file

@ -20,7 +20,11 @@ class TorGuiReceiveTest(TorGuiBaseTest):
QtTest.QTest.qWait(4000) QtTest.QTest.qWait(4000)
self.assertTrue(os.path.isfile(expected_file)) self.assertTrue(os.path.isfile(expected_file))
# 'Grouped' tests follow from here
def run_all_receive_mode_tests(self, public_mode, receive_allow_receiver_shutdown): def run_all_receive_mode_tests(self, public_mode, receive_allow_receiver_shutdown):
'''Run a full suite of tests in Receive mode'''
self.click_mode(self.gui.receive_mode) self.click_mode(self.gui.receive_mode)
self.history_is_not_visible(self.gui.receive_mode) self.history_is_not_visible(self.gui.receive_mode)
self.click_toggle_history(self.gui.receive_mode) self.click_toggle_history(self.gui.receive_mode)
@ -28,12 +32,12 @@ class TorGuiReceiveTest(TorGuiBaseTest):
self.server_working_on_start_button_pressed(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.server_status_indicator_says_starting(self.gui.receive_mode)
self.settings_button_is_hidden() self.settings_button_is_hidden()
self.a_server_is_started(self.gui.receive_mode) self.server_is_started(self.gui.receive_mode, startup_time=45000)
self.a_web_server_is_running() self.web_server_is_running()
self.have_an_onion_service() self.have_an_onion_service()
self.have_a_slug(self.gui.receive_mode, public_mode) self.have_a_slug(self.gui.receive_mode, public_mode)
self.url_description_shown(self.gui.receive_mode) self.url_description_shown(self.gui.receive_mode)
self.have_copy_url_button(self.gui.receive_mode) self.have_copy_url_button(self.gui.receive_mode, public_mode)
self.server_status_indicator_says_started(self.gui.receive_mode) self.server_status_indicator_says_started(self.gui.receive_mode)
self.web_page(self.gui.receive_mode, 'Select the files you want to send, then click', public_mode) self.web_page(self.gui.receive_mode, 'Select the files you want to send, then click', public_mode)
self.upload_file(public_mode, '/tmp/OnionShare/test.txt') self.upload_file(public_mode, '/tmp/OnionShare/test.txt')
@ -43,9 +47,9 @@ class TorGuiReceiveTest(TorGuiBaseTest):
self.counter_incremented(self.gui.receive_mode, 2) self.counter_incremented(self.gui.receive_mode, 2)
self.history_indicator(self.gui.receive_mode, public_mode) self.history_indicator(self.gui.receive_mode, public_mode)
self.server_is_stopped(self.gui.receive_mode, False) self.server_is_stopped(self.gui.receive_mode, False)
self.web_service_is_stopped() self.web_server_is_stopped()
self.server_status_indicator_says_closed(self.gui.receive_mode, False) self.server_status_indicator_says_closed(self.gui.receive_mode, False)
self.server_working_on_start_button_pressed(self.gui.receive_mode) self.server_working_on_start_button_pressed(self.gui.receive_mode)
self.a_server_is_started(self.gui.receive_mode) self.server_is_started(self.gui.receive_mode, startup_time=45000)
self.history_indicator(self.gui.receive_mode, public_mode) self.history_indicator(self.gui.receive_mode, public_mode)

View file

@ -6,6 +6,7 @@ from .GuiShareTest import GuiShareTest
class TorGuiShareTest(TorGuiBaseTest, GuiShareTest): class TorGuiShareTest(TorGuiBaseTest, GuiShareTest):
def download_share(self, public_mode): def download_share(self, public_mode):
'''Test downloading a share'''
# Set up connecting to the onion # Set up connecting to the onion
(socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port() (socks_address, socks_port) = self.gui.app.onion.get_tor_socks_port()
session = requests.session() session = requests.session()
@ -27,27 +28,46 @@ class TorGuiShareTest(TorGuiBaseTest, GuiShareTest):
file_to_write.close() file_to_write.close()
zip = zipfile.ZipFile('/tmp/download.zip') zip = zipfile.ZipFile('/tmp/download.zip')
QtTest.QTest.qWait(4000) QtTest.QTest.qWait(4000)
self.assertEquals('onionshare', zip.read('test.txt').decode('utf-8')) self.assertEqual('onionshare', zip.read('test.txt').decode('utf-8'))
# Persistence tests # Persistence tests
def have_same_onion(self, onion): def have_same_onion(self, onion):
'''Test that we have the same onion''' '''Test that we have the same onion'''
self.assertEqual(self.gui.app.onion_host, onion) self.assertEqual(self.gui.app.onion_host, onion)
# 'Grouped' tests follow from here
def run_all_share_mode_started_tests(self, public_mode): def run_all_share_mode_started_tests(self, public_mode):
"""Tests in share mode after starting a share""" """Tests in share mode after starting a share"""
self.server_working_on_start_button_pressed(self.gui.share_mode) self.server_working_on_start_button_pressed(self.gui.share_mode)
self.server_status_indicator_says_starting(self.gui.share_mode) self.server_status_indicator_says_starting(self.gui.share_mode)
self.add_delete_buttons_hidden() self.add_delete_buttons_hidden()
self.settings_button_is_hidden() self.settings_button_is_hidden()
self.a_server_is_started(self.gui.share_mode) self.server_is_started(self.gui.share_mode, startup_time=45000)
self.a_web_server_is_running() self.web_server_is_running()
self.have_an_onion_service() self.have_an_onion_service()
self.have_a_slug(self.gui.share_mode, public_mode) self.have_a_slug(self.gui.share_mode, public_mode)
self.url_description_shown(self.gui.share_mode) self.url_description_shown(self.gui.share_mode)
self.have_copy_url_button(self.gui.share_mode) self.have_copy_url_button(self.gui.share_mode, public_mode)
self.server_status_indicator_says_started(self.gui.share_mode) self.server_status_indicator_says_started(self.gui.share_mode)
def run_all_share_mode_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.download_share(public_mode)
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, startup_time=45000)
self.history_indicator(self.gui.share_mode, public_mode)
def run_all_share_mode_persistent_tests(self, public_mode, stay_open): def run_all_share_mode_persistent_tests(self, public_mode, stay_open):
"""Same as end-to-end share tests but also test the slug is the same on multiple shared""" """Same as end-to-end share tests but also test the slug is the same on multiple shared"""
self.run_all_share_mode_setup_tests() self.run_all_share_mode_setup_tests()
@ -58,6 +78,7 @@ class TorGuiShareTest(TorGuiBaseTest, GuiShareTest):
self.have_same_onion(onion) self.have_same_onion(onion)
self.have_same_slug(slug) self.have_same_slug(slug)
def run_all_share_mode_timer_tests(self, public_mode): def run_all_share_mode_timer_tests(self, public_mode):
"""Auto-stop timer tests in share mode""" """Auto-stop timer tests in share mode"""
self.run_all_share_mode_setup_tests() self.run_all_share_mode_setup_tests()
@ -65,5 +86,5 @@ class TorGuiShareTest(TorGuiBaseTest, GuiShareTest):
self.run_all_share_mode_started_tests(public_mode) self.run_all_share_mode_started_tests(public_mode)
self.timeout_widget_hidden(self.gui.share_mode) self.timeout_widget_hidden(self.gui.share_mode)
self.server_timed_out(self.gui.share_mode, 125000) self.server_timed_out(self.gui.share_mode, 125000)
self.web_service_is_stopped() self.web_server_is_stopped()

View file

@ -0,0 +1,21 @@
#!/usr/bin/env python3
import unittest
from .GuiShareTest import GuiShareTest
class Local404PublicModeRateLimitTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"close_after_first_download": False,
"public_mode": True
}
cls.gui = GuiShareTest.set_up(test_settings, 'Local404PublicModeRateLimitTest')
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_tests(True, True)
self.hit_404(True)
if __name__ == "__main__":
unittest.main()

View file

@ -0,0 +1,20 @@
#!/usr/bin/env python3
import unittest
from .GuiShareTest import GuiShareTest
class Local404RateLimitTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
"close_after_first_download": False
}
cls.gui = GuiShareTest.set_up(test_settings, 'Local404RateLimitTest')
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_share_mode_tests(False, True)
self.hit_404(False)
if __name__ == "__main__":
unittest.main()

View file

@ -0,0 +1,20 @@
#!/usr/bin/env python3
import unittest
from .GuiReceiveTest import GuiReceiveTest
class LocalReceiveModeTest(unittest.TestCase, GuiReceiveTest):
@classmethod
def setUpClass(cls):
test_settings = {
"receive_allow_receiver_shutdown": True
}
cls.gui = GuiReceiveTest.set_up(test_settings, 'LocalReceiveModeTest')
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_receive_mode_tests(False, True)
self.run_receive_mode_sender_closed_tests(False)
if __name__ == "__main__":
unittest.main()

View file

@ -0,0 +1,20 @@
#!/usr/bin/env python3
import unittest
from .GuiReceiveTest import GuiReceiveTest
class LocalReceiveModeTimerTest(unittest.TestCase, GuiReceiveTest):
@classmethod
def setUpClass(cls):
test_settings = {
"public_mode": False,
"shutdown_timeout": True,
}
cls.gui = GuiReceiveTest.set_up(test_settings, 'LocalReceiveModeTimerTest')
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_receive_mode_timer_tests(False)
if __name__ == "__main__":
unittest.main()

View file

@ -0,0 +1,19 @@
#!/usr/bin/env python3
import unittest
from .GuiReceiveTest import GuiReceiveTest
class LocalReceiveModeTest(unittest.TestCase, GuiReceiveTest):
@classmethod
def setUpClass(cls):
test_settings = {
"receive_allow_receiver_shutdown": True
}
cls.gui = GuiReceiveTest.set_up(test_settings, 'LocalReceiveModeTest')
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_receive_mode_unwritable_dir_tests(False, True)
if __name__ == "__main__":
unittest.main()

View file

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import pytest
import unittest import unittest
from .GuiReceiveTest import GuiReceiveTest from .GuiReceiveTest import GuiReceiveTest

View file

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import pytest
import unittest import unittest
from .GuiReceiveTest import GuiReceiveTest from .GuiReceiveTest import GuiReceiveTest

View file

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import pytest
import unittest import unittest
from .GuiShareTest import GuiShareTest from .GuiShareTest import GuiShareTest

View file

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import pytest
import unittest import unittest
from .GuiShareTest import GuiShareTest from .GuiShareTest import GuiShareTest

View file

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import pytest
import unittest import unittest
from .GuiShareTest import GuiShareTest from .GuiShareTest import GuiShareTest

View file

@ -0,0 +1,18 @@
#!/usr/bin/env python3
import unittest
from .GuiShareTest import GuiShareTest
class LocalShareModeTest(unittest.TestCase, GuiShareTest):
@classmethod
def setUpClass(cls):
test_settings = {
}
cls.gui = GuiShareTest.set_up(test_settings, 'LocalShareModeTest')
def test_gui(self):
self.run_all_common_setup_tests()
self.run_all_large_file_tests(False, True)
if __name__ == "__main__":
unittest.main()

View file

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import pytest
import unittest import unittest
from .GuiShareTest import GuiShareTest from .GuiShareTest import GuiShareTest

View file

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import pytest
import unittest import unittest
from .GuiShareTest import GuiShareTest from .GuiShareTest import GuiShareTest

View file

@ -19,7 +19,7 @@ class ShareModeCancelSecondShareTest(unittest.TestCase, TorGuiShareTest):
self.run_all_share_mode_tests(False, False) self.run_all_share_mode_tests(False, False)
self.cancel_the_share(self.gui.share_mode) self.cancel_the_share(self.gui.share_mode)
self.server_is_stopped(self.gui.share_mode, False) self.server_is_stopped(self.gui.share_mode, False)
self.web_service_is_stopped() self.web_server_is_stopped()
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()