| 1 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
|
| 3 |
#include "test_utils.h" |
| 4 |
#include "tests.h" |
| 5 |
#include "tests_common.h" |
| 6 |
|
| 7 |
#include "common/commandstatus.h" |
| 8 |
#include "common/config.h" |
| 9 |
#include "common/log.h" |
| 10 |
#include "common/version.h" |
| 11 |
#include "common/settings.h" |
| 12 |
|
| 13 |
#include <QRegularExpression> |
| 14 |
|
| 15 |
void CoreTests::configPath() |
| 16 |
{ |
| 17 |
RUN("print(info('config'))", Settings().fileName()); |
| 18 |
} |
| 19 |
|
| 20 |
void CoreTests::readLog() |
| 21 |
{ |
| 22 |
QByteArray stdoutActual; |
| 23 |
QByteArray stderrActual; |
| 24 |
QCOMPARE( run(Args("info") << "log", &stdoutActual, &stderrActual), 0 ); |
| 25 |
QVERIFY2( testStderr(stderrActual), stderrActual ); |
| 26 |
|
| 27 |
QCOMPARE(stdoutActual, qEnvironmentVariable("COPYQ_LOG_FILE") + "\n"); |
| 28 |
|
| 29 |
const QString logFile = logFileName(); |
| 30 |
QCOMPARE(stdoutActual, logFile + "\n"); |
| 31 |
|
| 32 |
const QByteArray log = readLogFile(maxReadLogSize); |
| 33 |
QVERIFY2(!log.isEmpty(), logFile.toUtf8()); |
| 34 |
|
| 35 |
const QStringList lines = splitLines(readLogFile(maxReadLogSize)); |
| 36 |
|
| 37 |
const auto configPattern = QStringLiteral( |
| 38 |
R"(^\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}\] DEBUG <Server-\d+>: Loading configuration$)"); |
| 39 |
QVERIFY2(count(lines, configPattern), log); |
| 40 |
|
| 41 |
const auto monitorPattern = QStringLiteral( |
| 42 |
R"(^\[.*\] DEBUG <Server-\d+>: Starting monitor$)"); |
| 43 |
QVERIFY2(count(lines, monitorPattern), log); |
| 44 |
|
| 45 |
{ |
| 46 |
QTemporaryDir tmpDir; |
| 47 |
QVERIFY(tmpDir.isValid()); |
| 48 |
m_test->setEnv("COPYQ_LOG_FILE", ""); |
| 49 |
m_test->setEnv("COPYQ_LOG_DIR", tmpDir.path()); |
| 50 |
QCOMPARE( run(Args("info") << "log", &stdoutActual, &stderrActual), 0 ); |
| 51 |
QVERIFY2( testStderr(stderrActual), stderrActual ); |
| 52 |
const QString actualPattern = QString::fromUtf8(stdoutActual) |
| 53 |
.replace(QRegularExpression(R"(-\d*-\d*\.log\n$)"), "*"); |
| 54 |
const QString expectedPattern = QStringLiteral("%1/copyq*").arg(tmpDir.path()); |
| 55 |
QCOMPARE( actualPattern, expectedPattern ); |
| 56 |
} |
| 57 |
|
| 58 |
{ |
| 59 |
m_test->setEnv("COPYQ_LOG_FILE", ""); |
| 60 |
m_test->setEnv("COPYQ_LOG_DIR", ""); |
| 61 |
QCOMPARE( run(Args("info") << "log", &stdoutActual, &stderrActual), 0 ); |
| 62 |
QVERIFY2( testStderr(stderrActual), stderrActual ); |
| 63 |
const QString actualPattern = QString::fromUtf8(stdoutActual) |
| 64 |
.replace(QRegularExpression(R"(-\d*-\d*\.log\n$)"), "*"); |
| 65 |
const QString expectedPattern = QStringLiteral("%1/copyq*") |
| 66 |
.arg(getDefaultLogFilePath()); |
| 67 |
QCOMPARE( actualPattern, expectedPattern ); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
void CoreTests::rotateLog() |
| 72 |
{ |
| 73 |
QByteArray stdoutActual; |
| 74 |
QByteArray stderrActual; |
| 75 |
QCOMPARE( run(Args("info") << "log", &stdoutActual, &stderrActual), 0 ); |
| 76 |
QVERIFY2( testStderr(stderrActual), stderrActual ); |
| 77 |
const QString logDirPath = QString::fromUtf8(stdoutActual).section("/", 0, -2); |
| 78 |
QVERIFY( !logDirPath.isEmpty() ); |
| 79 |
|
| 80 |
const QByteArray logData(logFileSize, '-'); |
| 81 |
const QDir logDir(logDirPath); |
| 82 |
QVERIFY2( logDir.exists(), logDirPath.toUtf8() ); |
| 83 |
const auto listLogFiles = [&](const int rotateNumber = -1) { |
| 84 |
const QString pattern = rotateNumber == -1 |
| 85 |
? QStringLiteral("tests*.log.*") |
| 86 |
: QStringLiteral("tests*.log.%1").arg(rotateNumber); |
| 87 |
return logDir.entryList({pattern}, QDir::Files, QDir::Name); |
| 88 |
}; |
| 89 |
const auto logFilesMessage = [&](int i) { |
| 90 |
return QStringLiteral("%2 (rotateCount = %1):\n %3").arg(i).arg( |
| 91 |
logDirPath.toUtf8(), |
| 92 |
listLogFiles().join("\n ")).toUtf8(); |
| 93 |
}; |
| 94 |
|
| 95 |
for (int i = 1; i < logFileCount; ++i) { |
| 96 |
QCOMPARE( run(Args("serverLog") << "-", &stdoutActual, &stderrActual, logData), 0 ); |
| 97 |
QVERIFY2( testStderr(stderrActual), stderrActual ); |
| 98 |
QVERIFY2( listLogFiles().count() >= i, logFilesMessage(i) ); |
| 99 |
QVERIFY2( !listLogFiles(i).isEmpty(), logFilesMessage(i) ); |
| 100 |
} |
| 101 |
QCOMPARE( run(Args("serverLog") << "-", &stdoutActual, &stderrActual, logData), 0 ); |
| 102 |
QVERIFY2( testStderr(stderrActual), stderrActual ); |
| 103 |
const auto logFiles = listLogFiles(logFileCount); |
| 104 |
QVERIFY2( logFiles.isEmpty(), logFilesMessage(logFileCount) ); |
| 105 |
} |
| 106 |
|
| 107 |
void CoreTests::pluginsDisabled() |
| 108 |
{ |
| 109 |
// Ensure that plugins that could interfere with tests are disabled |
| 110 |
RUN_EXPECT_ERROR("plugins.itemsync", CommandException); |
| 111 |
RUN_EXPECT_ERROR("plugins.itemencrypted", CommandException); |
| 112 |
RUN_EXPECT_ERROR("plugins.itempinned", CommandException); |
| 113 |
|
| 114 |
const QByteArray log = readLogFile(maxReadLogSize); |
| 115 |
QVERIFY2( !log.contains("itemencrypted"), log ); |
| 116 |
QVERIFY2( !log.contains("[copyq.plugin"), log ); |
| 117 |
} |
| 118 |
|
| 119 |
void CoreTests::commandHelp() |
| 120 |
{ |
| 121 |
QByteArray stdoutActual; |
| 122 |
QByteArray stderrActual; |
| 123 |
QCOMPARE( run(Args("help"), &stdoutActual, &stderrActual), 0 ); |
| 124 |
QVERIFY2( testStderr(stderrActual), stderrActual ); |
| 125 |
QVERIFY(!stdoutActual.isEmpty()); |
| 126 |
|
| 127 |
const QStringList commands = QStringList() |
| 128 |
<< "show" |
| 129 |
<< "hide" |
| 130 |
<< "toggle" |
| 131 |
<< "menu" |
| 132 |
<< "exit" |
| 133 |
<< "help" |
| 134 |
<< "version" |
| 135 |
<< "clipboard" |
| 136 |
<< "copy" |
| 137 |
<< "paste" |
| 138 |
<< "action" |
| 139 |
<< "add" |
| 140 |
<< "remove"; |
| 141 |
|
| 142 |
for (const auto &command : commands) { |
| 143 |
QCOMPARE( run(Args("help") << command, &stdoutActual, &stderrActual), 0 ); |
| 144 |
QVERIFY2( testStderr(stderrActual), stderrActual ); |
| 145 |
QVERIFY( !stdoutActual.isEmpty() ); |
| 146 |
const QString help = QString::fromUtf8(stdoutActual); |
| 147 |
QVERIFY( help.contains(QRegularExpression("\\b" + QRegularExpression::escape(command) + "\\b")) ); |
| 148 |
} |
| 149 |
|
| 150 |
// Print error on unknown function name. |
| 151 |
m_test->ignoreErrors(QRegularExpression("Error: Command not found!")); |
| 152 |
RUN_EXPECT_ERROR("help" << "xxx", CommandException); |
| 153 |
} |
| 154 |
|
| 155 |
void CoreTests::commandVersion() |
| 156 |
{ |
| 157 |
QByteArray stdoutActual; |
| 158 |
QByteArray stderrActual; |
| 159 |
QCOMPARE( run(Args("version"), &stdoutActual, &stderrActual), 0 ); |
| 160 |
QVERIFY2( testStderr(stderrActual), stderrActual ); |
| 161 |
QVERIFY( !stdoutActual.isEmpty() ); |
| 162 |
|
| 163 |
const QString version = QString::fromUtf8(stdoutActual); |
| 164 |
// Version contains application name and version. |
| 165 |
QVERIFY( version.contains(QRegularExpression("\\bCopyQ\\b.*" + QRegularExpression::escape(versionString))) ); |
| 166 |
// Version contains Qt version. |
| 167 |
QVERIFY( version.contains(QRegularExpression("\\bQt:\\s+\\d")) ); |
| 168 |
|
| 169 |
// Version string format: Major.Minor.Patch[.N-gSHA] |
| 170 |
// 14.0.0 (tagged release) |
| 171 |
// 14.0.0.4-g1175475d (4 commits after tag) |
| 172 |
const QRegularExpression re( |
| 173 |
"^\\d+\\.\\d+\\.\\d+" // Major.Minor.Patch |
| 174 |
"(\\.\\d+-g[0-9a-f]+)?$" // optional .commits-gSHA suffix |
| 175 |
); |
| 176 |
QVERIFY2( |
| 177 |
re.match(versionString).hasMatch(), |
| 178 |
qPrintable(QStringLiteral("versionString '%1' does not match expected format").arg(versionString)) |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
void CoreTests::badCommand() |
| 183 |
{ |
| 184 |
RUN_EXPECT_ERROR_WITH_STDERR("xxx", CommandException, "xxx"); |
| 185 |
RUN_EXPECT_ERROR_WITH_STDERR("tab" << testTab(1) << "yyy", CommandException, "yyy"); |
| 186 |
|
| 187 |
// Bad command shouldn't create new tab. |
| 188 |
QByteArray stdoutActual; |
| 189 |
QByteArray stderrActual; |
| 190 |
QCOMPARE( run(Args("tab"), &stdoutActual, &stderrActual), 0 ); |
| 191 |
QVERIFY2( testStderr(stderrActual), stderrActual ); |
| 192 |
QVERIFY( !QString::fromUtf8(stdoutActual) |
| 193 |
.contains(QRegularExpression("^" + QRegularExpression::escape(testTab(1)) + "$")) ); |
| 194 |
} |
| 195 |
|
| 196 |
void CoreTests::badSessionName() |
| 197 |
{ |
| 198 |
m_test->ignoreErrors(QRegularExpression( |
| 199 |
"ERROR: Session name must contain at most 16 characters" |
| 200 |
"|ERROR.*which can be letters, digits" |
| 201 |
)); |
| 202 |
RUN_EXPECT_ERROR("-s" << "max_16_characters_in_session_name_allowed" << "", CommandBadSyntax); |
| 203 |
RUN_EXPECT_ERROR("-s" << "spaces disallowed" << "", CommandBadSyntax); |
| 204 |
} |
| 205 |
|
| 206 |
void CoreTests::commandSession() |
| 207 |
{ |
| 208 |
RUN("--session" << sessionName << "eval" << "print(1)", "1"); |
| 209 |
} |
| 210 |
|
| 211 |
void CoreTests::commandCatchExceptions() |
| 212 |
{ |
| 213 |
RUN("try { removeTab('MISSING') } catch(e) { print(e) }", |
| 214 |
"Error: Tab with given name doesn't exist!"); |
| 215 |
} |