1 | //===-- Unittests for stpncpy ---------------------------------------------===// |
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/__support/CPP/span.h" |
11 | #include "src/string/stpncpy.h" |
12 | #include "test/UnitTest/Test.h" |
13 | #include <stddef.h> // For size_t. |
14 | |
15 | class LlvmLibcStpncpyTest : public LIBC_NAMESPACE::testing::Test { |
16 | public: |
17 | void check_stpncpy(LIBC_NAMESPACE::cpp::span<char> dst, |
18 | const LIBC_NAMESPACE::cpp::span<const char> src, size_t n, |
19 | const LIBC_NAMESPACE::cpp::span<const char> expected, |
20 | size_t expectedCopied) { |
21 | // Making sure we don't overflow buffer. |
22 | ASSERT_GE(dst.size(), n); |
23 | // Making sure stpncpy returns a pointer to the end of dst. |
24 | ASSERT_EQ(LIBC_NAMESPACE::stpncpy(dst.data(), src.data(), n), |
25 | dst.data() + expectedCopied); |
26 | // Expected must be of the same size as dst. |
27 | ASSERT_EQ(dst.size(), expected.size()); |
28 | // Expected and dst are the same. |
29 | for (size_t i = 0; i < expected.size(); ++i) |
30 | ASSERT_EQ(expected[i], dst[i]); |
31 | } |
32 | }; |
33 | |
34 | TEST_F(LlvmLibcStpncpyTest, Untouched) { |
35 | char dst[] = {'a', 'b'}; |
36 | const char src[] = {'x', '\0'}; |
37 | const char expected[] = {'a', 'b'}; |
38 | check_stpncpy(dst, src, 0, expected, 0); |
39 | } |
40 | |
41 | TEST_F(LlvmLibcStpncpyTest, CopyOne) { |
42 | char dst[] = {'a', 'b'}; |
43 | const char src[] = {'x', 'y'}; |
44 | const char expected[] = {'x', 'b'}; // no \0 is appended |
45 | check_stpncpy(dst, src, 1, expected, 1); |
46 | } |
47 | |
48 | TEST_F(LlvmLibcStpncpyTest, CopyNull) { |
49 | char dst[] = {'a', 'b'}; |
50 | const char src[] = {'\0', 'y'}; |
51 | const char expected[] = {'\0', 'b'}; |
52 | check_stpncpy(dst, src, 1, expected, 0); |
53 | } |
54 | |
55 | TEST_F(LlvmLibcStpncpyTest, CopyPastSrc) { |
56 | char dst[] = {'a', 'b'}; |
57 | const char src[] = {'\0', 'y'}; |
58 | const char expected[] = {'\0', '\0'}; |
59 | check_stpncpy(dst, src, 2, expected, 0); |
60 | } |
61 | |
62 | TEST_F(LlvmLibcStpncpyTest, CopyTwoNoNull) { |
63 | char dst[] = {'a', 'b'}; |
64 | const char src[] = {'x', 'y'}; |
65 | const char expected[] = {'x', 'y'}; |
66 | check_stpncpy(dst, src, 2, expected, 2); |
67 | } |
68 | |
69 | TEST_F(LlvmLibcStpncpyTest, CopyTwoWithNull) { |
70 | char dst[] = {'a', 'b'}; |
71 | const char src[] = {'x', '\0'}; |
72 | const char expected[] = {'x', '\0'}; |
73 | check_stpncpy(dst, src, 2, expected, 1); |
74 | } |
75 | |
76 | #if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER) |
77 | |
78 | TEST_F(LlvmLibcStpncpyTest, CrashOnNullPtr) { |
79 | ASSERT_DEATH([]() { LIBC_NAMESPACE::stpncpy(nullptr, nullptr, 1); }, |
80 | WITH_SIGNAL(-1)); |
81 | } |
82 | |
83 | #endif // defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER) |
84 | |