1 // Testing whether or not files generated on the author's machine can be read
2 // on other machines as well...
3
4 #ifdef HAVE_CONFIG_H
5 #include "config.h"
6 #endif
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include <cppunit/CompilerOutputter.h>
13 #include <cppunit/TestCaller.h>
14 #include <cppunit/TestFixture.h>
15 #include <cppunit/extensions/TestFactoryRegistry.h>
16 #include <cppunit/ui/text/TestRunner.h>
17
18 #include <unistd.h>
19 #include <cstring>
20 #include <list>
21
22 #include "blowfishfactory.hh"
23 #include "file.h"
24 #include "securearray.hh"
25 #include "testpaths.h"
26
27 constexpr auto TEST_PASSWORD{"test1"};
28
29 constexpr auto ROUNDS{200};
30
31 constexpr auto NAME{"Test name"};
32 constexpr auto HOST{"Test host"};
33 constexpr auto USERNAME{"Test username"};
34 constexpr auto PASSWORD{"Test password"};
35 constexpr auto COMMENT{"Test comment"};
36
makeName(int number)37 inline std::string makeName(int number) {
38 std::string name{NAME};
39 name += " " + std::to_string(number);
40
41 return name;
42 }
43
makePasswordRecord(int number)44 inline yapet::PasswordRecord makePasswordRecord(int number) {
45 yapet::PasswordRecord passwordRecord{};
46
47 passwordRecord.name(makeName(number).c_str());
48
49 std::string host = HOST;
50 host += " " + std::to_string(number);
51 passwordRecord.host(host.c_str());
52
53 std::string username = USERNAME;
54 username += " " + std::to_string(number);
55 passwordRecord.username(username.c_str());
56
57 std::string password = PASSWORD;
58 password += " " + std::to_string(number);
59 passwordRecord.password(password.c_str());
60
61 std::string comment = COMMENT;
62 comment += " " + std::to_string(number);
63 passwordRecord.comment(comment.c_str());
64
65 return passwordRecord;
66 }
67
comparePasswordRecords(const yapet::PasswordRecord & actual,const yapet::PasswordRecord & expected)68 inline void comparePasswordRecords(const yapet::PasswordRecord &actual,
69 const yapet::PasswordRecord &expected) {
70 CPPUNIT_ASSERT(
71 std::strcmp(reinterpret_cast<const char *>(actual.name()),
72 reinterpret_cast<const char *>(expected.name())) == 0);
73 CPPUNIT_ASSERT(
74 std::strcmp(reinterpret_cast<const char *>(actual.host()),
75 reinterpret_cast<const char *>(expected.host())) == 0);
76 CPPUNIT_ASSERT(
77 std::strcmp(reinterpret_cast<const char *>(actual.username()),
78 reinterpret_cast<const char *>(expected.username())) == 0);
79 CPPUNIT_ASSERT(
80 std::strcmp(reinterpret_cast<const char *>(actual.password()),
81 reinterpret_cast<const char *>(expected.password())) == 0);
82 CPPUNIT_ASSERT(
83 std::strcmp(reinterpret_cast<const char *>(actual.comment()),
84 reinterpret_cast<const char *>(expected.comment())) == 0);
85 }
86
testFile(const char * filename)87 inline void testFile(const char *filename) {
88 auto password{yapet::toSecureArray(TEST_PASSWORD)};
89 std::shared_ptr<yapet::AbstractCryptoFactory> factory{
90 new yapet::BlowfishFactory{password, yapet::MetaData{}}};
91
92 auto crypto{factory->crypto()};
93
94 YAPET::File file{factory, filename, false, false};
95
96 auto passwords{file.read()};
97 CPPUNIT_ASSERT(passwords.size() == ROUNDS);
98
99 std::list<yapet::PasswordListItem>::iterator it = passwords.begin();
100 for (int i = 0; it != passwords.end(); i++, it++) {
101 auto name{makeName(i)};
102 CPPUNIT_ASSERT(std::strcmp(name.c_str(), reinterpret_cast<const char *>(
103 it->name())) == 0);
104
105 auto serializedPasswordRecord{crypto->decrypt(it->encryptedRecord())};
106 yapet::PasswordRecord actual{serializedPasswordRecord};
107
108 yapet::PasswordRecord expected{makePasswordRecord(i)};
109 comparePasswordRecords(actual, expected);
110 }
111 }
112
113 class ForeignTest : public CppUnit::TestFixture {
114 public:
suite()115 static CppUnit::TestSuite *suite() {
116 CppUnit::TestSuite *suiteOfTests = new CppUnit::TestSuite("Blowfish");
117
118 suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
119 "32bit little endian pre 0.6", &ForeignTest::test32lePre06));
120 suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
121 "32bit big endian pre 0.6", &ForeignTest::test32bePre06));
122
123 suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
124 "64bit little endian pre 0.6", &ForeignTest::test64lePre06));
125 suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
126 "64bit big endian pre 0.6", &ForeignTest::test64bePre06));
127
128 suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
129 "32bit little endian 0.6", &ForeignTest::test32le06));
130 suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
131 "32bit big endian 0.6", &ForeignTest::test32be06));
132
133 suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
134 "64bit little endian 0.6", &ForeignTest::test64le06));
135 suiteOfTests->addTest(new CppUnit::TestCaller<ForeignTest>(
136 "64bit big endian 0.6", &ForeignTest::test64be06));
137
138 return suiteOfTests;
139 }
140
test32lePre06()141 void test32lePre06() { testFile(BUILDDIR "/f32le0.5.pet"); }
142
test32bePre06()143 void test32bePre06() { testFile(BUILDDIR "/f32be0.5.pet"); }
144
test64lePre06()145 void test64lePre06() { testFile(BUILDDIR "/f64le0.5.pet"); }
146
test64bePre06()147 void test64bePre06() { testFile(BUILDDIR "/f64be0.5.pet"); }
148
test32le06()149 void test32le06() { testFile(BUILDDIR "/f32le0.6.pet"); }
150
test32be06()151 void test32be06() { testFile(BUILDDIR "/f32be0.6.pet"); }
152
test64le06()153 void test64le06() { testFile(BUILDDIR "/f64le0.6.pet"); }
154
test64be06()155 void test64be06() { testFile(BUILDDIR "/f64be0.6.pet"); }
156 };
157
main()158 int main() {
159 CppUnit::TextUi::TestRunner runner;
160 runner.addTest(ForeignTest ::suite());
161 return runner.run() ? 0 : 1;
162 }
163