Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Version 0.06 release
[xestiacalendar/.git] / source / tests / xestiacalendar_testcommon.cpp
1 // xestiacalendar_testcommon.cpp - Xestia Calendar Unit Test Common Functions.
2 //
3 // (c) 2016-2017 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
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.
10 //
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.
15 //
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 <fstream>
21 #include "xestiacalendar_testcommon.h"
22 #include "../common/file.h"
24 ProcessConnectionDataFileResult ProcessConnectionDataFile(string dataFilename, 
25         CalDAVConnectionData *connData){
26                 
27         ProcessConnectionDataFileResult processResult = PROCESSCONNECTIONDATAFILE_UNITTESTFAIL;
28                 
29         // Check if the file exists and return 
30         // PROCESSCONNECTIONDATAFILE_MISSING if not.
31         
32         if (!FileExists(dataFilename)){
33                 return PROCESSCONNECTIONDATAFILE_MISSING;
34         }
35         
36         ifstream fileStream;
37         string receivedStringData = "";
38         
39         fileStream.open(dataFilename, ifstream::in);
40         
41         if (fileStream.rdstate() & ifstream::failbit){
42                 return PROCESSCONNECTIONDATAFILE_CANNOTOPEN;
43         }
45         if (fileStream.rdstate() & ifstream::badbit){
46                 return PROCESSCONNECTIONDATAFILE_CANNOTOPEN;
47         }
48         
49         // Process the file.
50         
51         char *bufferRead = new char[256];
52         
53         while (!fileStream.eof()){
54                 
55                 fileStream.getline(bufferRead, 256);
56                 receivedStringData.append(bufferRead);
57                 receivedStringData.append("\n");
58                 
59         }
60         
61         delete[] bufferRead;
62         
63         bool newLine = false;
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;
73         char bufferChar = 0;
74         int stringDataSize = receivedStringData.size();
75         int seekCount = 0;
76         string propertyName;
77         string propertyValue;
79         while (seekCount < stringDataSize){
81                 if (receivedStringData[seekCount] == '='){
82                 
83                         equalFound = true;              
84                 
85                 } else if (receivedStringData[seekCount] == '\n'){
86                 
87                         // Newline reached. Check for what type of
88                         // data it is.
89                 
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.
93                                                 
94                         if (equalFound == true &&
95                                 propertyName.size() > 0 &&
96                                 propertyValue.size() > 0){
97                         
98                                 if (propertyName == "server" && serverNameFound == false){
99                                 
100                                         // Setup the server hostname.
101                                 
102                                         connData->hostname = propertyValue;
103                                         serverNameFound = true;
104                                 
105                                 } else if (propertyName == "port" && serverPortFound == false){
106                                 
107                                         // Setup the server port.
108                                 
109                                         int portNum;
110                                         bool portNumValid = true;
111                                 
112                                         try{
113                                                 portNum = stoi(propertyValue);
114                                         }
115                                         
116                                         catch(const invalid_argument &oor){
117                                                 portNumValid = false;
118                                         }
119                                         
120                                         // Port Number is valid so add to the
121                                         // CalDAVConnectionData handle.
122                                         
123                                         if (portNumValid == true){
124                                                 connData->port = portNum;
125                                                 serverPortFound = true;
126                                         }
127                                 
128                                 } else if (propertyName == "user" && serverUserFound == false){
129                                 
130                                         // Setup the server user.
131                                         
132                                         connData->username = propertyValue;
133                                         serverUserFound = true;
134                                 
135                                 } else if (propertyName == "pass" && serverPassFound == false){
136                                 
137                                         // Setup the server pass.
138                                         
139                                         connData->password = propertyValue;
140                                         serverPassFound = true;                         
141                                 
142                                 } else if (propertyName == "ssl" && serverSSLFound == false){
143                                 
144                                         // Setup the server SSL status.
145                                         
146                                         if (propertyValue == "true"){
147                                                 connData->useSSL = true;
148                                         } else {
149                                                 connData->useSSL = false;                                       
150                                         }
151                                         
152                                         serverSSLFound = true;
153                                 
154                                 } else if (propertyName == "prefix" && serverPrefixFound == false){
155                                 
156                                         // Setup the server prefix.
157                                 
158                                         connData->prefix = propertyValue;
159                                         serverPrefixFound = true;
160                                 
161                                 }
162                                 
163                         }
164                         
165                         // Reset the variables.
166                 
167                         equalFound = false;
168                         propertyName.clear();
169                         propertyValue.clear();
170                 
171                 } else {
172                 
173                         // No special character so add it to the 
174                         // Property name or value depending on 
175                         // if the equal sign has been found.
176                 
177                         bufferChar = receivedStringData[seekCount];
178                 
179                         if (equalFound == true){
180                                 propertyValue += bufferChar;
181                         } else {
182                                 propertyName += bufferChar;
183                         }
184                         
185                         bufferChar = 0;
186                 
187                 }
189                 seekCount++;
191         }
192         
193         // Check that the CalDAV connection data object 
194         // contains valid data.
195         
196         bool calDAVConnDataResult = CalDAVObjectValidSettings(connData);
197         
198         if (calDAVConnDataResult == false){
199                 processResult = PROCESSCONNECTIONDATAFILE_INVALID;
200         } else {
201                 processResult = PROCESSCONNECTIONDATAFILE_OK;
202         }
203         
204         return processResult;
205                 
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy