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);
The exception that is thrown when a parsing error occurs.
const std::vector< Field > & fields() const
The exception that is thrown when an encryption/decryption error occurs.
void setLabel(const std::string &label)
Sets the label.
const std::string & label() const
Returns the label.
The NodeEntry class acts as parent for other entries.
const std::vector< Entry * > & children() const
The PasswordFile class holds account information in the form of Entry and Field instances and provide...
const NodeEntry * rootEntry() const
Returns the root entry if present or nullptr otherwise.
void open(PasswordFileOpenFlags options=PasswordFileOpenFlags::Default)
Opens the file.
void load()
Reads the contents of the file.
std::uint32_t version() const
Returns the file version used the last time when saving the file (the version of the file as it is on...
void setPassword(const std::string &password)
Sets the current password.
bool isEncryptionUsed()
Returns an indication whether encryption is used and the file is open; returns always false otherwise...
void save(PasswordFileSaveFlags options=PasswordFileSaveFlags::Default)
Writes the current root entry to the file under path() replacing its previous contents.
std::string & encryptedExtendedHeader()
Returns the encrypted extended header.
void setPath(const std::string &value)
Sets the current file path.
std::string & extendedHeader()
Returns the extended header.
void doBackup()
Creates a backup of the file.
PasswordFileSaveFlags saveOptions() const
Returns the save options used the last time when saving the file.
The PasswordFileTests class tests the Io::PasswordFile class.
void testBasicWriting()
Tests writing (and reading again) using basic features.
void testExtendedWriting()
Tests writing (and reading again) using extended features.
void testReading()
Tests reading the testfiles testfile{1,2}.pwmgr.
Contains all IO related classes.
PASSWORD_FILE_EXPORT std::string flagsToString(PasswordFileOpenFlags flags)
Returns a comma-separated string for the specified flags.
CPPUNIT_TEST_SUITE_REGISTRATION(PasswordFileTests)