1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 //*********************************************************************
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/
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
36 #include "gtest/gtest.h"
38 #include <oic_string.h>
39 #include <oic_malloc.h>
41 const char SENTINEL_VALUE = 127;
42 TEST(StringTests, StrdupNormalDup)
44 char param[] = "This is a normal parameter";
46 char* result = OICStrdup(param);
48 ASSERT_TRUE(result != NULL);
50 // ensure not the same pointer
51 EXPECT_NE(param, result);
53 EXPECT_STREQ(param, result);
58 // Tests a normal copy where the buffer is exactly long enough
59 TEST(StringTests, StrcpyExactSize)
62 memset(target, SENTINEL_VALUE, sizeof(target));
63 char source[] = "123456789";
65 char* result = OICStrcpy(target, sizeof(target), source);
67 EXPECT_EQ(target, result);
68 EXPECT_EQ(sizeof(target) - 1, strlen(target));
69 EXPECT_STREQ(source, result);
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)
78 memset(target, SENTINEL_VALUE, sizeof(target));
79 char source[] = "123456789";
81 char* result = OICStrcpy(target, sizeof(target) - 5, source);
83 EXPECT_EQ(target, result);
84 EXPECT_EQ(sizeof(target) - 1 - 5, strlen(target));
85 EXPECT_STREQ(source, result);
87 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
89 EXPECT_EQ(SENTINEL_VALUE, result[i]);
93 // tests a copy where the source is smaller than the target
94 TEST(StringTests, StrcpyShorterSource)
97 memset(target, SENTINEL_VALUE, sizeof(target));
98 char source[] = "12345";
100 char* result = OICStrcpy(target, sizeof(target), source);
102 EXPECT_EQ(target, result);
103 EXPECT_EQ(sizeof(source) - 1, strlen(result));
104 EXPECT_STREQ(source, result);
106 for(size_t i = sizeof(source); i < sizeof(target); ++i)
108 EXPECT_EQ(SENTINEL_VALUE, result[i]);
112 // tests a copy where the destination is larger than the target
113 TEST(StringTests, StrcpyShorterDestination)
116 memset(target, SENTINEL_VALUE, sizeof(target));
117 char source[] = "123456789012345";
119 char *result = OICStrcpy(target, sizeof(target), source);
121 EXPECT_EQ(target, result);
122 EXPECT_EQ(sizeof(target) - 1, strlen(result));
123 EXPECT_STREQ("123456789", result);
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)
132 memset(target, SENTINEL_VALUE, sizeof(target));
133 char source[] = "123456789012345";
135 char *result = OICStrcpy(target, sizeof(target) - 5, source);
137 EXPECT_EQ(target, result);
138 EXPECT_EQ(sizeof(target) - 1 - 5, strlen(result));
139 EXPECT_STREQ("123456789", result);
141 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
143 EXPECT_EQ(SENTINEL_VALUE, result[i]);
147 // tests a copy where the source is of length 0
148 TEST(StringTests, StrcpyZeroSource)
151 memset(target, SENTINEL_VALUE, sizeof(target));
154 char *result = OICStrcpy(target, sizeof(target), source);
156 EXPECT_EQ(target, result);
157 EXPECT_EQ(sizeof(source) - 1, strlen(result));
158 EXPECT_STREQ("", result);
160 for(size_t i = sizeof(source); i < sizeof(target); ++i)
162 EXPECT_EQ(SENTINEL_VALUE, result[i]);
166 // tests a copy where the destination is of length 0
167 TEST(StringTests, StrcpyZeroDestination)
170 char source[] = "123456789";
172 char *result = OICStrcpy(target, sizeof(target), source);
174 EXPECT_EQ(target, result);
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)
183 memset(target, SENTINEL_VALUE, sizeof(target));
184 char source[] = "123456789";
186 char *result = OICStrcpy(target, sizeof(target) - 5, source);
188 EXPECT_EQ(target, result);
190 for(size_t i = 0; i < sizeof(target); ++i)
192 EXPECT_EQ(SENTINEL_VALUE, result[i]);
196 // Tests a normal cat where the target has exactly enough room
197 TEST(StringTests, StrcatExactSize)
199 char target[10] = "Orig";
200 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
201 char source[] = "12345";
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);
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)
214 char target[10 + 5] = "Orig";
215 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
216 char source[] = "12345";
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);
223 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
225 EXPECT_EQ(SENTINEL_VALUE, result[i]);
229 // tests a normal cat where the target has exactly enough room,
230 // except it is of strlen 0
231 TEST(StringTests, StrcatExactSizeEmptySourceString)
234 memset(target, SENTINEL_VALUE, sizeof(target));
236 char source[] = "123456789";
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);
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)
250 memset(target, SENTINEL_VALUE, sizeof(target));
252 char source[] = "123456789";
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);
259 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
261 EXPECT_EQ(SENTINEL_VALUE, result[i]);
265 // tests a normal cat where the target has extra room
266 TEST(StringTests, StrcatExtraRoom)
268 char target[10] = "Orig";
269 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
270 char source[] = "12";
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);
277 for(size_t i = sizeof("Orig12"); i < sizeof(target); ++i)
279 EXPECT_EQ(SENTINEL_VALUE, result[i]);
283 // Tests a normal cat where the target has insufficient room
284 TEST(StringTests, StrcatInsufficientRoom)
287 memset(target, SENTINEL_VALUE, sizeof(target));
289 char source[] = "1234567890123456";
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);
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)
303 memset(target, SENTINEL_VALUE, sizeof(target));
305 char source[] = "1234567890123456";
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);
312 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
314 EXPECT_EQ(SENTINEL_VALUE, result[i]);
318 // Tests a normal cat where the target has zero room
319 TEST(StringTests, StrcatZeroRoom)
321 char target[10] = "Original1";
322 char source[] = "12345";
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);
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)
335 char target[10 + 5] = "Original1";
336 memset(target + sizeof("Original1"), SENTINEL_VALUE, sizeof(target) - sizeof("Original1"));
337 char source[] = "12345";
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);
344 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
346 EXPECT_EQ(SENTINEL_VALUE, result[i]);
350 // Tests a cat where the source is zero length
351 TEST(StringTests, StrcatZeroSource)
353 char target[10] = "Orig";
354 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
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);
362 for(size_t i = sizeof("Orig"); i < sizeof(target); ++i)
364 EXPECT_EQ(SENTINEL_VALUE, result[i]);
368 // Tests a cat where the Destination is zero length
369 TEST(StringTests, StrcatZeroDestination)
372 char source[] = "12345";
374 char *result = OICStrcat(target, sizeof(target), source);
375 EXPECT_EQ(target, result);
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)
384 memset(target, SENTINEL_VALUE, sizeof(target));
385 char source[] = "123456789";
387 char *result = OICStrcat(target, sizeof(target) - 5, source);
389 EXPECT_EQ(target, result);
391 for(size_t i = 0; i < sizeof(target); ++i)
393 EXPECT_EQ(SENTINEL_VALUE, result[i]);
397 // Tests a partial copy where the source length parameter is shorter
398 // than the string length
399 TEST(StringTests, StrcpyPartialShorterSourceLen)
402 memset(target, SENTINEL_VALUE, sizeof(target));
403 char source[] = "123456789";
405 char* result = OICStrcpyPartial(target, sizeof(target), source, strlen(source) - 5);
407 EXPECT_EQ(target, result);
408 EXPECT_EQ(strlen(source) - 5, strlen(target));
409 EXPECT_STREQ("1234", result);
411 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
413 EXPECT_EQ(SENTINEL_VALUE, result[i]);
417 // Tests a partial copy where the source length parameter is equal
418 // to the string length
419 TEST(StringTests, StrcpyPartialEqualSourceLen)
422 memset(target, SENTINEL_VALUE, sizeof(target));
423 char source[] = "123456789";
425 char* result = OICStrcpyPartial(target, sizeof(target), source, strlen(source));
427 EXPECT_EQ(target, result);
428 EXPECT_EQ(sizeof(target) - 1, strlen(target));
429 EXPECT_STREQ(source, result);
432 // Tests a partial copy where the source length parameter is longer
433 // than the string length
434 TEST(StringTests, StrcpyPartialLongerSourceLen)
437 memset(target, SENTINEL_VALUE, sizeof(target));
438 char source[] = "123456789";
440 char* result = OICStrcpyPartial(target, sizeof(target), source, 99);
442 EXPECT_EQ(target, result);
443 EXPECT_EQ(sizeof(target) - 1, strlen(target));
444 EXPECT_STREQ(source, result);
447 // Tests a partial copy where the source length is zero
448 TEST(StringTests, StrcpyPartialZeroSourceLen)
451 memset(target, SENTINEL_VALUE, sizeof(target));
452 char source[] = "123456789";
454 char* result = OICStrcpyPartial(target, sizeof(target), source, 0);
456 EXPECT_EQ(target, result);
458 for(size_t i = 0; i < sizeof(target); ++i)
460 EXPECT_EQ(SENTINEL_VALUE, target[i]);
464 // Tests a partial cat where the source length parameter is shorter
465 // than the string length
466 TEST(StringTests, StrcatPartialShorterSourceLen)
468 char target[10] = "Orig";
469 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
470 char source[] = "123456";
472 char* result = OICStrcatPartial(target, sizeof(target), source, strlen(source) - 3);
474 EXPECT_EQ(target, result);
475 EXPECT_EQ((sizeof("Orig") - 1) + (strlen(source) - 3), strlen(target));
476 EXPECT_STREQ("Orig123", result);
478 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
480 EXPECT_EQ(SENTINEL_VALUE, result[i]);
484 // Tests a partial cat where the source length parameter is equal
485 // to the string length
486 TEST(StringTests, StrcatPartialEqualSourceLen)
488 char target[10] = "Orig";
489 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
490 char source[] = "123";
492 char* result = OICStrcatPartial(target, sizeof(target), source, strlen(source));
494 EXPECT_EQ(target, result);
495 EXPECT_EQ((sizeof("Orig") - 1) + strlen(source), strlen(target));
496 EXPECT_STREQ("Orig123", result);
498 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
500 EXPECT_EQ(SENTINEL_VALUE, result[i]);
504 // Tests a partial cat where the source length parameter is longer
505 // than the string length
506 TEST(StringTests, StrcatPartialLongerSourceLen)
508 char target[10] = "Orig";
509 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
510 char source[] = "123";
512 char* result = OICStrcatPartial(target, sizeof(target), source, 99);
514 EXPECT_EQ(target, result);
515 EXPECT_EQ((sizeof("Orig") - 1) + strlen(source), strlen(target));
516 EXPECT_STREQ("Orig123", result);
518 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
520 EXPECT_EQ(SENTINEL_VALUE, result[i]);
524 // Tests a partial cat where the source length is zero
525 TEST(StringTests, StrcatPartialZeroSourceLen)
527 char target[10] = "Orig";
528 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
529 char source[] = "123";
531 char* result = OICStrcatPartial(target, sizeof(target), source, 0);
533 EXPECT_EQ(target, result);
534 EXPECT_EQ(sizeof("Orig") - 1, strlen(target));
535 EXPECT_STREQ("Orig", result);
537 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
539 EXPECT_EQ(SENTINEL_VALUE, result[i]);