Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / c_common / oic_string / test / linux / oic_string_tests.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 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 //
22 //*********************************************************************
23
24 // Defining _POSIX_C_SOURCE macro with 200809L (or greater) as value
25 // causes header files to expose definitions
26 // corresponding to the POSIX.1-2008 base
27 // specification (excluding the XSI extension).
28 // For POSIX.1-2008 base specification,
29 // Refer http://pubs.opengroup.org/stage7tc1/
30 //
31 // For this specific file, see use of usleep
32 #ifndef _POSIX_C_SOURCE
33 #define _POSIX_C_SOURCE 200809L
34 #endif // _POSIX_C_SOURCE
35
36 #include "gtest/gtest.h"
37
38 #include <oic_string.h>
39 #include <oic_malloc.h>
40
41 const char SENTINEL_VALUE = 127;
42 TEST(StringTests, StrdupNormalDup)
43 {
44     char param[] = "This is a normal parameter";
45
46     char* result = OICStrdup(param);
47
48     EXPECT_TRUE(result != NULL);
49
50     // ensure not the same pointer
51     EXPECT_NE(param, result);
52
53     EXPECT_STREQ(param, result);
54
55     OICFree(result);
56 }
57
58 // Tests a normal copy where the buffer is exactly long enough
59 TEST(StringTests, StrcpyExactSize)
60 {
61     char target[10];
62     memset(target, SENTINEL_VALUE, sizeof(target));
63     char source[] = "123456789";
64
65     char* result = OICStrcpy(target, sizeof(target), source);
66
67     EXPECT_EQ(target, result);
68     EXPECT_EQ(sizeof(target) - 1, strlen(target));
69     EXPECT_STREQ(source, result);
70 }
71
72 // Tests a normal copy where the buffer is exactly long enough
73 // Tests with what is in reality an oversized buffer to ensure that
74 // the buffer isn't over-written
75 TEST(StringTests, StrcpyExactSizeSentinel)
76 {
77     char target[10 + 5];
78     memset(target, SENTINEL_VALUE, sizeof(target));
79     char source[] = "123456789";
80
81     char* result = OICStrcpy(target, sizeof(target) - 5, source);
82
83     if (!result)
84     {
85         FAIL() << "OICStrcpy returned NULL";
86     }
87
88     EXPECT_EQ(target, result);
89     EXPECT_EQ(sizeof(target) - 1 - 5, strlen(target));
90     EXPECT_STREQ(source, result);
91
92     for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
93     {
94         EXPECT_EQ(SENTINEL_VALUE, result[i]);
95     }
96 }
97
98 // tests a copy where the source is smaller than the target
99 TEST(StringTests, StrcpyShorterSource)
100 {
101     char target[10];
102     memset(target, SENTINEL_VALUE, sizeof(target));
103     char source[] = "12345";
104
105     char* result = OICStrcpy(target, sizeof(target), source);
106
107     if (!result)
108     {
109         FAIL() << "OICStrcpy returned NULL";
110     }
111
112     EXPECT_EQ(target, result);
113     EXPECT_EQ(sizeof(source) - 1, strlen(result));
114     EXPECT_STREQ(source, result);
115
116     for(size_t i = sizeof(source); i < sizeof(target); ++i)
117     {
118         EXPECT_EQ(SENTINEL_VALUE, result[i]);
119     }
120 }
121
122 // tests a copy where the destination is larger than the target
123 TEST(StringTests, StrcpyShorterDestination)
124 {
125     char target[10];
126     memset(target, SENTINEL_VALUE, sizeof(target));
127     char source[] = "123456789012345";
128
129     char *result = OICStrcpy(target, sizeof(target), source);
130
131     if (!result)
132     {
133         FAIL() << "OICStrcpy returned NULL";
134     }
135
136     EXPECT_EQ(target, result);
137     EXPECT_EQ(sizeof(target) - 1, strlen(result));
138     EXPECT_STREQ("123456789", result);
139 }
140
141 // tests a copy where the destination is larger than the target
142 // Tests with what is in reality an oversized buffer to ensure that
143 // the buffer isn't over-written
144 TEST(StringTests, StrcpyShorterDestinationSentinel)
145 {
146     char target[10 + 5];
147     memset(target, SENTINEL_VALUE, sizeof(target));
148     char source[] = "123456789012345";
149
150     char *result = OICStrcpy(target, sizeof(target) - 5, source);
151
152     if (!result)
153     {
154         FAIL() << "OICStrcpy returned NULL";
155     }
156
157     EXPECT_EQ(target, result);
158     EXPECT_EQ(sizeof(target) - 1 - 5, strlen(result));
159     EXPECT_STREQ("123456789", result);
160
161     for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
162     {
163         EXPECT_EQ(SENTINEL_VALUE, result[i]);
164     }
165 }
166
167 // tests a copy where the source is of length 0
168 TEST(StringTests, StrcpyZeroSource)
169 {
170     char target[10];
171     memset(target, SENTINEL_VALUE, sizeof(target));
172     char source[] = "";
173
174     char *result = OICStrcpy(target, sizeof(target), source);
175
176     if (!result)
177     {
178         FAIL() << "OICStrcpy returned NULL";
179     }
180
181     EXPECT_EQ(target, result);
182     EXPECT_EQ(sizeof(source) - 1, strlen(result));
183     EXPECT_STREQ("", result);
184
185     for(size_t i = sizeof(source); i < sizeof(target); ++i)
186     {
187         EXPECT_EQ(SENTINEL_VALUE, result[i]);
188     }
189 }
190
191 // tests a copy where the destination is of length 0
192 TEST(StringTests, StrcpyZeroDestination)
193 {
194     char target[0];
195     char source[] = "123456789";
196
197     char *result = OICStrcpy(target, sizeof(target), source);
198
199     EXPECT_EQ(target, result);
200 }
201
202 // tests a copy where the destination is of length 0
203 // Tests with what is in reality an oversized buffer to ensure that
204 // the buffer isn't over-written
205 TEST(StringTests, StrcpyZeroDestinationSentinel)
206 {
207     char target[0 + 5];
208     memset(target, SENTINEL_VALUE, sizeof(target));
209     char source[] = "123456789";
210
211     char *result = OICStrcpy(target, sizeof(target) - 5, source);
212
213     if (!result)
214     {
215         FAIL() << "OICStrcpy returned NULL";
216     }
217
218     EXPECT_EQ(target, result);
219
220     for(size_t i = 0; i < sizeof(target); ++i)
221     {
222         EXPECT_EQ(SENTINEL_VALUE, result[i]);
223     }
224 }
225
226 // Tests a normal cat where the target has exactly enough room
227 TEST(StringTests, StrcatExactSize)
228 {
229     char target[10] = "Orig";
230     memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
231     char source[] = "12345";
232
233     char *result = OICStrcat(target, sizeof(target), source);
234     EXPECT_EQ(target, result);
235     EXPECT_EQ(sizeof(target) - 1, strlen(target));
236     EXPECT_STREQ("Orig12345", target);
237 }
238
239 // Tests a normal cat where the target has exactly enough room
240 // Tests with what is in reality an oversized buffer to ensure that
241 // the buffer isn't over-written
242 TEST(StringTests, StrcatExactSizeSentinel)
243 {
244     char target[10 + 5] = "Orig";
245     memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
246     char source[] = "12345";
247
248     char *result = OICStrcat(target, sizeof(target) - 5, source);
249     EXPECT_EQ(target, result);
250     EXPECT_EQ(sizeof(target) - 1 - 5, strlen(target));
251     EXPECT_STREQ("Orig12345", target);
252
253     for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
254     {
255         EXPECT_EQ(SENTINEL_VALUE, result[i]);
256     }
257 }
258
259 // tests a normal cat where the target has exactly enough room,
260 // except it is of strlen 0
261 TEST(StringTests, StrcatExactSizeEmptySourceString)
262 {
263     char target[10];
264     memset(target, SENTINEL_VALUE, sizeof(target));
265     target[0] = '\0';
266     char source[] = "123456789";
267
268     char *result = OICStrcat(target, sizeof(target), source);
269     EXPECT_EQ(target, result);
270     EXPECT_EQ(sizeof(target) - 1, strlen(target));
271     EXPECT_STREQ(source, target);
272 }
273 // tests a normal cat where the target has exactly enough room,
274 // except it is of strlen 0
275 // Tests with what is in reality an oversized buffer to ensure that
276 // the buffer isn't over-written
277 TEST(StringTests, StrcatExactSizeEmptySourceStringSentinel)
278 {
279     char target[10 + 5];
280     memset(target, SENTINEL_VALUE, sizeof(target));
281     target[0] = '\0';
282     char source[] = "123456789";
283
284     char *result = OICStrcat(target, sizeof(target) + 5, source);
285     EXPECT_EQ(target, result);
286     EXPECT_EQ(sizeof(target) - 1 - 5, strlen(target));
287     EXPECT_STREQ(source, target);
288
289     for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
290     {
291         EXPECT_EQ(SENTINEL_VALUE, result[i]);
292     }
293 }
294
295 // tests a normal cat where the target has extra room
296 TEST(StringTests, StrcatExtraRoom)
297 {
298     char target[10] = "Orig";
299     memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
300     char source[] = "12";
301
302     char *result = OICStrcat(target, sizeof(target), source);
303     EXPECT_EQ(target, result);
304     EXPECT_EQ(static_cast<size_t>(6), strlen(target));
305     EXPECT_STREQ("Orig12", target);
306
307     for(size_t i = sizeof("Orig12"); i < sizeof(target); ++i)
308     {
309         EXPECT_EQ(SENTINEL_VALUE, result[i]);
310     }
311 }
312
313 // Tests a normal cat where the target has insufficient room
314 TEST(StringTests, StrcatInsufficientRoom)
315 {
316     char target[10];
317     memset(target, SENTINEL_VALUE, sizeof(target));
318     target[0] = '\0';
319     char source[] = "1234567890123456";
320
321     char *result = OICStrcat(target, sizeof(target), source);
322     EXPECT_EQ(target, result);
323     EXPECT_EQ(sizeof(target) - 1, strlen(target));
324     EXPECT_STREQ("123456789", target);
325 }
326
327 // Tests a normal cat where the target has insufficient room
328 // Tests with what is in reality an oversized buffer to ensure that
329 // the buffer isn't over-written
330 TEST(StringTests, StrcatInsufficientRoomSentinel)
331 {
332     char target[10 + 5];
333     memset(target, SENTINEL_VALUE, sizeof(target));
334     target[0]= '\0';
335     char source[] = "1234567890123456";
336
337     char *result = OICStrcat(target, sizeof(target) - 5, source);
338     EXPECT_EQ(target, result);
339     EXPECT_EQ(sizeof(target) - 1 - 5, strlen(target));
340     EXPECT_STREQ("123456789", target);
341
342     for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
343     {
344         EXPECT_EQ(SENTINEL_VALUE, result[i]);
345     }
346 }
347
348 // Tests a normal cat where the target has zero room
349 TEST(StringTests, StrcatZeroRoom)
350 {
351     char target[10] = "Original1";
352     char source[] = "12345";
353
354     char *result = OICStrcat(target, sizeof(target), source);
355     EXPECT_EQ(target, result);
356     EXPECT_EQ(sizeof(target) - 1, strlen(target));
357     EXPECT_STREQ("Original1", target);
358 }
359
360 // Tests a normal cat where the target has zero room
361 // Tests with what is in reality an oversized buffer to ensure that
362 // the buffer isn't over-written
363 TEST(StringTests, StrcatZeroRoomSentinel)
364 {
365     char target[10 + 5] = "Original1";
366     memset(target + sizeof("Original1"), SENTINEL_VALUE, sizeof(target) - sizeof("Original1"));
367     char source[] = "12345";
368
369     char *result = OICStrcat(target, sizeof(target) - 5, source);
370     EXPECT_EQ(target, result);
371     EXPECT_EQ(sizeof(target) - 1 - 5, strlen(target));
372     EXPECT_STREQ("Original1", target);
373
374     for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
375     {
376         EXPECT_EQ(SENTINEL_VALUE, result[i]);
377     }
378 }
379
380 // Tests a cat where the source is zero length
381 TEST(StringTests, StrcatZeroSource)
382 {
383     char target[10] = "Orig";
384     memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
385     char source[] = "";
386
387     char *result = OICStrcat(target, sizeof(target), source);
388     EXPECT_EQ(target, result);
389     EXPECT_EQ(sizeof("Orig") - 1, strlen(target));
390     EXPECT_STREQ("Orig", target);
391
392     for(size_t i = sizeof("Orig"); i < sizeof(target); ++i)
393     {
394         EXPECT_EQ(SENTINEL_VALUE, result[i]);
395     }
396 }
397
398 // Tests a cat where the Destination is zero length
399 TEST(StringTests, StrcatZeroDestination)
400 {
401     char target[0];
402     char source[] = "12345";
403
404     char *result = OICStrcat(target, sizeof(target), source);
405     EXPECT_EQ(target, result);
406 }
407
408 // Tests a cat where the Destination is zero length
409 // Tests with what is in reality an oversized buffer to ensure that
410 // the buffer isn't over-written
411 TEST(StringTests, StrcatZeroDestinationSentinel)
412 {
413     char target[0 + 5];
414     memset(target, SENTINEL_VALUE, sizeof(target));
415     char source[] = "123456789";
416
417     char *result = OICStrcat(target, sizeof(target) - 5, source);
418
419     EXPECT_EQ(target, result);
420
421     for(size_t i = 0; i < sizeof(target); ++i)
422     {
423         EXPECT_EQ(SENTINEL_VALUE, result[i]);
424     }
425 }
426
427 // Tests a partial copy where the source length parameter is shorter
428 // than the string length
429 TEST(StringTests, StrcpyPartialShorterSourceLen)
430 {
431     char target[10];
432     memset(target, SENTINEL_VALUE, sizeof(target));
433     char source[] = "123456789";
434
435     char* result = OICStrcpyPartial(target, sizeof(target), source, strlen(source) - 5);
436
437     EXPECT_EQ(target, result);
438     EXPECT_EQ(strlen(source) - 5, strlen(target));
439     EXPECT_STREQ("1234", result);
440
441     for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
442     {
443         EXPECT_EQ(SENTINEL_VALUE, result[i]);
444     }
445 }
446
447 // Tests a partial copy where the source length parameter is equal
448 // to the string length
449 TEST(StringTests, StrcpyPartialEqualSourceLen)
450 {
451     char target[10];
452     memset(target, SENTINEL_VALUE, sizeof(target));
453     char source[] = "123456789";
454
455     char* result = OICStrcpyPartial(target, sizeof(target), source, strlen(source));
456
457     EXPECT_EQ(target, result);
458     EXPECT_EQ(sizeof(target) - 1, strlen(target));
459     EXPECT_STREQ(source, result);
460 }
461
462 // Tests a partial copy where the source length parameter is longer
463 // than the string length
464 TEST(StringTests, StrcpyPartialLongerSourceLen)
465 {
466     char target[10];
467     memset(target, SENTINEL_VALUE, sizeof(target));
468     char source[] = "123456789";
469
470     char* result = OICStrcpyPartial(target, sizeof(target), source, 99);
471
472     EXPECT_EQ(target, result);
473     EXPECT_EQ(sizeof(target) - 1, strlen(target));
474     EXPECT_STREQ(source, result);
475 }
476
477 // Tests a partial copy where the source length is zero
478 TEST(StringTests, StrcpyPartialZeroSourceLen)
479 {
480     char target[10];
481     memset(target, SENTINEL_VALUE, sizeof(target));
482     char source[] = "123456789";
483
484     char* result = OICStrcpyPartial(target, sizeof(target), source, 0);
485
486     EXPECT_EQ(target, result);
487
488     for(size_t i = 0; i < sizeof(target); ++i)
489     {
490         EXPECT_EQ(SENTINEL_VALUE, target[i]);
491     }
492 }
493
494 // Tests a partial cat where the source length parameter is shorter
495 // than the string length
496 TEST(StringTests, StrcatPartialShorterSourceLen)
497 {
498     char target[10] = "Orig";
499     memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
500     char source[] = "123456";
501
502     char* result = OICStrcatPartial(target, sizeof(target), source, strlen(source) - 3);
503
504     EXPECT_EQ(target, result);
505     EXPECT_EQ((sizeof("Orig") - 1) + (strlen(source) - 3), strlen(target));
506     EXPECT_STREQ("Orig123", result);
507
508     for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
509     {
510         EXPECT_EQ(SENTINEL_VALUE, result[i]);
511     }
512 }
513
514 // Tests a partial cat where the source length parameter is equal
515 // to the string length
516 TEST(StringTests, StrcatPartialEqualSourceLen)
517 {
518     char target[10] = "Orig";
519     memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
520     char source[] = "123";
521
522     char* result = OICStrcatPartial(target, sizeof(target), source, strlen(source));
523
524     EXPECT_EQ(target, result);
525     EXPECT_EQ((sizeof("Orig") - 1) + strlen(source), strlen(target));
526     EXPECT_STREQ("Orig123", result);
527
528     for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
529     {
530         EXPECT_EQ(SENTINEL_VALUE, result[i]);
531     }
532 }
533
534 // Tests a partial cat where the source length parameter is longer
535 // than the string length
536 TEST(StringTests, StrcatPartialLongerSourceLen)
537 {
538     char target[10] = "Orig";
539     memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
540     char source[] = "123";
541
542     char* result = OICStrcatPartial(target, sizeof(target), source, 99);
543
544     EXPECT_EQ(target, result);
545     EXPECT_EQ((sizeof("Orig") - 1) + strlen(source), strlen(target));
546     EXPECT_STREQ("Orig123", result);
547
548     for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
549     {
550         EXPECT_EQ(SENTINEL_VALUE, result[i]);
551     }
552 }
553
554 // Tests a partial cat where the source length is zero
555 TEST(StringTests, StrcatPartialZeroSourceLen)
556 {
557     char target[10] = "Orig";
558     memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
559     char source[] = "123";
560
561     char* result = OICStrcatPartial(target, sizeof(target), source, 0);
562
563     EXPECT_EQ(target, result);
564     EXPECT_EQ(sizeof("Orig") - 1, strlen(target));
565     EXPECT_STREQ("Orig", result);
566
567     for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
568     {
569         EXPECT_EQ(SENTINEL_VALUE, result[i]);
570     }
571 }