| 1 | //===-- Unittests for strcpy ----------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "hdr/signal_macros.h" |
| 10 | #include "src/string/strcpy.h" |
| 11 | #include "test/UnitTest/Test.h" |
| 12 | |
| 13 | TEST(LlvmLibcStrCpyTest, EmptySrc) { |
| 14 | const char *empty = "" ; |
| 15 | char dest[4] = {'a', 'b', 'c', '\0'}; |
| 16 | |
| 17 | char *result = LIBC_NAMESPACE::strcpy(dest, empty); |
| 18 | ASSERT_EQ(dest, result); |
| 19 | ASSERT_STREQ(dest, result); |
| 20 | ASSERT_STREQ(dest, empty); |
| 21 | } |
| 22 | |
| 23 | TEST(LlvmLibcStrCpyTest, EmptyDest) { |
| 24 | const char *abc = "abc" ; |
| 25 | char dest[4]; |
| 26 | |
| 27 | char *result = LIBC_NAMESPACE::strcpy(dest, abc); |
| 28 | ASSERT_EQ(dest, result); |
| 29 | ASSERT_STREQ(dest, result); |
| 30 | ASSERT_STREQ(dest, abc); |
| 31 | } |
| 32 | |
| 33 | TEST(LlvmLibcStrCpyTest, OffsetDest) { |
| 34 | const char *abc = "abc" ; |
| 35 | char dest[7]; |
| 36 | |
| 37 | dest[0] = 'x'; |
| 38 | dest[1] = 'y'; |
| 39 | dest[2] = 'z'; |
| 40 | |
| 41 | char *result = LIBC_NAMESPACE::strcpy(dest + 3, abc); |
| 42 | ASSERT_EQ(dest + 3, result); |
| 43 | ASSERT_STREQ(dest + 3, result); |
| 44 | ASSERT_STREQ(dest, "xyzabc" ); |
| 45 | } |
| 46 | |
| 47 | #if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER) |
| 48 | |
| 49 | TEST(LlvmLibcStrCpyTest, CrashOnNullPtr) { |
| 50 | ASSERT_DEATH([]() { LIBC_NAMESPACE::strcpy(nullptr, nullptr); }, |
| 51 | WITH_SIGNAL(-1)); |
| 52 | } |
| 53 | |
| 54 | #endif // defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER) |
| 55 | |