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