Merge test folder of test branch
[iotivity.git] / test / src / common / commonutil / c_cpp / ResourceHelper.cpp
1 /******************************************************************
2  *
3  * Copyright 2018 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include <sstream>
22 #include <ResourceHelper.h>
23 #ifdef HAVE_WINDOWS_H
24 #include "CommonUtil.h"
25 #include <windows.h>
26 #endif
27
28 ResourceHelper *ResourceHelper::s_resourceHelperInstance = NULL;
29 mutex ResourceHelper::s_mutex;
30
31 ResourceHelper::ResourceHelper(void)
32 {
33     initializeResultMap();
34
35     m_resourceFoundCallbackCount = 0;
36     m_groupFoundCallbackCount = 0;
37     m_configReceivedCallbackCount = 0;
38     m_presenceReceivedCallbackCount = 0;
39     m_presenceCallbackResult = OC_STACK_OK;
40     m_isResourceEnlisted = 0;
41     m_callbackErrorCode = 0;
42     m_actionSetCallbackCount = 0;
43
44     m_defaultRegionValue = "";
45     m_defaultTimeValue = "";
46     m_defaultCurrentTimeValue = "";
47     m_defaultNetworkValue = "";
48     m_defaultIPAddressValue = "";
49     m_defaultSecurityValue = "";
50     m_defaultModeValue = "";
51     m_defaultConfigurationValue = "";
52     m_defaultFactorySetValue = "";
53 }
54
55 ResourceHelper::~ResourceHelper()
56 {
57     if (s_resourceHelperInstance != NULL)
58     {
59         delete s_resourceHelperInstance;
60         s_resourceHelperInstance = NULL;
61     }
62 }
63
64 ResourceHelper *ResourceHelper::getInstance(void)
65 {
66     if (s_resourceHelperInstance == NULL)
67     {
68         s_mutex.lock();
69
70         if (s_resourceHelperInstance == NULL)
71         {
72             s_resourceHelperInstance = new ResourceHelper();
73
74         }
75
76         s_mutex.unlock();
77     }
78
79     return s_resourceHelperInstance;
80 }
81
82 void ResourceHelper::initializeResultMap(void)
83 {
84     m_resultMap[OC_STACK_OK] = "OC_STACK_OK";
85     m_resultMap[OC_STACK_RESOURCE_CREATED] = "OC_STACK_RESOURCE_CREATED";
86     m_resultMap[OC_STACK_RESOURCE_DELETED] = "OC_STACK_RESOURCE_DELETED";
87     m_resultMap[OC_STACK_CONTINUE] = "OC_STACK_CONTINUE";
88     m_resultMap[OC_STACK_INVALID_URI] = "OC_STACK_INVALID_URI";
89     m_resultMap[OC_STACK_INVALID_QUERY] = "OC_STACK_INVALID_QUERY";
90     m_resultMap[OC_STACK_INVALID_IP] = "OC_STACK_INVALID_IP";
91     m_resultMap[OC_STACK_INVALID_PORT] = "OC_STACK_INVALID_PORT";
92     m_resultMap[OC_STACK_INVALID_CALLBACK] = "OC_STACK_INVALID_CALLBACK";
93     m_resultMap[OC_STACK_INVALID_METHOD] = "OC_STACK_INVALID_METHOD";
94     m_resultMap[OC_STACK_INVALID_PARAM] = "OC_STACK_INVALID_PARAM";
95     m_resultMap[OC_STACK_INVALID_OBSERVE_PARAM] = "OC_STACK_INVALID_OBSERVE_PARAM";
96     m_resultMap[OC_STACK_NO_MEMORY] = "OC_STACK_NO_MEMORY";
97     m_resultMap[OC_STACK_COMM_ERROR] = "OC_STACK_COMM_ERROR";
98     m_resultMap[OC_STACK_NOTIMPL] = "OC_STACK_NOTIMPL";
99     m_resultMap[OC_STACK_NO_RESOURCE] = "OC_STACK_NO_RESOURCE";
100     m_resultMap[OC_STACK_RESOURCE_ERROR] = "OC_STACK_RESOURCE_ERROR";
101     m_resultMap[OC_STACK_SLOW_RESOURCE] = "OC_STACK_SLOW_RESOURCE";
102     m_resultMap[OC_STACK_NO_OBSERVERS] = "OC_STACK_NO_OBSERVERS";
103     m_resultMap[OC_STACK_OBSERVER_NOT_FOUND] = "OC_STACK_OBSERVER_NOT_FOUND";
104     m_resultMap[OC_STACK_PRESENCE_STOPPED] = "OC_STACK_PRESENCE_STOPPED";
105     m_resultMap[OC_STACK_PRESENCE_TIMEOUT] = "OC_STACK_PRESENCE_TIMEOUT";
106     m_resultMap[OC_STACK_PRESENCE_DO_NOT_HANDLE] = "OC_STACK_PRESENCE_DO_NOT_HANDLE";
107     m_resultMap[OC_STACK_VIRTUAL_DO_NOT_HANDLE] = "OC_STACK_VIRTUAL_DO_NOT_HANDLE";
108     m_resultMap[OC_STACK_INVALID_OPTION] = "OC_STACK_INVALID_OPTION";
109     m_resultMap[OC_STACK_MALFORMED_RESPONSE] = "OC_STACK_MALFORMED_RESPONSE";
110     m_resultMap[OC_STACK_PERSISTENT_BUFFER_REQUIRED] = "OC_STACK_PERSISTENT_BUFFER_REQUIRED";
111     m_resultMap[OC_STACK_INVALID_REQUEST_HANDLE] = "OC_STACK_INVALID_REQUEST_HANDLE";
112     m_resultMap[OC_STACK_ERROR] = "OC_STACK_ERROR";
113 }
114
115 bool ResourceHelper::waitForResourceFound(vector< shared_ptr< OCResource > > &foundResourceList,
116         int timeOut)
117 {
118     bool isTimeOut = false;
119     int passedTime = 0;
120     while (foundResourceList.empty())
121     {
122         waitInSecond(CALLBACK_WAIT_MIN);
123         passedTime++;
124         cout << "Waited " << passedTime << " seconds for finding resources" << endl;
125
126         if (passedTime > timeOut)
127         {
128             isTimeOut = true;
129             break;
130         }
131     }
132
133     return isTimeOut;
134 }
135
136 bool ResourceHelper::IsCallbackTimeoutOccured(int &callbackMonitor, int timeOut)
137 {
138     bool isTimeOut = false;
139     clock_t startTime = clock();
140     while (callbackMonitor == 0)
141     {
142         waitInSecond(1);
143
144         if ((clock() - startTime) / 1000 > timeOut)
145         {
146             isTimeOut = true;
147             break;
148         }
149     }
150
151     return isTimeOut;
152 }
153
154 bool ResourceHelper::containsResource(vector< shared_ptr< OCResource > > resourceList,
155         vector< string > resourceTypeList, string &errorMessage)
156 {
157     bool isAllEnlisted = true;
158     errorMessage += "TestCase Failed.";
159
160     for (auto resourceType : resourceTypeList)
161     {
162         bool isEnlisted = false;
163         for (auto resource : resourceList)
164         {
165             string fromType = "";
166             string uri = "";
167             string fromUri = "";
168             QueryParamsMap paramsMap;
169             fromType = resourceType.substr(resourceType.find_last_of(".") + 1, string::npos);
170             uri = resource->uri();
171             fromUri = uri.substr(uri.find_last_of("/") + 1,
172                     uri.find_last_of("-") - uri.find_last_of("/") - 1);
173
174             if (fromUri.compare(fromType) == 0)
175             {
176                 isEnlisted = true;
177                 cout << "Resource Type Found: " << resourceType << endl;
178                 break;
179             }
180         }
181
182         if (!isEnlisted)
183         {
184             isAllEnlisted = false;
185             errorMessage += " The following resource type not found: " + resourceType + "\n";
186
187         }
188
189     }
190
191     return isAllEnlisted;
192 }
193
194 void ResourceHelper::printRepresentation(OCRepresentation rep)
195 {
196     string uri = rep.getUri();
197     if (uri.compare("") == 0)
198     {
199         cout << "The representation has no uri." << endl;
200     }
201     else
202     {
203         cout << "The representation has uri: " << uri << endl;
204     }
205
206     if (rep.hasAttribute(INTERFACE_KEY))
207     {
208         cout << "The representation has interface: " << rep.getValueToString(INTERFACE_KEY) << endl;
209     }
210
211     if (rep.hasAttribute(RESOURCE_TYPE_KEY))
212     {
213         cout << "The representation has interface: " << rep.getValueToString(RESOURCE_TYPE_KEY)
214                 << endl;
215     }
216
217     if(rep.getPayload())
218     {
219         printPayload(rep.getPayload(), rep);
220     }
221 }
222
223 void ResourceHelper::printPayload(OCRepPayload* incomingPayload, OCRepresentation rep, int level)
224 {
225     bool hasInterface = false;
226     bool hasType = false;
227     level++;
228
229     OCStringLL* interfaces = incomingPayload->interfaces;
230     string interfaceInfo = "The representation Interfaces are: [";
231     while (interfaces)
232     {
233         hasInterface = true;
234
235         interfaceInfo += string(interfaces->value) + " ";
236         interfaces = interfaces->next;
237     }
238
239     if (hasInterface)
240     {
241         cout << interfaceInfo << "]" << endl;
242     }
243
244     OCStringLL* types = incomingPayload->types;
245     string typeInfo = "The representation Types are: [";
246     while (types)
247     {
248         hasType = true;
249
250         typeInfo += string(types->value) + " ";
251         types = types->next;
252     }
253
254     if (hasType)
255     {
256         cout << typeInfo << "]" << endl;
257     }
258
259     if (level == 1)
260     {
261         cout << "The representation has following Attributes: " << endl;
262     }
263
264     while (incomingPayload)
265     {
266         OCRepPayloadValue *repValue = incomingPayload->values;
267         while (repValue)
268         {
269             string value = "";
270             string tabValue = "";
271             for (int i = 0; i < level; i++)
272             {
273                 tabValue += "\t\t\t";
274             }
275             cout << tabValue << repValue->name << " : ";
276
277             if (repValue->type == OCRepPayloadPropType::OCREP_PROP_INT)
278             {
279 #ifdef __LINUX__
280                 value = to_string(repValue->i);
281 #endif
282
283 #if defined(__ANDROID__) || defined(__TIZEN__)
284                 std::stringstream strstream;
285                 strstream << repValue->i;
286                 strstream >> value;
287 #endif
288             }
289             if (repValue->type == OCRepPayloadPropType::OCREP_PROP_DOUBLE)
290             {
291                 value = to_string(repValue->d);
292             }
293             if (repValue->type == OCRepPayloadPropType::OCREP_PROP_BOOL)
294             {
295                 value = repValue->b ? "true" : "false";
296             }
297             if (repValue->type == OCRepPayloadPropType::OCREP_PROP_STRING)
298             {
299                 value = string(repValue->str);
300             }
301             if (repValue->type == OCRepPayloadPropType::OCREP_PROP_ARRAY)
302             {
303                 if (repValue->arr.type == OCRepPayloadPropType::OCREP_PROP_OBJECT)
304                 {
305                     cout << "[" << endl;
306                     size_t arraySize = repValue->arr.dimensions[0];
307                     OCRepPayload** objectArray = repValue->arr.objArray;
308                     for (size_t sz = 0; sz < arraySize; sz++)
309                     {
310                         OCRepPayload* payload = *objectArray;
311                         cout << "\t\t\t\t\t {" << endl;
312                         printPayload(payload, rep, level);
313                         cout << "\t\t\t\t\t }" << endl;
314                         objectArray++;
315                     }
316                     cout << "\t\t\t\t ]" << endl;
317                 }
318                 else
319                 {
320                     value = rep.getValueToString(repValue->name);
321                 }
322             }
323             if (repValue->type == OCRepPayloadPropType::OCREP_PROP_OBJECT)
324             {
325                 cout << "\t{" << endl;
326                 printPayload(repValue->obj, rep, level);
327                 cout << "\t}" << endl;
328             }
329             if (repValue->type == OCRepPayloadPropType::OCREP_PROP_NULL)
330             {
331                 value = "[]";
332             }
333
334             cout << value << endl;
335             repValue = repValue->next;
336         }
337         incomingPayload = incomingPayload->next;
338     }
339 }
340
341 void ResourceHelper::waitInSecond(unsigned int seconds)
342 {
343     sleep(seconds);
344 }
345
346 void ResourceHelper::duplicateString(char ** targetString, string sourceString)
347 {
348     *targetString = new char[sourceString.length() + 1];
349     strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
350 }
351
352 bool ResourceHelper::containsElement(vector< string > list, string target)
353 {
354     bool isFound = false;
355     for (string element : list)
356     {
357         if (element.find(target) != string::npos)
358         {
359             isFound = true;
360             break;
361         }
362     }
363     return isFound;
364 }
365
366 string ResourceHelper::getOnlyTCPHost(vector< string > allHosts)
367 {
368     string tcpHost = "";
369     for (string host : allHosts)
370     {
371         cout << "Found a host: " << host << endl;
372         if (host.find("tcp") != string::npos)
373         {
374             tcpHost = host;
375             break;
376         }
377     }
378
379     return tcpHost;
380 }
381
382 string ResourceHelper::getOnlySecuredHost(vector< string > allHosts)
383 {
384     string securedHost = "";
385     for (string host : allHosts)
386     {
387         cout << "Found a host: " << host << endl;
388         if (host.find(SECURED_ENDPOINT_KEY) != string::npos)
389         {
390             securedHost = host;
391             break;
392         }
393     }
394
395     return securedHost;
396 }
397
398 string ResourceHelper::executeCommand(string cmd) {
399     array<char, 128> buffer;
400     string result;
401     shared_ptr<FILE> pipe(popen(cmd.c_str(), "r"), pclose);
402     if (!pipe) throw std::runtime_error("popen() failed!");
403     while (!feof(pipe.get())) {
404         if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
405             result += buffer.data();
406     }
407     return result;
408 }