1 // xestiacalendar_testcommon.cpp - Xestia Calendar Unit Test Common Functions.
3 // (c) 2016-2017 Xestia Software Development.
5 // This file is part of Xestia Calendar.
7 // Xestia Calendar 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 Calendar 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/>
21 #include "xestiacalendar_testcommon.h"
22 #include "../common/file.h"
24 ProcessConnectionDataFileResult ProcessConnectionDataFile(string dataFilename,
25 CalDAVConnectionData *connData){
27 ProcessConnectionDataFileResult processResult = PROCESSCONNECTIONDATAFILE_UNITTESTFAIL;
29 // Check if the file exists and return
30 // PROCESSCONNECTIONDATAFILE_MISSING if not.
32 if (!FileExists(dataFilename)){
33 return PROCESSCONNECTIONDATAFILE_MISSING;
37 string receivedStringData = "";
39 fileStream.open(dataFilename, ifstream::in);
41 if (fileStream.rdstate() & ifstream::failbit){
42 return PROCESSCONNECTIONDATAFILE_CANNOTOPEN;
45 if (fileStream.rdstate() & ifstream::badbit){
46 return PROCESSCONNECTIONDATAFILE_CANNOTOPEN;
51 char *bufferRead = new char[256];
53 while (!fileStream.eof()){
55 fileStream.getline(bufferRead, 256);
56 receivedStringData.append(bufferRead);
57 receivedStringData.append("\n");
64 bool skipMode = false;
65 bool equalFound = false;
66 bool quoteMode = false;
67 bool serverNameFound = false;
68 bool serverUserFound = false;
69 bool serverPassFound = false;
70 bool serverSSLFound = false;
71 bool serverPortFound = false;
72 bool serverPrefixFound = false;
74 int stringDataSize = receivedStringData.size();
79 while (seekCount < stringDataSize){
81 if (receivedStringData[seekCount] == '='){
85 } else if (receivedStringData[seekCount] == '\n'){
87 // Newline reached. Check for what type of
90 // Chceck that the equals sign has been found,
91 // there is a values in the property name
92 // and property value before doing anything.
94 if (equalFound == true &&
95 propertyName.size() > 0 &&
96 propertyValue.size() > 0){
98 if (propertyName == "server" && serverNameFound == false){
100 // Setup the server hostname.
102 connData->hostname = propertyValue;
103 serverNameFound = true;
105 } else if (propertyName == "port" && serverPortFound == false){
107 // Setup the server port.
110 bool portNumValid = true;
113 portNum = stoi(propertyValue);
116 catch(const invalid_argument &oor){
117 portNumValid = false;
120 // Port Number is valid so add to the
121 // CalDAVConnectionData handle.
123 if (portNumValid == true){
124 connData->port = portNum;
125 serverPortFound = true;
128 } else if (propertyName == "user" && serverUserFound == false){
130 // Setup the server user.
132 connData->username = propertyValue;
133 serverUserFound = true;
135 } else if (propertyName == "pass" && serverPassFound == false){
137 // Setup the server pass.
139 connData->password = propertyValue;
140 serverPassFound = true;
142 } else if (propertyName == "ssl" && serverSSLFound == false){
144 // Setup the server SSL status.
146 if (propertyValue == "true"){
147 connData->useSSL = true;
149 connData->useSSL = false;
152 serverSSLFound = true;
154 } else if (propertyName == "prefix" && serverPrefixFound == false){
156 // Setup the server prefix.
158 connData->prefix = propertyValue;
159 serverPrefixFound = true;
165 // Reset the variables.
168 propertyName.clear();
169 propertyValue.clear();
173 // No special character so add it to the
174 // Property name or value depending on
175 // if the equal sign has been found.
177 bufferChar = receivedStringData[seekCount];
179 if (equalFound == true){
180 propertyValue += bufferChar;
182 propertyName += bufferChar;
193 // Check that the CalDAV connection data object
194 // contains valid data.
196 bool calDAVConnDataResult = CalDAVObjectValidSettings(connData);
198 if (calDAVConnDataResult == false){
199 processResult = PROCESSCONNECTIONDATAFILE_INVALID;
201 processResult = PROCESSCONNECTIONDATAFILE_OK;
204 return processResult;