1 // $Id: import14.cc 5623 2013-11-21 14:36:29Z rafisol $
2 //
3 // Test
4 
5 #include <iostream>
6 #include <typeinfo>
7 
8 #include "aes256factory.hh"
9 #include "csvimport.h"
10 #include "file.h"
11 #include "filehelper.hh"
12 #include "testpaths.h"
13 
14 const char* VAL128 =
15     "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789abcdefghijklmn"
16     "opqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789abcdefg";
17 
18 const char* VAL256 =
19     "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789abcdefghijklmn"
20     "opqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzABC"
21     "DEFGHIJKLMNOPQRSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQ"
22     "RSTUVXYZ0123456789abcdefghijklmno";
23 
24 const char* VAL512 =
25     "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789abcdefghijklmn"
26     "opqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzABC"
27     "DEFGHIJKLMNOPQRSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQ"
28     "RSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ012345"
29     "6789abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789abcdefghij"
30     "klmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789abcdefghijklmnopqrstuvxy"
31     "zABCDEFGHIJKLMNOPQRSTUVXYZ0123456789abcdefghijklmnopqrstuvxyzABCDEF";
32 
main(int,char **)33 int main(int, char**) {
34     try {
35         CSVImport imp(SRCDIR "/test10.csv", BUILDDIR "/test10.pet", ';');
36         imp.import("test10");
37 
38         if (!imp.hadErrors()) {
39             imp.printLog();
40             return 1;
41         }
42 
43         if (imp.numErrors() != 3) {
44             std::cerr << "Expected 3 errors, got " << imp.numErrors()
45                       << std::endl;
46             imp.printLog();
47             return 1;
48         }
49 
50         imp.printLog();
51     } catch (std::exception& ex) {
52         std::cout << typeid(ex).name() << ": " << ex.what() << std::endl;
53         return 1;
54     }
55 
56     try {
57         auto password{yapet::toSecureArray("test10")};
58         std::shared_ptr<yapet::AbstractCryptoFactory> cryptoFactory{
59             new yapet::Aes256Factory{
60                 password, yapet::readMetaData(BUILDDIR "/test10.pet", false)}};
61         auto crypto{cryptoFactory->crypto()};
62 
63         YAPET::File file{cryptoFactory, BUILDDIR "/test10.pet", false};
64 
65         assert(file.getHeaderVersion() == yapet::HEADER_VERSION::VERSION_2);
66         std::list<yapet::PasswordListItem> list = file.read();
67 
68         if (list.size() != 4) {
69             std::cerr << "List size expected to be 4, got " << list.size()
70                       << std::endl;
71             return 1;
72         }
73 
74         std::list<yapet::PasswordListItem>::iterator it = list.begin();
75 
76         //
77         // Check first record
78         //
79 
80         yapet::PasswordRecord passwordRecord{
81             crypto->decrypt(it->encryptedRecord())};
82 
83         if (std::strcmp(reinterpret_cast<const char*>(passwordRecord.name()),
84                         VAL128) != 0) {
85             std::cerr << "Name does not match" << std::endl;
86 
87             return 1;
88         }
89 
90         if (std::strcmp(reinterpret_cast<const char*>(passwordRecord.host()),
91                         VAL256) != 0) {
92             std::cerr << "Host does not match" << std::endl;
93 
94             return 1;
95         }
96 
97         if (std::strcmp(
98                 reinterpret_cast<const char*>(passwordRecord.username()),
99                 VAL256) != 0) {
100             std::cerr << "User Name does not match" << std::endl;
101 
102             return 1;
103         }
104 
105         if (std::strcmp(
106                 reinterpret_cast<const char*>(passwordRecord.password()),
107                 VAL256) != 0) {
108             std::cerr << "Password does not match" << std::endl;
109 
110             return 1;
111         }
112 
113         if (std::strcmp(reinterpret_cast<const char*>(passwordRecord.comment()),
114                         VAL512) != 0) {
115             std::cerr << "Comment does not match" << std::endl;
116 
117             return 1;
118         }
119 
120         //
121         // Check second record
122         //
123 
124         it++;
125         passwordRecord =
126             yapet::PasswordRecord{crypto->decrypt(it->encryptedRecord())};
127 
128         if (std::strcmp(reinterpret_cast<const char*>(passwordRecord.name()),
129                         "name1") != 0) {
130             std::cerr << "Name does not match" << std::endl;
131 
132             return 1;
133         }
134 
135         if (std::strcmp(reinterpret_cast<const char*>(passwordRecord.host()),
136                         "host1") != 0) {
137             std::cerr << "Host does not match" << std::endl;
138 
139             return 1;
140         }
141 
142         if (std::strcmp(
143                 reinterpret_cast<const char*>(passwordRecord.username()),
144                 "username1") != 0) {
145             std::cerr << "User Name does not match" << std::endl;
146 
147             return 1;
148         }
149 
150         if (std::strcmp(
151                 reinterpret_cast<const char*>(passwordRecord.password()),
152                 "password1") != 0) {
153             std::cerr << "Password does not match" << std::endl;
154 
155             return 1;
156         }
157 
158         if (std::strcmp(reinterpret_cast<const char*>(passwordRecord.comment()),
159                         "comment1") != 0) {
160             std::cerr << "Comment does not match" << std::endl;
161 
162             return 1;
163         }
164 
165         //
166         // Check third record
167         //
168 
169         it++;
170         passwordRecord =
171             yapet::PasswordRecord{crypto->decrypt(it->encryptedRecord())};
172 
173         if (std::strcmp(reinterpret_cast<const char*>(passwordRecord.name()),
174                         "name2") != 0) {
175             std::cerr << "Name does not match" << std::endl;
176 
177             return 1;
178         }
179 
180         if (std::strcmp(reinterpret_cast<const char*>(passwordRecord.host()),
181                         "host2") != 0) {
182             std::cerr << "Host does not match" << std::endl;
183 
184             return 1;
185         }
186 
187         if (std::strcmp(
188                 reinterpret_cast<const char*>(passwordRecord.username()),
189                 "username2") != 0) {
190             std::cerr << "User Name does not match" << std::endl;
191 
192             return 1;
193         }
194 
195         if (std::strcmp(
196                 reinterpret_cast<const char*>(passwordRecord.password()),
197                 "password2") != 0) {
198             std::cerr << "Password does not match" << std::endl;
199 
200             return 1;
201         }
202 
203         if (std::strcmp(reinterpret_cast<const char*>(passwordRecord.comment()),
204                         "comment2") != 0) {
205             std::cerr << "Comment does not match" << std::endl;
206 
207             return 1;
208         }
209 
210         //
211         // Check fourth record
212         //
213 
214         it++;
215         passwordRecord =
216             yapet::PasswordRecord{crypto->decrypt(it->encryptedRecord())};
217 
218         if (std::strcmp(reinterpret_cast<const char*>(passwordRecord.name()),
219                         "name3") != 0) {
220             std::cerr << "Name does not match" << std::endl;
221 
222             return 1;
223         }
224 
225         if (std::strcmp(reinterpret_cast<const char*>(passwordRecord.host()),
226                         "host3") != 0) {
227             std::cerr << "Host does not match" << std::endl;
228 
229             return 1;
230         }
231 
232         if (std::strcmp(
233                 reinterpret_cast<const char*>(passwordRecord.username()),
234                 "username3") != 0) {
235             std::cerr << "User Name does not match" << std::endl;
236 
237             return 1;
238         }
239 
240         if (std::strcmp(
241                 reinterpret_cast<const char*>(passwordRecord.password()),
242                 "password3") != 0) {
243             std::cerr << "Password does not match" << std::endl;
244 
245             return 1;
246         }
247 
248         if (std::strcmp(reinterpret_cast<const char*>(passwordRecord.comment()),
249                         "comment3") != 0) {
250             std::cerr << "Comment does not match" << std::endl;
251 
252             return 1;
253         }
254 
255     } catch (std::exception& ex) {
256         std::cout << typeid(ex).name() << ": " << ex.what() << std::endl;
257         return 1;
258     }
259 
260     return 0;
261 }
262