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_EQUAL(EntryType::Account, rootEntry->
children()[0]->type());
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_EQUAL(FieldType::Password, static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(0).type());
87 static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(0).tiedAccount() ==
static_cast<AccountEntry *
>(rootEntry->
children()[0]));
88 CPPUNIT_ASSERT_EQUAL(FieldType::Normal, static_cast<AccountEntry *>(rootEntry->
children()[0])->fields().at(1).type());
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_EQUAL(EntryType::Account, rootEntry->
children()[1]->type());
94 CPPUNIT_ASSERT_EQUAL(0_st, static_cast<AccountEntry *>(rootEntry->
children()[1])->fields().size());
97 CPPUNIT_ASSERT_EQUAL(
"testcategory1"s, rootEntry->
children()[2]->label());
98 CPPUNIT_ASSERT_EQUAL(EntryType::Node, rootEntry->
children()[2]->type());
100 CPPUNIT_ASSERT_EQUAL(3_st, category->children().size());
101 CPPUNIT_ASSERT_EQUAL(EntryType::Node, category->children()[2]->type());
102 CPPUNIT_ASSERT_EQUAL(2_st, static_cast<NodeEntry *>(category->children()[2])->children().size());
105 CPPUNIT_ASSERT_EQUAL(
"testaccount3"s, rootEntry->
children()[3]->label());
116 CPPUNIT_ASSERT_EQUAL(
"testfile2 - modified"s, rootEntry2->
label());
117 CPPUNIT_ASSERT_EQUAL(2_st, rootEntry2->
children().size());
118 CPPUNIT_ASSERT_EQUAL(
"newAccount"s, rootEntry2->
children()[1]->label());
120 CPPUNIT_ASSERT_EQUAL(
"testfile2"s, rootEntry2->
label());
121 CPPUNIT_ASSERT_EQUAL(1_st, rootEntry2->
children().size());
129 void PasswordFileTests::testWriting()
131 const string testfile1 = TestUtilities::workingCopyPath(
"testfile1.pwmgr");
132 const 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 under path() replacing its previous contents.
CPPUNIT_TEST_SUITE_REGISTRATION(PasswordFileTests)