TestTools.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (C) 2024 KeePassXC Team <team@keepassxc.org>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 or (at your option)
  7. * version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "TestTools.h"
  18. #include "core/Clock.h"
  19. #include <QRegularExpression>
  20. #include <QTest>
  21. #include <QUuid>
  22. QTEST_GUILESS_MAIN(TestTools)
  23. namespace
  24. {
  25. QString createDecimal(QString wholes, QString fractions, QString unit)
  26. {
  27. return wholes + QLocale().decimalPoint() + fractions + " " + unit;
  28. }
  29. } // namespace
  30. void TestTools::testHumanReadableFileSize()
  31. {
  32. constexpr auto kibibyte = 1024u;
  33. using namespace Tools;
  34. QCOMPARE(QString("1 B"), humanReadableFileSize(1));
  35. QCOMPARE(createDecimal("1", "00", "KiB"), humanReadableFileSize(kibibyte));
  36. QCOMPARE(createDecimal("1", "00", "MiB"), humanReadableFileSize(kibibyte * kibibyte));
  37. QCOMPARE(createDecimal("1", "00", "GiB"), humanReadableFileSize(kibibyte * kibibyte * kibibyte));
  38. QCOMPARE(QString("100 B"), humanReadableFileSize(100, 0));
  39. QCOMPARE(createDecimal("1", "10", "KiB"), humanReadableFileSize(kibibyte + 100));
  40. QCOMPARE(createDecimal("1", "001", "KiB"), humanReadableFileSize(kibibyte + 1, 3));
  41. QCOMPARE(createDecimal("15", "00", "KiB"), humanReadableFileSize(kibibyte * 15));
  42. }
  43. void TestTools::testIsHex()
  44. {
  45. QVERIFY(Tools::isHex("0123456789abcdefABCDEF"));
  46. QVERIFY(!Tools::isHex(QByteArray("0xnothex")));
  47. }
  48. void TestTools::testIsBase64()
  49. {
  50. QVERIFY(Tools::isBase64(QByteArray("1234")));
  51. QVERIFY(Tools::isBase64(QByteArray("123=")));
  52. QVERIFY(Tools::isBase64(QByteArray("12==")));
  53. QVERIFY(Tools::isBase64(QByteArray("abcd9876MN==")));
  54. QVERIFY(Tools::isBase64(QByteArray("abcd9876DEFGhijkMNO=")));
  55. QVERIFY(Tools::isBase64(QByteArray("abcd987/DEFGh+jk/NO=")));
  56. QVERIFY(!Tools::isBase64(QByteArray("abcd123==")));
  57. QVERIFY(!Tools::isBase64(QByteArray("abc_")));
  58. QVERIFY(!Tools::isBase64(QByteArray("123")));
  59. }
  60. void TestTools::testIsAsciiString()
  61. {
  62. QVERIFY(Tools::isAsciiString("abcd9876DEFGhijkMNO"));
  63. QVERIFY(Tools::isAsciiString("-!&5a?`~"));
  64. QVERIFY(!Tools::isAsciiString("Štest"));
  65. QVERIFY(!Tools::isAsciiString("Ãß"));
  66. }
  67. void TestTools::testEnvSubstitute()
  68. {
  69. QProcessEnvironment environment;
  70. #if defined(Q_OS_WIN)
  71. environment.insert("HOMEDRIVE", "C:");
  72. environment.insert("HOMEPATH", "\\Users\\User");
  73. environment.insert("USERPROFILE", "C:\\Users\\User");
  74. QCOMPARE(Tools::envSubstitute("%HOMEDRIVE%%HOMEPATH%\\.ssh\\id_rsa", environment),
  75. QString("C:\\Users\\User\\.ssh\\id_rsa"));
  76. QCOMPARE(Tools::envSubstitute("start%EMPTY%%EMPTY%%%HOMEDRIVE%%end", environment), QString("start%C:%end"));
  77. QCOMPARE(Tools::envSubstitute("%USERPROFILE%\\.ssh\\id_rsa", environment),
  78. QString("C:\\Users\\User\\.ssh\\id_rsa"));
  79. QCOMPARE(Tools::envSubstitute("~\\.ssh\\id_rsa", environment), QString("C:\\Users\\User\\.ssh\\id_rsa"));
  80. #else
  81. environment.insert("HOME", QString("/home/user"));
  82. environment.insert("USER", QString("user"));
  83. QCOMPARE(Tools::envSubstitute("~/.ssh/id_rsa", environment), QString("/home/user/.ssh/id_rsa"));
  84. QCOMPARE(Tools::envSubstitute("$HOME/.ssh/id_rsa", environment), QString("/home/user/.ssh/id_rsa"));
  85. QCOMPARE(Tools::envSubstitute("start/$EMPTY$$EMPTY$HOME/end", environment), QString("start/$/home/user/end"));
  86. #endif
  87. }
  88. void TestTools::testValidUuid()
  89. {
  90. auto validUuid = Tools::uuidToHex(QUuid::createUuid());
  91. auto nonRfc4122Uuid = "1234567890abcdef1234567890abcdef";
  92. auto emptyUuid = QString();
  93. auto shortUuid = validUuid.left(10);
  94. auto longUuid = validUuid + "baddata";
  95. auto nonHexUuid = Tools::uuidToHex(QUuid::createUuid()).replace(0, 1, 'p');
  96. QVERIFY(Tools::isValidUuid(validUuid));
  97. /* Before https://github.com/keepassxreboot/keepassxc/pull/1770/, entry
  98. * UUIDs are simply random 16-byte strings. Such older entries should be
  99. * accepted as well. */
  100. QVERIFY(Tools::isValidUuid(nonRfc4122Uuid));
  101. QVERIFY(!Tools::isValidUuid(emptyUuid));
  102. QVERIFY(!Tools::isValidUuid(shortUuid));
  103. QVERIFY(!Tools::isValidUuid(longUuid));
  104. QVERIFY(!Tools::isValidUuid(nonHexUuid));
  105. }
  106. void TestTools::testBackupFilePatternSubstitution_data()
  107. {
  108. QTest::addColumn<QString>("pattern");
  109. QTest::addColumn<QString>("dbFilePath");
  110. QTest::addColumn<QString>("expectedSubstitution");
  111. static const auto DEFAULT_DB_FILE_NAME = QStringLiteral("KeePassXC");
  112. static const auto DEFAULT_DB_FILE_PATH = QStringLiteral("/tmp/") + DEFAULT_DB_FILE_NAME + QStringLiteral(".kdbx");
  113. static const auto NOW = Clock::currentDateTime();
  114. auto DEFAULT_FORMATTED_TIME = NOW.toString("dd_MM_yyyy_hh-mm-ss");
  115. QTest::newRow("Null pattern") << QString() << DEFAULT_DB_FILE_PATH << QString();
  116. QTest::newRow("Empty pattern") << QString("") << DEFAULT_DB_FILE_PATH << QString("");
  117. QTest::newRow("Null database path") << "valid_pattern" << QString() << QString();
  118. QTest::newRow("Empty database path") << "valid_pattern" << QString("") << QString();
  119. QTest::newRow("Unclosed/invalid pattern") << "{DB_FILENAME" << DEFAULT_DB_FILE_PATH << "{DB_FILENAME";
  120. QTest::newRow("Unknown pattern") << "{NO_MATCH}" << DEFAULT_DB_FILE_PATH << "{NO_MATCH}";
  121. QTest::newRow("Do not replace escaped patterns (filename)")
  122. << "\\{DB_FILENAME\\}" << DEFAULT_DB_FILE_PATH << "{DB_FILENAME}";
  123. QTest::newRow("Do not replace escaped patterns (time)")
  124. << "\\{TIME:dd.MM.yyyy\\}" << DEFAULT_DB_FILE_PATH << "{TIME:dd.MM.yyyy}";
  125. QTest::newRow("Multiple patterns should be replaced")
  126. << "{DB_FILENAME} {TIME} {DB_FILENAME}" << DEFAULT_DB_FILE_PATH
  127. << DEFAULT_DB_FILE_NAME + QStringLiteral(" ") + DEFAULT_FORMATTED_TIME + QStringLiteral(" ")
  128. + DEFAULT_DB_FILE_NAME;
  129. QTest::newRow("Default time pattern") << "{TIME}" << DEFAULT_DB_FILE_PATH << DEFAULT_FORMATTED_TIME;
  130. QTest::newRow("Default time pattern (empty formatter)")
  131. << "{TIME:}" << DEFAULT_DB_FILE_PATH << DEFAULT_FORMATTED_TIME;
  132. QTest::newRow("Custom time pattern") << "{TIME:dd-ss}" << DEFAULT_DB_FILE_PATH << NOW.toString("dd-ss");
  133. QTest::newRow("Time pattern twice") << "{TIME:yy} {TIME}" << DEFAULT_DB_FILE_PATH
  134. << NOW.toString("yy") + QStringLiteral(" ") + DEFAULT_FORMATTED_TIME;
  135. QTest::newRow("Complex custom time pattern")
  136. << "./{TIME:yy}/{DB_FILENAME}_{TIME:yyyyMMdd_HHmmss}.old.kdbx" << DEFAULT_DB_FILE_PATH
  137. << QStringLiteral("./") + NOW.toString("yy") + QStringLiteral("/") + DEFAULT_DB_FILE_NAME + QStringLiteral("_")
  138. + NOW.toString("yyyyMMdd_HHmmss") + QStringLiteral(".old.kdbx");
  139. QTest::newRow("Invalid custom time pattern") << "{TIME:dd/-ss}" << DEFAULT_DB_FILE_PATH << NOW.toString("dd/-ss");
  140. QTest::newRow("Recursive substitution") << "{TIME:'{TIME}'}" << DEFAULT_DB_FILE_PATH << DEFAULT_FORMATTED_TIME;
  141. QTest::newRow("{DB_FILENAME} substitution")
  142. << "some {DB_FILENAME} thing" << DEFAULT_DB_FILE_PATH
  143. << QStringLiteral("some ") + DEFAULT_DB_FILE_NAME + QStringLiteral(" thing");
  144. QTest::newRow("{DB_FILENAME} substitution with multiple extensions")
  145. << "some {DB_FILENAME} thing" << "/tmp/KeePassXC.kdbx.ext" << "some KeePassXC.kdbx thing";
  146. // Not relevant right now, added test anyway
  147. QTest::newRow("There should be no substitution loops")
  148. << "{DB_FILENAME}" << "{TIME:'{DB_FILENAME}'}.ext" << "{TIME:'{DB_FILENAME}'}";
  149. }
  150. void TestTools::testBackupFilePatternSubstitution()
  151. {
  152. QFETCH(QString, pattern);
  153. QFETCH(QString, dbFilePath);
  154. QFETCH(QString, expectedSubstitution);
  155. QCOMPARE(Tools::substituteBackupFilePath(pattern, dbFilePath), expectedSubstitution);
  156. }
  157. void TestTools::testEscapeRegex_data()
  158. {
  159. QTest::addColumn<QString>("input");
  160. QTest::addColumn<QString>("expected");
  161. QString all_regular_characters = "0123456789";
  162. for (char c = 'a'; c != 'z'; ++c) {
  163. all_regular_characters += QChar::fromLatin1(c);
  164. }
  165. for (char c = 'A'; c != 'Z'; ++c) {
  166. all_regular_characters += QChar::fromLatin1(c);
  167. }
  168. QTest::newRow("Regular characters should not be escaped") << all_regular_characters << all_regular_characters;
  169. QTest::newRow("Special characters should be escaped")
  170. << R"(.^$*+-?()[]{}|\)" << R"(\.\^\$\*\+\-\?\(\)\[\]\{\}\|\\)";
  171. QTest::newRow("Null character") << QString::fromLatin1("ab\0c", 4) << "ab\\0c";
  172. }
  173. void TestTools::testEscapeRegex()
  174. {
  175. QFETCH(QString, input);
  176. QFETCH(QString, expected);
  177. auto actual = Tools::escapeRegex(input);
  178. QCOMPARE(actual, expected);
  179. }
  180. void TestTools::testConvertToRegex()
  181. {
  182. QFETCH(QString, input);
  183. QFETCH(int, options);
  184. QFETCH(QString, expected);
  185. auto regex = Tools::convertToRegex(input, options).pattern();
  186. QCOMPARE(regex, expected);
  187. }
  188. void TestTools::testConvertToRegex_data()
  189. {
  190. const QString input = R"(te|st*t?[5]^(test);',.)";
  191. QTest::addColumn<QString>("input");
  192. QTest::addColumn<int>("options");
  193. QTest::addColumn<QString>("expected");
  194. QTest::newRow("No Options") << input << static_cast<int>(Tools::RegexConvertOpts::DEFAULT)
  195. << QString(R"(te|st*t?[5]^(test);',.)");
  196. // Escape regex
  197. QTest::newRow("Escape Regex") << input << static_cast<int>(Tools::RegexConvertOpts::ESCAPE_REGEX)
  198. << Tools::escapeRegex(input);
  199. QTest::newRow("Escape Regex and exact match")
  200. << input << static_cast<int>(Tools::RegexConvertOpts::ESCAPE_REGEX | Tools::RegexConvertOpts::EXACT_MATCH)
  201. << "^(?:" + Tools::escapeRegex(input) + ")$";
  202. // Exact match does not escape the pattern
  203. QTest::newRow("Exact Match") << input << static_cast<int>(Tools::RegexConvertOpts::EXACT_MATCH)
  204. << QString(R"(^(?:te|st*t?[5]^(test);',.)$)");
  205. // Exact match with improper regex
  206. QTest::newRow("Exact Match") << ")av(" << static_cast<int>(Tools::RegexConvertOpts::EXACT_MATCH)
  207. << QString(R"(^(?:)av()$)");
  208. QTest::newRow("Exact Match & Wildcard")
  209. << input << static_cast<int>(Tools::RegexConvertOpts::EXACT_MATCH | Tools::RegexConvertOpts::WILDCARD_ALL)
  210. << QString(R"(^(?:te|st.*t.\[5\]\^\(test\)\;\'\,\.)$)");
  211. QTest::newRow("Wildcard Single Match") << input << static_cast<int>(Tools::RegexConvertOpts::WILDCARD_SINGLE_MATCH)
  212. << QString(R"(te\|st\*t.\[5\]\^\(test\)\;\'\,\.)");
  213. QTest::newRow("Wildcard OR") << input << static_cast<int>(Tools::RegexConvertOpts::WILDCARD_LOGICAL_OR)
  214. << QString(R"(te|st\*t\?\[5\]\^\(test\)\;\'\,\.)");
  215. QTest::newRow("Wildcard Unlimited Match")
  216. << input << static_cast<int>(Tools::RegexConvertOpts::WILDCARD_UNLIMITED_MATCH)
  217. << QString(R"(te\|st.*t\?\[5\]\^\(test\)\;\'\,\.)");
  218. }
  219. void TestTools::testArrayContainsValues()
  220. {
  221. const auto values = QStringList() << "first" << "second" << "third";
  222. // One missing
  223. const auto result1 =
  224. Tools::getMissingValuesFromList<QString>(values, QStringList() << "first" << "second" << "none");
  225. QCOMPARE(result1.length(), 1);
  226. QCOMPARE(result1.first(), QString("none"));
  227. // All found
  228. const auto result2 =
  229. Tools::getMissingValuesFromList<QString>(values, QStringList() << "first" << "second" << "third");
  230. QCOMPARE(result2.length(), 0);
  231. // None are found
  232. const auto numberValues = QList<int>({1, 2, 3, 4, 5});
  233. const auto result3 = Tools::getMissingValuesFromList<int>(numberValues, QList<int>({6, 7, 8}));
  234. QCOMPARE(result3.length(), 3);
  235. }