Debian Codegraph @

src/tests/tests_encryption.cpp

raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "test_utils.h"
4 #include "tests.h"
5
6 #include "common/commandstatus.h"
7
8 #include <QRegularExpression>
9 #include <QStandardPaths>
10
11 void CoreTests::tabEncryption()
12 {
13 const QString tab = testTab(1);
14 const Args args = Args("tab") << tab << "separator" << " ";
15
16 // Enable tab encryption - this should work even if the encryption support
17 // is not build into the app.
18 RUN("config" << "encrypt_tabs" << "true", "true\n");
19
20 // Verify tab doesn't exist yet
21 QVERIFY( !hasTab(tab) );
22
23 // Add test data to the tab
24 RUN(args << "size", "0\n");
25 RUN(args << "add" << "test data 3" << "test data 2" << "test data 1", "");
26 QVERIFY( hasTab(tab) );
27
28 // Verify data was added correctly
29 RUN(args << "size", "3\n");
30 RUN(args << "read" << "0" << "1" << "2", "test data 1 test data 2 test data 3");
31
32 // Verify tab reloads properly
33 RUN("unload" << tab, tab + "\n");
34 RUN(args << "size", "3\n");
35 RUN(args << "read" << "0" << "1" << "2", "test data 1 test data 2 test data 3");
36
37 // Restart server to verify encryption/decryption works
38 // The COPYQ_PASSWORD environment variable should be used automatically
39 TEST( m_test->stopServer() );
40 TEST( m_test->startServer() );
41 RUN("show", "");
42 KEYS(clipboardBrowserId);
43
44 // Verify tab still exists
45 QVERIFY( hasTab(tab) );
46
47 // Verify data persisted and was decrypted correctly
48 RUN(args << "size", "3\n");
49 RUN(args << "read" << "0" << "1" << "2", "test data 1 test data 2 test data 3");
50
51 #ifdef WITH_QCA_ENCRYPTION
52 // Password is needed when disabling encryption
53 RUN_MULTIPLE(
54 [&]{ KEYS(passwordEntryCurrentId << ":TEST123" << "ENTER"); },
55 [&]{ RUN("config" << "encrypt_tabs" << "false", "false\n"); }
56 );
57 KEYS(clipboardBrowserId);
58 #endif
59
60 // Verify that password is not needed after disabling encryption
61 m_test->setEnv("COPYQ_PASSWORD", "");
62 TEST( m_test->stopServer() );
63 TEST( m_test->startServer() );
64 RUN(args << "size", "3\n");
65 RUN(args << "read" << "0" << "1" << "2", "test data 1 test data 2 test data 3");
66 }
67
68 void CoreTests::tabEncryptionPasswordNew()
69 {
70 #ifdef WITH_QCA_ENCRYPTION
71 m_test->setEnv("COPYQ_PASSWORD", "");
72 TEST( m_test->stopServer() );
73 TEST( m_test->startServer() );
74 RUN("show", "");
75 KEYS(clipboardBrowserId);
76
77 RUN_MULTIPLE(
78 [&]{
79 KEYS(
80 // Choose new password
81 passwordEntryNewId << ":TEST123" << "ENTER"
82 // Retype the password
83 << passwordEntryRetypeId << ":TEST123" << "ENTER"
84 );
85 },
86 [&]{ RUN("config" << "encrypt_tabs" << "true", "true\n"); }
87 );
88 KEYS(clipboardBrowserId);
89
90 const QString tab = testTab(1);
91 const Args args = Args("tab") << tab << "separator" << " ";
92
93 // Add test data to the tab
94 RUN(args << "size", "0\n");
95 RUN(args << "add" << "test data 3" << "test data 2" << "test data 1", "");
96 QVERIFY( hasTab(tab) );
97
98 // Verify data was added correctly
99 RUN(args << "size", "3\n");
100 RUN(args << "read" << "0" << "1" << "2", "test data 1 test data 2 test data 3");
101
102 // Verify tab reloads properly
103 RUN("unload" << tab, tab + "\n");
104 RUN(args << "size", "3\n");
105 RUN(args << "read" << "0" << "1" << "2", "test data 1 test data 2 test data 3");
106 #else
107 SKIP("Encryption support not built-in");
108 #endif
109 }
110
111 void CoreTests::tabEncryptionPasswordCurrent()
112 {
113 #ifdef WITH_QCA_ENCRYPTION
114 const QString tab = testTab(1);
115 const Args args = Args("tab") << tab << "separator" << " ";
116 RUN(args << "add" << "test data 3" << "test data 2" << "test data 1", "");
117
118 RUN("config" << "encrypt_tabs" << "true", "true\n");
119
120 m_test->setEnv("COPYQ_PASSWORD", ":TEST");
121 m_test->ignoreErrors(QRegularExpression(
122 "Unwrap DEK: authentication failed"
123 "|Loaded password does not unlock wrapped key"
124 ));
125 TEST( m_test->stopServer() );
126 TEST( m_test->startServer() );
127
128 KEYS(passwordEntryCurrentId << ":TEST123" << "ENTER");
129
130 WAIT_ON_OUTPUT(args << "size", "3\n");
131 RUN(args << "read" << "0" << "1" << "2", "test data 1 test data 2 test data 3");
132 #else
133 SKIP("Encryption support not built-in");
134 #endif
135 }
136
137 void CoreTests::tabEncryptionPasswordRetry()
138 {
139 #ifdef WITH_QCA_ENCRYPTION
140 m_test->setEnv("COPYQ_PASSWORD", "");
141 TEST( m_test->stopServer() );
142 TEST( m_test->startServer() );
143 RUN("show", "");
144 KEYS(clipboardBrowserId);
145
146 RUN_MULTIPLE(
147 [&]{
148 KEYS(
149 // Choose new password - attempt 1
150 passwordEntryNewId << ":TEST123" << "ENTER"
151 // Retype the password - attempt 1
152 << passwordEntryRetypeId << ":TEST1234" << "ENTER"
153 // Choose new password - attempt 2
154 << passwordEntryNewId << ":TEST123" << "ENTER"
155 // Retype the password - attempt 2
156 << passwordEntryRetypeId << ":TEST123" << "ENTER"
157 );
158 },
159 [&]{ RUN("config" << "encrypt_tabs" << "true", "true\n"); }
160 );
161 KEYS(clipboardBrowserId);
162 #else
163 SKIP("Encryption support not built-in");
164 #endif
165 }
166
167 void CoreTests::tabEncryptionPasswordRetryFail()
168 {
169 #ifdef WITH_QCA_ENCRYPTION
170 m_test->setEnv("COPYQ_PASSWORD", "");
171 TEST( m_test->stopServer() );
172 TEST( m_test->startServer() );
173 RUN("show", "");
174 KEYS(clipboardBrowserId);
175
176 RUN_MULTIPLE(
177 [&]{
178 KEYS(
179 // Choose new password - attempt 1
180 passwordEntryNewId << ":TEST123" << "ENTER"
181 // Retype the password - attempt 1
182 << passwordEntryRetypeId << ":TEST1234" << "ENTER"
183
184 // Choose new password - attempt 2
185 << passwordEntryNewId << "ENTER"
186 // Empty password - attempt 2
187 << passwordMessageEmptyId << "ENTER"
188
189 // Choose new password - attempt 3
190 << passwordEntryNewId << ":TEST123" << "ENTER"
191 // Retype the password - attempt 3
192 << passwordEntryRetypeId << ":TEST1234" << "ENTER"
193
194 << passwordMessageFailedId << "ENTER"
195 );
196 },
197 [&]{ RUN("config" << "encrypt_tabs" << "true", "true\n"); }
198 );
199
200 // If the initial password was not provided, encryption should be disabled.
201 RUN("config" << "encrypt_tabs", "false\n");
202 #else
203 SKIP("Encryption support not built-in");
204 #endif
205 }
206
207 void CoreTests::tabEncryptionPasswordAbortRetry()
208 {
209 #ifdef WITH_QCA_ENCRYPTION
210 TEST( m_test->stopServer() );
211 TEST( m_test->startServer() );
212 RUN("show", "");
213 KEYS(clipboardBrowserId);
214
215 RUN("config" << "encrypt_tabs" << "true", "true\n");
216
217 TEST( m_test->stopServer() );
218 m_test->setEnv("COPYQ_PASSWORD", "");
219 TEST( m_test->startServer() );
220
221 m_test->ignoreErrors(QRegularExpression("Tab encryption password required but not provided"));
222 KEYS(passwordEntryCurrentId << "ESC");
223 KEYS(clipboardBrowserRefreshButtonId);
224
225 RUN_EXPECT_ERROR_WITH_STDERR("add('A')", CommandException, "Invalid tab");
226
227 KEYS(clipboardBrowserRefreshButtonId << "SPACE");
228 KEYS(passwordEntryCurrentId << ":TEST123" << "ENTER");
229 KEYS(clipboardBrowserId);
230
231 RUN("add('A')", "");
232 RUN("read(0)", "A");
233 #else
234 SKIP("Encryption support not built-in");
235 #endif
236 }
237
238 void CoreTests::tabEncryptionLargeItems()
239 {
240 #ifdef WITH_QCA_ENCRYPTION
241 RUN("config" << "encrypt_tabs" << "true", "true\n");
242
243 const auto tab = testTab(1);
244 const auto args = Args("tab") << tab;
245
246 const auto script = R"(
247 write(0, [{
248 'text/plain': '1234567890'.repeat(10000),
249 'application/x-copyq-test-data': 'abcdefghijklmnopqrstuvwxyz'.repeat(10000),
250 }])
251 )";
252 RUN(args << script, "");
253
254 RUN(args << "read(0).left(20)", "12345678901234567890");
255 RUN(args << "read(0).length", "100000\n");
256 RUN(args << "ItemSelection().selectAll().itemAtIndex(0)[mimeText].length", "100000\n");
257
258 RUN("unload" << tab, tab + "\n");
259 RUN("show" << tab, "");
260
261 // Read data after reloading the tab
262 RUN(args << "read(0).left(20)", "12345678901234567890");
263 RUN(args << "read(0).length", "100000\n");
264 RUN(args << "ItemSelection().selectAll().itemAtIndex(0)[mimeText].length", "100000\n");
265 #else
266 SKIP("Encryption support not built-in");
267 #endif
268 }
269
270 void CoreTests::tabEncryptionChangePassword()
271 {
272 #ifdef WITH_QCA_ENCRYPTION
273 RUN("config" << "encrypt_tabs" << "true", "true\n");
274
275 const auto tab = testTab(1);
276 const auto args = Args("tab") << tab;
277 RUN("show" << tab, "");
278
279 RUN(args << "add" << "3" << "2" << "1", "");
280 QVERIFY( hasTab(tab) );
281 RUN(args << "size", "3\n");
282 RUN(args << "read" << "0" << "1" << "2" << "3", "1\n2\n3\n");
283
284 // Open preferences dialog.
285 KEYS("Ctrl+P" << configurationDialogId);
286
287 KEYS("mouse|PRESS|pushButtonChangeEncryptionPassword");
288 KEYS("mouse|RELEASE|pushButtonChangeEncryptionPassword");
289 KEYS(
290 // Current password is always needed when changing
291 passwordEntryCurrentId << ":TEST123" << "ENTER"
292
293 << passwordEntryNewId << ":TEST1234" << "ENTER"
294 << passwordEntryRetypeId << ":TEST1234" << "ENTER"
295 << passwordMessageChangeId << "ENTER"
296 );
297
298 RUN(args << "size", "3\n");
299 RUN(args << "read" << "0" << "1" << "2" << "3", "1\n2\n3\n");
300
301 TEST( m_test->stopServer() );
302 m_test->setEnv("COPYQ_PASSWORD", "TEST1234");
303 TEST( m_test->startServer() );
304
305 RUN(args << "size", "3\n");
306 RUN(args << "read" << "0" << "1" << "2" << "3", "1\n2\n3\n");
307 #else
308 SKIP("Encryption support not built-in");
309 #endif
310 }