Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / OCTestApps / OCServerTestApp.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6 #include "OCTestApp.h"
7 #include "stdlib.h"
8 #include "stdio.h"
9 #include <string.h>
10 #include <iostream>
11
12 // Headers Required For the Tests
13 #include "OCPlatform.h"
14 #include "OCApi.h"
15 #include "OCReflect.h"
16
17
18 using namespace std;
19 using namespace OC;
20 using namespace OC::OCReflect;
21
22 void Test_OCFunction_0(int functionIndex);
23 void Test_OCFunction_1(int functionIndex);
24 void Test_OCFunction_2(int functionIndex);
25 void Test_OCFunction_3(int functionIndex);
26 void Test_OCFunction_4(int functionIndex);
27 void Test_OCFunction_5(int functionIndex);
28 void Test_OCFunction_6(int functionIndex);
29 void Test_OCFunction_7(int functionIndex);
30 void Test_OCFunction_8(int functionIndex);
31 void Test_OCFunction_9(int functionIndex);
32 void Test_OCFunction_10(int functionIndex);
33 void Test_OCFunction_11(int functionIndex);
34 void Test_OCFunction_12(int functionIndex);
35
36 FunctionStruct              functionStruct[] =  {
37                                                     { &Test_OCFunction_0, "OCInProcServer", "DNRT" },
38                                                     { &Test_OCFunction_1, "OCOutOfProcServer", "DNRT" },
39                                                     { &Test_OCFunction_2, "OCInProcServer - registerResource", "DNRT" },
40                                                     { &Test_OCFunction_3, "OCOutOfProcServer - registerResource", "DNRT" },
41                                                     { &Test_OCFunction_4, "remote_resource", "DNRT" },
42                                                     { &Test_OCFunction_5, "remoteResource - operator", "DNRT" },
43                                                     { &Test_OCFunction_6, "method", "DNRT" },
44                                                     { &Test_OCFunction_7, "method - operator", "DNRT" },
45                                                     { &Test_OCFunction_8, "~OCInProcServer", "DNRT" },
46                                                     { &Test_OCFunction_9, "~OCOutOfProcServer", "DNRT" },
47                                                     { &Test_OCFunction_10, "~remote_resource", "DNRT" },
48                                                     { &Test_OCFunction_11, "~OCResource", "DNRT" },
49                                                     { &Test_OCFunction_12, "~method", "DNRT" },                                                    
50                                                 };
51
52 InProcServerWrapper        *pInProcServer = nullptr;
53 OutOfProcServerWrapper     *pOutProcServer = nullptr;
54 remote_resource            *pRemoteResource = nullptr;
55 OCResource                 *pResource = nullptr;
56 method                     *pMethod = nullptr;
57
58 int main(void)
59 {
60     int                     functionIndex = 0, functionNameSize = 0, functionNameLen = 0, functionCount = 0;
61     int                     resultSize = 0, resultLen = 0, lenOfResult = 0;
62     char                    rightSide[MaxResultSize], a;
63     
64
65     // Get The Size Of Each Of The Structure Elements
66     functionCount = ElementCount(functionStruct);    
67     
68     // Run The Tests  
69     Test_OCFunction_0(0);
70     Test_OCFunction_1(1);
71     Test_OCFunction_2(2);
72     Test_OCFunction_3(3);
73     Test_OCFunction_4(4);
74     Test_OCFunction_5(5);
75     Test_OCFunction_6(6);
76     Test_OCFunction_7(7);
77     Test_OCFunction_8(8);
78     Test_OCFunction_9(9);
79     Test_OCFunction_10(10);
80     Test_OCFunction_11(11);
81     Test_OCFunction_12(12);    
82     
83     for (functionIndex = 0; functionIndex < functionCount; functionIndex++)
84     {
85         functionNameLen = strlen((char*)&functionStruct[functionIndex].m_FunctionName);
86         if (functionNameLen > functionNameSize)
87             functionNameSize = functionNameLen;
88         
89         resultLen = strlen((char*)&functionStruct[functionIndex].m_Result);
90         if (resultLen > resultSize)
91             resultSize = resultLen;
92     }
93
94     for (functionIndex = 0; functionIndex < functionCount; functionIndex++)
95     {
96         // Set The Width Of This Line
97         cout.width(ResultColumn);
98
99         // Print The Function Name
100         cout << left << functionStruct[functionIndex].m_FunctionName;
101
102         // Print The Result
103         strcpy((char*)&rightSide, (char*)&functionStruct[functionIndex].m_Result);
104         lenOfResult = strlen((char*)&functionStruct[functionIndex].m_Result);
105         memset((char*)&rightSide[lenOfResult], ' ', MaxResultSize - lenOfResult);
106         rightSide[MaxResultSize - 1] = 0;
107         cout << right << rightSide << endl;
108     }
109     
110     cin >> a;  // Wait For Keyboard Input
111     return 0;
112 }
113
114 void Test_OCFunction_0(int functionIndex)
115 {
116     bool                    bResult = true;
117     PlatformConfig          cfg;
118     
119     try
120     {
121         cfg.serviceType = ServiceType::InProc;
122         cfg.mode = ModeType::Server;
123         cfg.ipAddress = "192.168.1.5";
124         cfg.port = 8080;
125         
126         pInProcServer = new InProcServerWrapper(cfg);   
127         
128             bResult = true;
129     }
130
131     catch (...)
132     {
133             bResult = false;
134     } 
135
136     if (bResult)
137     {
138             strcpy((char*)&functionStruct[functionIndex].m_Result, "Pass");
139     }
140     else
141     {
142             strcpy((char*)&functionStruct[functionIndex].m_Result, "Fail");
143     } 
144 }
145
146 void Test_OCFunction_1(int functionIndex)
147 {
148     bool                    bResult = true;
149     PlatformConfig          cfg;
150     
151     try
152     {
153         cfg.serviceType = ServiceType::InProc;
154         cfg.mode = ModeType::Server;
155         cfg.ipAddress = "192.168.1.5";
156         cfg.port = 8080;
157         
158         pOutProcServer = new OutOfProcServerWrapper(cfg);       
159         
160             bResult = true;
161     }
162
163     catch (...)
164     {
165             bResult = false;
166     } 
167
168     if (bResult)
169     {
170             strcpy((char*)&functionStruct[functionIndex].m_Result, "Pass");
171     }
172     else
173     {
174             strcpy((char*)&functionStruct[functionIndex].m_Result, "Fail");
175     } 
176 }
177
178 void Test_OCFunction_2(int functionIndex)
179 {
180     bool                    bResult = true;
181     string                  resName, resType;
182     named_property_binding_vector vc;
183     
184     try
185     {
186         if (pInProcServer != nullptr)
187         {
188             resName = "ResourceName";
189             resType = "ResourceType";
190             pInProcServer->registerResource(resName, resType, vc);        
191             bResult = true;
192         }
193     }
194
195     catch (...)
196     {
197             bResult = false;
198     } 
199
200     if (bResult)
201     {
202             strcpy((char*)&functionStruct[functionIndex].m_Result, "Pass");
203     }
204     else
205     {
206             strcpy((char*)&functionStruct[functionIndex].m_Result, "Fail");
207     } 
208 }
209
210 void Test_OCFunction_3(int functionIndex)
211 {
212     bool                    bResult = true;
213     string                  resName, resType;
214     named_property_binding_vector vc;
215     
216     try
217     {
218         if (pOutProcServer != nullptr)
219         {
220             resName = "ResourceName";
221             resType = "ResourceType";
222             pOutProcServer->registerResource(resName, resType, vc);        
223             bResult = true;
224         }
225     }
226
227     catch (...)
228     {
229             bResult = false;
230     } 
231
232     if (bResult)
233     {
234             strcpy((char*)&functionStruct[functionIndex].m_Result, "Pass");
235     }
236     else
237     {
238             strcpy((char*)&functionStruct[functionIndex].m_Result, "Fail");
239     } 
240 }
241
242 void Test_OCFunction_4(int functionIndex)
243 {
244     bool                    bResult = false;
245     string                  host, loc;
246     boost::property_tree::ptree   tree;
247     
248     try
249     {
250         host = "Host";
251         loc = "Nowhere Special";
252         pResource = new OCResource(host, tree);
253         pRemoteResource = new remote_resource(*pResource, loc);
254         bResult = true;
255     }
256
257     catch (...)
258     {
259 cout << "Ouch!" << endl;
260             bResult = false;
261     } 
262
263     if (bResult)
264     {
265             strcpy((char*)&functionStruct[functionIndex].m_Result, "Pass");
266     }
267     else
268     {
269             strcpy((char*)&functionStruct[functionIndex].m_Result, "Fail");
270     }
271 }
272
273 void Test_OCFunction_5(int functionIndex)
274 {
275     bool                    bResult = false; 
276     OC::OCReflect::entity   localEntity; 
277     string                  str;
278     
279     try
280     {
281         if (pRemoteResource)
282         {
283             str = "SomeEntity";
284             //localEntity = pRemoteResource;//(str);
285             bResult = true;
286         }        
287     }
288
289     catch (...)
290     {
291             bResult = false;
292     } 
293
294     if (bResult)
295     {
296             strcpy((char*)&functionStruct[functionIndex].m_Result, "Pass");
297     }
298     else
299     {
300             strcpy((char*)&functionStruct[functionIndex].m_Result, "Fail");
301     }
302 }
303
304 void Test_OCFunction_6(int functionIndex)
305 {
306     bool                    bResult = false;
307     string                  str;
308     
309     try
310     {
311         if (pResource != nullptr)
312         {
313             str = "SomeName";
314             pMethod = new method(*pResource, str);
315             bResult = true;
316         }
317     }
318
319     catch (...)
320     {
321             bResult = false;
322     } 
323
324     if (bResult)
325     {
326             strcpy((char*)&functionStruct[functionIndex].m_Result, "Pass");
327     }
328     else
329     {
330             strcpy((char*)&functionStruct[functionIndex].m_Result, "Fail");
331     }
332 }
333
334 void Test_OCFunction_7(int functionIndex)
335 {
336     bool                    bResult = false;
337     OC::OCReflect::tagged_property prop; 
338     
339     try
340     {
341         if (pMethod != nullptr)
342         {
343             //prop = pMethod(TS ...xs);
344             bResult = true;
345         }
346     }
347
348     catch (...)
349     {
350             bResult = false;
351     } 
352
353     if (bResult)
354     {
355             strcpy((char*)&functionStruct[functionIndex].m_Result, "Pass");
356     }
357     else
358     {
359             strcpy((char*)&functionStruct[functionIndex].m_Result, "Fail");
360     }
361 }
362
363 void Test_OCFunction_8(int functionIndex)
364 {
365     bool                    bResult = false;    
366     
367     try
368     {
369         if (pInProcServer != nullptr)
370         {
371             delete pInProcServer;
372             bResult = true;
373         }
374     }
375
376     catch (...)
377     {
378             bResult = false;
379     } 
380
381     if (bResult)
382     {
383             strcpy((char*)&functionStruct[functionIndex].m_Result, "Pass");
384     }
385     else
386     {
387             strcpy((char*)&functionStruct[functionIndex].m_Result, "Fail");
388     }
389 }
390
391 void Test_OCFunction_9(int functionIndex)
392 {
393     bool                    bResult = false;    
394     
395     try
396     {
397         if (pOutProcServer != nullptr)
398         {
399             delete pOutProcServer;
400             bResult = true;
401         }
402     }
403
404     catch (...)
405     {
406             bResult = false;
407     } 
408
409     if (bResult)
410     {
411             strcpy((char*)&functionStruct[functionIndex].m_Result, "Pass");
412     }
413     else
414     {
415             strcpy((char*)&functionStruct[functionIndex].m_Result, "Fail");
416     }
417 }
418
419 void Test_OCFunction_10(int functionIndex)
420 {
421     bool                    bResult = false;
422      
423     try
424     {
425         if (pRemoteResource != nullptr)
426         {
427             delete pRemoteResource;
428             bResult = true;
429         }
430     }
431
432     catch (...)
433     {
434             bResult = false;
435     } 
436
437     if (bResult)
438     {
439             strcpy((char*)&functionStruct[functionIndex].m_Result, "Pass");
440     }
441     else
442     {
443             strcpy((char*)&functionStruct[functionIndex].m_Result, "Fail");
444     }
445 }
446
447
448 void Test_OCFunction_11(int functionIndex)
449 {
450     bool                    bResult = false;
451      
452     try
453     {
454         if (pResource != nullptr)
455         {
456             delete pResource;
457             bResult = true;
458         }        
459     }
460
461     catch (...)
462     {
463             bResult = false;
464     } 
465
466     if (bResult)
467     {
468             strcpy((char*)&functionStruct[functionIndex].m_Result, "Pass");
469     }
470     else
471     {
472             strcpy((char*)&functionStruct[functionIndex].m_Result, "Fail");
473     }
474 }
475
476 void Test_OCFunction_12(int functionIndex)
477 {
478     bool                    bResult = false;
479     
480     try
481     {
482         if (pMethod != nullptr)
483         {
484             delete pMethod;
485             bResult = true;
486         }        
487     }
488
489     catch (...)
490     {
491             bResult = false;
492     } 
493
494     if (bResult)
495     {
496             strcpy((char*)&functionStruct[functionIndex].m_Result, "Pass");
497     }
498     else
499     {
500             strcpy((char*)&functionStruct[functionIndex].m_Result, "Fail");
501     }
502 }