1 // xestiaab_common.h - Xestia Address Book Common Functions Unit Tests.
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include <gtest/gtest.h>
20 #include "../common/dirs.h"
21 #include "../common/filename.h"
22 #include "../common/mime.h"
23 #include "../common/svrblist.h"
24 #include "../common/textprocessing.h"
26 static wxString AccountInput1 = "Example1";
27 static wxString AccountInput2 = "Example2";
28 static wxString AccountInput3 = "Example3";
30 TEST(CommonFunctions, GivenAPredefinedPathWhenGetUserDirIsCalledThenReceivedPathMatches){
32 // Run the unit tests depending on the operating system being used.
34 #if defined(__WIN32__)
36 #elif defined(__APPLE__)
40 // Setup for comparison purposes. Should be /home/<user>/.xestiaab
44 wxString UserDirCheck = "";
45 UserDirCheck.Append(wxString::FromUTF8(getenv("HOME")));
46 UserDirCheck.Append(wxT("/.xestiaab/"));
50 wxString UserDir = GetUserDir();
54 ASSERT_EQ(UserDir, UserDirCheck);
60 TEST(CommonFunctions, GivenAPredefinedPathWhenGetUserPrefDirIsCalledThenReceivedPathMatches){
62 // Run the unit tests depending on the operating system being used.
64 #if defined(__WIN32__)
66 #elif defined(__APPLE__)
70 // Setup for comparison purposes. Should be /home/<user>/.xestiaab/preferences
74 wxString UserPrefDirCheck = "";
75 UserPrefDirCheck.Append(wxString::FromUTF8(getenv("HOME")));
76 UserPrefDirCheck.Append(wxT("/.xestiaab/preferences/"));
80 wxString UserPrefDir = GetUserPrefDir();
84 ASSERT_EQ(UserPrefDir, UserPrefDirCheck);
90 TEST(CommonFunctions, GivenThreePredefinedPathsWhenGetAccountDirIsCalledForEachThenReceivedPathsMatches){
92 // Run the unit tests depending on the operating system being used.
94 #if defined(__WIN32__)
96 #elif defined(__APPLE__)
100 // Setup for comparison purposes. Should be /home/<user>/.xestiaab/preferences
102 wxString AccountDirCheck1 = "";
103 wxString AccountDirCheck2 = "";
104 wxString AccountDirCheck3 = "";
108 AccountDirCheck1.Append(wxString::FromUTF8(getenv("HOME")));
109 AccountDirCheck1.Append(wxT("/.xestiaab/accounts/"));
110 AccountDirCheck1.Append("Example1");
111 AccountDirCheck1.Append(wxT("/"));
113 AccountDirCheck2.Append(wxString::FromUTF8(getenv("HOME")));
114 AccountDirCheck2.Append(wxT("/.xestiaab/accounts/"));
115 AccountDirCheck2.Append("Example2");
116 AccountDirCheck2.Append(wxT("/"));
118 AccountDirCheck3.Append(wxString::FromUTF8(getenv("HOME")));
119 AccountDirCheck3.Append(wxT("/.xestiaab/accounts/"));
120 AccountDirCheck3.Append("Example3");
121 AccountDirCheck3.Append(wxT("/"));
125 wxString AccountDir1 = GetAccountDir(AccountInput1, false);
126 wxString AccountDir2 = GetAccountDir(AccountInput2, false);
127 wxString AccountDir3 = GetAccountDir(AccountInput3, false);
131 ASSERT_EQ(AccountDir1, AccountDirCheck1);
132 ASSERT_EQ(AccountDir2, AccountDirCheck2);
133 ASSERT_EQ(AccountDir3, AccountDirCheck3);
139 TEST(CommonFunctions, GivenThreePredefinedFilePathsWhenGetAccountDirIsCalledWithAppendServerCertificateSetToTrueForEachThenReceivedPathsMatches){
141 // Run the unit tests depending on the operating system being used.
143 #if defined(__WIN32__)
145 #elif defined(__APPLE__)
149 // Setup for comparison purposes. Should be /home/<user>/.xestiaab/preferences
151 wxString AccountDirCheck1 = "";
152 wxString AccountDirCheck2 = "";
153 wxString AccountDirCheck3 = "";
157 AccountDirCheck1.Append(wxString::FromUTF8(getenv("HOME")));
158 AccountDirCheck1.Append(wxT("/.xestiaab/accounts/"));
159 AccountDirCheck1.Append("Example1");
160 AccountDirCheck1.Append(wxT("/server.crt"));
162 AccountDirCheck2.Append(wxString::FromUTF8(getenv("HOME")));
163 AccountDirCheck2.Append(wxT("/.xestiaab/accounts/"));
164 AccountDirCheck2.Append("Example2");
165 AccountDirCheck2.Append(wxT("/server.crt"));
167 AccountDirCheck3.Append(wxString::FromUTF8(getenv("HOME")));
168 AccountDirCheck3.Append(wxT("/.xestiaab/accounts/"));
169 AccountDirCheck3.Append("Example3");
170 AccountDirCheck3.Append(wxT("/server.crt"));
174 wxString AccountDir1 = GetAccountDir(AccountInput1, true);
175 wxString AccountDir2 = GetAccountDir(AccountInput2, true);
176 wxString AccountDir3 = GetAccountDir(AccountInput3, true);
180 ASSERT_EQ(AccountDir1, AccountDirCheck1);
181 ASSERT_EQ(AccountDir2, AccountDirCheck2);
182 ASSERT_EQ(AccountDir3, AccountDirCheck3);
188 TEST(CommonFunctions, GivenPredefinedFilePathWhenGetAccountsFileIsCalledThenReceivedFilePathMatches){
190 // Run the unit tests depending on the operating system being used.
192 #if defined(__WIN32__)
194 #elif defined(__APPLE__)
198 // Setup for comparison purposes. Should be
199 // /home/<user>/.xestiaab/preferences/accounts
203 wxString AccountsFileCheck = "";
204 AccountsFileCheck.Clear();
205 AccountsFileCheck.Append(wxString::FromUTF8(getenv("HOME")));
206 AccountsFileCheck.Append(wxT("/.xestiaab/preferences/accounts"));
210 wxString AccountsFile = GetAccountsFile();
214 ASSERT_EQ(AccountsFile, AccountsFileCheck);
220 TEST(CommonFunctions, GivenPredefinedFilePathWhenGetSettingsFileIsCalledThenReceivedFilePathMatches){
222 // Run the unit tests depending on the operating system being used.
224 #if defined(__WIN32__)
226 #elif defined(__APPLE__)
230 // Setup for comparison purposes. Should be
231 // /home/<user>/.xestiaab/preferences/accounts
235 wxString SettingsFileCheck = "";
236 SettingsFileCheck.Clear();
237 SettingsFileCheck.Append(wxString::FromUTF8(getenv("HOME")));
238 SettingsFileCheck.Append(wxT("/.xestiaab/preferences/settings"));
242 wxString SettingsFile = GetSettingsFile();
246 ASSERT_EQ(SettingsFile, SettingsFileCheck);
252 TEST(CommonFunctions, GivenFivePredefinedFilenamePathsWhenCreateFilenamePathIsCalledThenReceivedPathMatches){
254 // Run the unit tests depending on the operating system being used.
256 #if defined(__WIN32__)
258 #elif defined(__APPLE__)
264 wxString FilenamePath1 = "/test";
265 wxString FilenamePath2 = "/example/test";
266 wxString FilenamePath3 = "/home/meep/test";
267 wxString FilenamePath4 = "/home/meep/moo/test";
268 wxString FilenamePath5 = "/home/meep/moo/yargh/test";
272 wxString GeneratedFilenamePath1 = CreateFilenamePath("", "test");
273 wxString GeneratedFilenamePath2 = CreateFilenamePath("/example", "test");
274 wxString GeneratedFilenamePath3 = CreateFilenamePath("/home/meep", "test");
275 wxString GeneratedFilenamePath4 = CreateFilenamePath("/home/meep/moo", "test");
276 wxString GeneratedFilenamePath5 = CreateFilenamePath("/home/meep/moo/yargh", "test");
280 ASSERT_EQ(FilenamePath1, GeneratedFilenamePath1);
281 ASSERT_EQ(FilenamePath2, GeneratedFilenamePath2);
282 ASSERT_EQ(FilenamePath3, GeneratedFilenamePath3);
283 ASSERT_EQ(FilenamePath4, GeneratedFilenamePath4);
284 ASSERT_EQ(FilenamePath5, GeneratedFilenamePath5);
290 TEST(CommonFunctions, GivenTwoFilesWhenGetMIMEIsCalledThenBothFilesMIMETypesAreGiven){
292 // Get the MIME type based on the files given.
296 wxString File1MIMEFormat = "";
297 wxString File2MIMEFormat = "";
301 File1MIMEFormat = GetMIME("TextMIMEExample.txt");
302 File2MIMEFormat = GetMIME("PictureMIMEExample.png");
306 ASSERT_EQ("text/plain", GetMIME("TextMIMEExample.txt"));
307 ASSERT_EQ("image/png", GetMIME("PictureMIMEExample.png"));
311 TEST(CommonFunctions, GivenTwoDomainNamesWhenCheckBlacklistIsCalledThenReturnsCorrectStatus){
313 // Test that the blacklist is actually working.
317 bool domainStatus = true;
321 domainStatus = CheckBlacklist("example.com");
325 ASSERT_EQ(false, domainStatus);
329 domainStatus = false;
330 domainStatus = CheckBlacklist("google.com");
334 ASSERT_EQ(true, domainStatus);
338 TEST(CommonFunctions, EscapeString){
340 // Test the escape string function.
342 wxString EscapeStringData = "Example";
343 EscapeString(&EscapeStringData, false);
344 ASSERT_EQ("Example", EscapeStringData.ToStdString());
346 EscapeStringData = "Here we go, an \"example\" of escaping string.";
347 EscapeString(&EscapeStringData, false);
348 ASSERT_EQ("Here we go\\, an \"example\" of escaping string.", EscapeStringData.ToStdString());
350 EscapeStringData = "Lets put some \nnew \nlines \nin \nhere.";
351 EscapeString(&EscapeStringData, false);
352 ASSERT_EQ("Lets put some \\nnew \\nlines \\nin \\nhere.", EscapeStringData.ToStdString());
354 EscapeStringData = "Lets put some \r\nnew \r\nlines \r\nin \r\nhere.";
355 EscapeString(&EscapeStringData, false);
356 ASSERT_EQ("Lets put some \\nnew \\nlines \\nin \\nhere.", EscapeStringData.ToStdString());
358 EscapeStringData = "A:\\path\\example\\lets\\have\\some\\fun";
359 EscapeString(&EscapeStringData, false);
360 ASSERT_EQ("A:\\\\path\\\\example\\\\lets\\\\have\\\\some\\\\fun", EscapeStringData.ToStdString());
364 TEST(CommonFunctions, CaptureString){
366 // Test the capture string function.
368 wxString CaptureStringData = "Example";
369 CaptureString(&CaptureStringData, false);
370 ASSERT_EQ("Example", CaptureStringData.ToStdString());
372 CaptureStringData = "Here we go\\, an \"example\" of escaping string.";
373 CaptureString(&CaptureStringData, false);
374 ASSERT_EQ("Here we go, an \"example\" of escaping string.", CaptureStringData.ToStdString());
376 CaptureStringData = "Lets put some \\nnew \\nlines \\nin \\nhere.";
377 CaptureString(&CaptureStringData, false);
378 ASSERT_EQ("Lets put some \nnew \nlines \nin \nhere.", CaptureStringData.ToStdString());
380 CaptureStringData = "A:\\\\path\\\\example\\\\lets\\\\have\\\\some\\\\fun";
381 CaptureString(&CaptureStringData, false);
382 ASSERT_EQ("A:\\path\\example\\lets\\have\\some\\fun", CaptureStringData.ToStdString());
386 TEST(CommonFunctions, ResetUnusedString){
388 // Tests for resetting an unused string.
390 wxString ResetUnusedStringData = "Random string with a \\\\\\\\serverpath\\\\location";
391 ResetUnusedString(&ResetUnusedStringData);
392 ASSERT_EQ("Random string with a \\\\serverpath\\location", ResetUnusedStringData.ToStdString());
394 ResetUnusedStringData = "Some text \\nwith new \\nlines!";
395 ResetUnusedString(&ResetUnusedStringData);
396 ASSERT_EQ("Some text \nwith new \nlines!", ResetUnusedStringData.ToStdString());
398 ResetUnusedStringData = "Some text \\nwith new \\nlines!";
399 ResetUnusedString(&ResetUnusedStringData);
400 ASSERT_EQ("Some text \nwith new \nlines!", ResetUnusedStringData.ToStdString());
402 ResetUnusedStringData = "And now the list of characters: \\\\ \\n \\; \\,";
403 ResetUnusedString(&ResetUnusedStringData);
404 ASSERT_EQ("And now the list of characters: \\ \n ; ,", ResetUnusedStringData.ToStdString());
408 TEST(CommonFunctions, ConvertToHTML){
410 // Tests for converting data for inserting into a HTML
411 // formatted document.
413 wxString ConvertToHTMLData = "The test line where < is probably better than >!";
414 ConvertToHTML(&ConvertToHTMLData);
415 ASSERT_EQ("The test line where < is probably better than >!", ConvertToHTMLData.ToStdString());
417 ConvertToHTMLData = "More testing &<>\n";
418 ConvertToHTML(&ConvertToHTMLData);
419 ASSERT_EQ("More testing &<><br>", ConvertToHTMLData.ToStdString());
421 ConvertToHTMLData = "This is the first line.\nThis is the second line.\nThis is the third line.";
422 ConvertToHTML(&ConvertToHTMLData);
423 ASSERT_EQ("This is the first line.<br>This is the second line.<br>This is the third line.", ConvertToHTMLData.ToStdString());
427 TEST(CommonFunctions, MapDataExists){
429 // Tests for checking that map data exists.
431 std::map<int,int> MapExample;
434 MapExample.insert(std::make_pair(0,1));
435 MapExample.insert(std::make_pair(1,1));
436 MapExample.insert(std::make_pair(2,1));
437 MapExample.insert(std::make_pair(5,1));
438 MapExample.insert(std::make_pair(500,1));
439 MapExample.insert(std::make_pair(2415,1));
441 ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
444 ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
447 ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
450 ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
453 ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
456 ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
459 ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
462 ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
465 ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
468 ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
471 ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
474 ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));