1 // xestiaab_common.h - Xestia Address Book Common Functions Unit Tests.
3 // (c) 2012-2017 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)
33 // Run the unit tests depending on the operating system being used.
35 #if defined(__WIN32__)
37 #elif defined(__APPLE__)
41 // Setup for comparison purposes. Should be /home/<user>/.xestiaab
45 wxString UserDirCheck = "";
46 UserDirCheck.Append(wxString::FromUTF8(getenv("HOME")));
47 UserDirCheck.Append(wxT("/.xestiaab/"));
51 wxString UserDir = GetUserDir();
55 ASSERT_EQ(UserDir, UserDirCheck);
61 TEST(CommonFunctions, GivenAPredefinedPathWhenGetUserPrefDirIsCalledThenReceivedPathMatches)
64 // Run the unit tests depending on the operating system being used.
66 #if defined(__WIN32__)
68 #elif defined(__APPLE__)
72 // Setup for comparison purposes. Should be /home/<user>/.xestiaab/preferences
76 wxString UserPrefDirCheck = "";
77 UserPrefDirCheck.Append(wxString::FromUTF8(getenv("HOME")));
78 UserPrefDirCheck.Append(wxT("/.xestiaab/preferences/"));
82 wxString UserPrefDir = GetUserPrefDir();
86 ASSERT_EQ(UserPrefDir, UserPrefDirCheck);
92 TEST(CommonFunctions, GivenThreePredefinedPathsWhenGetAccountDirIsCalledForEachThenReceivedPathsMatches)
95 // Run the unit tests depending on the operating system being used.
97 #if defined(__WIN32__)
99 #elif defined(__APPLE__)
103 // Setup for comparison purposes. Should be /home/<user>/.xestiaab/preferences
105 wxString AccountDirCheck1 = "";
106 wxString AccountDirCheck2 = "";
107 wxString AccountDirCheck3 = "";
111 AccountDirCheck1.Append(wxString::FromUTF8(getenv("HOME")));
112 AccountDirCheck1.Append(wxT("/.xestiaab/accounts/"));
113 AccountDirCheck1.Append("Example1");
114 AccountDirCheck1.Append(wxT("/"));
116 AccountDirCheck2.Append(wxString::FromUTF8(getenv("HOME")));
117 AccountDirCheck2.Append(wxT("/.xestiaab/accounts/"));
118 AccountDirCheck2.Append("Example2");
119 AccountDirCheck2.Append(wxT("/"));
121 AccountDirCheck3.Append(wxString::FromUTF8(getenv("HOME")));
122 AccountDirCheck3.Append(wxT("/.xestiaab/accounts/"));
123 AccountDirCheck3.Append("Example3");
124 AccountDirCheck3.Append(wxT("/"));
128 wxString AccountDir1 = GetAccountDir(AccountInput1, false);
129 wxString AccountDir2 = GetAccountDir(AccountInput2, false);
130 wxString AccountDir3 = GetAccountDir(AccountInput3, false);
134 ASSERT_EQ(AccountDir1, AccountDirCheck1);
135 ASSERT_EQ(AccountDir2, AccountDirCheck2);
136 ASSERT_EQ(AccountDir3, AccountDirCheck3);
142 TEST(CommonFunctions, GivenThreePredefinedFilePathsWhenGetAccountDirIsCalledWithAppendServerCertificateSetToTrueForEachThenReceivedPathsMatches)
145 // Run the unit tests depending on the operating system being used.
147 #if defined(__WIN32__)
149 #elif defined(__APPLE__)
153 // Setup for comparison purposes. Should be /home/<user>/.xestiaab/preferences
155 wxString AccountDirCheck1 = "";
156 wxString AccountDirCheck2 = "";
157 wxString AccountDirCheck3 = "";
161 AccountDirCheck1.Append(wxString::FromUTF8(getenv("HOME")));
162 AccountDirCheck1.Append(wxT("/.xestiaab/accounts/"));
163 AccountDirCheck1.Append("Example1");
164 AccountDirCheck1.Append(wxT("/server.crt"));
166 AccountDirCheck2.Append(wxString::FromUTF8(getenv("HOME")));
167 AccountDirCheck2.Append(wxT("/.xestiaab/accounts/"));
168 AccountDirCheck2.Append("Example2");
169 AccountDirCheck2.Append(wxT("/server.crt"));
171 AccountDirCheck3.Append(wxString::FromUTF8(getenv("HOME")));
172 AccountDirCheck3.Append(wxT("/.xestiaab/accounts/"));
173 AccountDirCheck3.Append("Example3");
174 AccountDirCheck3.Append(wxT("/server.crt"));
178 wxString AccountDir1 = GetAccountDir(AccountInput1, true);
179 wxString AccountDir2 = GetAccountDir(AccountInput2, true);
180 wxString AccountDir3 = GetAccountDir(AccountInput3, true);
184 ASSERT_EQ(AccountDir1, AccountDirCheck1);
185 ASSERT_EQ(AccountDir2, AccountDirCheck2);
186 ASSERT_EQ(AccountDir3, AccountDirCheck3);
192 TEST(CommonFunctions, GivenPredefinedFilePathWhenGetAccountsFileIsCalledThenReceivedFilePathMatches)
195 // Run the unit tests depending on the operating system being used.
197 #if defined(__WIN32__)
199 #elif defined(__APPLE__)
203 // Setup for comparison purposes. Should be
204 // /home/<user>/.xestiaab/preferences/accounts
208 wxString AccountsFileCheck = "";
209 AccountsFileCheck.Clear();
210 AccountsFileCheck.Append(wxString::FromUTF8(getenv("HOME")));
211 AccountsFileCheck.Append(wxT("/.xestiaab/preferences/accounts"));
215 wxString AccountsFile = GetAccountsFile();
219 ASSERT_EQ(AccountsFile, AccountsFileCheck);
225 TEST(CommonFunctions, GivenPredefinedFilePathWhenGetSettingsFileIsCalledThenReceivedFilePathMatches)
228 // Run the unit tests depending on the operating system being used.
230 #if defined(__WIN32__)
232 #elif defined(__APPLE__)
236 // Setup for comparison purposes. Should be
237 // /home/<user>/.xestiaab/preferences/accounts
241 wxString SettingsFileCheck = "";
242 SettingsFileCheck.Clear();
243 SettingsFileCheck.Append(wxString::FromUTF8(getenv("HOME")));
244 SettingsFileCheck.Append(wxT("/.xestiaab/preferences/settings"));
248 wxString SettingsFile = GetSettingsFile();
252 ASSERT_EQ(SettingsFile, SettingsFileCheck);
258 TEST(CommonFunctions, GivenFivePredefinedFilenamePathsWhenCreateFilenamePathIsCalledThenReceivedPathMatches)
261 // Run the unit tests depending on the operating system being used.
263 #if defined(__WIN32__)
265 #elif defined(__APPLE__)
271 wxString FilenamePath1 = "/test";
272 wxString FilenamePath2 = "/example/test";
273 wxString FilenamePath3 = "/home/meep/test";
274 wxString FilenamePath4 = "/home/meep/moo/test";
275 wxString FilenamePath5 = "/home/meep/moo/yargh/test";
279 wxString GeneratedFilenamePath1 = CreateFilenamePath("", "test");
280 wxString GeneratedFilenamePath2 = CreateFilenamePath("/example", "test");
281 wxString GeneratedFilenamePath3 = CreateFilenamePath("/home/meep", "test");
282 wxString GeneratedFilenamePath4 = CreateFilenamePath("/home/meep/moo", "test");
283 wxString GeneratedFilenamePath5 = CreateFilenamePath("/home/meep/moo/yargh", "test");
287 ASSERT_EQ(FilenamePath1, GeneratedFilenamePath1);
288 ASSERT_EQ(FilenamePath2, GeneratedFilenamePath2);
289 ASSERT_EQ(FilenamePath3, GeneratedFilenamePath3);
290 ASSERT_EQ(FilenamePath4, GeneratedFilenamePath4);
291 ASSERT_EQ(FilenamePath5, GeneratedFilenamePath5);
297 TEST(CommonFunctions, GivenTwoFilesWhenGetMIMEIsCalledThenBothFilesMIMETypesAreGiven)
300 // Get the MIME type based on the files given.
304 wxString File1MIMEFormat = "";
305 wxString File2MIMEFormat = "";
309 File1MIMEFormat = GetMIME("TextMIMEExample.txt");
310 File2MIMEFormat = GetMIME("PictureMIMEExample.png");
314 ASSERT_EQ("text/plain", GetMIME("TextMIMEExample.txt"));
315 ASSERT_EQ("image/png", GetMIME("PictureMIMEExample.png"));
319 TEST(CommonFunctions, GivenTwoDomainNamesWhenCheckBlacklistIsCalledThenReturnsCorrectStatus)
322 // Test that the blacklist is actually working.
326 bool domainStatus = true;
330 domainStatus = CheckBlacklist("example.com");
334 ASSERT_EQ(false, domainStatus);
338 domainStatus = false;
339 domainStatus = CheckBlacklist("google.com");
343 ASSERT_EQ(true, domainStatus);
347 TEST(CommonFunctions, GivenExampleStringWhenEscapeStringIsCalledThenReturnedStringIsExample)
350 // Test the escape string function.
354 wxString EscapeStringData = "Example";
358 EscapeString(&EscapeStringData, false);
362 ASSERT_EQ("Example", EscapeStringData.ToStdString());
366 TEST(CommonFunctions, GivenStringWithCommaWhenEscapeStringIsCalledThenCommaIsEscaped)
369 // Test the escape string function.
373 EscapeStringData = "Here we go, an \"example\" of escaping string.";
377 EscapeString(&EscapeStringData, false);
381 ASSERT_EQ("Here we go\\, an \"example\" of escaping string.", EscapeStringData.ToStdString());
385 TEST(CommonFunctions, GivenStringWithNewlineCharactersWhenEscapeStringIsCalledThenNewlinesAreEscaped)
388 // Test the escape string function.
392 EscapeStringData = "Lets put some \nnew \nlines \nin \nhere.";
396 EscapeString(&EscapeStringData, false);
400 ASSERT_EQ("Lets put some \\nnew \\nlines \\nin \\nhere.", EscapeStringData.ToStdString());
404 TEST(CommonFunctions, GivenAStringWithReturnCharactersWhenEscapeStringIsCalledThenReturnsAreRemoved)
407 // Test the escape string function.
411 EscapeStringData = "Lets put some \r\nnew \r\nlines \r\nin \r\nhere.";
415 EscapeString(&EscapeStringData, false);
419 ASSERT_EQ("Lets put some \\nnew \\nlines \\nin \\nhere.", EscapeStringData.ToStdString());
423 TEST(CommonFunctions, GivenStringWithBackslashesWhenEscapeStringIsCalledThenBackslashesAreEscaped)
426 // Test the escape string function.
430 EscapeStringData = "A:\\path\\example\\lets\\have\\some\\fun";
434 EscapeString(&EscapeStringData, false);
438 ASSERT_EQ("A:\\\\path\\\\example\\\\lets\\\\have\\\\some\\\\fun", EscapeStringData.ToStdString());
442 TEST(CommonFunctions, GivenExampleStringWhenCaptureStringIsCalledThenReturnedStringIsExample)
445 // Test the capture string function.
449 wxString CaptureStringData = "Example";
453 CaptureString(&CaptureStringData, false);
457 ASSERT_EQ("Example", CaptureStringData.ToStdString());
461 TEST(CommonFunctions, GivenStringWithEscapedCommasWhenCaptureStringIsCalledThenCommasAreCaptured)
464 // Test the capture string function.
468 CaptureStringData = "Here we go\\, an \"example\" of escaping string.";
472 CaptureString(&CaptureStringData, false);
476 ASSERT_EQ("Here we go, an \"example\" of escaping string.", CaptureStringData.ToStdString());
480 TEST(CommonFunctions, GivenStringWithEscapedNewlinesWhenCaptureStringIsCalledThenNewlinesAreCaptured)
483 // Test the capture string function.
487 CaptureStringData = "Lets put some \\nnew \\nlines \\nin \\nhere.";
491 CaptureString(&CaptureStringData, false);
495 ASSERT_EQ("Lets put some \nnew \nlines \nin \nhere.", CaptureStringData.ToStdString());
499 TEST(CommonFunctions, GivenStringWithEscapedBackslashesWhenCaptureStringIsCalledThenBackslashesAreCaptured)
502 // Test the capture string function.
506 CaptureStringData = "A:\\\\path\\\\example\\\\lets\\\\have\\\\some\\\\fun";
510 CaptureString(&CaptureStringData, false);
514 ASSERT_EQ("A:\\path\\example\\lets\\have\\some\\fun", CaptureStringData.ToStdString());
518 TEST(CommonFunctions, GivenStringWithSeveralBackslashesWhenResetUnusedStringIsCalledThenStringIsReset)
521 // Tests for resetting an unused string.
525 wxString ResetUnusedStringData = "Random string with a \\\\\\\\serverpath\\\\location";
529 ResetUnusedString(&ResetUnusedStringData);
533 ASSERT_EQ("Random string with a \\\\serverpath\\location", ResetUnusedStringData.ToStdString());
537 TEST(CommonFunctions, GivenStringWithNewlinesWhenResetUnusedStringIsCalledThenStringIsFormatted)
540 // Tests for resetting an unused string.
544 ResetUnusedStringData = "Some text \\nwith new \\nlines!";
548 ResetUnusedString(&ResetUnusedStringData);
552 ASSERT_EQ("Some text \nwith new \nlines!", ResetUnusedStringData.ToStdString());
556 TEST(CommonFunctions, GivenStringWithSeveralCharactersWhenResetUnusedStringThenStringIsFormatted)
559 // Tests for resetting an unused string.
563 ResetUnusedStringData = "And now the list of characters: \\\\ \\n \\; \\,";
567 ResetUnusedString(&ResetUnusedStringData);
571 ASSERT_EQ("And now the list of characters: \\ \n ; ,", ResetUnusedStringData.ToStdString());
575 TEST(CommonFunctions, GivenStringWhenConvertToHTMLIsCalledThenStringisHTMLEscaped)
578 // Tests for converting data for inserting into a HTML
579 // formatted document.
583 wxString ConvertToHTMLData = "The test line where < is probably better than >!";
587 ConvertToHTML(&ConvertToHTMLData);
591 ASSERT_EQ("The test line where < is probably better than >!", ConvertToHTMLData.ToStdString());
595 TEST(CommonFunctions, GivenStringWithNewlineWhenConvertToHTMLIsCalledThenStringIsHTMLEscapedWithFormattedNewline)
598 // Tests for converting data for inserting into a HTML
599 // formatted document.
603 ConvertToHTMLData = "More testing &<>\n";
607 ConvertToHTML(&ConvertToHTMLData);
611 ASSERT_EQ("More testing &<><br>", ConvertToHTMLData.ToStdString());
615 TEST(CommonFunctions, GivenStringWithSeveralLinesWhenConvertToHTMLIsCalledThenStringIsHTMLEscapedWithFormattedNewlines)
618 // Tests for converting data for inserting into a HTML
619 // formatted document.
623 ConvertToHTMLData = "This is the first line.\nThis is the second line.\nThis is the third line.";
627 ConvertToHTML(&ConvertToHTMLData);
631 ASSERT_EQ("This is the first line.<br>This is the second line.<br>This is the third line.", ConvertToHTMLData.ToStdString());
635 static std::map<int,int> mapExample;
636 static int itemIndex = 0;
638 static mapExample.insert(std::make_pair(0,1));
639 static mapExample.insert(std::make_pair(1,1));
640 static mapExample.insert(std::make_pair(2,1));
641 static mapExample.insert(std::make_pair(5,1));
642 static mapExample.insert(std::make_pair(500,1));
643 static mapExample.insert(std::make_pair(2415,1));
644 static mapExistsStatus = false;
646 TEST(CommonFunctions, GivenMapExistsStatusIsFalseAndItemIndexIs0WhenMapDataExistsIsCalledThenConfirmItemIndex0DoesExist)
649 // Tests for checking that map data exists.
653 mapExistsStatus = false;
658 mapExistsStatus = MapDataExists(&itemIndex, &mapExample);
662 ASSERT_EQ(true, mapExistsStatus);
666 TEST(CommonFunctions, GivenMapExistsStatusIsFalseAndItemIndexIs1WhenMapDataExistsIsCalledThenConfirmItemIndex1DoesExist)
669 // Tests for checking that map data exists.
673 mapExistsStatus = false;
678 mapExistsStatus = MapDataExists(&itemIndex, &mapExample);
682 ASSERT_EQ(true, mapExistsStatus);
686 TEST(CommonFunctions, GivenMapExistsStatusIsFalseAndItemIndexIs2WhenMapDataExistsIsCalledThenConfirmItemIndex2DoesExist)
689 // Tests for checking that map data exists.
693 mapStatusExists = false;
698 mapStatusExists = MapDataExists(&itemIndex, &mapExample);
702 ASSERT_EQ(true, mapStatusExists);
706 TEST(CommonFunctions, GivenMapExistsStatusIsTrueAndItemIndexIs3WhenMapDataExistsIsCalledThenConfirmItemIndex3DoesntExist)
709 // Tests for checking that map data exists.
713 mapStatusExists = true;
718 mapStatusExists = MapDataExists(&itemIndex, &mapExample);
722 ASSERT_EQ(false, mapStatusExists);
726 TEST(CommonFunctions, GivenMapExistsStatusIsFalseAndItemIndexIs5WhenMapDataExistsIsCalledThenConfirmItemIndex5DoesExist)
729 // Tests for checking that map data exists.
733 mapStatusExists = false;
738 mapStatusExists = MapDataExists(&itemIndex, &mapExample);
742 ASSERT_EQ(true, mapStatusExists);
746 TEST(CommonFunctions, GivenMapExistsStatusIsTrueAndItemIndexIs6WhenMapDataExistsIsCalledThenConfirmItemIndex6DoesntExist)
749 // Tests for checking that map data exists.
753 mapStatusExists = true;
758 mapStatusExists = MapDataExists(&itemIndex, &mapExample);
762 ASSERT_EQ(false, mapStatusExists);
766 TEST(CommonFunctions, GivenMapExistsStatusIsFalseAndItemIndexIs499WhenMapDataExistsIsCalledThenConfirmItemIndex499DoesntExist)
769 // Tests for checking that map data exists.
773 mapStatusExists = false;
778 mapStatusExists = MapDataExists(&itemIndex, &mapExample);
782 ASSERT_EQ(false, mapStatusExists);
786 TEST(CommonFunctions, GivenMapExistsStatusIsFalseAndItemIndexIs500WhenMapDataExistsIsCalledThenConfirmItemIndex500DoesExist)
789 // Tests for checking that map data exists.
793 mapStatusExists = false;
798 mapStatusExists = MapDataExists(&itemIndex, &mapExample);
802 ASSERT_EQ(true, mapStatusExists);
806 TEST(CommonFunctions, GivenMapExistsStatusIsTrueAndItemIndexIs501WhenMapDataExistsIsCalledThenConfirmItemIndex501DoesntExist)
809 // Tests for checking that map data exists.
813 mapStatusExists = true;
818 mapStatusExists = MapDataExists(&itemIndex, &mapExample);
822 ASSERT_EQ(false, mapStatusExists);
826 TEST(CommonFunctions, GivenMapExistsStatusIsTrueAndItemIndexIs2414WhenMapDataExistsIsCalledThenConfirmItemIndex2414DoesntExist)
829 // Tests for checking that map data exists.
833 mapStatusExists = true;
838 mapStatusExists = MapDataExists(&itemIndex, &mapExample);
842 ASSERT_EQ(false, mapStatusExists);
846 TEST(CommonFunctions, GivenMapExistsStatusIsFalseAndItemIndexIs2415WhenMapDataExistsIsCalledThenConfirmItemIndex2415DoesExist)
849 // Tests for checking that map data exists.
853 mapStatusExists = false;
858 mapStatusExists = MapDataExists(&itemIndex, &mapExample);
862 ASSERT_EQ(true, mapStatusExists);
866 TEST(CommonFunctions, GivenMapExistsStatusIsTrueAndItemIndexIs2416WhenMapDataExistsIsCalledThenConfirmItemIndex2416DoesntExist)
869 // Tests for checking that map data exists.
873 mapStatusExists = true;
878 mapStatusExists = MapDataExists(&itemIndex, &mapExample);
882 ASSERT_EQ(false, mapStatusExists);