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 static const char SENTINEL_CHAR = '?';
44 TEST(StringTests, StrdupNormalDup)
46 char param[] = "This is a normal parameter";
48 char* result = OICStrdup(param);
50 EXPECT_TRUE(result != NULL);
52 // ensure not the same pointer
53 EXPECT_NE(param, result);
55 EXPECT_STREQ(param, result);
60 // Tests a normal copy where the buffer is exactly long enough
61 TEST(StringTests, StrcpyExactSize)
64 memset(target, SENTINEL_VALUE, sizeof(target));
65 char source[] = "123456789";
67 char* result = OICStrcpy(target, sizeof(target), source);
69 EXPECT_EQ(target, result);
70 EXPECT_EQ(sizeof(target) - 1, strlen(target));
71 EXPECT_STREQ(source, result);
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)
80 memset(target, SENTINEL_VALUE, sizeof(target));
81 char source[] = "123456789";
83 char* result = OICStrcpy(target, sizeof(target) - 5, source);
87 FAIL() << "OICStrcpy returned NULL";
90 EXPECT_EQ(target, result);
91 EXPECT_EQ(sizeof(target) - 1 - 5, strlen(target));
92 EXPECT_STREQ(source, result);
94 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
96 EXPECT_EQ(SENTINEL_VALUE, result[i]);
100 // tests a copy where the source is smaller than the target
101 TEST(StringTests, StrcpyShorterSource)
104 memset(target, SENTINEL_VALUE, sizeof(target));
105 char source[] = "12345";
107 char* result = OICStrcpy(target, sizeof(target), source);
111 FAIL() << "OICStrcpy returned NULL";
114 EXPECT_EQ(target, result);
115 EXPECT_EQ(sizeof(source) - 1, strlen(result));
116 EXPECT_STREQ(source, result);
118 for(size_t i = sizeof(source); i < sizeof(target); ++i)
120 EXPECT_EQ(SENTINEL_VALUE, result[i]);
124 // tests a copy where the destination is larger than the target
125 TEST(StringTests, StrcpyShorterDestination)
128 memset(target, SENTINEL_VALUE, sizeof(target));
129 char source[] = "123456789012345";
131 char *result = OICStrcpy(target, sizeof(target), source);
135 FAIL() << "OICStrcpy returned NULL";
138 EXPECT_EQ(target, result);
139 EXPECT_EQ(sizeof(target) - 1, strlen(result));
140 EXPECT_STREQ("123456789", result);
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)
149 memset(target, SENTINEL_VALUE, sizeof(target));
150 char source[] = "123456789012345";
152 char *result = OICStrcpy(target, sizeof(target) - 5, source);
156 FAIL() << "OICStrcpy returned NULL";
159 EXPECT_EQ(target, result);
160 EXPECT_EQ(sizeof(target) - 1 - 5, strlen(result));
161 EXPECT_STREQ("123456789", result);
163 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
165 EXPECT_EQ(SENTINEL_VALUE, result[i]);
169 // tests a copy where the source is of length 0
170 TEST(StringTests, StrcpyZeroSource)
173 memset(target, SENTINEL_VALUE, sizeof(target));
176 char *result = OICStrcpy(target, sizeof(target), source);
180 FAIL() << "OICStrcpy returned NULL";
183 EXPECT_EQ(target, result);
184 EXPECT_EQ(sizeof(source) - 1, strlen(result));
185 EXPECT_STREQ("", result);
187 for(size_t i = sizeof(source); i < sizeof(target); ++i)
189 EXPECT_EQ(SENTINEL_VALUE, result[i]);
193 // tests a copy where the destination is of length 0
194 TEST(StringTests, StrcpyZeroDestination)
196 char target[1] = { SENTINEL_CHAR };
197 char source[] = "123456789";
198 char beforeValue = target[0];
200 char *result = OICStrcpy(target, 0, source);
202 char afterValue = target[0];
203 EXPECT_EQ(target, result);
204 EXPECT_EQ(beforeValue, afterValue);
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)
213 memset(target, SENTINEL_VALUE, sizeof(target));
214 char source[] = "123456789";
216 char *result = OICStrcpy(target, sizeof(target) - 5, source);
220 FAIL() << "OICStrcpy returned NULL";
223 EXPECT_EQ(target, result);
225 for(size_t i = 0; i < sizeof(target); ++i)
227 EXPECT_EQ(SENTINEL_VALUE, result[i]);
231 // Tests a normal cat where the target has exactly enough room
232 TEST(StringTests, StrcatExactSize)
234 char target[10] = "Orig";
235 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
236 char source[] = "12345";
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);
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)
249 char target[10 + 5] = "Orig";
250 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
251 char source[] = "12345";
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);
258 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
260 EXPECT_EQ(SENTINEL_VALUE, result[i]);
264 // tests a normal cat where the target has exactly enough room,
265 // except it is of strlen 0
266 TEST(StringTests, StrcatExactSizeEmptySourceString)
269 memset(target, SENTINEL_VALUE, sizeof(target));
271 char source[] = "123456789";
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);
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)
285 memset(target, SENTINEL_VALUE, sizeof(target));
287 char source[] = "123456789";
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);
294 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
296 EXPECT_EQ(SENTINEL_VALUE, result[i]);
300 // tests a normal cat where the target has extra room
301 TEST(StringTests, StrcatExtraRoom)
303 char target[10] = "Orig";
304 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
305 char source[] = "12";
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);
312 for(size_t i = sizeof("Orig12"); i < sizeof(target); ++i)
314 EXPECT_EQ(SENTINEL_VALUE, result[i]);
318 // Tests a normal cat where the target has insufficient room
319 TEST(StringTests, StrcatInsufficientRoom)
322 memset(target, SENTINEL_VALUE, sizeof(target));
324 char source[] = "1234567890123456";
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);
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)
338 memset(target, SENTINEL_VALUE, sizeof(target));
340 char source[] = "1234567890123456";
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);
347 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
349 EXPECT_EQ(SENTINEL_VALUE, result[i]);
353 // Tests a normal cat where the target has zero room
354 TEST(StringTests, StrcatZeroRoom)
356 char target[10] = "Original1";
357 char source[] = "12345";
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);
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)
370 char target[10 + 5] = "Original1";
371 memset(target + sizeof("Original1"), SENTINEL_VALUE, sizeof(target) - sizeof("Original1"));
372 char source[] = "12345";
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);
379 for(size_t i = sizeof(target) - 5; i < sizeof(target); ++i)
381 EXPECT_EQ(SENTINEL_VALUE, result[i]);
385 // Tests a cat where the source is zero length
386 TEST(StringTests, StrcatZeroSource)
388 char target[10] = "Orig";
389 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
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);
397 for(size_t i = sizeof("Orig"); i < sizeof(target); ++i)
399 EXPECT_EQ(SENTINEL_VALUE, result[i]);
403 // Tests a cat where the Destination is zero length
404 TEST(StringTests, StrcatZeroDestination)
406 char target[1] = { SENTINEL_CHAR };
407 char source[] = "12345";
408 char beforeValue = target[0];
410 char *result = OICStrcat(target, 0, source);
412 char afterValue = target[0];
413 EXPECT_EQ(target, result);
414 EXPECT_EQ(beforeValue, afterValue);
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)
423 memset(target, SENTINEL_VALUE, sizeof(target));
424 char source[] = "123456789";
426 char *result = OICStrcat(target, sizeof(target) - 5, source);
428 EXPECT_EQ(target, result);
430 for(size_t i = 0; i < sizeof(target); ++i)
432 EXPECT_EQ(SENTINEL_VALUE, result[i]);
436 // Tests a partial copy where the source length parameter is shorter
437 // than the string length
438 TEST(StringTests, StrcpyPartialShorterSourceLen)
441 memset(target, SENTINEL_VALUE, sizeof(target));
442 char source[] = "123456789";
444 char* result = OICStrcpyPartial(target, sizeof(target), source, strlen(source) - 5);
446 EXPECT_EQ(target, result);
447 EXPECT_EQ(strlen(source) - 5, strlen(target));
448 EXPECT_STREQ("1234", result);
450 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
452 EXPECT_EQ(SENTINEL_VALUE, result[i]);
456 // Tests a partial copy where the source length parameter is equal
457 // to the string length
458 TEST(StringTests, StrcpyPartialEqualSourceLen)
461 memset(target, SENTINEL_VALUE, sizeof(target));
462 char source[] = "123456789";
464 char* result = OICStrcpyPartial(target, sizeof(target), source, strlen(source));
466 EXPECT_EQ(target, result);
467 EXPECT_EQ(sizeof(target) - 1, strlen(target));
468 EXPECT_STREQ(source, result);
471 // Tests a partial copy where the source length parameter is longer
472 // than the string length
473 TEST(StringTests, StrcpyPartialLongerSourceLen)
476 memset(target, SENTINEL_VALUE, sizeof(target));
477 char source[] = "123456789";
479 char* result = OICStrcpyPartial(target, sizeof(target), source, 99);
481 EXPECT_EQ(target, result);
482 EXPECT_EQ(sizeof(target) - 1, strlen(target));
483 EXPECT_STREQ(source, result);
486 // Tests a partial copy where the source length is zero
487 TEST(StringTests, StrcpyPartialZeroSourceLen)
490 memset(target, SENTINEL_VALUE, sizeof(target));
491 char source[] = "123456789";
493 char* result = OICStrcpyPartial(target, sizeof(target), source, 0);
495 EXPECT_EQ(target, result);
497 for(size_t i = 0; i < sizeof(target); ++i)
499 EXPECT_EQ(SENTINEL_VALUE, target[i]);
503 // Tests a partial cat where the source length parameter is shorter
504 // than the string length
505 TEST(StringTests, StrcatPartialShorterSourceLen)
507 char target[10] = "Orig";
508 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
509 char source[] = "123456";
511 char* result = OICStrcatPartial(target, sizeof(target), source, strlen(source) - 3);
513 EXPECT_EQ(target, result);
514 EXPECT_EQ((sizeof("Orig") - 1) + (strlen(source) - 3), strlen(target));
515 EXPECT_STREQ("Orig123", result);
517 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
519 EXPECT_EQ(SENTINEL_VALUE, result[i]);
523 // Tests a partial cat where the source length parameter is equal
524 // to the string length
525 TEST(StringTests, StrcatPartialEqualSourceLen)
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, strlen(source));
533 EXPECT_EQ(target, result);
534 EXPECT_EQ((sizeof("Orig") - 1) + strlen(source), strlen(target));
535 EXPECT_STREQ("Orig123", result);
537 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
539 EXPECT_EQ(SENTINEL_VALUE, result[i]);
543 // Tests a partial cat where the source length parameter is longer
544 // than the string length
545 TEST(StringTests, StrcatPartialLongerSourceLen)
547 char target[10] = "Orig";
548 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
549 char source[] = "123";
551 char* result = OICStrcatPartial(target, sizeof(target), source, 99);
553 EXPECT_EQ(target, result);
554 EXPECT_EQ((sizeof("Orig") - 1) + strlen(source), strlen(target));
555 EXPECT_STREQ("Orig123", result);
557 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
559 EXPECT_EQ(SENTINEL_VALUE, result[i]);
563 // Tests a partial cat where the source length is zero
564 TEST(StringTests, StrcatPartialZeroSourceLen)
566 char target[10] = "Orig";
567 memset(target + sizeof("Orig"), SENTINEL_VALUE, sizeof(target) - sizeof("Orig"));
568 char source[] = "123";
570 char* result = OICStrcatPartial(target, sizeof(target), source, 0);
572 EXPECT_EQ(target, result);
573 EXPECT_EQ(sizeof("Orig") - 1, strlen(target));
574 EXPECT_STREQ("Orig", result);
576 for(size_t i = strlen(target) + 1; i< sizeof(target); ++i)
578 EXPECT_EQ(SENTINEL_VALUE, result[i]);