Debian Codegraph @

src/tests/tests_import_export.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/temporaryfile.h"
7
8 #include <QRegularExpression>
9
10 namespace {
11
12 const QString exportFilePath(const QString &suffix = QStringLiteral("1"))
13 {
14 return QStringLiteral("%1/copyq-test-%2.cpq")
15 .arg(qEnvironmentVariable("COPYQ_SETTINGS_PATH"), suffix);
16 }
17
18 } // namespace
19
20 void CoreTests::exportImport(int flags)
21 {
22 #ifdef Q_OS_MAC
23 if (flags & (ExportSettings | ExportCommands))
24 SKIP("Mnemonic for focusing shortcut button doesn't work on OS X");
25 #endif
26 #ifndef WITH_QCA_ENCRYPTION
27 if (flags & ExportWithPassword)
28 SKIP("Encryption support not built-in");
29 #endif
30 const auto tab = testTab(1);
31 const auto args = Args("tab") << tab;
32
33 if (flags & ExportTab) {
34 RUN("show" << tab, "");
35
36 RUN(args << "add" << "3" << "2" << "1", "");
37 QVERIFY( hasTab(tab) );
38 RUN(args << "size", "3\n");
39 RUN(args << "read" << "0" << "1" << "2" << "3", "1\n2\n3\n");
40 }
41
42 RUN("setCommands([{name: 'test', cmd: 'copyq help'}])", "");
43 RUN("commands()[0].name", "test\n");
44 RUN("commands()[0].cmd", "copyq help\n");
45 RUN("commands()[0].enable", "true\n");
46 RUN("commands().length", "1\n");
47
48 RUN("config('maxitems', 20)", "20\n");
49
50 // Export
51 KEYS("Ctrl+S" << exportDialogId);
52 if (flags & ExportSettings)
53 KEYS(exportDialogId << "ALT+N"); // Enable exporting configuration
54 if (flags & ExportCommands)
55 KEYS(exportDialogId << "ALT+M"); // Enable exporting commands
56 KEYS("ENTER");
57
58 // Export with password only if supported
59 #ifdef WITH_QCA_ENCRYPTION
60 KEYS(passwordEntryExportId);
61 if (flags & ExportWithPassword) {
62 KEYS(":TEST1234" << "ENTER");
63 KEYS(passwordEntryRetypeId << ":TEST1234");
64 }
65 KEYS("ENTER");
66 #endif
67
68 KEYS(fileNameEditId << ":" + exportFilePath());
69 KEYS("ENTER");
70 KEYS(clipboardBrowserId);
71 Q_ASSERT(QFile::exists(exportFilePath()));
72
73 RUN("config('maxitems', 10)", "10\n");
74
75 // Import
76 KEYS("Ctrl+Shift+I" << fileNameEditId << ":" + exportFilePath());
77 KEYS("ENTER");
78
79 // Import with password only if supported
80 #ifdef WITH_QCA_ENCRYPTION
81 if (flags & ExportWithPassword) {
82 KEYS(passwordEntryImportId << ":TEST1234");
83 KEYS("ENTER");
84 }
85 #endif
86
87 KEYS(importDialogId << "ENTER");
88 KEYS(clipboardBrowserId);
89
90 if (flags & ExportTab) {
91 const auto tabCopy = QStringLiteral("%1 (1)").arg(tab);
92 RUN("tab", QLatin1String(clipboardTabName) + "\n" + tab + "\n" + tabCopy + "\n");
93 const auto args1 = Args("tab") << tabCopy;
94 RUN(args1 << "size", "3\n");
95 RUN(args1 << "read" << "0" << "1" << "2" << "3", "1\n2\n3\n");
96 }
97
98 if (flags & ExportSettings)
99 RUN("config('maxitems')", "20\n");
100 else
101 RUN("config('maxitems')", "10\n");
102
103 RUN("commands()[0].name", "test\n");
104 RUN("commands()[0].cmd", "copyq help\n");
105 RUN("commands()[0].enable", "true\n");
106 if (flags & ExportCommands) {
107 RUN("commands()[1].name", "test\n");
108 RUN("commands()[1].cmd", "copyq help\n");
109 RUN("commands()[1].enable", "true\n");
110 RUN("commands().length", "2\n");
111 } else {
112 RUN("commands().length", "1\n");
113 }
114 }
115
116 void CoreTests::exportImportNoPasswordTab() { exportImport(ExportTab); }
117 void CoreTests::exportImportNoPasswordSettingsOnly() { exportImport(ExportSettings); }
118 void CoreTests::exportImportNoPasswordCommandsOnly() { exportImport(ExportCommands); }
119 void CoreTests::exportImportPasswordTab() { exportImport(ExportWithPassword | ExportTab); }
120 void CoreTests::exportImportPasswordSettingsOnly() { exportImport(ExportWithPassword | ExportSettings); }
121 void CoreTests::exportImportPasswordCommandsOnly() { exportImport(ExportWithPassword | ExportCommands); }
122
123 void CoreTests::exportImportErrors()
124 {
125 #ifdef Q_OS_WIN
126 const auto fileNotFound = QStringLiteral("The system cannot find the path specified.");
127 #else
128 const auto fileNotFound = QStringLiteral("No such file or directory");
129 #endif
130
131 // Import non-existent file
132 const auto invalidFile = QStringLiteral("/tmp/copyq-invalid/file.cpq");
133 const auto openError = QStringLiteral(
134 R"(Failed to open file for import: "%1" message: "%2")"
135 ).arg(QRegularExpression::escape(invalidFile), fileNotFound);
136 m_test->ignoreErrors(QRegularExpression(openError));
137 const auto clientErrorTemplate = QStringLiteral("ScriptError: Failed to import file \"%1\"");
138 const auto clientErrorInvalid = clientErrorTemplate.arg(invalidFile);
139 RUN_EXPECT_ERROR_WITH_STDERR("importData" << invalidFile, 4, clientErrorInvalid);
140 RUN_EXPECT_ERROR_WITH_STDERR("importTab" << invalidFile, 4, clientErrorInvalid);
141 QVERIFY2( dropLogsToFileCountAndSize(0, 0), "Failed to remove log files" );
142
143 // Export to non-existent directory
144 const auto exportError = QStringLiteral(
145 R"(Failed to open file for export( \(v2\))?: "%1" message: "%2")"
146 ).arg(QRegularExpression::escape(invalidFile), fileNotFound);
147 m_test->ignoreErrors(QRegularExpression(exportError));
148 const auto clientErrorExport =
149 QStringLiteral("ScriptError: Failed to export file \"%1\"").arg(invalidFile);
150 RUN_EXPECT_ERROR_WITH_STDERR("exportData" << invalidFile, 4, clientErrorExport);
151 RUN_EXPECT_ERROR_WITH_STDERR("exportTab" << invalidFile, 4, clientErrorExport);
152 QVERIFY2( dropLogsToFileCountAndSize(0, 0), "Failed to remove log files" );
153
154 // Import invalid file
155 QFile tmp(exportFilePath(QStringLiteral("invalid")));
156 QVERIFY(tmp.open(QFile::WriteOnly));
157 tmp.write("TEST");
158 tmp.close();
159 const auto path = tmp.fileName();
160
161 const auto headerError = QStringLiteral(
162 R"(Failed to read file header for import - file: "%1" reason: 1)"
163 ).arg(QRegularExpression::escape(path));
164 m_test->ignoreErrors(QRegularExpression(headerError));
165 const auto clientError = clientErrorTemplate.arg(path);
166 RUN_EXPECT_ERROR_WITH_STDERR("importData" << path, 4, clientError);
167 RUN_EXPECT_ERROR_WITH_STDERR("importTab" << path, 4, clientError);
168 QVERIFY2( dropLogsToFileCountAndSize(0, 0), "Failed to remove log files" );
169
170 #ifndef Q_OS_WIN
171 // Import file without permissions
172 tmp.setPermissions(QFile::Permissions());
173 const auto permissionError = QStringLiteral(
174 R"(Failed to open file for import: "%1" message: "Permission denied")"
175 ).arg(QRegularExpression::escape(path));
176 m_test->ignoreErrors(QRegularExpression(permissionError));
177 RUN_EXPECT_ERROR_WITH_STDERR("importData" << path, 4, clientError);
178 RUN_EXPECT_ERROR_WITH_STDERR("importTab" << path, 4, clientError);
179 QVERIFY2( dropLogsToFileCountAndSize(0, 0), "Failed to remove log files" );
180 #endif
181
182 // Export incomplete file
183 const auto pathPartial = exportFilePath(QStringLiteral("Partial"));
184 m_test->ignoreErrors({});
185 RUN("exportData" << pathPartial, "");
186
187 QFile f(pathPartial);
188 QVERIFY2( f.resize(f.size() - 1), f.errorString().toUtf8() );
189 const auto importErrorV = QStringLiteral(R"(Import \(v%1\) failed - file: "%2" reason: 1)");
190 const auto importErrorV4 = importErrorV.arg(4).arg(QRegularExpression::escape(pathPartial));
191 m_test->ignoreErrors(QRegularExpression(importErrorV4));
192 const auto clientErrorPartial = clientErrorTemplate.arg(pathPartial);
193 RUN_EXPECT_ERROR_WITH_STDERR("importData" << pathPartial, 4, clientErrorPartial);
194 QVERIFY2( dropLogsToFileCountAndSize(0, 0), "Failed to remove log files" );
195
196 QVERIFY(f.open(QFile::WriteOnly));
197 QDataStream in(&f);
198 in.setVersion(QDataStream::Qt_4_7);
199 in << QByteArray("CopyQ v3");
200 f.close();
201 const auto importErrorV3 = importErrorV.arg(3).arg(QRegularExpression::escape(pathPartial));
202 m_test->ignoreErrors(QRegularExpression(importErrorV3));
203 RUN_EXPECT_ERROR_WITH_STDERR("importData" << pathPartial, 4, clientErrorPartial);
204 QVERIFY2( dropLogsToFileCountAndSize(0, 0), "Failed to remove log files" );
205
206 QVERIFY(f.open(QFile::WriteOnly));
207 in << QByteArray("CopyQ v4");
208 f.close();
209 m_test->ignoreErrors(QRegularExpression(importErrorV4));
210 RUN_EXPECT_ERROR_WITH_STDERR("importData" << pathPartial, 4, clientErrorPartial);
211 QVERIFY2( dropLogsToFileCountAndSize(0, 0), "Failed to remove log files" );
212
213 QVERIFY(f.open(QFile::WriteOnly));
214 in << QByteArray("CopyQ v2");
215 f.close();
216 const auto importErrorV2 = importErrorV.arg(2).arg(QRegularExpression::escape(pathPartial));
217 m_test->ignoreErrors(QRegularExpression(importErrorV2));
218 RUN_EXPECT_ERROR_WITH_STDERR("importData" << pathPartial, 4, clientErrorPartial);
219 }