1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
import pytest
import os
import requests
import shutil
import sys
from datetime import datetime, timedelta
from PySide2 import QtCore, QtTest
from .gui_base_test import GuiBaseTest
class TestReceive(GuiBaseTest):
# Shared test methods
def upload_file(
self, tab, file_to_upload, expected_basename, identical_files_at_once=False
):
"""Test that we can upload the file"""
# Wait 2 seconds to make sure the filename, based on timestamp, isn't accidentally reused
QtTest.QTest.qWait(2000, self.gui.qtapp)
files = {"file[]": open(file_to_upload, "rb")}
url = f"http://127.0.0.1:{tab.app.port}/upload"
if tab.settings.get("general", "public"):
requests.post(url, files=files)
if identical_files_at_once:
# Send a duplicate upload to test for collisions
requests.post(url, files=files)
else:
requests.post(
url,
files=files,
auth=requests.auth.HTTPBasicAuth(
"onionshare", tab.get_mode().web.password
),
)
if identical_files_at_once:
# Send a duplicate upload to test for collisions
requests.post(
url,
files=files,
auth=requests.auth.HTTPBasicAuth(
"onionshare", tab.get_mode().web.password
),
)
QtTest.QTest.qWait(1000, self.gui.qtapp)
# Make sure the file is within the last 10 seconds worth of fileames
exists = False
now = datetime.now()
for _ in range(10):
date_dir = now.strftime("%Y-%m-%d")
if identical_files_at_once:
time_dir = now.strftime("%H%M%S-1")
else:
time_dir = now.strftime("%H%M%S")
receive_mode_dir = os.path.join(
tab.settings.get("receive", "data_dir"), date_dir, time_dir
)
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, tab):
"""Test that we can't upload the file when permissions are wrong, and expected content is shown"""
QtTest.QTest.qWait(1000, self.gui.qtapp)
files = {"file[]": open(self.tmpfile_test, "rb")}
url = f"http://127.0.0.1:{tab.app.port}/upload"
if tab.settings.get("general", "public"):
r = requests.post(url, files=files)
else:
r = requests.post(
url,
files=files,
auth=requests.auth.HTTPBasicAuth(
"onionshare", tab.get_mode().web.password
),
)
def accept_dialog():
window = tab.common.gui.qtapp.activeWindow()
if window:
window.close()
QtCore.QTimer.singleShot(1000, accept_dialog)
self.assertTrue("Error uploading, please inform the OnionShare user" in r.text)
def try_without_auth_in_non_public_mode(self, tab):
r = requests.post(f"http://127.0.0.1:{tab.app.port}/upload")
self.assertEqual(r.status_code, 401)
r = requests.get(f"http://127.0.0.1:{tab.app.port}/close")
self.assertEqual(r.status_code, 401)
# 'Grouped' tests follow from here
def run_all_receive_mode_setup_tests(self, tab):
"""Set up a share in Receive mode and start it"""
self.history_is_not_visible(tab)
self.click_toggle_history(tab)
self.history_is_visible(tab)
self.server_working_on_start_button_pressed(tab)
self.server_status_indicator_says_starting(tab)
self.server_is_started(tab)
self.web_server_is_running(tab)
self.have_a_password(tab)
self.url_description_shown(tab)
self.have_copy_url_button(tab)
self.have_show_qr_code_button(tab)
self.server_status_indicator_says_started(tab)
def run_all_receive_mode_tests(self, tab):
"""Upload files in receive mode and stop the share"""
self.run_all_receive_mode_setup_tests(tab)
if not tab.settings.get("general", "public"):
self.try_without_auth_in_non_public_mode(tab)
self.upload_file(tab, self.tmpfile_test, "test.txt")
self.history_widgets_present(tab)
self.counter_incremented(tab, 1)
self.upload_file(tab, self.tmpfile_test, "test.txt")
self.counter_incremented(tab, 2)
self.upload_file(tab, self.tmpfile_test2, "test2.txt")
self.counter_incremented(tab, 3)
self.upload_file(tab, self.tmpfile_test2, "test2.txt")
self.counter_incremented(tab, 4)
# Test uploading the same file twice at the same time, and make sure no collisions
self.upload_file(tab, self.tmpfile_test, "test.txt", True)
self.counter_incremented(tab, 6)
self.history_indicator(tab, "2")
self.server_is_stopped(tab)
self.web_server_is_stopped(tab)
self.server_status_indicator_says_closed(tab)
self.server_working_on_start_button_pressed(tab)
self.server_is_started(tab)
self.history_indicator(tab, "2")
def run_all_clear_all_button_tests(self, tab):
"""Test the Clear All history button"""
self.run_all_receive_mode_setup_tests(tab)
self.upload_file(tab, self.tmpfile_test, "test.txt")
self.history_widgets_present(tab)
self.clear_all_history_items(tab, 0)
self.upload_file(tab, self.tmpfile_test, "test.txt")
self.clear_all_history_items(tab, 2)
def run_all_upload_non_writable_dir_tests(self, tab):
"""Test uploading a file when the data_dir is non-writable"""
upload_dir = os.path.join(self.tmpdir.name, "OnionShare")
shutil.rmtree(upload_dir, ignore_errors=True)
os.makedirs(upload_dir, 0o700)
# Set the upload dir setting
tab.get_mode().data_dir_lineedit.setText(upload_dir)
tab.settings.set("receive", "data_dir", upload_dir)
self.run_all_receive_mode_setup_tests(tab)
os.chmod(upload_dir, 0o400)
self.upload_file_should_fail(tab)
self.server_is_stopped(tab)
self.web_server_is_stopped(tab)
self.server_status_indicator_says_closed(tab)
os.chmod(upload_dir, 0o700)
# Tests
def test_clear_all_button(self):
"""
Clear all history items should work
"""
tab = self.new_receive_tab()
self.run_all_common_setup_tests()
self.run_all_clear_all_button_tests(tab)
self.close_all_tabs()
def test_autostop_timer(self):
"""
Test autostop timer
"""
tab = self.new_receive_tab()
tab.get_mode().mode_settings_widget.toggle_advanced_button.click()
tab.get_mode().mode_settings_widget.autostop_timer_checkbox.click()
self.run_all_common_setup_tests()
self.run_all_receive_mode_setup_tests(tab)
self.set_timeout(tab, 5)
self.autostop_timer_widget_hidden(tab)
self.server_timed_out(tab, 15000)
self.web_server_is_stopped(tab)
self.close_all_tabs()
def test_upload(self):
"""
Test uploading files
"""
tab = self.new_receive_tab()
self.run_all_common_setup_tests()
self.run_all_receive_mode_tests(tab)
self.close_all_tabs()
@pytest.mark.skipif(sys.platform == "win32", reason="Windows doesn't have chmod")
def test_upload_non_writable_dir(self):
"""
Test uploading files to a non-writable directory
"""
tab = self.new_receive_tab()
self.run_all_upload_non_writable_dir_tests(tab)
self.close_all_tabs()
def test_public_upload(self):
"""
Test uploading files in public mode
"""
tab = self.new_receive_tab()
tab.get_mode().mode_settings_widget.public_checkbox.click()
self.run_all_common_setup_tests()
self.run_all_receive_mode_tests(tab)
self.close_all_tabs()
@pytest.mark.skipif(sys.platform == "win32", reason="Windows doesn't have chmod")
def test_public_upload_non_writable_dir(self):
"""
Test uploading files to a non-writable directory in public mode
"""
tab = self.new_receive_tab()
tab.get_mode().mode_settings_widget.public_checkbox.click()
self.run_all_upload_non_writable_dir_tests(tab)
self.close_all_tabs()
|