resource: Add test for ConstructResourceObject with InvalidUri
[platform/upstream/iotivity.git] / resource / src / OCResource.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 "OCResource.h"
22 #include "OCUtilities.h"
23
24 #include <boost/lexical_cast.hpp>
25 #include <sstream>
26
27 namespace OC {
28
29 static const char COAP[] = "coap://";
30 static const char COAPS[] = "coaps://";
31 using OC::nil_guard;
32 using OC::result_guard;
33 using OC::checked_guard;
34
35 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
36                         const OCDevAddr& devAddr, const std::string& uri,
37                         const std::string& serverId, bool observable,
38                         const std::vector<std::string>& resourceTypes,
39                         const std::vector<std::string>& interfaces)
40  :  m_clientWrapper(clientWrapper), m_uri(uri),
41     m_resourceId(serverId, m_uri), m_devAddr(devAddr),
42     m_isObservable(observable), m_isCollection(false),
43     m_resourceTypes(resourceTypes), m_interfaces(interfaces),
44     m_observeHandle(nullptr)
45 {
46     m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
47                         != m_interfaces.end();
48
49     if (m_uri.empty() ||
50         resourceTypes.empty() ||
51         interfaces.empty()||
52         m_clientWrapper.expired())
53     {
54         throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
55                 interfaces.empty(), m_clientWrapper.expired(), false, false);
56     }
57 }
58
59 OCResource::OCResource(std::weak_ptr<IClientWrapper> clientWrapper,
60                         const std::string& host, const std::string& uri,
61                         const std::string& serverId,
62                         OCConnectivityType connectivityType, bool observable,
63                         const std::vector<std::string>& resourceTypes,
64                         const std::vector<std::string>& interfaces)
65  :  m_clientWrapper(clientWrapper), m_uri(uri),
66     m_resourceId(serverId, m_uri),
67     m_devAddr{ OC_DEFAULT_ADAPTER, OC_DEFAULT_FLAGS, 0, {0}, 0 },
68     m_isObservable(observable), m_isCollection(false),
69     m_resourceTypes(resourceTypes), m_interfaces(interfaces),
70     m_observeHandle(nullptr)
71 {
72     m_isCollection = std::find(m_interfaces.begin(), m_interfaces.end(), LINK_INTERFACE)
73                         != m_interfaces.end();
74
75     if (m_uri.empty() ||
76         resourceTypes.empty() ||
77         interfaces.empty()||
78         m_clientWrapper.expired())
79     {
80         throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
81                 interfaces.empty(), m_clientWrapper.expired(), false, false);
82     }
83
84     if (uri.length() == 1 && uri[0] == '/')
85     {
86         throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
87                 interfaces.empty(), m_clientWrapper.expired(), false, false);
88     }
89
90     if (uri[0] != '/')
91     {
92         throw ResourceInitException(m_uri.empty(), resourceTypes.empty(),
93                 interfaces.empty(), m_clientWrapper.expired(), false, false);
94     }
95
96     // construct the devAddr from the pieces we have
97     m_devAddr.adapter = static_cast<OCTransportAdapter>(connectivityType >> CT_ADAPTER_SHIFT);
98     m_devAddr.flags = static_cast<OCTransportFlags>(connectivityType & CT_MASK_FLAGS);
99     size_t len = host.length();
100     if (len >= MAX_ADDR_STR_SIZE)
101     {
102         throw std::length_error("host address is too long.");
103     }
104
105     this->setHost(host);
106 }
107
108 OCResource::~OCResource()
109 {
110 }
111
112 void OCResource::setHost(const std::string& host)
113 {
114     size_t prefix_len;
115
116     if(host.compare(0, sizeof(COAP) - 1, COAP) == 0)
117     {
118         prefix_len = sizeof(COAP) - 1;
119     }
120     else if(host.compare(0, sizeof(COAPS) - 1, COAPS) == 0)
121     {
122         prefix_len = sizeof(COAPS) - 1;
123         m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_SECURE);
124     }
125     else
126     {
127         throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
128             m_interfaces.empty(), m_clientWrapper.expired(), false, false);
129     }
130
131     // removed coap:// or coaps://
132     std::string host_token = host.substr(prefix_len);
133
134     if(host_token[0] == '[')
135     {
136         m_devAddr.flags = static_cast<OCTransportFlags>(m_devAddr.flags & OC_IP_USE_V6);
137
138         size_t found = host_token.find(']');
139
140         if(found == std::string::npos || found == 0)
141         {
142             throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
143                 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
144         }
145         // extract the ipaddress
146         std::string ip6Addr = host_token.substr(1, found-1);
147         ip6Addr.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
148         m_devAddr.addr[ip6Addr.length()] = '\0';
149         //skip ']' and ':' characters in host string
150         host_token = host_token.substr(found + 2);
151     }
152     else
153     {
154         size_t found = host_token.find(':');
155
156         if(found == std::string::npos || found == 0)
157         {
158             throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
159                 m_interfaces.empty(), m_clientWrapper.expired(), false, false);
160         }
161
162         std::string addrPart = host_token.substr(0, found);
163         addrPart.copy(m_devAddr.addr, sizeof(m_devAddr.addr));
164         m_devAddr.addr[addrPart.length()] = '\0';
165         //skip ':' character in host string
166         host_token = host_token.substr(found + 1);
167     }
168
169     int port = std::stoi(host_token);
170
171     if( port < 0 || port > UINT16_MAX )
172     {
173         throw ResourceInitException(m_uri.empty(), m_resourceTypes.empty(),
174             m_interfaces.empty(), m_clientWrapper.expired(), false, false);
175     }
176
177     m_devAddr.port = static_cast<uint16_t>(port);
178
179 }
180
181 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
182                               GetCallback attributeHandler, QualityOfService QoS)
183 {
184     return checked_guard(m_clientWrapper.lock(),
185                             &IClientWrapper::GetResourceRepresentation,
186                             m_devAddr, m_uri,
187                             queryParametersMap, m_headerOptions,
188                             attributeHandler, QoS);
189 }
190
191 OCStackResult OCResource::get(const QueryParamsMap& queryParametersMap,
192                               GetCallback attributeHandler)
193 {
194     QualityOfService defaultQos = OC::QualityOfService::NaQos;
195     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
196     return result_guard(get(queryParametersMap, attributeHandler, defaultQos));
197 }
198
199 OCStackResult OCResource::get(const std::string& resourceType,
200                      const std::string& resourceInterface, const QueryParamsMap& queryParametersMap,
201                      GetCallback attributeHandler)
202 {
203     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
204     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
205
206     return result_guard(get(resourceType, resourceInterface, queryParametersMap, attributeHandler, defaultQoS));
207 }
208
209 OCStackResult OCResource::get(const std::string& resourceType, const std::string& resourceInterface, const QueryParamsMap& queryParametersMap, GetCallback attributeHandler,
210         QualityOfService QoS)
211 {
212     QueryParamsMap mapCpy(queryParametersMap);
213
214     if(!resourceType.empty())
215     {
216         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
217     }
218
219     if(!resourceInterface.empty())
220     {
221         mapCpy[OC::Key::INTERFACESKEY]= resourceInterface;
222     }
223
224     return result_guard(get(mapCpy, attributeHandler, QoS));
225 }
226
227 OCStackResult OCResource::put(const OCRepresentation& rep,
228                               const QueryParamsMap& queryParametersMap, PutCallback attributeHandler,
229                               QualityOfService QoS)
230 {
231     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PutResourceRepresentation,
232                          m_devAddr, m_uri, rep, queryParametersMap,
233                          m_headerOptions, attributeHandler, QoS);
234 }
235
236 OCStackResult OCResource::put(const OCRepresentation& rep,
237                               const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
238 {
239     QualityOfService defaultQos = OC::QualityOfService::NaQos;
240     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
241     return result_guard(put(rep, queryParametersMap, attributeHandler, defaultQos));
242 }
243
244 OCStackResult OCResource::put(const std::string& resourceType,
245                               const std::string& resourceInterface, const OCRepresentation& rep,
246                               const QueryParamsMap& queryParametersMap,
247                               PutCallback attributeHandler)
248 {
249     QualityOfService defaultQos = OC::QualityOfService::NaQos;
250     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
251
252     return result_guard(put(resourceType, resourceInterface, rep, queryParametersMap,
253             attributeHandler, defaultQos));
254 }
255
256 OCStackResult OCResource::put(const std::string& resourceType,
257                               const std::string& resourceInterface, const OCRepresentation& rep,
258                               const QueryParamsMap& queryParametersMap,
259                               PutCallback attributeHandler,
260                               QualityOfService QoS)
261 {
262     QueryParamsMap mapCpy(queryParametersMap);
263
264     if(!resourceType.empty())
265     {
266         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
267     }
268
269     if(!resourceInterface.empty())
270     {
271         mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
272     }
273
274     return result_guard(put(rep, mapCpy, attributeHandler, QoS));
275 }
276
277 OCStackResult OCResource::post(const OCRepresentation& rep,
278                                const QueryParamsMap& queryParametersMap, PostCallback attributeHandler,
279                                QualityOfService QoS)
280 {
281     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
282                          m_devAddr, m_uri, rep, queryParametersMap,
283                          m_headerOptions, attributeHandler, QoS);
284 }
285
286 OCStackResult OCResource::post(const OCRepresentation& rep,
287                               const QueryParamsMap& queryParametersMap, PutCallback attributeHandler)
288 {
289     QualityOfService defaultQos = OC::QualityOfService::NaQos;
290     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
291     return result_guard(post(rep, queryParametersMap, attributeHandler, defaultQos));
292 }
293
294 OCStackResult OCResource::post(const std::string& resourceType,
295                                const std::string& resourceInterface, const OCRepresentation& rep,
296                                const QueryParamsMap& queryParametersMap,
297                                PostCallback attributeHandler)
298 {
299     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
300     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
301
302     return result_guard(post(resourceType, resourceInterface, rep, queryParametersMap, attributeHandler,
303             defaultQoS));
304 }
305
306 OCStackResult OCResource::post(const std::string& resourceType,
307                                const std::string& resourceInterface, const OCRepresentation& rep,
308                                const QueryParamsMap& queryParametersMap,
309                                PostCallback attributeHandler,
310                                QualityOfService QoS)
311 {
312     QueryParamsMap mapCpy(queryParametersMap);
313
314     if(!resourceType.empty())
315     {
316         mapCpy[OC::Key::RESOURCETYPESKEY]=resourceType;
317     }
318
319     if(!resourceInterface.empty())
320     {
321         mapCpy[OC::Key::INTERFACESKEY]=resourceInterface;
322     }
323
324     return result_guard(post(rep, mapCpy, attributeHandler, QoS));
325 }
326
327 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler, QualityOfService QoS)
328 {
329     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::DeleteResource,
330                          m_devAddr, m_uri, m_headerOptions, deleteHandler, QoS);
331 }
332
333 OCStackResult OCResource::deleteResource(DeleteCallback deleteHandler)
334 {
335     QualityOfService defaultQos = OC::QualityOfService::NaQos;
336     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQos);
337
338     return result_guard(deleteResource(deleteHandler, defaultQos));
339 }
340
341 OCStackResult OCResource::observe(ObserveType observeType,
342         const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler,
343         QualityOfService QoS)
344 {
345     if(m_observeHandle != nullptr)
346     {
347         return result_guard(OC_STACK_INVALID_PARAM);
348     }
349
350     return checked_guard(m_clientWrapper.lock(), &IClientWrapper::ObserveResource,
351                          observeType, &m_observeHandle, m_devAddr,
352                          m_uri, queryParametersMap, m_headerOptions,
353                          observeHandler, QoS);
354 }
355
356 OCStackResult OCResource::observe(ObserveType observeType,
357         const QueryParamsMap& queryParametersMap, ObserveCallback observeHandler)
358 {
359     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
360     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
361
362     return result_guard(observe(observeType, queryParametersMap, observeHandler, defaultQoS));
363 }
364
365 OCStackResult OCResource::cancelObserve()
366 {
367     QualityOfService defaultQoS = OC::QualityOfService::NaQos;
368     checked_guard(m_clientWrapper.lock(), &IClientWrapper::GetDefaultQos, defaultQoS);
369     return result_guard(cancelObserve(defaultQoS));
370 }
371
372 OCStackResult OCResource::cancelObserve(QualityOfService QoS)
373 {
374     if(m_observeHandle == nullptr)
375     {
376         return result_guard(OC_STACK_INVALID_PARAM);
377     }
378
379     OCStackResult result =  checked_guard(m_clientWrapper.lock(),
380             &IClientWrapper::CancelObserveResource,
381             m_observeHandle, "", m_uri, m_headerOptions, QoS);
382
383     if(result == OC_STACK_OK)
384     {
385         m_observeHandle = nullptr;
386     }
387
388     return result;
389 }
390
391 std::string OCResource::host() const
392 {
393     std::ostringstream ss;
394     if (m_devAddr.flags & OC_SECURE)
395     {
396         ss << COAPS;
397     }
398     else
399     {
400         ss << COAP;
401     }
402     if (m_devAddr.flags & OC_IP_USE_V6)
403     {
404         ss << '[' << m_devAddr.addr << ']';
405     }
406     else
407     {
408         ss << m_devAddr.addr;
409     }
410     if (m_devAddr.port)
411     {
412         ss << ':' << m_devAddr.port;
413     }
414     return ss.str();
415 }
416
417 std::string OCResource::uri() const
418 {
419     return m_uri;
420 }
421
422 OCConnectivityType OCResource::connectivityType() const
423 {
424     return static_cast<OCConnectivityType>(
425            (m_devAddr.adapter << CT_ADAPTER_SHIFT) | (m_devAddr.flags & CT_MASK_FLAGS));
426 }
427
428 bool OCResource::isObservable() const
429 {
430     return m_isObservable;
431 }
432
433 OCResourceIdentifier OCResource::uniqueIdentifier() const
434 {
435     return m_resourceId;
436 }
437
438 std::string OCResource::sid() const
439 {
440     return this->uniqueIdentifier().m_representation;
441 }
442
443 bool OCResource::operator==(const OCResource &other) const
444 {
445     return m_resourceId == other.m_resourceId;
446 }
447
448 bool OCResource::operator!=(const OCResource &other) const
449 {
450     return m_resourceId != other.m_resourceId;
451 }
452
453 bool OCResource::operator<(const OCResource &other) const
454 {
455     return m_resourceId < other.m_resourceId;
456 }
457
458 bool OCResource::operator>(const OCResource &other) const
459 {
460     return m_resourceId > other.m_resourceId;
461 }
462
463 bool OCResource::operator<=(const OCResource &other) const
464 {
465     return m_resourceId <= other.m_resourceId;
466 }
467
468 bool OCResource::operator>=(const OCResource &other) const
469 {
470     return m_resourceId >= other.m_resourceId;
471 }
472
473 OCResourceIdentifier::OCResourceIdentifier(const std::string& wireServerIdentifier,
474         const std::string& resourceUri)
475     :m_representation(wireServerIdentifier), m_resourceUri(resourceUri)
476 {
477 }
478
479 std::ostream& operator <<(std::ostream& os, const OCResourceIdentifier& ri)
480 {
481     os << ri.m_representation<<ri.m_resourceUri;
482
483     return os;
484 }
485
486 bool OCResourceIdentifier::operator==(const OCResourceIdentifier &other) const
487 {
488     return m_representation == other.m_representation
489         && m_resourceUri == other.m_resourceUri;
490 }
491
492 bool OCResourceIdentifier::operator!=(const OCResourceIdentifier &other) const
493 {
494     return !(*this == other);
495 }
496
497 bool OCResourceIdentifier::operator<(const OCResourceIdentifier &other) const
498 {
499     return m_resourceUri < other.m_resourceUri
500         || (m_resourceUri == other.m_resourceUri &&
501                 m_representation < other.m_representation);
502 }
503
504 bool OCResourceIdentifier::operator>(const OCResourceIdentifier &other) const
505 {
506     return *this != other && !(*this<other);
507 }
508
509 bool OCResourceIdentifier::operator<=(const OCResourceIdentifier &other) const
510 {
511     return !(*this > other);
512 }
513
514 bool OCResourceIdentifier::operator>=(const OCResourceIdentifier &other) const
515 {
516     return !(*this < other);
517 }
518
519 } // namespace OC
520