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> 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();
53 "read", TestUtilities::testFilePath(
"testfile1.pwmgr"),
"123456", TestUtilities::testFilePath(
"testfile2.pwmgr"),
string(),
false,
false);
57 const string &testfile2password,
bool testfilesMod,
bool extendedHeaderMod)
63 file.
open(PasswordFileOpenFlags::ReadOnly);
65 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, !testfile1password.empty(), file.
isEncryptionUsed());
68 if (!testfile1password.empty()) {
76 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"testfile1"s, rootEntry->
label());
77 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, 4_st, rootEntry->
children().size());
80 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"testaccount1"s, rootEntry->
children()[0]->label());
81 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, EntryType::Account, rootEntry->
children()[0]->type());
82 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"pin"s, static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(0).name());
83 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"123456"s, static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(0).value());
84 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, FieldType::Password, static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(0).type());
86 static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(0).tiedAccount() ==
static_cast<AccountEntry *
>(rootEntry->
children()[0]));
87 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, FieldType::Normal, static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(1).type());
88 CPPUNIT_ASSERT_THROW_MESSAGE(context, static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(2), out_of_range);
91 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"testaccount2"s, rootEntry->
children()[1]->label());
92 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, EntryType::Account, rootEntry->
children()[1]->type());
93 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, 0_st, static_cast<AccountEntry *>(rootEntry->
children()[1])->fields().size());
96 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"testcategory1"s, rootEntry->
children()[2]->label());
97 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, EntryType::Node, rootEntry->
children()[2]->type());
99 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, 3_st, category->children().size());
100 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, EntryType::Node, category->children()[2]->type());
101 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, 2_st, static_cast<NodeEntry *>(category->children()[2])->children().size());
104 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"testaccount3"s, rootEntry->
children()[3]->label());
108 }
else if (extendedHeaderMod) {
109 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"foo"s, file.
extendedHeader());
112 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
""s, file.
extendedHeader());
119 file.
open(PasswordFileOpenFlags::ReadOnly);
121 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, !testfile2password.empty(), file.
isEncryptionUsed());
126 if (extendedHeaderMod) {
127 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, static_cast<uint32>(6), file.
version());
130 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, static_cast<uint32>(3), file.
version());
133 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"testfile2 - modified"s, rootEntry2->
label());
134 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, 2_st, rootEntry2->
children().size());
135 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"newAccount"s, rootEntry2->
children()[1]->label());
137 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, static_cast<uint32>(3), file.
version());
138 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"testfile2"s, rootEntry2->
label());
139 CPPUNIT_ASSERT_EQUAL_MESSAGE(context, 1_st, rootEntry2->
children().size());
141 if (extendedHeaderMod) {
142 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
"foo"s, file.
extendedHeader());
146 CPPUNIT_ASSERT_EQUAL_MESSAGE(context,
""s, file.
extendedHeader());
156 const string testfile1 = TestUtilities::workingCopyPath(
"testfile1.pwmgr");
157 const string testfile2 = TestUtilities::workingCopyPath(
"testfile2.pwmgr");
166 file.
save(PasswordFileSaveFlags::Compression);
176 file.
save(PasswordFileSaveFlags::Encryption);
179 testReading(
"basic writing", testfile1,
string(), testfile2,
"654321",
true,
false);
182 testReading(
"basic writing", testfile1 +
".backup",
"123456", testfile2 +
".backup",
string(),
false,
false);
190 const string testfile1 = TestUtilities::workingCopyPath(
"testfile1.pwmgr");
191 const string testfile2 = TestUtilities::workingCopyPath(
"testfile2.pwmgr");
203 file.
save(PasswordFileSaveFlags::Encryption | PasswordFileSaveFlags::PasswordHashing);
217 file.
save(PasswordFileSaveFlags::Encryption | PasswordFileSaveFlags::PasswordHashing);
220 testReading(
"extended writing", testfile1,
"123456", testfile2,
"654321",
true,
true);
223 testReading(
"extended writing", testfile1 +
".backup",
"123456", testfile2 +
".backup",
string(),
false,
false);
The NodeEntry class acts as parent for other entries.
The PasswordFile class holds account information in the form of Entry and Field instances and provide...
const std::string & label() const
Returns the label.
void setLabel(const std::string &label)
Sets the label.
void load()
Reads the contents of the file.
std::string & encryptedExtendedHeader()
Returns the encrypted extended header.
std::string PASSWORD_FILE_EXPORT flagsToString(PasswordFileOpenFlags flags)
Returns a comma-separated string for the specified flags.
The PasswordFileTests class tests the Io::PasswordFile class.
void doBackup()
Creates a backup of the file.
Contains all IO related classes.
const NodeEntry * rootEntry() const
Returns the root entry if present or nullptr otherwise.
void open(PasswordFileOpenFlags options=PasswordFileOpenFlags::Default)
Opens the file.
The exception that is thrown when a parsing error occurs.
void testExtendedWriting()
Tests writing (and reading again) using extended features.
bool isEncryptionUsed()
Returns an indication whether encryption is used and the file is open; returns always false otherwise...
void setPassword(const std::string &password)
Sets the current password.
void setPath(const std::string &value)
Sets the current file path.
const std::vector< Entry * > & children() const
std::string & extendedHeader()
Returns the extended header.
void testBasicWriting()
Tests writing (and reading again) using basic features.
The exception that is thrown when an encryption/decryption error occurs.
uint32 version() const
Returns the file version used the last time when saving the file (the version of the file as it is on...
void testReading()
Tests reading the testfiles testfile{1,2}.pwmgr.
void save(PasswordFileSaveFlags options=PasswordFileSaveFlags::Default)
Writes the current root entry to the file under path() replacing its previous contents.
CPPUNIT_TEST_SUITE_REGISTRATION(PasswordFileTests)
PasswordFileSaveFlags saveOptions() const
Returns the save options used the last time when saving the file.