1 // xestiacalendar_caldav.h - Xestia Calendar CalDAV Object Unit Tests
3 // (c) 2016 Xestia Software Development.
5 // This file is part of Xestia Calendar.
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 Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include "../objects/CalDAV/CalDAV.h"
20 #include "xestiacalendar_testcommon.h"
26 TEST(CalDAV, BasicTests){
28 CalDAVConnectionData ConnPlain;
29 CalDAVConnectionData ConnNormal;
30 CalDAVConnectionData ConnInvalidSSL;
31 CalDAVConnectionData ConnTimeout;
32 ProcessConnectionDataFileResult DataFileResult;
34 bool ValidDataPlain = false;
35 bool ValidDataNormal = false;
36 bool ValidDataInvalidSSL = false;
37 bool ValidDataTimeout = false;
39 // Attempt to read the caldavtest-plain.auth file.
41 DataFileResult = ProcessConnectionDataFile("caldavtest-plain.auth", &ConnPlain);
42 if (DataFileResult == PROCESSCONNECTIONDATAFILE_OK){
43 ValidDataPlain = true;
46 // Attempt to read the caldavtest.auth file.
48 DataFileResult = ProcessConnectionDataFile("caldavtest.auth", &ConnNormal);
49 if (DataFileResult == PROCESSCONNECTIONDATAFILE_OK){
50 ValidDataNormal = true;
53 // Attempt to read the caldavtest-fail.auth file.
55 DataFileResult = ProcessConnectionDataFile("caldavtest-fail.auth", &ConnInvalidSSL);
56 if (DataFileResult == PROCESSCONNECTIONDATAFILE_OK){
57 ValidDataInvalidSSL = true;
60 // Attempt to read the caldavtest-timeout.auth file.
62 DataFileResult = ProcessConnectionDataFile("caldavtest-timeout.auth", &ConnTimeout);
63 if (DataFileResult == PROCESSCONNECTIONDATAFILE_OK){
64 ValidDataTimeout = true;
67 if (ValidDataPlain == false){
69 // Cannot read the caldavtest-plain.auth file properly.
71 cout << "The caldavtest-plain.auth file does not exist or is invalid!" << endl;
72 cout << "Please create the caldavtest-plain.auth file using the following format:" << endl << endl;
73 cout << "server=localname" << endl;
74 cout << "port=8080" << endl;
75 cout << "user=username" << endl;
76 cout << "pass=password" << endl;
77 cout << "ssl=false" << endl;
78 cout << "prefix=/lalala/lookatme/thisisa/prefix" << endl << endl;
82 if (ValidDataNormal == false){
84 // Cannot read the caldavtest.auth file properly.
86 cout << "The caldavtest.auth file does not exist or is invalid!" << endl;
87 cout << "Please create the caldavtest.auth file using the following format:" << endl << endl;
88 cout << "server=localname" << endl;
89 cout << "port=8080" << endl;
90 cout << "user=username" << endl;
91 cout << "pass=password" << endl;
92 cout << "ssl=false" << endl;
93 cout << "prefix=/lalala/lookatme/thisisa/prefix" << endl << endl;
97 if (ValidDataInvalidSSL == false){
99 // Cannot read the caldavtest-fail.auth file properly.
101 cout << "The caldavtest-fail.auth file does not exist or is invalid!" << endl;
102 cout << "Please create the caldavtest-fail.auth file using the following format:" << endl << endl;
103 cout << "server=fail.localname" << endl;
104 cout << "port=8080" << endl;
105 cout << "user=username" << endl;
106 cout << "pass=password" << endl;
107 cout << "ssl=false" << endl;
108 cout << "prefix=/lalala/lookatme/thisisa/prefix" << endl << endl;
112 if (ValidDataTimeout == false){
114 // Cannot read the caldavtest-timeout.auth file properly.
116 cout << "The caldavtest-timeout.auth file does not exist or is invalid!" << endl;
117 cout << "Please create the caldavtest-timeout.auth file using the following format:" << endl << endl;
118 cout << "server=fail.localname" << endl;
119 cout << "port=8080" << endl;
120 cout << "user=username" << endl;
121 cout << "pass=password" << endl;
122 cout << "ssl=false" << endl;
123 cout << "prefix=/lalala/lookatme/thisisa/prefix" << endl << endl;
127 ASSERT_EQ(true, ValidDataPlain);
128 ASSERT_EQ(true, ValidDataNormal);
129 ASSERT_EQ(true, ValidDataInvalidSSL);
130 ASSERT_EQ(true, ValidDataTimeout);
132 // (*nix version) Setup an initial connection (just plain
136 CalDAVPlain.SetupConnectionData(&ConnPlain);
138 // Verify that the settings match with the CalDAVConnectionData
141 CalDAVStatus CalDAVPlainStatus = CalDAVPlain.GetConnectionData();
143 ASSERT_EQ(CalDAVPlainStatus.Hostname, ConnPlain.Hostname);
144 ASSERT_EQ(CalDAVPlainStatus.Username, ConnPlain.Username);
145 ASSERT_EQ(CalDAVPlainStatus.Port, ConnPlain.Port);
146 ASSERT_EQ(CalDAVPlainStatus.Prefix, ConnPlain.Prefix);
147 ASSERT_EQ(CalDAVPlainStatus.UseSSL, ConnPlain.UseSSL);
149 // Verify that the connection was successful.
151 CalDAVServerResult ConnResult = CalDAVPlain.Connect();
153 ASSERT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result);
154 ASSERT_EQ(200, ConnResult.HTTPCode);
155 ASSERT_EQ(CURLE_OK, ConnResult.Code);
157 // Do another connection and this time the connection should
158 // fail due to being an invalid host name.
160 CalDAVConnectionData ConnPlainFail;
161 ConnPlainFail.Hostname = "server.invalid";
162 ConnPlainFail.Username = "fail";
163 ConnPlainFail.Password = "fail";
164 ConnPlainFail.Port = 80;
165 ConnPlainFail.UseSSL = false;
167 // Setup the CalDAV connection object.
169 CalDAV CalDAVPlainFail;
170 CalDAVPlainFail.SetupConnectionData(&ConnPlainFail);
172 // Setup the CalDAVStatus object.
174 CalDAVStatus CalDAVPlainFailStatus = CalDAVPlain.GetConnectionData();
178 ConnResult = CalDAVPlainFail.Connect();
180 ASSERT_EQ(CALDAVQUERYRESULT_SERVERERROR, ConnResult.Result);
181 ASSERT_EQ(0, ConnResult.HTTPCode);
182 ASSERT_EQ(CURLE_COULDNT_RESOLVE_HOST, ConnResult.Code);
184 // (*nix version) Setup an initial connection (with a valid
188 CalDAVNormal.SetupConnectionData(&ConnNormal);
190 // Verify that the settings match with the CalDAVConnectionData
193 CalDAVStatus CalDAVNormalStatus = CalDAVNormal.GetConnectionData();
195 ASSERT_EQ(CalDAVNormalStatus.Hostname, ConnNormal.Hostname);
196 ASSERT_EQ(CalDAVNormalStatus.Username, ConnNormal.Username);
197 ASSERT_EQ(CalDAVNormalStatus.Port, ConnNormal.Port);
198 ASSERT_EQ(CalDAVNormalStatus.Prefix, ConnNormal.Prefix);
199 ASSERT_EQ(CalDAVNormalStatus.UseSSL, ConnNormal.UseSSL);
201 // Verify that the connection was successful (with a valid
204 ConnResult = CalDAVNormal.Connect();
206 ASSERT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result);
207 ASSERT_EQ(200, ConnResult.HTTPCode);
208 ASSERT_EQ(CURLE_OK, ConnResult.Code);
210 // (*nix version) Setup an initial connection on a server that
211 // will fail due to having an invalid SSL certificate.
213 CalDAV CalDAVInvalidSSL;
214 CalDAVInvalidSSL.SetupConnectionData(&ConnInvalidSSL);
216 // Verify that the settings match with the CalDAVConnectionData
219 CalDAVStatus CalDAVInvalidSSLStatus = CalDAVInvalidSSL.GetConnectionData();
221 ASSERT_EQ(CalDAVInvalidSSLStatus.Hostname, ConnInvalidSSL.Hostname);
222 ASSERT_EQ(CalDAVInvalidSSLStatus.Username, ConnInvalidSSL.Username);
223 ASSERT_EQ(CalDAVInvalidSSLStatus.Port, ConnInvalidSSL.Port);
224 ASSERT_EQ(CalDAVInvalidSSLStatus.Prefix, ConnInvalidSSL.Prefix);
225 ASSERT_EQ(CalDAVInvalidSSLStatus.UseSSL, ConnInvalidSSL.UseSSL);
227 // Verify that the connection had failed. (with an invalid
230 ConnResult = CalDAVInvalidSSL.Connect();
232 ASSERT_EQ(CALDAVQUERYRESULT_SERVERERROR, ConnResult.Result);
233 ASSERT_EQ(0, ConnResult.HTTPCode);
234 ASSERT_EQ(CURLE_SSL_CACERT, ConnResult.Code);
236 // (*nix version) Setup an inital connection on a server where
239 ConnTimeout.Timeout = 5;
241 CalDAV CalDAVTimeout;
242 CalDAVTimeout.SetupConnectionData(&ConnTimeout);
244 // Verify that the settings match with the CalDAVConnectionData
247 CalDAVStatus CalDAVTimeoutStatus = CalDAVTimeout.GetConnectionData();
249 ASSERT_EQ(CalDAVTimeoutStatus.Hostname, ConnTimeout.Hostname);
250 ASSERT_EQ(CalDAVTimeoutStatus.Username, ConnTimeout.Username);
251 ASSERT_EQ(CalDAVTimeoutStatus.Port, ConnTimeout.Port);
252 ASSERT_EQ(CalDAVTimeoutStatus.Prefix, ConnTimeout.Prefix);
253 ASSERT_EQ(CalDAVTimeoutStatus.Timeout, ConnTimeout.Timeout);
254 ASSERT_EQ(CalDAVTimeoutStatus.UseSSL, ConnTimeout.UseSSL);
256 // Verify that the connection had timed out.
258 ConnResult = CalDAVTimeout.Connect();
260 ASSERT_EQ(CALDAVQUERYRESULT_SERVERERROR, ConnResult.Result);
261 ASSERT_EQ(0, ConnResult.HTTPCode);
262 ASSERT_EQ(CURLE_OPERATION_TIMEDOUT, ConnResult.Code);
266 TEST(CalDAV, BuildServerAddress){
268 CalDAVConnectionData ConnNormal;
269 ProcessConnectionDataFileResult DataFileResult;
270 bool ValidDataNormal = false;
272 // Attempt to read the caldavtest.auth file.
274 DataFileResult = ProcessConnectionDataFile("caldavtest.auth", &ConnNormal);
275 if (DataFileResult == PROCESSCONNECTIONDATAFILE_OK){
276 ValidDataNormal = true;
279 if (ValidDataNormal == false){
281 // Cannot read the caldavtest.auth file properly.
283 cout << "The caldavtest.auth file does not exist or is invalid!" << endl;
284 cout << "Please create the caldavtest.auth file using the following format:" << endl << endl;
285 cout << "server=localname" << endl;
286 cout << "port=8080" << endl;
287 cout << "user=username" << endl;
288 cout << "pass=password" << endl;
289 cout << "ssl=false" << endl;
290 cout << "prefix=/lalala/lookatme/thisisa/prefix" << endl << endl;
294 ASSERT_EQ(true, ValidDataNormal);
296 // Setup the server address to check.
298 string ServerAddress;
300 // Setup the server address.
302 if (ConnNormal.UseSSL == true){
303 ServerAddress += "https://";
305 ServerAddress += "http://";
308 ServerAddress += ConnNormal.Hostname;
310 // Check if server port is 80, otherwise
311 // specifiy the port number in the address.
313 if (ConnNormal.Port != 80){
314 ServerAddress += ":";
315 ServerAddress += to_string(ConnNormal.Port);
318 ServerAddress += "/principals/";
320 ASSERT_EQ(ServerAddress, BuildServerAddress(&ConnNormal, "/principals/"));
324 TEST(CalDAV, CalendarServerSupport){
326 CalDAVConnectionData ConnNormal;
328 bool ValidDataNormal = false;
330 // Attempt to read the caldavtest.auth file.
332 ProcessConnectionDataFileResult DataFileResult = ProcessConnectionDataFile("caldavtest.auth", &ConnNormal);
333 if (DataFileResult == PROCESSCONNECTIONDATAFILE_OK){
334 ValidDataNormal = true;
337 ASSERT_EQ(true, ValidDataNormal);
339 // Setup the connection.
341 CalDAV ServerConnection;
343 ServerConnection.SetupConnectionData(&ConnNormal);
345 // Verify the connection settings.
347 CalDAVStatus CalDAVStatus = ServerConnection.GetConnectionData();
349 ASSERT_EQ(CalDAVStatus.Hostname, ConnNormal.Hostname);
350 ASSERT_EQ(CalDAVStatus.Username, ConnNormal.Username);
351 ASSERT_EQ(CalDAVStatus.Port, ConnNormal.Port);
352 ASSERT_EQ(CalDAVStatus.Prefix, ConnNormal.Prefix);
353 ASSERT_EQ(CalDAVStatus.UseSSL, ConnNormal.UseSSL);
355 // Connect to the server.
357 CalDAVServerResult ConnResult = ServerConnection.Connect();
359 ASSERT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result);
360 ASSERT_EQ(200, ConnResult.HTTPCode);
361 ASSERT_EQ(CURLE_OK, ConnResult.Code);
363 // Check for basic support of the calendar server.
365 CalDAVServerSupport ConnSupport = ServerConnection.GetServerSupport();
367 ASSERT_EQ(true, ConnSupport.BasicSupport);
371 TEST(CalDAV, GetCalendarHome){
373 CalDAVConnectionData ConnNormal;
375 bool ValidDataNormal = false;
377 // Attempt to read the caldavtest.auth file.
379 ProcessConnectionDataFileResult DataFileResult = ProcessConnectionDataFile("caldavtest.auth", &ConnNormal);
380 if (DataFileResult == PROCESSCONNECTIONDATAFILE_OK){
381 ValidDataNormal = true;
384 ASSERT_EQ(true, ValidDataNormal);
386 // Setup the connection.
388 CalDAV ServerConnection;
390 ServerConnection.SetupConnectionData(&ConnNormal);
392 // Verify the connection settings.
394 CalDAVStatus CalDAVStatus = ServerConnection.GetConnectionData();
396 ASSERT_EQ(CalDAVStatus.Hostname, ConnNormal.Hostname);
397 ASSERT_EQ(CalDAVStatus.Username, ConnNormal.Username);
398 ASSERT_EQ(CalDAVStatus.Port, ConnNormal.Port);
399 ASSERT_EQ(CalDAVStatus.Prefix, ConnNormal.Prefix);
400 ASSERT_EQ(CalDAVStatus.UseSSL, ConnNormal.UseSSL);
402 // Connect to the server.
404 CalDAVServerResult ConnResult = ServerConnection.Connect();
406 ASSERT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result);
407 ASSERT_EQ(200, ConnResult.HTTPCode);
408 ASSERT_EQ(CURLE_OK, ConnResult.Code);
410 // Check for basic support of the calendar server.
412 CalDAVServerSupport ConnSupport = ServerConnection.GetServerSupport();
414 ASSERT_EQ(true, ConnSupport.BasicSupport);
416 // Get the user principal.
418 string CurrentUserPrincipal = ServerConnection.GetUserPrincipal();
420 // Check the response from the server.
422 ConnResult = ServerConnection.GetServerResult();
424 EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result);
425 ASSERT_EQ(207, ConnResult.HTTPCode);
426 ASSERT_EQ(CURLE_OK, ConnResult.Code);
428 // Get the calendar home.
430 string CalendarHome = ServerConnection.GetCalendarHome(CurrentUserPrincipal);
432 // Check the response from the server.
434 ConnResult = ServerConnection.GetServerResult();
436 EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result);
437 ASSERT_EQ(207, ConnResult.HTTPCode);
438 ASSERT_EQ(CURLE_OK, ConnResult.Code);
442 TEST(CalDAV, ListCalendars){
444 CalDAVConnectionData ConnNormal;
445 string CurrentUserPrincipal;
447 bool ValidDataNormal = false;
449 // Attempt to read the caldavtest.auth file.
451 ProcessConnectionDataFileResult DataFileResult = ProcessConnectionDataFile("caldavtest.auth", &ConnNormal);
452 if (DataFileResult == PROCESSCONNECTIONDATAFILE_OK){
453 ValidDataNormal = true;
456 ASSERT_EQ(true, ValidDataNormal);
458 // Setup the connection.
460 CalDAV ServerConnection;
462 ServerConnection.SetupConnectionData(&ConnNormal);
464 // Verify the connection settings.
466 CalDAVStatus CalDAVStatus = ServerConnection.GetConnectionData();
468 ASSERT_EQ(CalDAVStatus.Hostname, ConnNormal.Hostname);
469 ASSERT_EQ(CalDAVStatus.Username, ConnNormal.Username);
470 ASSERT_EQ(CalDAVStatus.Port, ConnNormal.Port);
471 ASSERT_EQ(CalDAVStatus.Prefix, ConnNormal.Prefix);
472 ASSERT_EQ(CalDAVStatus.UseSSL, ConnNormal.UseSSL);
474 // Connect to the server.
476 CalDAVServerResult ConnResult = ServerConnection.Connect();
478 EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result);
479 ASSERT_EQ(200, ConnResult.HTTPCode);
480 ASSERT_EQ(CURLE_OK, ConnResult.Code);
482 // Check that the server supports CalDAV.
484 CalDAVServerSupport ConnSupport = ServerConnection.GetServerSupport();
485 ConnResult = ServerConnection.GetServerResult();
487 EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result);
488 ASSERT_EQ(200, ConnResult.HTTPCode);
489 ASSERT_EQ(CURLE_OK, ConnResult.Code);
490 ASSERT_EQ(true, ConnSupport.BasicSupport);
492 // Get the list of calendars.
494 CalDAVCalendarList CalendarList = ServerConnection.GetCalendars();
496 // Check the response result from the server.
498 ConnResult = ServerConnection.GetServerResult();
500 EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result);
501 ASSERT_EQ(207, ConnResult.HTTPCode);
502 ASSERT_EQ(CURLE_OK, ConnResult.Code);
506 TEST(CalDAV, AddCalendar){
508 CalDAVConnectionData ConnNormal;
509 string CurrentUserPrincipal;
511 bool ValidDataNormal = false;
513 // Attempt to read the caldavtest.auth file.
515 ProcessConnectionDataFileResult DataFileResult = ProcessConnectionDataFile("caldavtest.auth", &ConnNormal);
516 if (DataFileResult == PROCESSCONNECTIONDATAFILE_OK){
517 ValidDataNormal = true;
520 ASSERT_EQ(true, ValidDataNormal);
522 // Setup the connection.
524 CalDAV ServerConnection;
526 ServerConnection.SetupConnectionData(&ConnNormal);
528 // Verify the connection settings.
530 CalDAVStatus CalDAVStatus = ServerConnection.GetConnectionData();
532 ASSERT_EQ(CalDAVStatus.Hostname, ConnNormal.Hostname);
533 ASSERT_EQ(CalDAVStatus.Username, ConnNormal.Username);
534 ASSERT_EQ(CalDAVStatus.Port, ConnNormal.Port);
535 ASSERT_EQ(CalDAVStatus.Prefix, ConnNormal.Prefix);
536 ASSERT_EQ(CalDAVStatus.UseSSL, ConnNormal.UseSSL);
538 // Connect to the server.
540 CalDAVServerResult ConnResult = ServerConnection.Connect();
542 EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result);
543 ASSERT_EQ(200, ConnResult.HTTPCode);
544 ASSERT_EQ(CURLE_OK, ConnResult.Code);
546 // Add a calendar to the server.
548 ConnResult = ServerConnection.AddCalendar("New Calendar");
550 EXPECT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result);
551 ASSERT_EQ(201, ConnResult.HTTPCode);
552 ASSERT_EQ(CURLE_OK, ConnResult.Code);