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);
23 CPPUNIT_TEST(testWriting);
25 CPPUNIT_TEST_SUITE_END();
33 const string &testfile1path,
const string &testfile1password,
const string &testfile2,
const string &testfile2password,
bool testfile2Mod);
54 testReading(TestUtilities::testFilePath(
"testfile1.pwmgr"),
"123456", TestUtilities::testFilePath(
"testfile2.pwmgr"),
string(),
false);
58 const string &testfile1path,
const string &testfile1password,
const string &testfile2,
const string &testfile2password,
bool testfile2Mod)
69 if (!testfile1password.empty()) {
77 CPPUNIT_ASSERT_EQUAL(
"testfile1"s, rootEntry->
label());
78 CPPUNIT_ASSERT_EQUAL(4_st, rootEntry->
children().size());
81 CPPUNIT_ASSERT_EQUAL(
"testaccount1"s, rootEntry->
children()[0]->label());
82 CPPUNIT_ASSERT(rootEntry->
children()[0]->type() == EntryType::Account);
83 CPPUNIT_ASSERT_EQUAL(
"pin"s, static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(0).name());
84 CPPUNIT_ASSERT_EQUAL(
"123456"s, static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(0).value());
85 CPPUNIT_ASSERT(static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(0).type() == FieldType::Password);
87 static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(0).tiedAccount() ==
static_cast<AccountEntry *
>(rootEntry->
children()[0]));
88 CPPUNIT_ASSERT(static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(1).type() == FieldType::Normal);
89 CPPUNIT_ASSERT_THROW(static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(2), out_of_range);
92 CPPUNIT_ASSERT_EQUAL(
"testaccount2"s, rootEntry->
children()[1]->label());
93 CPPUNIT_ASSERT(rootEntry->
children()[1]->type() == EntryType::Account);
94 CPPUNIT_ASSERT(static_cast<AccountEntry *>(rootEntry->
children()[1])->fields().empty());
97 CPPUNIT_ASSERT_EQUAL(
"testcategory1"s, rootEntry->
children()[2]->label());
98 CPPUNIT_ASSERT(rootEntry->
children()[2]->type() == EntryType::Node);
100 CPPUNIT_ASSERT(category->children().size() == 3);
101 CPPUNIT_ASSERT(category->children()[2]->type() == EntryType::Node);
102 CPPUNIT_ASSERT(static_cast<NodeEntry *>(category->children()[2])->children().size() == 2);
105 CPPUNIT_ASSERT_EQUAL(
"testaccount3"s, rootEntry->
children()[3]->label());
116 CPPUNIT_ASSERT(rootEntry->
label() ==
"testfile2 - modified");
117 CPPUNIT_ASSERT(rootEntry->
children().size() == 2);
118 CPPUNIT_ASSERT(rootEntry->
children()[1]->label() ==
"newAccount");
120 CPPUNIT_ASSERT(rootEntry->
label() ==
"testfile2");
121 CPPUNIT_ASSERT(rootEntry->
children().size() == 1);
129 void PasswordFileTests::testWriting()
131 string testfile1 = TestUtilities::workingCopyPath(
"testfile1.pwmgr");
132 string testfile2 = TestUtilities::workingCopyPath(
"testfile2.pwmgr");
141 file.
save(
false,
true);
151 file.
save(
true,
false);
154 testReading(testfile1,
string(), testfile2,
"654321",
true);
157 testReading(testfile1 +
".backup",
"123456", testfile2 +
".backup",
string(),
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.
void open(bool readOnly=false)
Opens the file.
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.
The exception that is thrown when a parsing error occurs.
bool isEncryptionUsed()
Returns an indication whether encryption is used if the file is open; returns always false otherwise...
void setPath(const std::string &value)
Sets the current file path.
const std::vector< Entry * > & children() const
The exception that is thrown when an encryption/decryption error occurs.
void setPassword(const std::string &value)
Sets the current password.
void testReading()
Tests reading the testfiles testfile{1,2}.pwmgr.
void save(bool useEncryption=true, bool useCompression=true)
Writes the current root entry to the file.
CPPUNIT_TEST_SUITE_REGISTRATION(PasswordFileTests)