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