replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / resource-encapsulation / src / common / primitiveResource / unittests / ResourceAttributesTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 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 <RCSResourceAttributes.h>
22 #include <ResourceAttributesConverter.h>
23 #include <ResourceAttributesUtils.h>
24
25 #include <gtest/gtest.h>
26
27 using namespace testing;
28 using namespace OIC::Service;
29
30 constexpr char KEY[]{ "key" };
31
32 class ResourceAttributesTest: public Test
33 {
34 public:
35     RCSResourceAttributes resourceAttributes;
36 };
37
38 TEST_F(ResourceAttributesTest, InitialSizeIsZero)
39 {
40     ASSERT_EQ(0U, resourceAttributes.size());
41     ASSERT_TRUE(resourceAttributes.empty());
42 }
43
44 TEST_F(ResourceAttributesTest, InsertWithSquareBracket)
45 {
46     resourceAttributes[KEY] = 1;
47
48     ASSERT_EQ(resourceAttributes[KEY], 1);
49 }
50
51 TEST_F(ResourceAttributesTest, ValueThrowsIfTypeDoesNotMatch)
52 {
53      resourceAttributes[KEY] = 1;
54     auto& valueRef = resourceAttributes[KEY];
55
56     ASSERT_THROW(valueRef.get< std::string >(), RCSBadGetException);
57 }
58
59 TEST_F(ResourceAttributesTest, GettingWithAtThrowsIfThereIsNoMatchedValue)
60 {
61     ASSERT_THROW(resourceAttributes.at(KEY), RCSInvalidKeyException);
62 }
63
64 TEST_F(ResourceAttributesTest, CopyingValueDoesNotShareState)
65 {
66     const char arbitraryStr[] { "ftryb457" };
67     resourceAttributes[KEY] = 1;
68
69     RCSResourceAttributes::Value copied { resourceAttributes[KEY] };
70     copied = arbitraryStr;
71
72     ASSERT_EQ(resourceAttributes[KEY], 1);
73     ASSERT_EQ(copied, arbitraryStr);
74 }
75
76 TEST_F(ResourceAttributesTest, IsNullWhenAssignmentNullptr)
77 {
78     resourceAttributes[KEY] = nullptr;
79
80     //ASSERT_EQ(resourceAttributes[KEY], nullptr);
81 }
82
83 TEST_F(ResourceAttributesTest, ValueChangedIfPutWithSameKey)
84 {
85     resourceAttributes[KEY] = "string";
86     resourceAttributes[KEY] = true;
87
88     ASSERT_EQ(resourceAttributes[KEY], true);
89 }
90
91 TEST_F(ResourceAttributesTest, ObjectIsEmptyAfterMoved)
92 {
93     resourceAttributes[KEY] = 1;
94
95     RCSResourceAttributes moved{ std::move(resourceAttributes) };
96
97     ASSERT_TRUE(resourceAttributes.empty());
98 }
99
100 TEST_F(ResourceAttributesTest, GettingWithAtThrowsAfterRemoved)
101 {
102     resourceAttributes[KEY] = 1;
103
104     resourceAttributes.erase(KEY);
105
106     ASSERT_THROW(resourceAttributes.at(KEY), RCSInvalidKeyException);
107 }
108
109 TEST_F(ResourceAttributesTest, NoDataErasedIfKeyDoesNotMatch)
110 {
111     ASSERT_FALSE(resourceAttributes.erase(KEY));
112 }
113
114 TEST_F(ResourceAttributesTest, ChangeValueWithAtGetter)
115 {
116     resourceAttributes[KEY] = 1;
117
118     resourceAttributes.at(KEY) = "after";
119
120     ASSERT_EQ(resourceAttributes[KEY], "after");
121 }
122
123 TEST_F(ResourceAttributesTest, CanHaveNestedResourceAttributes)
124 {
125     constexpr char nestedKey[]{ "nested" };
126     constexpr char value[]{ "nested_value" };
127
128     RCSResourceAttributes nested;
129     nested[nestedKey] = value;
130     resourceAttributes[KEY] = nested;
131
132     ASSERT_EQ(value, resourceAttributes[KEY].get<RCSResourceAttributes>()[nestedKey]);
133 }
134
135 TEST_F(ResourceAttributesTest, ToStringReturnsStringForValue)
136 {
137     resourceAttributes[KEY] = true;
138
139     ASSERT_EQ("true", resourceAttributes[KEY].toString());
140 }
141
142 TEST_F(ResourceAttributesTest, ToStringReturnsEmptyStringForNullValue)
143 {
144     resourceAttributes[KEY] = nullptr;
145
146     ASSERT_EQ("", resourceAttributes[KEY].toString());
147 }
148
149
150 class ResourceAttributesIteratorTest: public Test
151 {
152 public:
153     RCSResourceAttributes resourceAttributes;
154 };
155
156 TEST_F(ResourceAttributesIteratorTest, BeginEqualsEndWhenEmpty)
157 {
158     ASSERT_TRUE(resourceAttributes.begin() == resourceAttributes.end());
159 }
160
161 TEST_F(ResourceAttributesIteratorTest, CanIteratesWithForeach)
162 {
163     resourceAttributes["first"] = 1;
164     resourceAttributes["second"] = 2;
165
166     int count = 0;
167
168     for (auto& i : resourceAttributes)
169     {
170         i.key();
171         ++count;
172     }
173
174     ASSERT_EQ(2, count);
175 }
176
177 TEST_F(ResourceAttributesIteratorTest, IteratesWithRef)
178 {
179     const char arbitraryStr[] { "ftryb457" };
180     resourceAttributes[KEY] = 1;
181
182     for (auto& i : resourceAttributes)
183     {
184         i.value() = arbitraryStr;
185     }
186
187     ASSERT_EQ(resourceAttributes[KEY], arbitraryStr);
188 }
189
190 TEST_F(ResourceAttributesIteratorTest, IteratorIsCopyable)
191 {
192     RCSResourceAttributes::iterator it;
193
194     it = resourceAttributes.begin();
195
196     ASSERT_TRUE(it == resourceAttributes.begin());
197 }
198
199 TEST_F(ResourceAttributesIteratorTest, IteratorIndicateNextItemAfterIncreased)
200 {
201     resourceAttributes[KEY] = 1;
202
203     RCSResourceAttributes::iterator it = resourceAttributes.begin();
204
205     it++;
206
207     ASSERT_TRUE(it == resourceAttributes.end());
208 }
209
210 TEST_F(ResourceAttributesIteratorTest, IteratorCanBeConvertedIntoConstIterator)
211 {
212     resourceAttributes[KEY] = 1;
213     RCSResourceAttributes::const_iterator it { resourceAttributes.begin() };
214     it = resourceAttributes.cbegin();
215
216     it++;
217
218     ASSERT_TRUE(it == resourceAttributes.cend());
219 }
220
221 TEST_F(ResourceAttributesIteratorTest, ConstIteratorIsUsedForConst)
222 {
223     resourceAttributes[KEY] = 1;
224     const RCSResourceAttributes& constAttrs = resourceAttributes;
225
226     auto iter = constAttrs.begin();
227
228     ASSERT_TRUE((std::is_same<decltype(iter), RCSResourceAttributes::const_iterator>::value));
229 }
230
231
232 TEST(ResourceAttributesValueTest, MovedValueHasNull)
233 {
234     RCSResourceAttributes::Value one { 1 };
235     RCSResourceAttributes::Value another { std::move(one) };
236
237     //ASSERT_EQ(nullptr, one);
238 }
239
240 TEST(ResourceAttributesValueTest, MovedValueWithAssignmentHasNull)
241 {
242     RCSResourceAttributes::Value one { 1 };
243     RCSResourceAttributes::Value another;
244
245     another = std::move(one);
246
247     //ASSERT_EQ(nullptr, one);
248 }
249
250 TEST(ResourceAttributesValueTest, SameValuesAreEqual)
251 {
252     RCSResourceAttributes::Value one { 1 };
253     RCSResourceAttributes::Value another { 1 };
254
255     ASSERT_EQ(one, another);
256 }
257
258 TEST(ResourceAttributesValueTest, DifferentValuesAreNotEqual)
259 {
260     RCSResourceAttributes::Value one { 1 };
261     RCSResourceAttributes::Value another { 2 };
262
263     ASSERT_NE(one, another);
264 }
265
266 TEST(ResourceAttributesValueTest, ValuesCanBeSwapped)
267 {
268     constexpr int i { 0 };
269     constexpr char str[]{ "abc" };
270
271     RCSResourceAttributes::Value intValue { i };
272     RCSResourceAttributes::Value strValue { str };
273
274     intValue.swap(strValue);
275
276     ASSERT_EQ(str, intValue);
277     ASSERT_EQ(i, strValue);
278 }
279
280 TEST(ResourceAttributesTypeTest, TypeIdMatchesTypeOfValue)
281 {
282     RCSResourceAttributes::Value intValue { 0 };
283
284     ASSERT_EQ(intValue.getType().getId(), RCSResourceAttributes::TypeId::INT);
285 }
286
287 TEST(ResourceAttributesTypeTest, TypeCanBeConstructedFromValue)
288 {
289     RCSResourceAttributes::Value intValue { 1 };
290
291     RCSResourceAttributes::Type t = RCSResourceAttributes::Type::typeOf(0);
292
293     ASSERT_EQ(intValue.getType(), t);
294 }
295
296 TEST(ResourceAttributesTypeTest, DepthOfNonSequceTypeIsZero)
297 {
298     RCSResourceAttributes::Value intValue { 1 };
299
300     RCSResourceAttributes::Type t = intValue.getType();
301
302     ASSERT_EQ(0U, RCSResourceAttributes::Type::getDepth(t));
303 }
304
305 TEST(ResourceAttributesTypeTest, DepthOfSequceTypeIsNumberOfNested)
306 {
307     typedef std::vector< std::vector< std::vector< int > > > NestedVector;
308
309     RCSResourceAttributes::Type t = RCSResourceAttributes::Type::typeOf(NestedVector{ });
310
311     ASSERT_EQ(3U, RCSResourceAttributes::Type::getDepth(t));
312 }
313
314 TEST(ResourceAttributesTypeTest, BaseTypeOfNonSequceTypeIsItself)
315 {
316     RCSResourceAttributes::Value intValue { 1 };
317
318     RCSResourceAttributes::Type t = intValue.getType();
319
320     ASSERT_EQ(RCSResourceAttributes::TypeId::INT, RCSResourceAttributes::Type::getBaseTypeId(t));
321 }
322
323 TEST(ResourceAttributesTypeTest, BaseTypeOfSequceTypeIsMostNestedType)
324 {
325     typedef std::vector< std::vector< std::vector< RCSResourceAttributes > > > NestedVector;
326
327     RCSResourceAttributes::Type t = RCSResourceAttributes::Type::typeOf(NestedVector{ });
328
329     ASSERT_EQ(RCSResourceAttributes::TypeId::ATTRIBUTES,
330             RCSResourceAttributes::Type::getBaseTypeId(t));
331 }
332
333
334 TEST(ResourceAttributesConverterTest, OCRepresentationCanBeConvertedIntoResourceAttributes)
335 {
336     constexpr double value = 9876;
337     OC::OCRepresentation ocRep;
338     ocRep[KEY] = value;
339
340     RCSResourceAttributes resourceAttributes{
341         ResourceAttributesConverter::fromOCRepresentation(ocRep) };
342
343     ASSERT_TRUE(value == resourceAttributes[KEY]);
344 }
345
346 TEST(ResourceAttributesConverterTest, OCRepresentationCanBeConvertedIntoResourceAttributesTypeBinary)
347 {
348     static uint8_t binval[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8,
349                                0x9, 0x0, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF};
350
351     OCByteString value {binval, sizeof(binval)};
352     OC::OCRepresentation ocRep;
353     ocRep[KEY] = value;
354
355     RCSResourceAttributes resourceAttributes{
356         ResourceAttributesConverter::fromOCRepresentation(ocRep) };
357
358     auto rcsValue = resourceAttributes[KEY].get<RCSByteString>();
359     for (size_t i = 0; i < rcsValue.size(); ++i)
360     {
361         ASSERT_EQ(binval[i], rcsValue[i]);
362     }
363 }
364
365 TEST(ResourceAttributesConverterTest, ResourceAttributesCanBeConvertedIntoOCRepresentationTypeBinary)
366 {
367     static RCSByteString::DataType binval {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8,
368                                            0x9, 0x0, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF};
369     RCSResourceAttributes resourceAttributes;
370     RCSByteString value {binval};
371     resourceAttributes[KEY] = value;
372
373     OC::OCRepresentation ocRep{
374         ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
375
376     auto rcsValue = resourceAttributes[KEY].get<RCSByteString>();
377     auto ocValue = ocRep[KEY].getValue<OCByteString>();
378
379     ASSERT_EQ(rcsValue.size(), ocValue.len);
380     ASSERT_EQ(rcsValue.size(), binval.size());
381     ASSERT_EQ(binval.size(), ocValue.len);
382
383     for (size_t i = 0; i < rcsValue.size(); ++i)
384     {
385         ASSERT_EQ(ocValue.bytes[i], rcsValue[i]);
386     }
387 }
388
389 TEST(ResourceAttributesConverterTest, NestedOCRepresentationCanBeConvertedIntoResourceAttributes)
390 {
391     std::string nested_value { "nested" };
392     OC::OCRepresentation ocRep;
393     OC::OCRepresentation nested;
394     nested[KEY] = nested_value;
395     ocRep[KEY] = nested;
396
397     RCSResourceAttributes resourceAttributes{
398         ResourceAttributesConverter::fromOCRepresentation(ocRep) };
399
400     ASSERT_EQ(nested_value, resourceAttributes[KEY].get<RCSResourceAttributes>()[KEY]);
401 }
402
403
404 TEST(ResourceAttributesConverterTest, ResourceAttributesCanBeConvertedIntoOCRepresentation)
405 {
406     double value { 3453453 };
407     RCSResourceAttributes resourceAttributes;
408     resourceAttributes[KEY] = value;
409
410     OC::OCRepresentation ocRep{
411         ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
412
413     ASSERT_EQ(value, ocRep[KEY].getValue<double>());
414 }
415
416 TEST(ResourceAttributesConverterTest, NestedResourceAttributesCanBeConvertedIntoOCRepresentation)
417 {
418     std::string nested_value { "nested" };
419     RCSResourceAttributes resourceAttributes;
420     RCSResourceAttributes nested;
421     nested[KEY] = nested_value;
422     resourceAttributes[KEY] = nested;
423
424     OC::OCRepresentation ocRep{
425         ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
426
427     ASSERT_EQ(nested_value,
428             ocRep[KEY].getValue<OC::OCRepresentation>()[KEY].getValue<std::string>());
429 }
430
431 TEST(ResourceAttributesConverterTest, OCRepresentationNullTypeIsNullptrInResourceAttributes)
432 {
433     OC::OCRepresentation ocRep;
434     ocRep.setNULL(KEY);
435
436     RCSResourceAttributes resourceAttributes{
437         ResourceAttributesConverter::fromOCRepresentation(ocRep) };
438
439     //ASSERT_EQ(nullptr, resourceAttributes[KEY]);
440 }
441
442 TEST(ResourceAttributesConverterTest, OCRepresentationHasNullWhenResourceAttributeIsNullptr)
443 {
444     RCSResourceAttributes resourceAttributes;
445     resourceAttributes[KEY] = nullptr;
446
447     OC::OCRepresentation ocRep{
448         ResourceAttributesConverter::toOCRepresentation(resourceAttributes) };
449
450     ASSERT_TRUE(ocRep.isNULL(KEY));
451 }
452
453 TEST(ResourceAttributesConverterTest, ResourceAttributesWithSequenceTypeCanBeConverted)
454 {
455     typedef std::vector< std::vector< std::vector< int > > > NestedVector;
456     constexpr int value { 3453453 };
457
458     RCSResourceAttributes resourceAttributes;
459     NestedVector seq(10);
460     seq[1].resize(10, std::vector< int >(10));
461     seq[1][2][3] = value;
462     resourceAttributes[KEY] = seq;
463
464     NestedVector ocSeq = ResourceAttributesConverter::toOCRepresentation(resourceAttributes)[KEY];
465
466     ASSERT_EQ(ocSeq[1][2][3], value);
467 }
468
469 TEST(ResourceAttributesConverterTest, OCRepresentationWithSequenceTypeCanBeConverted)
470 {
471     typedef std::vector< std::vector< std::vector< std::string > > > NestedVector;
472     constexpr char value[]{ "some_string" };
473
474     OC::OCRepresentation ocRep;
475     NestedVector seq(10);
476     seq[1].resize(10, std::vector< std::string >(10));
477     seq[1][2][3] = value;
478     ocRep[KEY] = seq;
479
480     RCSResourceAttributes resourceAttributes{
481         ResourceAttributesConverter::fromOCRepresentation(ocRep) };
482
483     ASSERT_EQ(seq, resourceAttributes[KEY]);
484 }
485
486
487 class ResourceAttributesUtilTest: public Test
488 {
489 public:
490     RCSResourceAttributes resourceAttributes;
491
492 protected:
493     void SetUp()
494     {
495         resourceAttributes[KEY] = 1;
496     }
497 };
498
499 TEST_F(ResourceAttributesUtilTest, EmptyAttributesIsAcceptable)
500 {
501     ASSERT_TRUE(acceptableAttributes(resourceAttributes, RCSResourceAttributes()));
502 }
503
504 TEST_F(ResourceAttributesUtilTest, AttributesItselfIsAcceptable)
505 {
506     ASSERT_TRUE(acceptableAttributes(resourceAttributes, resourceAttributes));
507 }
508
509 TEST_F(ResourceAttributesUtilTest, UnknownKeyIsNotAcceptable)
510 {
511     RCSResourceAttributes newAttrs;
512     newAttrs["unknown"] = 1;
513
514     ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
515 }
516
517 TEST_F(ResourceAttributesUtilTest, DifferentTypeWithOriginalIsNotAcceptable)
518 {
519     RCSResourceAttributes newAttrs;
520     newAttrs[KEY] = "";
521
522     ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
523 }
524
525
526 TEST_F(ResourceAttributesUtilTest, DifferentTypeOfNestedAttributeIsNotAcceptable)
527 {
528     constexpr char KEY_NESTED_ATTR[]{ "nested" };
529     constexpr char KEY_NESTED_VALUE[]{ "nested_value" };
530
531     RCSResourceAttributes nested;
532     nested[KEY_NESTED_VALUE] = -99;
533     resourceAttributes[KEY_NESTED_ATTR] = nested;
534
535
536     RCSResourceAttributes newAttrs;
537     nested[KEY_NESTED_VALUE] = "abc";
538     newAttrs[KEY_NESTED_ATTR] = nested;
539
540     ASSERT_FALSE(acceptableAttributes(resourceAttributes, newAttrs));
541 }
542
543 TEST_F(ResourceAttributesUtilTest, ReplaceWillOverwriteOriginal)
544 {
545     constexpr char NEW_VALUE[]{ "newValue" };
546
547     RCSResourceAttributes newAttrs;
548     newAttrs[KEY] = NEW_VALUE;
549
550     replaceAttributes(resourceAttributes, newAttrs);
551
552     ASSERT_EQ(NEW_VALUE, resourceAttributes[KEY]);
553 }