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 EXPECT_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);
85 FAIL() << "OICStrcpy returned NULL";
88 EXPECT_EQ(target, result);
89 EXPECT_EQ(sizeof(target) - 1 - 5, strlen(target));
90 EXPECT_STREQ(source, result);
92 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
94 EXPECT_EQ(SENTINEL_VALUE, result[i]);
98 // tests a copy where the source is smaller than the target
99 TEST(StringTests, StrcpyShorterSource)
102 memset(target, SENTINEL_VALUE, sizeof(target));
103 char source[] = "12345";
105 char* result = OICStrcpy(target, sizeof(target), source);
109 FAIL() << "OICStrcpy returned NULL";
112 EXPECT_EQ(target, result);
113 EXPECT_EQ(sizeof(source) - 1, strlen(result));
114 EXPECT_STREQ(source, result);
116 for(size_t i = sizeof(source); i < sizeof(target); ++i)
118 EXPECT_EQ(SENTINEL_VALUE, result[i]);
122 // tests a copy where the destination is larger than the target
123 TEST(StringTests, StrcpyShorterDestination)
126 memset(target, SENTINEL_VALUE, sizeof(target));
127 char source[] = "123456789012345";
129 char *result = OICStrcpy(target, sizeof(target), source);
133 FAIL() << "OICStrcpy returned NULL";
136 EXPECT_EQ(target, result);
137 EXPECT_EQ(sizeof(target) - 1, strlen(result));
138 EXPECT_STREQ("123456789", result);
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)
147 memset(target, SENTINEL_VALUE, sizeof(target));
148 char source[] = "123456789012345";
150 char *result = OICStrcpy(target, sizeof(target) - 5, source);
154 FAIL() << "OICStrcpy returned NULL";
157 EXPECT_EQ(target, result);
158 EXPECT_EQ(sizeof(target) - 1 - 5, strlen(result));
159 EXPECT_STREQ("123456789", result);
161 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
163 EXPECT_EQ(SENTINEL_VALUE, result[i]);
167 // tests a copy where the source is of length 0
168 TEST(StringTests, StrcpyZeroSource)
171 memset(target, SENTINEL_VALUE, sizeof(target));
174 char *result = OICStrcpy(target, sizeof(target), source);
178 FAIL() << "OICStrcpy returned NULL";
181 EXPECT_EQ(target, result);
182 EXPECT_EQ(sizeof(source) - 1, strlen(result));
183 EXPECT_STREQ("", result);
185 for(size_t i = sizeof(source); i < sizeof(target); ++i)
187 EXPECT_EQ(SENTINEL_VALUE, result[i]);
191 // tests a copy where the destination is of length 0
192 TEST(StringTests, StrcpyZeroDestination)
195 char source[] = "123456789";
197 char *result = OICStrcpy(target, sizeof(target), source);
199 EXPECT_EQ(target, result);
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)
208 memset(target, SENTINEL_VALUE, sizeof(target));
209 char source[] = "123456789";
211 char *result = OICStrcpy(target, sizeof(target) - 5, source);
215 FAIL() << "OICStrcpy returned NULL";
218 EXPECT_EQ(target, result);
220 for(size_t i = 0; i < sizeof(target); ++i)
222 EXPECT_EQ(SENTINEL_VALUE, result[i]);
226 // Tests a normal cat where the target has exactly enough room
227 TEST(StringTests, StrcatExactSize)
229 char target[10] = "Orig";
230 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
231 char source[] = "12345";
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);
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)
244 char target[10 + 5] = "Orig";
245 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
246 char source[] = "12345";
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);
253 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
255 EXPECT_EQ(SENTINEL_VALUE, result[i]);
259 // tests a normal cat where the target has exactly enough room,
260 // except it is of strlen 0
261 TEST(StringTests, StrcatExactSizeEmptySourceString)
264 memset(target, SENTINEL_VALUE, sizeof(target));
266 char source[] = "123456789";
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);
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)
280 memset(target, SENTINEL_VALUE, sizeof(target));
282 char source[] = "123456789";
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);
289 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
291 EXPECT_EQ(SENTINEL_VALUE, result[i]);
295 // tests a normal cat where the target has extra room
296 TEST(StringTests, StrcatExtraRoom)
298 char target[10] = "Orig";
299 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
300 char source[] = "12";
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);
307 for(size_t i = sizeof("Orig12"); i < sizeof(target); ++i)
309 EXPECT_EQ(SENTINEL_VALUE, result[i]);
313 // Tests a normal cat where the target has insufficient room
314 TEST(StringTests, StrcatInsufficientRoom)
317 memset(target, SENTINEL_VALUE, sizeof(target));
319 char source[] = "1234567890123456";
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);
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)
333 memset(target, SENTINEL_VALUE, sizeof(target));
335 char source[] = "1234567890123456";
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);
342 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
344 EXPECT_EQ(SENTINEL_VALUE, result[i]);
348 // Tests a normal cat where the target has zero room
349 TEST(StringTests, StrcatZeroRoom)
351 char target[10] = "Original1";
352 char source[] = "12345";
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);
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)
365 char target[10 + 5] = "Original1";
366 memset(target + sizeof("Original1"), SENTINEL_VALUE, sizeof(target) - sizeof("Original1"));
367 char source[] = "12345";
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);
374 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
376 EXPECT_EQ(SENTINEL_VALUE, result[i]);
380 // Tests a cat where the source is zero length
381 TEST(StringTests, StrcatZeroSource)
383 char target[10] = "Orig";
384 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
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);
392 for(size_t i = sizeof("Orig"); i < sizeof(target); ++i)
394 EXPECT_EQ(SENTINEL_VALUE, result[i]);
398 // Tests a cat where the Destination is zero length
399 TEST(StringTests, StrcatZeroDestination)
402 char source[] = "12345";
404 char *result = OICStrcat(target, sizeof(target), source);
405 EXPECT_EQ(target, result);
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)
414 memset(target, SENTINEL_VALUE, sizeof(target));
415 char source[] = "123456789";
417 char *result = OICStrcat(target, sizeof(target) - 5, source);
419 EXPECT_EQ(target, result);
421 for(size_t i = 0; i < sizeof(target); ++i)
423 EXPECT_EQ(SENTINEL_VALUE, result[i]);
427 // Tests a partial copy where the source length parameter is shorter
428 // than the string length
429 TEST(StringTests, StrcpyPartialShorterSourceLen)
432 memset(target, SENTINEL_VALUE, sizeof(target));
433 char source[] = "123456789";
435 char* result = OICStrcpyPartial(target, sizeof(target), source, strlen(source) - 5);
437 EXPECT_EQ(target, result);
438 EXPECT_EQ(strlen(source) - 5, strlen(target));
439 EXPECT_STREQ("1234", result);
441 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
443 EXPECT_EQ(SENTINEL_VALUE, result[i]);
447 // Tests a partial copy where the source length parameter is equal
448 // to the string length
449 TEST(StringTests, StrcpyPartialEqualSourceLen)
452 memset(target, SENTINEL_VALUE, sizeof(target));
453 char source[] = "123456789";
455 char* result = OICStrcpyPartial(target, sizeof(target), source, strlen(source));
457 EXPECT_EQ(target, result);
458 EXPECT_EQ(sizeof(target) - 1, strlen(target));
459 EXPECT_STREQ(source, result);
462 // Tests a partial copy where the source length parameter is longer
463 // than the string length
464 TEST(StringTests, StrcpyPartialLongerSourceLen)
467 memset(target, SENTINEL_VALUE, sizeof(target));
468 char source[] = "123456789";
470 char* result = OICStrcpyPartial(target, sizeof(target), source, 99);
472 EXPECT_EQ(target, result);
473 EXPECT_EQ(sizeof(target) - 1, strlen(target));
474 EXPECT_STREQ(source, result);
477 // Tests a partial copy where the source length is zero
478 TEST(StringTests, StrcpyPartialZeroSourceLen)
481 memset(target, SENTINEL_VALUE, sizeof(target));
482 char source[] = "123456789";
484 char* result = OICStrcpyPartial(target, sizeof(target), source, 0);
486 EXPECT_EQ(target, result);
488 for(size_t i = 0; i < sizeof(target); ++i)
490 EXPECT_EQ(SENTINEL_VALUE, target[i]);
494 // Tests a partial cat where the source length parameter is shorter
495 // than the string length
496 TEST(StringTests, StrcatPartialShorterSourceLen)
498 char target[10] = "Orig";
499 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
500 char source[] = "123456";
502 char* result = OICStrcatPartial(target, sizeof(target), source, strlen(source) - 3);
504 EXPECT_EQ(target, result);
505 EXPECT_EQ((sizeof("Orig") - 1) + (strlen(source) - 3), strlen(target));
506 EXPECT_STREQ("Orig123", result);
508 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
510 EXPECT_EQ(SENTINEL_VALUE, result[i]);
514 // Tests a partial cat where the source length parameter is equal
515 // to the string length
516 TEST(StringTests, StrcatPartialEqualSourceLen)
518 char target[10] = "Orig";
519 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
520 char source[] = "123";
522 char* result = OICStrcatPartial(target, sizeof(target), source, strlen(source));
524 EXPECT_EQ(target, result);
525 EXPECT_EQ((sizeof("Orig") - 1) + strlen(source), strlen(target));
526 EXPECT_STREQ("Orig123", result);
528 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
530 EXPECT_EQ(SENTINEL_VALUE, result[i]);
534 // Tests a partial cat where the source length parameter is longer
535 // than the string length
536 TEST(StringTests, StrcatPartialLongerSourceLen)
538 char target[10] = "Orig";
539 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
540 char source[] = "123";
542 char* result = OICStrcatPartial(target, sizeof(target), source, 99);
544 EXPECT_EQ(target, result);
545 EXPECT_EQ((sizeof("Orig") - 1) + strlen(source), strlen(target));
546 EXPECT_STREQ("Orig123", result);
548 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
550 EXPECT_EQ(SENTINEL_VALUE, result[i]);
554 // Tests a partial cat where the source length is zero
555 TEST(StringTests, StrcatPartialZeroSourceLen)
557 char target[10] = "Orig";
558 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
559 char source[] = "123";
561 char* result = OICStrcatPartial(target, sizeof(target), source, 0);
563 EXPECT_EQ(target, result);
564 EXPECT_EQ(sizeof("Orig") - 1, strlen(target));
565 EXPECT_STREQ("Orig", result);
567 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
569 EXPECT_EQ(SENTINEL_VALUE, result[i]);