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, GivenExampleStringWhenEscapeStringIsCalledThenReturnedStringIsExample){
340 // Test the escape string function.
344 wxString EscapeStringData = "Example";
348 EscapeString(&EscapeStringData, false);
352 ASSERT_EQ("Example", EscapeStringData.ToStdString());
356 TEST(CommonFunctions, GivenStringWithCommaWhenEscapeStringIsCalledThenCommaIsEscaped){
358 // Test the escape string function.
362 EscapeStringData = "Here we go, an \"example\" of escaping string.";
366 EscapeString(&EscapeStringData, false);
370 ASSERT_EQ("Here we go\\, an \"example\" of escaping string.", EscapeStringData.ToStdString());
374 TEST(CommonFunctions, GivenStringWithNewlineCharactersWhenEscapeStringIsCalledThenNewlinesAreEscaped){
376 // Test the escape string function.
380 EscapeStringData = "Lets put some \nnew \nlines \nin \nhere.";
384 EscapeString(&EscapeStringData, false);
388 ASSERT_EQ("Lets put some \\nnew \\nlines \\nin \\nhere.", EscapeStringData.ToStdString());
392 TEST(CommonFunctions, GivenAStringWithReturnCharactersWhenEscapeStringIsCalledThenReturnsAreRemoved){
394 // Test the escape string function.
398 EscapeStringData = "Lets put some \r\nnew \r\nlines \r\nin \r\nhere.";
402 EscapeString(&EscapeStringData, false);
406 ASSERT_EQ("Lets put some \\nnew \\nlines \\nin \\nhere.", EscapeStringData.ToStdString());
410 TEST(CommonFunctions, GivenStringWithBackslashesWhenEscapeStringIsCalledThenBackslashesAreEscaped){
412 // Test the escape string function.
416 EscapeStringData = "A:\\path\\example\\lets\\have\\some\\fun";
420 EscapeString(&EscapeStringData, false);
424 ASSERT_EQ("A:\\\\path\\\\example\\\\lets\\\\have\\\\some\\\\fun", EscapeStringData.ToStdString());
428 TEST(CommonFunctions, GivenExampleStringWhenCaptureStringIsCalledThenReturnedStringIsExample){
430 // Test the capture string function.
434 wxString CaptureStringData = "Example";
438 CaptureString(&CaptureStringData, false);
442 ASSERT_EQ("Example", CaptureStringData.ToStdString());
446 TEST(CommonFunctions, GivenStringWithEscapedCommasWhenCaptureStringIsCalledThenCommasAreCaptured){
448 // Test the capture string function.
452 CaptureStringData = "Here we go\\, an \"example\" of escaping string.";
456 CaptureString(&CaptureStringData, false);
460 ASSERT_EQ("Here we go, an \"example\" of escaping string.", CaptureStringData.ToStdString());
464 TEST(CommonFunctions, GivenStringWithEscapedNewlinesWhenCaptureStringIsCalledThenNewlinesAreCaptured){
466 // Test the capture string function.
470 CaptureStringData = "Lets put some \\nnew \\nlines \\nin \\nhere.";
474 CaptureString(&CaptureStringData, false);
478 ASSERT_EQ("Lets put some \nnew \nlines \nin \nhere.", CaptureStringData.ToStdString());
482 TEST(CommonFunctions, GivenStringWithEscapedBackslashesWhenCaptureStringIsCalledThenBackslashesAreCaptured){
484 // Test the capture string function.
488 CaptureStringData = "A:\\\\path\\\\example\\\\lets\\\\have\\\\some\\\\fun";
492 CaptureString(&CaptureStringData, false);
496 ASSERT_EQ("A:\\path\\example\\lets\\have\\some\\fun", CaptureStringData.ToStdString());
500 TEST(CommonFunctions, GivenStringWithSeveralBackslashesWhenResetUnusedStringIsCalledThenStringIsReset){
502 // Tests for resetting an unused string.
506 wxString ResetUnusedStringData = "Random string with a \\\\\\\\serverpath\\\\location";
510 ResetUnusedString(&ResetUnusedStringData);
514 ASSERT_EQ("Random string with a \\\\serverpath\\location", ResetUnusedStringData.ToStdString());
518 TEST(CommonFunctions, GivenStringWithNewlinesWhenResetUnusedStringIsCalledThenStringIsFormatted){
520 // Tests for resetting an unused string.
524 ResetUnusedStringData = "Some text \\nwith new \\nlines!";
528 ResetUnusedString(&ResetUnusedStringData);
532 ASSERT_EQ("Some text \nwith new \nlines!", ResetUnusedStringData.ToStdString());
536 TEST(CommonFunctions, GivenStringWithSeveralCharactersWhenResetUnusedStringThenStringIsFormatted){
538 // Tests for resetting an unused string.
542 ResetUnusedStringData = "And now the list of characters: \\\\ \\n \\; \\,";
546 ResetUnusedString(&ResetUnusedStringData);
550 ASSERT_EQ("And now the list of characters: \\ \n ; ,", ResetUnusedStringData.ToStdString());
554 TEST(CommonFunctions, ConvertToHTML){
556 // Tests for converting data for inserting into a HTML
557 // formatted document.
559 wxString ConvertToHTMLData = "The test line where < is probably better than >!";
560 ConvertToHTML(&ConvertToHTMLData);
561 ASSERT_EQ("The test line where < is probably better than >!", ConvertToHTMLData.ToStdString());
563 ConvertToHTMLData = "More testing &<>\n";
564 ConvertToHTML(&ConvertToHTMLData);
565 ASSERT_EQ("More testing &<><br>", ConvertToHTMLData.ToStdString());
567 ConvertToHTMLData = "This is the first line.\nThis is the second line.\nThis is the third line.";
568 ConvertToHTML(&ConvertToHTMLData);
569 ASSERT_EQ("This is the first line.<br>This is the second line.<br>This is the third line.", ConvertToHTMLData.ToStdString());
573 TEST(CommonFunctions, MapDataExists){
575 // Tests for checking that map data exists.
577 std::map<int,int> MapExample;
580 MapExample.insert(std::make_pair(0,1));
581 MapExample.insert(std::make_pair(1,1));
582 MapExample.insert(std::make_pair(2,1));
583 MapExample.insert(std::make_pair(5,1));
584 MapExample.insert(std::make_pair(500,1));
585 MapExample.insert(std::make_pair(2415,1));
587 ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
590 ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
593 ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
596 ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
599 ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
602 ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
605 ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
608 ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
611 ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
614 ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));
617 ASSERT_EQ(true, MapDataExists(&ItemIndex, &MapExample));
620 ASSERT_EQ(false, MapDataExists(&ItemIndex, &MapExample));