1 // xestiacalendar_testcommon.cpp - Xestia Calendar Unit Test Common Functions.
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/>
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;