Passwordfile library  3.1.2
C++ library to read/write passwords from/to encrypted files
passwordfiletests.cpp
Go to the documentation of this file.
1 #include "../io/passwordfile.h"
2 #include "../io/cryptoexception.h"
3 #include "../io/entry.h"
4 
5 #include <c++utilities/tests/testutils.h>
6 
7 #include <cppunit/extensions/HelperMacros.h>
8 #include <cppunit/TestFixture.h>
9 
10 using namespace std;
11 using namespace Io;
12 
13 using namespace CPPUNIT_NS;
14 
18 class PasswordFileTests : public TestFixture
19 {
20  CPPUNIT_TEST_SUITE(PasswordFileTests);
21  CPPUNIT_TEST(testReading);
22 #ifdef PLATFORM_UNIX
23  CPPUNIT_TEST(testWriting);
24 #endif
25  CPPUNIT_TEST_SUITE_END();
26 
27 public:
28  void setUp();
29  void tearDown();
30 
31  void testReading();
32  void testReading(const string &testfile1path, const string &testfile1password, const string &testfile2, const string &testfile2password, bool testfile2Mod);
33 #ifdef PLATFORM_UNIX
34  void testWriting();
35 #endif
36 };
37 
39 
41 {}
42 
44 {}
45 
50 {
51  testReading(TestUtilities::testFilePath("testfile1.pwmgr"), "123456",
52  TestUtilities::testFilePath("testfile2.pwmgr"), string(), false);
53 }
54 
55 void PasswordFileTests::testReading(const string &testfile1path, const string &testfile1password, const string &testfile2, const string &testfile2password, bool testfile2Mod)
56 {
57  PasswordFile file;
58 
59  // open testfile 1 ...
60  file.setPath(testfile1path);
61  file.open(true);
62 
63  CPPUNIT_ASSERT(file.isEncryptionUsed() == !testfile1password.empty());
64  // attempt to decrypt using a wrong password
65  file.setPassword(testfile1password + "asdf");
66  if(!testfile1password.empty()) {
67  CPPUNIT_ASSERT_THROW(file.load(), CryptoException);
68  }
69  // attempt to decrypt using the correct password
70  file.setPassword(testfile1password);
71  file.load();
72  // test root entry
73  const NodeEntry *rootEntry = file.rootEntry();
74  CPPUNIT_ASSERT(rootEntry->label() == "testfile1");
75  CPPUNIT_ASSERT(rootEntry->children().size() == 4);
76 
77  // test testaccount1
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);
86 
87  // test testaccount2
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());
91 
92  // test testcategory1
93  CPPUNIT_ASSERT(rootEntry->children()[2]->label() == "testcategory1");
94  CPPUNIT_ASSERT(rootEntry->children()[2]->type() == EntryType::Node);
95  const NodeEntry *category = static_cast<NodeEntry *>(rootEntry->children()[2]);
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);
99 
100  // test testaccount3
101  CPPUNIT_ASSERT(rootEntry->children()[3]->label() == "testaccount3");
102 
103  // open testfile 2
104  file.setPath(testfile2);
105  file.open(true);
106 
107  CPPUNIT_ASSERT(file.isEncryptionUsed() == !testfile2password.empty());
108  file.setPassword(testfile2password);
109  file.load();
110  rootEntry = file.rootEntry();
111  if(testfile2Mod) {
112  CPPUNIT_ASSERT(rootEntry->label() == "testfile2 - modified");
113  CPPUNIT_ASSERT(rootEntry->children().size() == 2);
114  CPPUNIT_ASSERT(rootEntry->children()[1]->label() == "newAccount");
115  } else {
116  CPPUNIT_ASSERT(rootEntry->label() == "testfile2");
117  CPPUNIT_ASSERT(rootEntry->children().size() == 1);
118  }
119 }
120 
121 #ifdef PLATFORM_UNIX
122 
125 void PasswordFileTests::testWriting()
126 {
127  string testfile1 = TestUtilities::workingCopyPath("testfile1.pwmgr");
128  string testfile2 = TestUtilities::workingCopyPath("testfile2.pwmgr");
129  PasswordFile file;
130 
131  // resave testfile 1
132  file.setPath(testfile1);
133  file.open(false);
134  file.setPassword("123456");
135  file.load();
136  file.doBackup();
137  file.save(false, true);
138 
139  // resave testfile 2
140  file.setPath(testfile2);
141  file.open(false);
142  file.load();
143  file.rootEntry()->setLabel("testfile2 - modified");
144  new AccountEntry("newAccount", file.rootEntry());
145  file.setPassword("654321");
146  file.doBackup();
147  file.save(true, false);
148 
149  // check results using the reading test
150  testReading(testfile1, string(),
151  testfile2, "654321", true);
152 
153  // check backup files
154  testReading(testfile1 + ".backup", "123456",
155  testfile2 + ".backup", string(), false);
156 }
157 #endif
The NodeEntry class acts as parent for other entries.
Definition: entry.h:97
The PasswordFile class holds account information in the form of Entry and Field instances and provide...
Definition: passwordfile.h:18
void setLabel(const std::string &label)
Sets the label.
Definition: entry.h:74
void load()
Reads the contents of the file.
void open(bool readOnly=false)
Opens the file.
STL namespace.
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
Definition: entry.h:127
The exception that is thrown when a parsing error occurs.
Definition: entry.h:147
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.
Definition: entry.h:64
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)