From ea9eaed7642efe480c3c79944d2f771f2ef51d54 Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Wed, 2 Mar 2016 12:19:42 +0000 Subject: [PATCH] Adde code and unit tests for checking CalDAV support. --- source/objects/CalDAV/CalDAV.cpp | 127 +++++++++++++++++++++++++++ source/objects/CalDAV/CalDAV.h | 15 ++++ source/tests/xestiacalendar_caldav.h | 47 ++++++++++ 3 files changed, 189 insertions(+) diff --git a/source/objects/CalDAV/CalDAV.cpp b/source/objects/CalDAV/CalDAV.cpp index 21e8ddc..bcd710a 100644 --- a/source/objects/CalDAV/CalDAV.cpp +++ b/source/objects/CalDAV/CalDAV.cpp @@ -133,6 +133,133 @@ CalDAVServerResult CalDAV::Connect(){ } +CalDAVServerSupport CalDAV::GetServerSupport(){ + + CalDAVServerSupport ServerStatus; + + // Setup the server connection. + + curl_easy_setopt(ConnectionHandle, CURLOPT_CUSTOMREQUEST, "OPTIONS"); + + CURLcode ServerResult = curl_easy_perform(ConnectionHandle); + + if (ServerResult != CURLE_OK){ + return ServerStatus; + } + + // Check that the server header has data in, + // otherwise return an "empty" CalDAVServerSupport. + + if (ServerHeader.size() == 0){ + return ServerStatus; + } + + // Process each line looking for the first DAV header + // line. + + bool NewlineMode = true; + + string DAVLine; + + for (int CharSeek = 0; CharSeek < ServerHeader.size(); CharSeek++){ + + if (NewlineMode == true){ + + // Check if we have reached the end of the string. + + if (CharSeek >= ServerHeader.size()){ + + break; + + } + + // Check the first four letters to make sure + // they are 'DAV:'. + + string DAVHeaderCheck = ""; + + try { + DAVHeaderCheck = ServerHeader.substr(CharSeek, 4); + } + + catch (out_of_range &oor){ + break; + } + + if (DAVHeaderCheck == "DAV:"){ + + CharSeek += 5; + + for (; CharSeek < ServerHeader.size(); CharSeek++){ + + if (ServerHeader[CharSeek] == '\n'){ + + break; + + } + + DAVLine.push_back(ServerHeader[CharSeek]); + + } + + break; + + } + + NewlineMode = false; + + } + + if (ServerHeader[CharSeek] == '\n'){ + + NewlineMode = true; + + } + + } + + // Process the DAV line. + + vector DAVLineData; + string DAVSegmentString; + + for (int CharSeek = 0; CharSeek < DAVLine.size(); CharSeek++){ + + if (DAVLine[CharSeek] == ' '){ + continue; + } + + if (DAVLine[CharSeek] == ','){ + + DAVLineData.push_back(DAVSegmentString); + DAVSegmentString.clear(); + continue; + + } + + DAVSegmentString += DAVLine[CharSeek]; + + } + + // Process the DAV values and set each value + // to true as required. + + for (int DAVItemSeek = 0; + DAVItemSeek < DAVLineData.size(); + DAVItemSeek++){ + + if (DAVLineData.at(DAVItemSeek) == "calendar-access"){ + + ServerStatus.BasicSupport = true; + + } + + } + + return ServerStatus; + +} + bool CalDAVObjectValidSettings(CalDAVConnectionData *ConnData){ // Check if the passed CalDAV Connection Data is has diff --git a/source/objects/CalDAV/CalDAV.h b/source/objects/CalDAV/CalDAV.h index 47b9617..d527541 100644 --- a/source/objects/CalDAV/CalDAV.h +++ b/source/objects/CalDAV/CalDAV.h @@ -22,6 +22,8 @@ #include #include #include +#include +#include using namespace std; @@ -63,6 +65,18 @@ struct CalDAVStatus{ }; +// CalDAVServerSupport: used for +// getting what the server supports +// from the CalDAV specification. + +struct CalDAVServerSupport{ + + // Variable name. Name in CalDAV header. + + bool BasicSupport = false; // calendar-access + +}; + // CalDAVServerResult: used for // getting the result of the // request made via the CalDAV @@ -88,6 +102,7 @@ class CalDAV{ void SetupConnectionData(CalDAVConnectionData *ConnData); CalDAVStatus GetConnectionData(); CalDAVServerResult Connect(); + CalDAVServerSupport GetServerSupport(); CalDAV(); ~CalDAV(); diff --git a/source/tests/xestiacalendar_caldav.h b/source/tests/xestiacalendar_caldav.h index 1557685..4b666c0 100644 --- a/source/tests/xestiacalendar_caldav.h +++ b/source/tests/xestiacalendar_caldav.h @@ -321,6 +321,53 @@ TEST(CalDAV, BuildServerAddress){ } +TEST(CalDAV, CalendarServerSupport){ + + CalDAVConnectionData ConnNormal; + + bool ValidDataNormal = false; + + // Attempt to read the caldavtest.auth file. + + ProcessConnectionDataFileResult DataFileResult = ProcessConnectionDataFile("caldavtest.auth", &ConnNormal); + if (DataFileResult == PROCESSCONNECTIONDATAFILE_OK){ + ValidDataNormal = true; + } + + ASSERT_EQ(true, ValidDataNormal); + + // Setup the connection. + + CalDAV ServerConnection; + + ServerConnection.SetupConnectionData(&ConnNormal); + + // Verify the connection settings. + + CalDAVStatus CalDAVStatus = ServerConnection.GetConnectionData(); + + ASSERT_EQ(CalDAVStatus.Hostname, ConnNormal.Hostname); + ASSERT_EQ(CalDAVStatus.Username, ConnNormal.Username); + ASSERT_EQ(CalDAVStatus.Port, ConnNormal.Port); + ASSERT_EQ(CalDAVStatus.Prefix, ConnNormal.Prefix); + ASSERT_EQ(CalDAVStatus.UseSSL, ConnNormal.UseSSL); + + // Connect to the server. + + CalDAVServerResult ConnResult = ServerConnection.Connect(); + + ASSERT_EQ(CALDAVQUERYRESULT_OK, ConnResult.Result); + ASSERT_EQ(200, ConnResult.HTTPCode); + ASSERT_EQ(CURLE_OK, ConnResult.Code); + + // Check for basic support of the calendar server. + + CalDAVServerSupport ConnSupport = ServerConnection.GetServerSupport(); + + ASSERT_EQ(true, ConnSupport.BasicSupport); + +} + TEST(CalDAV, ListCalendars){ CalDAVConnectionData ConnNormal; -- 2.39.5