1 #include "../io/passwordfile.h" 2 #include "../io/cryptoexception.h" 3 #include "../io/entry.h" 5 #include <c++utilities/tests/testutils.h> 7 #include <cppunit/extensions/HelperMacros.h> 8 #include <cppunit/TestFixture.h> 21 CPPUNIT_TEST(testReading);
23 CPPUNIT_TEST(testWriting);
25 CPPUNIT_TEST_SUITE_END();
32 void testReading(
const string &testfile1path,
const string &testfile1password,
const string &testfile2,
const string &testfile2password,
bool testfile2Mod);
51 testReading(TestUtilities::testFilePath(
"testfile1.pwmgr"),
"123456",
52 TestUtilities::testFilePath(
"testfile2.pwmgr"),
string(),
false);
55 void PasswordFileTests::testReading(
const string &testfile1path,
const string &testfile1password,
const string &testfile2,
const string &testfile2password,
bool testfile2Mod)
66 if(!testfile1password.empty()) {
74 CPPUNIT_ASSERT(rootEntry->
label() ==
"testfile1");
75 CPPUNIT_ASSERT(rootEntry->
children().size() == 4);
78 CPPUNIT_ASSERT(rootEntry->
children()[0]->label() ==
"testaccount1");
79 CPPUNIT_ASSERT(rootEntry->
children()[0]->type() == EntryType::Account);
80 CPPUNIT_ASSERT(static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(0).name() ==
"pin");
81 CPPUNIT_ASSERT(static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(0).value() ==
"123456");
82 CPPUNIT_ASSERT(static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(0).type() == FieldType::Password);
83 CPPUNIT_ASSERT(static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(0).tiedAccount() ==
static_cast<AccountEntry *
>(rootEntry->
children()[0]));
84 CPPUNIT_ASSERT(static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(1).type() == FieldType::Normal);
85 CPPUNIT_ASSERT_THROW(static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(2), out_of_range);
88 CPPUNIT_ASSERT(rootEntry->
children()[1]->label() ==
"testaccount2");
89 CPPUNIT_ASSERT(rootEntry->
children()[1]->type() == EntryType::Account);
90 CPPUNIT_ASSERT(static_cast<AccountEntry *>(rootEntry->
children()[1])->fields().empty());
93 CPPUNIT_ASSERT(rootEntry->
children()[2]->label() ==
"testcategory1");
94 CPPUNIT_ASSERT(rootEntry->
children()[2]->type() == EntryType::Node);
96 CPPUNIT_ASSERT(category->children().size() == 3);
97 CPPUNIT_ASSERT(category->children()[2]->type() == EntryType::Node);
98 CPPUNIT_ASSERT(static_cast<NodeEntry *>(category->children()[2])->children().size() == 2);
101 CPPUNIT_ASSERT(rootEntry->
children()[3]->label() ==
"testaccount3");
112 CPPUNIT_ASSERT(rootEntry->
label() ==
"testfile2 - modified");
113 CPPUNIT_ASSERT(rootEntry->
children().size() == 2);
114 CPPUNIT_ASSERT(rootEntry->
children()[1]->label() ==
"newAccount");
116 CPPUNIT_ASSERT(rootEntry->
label() ==
"testfile2");
117 CPPUNIT_ASSERT(rootEntry->
children().size() == 1);
125 void PasswordFileTests::testWriting()
127 string testfile1 = TestUtilities::workingCopyPath(
"testfile1.pwmgr");
128 string testfile2 = TestUtilities::workingCopyPath(
"testfile2.pwmgr");
137 file.
save(
false,
true);
147 file.
save(
true,
false);
150 testReading(testfile1,
string(),
151 testfile2,
"654321",
true);
154 testReading(testfile1 +
".backup",
"123456",
155 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...
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.
const NodeEntry * rootEntry() const
Returns the root entry if present or nullptr otherwise.
The PasswordFileTests class tests the Io::PasswordFile class.
void doBackup()
Creates a backup of the file.
Contains all IO related classes.
const std::vector< Entry * > & children() const
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::string & label() const
Returns the label.
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)