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