TestTools.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (C) 2017 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 <QLocale>
  19. #include <QTest>
  20. QTEST_GUILESS_MAIN(TestTools)
  21. namespace
  22. {
  23. QString createDecimal(QString wholes, QString fractions, QString unit)
  24. {
  25. return wholes + QLocale().decimalPoint() + fractions + " " + unit;
  26. }
  27. } // namespace
  28. void TestTools::testHumanReadableFileSize()
  29. {
  30. constexpr auto kibibyte = 1024u;
  31. using namespace Tools;
  32. QCOMPARE(createDecimal("1", "00", "B"), humanReadableFileSize(1));
  33. QCOMPARE(createDecimal("1", "00", "KiB"), humanReadableFileSize(kibibyte));
  34. QCOMPARE(createDecimal("1", "00", "MiB"), humanReadableFileSize(kibibyte * kibibyte));
  35. QCOMPARE(createDecimal("1", "00", "GiB"), humanReadableFileSize(kibibyte * kibibyte * kibibyte));
  36. QCOMPARE(QString("100 B"), humanReadableFileSize(100, 0));
  37. QCOMPARE(createDecimal("1", "10", "KiB"), humanReadableFileSize(kibibyte + 100));
  38. QCOMPARE(createDecimal("1", "001", "KiB"), humanReadableFileSize(kibibyte + 1, 3));
  39. QCOMPARE(createDecimal("15", "00", "KiB"), humanReadableFileSize(kibibyte * 15));
  40. }
  41. void TestTools::testIsHex()
  42. {
  43. QVERIFY(Tools::isHex("0123456789abcdefABCDEF"));
  44. QVERIFY(not Tools::isHex(QByteArray("0xnothex")));
  45. }
  46. void TestTools::testIsBase64()
  47. {
  48. QVERIFY(Tools::isBase64(QByteArray("1234")));
  49. QVERIFY(Tools::isBase64(QByteArray("123=")));
  50. QVERIFY(Tools::isBase64(QByteArray("12==")));
  51. QVERIFY(Tools::isBase64(QByteArray("abcd9876MN==")));
  52. QVERIFY(Tools::isBase64(QByteArray("abcd9876DEFGhijkMNO=")));
  53. QVERIFY(Tools::isBase64(QByteArray("abcd987/DEFGh+jk/NO=")));
  54. QVERIFY(not Tools::isBase64(QByteArray("abcd123==")));
  55. QVERIFY(not Tools::isBase64(QByteArray("abc_")));
  56. QVERIFY(not Tools::isBase64(QByteArray("123")));
  57. }
  58. void TestTools::testEnvSubstitute()
  59. {
  60. QProcessEnvironment environment;
  61. #if defined(Q_OS_WIN)
  62. environment.insert("HOMEDRIVE", "C:");
  63. environment.insert("HOMEPATH", "\\Users\\User");
  64. environment.insert("USERPROFILE", "C:\\Users\\User");
  65. QCOMPARE(Tools::envSubstitute("%HOMEDRIVE%%HOMEPATH%\\.ssh\\id_rsa", environment),
  66. QString("C:\\Users\\User\\.ssh\\id_rsa"));
  67. QCOMPARE(Tools::envSubstitute("start%EMPTY%%EMPTY%%%HOMEDRIVE%%end", environment), QString("start%C:%end"));
  68. QCOMPARE(Tools::envSubstitute("%USERPROFILE%\\.ssh\\id_rsa", environment),
  69. QString("C:\\Users\\User\\.ssh\\id_rsa"));
  70. QCOMPARE(Tools::envSubstitute("~\\.ssh\\id_rsa", environment), QString("C:\\Users\\User\\.ssh\\id_rsa"));
  71. #else
  72. environment.insert("HOME", QString("/home/user"));
  73. environment.insert("USER", QString("user"));
  74. QCOMPARE(Tools::envSubstitute("~/.ssh/id_rsa", environment), QString("/home/user/.ssh/id_rsa"));
  75. QCOMPARE(Tools::envSubstitute("$HOME/.ssh/id_rsa", environment), QString("/home/user/.ssh/id_rsa"));
  76. QCOMPARE(Tools::envSubstitute("start/$EMPTY$$EMPTY$HOME/end", environment), QString("start/$/home/user/end"));
  77. #endif
  78. }