1 #include "../io/cryptoexception.h"
2 #include "../io/entry.h"
3 #include "../io/passwordfile.h"
5 #include <c++utilities/tests/testutils.h>
7 #include <cppunit/TestFixture.h>
8 #include <cppunit/extensions/HelperMacros.h>
13 using namespace CppUtilities::Literals;
14 using namespace CPPUNIT_NS;
21 CPPUNIT_TEST(testReading);
22 CPPUNIT_TEST(testBasicWriting);
23 CPPUNIT_TEST(testExtendedWriting);
24 CPPUNIT_TEST_SUITE_END();
27 void setUp()
override;
28 void tearDown()
override;
31 void testReading(
const string &context,
const string &testfile1path,
const string &testfile1password,
const string &testfile2,
32 const string &testfile2password,
bool testfile2Mod,
bool extendedHeaderMod);
33 void testBasicWriting();
34 void testExtendedWriting();
52 testReading(
"read", testFilePath(
"testfile1.pwmgr"),
"123456", testFilePath(
"testfile2.pwmgr"),
string(),
false,
false);
56 const string &testfile2password,
bool testfilesMod,
bool extendedHeaderMod)
62 file.
open(PasswordFileOpenFlags::ReadOnly);
64 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, !testfile1password.empty(), file.
isEncryptionUsed());
67 if (!testfile1password.empty()) {
75 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"testfile1"s, rootEntry->
label());
76 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, 4_st, rootEntry->
children().size());
79 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"testaccount1"s, rootEntry->
children()[0]->label());
80 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, EntryType::Account, rootEntry->
children()[0]->type());
82 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"123456"s,
static_cast<AccountEntry *
>(rootEntry->
children()[0])->
fields().at(0).value());
83 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, FieldType::Password,
static_cast<AccountEntry *
>(rootEntry->
children()[0])->
fields().at(0).type());
86 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, FieldType::Normal,
static_cast<AccountEntry *
>(rootEntry->
children()[0])->
fields().at(1).type());
90 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"testaccount2"s, rootEntry->
children()[1]->label());
91 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, EntryType::Account, rootEntry->
children()[1]->type());
95 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"testcategory1"s, rootEntry->
children()[2]->label());
96 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, EntryType::Node, rootEntry->
children()[2]->type());
98 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, 3_st, category->
children().size());
99 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, EntryType::Node, category->
children()[2]->type());
103 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"testaccount3"s, rootEntry->
children()[3]->label());
107 }
else if (extendedHeaderMod) {
108 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"foo"s, file.
extendedHeader());
111 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
""s, file.
extendedHeader());
118 file.
open(PasswordFileOpenFlags::ReadOnly);
120 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, !testfile2password.empty(), file.
isEncryptionUsed());
125 if (extendedHeaderMod) {
126 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
static_cast<std::uint32_t
>(6), file.
version());
129 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
static_cast<std::uint32_t
>(3), file.
version());
132 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"testfile2 - modified"s, rootEntry2->
label());
133 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, 2_st, rootEntry2->
children().size());
134 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"newAccount"s, rootEntry2->
children()[1]->label());
136 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
static_cast<std::uint32_t
>(3), file.
version());
137 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"testfile2"s, rootEntry2->
label());
138 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, 1_st, rootEntry2->
children().size());
140 if (extendedHeaderMod) {
141 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"foo"s, file.
extendedHeader());
145 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
""s, file.
extendedHeader());
155 const string testfile1 = workingCopyPath(
"testfile1.pwmgr");
156 const string testfile2 = workingCopyPath(
"testfile2.pwmgr");
165 file.
save(PasswordFileSaveFlags::Compression);
175 file.
save(PasswordFileSaveFlags::Encryption);
178 testReading(
"basic writing", testfile1,
string(), testfile2,
"654321",
true,
false);
181 testReading(
"basic writing", testfile1 +
".backup",
"123456", testfile2 +
".backup",
string(),
false,
false);
189 const string testfile1 = workingCopyPath(
"testfile1.pwmgr");
190 const string testfile2 = workingCopyPath(
"testfile2.pwmgr");
202 file.
save(PasswordFileSaveFlags::Encryption | PasswordFileSaveFlags::PasswordHashing);
216 file.
save(PasswordFileSaveFlags::Encryption | PasswordFileSaveFlags::PasswordHashing);
219 testReading(
"extended writing", testfile1,
"123456", testfile2,
"654321",
true,
true);
222 testReading(
"extended writing", testfile1 +
".backup",
"123456", testfile2 +
".backup",
string(),
false,
false);