Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 17 | #define _GNU_SOURCE 1 |
| 18 | |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 19 | #include <string.h> |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 20 | |
| 21 | #include <errno.h> |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
Dan Albert | e5fdaa4 | 2014-06-14 01:04:31 +0000 | [diff] [blame] | 23 | #include <malloc.h> |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 24 | #include <math.h> |
Elliott Hughes | d2a9fb3 | 2015-07-27 20:55:03 -0700 | [diff] [blame] | 25 | #include <stdint.h> |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 26 | #include <sys/cdefs.h> |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 27 | |
Christopher Ferris | 71766c2 | 2016-02-12 17:24:27 -0800 | [diff] [blame] | 28 | #include <algorithm> |
| 29 | #include <vector> |
| 30 | |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 31 | #include "buffer_tests.h" |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 32 | #include "utils.h" |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 33 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 34 | #if defined(NOFORTIFY) |
| 35 | #define STRING_TEST string_nofortify |
| 36 | #else |
| 37 | #define STRING_TEST string |
| 38 | #endif |
| 39 | |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 40 | #if defined(__BIONIC__) |
| 41 | #define STRLCPY_SUPPORTED |
| 42 | #define STRLCAT_SUPPORTED |
| 43 | #endif |
| 44 | |
Chih-Hung Hsieh | d61ca37 | 2016-06-03 10:18:07 -0700 | [diff] [blame] | 45 | constexpr auto KB = 1024; |
| 46 | constexpr auto SMALL = 1 * KB; |
| 47 | constexpr auto MEDIUM = 4 * KB; |
| 48 | constexpr auto LARGE = 64 * KB; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 49 | |
| 50 | static int signum(int i) { |
| 51 | if (i < 0) { |
| 52 | return -1; |
| 53 | } else if (i > 0) { |
| 54 | return 1; |
| 55 | } |
| 56 | return 0; |
| 57 | } |
| 58 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 59 | TEST(STRING_TEST, strerror) { |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 60 | // Valid. |
| 61 | ASSERT_STREQ("Success", strerror(0)); |
Steven Moreland | 4ef83d6 | 2021-10-07 00:19:18 +0000 | [diff] [blame] | 62 | ASSERT_STREQ("Operation not permitted", strerror(1)); |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 63 | |
| 64 | // Invalid. |
Elliott Hughes | e6e6006 | 2013-01-10 16:01:59 -0800 | [diff] [blame] | 65 | ASSERT_STREQ("Unknown error -1", strerror(-1)); |
Elliott Hughes | eebf5fd | 2018-11-30 16:56:43 -0800 | [diff] [blame] | 66 | ASSERT_STREQ("Unknown error 134", strerror(EHWPOISON + 1)); |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 67 | } |
| 68 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 69 | TEST(STRING_TEST, strerror_l) { |
| 70 | // bionic just forwards to strerror(3). |
| 71 | ASSERT_STREQ("Success", strerror_l(0, LC_GLOBAL_LOCALE)); |
| 72 | } |
| 73 | |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 74 | #if defined(__BIONIC__) |
Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 75 | static void* ConcurrentStrErrorFn(void*) { |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 76 | bool equal = (strcmp("Unknown error 2002", strerror(2002)) == 0); |
| 77 | return reinterpret_cast<void*>(equal); |
| 78 | } |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 79 | #endif // __BIONIC__ |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 80 | |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 81 | // glibc's strerror isn't thread safe, only its strsignal. |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 82 | TEST(STRING_TEST, strerror_concurrent) { |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 83 | #if defined(__BIONIC__) |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 84 | const char* strerror1001 = strerror(1001); |
| 85 | ASSERT_STREQ("Unknown error 1001", strerror1001); |
| 86 | |
| 87 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 88 | ASSERT_EQ(0, pthread_create(&t, nullptr, ConcurrentStrErrorFn, nullptr)); |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 89 | void* result; |
| 90 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 91 | ASSERT_TRUE(static_cast<bool>(result)); |
| 92 | |
| 93 | ASSERT_STREQ("Unknown error 1001", strerror1001); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 94 | #else // __BIONIC__ |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 95 | GTEST_SKIP() << "thread-safe strerror not available"; |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 96 | #endif // __BIONIC__ |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 97 | } |
Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 98 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 99 | TEST(STRING_TEST, gnu_strerror_r) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 100 | #if !defined(ANDROID_HOST_MUSL) |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 101 | char buf[256]; |
| 102 | |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 103 | // Note that glibc doesn't necessarily write into the buffer. |
| 104 | |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 105 | // Valid. |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 106 | ASSERT_STREQ("Success", strerror_r(0, buf, sizeof(buf))); |
| 107 | #if defined(__BIONIC__) |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 108 | ASSERT_STREQ("Success", buf); |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 109 | #endif |
Steven Moreland | 4ef83d6 | 2021-10-07 00:19:18 +0000 | [diff] [blame] | 110 | ASSERT_STREQ("Operation not permitted", strerror_r(1, buf, sizeof(buf))); |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 111 | #if defined(__BIONIC__) |
Steven Moreland | 4ef83d6 | 2021-10-07 00:19:18 +0000 | [diff] [blame] | 112 | ASSERT_STREQ("Operation not permitted", buf); |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 113 | #endif |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 114 | |
| 115 | // Invalid. |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 116 | ASSERT_STREQ("Unknown error -1", strerror_r(-1, buf, sizeof(buf))); |
Nick Kralevich | 6060589 | 2013-01-15 10:35:09 -0800 | [diff] [blame] | 117 | ASSERT_STREQ("Unknown error -1", buf); |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 118 | ASSERT_STREQ("Unknown error 1234", strerror_r(1234, buf, sizeof(buf))); |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 119 | ASSERT_STREQ("Unknown error 1234", buf); |
| 120 | |
| 121 | // Buffer too small. |
Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 122 | errno = 0; |
| 123 | memset(buf, 0, sizeof(buf)); |
| 124 | ASSERT_EQ(buf, strerror_r(4567, buf, 2)); |
| 125 | ASSERT_STREQ("U", buf); |
| 126 | // The GNU strerror_r doesn't set errno (the POSIX one sets it to ERANGE). |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 127 | ASSERT_ERRNO(0); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 128 | #else |
| 129 | GTEST_SKIP() << "musl doesn't have GNU strerror_r"; |
| 130 | #endif |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 131 | } |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 132 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 133 | TEST(STRING_TEST, strsignal) { |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 134 | // A regular signal. |
| 135 | ASSERT_STREQ("Hangup", strsignal(1)); |
| 136 | |
| 137 | // A real-time signal. |
Elliott Hughes | 0990d4f | 2014-04-30 09:45:40 -0700 | [diff] [blame] | 138 | ASSERT_STREQ("Real-time signal 14", strsignal(SIGRTMIN + 14)); |
| 139 | // One of the signals the C library keeps to itself. |
Colin Cross | 23b986c | 2022-10-19 15:03:59 -0700 | [diff] [blame] | 140 | ASSERT_STREQ("Unknown signal 32", strsignal(32)); // __SIGRTMIN |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 141 | |
| 142 | // Errors. |
| 143 | ASSERT_STREQ("Unknown signal -1", strsignal(-1)); // Too small. |
| 144 | ASSERT_STREQ("Unknown signal 0", strsignal(0)); // Still too small. |
| 145 | ASSERT_STREQ("Unknown signal 1234", strsignal(1234)); // Too large. |
| 146 | } |
| 147 | |
Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 148 | static void* ConcurrentStrSignalFn(void*) { |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 149 | bool equal = (strcmp("Unknown signal 2002", strsignal(2002)) == 0); |
| 150 | return reinterpret_cast<void*>(equal); |
| 151 | } |
| 152 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 153 | TEST(STRING_TEST, strsignal_concurrent) { |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 154 | const char* strsignal1001 = strsignal(1001); |
| 155 | ASSERT_STREQ("Unknown signal 1001", strsignal1001); |
| 156 | |
| 157 | pthread_t t; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 158 | ASSERT_EQ(0, pthread_create(&t, nullptr, ConcurrentStrSignalFn, nullptr)); |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 159 | void* result; |
| 160 | ASSERT_EQ(0, pthread_join(t, &result)); |
| 161 | ASSERT_TRUE(static_cast<bool>(result)); |
| 162 | |
| 163 | ASSERT_STREQ("Unknown signal 1001", strsignal1001); |
| 164 | } |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 165 | |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 166 | // TODO: where did this number come from? |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 167 | #define ITER 500 |
| 168 | |
| 169 | // For every length we want to test, vary and change alignment |
| 170 | // of allocated memory, fill it with some values, calculate |
| 171 | // expected result and then run function and compare what we got. |
| 172 | // These tests contributed by Intel Corporation. |
| 173 | // TODO: make these tests more intention-revealing and less random. |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 174 | template<class Character> |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 175 | class StringTestState { |
| 176 | public: |
Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 177 | explicit StringTestState(size_t MAX_LEN) : MAX_LEN(MAX_LEN), align1_index_(0), align2_index_(0) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 178 | int max_alignment = 64; |
| 179 | |
| 180 | // TODO: fix the tests to not sometimes use twice their specified "MAX_LEN". |
Dan Albert | e5fdaa4 | 2014-06-14 01:04:31 +0000 | [diff] [blame] | 181 | glob_ptr = reinterpret_cast<Character*>(memalign(sysconf(_SC_PAGESIZE), 2 * sizeof(Character) * MAX_LEN + max_alignment)); |
| 182 | glob_ptr1 = reinterpret_cast<Character*>(memalign(sysconf(_SC_PAGESIZE), 2 * sizeof(Character) * MAX_LEN + max_alignment)); |
| 183 | glob_ptr2 = reinterpret_cast<Character*>(memalign(sysconf(_SC_PAGESIZE), 2 * sizeof(Character) * MAX_LEN + max_alignment)); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 184 | |
| 185 | InitLenArray(); |
| 186 | |
| 187 | srandom(1234); |
| 188 | } |
| 189 | |
| 190 | ~StringTestState() { |
| 191 | free(glob_ptr); |
| 192 | free(glob_ptr1); |
| 193 | free(glob_ptr2); |
| 194 | } |
| 195 | |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 196 | void BeginIterations() { |
| 197 | align1_index_ = 0; |
| 198 | align2_index_ = 0; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 199 | |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 200 | ResetPointers(); |
| 201 | } |
| 202 | |
| 203 | bool HasNextIteration() { |
| 204 | return (align1_index_ != (alignments_size - 1) || align2_index_ != (alignments_size - 1)); |
| 205 | } |
| 206 | |
| 207 | void NextIteration() { |
| 208 | if (align1_index_ == (alignments_size - 1) && align2_index_ == (alignments_size - 1)) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | if (align1_index_ == (alignments_size - 1)) { |
| 213 | align1_index_ = 0; |
| 214 | align2_index_++; |
| 215 | } else { |
| 216 | align1_index_++; |
| 217 | } |
| 218 | |
| 219 | ResetPointers(); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | const size_t MAX_LEN; |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 223 | Character *ptr, *ptr1, *ptr2; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 224 | size_t n; |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 225 | size_t len[ITER + 1]; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 226 | |
| 227 | private: |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 228 | static size_t alignments[]; |
| 229 | static size_t alignments_size; |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 230 | Character *glob_ptr, *glob_ptr1, *glob_ptr2; |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 231 | size_t align1_index_, align2_index_; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 232 | |
| 233 | // Calculate input lengths and fill state.len with them. |
| 234 | // Test small lengths with more density than big ones. Manually push |
| 235 | // smallest (0) and biggest (MAX_LEN) lengths. Avoid repeats. |
| 236 | // Return number of lengths to test. |
| 237 | void InitLenArray() { |
| 238 | n = 0; |
| 239 | len[n++] = 0; |
| 240 | for (size_t i = 1; i < ITER; ++i) { |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 241 | size_t l = static_cast<size_t>(exp(log(static_cast<double>(MAX_LEN)) * i / ITER)); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 242 | if (l != len[n - 1]) { |
| 243 | len[n++] = l; |
| 244 | } |
| 245 | } |
| 246 | len[n++] = MAX_LEN; |
| 247 | } |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 248 | |
| 249 | void ResetPointers() { |
| 250 | if (align1_index_ == alignments_size || align2_index_ == alignments_size) { |
| 251 | ptr = ptr1 = ptr2 = nullptr; |
| 252 | } else { |
| 253 | ptr = glob_ptr + alignments[align1_index_]; |
| 254 | ptr1 = glob_ptr1 + alignments[align1_index_]; |
| 255 | ptr2 = glob_ptr2 + alignments[align2_index_]; |
| 256 | } |
| 257 | } |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 258 | }; |
| 259 | |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 260 | template<class Character> |
| 261 | size_t StringTestState<Character>::alignments[] = { 24, 32, 16, 48, 0, 1, 2, 3, 4, 5, 6, 7, 11 }; |
| 262 | |
| 263 | template<class Character> |
| 264 | size_t StringTestState<Character>::alignments_size = sizeof(alignments)/sizeof(size_t); |
| 265 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 266 | TEST(STRING_TEST, strcat) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 267 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 268 | for (size_t i = 1; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 269 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 270 | memset(state.ptr2, '\2', state.MAX_LEN); |
| 271 | state.ptr2[state.MAX_LEN - 1] = '\0'; |
| 272 | memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN); |
| 273 | |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 274 | memset(state.ptr1, 'L', state.len[i]); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 275 | state.ptr1[random() % state.len[i]] = '\0'; |
| 276 | state.ptr1[state.len[i] - 1] = '\0'; |
| 277 | |
| 278 | strcpy(state.ptr + state.MAX_LEN - 1, state.ptr1); |
| 279 | |
| 280 | EXPECT_TRUE(strcat(state.ptr2, state.ptr1) == state.ptr2); |
| 281 | EXPECT_TRUE(memcmp(state.ptr, state.ptr2, 2 * state.MAX_LEN) == 0); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 286 | // one byte target with "\0" source |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 287 | TEST(STRING_TEST, strcpy2) { |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 288 | char buf[1]; |
| 289 | char* orig = strdup(""); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 290 | ASSERT_EQ(buf, strcpy(buf, orig)); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 291 | ASSERT_EQ('\0', buf[0]); |
| 292 | free(orig); |
| 293 | } |
| 294 | |
| 295 | // multibyte target where we under fill target |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 296 | TEST(STRING_TEST, strcpy3) { |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 297 | char buf[10]; |
| 298 | char* orig = strdup("12345"); |
| 299 | memset(buf, 'A', sizeof(buf)); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 300 | ASSERT_EQ(buf, strcpy(buf, orig)); |
| 301 | ASSERT_STREQ("12345", buf); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 302 | ASSERT_EQ('A', buf[6]); |
| 303 | ASSERT_EQ('A', buf[7]); |
| 304 | ASSERT_EQ('A', buf[8]); |
| 305 | ASSERT_EQ('A', buf[9]); |
| 306 | free(orig); |
| 307 | } |
| 308 | |
| 309 | // multibyte target where we fill target exactly |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 310 | TEST(STRING_TEST, strcpy4) { |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 311 | char buf[10]; |
| 312 | char* orig = strdup("123456789"); |
| 313 | memset(buf, 'A', sizeof(buf)); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 314 | ASSERT_EQ(buf, strcpy(buf, orig)); |
| 315 | ASSERT_STREQ("123456789", buf); |
| 316 | free(orig); |
| 317 | } |
| 318 | |
| 319 | // one byte target with "\0" source |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 320 | TEST(STRING_TEST, stpcpy2) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 321 | char buf[1]; |
Elliott Hughes | 1b2e844 | 2023-10-26 17:50:16 -0700 | [diff] [blame] | 322 | memset(buf, 'A', sizeof(buf)); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 323 | char* orig = strdup(""); |
Elliott Hughes | 1b2e844 | 2023-10-26 17:50:16 -0700 | [diff] [blame] | 324 | EXPECT_EQ(buf, stpcpy(buf, orig)); |
| 325 | EXPECT_EQ('\0', buf[0]); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 326 | free(orig); |
| 327 | } |
| 328 | |
| 329 | // multibyte target where we under fill target |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 330 | TEST(STRING_TEST, stpcpy3) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 331 | char buf[10]; |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 332 | memset(buf, 'A', sizeof(buf)); |
Elliott Hughes | 1b2e844 | 2023-10-26 17:50:16 -0700 | [diff] [blame] | 333 | char* orig = strdup("12345"); |
| 334 | EXPECT_EQ(buf+strlen(orig), stpcpy(buf, orig)); |
| 335 | EXPECT_STREQ("12345", buf); |
| 336 | EXPECT_EQ('A', buf[6]); |
| 337 | EXPECT_EQ('A', buf[7]); |
| 338 | EXPECT_EQ('A', buf[8]); |
| 339 | EXPECT_EQ('A', buf[9]); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 340 | free(orig); |
| 341 | } |
| 342 | |
| 343 | // multibyte target where we fill target exactly |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 344 | TEST(STRING_TEST, stpcpy4) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 345 | char buf[10]; |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 346 | memset(buf, 'A', sizeof(buf)); |
Elliott Hughes | 1b2e844 | 2023-10-26 17:50:16 -0700 | [diff] [blame] | 347 | char* orig = strdup("123456789"); |
| 348 | EXPECT_EQ(buf+strlen(orig), stpcpy(buf, orig)); |
| 349 | EXPECT_STREQ("123456789", buf); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 350 | free(orig); |
| 351 | } |
| 352 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 353 | TEST(STRING_TEST, strcat2) { |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 354 | char buf[10]; |
| 355 | memset(buf, 'A', sizeof(buf)); |
| 356 | buf[0] = 'a'; |
| 357 | buf[1] = '\0'; |
| 358 | char* res = strcat(buf, "01234"); |
| 359 | ASSERT_EQ(buf, res); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 360 | ASSERT_STREQ("a01234", buf); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 361 | ASSERT_EQ('A', buf[7]); |
| 362 | ASSERT_EQ('A', buf[8]); |
| 363 | ASSERT_EQ('A', buf[9]); |
| 364 | } |
| 365 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 366 | TEST(STRING_TEST, strcat3) { |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 367 | char buf[10]; |
| 368 | memset(buf, 'A', sizeof(buf)); |
| 369 | buf[0] = 'a'; |
| 370 | buf[1] = '\0'; |
| 371 | char* res = strcat(buf, "01234567"); |
| 372 | ASSERT_EQ(buf, res); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 373 | ASSERT_STREQ("a01234567", buf); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 376 | TEST(STRING_TEST, strncat2) { |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 377 | char buf[10]; |
| 378 | memset(buf, 'A', sizeof(buf)); |
| 379 | buf[0] = 'a'; |
| 380 | buf[1] = '\0'; |
| 381 | char* res = strncat(buf, "01234", sizeof(buf) - strlen(buf) - 1); |
| 382 | ASSERT_EQ(buf, res); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 383 | ASSERT_STREQ("a01234", buf); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 384 | ASSERT_EQ('A', buf[7]); |
| 385 | ASSERT_EQ('A', buf[8]); |
| 386 | ASSERT_EQ('A', buf[9]); |
| 387 | } |
| 388 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 389 | TEST(STRING_TEST, strncat3) { |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 390 | char buf[10]; |
| 391 | memset(buf, 'A', sizeof(buf)); |
| 392 | buf[0] = 'a'; |
| 393 | buf[1] = '\0'; |
| 394 | char* res = strncat(buf, "0123456789", 5); |
| 395 | ASSERT_EQ(buf, res); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 396 | ASSERT_STREQ("a01234", buf); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 397 | ASSERT_EQ('A', buf[7]); |
| 398 | ASSERT_EQ('A', buf[8]); |
| 399 | ASSERT_EQ('A', buf[9]); |
| 400 | } |
| 401 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 402 | TEST(STRING_TEST, strncat4) { |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 403 | char buf[10]; |
| 404 | memset(buf, 'A', sizeof(buf)); |
| 405 | buf[0] = 'a'; |
| 406 | buf[1] = '\0'; |
| 407 | char* res = strncat(buf, "01234567", 8); |
| 408 | ASSERT_EQ(buf, res); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 409 | ASSERT_STREQ("a01234567", buf); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 412 | TEST(STRING_TEST, strncat5) { |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 413 | char buf[10]; |
| 414 | memset(buf, 'A', sizeof(buf)); |
| 415 | buf[0] = 'a'; |
| 416 | buf[1] = '\0'; |
| 417 | char* res = strncat(buf, "01234567", 9); |
| 418 | ASSERT_EQ(buf, res); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 419 | ASSERT_STREQ("a01234567", buf); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 422 | TEST(STRING_TEST, strchr_with_0) { |
Nick Kralevich | 4f40e51 | 2013-04-19 16:54:22 -0700 | [diff] [blame] | 423 | char buf[10]; |
| 424 | const char* s = "01234"; |
| 425 | memcpy(buf, s, strlen(s) + 1); |
| 426 | EXPECT_TRUE(strchr(buf, '\0') == (buf + strlen(s))); |
| 427 | } |
| 428 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 429 | TEST(STRING_TEST, strchr_multiple) { |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 430 | char str[128]; |
| 431 | memset(str, 'a', sizeof(str) - 1); |
| 432 | str[sizeof(str)-1] = '\0'; |
| 433 | |
| 434 | // Verify that strchr finds the first occurrence of 'a' in a string |
| 435 | // filled with 'a' characters. Iterate over the string putting |
| 436 | // non 'a' characters at the front of the string during each iteration |
| 437 | // and continue to verify that strchr can find the first occurrence |
| 438 | // properly. The idea is to cover all possible alignments of the location |
| 439 | // of the first occurrence of the 'a' character and which includes |
| 440 | // other 'a' characters close by. |
| 441 | for (size_t i = 0; i < sizeof(str) - 1; i++) { |
| 442 | EXPECT_EQ(&str[i], strchr(str, 'a')); |
| 443 | str[i] = 'b'; |
| 444 | } |
| 445 | } |
| 446 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 447 | TEST(STRING_TEST, strchr) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 448 | int seek_char = 'R'; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 449 | |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 450 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 451 | for (size_t i = 1; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 452 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 453 | if (~seek_char > 0) { |
| 454 | memset(state.ptr1, ~seek_char, state.len[i]); |
| 455 | } else { |
| 456 | memset(state.ptr1, '\1', state.len[i]); |
| 457 | } |
| 458 | state.ptr1[state.len[i] - 1] = '\0'; |
| 459 | |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 460 | size_t pos = random() % state.MAX_LEN; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 461 | char* expected; |
| 462 | if (pos >= state.len[i] - 1) { |
| 463 | if (seek_char == 0) { |
| 464 | expected = state.ptr1 + state.len[i] - 1; |
| 465 | } else { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 466 | expected = nullptr; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 467 | } |
| 468 | } else { |
| 469 | state.ptr1[pos] = seek_char; |
| 470 | expected = state.ptr1 + pos; |
| 471 | } |
| 472 | |
| 473 | ASSERT_TRUE(strchr(state.ptr1, seek_char) == expected); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 478 | TEST(STRING_TEST, strchrnul) { |
Elliott Hughes | 7ac3c12 | 2015-08-26 09:59:29 -0700 | [diff] [blame] | 479 | const char* s = "01234222"; |
| 480 | EXPECT_TRUE(strchrnul(s, '2') == &s[2]); |
| 481 | EXPECT_TRUE(strchrnul(s, '8') == (s + strlen(s))); |
| 482 | EXPECT_TRUE(strchrnul(s, '\0') == (s + strlen(s))); |
| 483 | } |
| 484 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 485 | TEST(STRING_TEST, strcmp) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 486 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 487 | for (size_t i = 1; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 488 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 489 | memset(state.ptr1, 'v', state.MAX_LEN); |
| 490 | memset(state.ptr2, 'n', state.MAX_LEN); |
| 491 | state.ptr1[state.len[i] - 1] = '\0'; |
| 492 | state.ptr2[state.len[i] - 1] = '\0'; |
| 493 | |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 494 | size_t pos = 1 + (random() % (state.MAX_LEN - 1)); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 495 | int actual; |
| 496 | int expected; |
| 497 | if (pos >= state.len[i] - 1) { |
| 498 | memcpy(state.ptr1, state.ptr2, state.len[i]); |
| 499 | expected = 0; |
| 500 | actual = strcmp(state.ptr1, state.ptr2); |
| 501 | } else { |
| 502 | memcpy(state.ptr1, state.ptr2, pos); |
| 503 | if (state.ptr1[pos] > state.ptr2[pos]) { |
| 504 | expected = 1; |
| 505 | } else if (state.ptr1[pos] == state.ptr2[pos]) { |
| 506 | state.ptr1[pos + 1] = '\0'; |
| 507 | state.ptr2[pos + 1] = '\0'; |
| 508 | expected = 0; |
| 509 | } else { |
| 510 | expected = -1; |
| 511 | } |
| 512 | actual = strcmp(state.ptr1, state.ptr2); |
| 513 | } |
| 514 | |
| 515 | ASSERT_EQ(expected, signum(actual)); |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 520 | TEST(STRING_TEST, stpcpy) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 521 | StringTestState<char> state(SMALL); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 522 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 523 | size_t pos = random() % state.MAX_LEN; |
| 524 | |
| 525 | memset(state.ptr1, '\2', pos); |
| 526 | state.ptr1[pos] = '\0'; |
| 527 | state.ptr1[state.MAX_LEN - 1] = '\0'; |
| 528 | |
| 529 | memcpy(state.ptr, state.ptr1, state.MAX_LEN); |
| 530 | |
| 531 | memset(state.ptr2, '\1', state.MAX_LEN); |
| 532 | state.ptr2[state.MAX_LEN - 1] = '\0'; |
| 533 | |
| 534 | memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN); |
| 535 | memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1); |
| 536 | state.ptr[2 * state.MAX_LEN - 1] = '\0'; |
| 537 | |
| 538 | ASSERT_TRUE(stpcpy(state.ptr2, state.ptr1) == state.ptr2 + strlen(state.ptr1)); |
| 539 | ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN)) != 0 || |
| 540 | (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0)); |
| 541 | } |
| 542 | } |
| 543 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 544 | TEST(STRING_TEST, strcpy) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 545 | StringTestState<char> state(SMALL); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 546 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 547 | size_t pos = random() % state.MAX_LEN; |
| 548 | |
| 549 | memset(state.ptr1, '\2', pos); |
| 550 | state.ptr1[pos] = '\0'; |
| 551 | state.ptr1[state.MAX_LEN - 1] = '\0'; |
| 552 | |
| 553 | memcpy(state.ptr, state.ptr1, state.MAX_LEN); |
| 554 | |
| 555 | memset(state.ptr2, '\1', state.MAX_LEN); |
| 556 | state.ptr2[state.MAX_LEN - 1] = '\0'; |
| 557 | |
| 558 | memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN); |
| 559 | memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1); |
| 560 | state.ptr[2 * state.MAX_LEN - 1] = '\0'; |
| 561 | |
| 562 | ASSERT_TRUE(strcpy(state.ptr2, state.ptr1) == state.ptr2); |
| 563 | ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN)) != 0 || |
| 564 | (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0)); |
| 565 | } |
| 566 | } |
| 567 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 568 | TEST(STRING_TEST, strlcat) { |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 569 | #if defined(STRLCAT_SUPPORTED) |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 570 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 571 | for (size_t i = 0; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 572 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 573 | memset(state.ptr2, '\2', state.MAX_LEN + state.len[i]); |
| 574 | state.ptr2[state.MAX_LEN - 1] = '\0'; |
| 575 | memcpy(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]); |
| 576 | |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 577 | size_t pos = random() % state.MAX_LEN; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 578 | memset(state.ptr1, '\3', pos); |
| 579 | state.ptr1[pos] = '\0'; |
| 580 | if (pos < state.len[i]) { |
| 581 | memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, pos + 1); |
| 582 | } else { |
| 583 | memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, state.len[i]); |
| 584 | state.ptr[state.MAX_LEN + state.len[i] - 1] = '\0'; |
| 585 | } |
| 586 | |
| 587 | strlcat(state.ptr2, state.ptr1, state.MAX_LEN + state.len[i]); |
| 588 | |
| 589 | ASSERT_TRUE(memcmp(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]) == 0); |
| 590 | } |
| 591 | } |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 592 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 593 | GTEST_SKIP() << "strlcat not available"; |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 594 | #endif |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 595 | } |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 596 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 597 | TEST(STRING_TEST, strlcpy) { |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 598 | #if defined(STRLCPY_SUPPORTED) |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 599 | StringTestState<char> state(SMALL); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 600 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
| 601 | int rand = 'O'; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 602 | memset(state.ptr1, rand, state.MAX_LEN); |
| 603 | |
| 604 | size_t pos = random() % state.MAX_LEN; |
| 605 | if (pos < state.MAX_LEN) { |
| 606 | state.ptr1[pos] = '\0'; |
| 607 | } |
| 608 | memcpy(state.ptr, state.ptr1, state.MAX_LEN); |
| 609 | |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 610 | memset(state.ptr2, 'I', state.MAX_LEN); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 611 | memcpy(state.ptr + state.MAX_LEN, state.ptr2, state.MAX_LEN); |
| 612 | |
| 613 | if (pos > state.MAX_LEN - 1) { |
| 614 | memcpy(state.ptr + state.MAX_LEN, state.ptr1, state.MAX_LEN); |
| 615 | state.ptr[2 * state.MAX_LEN - 1] = '\0'; |
| 616 | } else { |
| 617 | memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1); |
| 618 | } |
| 619 | |
| 620 | ASSERT_EQ(strlcpy(state.ptr2, state.ptr1, state.MAX_LEN), strlen(state.ptr1)); |
| 621 | ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN) != 0) || |
| 622 | (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0)); |
| 623 | } |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 624 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 625 | GTEST_SKIP() << "strlcpy not available"; |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 626 | #endif |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 627 | } |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 628 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 629 | TEST(STRING_TEST, strncat) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 630 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 631 | for (size_t i = 1; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 632 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 633 | memset(state.ptr2, '\2', state.MAX_LEN); |
| 634 | state.ptr2[state.MAX_LEN - 1] = '\0'; |
| 635 | memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN); |
| 636 | |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 637 | memset(state.ptr1, 'I', state.len[i]); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 638 | state.ptr1[random() % state.len[i]] = '\0'; |
| 639 | state.ptr1[state.len[i] - 1] = '\0'; |
| 640 | |
| 641 | size_t pos = strlen(state.ptr1); |
| 642 | |
| 643 | size_t actual = random() % state.len[i]; |
| 644 | strncpy(state.ptr + state.MAX_LEN - 1, state.ptr1, std::min(actual, pos)); |
| 645 | state.ptr[state.MAX_LEN + std::min(actual, pos) - 1] = '\0'; |
| 646 | |
| 647 | ASSERT_TRUE(strncat(state.ptr2, state.ptr1, actual) == state.ptr2); |
| 648 | ASSERT_EQ(memcmp(state.ptr, state.ptr2, 2 * state.MAX_LEN), 0); |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 653 | TEST(STRING_TEST, strncmp) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 654 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 655 | for (size_t i = 1; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 656 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 657 | memset(state.ptr1, 'v', state.MAX_LEN); |
| 658 | memset(state.ptr2, 'n', state.MAX_LEN); |
| 659 | state.ptr1[state.len[i] - 1] = '\0'; |
| 660 | state.ptr2[state.len[i] - 1] = '\0'; |
| 661 | |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 662 | size_t pos = 1 + (random() % (state.MAX_LEN - 1)); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 663 | int actual; |
| 664 | int expected; |
| 665 | if (pos >= state.len[i] - 1) { |
| 666 | memcpy(state.ptr1, state.ptr2, state.len[i]); |
| 667 | expected = 0; |
| 668 | actual = strncmp(state.ptr1, state.ptr2, state.len[i]); |
| 669 | } else { |
| 670 | memcpy(state.ptr1, state.ptr2, pos); |
| 671 | if (state.ptr1[pos] > state.ptr2[pos]) { |
| 672 | expected = 1; |
| 673 | } else if (state.ptr1[pos] == state.ptr2[pos]) { |
| 674 | state.ptr1[pos + 1] = '\0'; |
| 675 | state.ptr2[pos + 1] = '\0'; |
| 676 | expected = 0; |
| 677 | } else { |
| 678 | expected = -1; |
| 679 | } |
| 680 | actual = strncmp(state.ptr1, state.ptr2, state.len[i]); |
| 681 | } |
| 682 | |
| 683 | ASSERT_EQ(expected, signum(actual)); |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 688 | TEST(STRING_TEST, stpncpy) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 689 | StringTestState<char> state(SMALL); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 690 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
| 691 | memset(state.ptr1, 'J', state.MAX_LEN); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 692 | // Choose a random size for our src buffer. |
| 693 | size_t ptr1_len = random() % state.MAX_LEN; |
| 694 | state.ptr1[ptr1_len] = '\0'; |
| 695 | // Copy ptr1 into ptr, used to verify that ptr1 does not get modified. |
| 696 | memcpy(state.ptr, state.ptr1, state.MAX_LEN); |
| 697 | // Init ptr2 to a set value. |
| 698 | memset(state.ptr2, '\1', state.MAX_LEN); |
| 699 | |
| 700 | // Choose a random amount of data to copy. |
| 701 | size_t copy_len = random() % state.MAX_LEN; |
| 702 | |
| 703 | // Set the second half of ptr to the expected pattern in ptr2. |
| 704 | memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN); |
| 705 | memcpy(state.ptr + state.MAX_LEN, state.ptr1, copy_len); |
| 706 | size_t expected_end; |
| 707 | if (copy_len > ptr1_len) { |
| 708 | memset(state.ptr + state.MAX_LEN + ptr1_len, '\0', copy_len - ptr1_len); |
| 709 | expected_end = ptr1_len; |
| 710 | } else { |
| 711 | expected_end = copy_len; |
| 712 | } |
| 713 | |
| 714 | ASSERT_EQ(state.ptr2 + expected_end, stpncpy(state.ptr2, state.ptr1, copy_len)); |
| 715 | |
| 716 | // Verify ptr1 was not modified. |
| 717 | ASSERT_EQ(0, memcmp(state.ptr1, state.ptr, state.MAX_LEN)); |
| 718 | // Verify ptr2 contains the expected data. |
| 719 | ASSERT_EQ(0, memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN)); |
| 720 | } |
| 721 | } |
| 722 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 723 | TEST(STRING_TEST, strncpy) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 724 | StringTestState<char> state(SMALL); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 725 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 726 | // Choose a random value to fill the string, except \0 (string terminator), |
| 727 | // or \1 (guarantees it's different from anything in ptr2). |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 728 | memset(state.ptr1, 'K', state.MAX_LEN); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 729 | // Choose a random size for our src buffer. |
| 730 | size_t ptr1_len = random() % state.MAX_LEN; |
| 731 | state.ptr1[ptr1_len] = '\0'; |
| 732 | // Copy ptr1 into ptr, used to verify that ptr1 does not get modified. |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 733 | memcpy(state.ptr, state.ptr1, state.MAX_LEN); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 734 | // Init ptr2 to a set value. |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 735 | memset(state.ptr2, '\1', state.MAX_LEN); |
| 736 | |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 737 | // Choose a random amount of data to copy. |
| 738 | size_t copy_len = random() % state.MAX_LEN; |
| 739 | |
| 740 | // Set the second half of ptr to the expected pattern in ptr2. |
| 741 | memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN); |
| 742 | memcpy(state.ptr + state.MAX_LEN, state.ptr1, copy_len); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 743 | if (copy_len > ptr1_len) { |
| 744 | memset(state.ptr + state.MAX_LEN + ptr1_len, '\0', copy_len - ptr1_len); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 745 | } |
| 746 | |
Elliott Hughes | b24d217 | 2024-10-03 16:36:47 +0000 | [diff] [blame] | 747 | ASSERT_EQ(state.ptr2, strncpy(state.ptr2, state.ptr1, copy_len)); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 748 | |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 749 | // Verify ptr1 was not modified. |
| 750 | ASSERT_EQ(0, memcmp(state.ptr1, state.ptr, state.MAX_LEN)); |
| 751 | // Verify ptr2 contains the expected data. |
| 752 | ASSERT_EQ(0, memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN)); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 756 | TEST(STRING_TEST, strrchr) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 757 | int seek_char = 'M'; |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 758 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 759 | for (size_t i = 1; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 760 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 761 | if (~seek_char > 0) { |
| 762 | memset(state.ptr1, ~seek_char, state.len[i]); |
| 763 | } else { |
| 764 | memset(state.ptr1, '\1', state.len[i]); |
| 765 | } |
| 766 | state.ptr1[state.len[i] - 1] = '\0'; |
| 767 | |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 768 | size_t pos = random() % state.MAX_LEN; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 769 | char* expected; |
| 770 | if (pos >= state.len[i] - 1) { |
| 771 | if (seek_char == 0) { |
| 772 | expected = state.ptr1 + state.len[i] - 1; |
| 773 | } else { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 774 | expected = nullptr; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 775 | } |
| 776 | } else { |
| 777 | state.ptr1[pos] = seek_char; |
| 778 | expected = state.ptr1 + pos; |
| 779 | } |
| 780 | |
| 781 | ASSERT_TRUE(strrchr(state.ptr1, seek_char) == expected); |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 786 | TEST(STRING_TEST, memchr) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 787 | int seek_char = 'N'; |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 788 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 789 | for (size_t i = 0; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 790 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 791 | memset(state.ptr1, ~seek_char, state.len[i]); |
| 792 | |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 793 | size_t pos = random() % state.MAX_LEN; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 794 | char* expected; |
| 795 | if (pos >= state.len[i]) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 796 | expected = nullptr; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 797 | } else { |
| 798 | state.ptr1[pos] = seek_char; |
| 799 | expected = state.ptr1 + pos; |
| 800 | } |
| 801 | |
| 802 | ASSERT_TRUE(memchr(state.ptr1, seek_char, state.len[i]) == expected); |
| 803 | } |
| 804 | } |
| 805 | } |
| 806 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 807 | TEST(STRING_TEST, memchr_zero) { |
Christopher Ferris | e03e1ea | 2014-07-30 16:06:56 -0700 | [diff] [blame] | 808 | uint8_t* buffer; |
| 809 | ASSERT_EQ(0, posix_memalign(reinterpret_cast<void**>(&buffer), 64, 64)); |
| 810 | memset(buffer, 10, 64); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 811 | ASSERT_TRUE(nullptr == memchr(buffer, 5, 0)); |
| 812 | ASSERT_TRUE(nullptr == memchr(buffer, 10, 0)); |
Christopher Ferris | e03e1ea | 2014-07-30 16:06:56 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 815 | TEST(STRING_TEST, memrchr) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 816 | int seek_char = 'P'; |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 817 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 818 | for (size_t i = 0; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 819 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 820 | memset(state.ptr1, ~seek_char, state.len[i]); |
| 821 | |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 822 | size_t pos = random() % state.MAX_LEN; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 823 | char* expected; |
| 824 | if (pos >= state.len[i]) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 825 | expected = nullptr; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 826 | } else { |
| 827 | state.ptr1[pos] = seek_char; |
| 828 | expected = state.ptr1 + pos; |
| 829 | } |
| 830 | |
| 831 | ASSERT_TRUE(memrchr(state.ptr1, seek_char, state.len[i]) == expected); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 836 | TEST(STRING_TEST, memcmp) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 837 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 838 | for (size_t i = 0; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 839 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
| 840 | int c1 = 'A'; |
| 841 | int c2 = 'N'; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 842 | memset(state.ptr1, c1, state.MAX_LEN); |
| 843 | memset(state.ptr2, c1, state.MAX_LEN); |
| 844 | |
| 845 | int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]); |
| 846 | state.ptr2[pos] = c2; |
| 847 | |
| 848 | int expected = (static_cast<int>(c1) - static_cast<int>(c2)); |
| 849 | int actual = memcmp(state.ptr1, state.ptr2, state.MAX_LEN); |
| 850 | |
| 851 | ASSERT_EQ(signum(expected), signum(actual)); |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 856 | TEST(STRING_TEST, wmemcmp) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 857 | StringTestState<wchar_t> state(SMALL); |
| 858 | |
| 859 | for (size_t i = 0; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 860 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 861 | long long mask = ((long long) 1 << 8 * sizeof(wchar_t)) - 1; |
| 862 | int c1 = rand() & mask; |
| 863 | int c2 = rand() & mask; |
| 864 | wmemset(state.ptr1, c1, state.MAX_LEN); |
| 865 | wmemset(state.ptr2, c1, state.MAX_LEN); |
| 866 | |
| 867 | int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]); |
| 868 | state.ptr2[pos] = c2; |
| 869 | |
| 870 | int expected = (static_cast<int>(c1) - static_cast<int>(c2)); |
| 871 | int actual = wmemcmp(state.ptr1, state.ptr2, (size_t) state.MAX_LEN); |
| 872 | |
| 873 | ASSERT_EQ(signum(expected), signum(actual)); |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 878 | TEST(STRING_TEST, memcpy) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 879 | StringTestState<char> state(LARGE); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 880 | int rand = 4; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 881 | for (size_t i = 0; i < state.n - 1; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 882 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 883 | size_t pos = random() % (state.MAX_LEN - state.len[i]); |
| 884 | |
| 885 | memset(state.ptr1, rand, state.len[i]); |
| 886 | memset(state.ptr1 + state.len[i], ~rand, state.MAX_LEN - state.len[i]); |
| 887 | |
| 888 | memset(state.ptr2, rand, state.len[i]); |
| 889 | memset(state.ptr2 + state.len[i], ~rand, state.MAX_LEN - state.len[i]); |
| 890 | memset(state.ptr2 + pos, '\0', state.len[i]); |
| 891 | |
| 892 | ASSERT_FALSE(memcpy(state.ptr2 + pos, state.ptr1 + pos, state.len[i]) != state.ptr2 + pos); |
| 893 | ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN)); |
| 894 | } |
| 895 | } |
| 896 | } |
| 897 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 898 | TEST(STRING_TEST, memset) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 899 | StringTestState<char> state(LARGE); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 900 | char ch = 'P'; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 901 | for (size_t i = 0; i < state.n - 1; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 902 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 903 | memset(state.ptr1, ~ch, state.MAX_LEN); |
| 904 | memcpy(state.ptr2, state.ptr1, state.MAX_LEN); |
| 905 | |
| 906 | size_t pos = random () % (state.MAX_LEN - state.len[i]); |
| 907 | for (size_t k = pos; k < pos + state.len[i]; k++) { |
| 908 | state.ptr1[k] = ch; |
| 909 | } |
| 910 | |
| 911 | ASSERT_TRUE(memset(state.ptr2 + pos, ch, state.len[i]) == state.ptr2 + pos); |
| 912 | |
| 913 | ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN)); |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 918 | TEST(STRING_TEST, memmove) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 919 | StringTestState<char> state(LARGE); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 920 | for (size_t i = 0; i < state.n - 1; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 921 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
| 922 | memset(state.ptr1, 'Q', 2 * state.MAX_LEN); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 923 | |
| 924 | size_t pos = random() % (state.MAX_LEN - state.len[i]); |
| 925 | |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 926 | memset(state.ptr1, 'R', state.len[i]); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 927 | memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN); |
| 928 | memcpy(state.ptr, state.ptr1, state.len[i]); |
| 929 | memcpy(state.ptr1 + pos, state.ptr, state.len[i]); |
| 930 | |
| 931 | ASSERT_TRUE(memmove(state.ptr2 + pos, state.ptr2, state.len[i]) == state.ptr2 + pos); |
| 932 | ASSERT_EQ(0, memcmp(state.ptr2, state.ptr1, 2 * state.MAX_LEN)); |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 937 | TEST(STRING_TEST, memmove_cache_size) { |
Varvara Rainchik | fce8614 | 2014-05-27 12:41:55 +0400 | [diff] [blame] | 938 | size_t len = 600000; |
| 939 | int max_alignment = 31; |
| 940 | int alignments[] = {0, 5, 11, 29, 30}; |
| 941 | char* ptr = reinterpret_cast<char*>(malloc(sizeof(char) * len)); |
| 942 | char* ptr1 = reinterpret_cast<char*>(malloc(2 * sizeof(char) * len)); |
| 943 | char* glob_ptr2 = reinterpret_cast<char*>(malloc(2 * sizeof(char) * len + max_alignment)); |
| 944 | size_t pos = 64; |
| 945 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 946 | ASSERT_TRUE(ptr != nullptr); |
| 947 | ASSERT_TRUE(ptr1 != nullptr); |
| 948 | ASSERT_TRUE(glob_ptr2 != nullptr); |
Varvara Rainchik | fce8614 | 2014-05-27 12:41:55 +0400 | [diff] [blame] | 949 | |
| 950 | for (int i = 0; i < 5; i++) { |
| 951 | char* ptr2 = glob_ptr2 + alignments[i]; |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 952 | memset(ptr1, 'S', 2 * len); |
| 953 | memset(ptr1, 'T', len); |
Varvara Rainchik | fce8614 | 2014-05-27 12:41:55 +0400 | [diff] [blame] | 954 | memcpy(ptr2, ptr1, 2 * len); |
| 955 | memcpy(ptr, ptr1, len); |
| 956 | memcpy(ptr1 + pos, ptr, len); |
| 957 | |
| 958 | ASSERT_TRUE(memmove(ptr2 + pos, ptr, len) == ptr2 + pos); |
| 959 | ASSERT_EQ(0, memcmp(ptr2, ptr1, 2 * len)); |
| 960 | } |
| 961 | free(ptr); |
| 962 | free(ptr1); |
| 963 | free(glob_ptr2); |
| 964 | } |
| 965 | |
Shu Zhang | 6c80ccd | 2014-05-12 18:12:15 +0800 | [diff] [blame] | 966 | static void verify_memmove(char* src_copy, char* dst, char* src, size_t size) { |
| 967 | memset(dst, 0, size); |
| 968 | memcpy(src, src_copy, size); |
| 969 | ASSERT_EQ(dst, memmove(dst, src, size)); |
| 970 | ASSERT_EQ(0, memcmp(dst, src_copy, size)); |
| 971 | } |
| 972 | |
| 973 | #define MEMMOVE_DATA_SIZE (1024*1024*3) |
| 974 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 975 | TEST(STRING_TEST, memmove_check) { |
Shu Zhang | 6c80ccd | 2014-05-12 18:12:15 +0800 | [diff] [blame] | 976 | char* buffer = reinterpret_cast<char*>(malloc(MEMMOVE_DATA_SIZE)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 977 | ASSERT_TRUE(buffer != nullptr); |
Shu Zhang | 6c80ccd | 2014-05-12 18:12:15 +0800 | [diff] [blame] | 978 | |
| 979 | char* src_data = reinterpret_cast<char*>(malloc(MEMMOVE_DATA_SIZE)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 980 | ASSERT_TRUE(src_data != nullptr); |
Shu Zhang | 6c80ccd | 2014-05-12 18:12:15 +0800 | [diff] [blame] | 981 | // Initialize to a known pattern to copy into src for each test and |
| 982 | // to compare dst against. |
| 983 | for (size_t i = 0; i < MEMMOVE_DATA_SIZE; i++) { |
| 984 | src_data[i] = (i + 1) % 255; |
| 985 | } |
| 986 | |
| 987 | // Check all different dst offsets between 0 and 127 inclusive. |
| 988 | char* src = buffer; |
| 989 | for (size_t i = 0; i < 127; i++) { |
| 990 | char* dst = buffer + 256 + i; |
| 991 | // Small copy. |
| 992 | verify_memmove(src_data, dst, src, 1024); |
| 993 | |
| 994 | // Medium copy. |
| 995 | verify_memmove(src_data, dst, src, 64 * 1024); |
| 996 | |
| 997 | // Medium copy. |
| 998 | verify_memmove(src_data, dst, src, 1024 * 1024 + 128 * 1024); |
| 999 | } |
| 1000 | |
| 1001 | // Check all leftover size offsets between 1 and 127 inclusive. |
| 1002 | char* dst = buffer + 256; |
| 1003 | src = buffer; |
| 1004 | for (size_t size = 1; size < 127; size++) { |
| 1005 | // Small copy. |
| 1006 | verify_memmove(src_data, dst, src, 1024); |
| 1007 | |
| 1008 | // Medium copy. |
| 1009 | verify_memmove(src_data, dst, src, 64 * 1024); |
| 1010 | |
| 1011 | // Large copy. |
| 1012 | verify_memmove(src_data, dst, src, 1024 * 1024 + 128 * 1024); |
| 1013 | } |
| 1014 | } |
| 1015 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1016 | TEST(STRING_TEST, bcopy) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 1017 | StringTestState<char> state(LARGE); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 1018 | for (size_t i = 0; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 1019 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
| 1020 | memset(state.ptr1, '4', state.MAX_LEN); |
| 1021 | memset(state.ptr1 + state.MAX_LEN, 'a', state.MAX_LEN); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 1022 | memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN); |
| 1023 | |
| 1024 | size_t start = random() % (2 * state.MAX_LEN - state.len[i]); |
| 1025 | memcpy(state.ptr2 + start, state.ptr1, state.len[i]); |
| 1026 | |
| 1027 | bcopy(state.ptr1, state.ptr1 + start, state.len[i]); |
| 1028 | ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, 2 * state.MAX_LEN)); |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1033 | TEST(STRING_TEST, bzero) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 1034 | StringTestState<char> state(LARGE); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 1035 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
| 1036 | memset(state.ptr1, 'R', state.MAX_LEN); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 1037 | |
| 1038 | size_t start = random() % state.MAX_LEN; |
| 1039 | size_t end = start + random() % (state.MAX_LEN - start); |
| 1040 | |
| 1041 | memcpy(state.ptr2, state.ptr1, start); |
| 1042 | memset(state.ptr2 + start, '\0', end - start); |
| 1043 | memcpy(state.ptr2 + end, state.ptr1 + end, state.MAX_LEN - end); |
| 1044 | |
| 1045 | bzero(state.ptr1 + start, end - start); |
| 1046 | |
| 1047 | ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN)); |
| 1048 | } |
| 1049 | } |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1050 | |
| 1051 | static void DoMemcpyTest(uint8_t* src, uint8_t* dst, size_t len) { |
| 1052 | memset(src, (len % 255) + 1, len); |
| 1053 | memset(dst, 0, len); |
| 1054 | |
| 1055 | ASSERT_EQ(dst, memcpy(dst, src, len)); |
| 1056 | ASSERT_TRUE(memcmp(src, dst, len) == 0); |
| 1057 | } |
| 1058 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1059 | TEST(STRING_TEST, memcpy_align) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1060 | RunSrcDstBufferAlignTest(LARGE, DoMemcpyTest); |
| 1061 | } |
| 1062 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1063 | TEST(STRING_TEST, memcpy_overread) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1064 | RunSrcDstBufferOverreadTest(DoMemcpyTest); |
| 1065 | } |
| 1066 | |
Shu Zhang | 6c80ccd | 2014-05-12 18:12:15 +0800 | [diff] [blame] | 1067 | static void DoMemmoveTest(uint8_t* src, uint8_t* dst, size_t len) { |
| 1068 | memset(src, (len % 255) + 1, len); |
| 1069 | memset(dst, 0, len); |
| 1070 | |
| 1071 | ASSERT_EQ(dst, memmove(dst, src, len)); |
| 1072 | ASSERT_TRUE(memcmp(src, dst, len) == 0); |
| 1073 | } |
| 1074 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1075 | TEST(STRING_TEST, memmove_align) { |
Shu Zhang | 6c80ccd | 2014-05-12 18:12:15 +0800 | [diff] [blame] | 1076 | RunSrcDstBufferAlignTest(LARGE, DoMemmoveTest); |
| 1077 | } |
| 1078 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1079 | TEST(STRING_TEST, memmove_overread) { |
Shu Zhang | 6c80ccd | 2014-05-12 18:12:15 +0800 | [diff] [blame] | 1080 | RunSrcDstBufferOverreadTest(DoMemmoveTest); |
| 1081 | } |
| 1082 | |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1083 | static void DoMemsetTest(uint8_t* buf, size_t len) { |
| 1084 | for (size_t i = 0; i < len; i++) { |
| 1085 | buf[i] = 0; |
| 1086 | } |
| 1087 | int value = (len % 255) + 1; |
| 1088 | ASSERT_EQ(buf, memset(buf, value, len)); |
| 1089 | for (size_t i = 0; i < len; i++) { |
| 1090 | ASSERT_EQ(value, buf[i]); |
| 1091 | } |
| 1092 | } |
| 1093 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1094 | TEST(STRING_TEST, memset_align) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1095 | RunSingleBufferAlignTest(LARGE, DoMemsetTest); |
| 1096 | } |
| 1097 | |
| 1098 | static void DoStrlenTest(uint8_t* buf, size_t len) { |
| 1099 | if (len >= 1) { |
| 1100 | memset(buf, (32 + (len % 96)), len - 1); |
| 1101 | buf[len-1] = '\0'; |
| 1102 | ASSERT_EQ(len-1, strlen(reinterpret_cast<char*>(buf))); |
| 1103 | } |
| 1104 | } |
| 1105 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1106 | TEST(STRING_TEST, strlen_align) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1107 | RunSingleBufferAlignTest(LARGE, DoStrlenTest); |
| 1108 | } |
| 1109 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1110 | TEST(STRING_TEST, strlen_overread) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1111 | RunSingleBufferOverreadTest(DoStrlenTest); |
| 1112 | } |
| 1113 | |
| 1114 | static void DoStrcpyTest(uint8_t* src, uint8_t* dst, size_t len) { |
| 1115 | if (len >= 1) { |
| 1116 | memset(src, (32 + (len % 96)), len - 1); |
| 1117 | src[len-1] = '\0'; |
| 1118 | memset(dst, 0, len); |
| 1119 | ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcpy(reinterpret_cast<char*>(dst), |
| 1120 | reinterpret_cast<char*>(src)))); |
| 1121 | ASSERT_TRUE(memcmp(src, dst, len) == 0); |
| 1122 | } |
| 1123 | } |
| 1124 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1125 | TEST(STRING_TEST, strcpy_align) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1126 | RunSrcDstBufferAlignTest(LARGE, DoStrcpyTest); |
| 1127 | } |
| 1128 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1129 | TEST(STRING_TEST, strcpy_overread) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1130 | RunSrcDstBufferOverreadTest(DoStrcpyTest); |
| 1131 | } |
| 1132 | |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1133 | #if defined(STRLCPY_SUPPORTED) |
| 1134 | static void DoStrlcpyTest(uint8_t* src, uint8_t* dst, size_t len) { |
| 1135 | if (len >= 1) { |
| 1136 | memset(src, (32 + (len % 96)), len - 1); |
| 1137 | src[len-1] = '\0'; |
| 1138 | memset(dst, 0, len); |
| 1139 | ASSERT_EQ(len-1, strlcpy(reinterpret_cast<char*>(dst), |
| 1140 | reinterpret_cast<char*>(src), len)); |
| 1141 | ASSERT_TRUE(memcmp(src, dst, len) == 0); |
| 1142 | } |
| 1143 | } |
| 1144 | #endif |
| 1145 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1146 | TEST(STRING_TEST, strlcpy_align) { |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1147 | #if defined(STRLCPY_SUPPORTED) |
| 1148 | RunSrcDstBufferAlignTest(LARGE, DoStrlcpyTest); |
| 1149 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1150 | GTEST_SKIP() << "strlcpy not available"; |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1151 | #endif |
| 1152 | } |
| 1153 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1154 | TEST(STRING_TEST, strlcpy_overread) { |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1155 | #if defined(STRLCPY_SUPPORTED) |
| 1156 | RunSrcDstBufferOverreadTest(DoStrlcpyTest); |
| 1157 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1158 | GTEST_SKIP() << "strlcpy not available"; |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1159 | #endif |
| 1160 | } |
| 1161 | |
| 1162 | |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 1163 | static void DoStpcpyTest(uint8_t* src, uint8_t* dst, size_t len) { |
| 1164 | if (len >= 1) { |
| 1165 | memset(src, (32 + (len % 96)), len - 1); |
| 1166 | src[len-1] = '\0'; |
| 1167 | memset(dst, 0, len); |
| 1168 | ASSERT_EQ(dst+len-1, reinterpret_cast<uint8_t*>(stpcpy(reinterpret_cast<char*>(dst), |
| 1169 | reinterpret_cast<char*>(src)))); |
| 1170 | ASSERT_TRUE(memcmp(src, dst, len) == 0); |
| 1171 | } |
| 1172 | } |
| 1173 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1174 | TEST(STRING_TEST, stpcpy_align) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 1175 | RunSrcDstBufferAlignTest(LARGE, DoStpcpyTest); |
| 1176 | } |
| 1177 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1178 | TEST(STRING_TEST, stpcpy_overread) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 1179 | RunSrcDstBufferOverreadTest(DoStpcpyTest); |
| 1180 | } |
| 1181 | |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1182 | // Use our own incrementer to cut down on the total number of calls. |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1183 | static size_t LargeSetIncrement(size_t len) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1184 | if (len >= 4096) { |
| 1185 | return 4096; |
| 1186 | } else if (len >= 1024) { |
| 1187 | return 1024; |
| 1188 | } else if (len >= 256) { |
| 1189 | return 256; |
| 1190 | } |
| 1191 | return 1; |
| 1192 | } |
| 1193 | |
Christopher Ferris | fdfcfce | 2015-09-23 22:09:09 -0700 | [diff] [blame] | 1194 | #define STRCAT_DST_LEN 64 |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1195 | |
| 1196 | static void DoStrcatTest(uint8_t* src, uint8_t* dst, size_t len) { |
| 1197 | if (len >= 1) { |
| 1198 | int value = 32 + (len % 96); |
| 1199 | memset(src, value, len - 1); |
| 1200 | src[len-1] = '\0'; |
| 1201 | |
| 1202 | if (len >= STRCAT_DST_LEN) { |
| 1203 | // Create a small buffer for doing quick compares in each loop. |
| 1204 | uint8_t cmp_buf[STRCAT_DST_LEN]; |
| 1205 | // Make sure dst string contains a different value then the src string. |
| 1206 | int value2 = 32 + (value + 2) % 96; |
| 1207 | memset(cmp_buf, value2, sizeof(cmp_buf)); |
| 1208 | |
Christopher Ferris | fdfcfce | 2015-09-23 22:09:09 -0700 | [diff] [blame] | 1209 | for (size_t i = 1; i <= STRCAT_DST_LEN;) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1210 | memset(dst, value2, i-1); |
| 1211 | memset(dst+i-1, 0, len-i); |
| 1212 | src[len-i] = '\0'; |
| 1213 | ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcat(reinterpret_cast<char*>(dst), |
| 1214 | reinterpret_cast<char*>(src)))); |
| 1215 | ASSERT_TRUE(memcmp(dst, cmp_buf, i-1) == 0); |
| 1216 | ASSERT_TRUE(memcmp(src, dst+i-1, len-i+1) == 0); |
Christopher Ferris | fdfcfce | 2015-09-23 22:09:09 -0700 | [diff] [blame] | 1217 | // This is an expensive loop, so don't loop through every value, |
| 1218 | // get to a certain size and then start doubling. |
| 1219 | if (i < 16) { |
| 1220 | i++; |
| 1221 | } else { |
| 1222 | i <<= 1; |
| 1223 | } |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1224 | } |
| 1225 | } else { |
| 1226 | dst[0] = '\0'; |
| 1227 | ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcat(reinterpret_cast<char*>(dst), |
| 1228 | reinterpret_cast<char*>(src)))); |
| 1229 | ASSERT_TRUE(memcmp(src, dst, len) == 0); |
| 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1234 | TEST(STRING_TEST, strcat_align) { |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1235 | RunSrcDstBufferAlignTest(MEDIUM, DoStrcatTest, LargeSetIncrement); |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1236 | } |
| 1237 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1238 | TEST(STRING_TEST, strcat_overread) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1239 | RunSrcDstBufferOverreadTest(DoStrcatTest); |
| 1240 | } |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1241 | |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1242 | #if defined(STRLCAT_SUPPORTED) |
| 1243 | static void DoStrlcatTest(uint8_t* src, uint8_t* dst, size_t len) { |
| 1244 | if (len >= 1) { |
| 1245 | int value = 32 + (len % 96); |
| 1246 | memset(src, value, len - 1); |
| 1247 | src[len-1] = '\0'; |
| 1248 | |
| 1249 | if (len >= STRCAT_DST_LEN) { |
| 1250 | // Create a small buffer for doing quick compares in each loop. |
| 1251 | uint8_t cmp_buf[STRCAT_DST_LEN]; |
| 1252 | // Make sure dst string contains a different value then the src string. |
| 1253 | int value2 = 32 + (value + 2) % 96; |
| 1254 | memset(cmp_buf, value2, sizeof(cmp_buf)); |
| 1255 | |
Christopher Ferris | fdfcfce | 2015-09-23 22:09:09 -0700 | [diff] [blame] | 1256 | for (size_t i = 1; i <= STRCAT_DST_LEN;) { |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1257 | memset(dst, value2, i-1); |
| 1258 | memset(dst+i-1, 0, len-i); |
| 1259 | src[len-i] = '\0'; |
| 1260 | ASSERT_EQ(len-1, strlcat(reinterpret_cast<char*>(dst), |
| 1261 | reinterpret_cast<char*>(src), len)); |
| 1262 | ASSERT_TRUE(memcmp(dst, cmp_buf, i-1) == 0); |
| 1263 | ASSERT_TRUE(memcmp(src, dst+i-1, len-i+1) == 0); |
Christopher Ferris | fdfcfce | 2015-09-23 22:09:09 -0700 | [diff] [blame] | 1264 | // This is an expensive loop, so don't loop through every value, |
| 1265 | // get to a certain size and then start doubling. |
| 1266 | if (i < 16) { |
| 1267 | i++; |
| 1268 | } else { |
| 1269 | i <<= 1; |
| 1270 | } |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1271 | } |
| 1272 | } else { |
| 1273 | dst[0] = '\0'; |
| 1274 | ASSERT_EQ(len-1, strlcat(reinterpret_cast<char*>(dst), |
| 1275 | reinterpret_cast<char*>(src), len)); |
| 1276 | ASSERT_TRUE(memcmp(src, dst, len) == 0); |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | #endif |
| 1281 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1282 | TEST(STRING_TEST, strlcat_align) { |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1283 | #if defined(STRLCAT_SUPPORTED) |
| 1284 | RunSrcDstBufferAlignTest(MEDIUM, DoStrlcatTest, LargeSetIncrement); |
| 1285 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1286 | GTEST_SKIP() << "strlcat not available"; |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1287 | #endif |
| 1288 | } |
| 1289 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1290 | TEST(STRING_TEST, strlcat_overread) { |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1291 | #if defined(STRLCAT_SUPPORTED) |
| 1292 | RunSrcDstBufferOverreadTest(DoStrlcatTest); |
| 1293 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1294 | GTEST_SKIP() << "strlcat not available"; |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1295 | #endif |
| 1296 | } |
| 1297 | |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1298 | static void DoStrcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) { |
| 1299 | if (len >= 1) { |
| 1300 | memset(buf1, (32 + (len % 96)), len - 1); |
| 1301 | buf1[len-1] = '\0'; |
| 1302 | memset(buf2, (32 + (len % 96)), len - 1); |
| 1303 | buf2[len-1] = '\0'; |
| 1304 | ASSERT_EQ(0, strcmp(reinterpret_cast<char*>(buf1), |
| 1305 | reinterpret_cast<char*>(buf2))); |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | static void DoStrcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) { |
| 1310 | // Do string length differences. |
| 1311 | int c = (32 + (len1 % 96)); |
| 1312 | memset(buf1, c, len1 - 1); |
| 1313 | buf1[len1-1] = '\0'; |
| 1314 | memset(buf2, c, len2 - 1); |
| 1315 | buf2[len2-1] = '\0'; |
| 1316 | ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1), |
| 1317 | reinterpret_cast<char*>(buf2))); |
| 1318 | |
| 1319 | // Do single character differences. |
| 1320 | size_t len; |
| 1321 | if (len1 > len2) { |
| 1322 | len = len2; |
| 1323 | } else { |
| 1324 | len = len1; |
| 1325 | } |
| 1326 | // Need at least a two character buffer to do this test. |
| 1327 | if (len > 1) { |
| 1328 | buf1[len-1] = '\0'; |
| 1329 | buf2[len-1] = '\0'; |
| 1330 | int diff_c = (c + 1) % 96; |
| 1331 | |
| 1332 | buf1[len-2] = diff_c; |
| 1333 | ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1), |
| 1334 | reinterpret_cast<char*>(buf2))); |
| 1335 | |
| 1336 | buf1[len-2] = c; |
| 1337 | buf2[len-2] = diff_c; |
| 1338 | ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1), |
| 1339 | reinterpret_cast<char*>(buf2))); |
| 1340 | } |
| 1341 | } |
| 1342 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1343 | TEST(STRING_TEST, strcmp_align) { |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1344 | RunCmpBufferAlignTest(MEDIUM, DoStrcmpTest, DoStrcmpFailTest, LargeSetIncrement); |
| 1345 | } |
| 1346 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1347 | TEST(STRING_TEST, strcmp_overread) { |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1348 | RunCmpBufferOverreadTest(DoStrcmpTest, DoStrcmpFailTest); |
| 1349 | } |
| 1350 | |
| 1351 | static void DoMemcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) { |
| 1352 | memset(buf1, len+1, len); |
| 1353 | memset(buf2, len+1, len); |
| 1354 | ASSERT_EQ(0, memcmp(buf1, buf2, len)); |
| 1355 | } |
| 1356 | |
| 1357 | static void DoMemcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) { |
| 1358 | size_t len; |
| 1359 | if (len1 > len2) { |
| 1360 | len = len2; |
| 1361 | } else { |
| 1362 | len = len1; |
| 1363 | } |
| 1364 | |
| 1365 | memset(buf1, len2+1, len); |
| 1366 | buf1[len-1] = len2; |
| 1367 | memset(buf2, len2+1, len); |
| 1368 | ASSERT_NE(0, memcmp(buf1, buf2, len)); |
| 1369 | |
| 1370 | buf1[len-1] = len2+1; |
| 1371 | buf2[len-1] = len2; |
| 1372 | ASSERT_NE(0, memcmp(buf1, buf2, len)); |
| 1373 | } |
| 1374 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1375 | TEST(STRING_TEST, memcmp_align) { |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1376 | RunCmpBufferAlignTest(MEDIUM, DoMemcmpTest, DoMemcmpFailTest, LargeSetIncrement); |
| 1377 | } |
| 1378 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1379 | TEST(STRING_TEST, memcmp_overread) { |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1380 | RunCmpBufferOverreadTest(DoMemcmpTest, DoMemcmpFailTest); |
| 1381 | } |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 1382 | |
Christopher Ferris | 2f030af | 2017-05-08 14:24:29 -0700 | [diff] [blame] | 1383 | static void DoMemchrTest(uint8_t* buf, size_t len) { |
| 1384 | if (len >= 1) { |
| 1385 | int value = len % 128; |
| 1386 | int search_value = (len % 128) + 1; |
| 1387 | memset(buf, value, len); |
| 1388 | // The buffer does not contain the search value. |
| 1389 | ASSERT_EQ(nullptr, memchr(buf, search_value, len)); |
| 1390 | if (len >= 2) { |
| 1391 | buf[0] = search_value; |
| 1392 | // The search value is the first element in the buffer. |
| 1393 | ASSERT_EQ(&buf[0], memchr(buf, search_value, len)); |
| 1394 | |
| 1395 | buf[0] = value; |
| 1396 | buf[len - 1] = search_value; |
| 1397 | // The search value is the last element in the buffer. |
| 1398 | ASSERT_EQ(&buf[len - 1], memchr(buf, search_value, len)); |
| 1399 | } |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | TEST(STRING_TEST, memchr_align) { |
| 1404 | RunSingleBufferAlignTest(MEDIUM, DoMemchrTest); |
| 1405 | } |
| 1406 | |
| 1407 | TEST(STRING_TEST, memchr_overread) { |
| 1408 | RunSingleBufferOverreadTest(DoMemchrTest); |
| 1409 | } |
| 1410 | |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 1411 | static void DoStrchrTest(uint8_t* buf, size_t len) { |
| 1412 | if (len >= 1) { |
| 1413 | char value = 32 + (len % 96); |
| 1414 | char search_value = 33 + (len % 96); |
| 1415 | memset(buf, value, len - 1); |
Christopher Ferris | 2f030af | 2017-05-08 14:24:29 -0700 | [diff] [blame] | 1416 | buf[len - 1] = '\0'; |
| 1417 | // The buffer does not contain the search value. |
| 1418 | ASSERT_EQ(nullptr, strchr(reinterpret_cast<char*>(buf), search_value)); |
| 1419 | // Search for the special '\0' character. |
| 1420 | ASSERT_EQ(reinterpret_cast<char*>(&buf[len - 1]), strchr(reinterpret_cast<char*>(buf), '\0')); |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 1421 | if (len >= 2) { |
| 1422 | buf[0] = search_value; |
Christopher Ferris | 2f030af | 2017-05-08 14:24:29 -0700 | [diff] [blame] | 1423 | // The search value is the first element in the buffer. |
| 1424 | ASSERT_EQ(reinterpret_cast<char*>(&buf[0]), strchr(reinterpret_cast<char*>(buf), |
| 1425 | search_value)); |
| 1426 | |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 1427 | buf[0] = value; |
Christopher Ferris | 2f030af | 2017-05-08 14:24:29 -0700 | [diff] [blame] | 1428 | buf[len - 2] = search_value; |
| 1429 | // The search value is the second to last element in the buffer. |
| 1430 | // The last element is the '\0' character. |
| 1431 | ASSERT_EQ(reinterpret_cast<char*>(&buf[len - 2]), strchr(reinterpret_cast<char*>(buf), |
| 1432 | search_value)); |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1437 | TEST(STRING_TEST, strchr_align) { |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 1438 | RunSingleBufferAlignTest(MEDIUM, DoStrchrTest); |
| 1439 | } |
| 1440 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1441 | TEST(STRING_TEST, strchr_overread) { |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 1442 | RunSingleBufferOverreadTest(DoStrchrTest); |
| 1443 | } |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 1444 | |
Christopher Ferris | 2f030af | 2017-05-08 14:24:29 -0700 | [diff] [blame] | 1445 | static void DoStrrchrTest(uint8_t* buf, size_t len) { |
| 1446 | if (len >= 1) { |
| 1447 | char value = 32 + (len % 96); |
| 1448 | char search_value = 33 + (len % 96); |
| 1449 | memset(buf, value, len - 1); |
| 1450 | buf[len - 1] = '\0'; |
| 1451 | // The buffer does not contain the search value. |
| 1452 | ASSERT_EQ(nullptr, strrchr(reinterpret_cast<char*>(buf), search_value)); |
| 1453 | // Search for the special '\0' character. |
| 1454 | ASSERT_EQ(reinterpret_cast<char*>(&buf[len - 1]), strrchr(reinterpret_cast<char*>(buf), '\0')); |
| 1455 | if (len >= 2) { |
| 1456 | buf[0] = search_value; |
| 1457 | // The search value is the first element in the buffer. |
| 1458 | ASSERT_EQ(reinterpret_cast<char*>(&buf[0]), strrchr(reinterpret_cast<char*>(buf), |
| 1459 | search_value)); |
| 1460 | |
| 1461 | buf[0] = value; |
| 1462 | buf[len - 2] = search_value; |
| 1463 | // The search value is the second to last element in the buffer. |
| 1464 | // The last element is the '\0' character. |
| 1465 | ASSERT_EQ(reinterpret_cast<char*>(&buf[len - 2]), strrchr(reinterpret_cast<char*>(buf), |
| 1466 | search_value)); |
| 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | |
| 1471 | TEST(STRING_TEST, strrchr_align) { |
| 1472 | RunSingleBufferAlignTest(MEDIUM, DoStrrchrTest); |
| 1473 | } |
| 1474 | |
| 1475 | TEST(STRING_TEST, strrchr_overread) { |
| 1476 | RunSingleBufferOverreadTest(DoStrrchrTest); |
| 1477 | } |
| 1478 | |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 1479 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 1480 | static void TestBasename(const char* in, const char* expected_out) { |
| 1481 | errno = 0; |
| 1482 | const char* out = basename(in); |
| 1483 | ASSERT_STREQ(expected_out, out) << in; |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame] | 1484 | ASSERT_ERRNO(0) << in; |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 1485 | } |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 1486 | #endif |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 1487 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1488 | TEST(STRING_TEST, __gnu_basename) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 1489 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 1490 | TestBasename("", ""); |
| 1491 | TestBasename("/usr/lib", "lib"); |
| 1492 | TestBasename("/usr/", ""); |
| 1493 | TestBasename("usr", "usr"); |
| 1494 | TestBasename("/", ""); |
| 1495 | TestBasename(".", "."); |
| 1496 | TestBasename("..", ".."); |
| 1497 | TestBasename("///", ""); |
| 1498 | TestBasename("//usr//lib//", ""); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 1499 | #else |
| 1500 | GTEST_SKIP() << "musl doesn't have GNU basename"; |
| 1501 | #endif |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 1502 | } |
Elliott Hughes | 41ef902 | 2015-02-14 13:21:22 -0800 | [diff] [blame] | 1503 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1504 | TEST(STRING_TEST, strnlen_147048) { |
Elliott Hughes | 41ef902 | 2015-02-14 13:21:22 -0800 | [diff] [blame] | 1505 | // https://code.google.com/p/android/issues/detail?id=147048 |
| 1506 | char stack_src[64] = {0}; |
| 1507 | EXPECT_EQ(0U, strnlen(stack_src, 1024*1024*1024)); |
| 1508 | char* heap_src = new char[1]; |
| 1509 | *heap_src = '\0'; |
| 1510 | EXPECT_EQ(0U, strnlen(heap_src, 1024*1024*1024)); |
| 1511 | delete[] heap_src; |
| 1512 | } |
Elliott Hughes | 3cfb52a | 2015-02-18 21:29:13 -0800 | [diff] [blame] | 1513 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1514 | TEST(STRING_TEST, strnlen_74741) { |
Elliott Hughes | d2a9fb3 | 2015-07-27 20:55:03 -0700 | [diff] [blame] | 1515 | ASSERT_EQ(4U, strnlen("test", SIZE_MAX)); |
| 1516 | } |
| 1517 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1518 | TEST(STRING_TEST, mempcpy) { |
Elliott Hughes | 3cfb52a | 2015-02-18 21:29:13 -0800 | [diff] [blame] | 1519 | char dst[6]; |
| 1520 | ASSERT_EQ(&dst[4], reinterpret_cast<char*>(mempcpy(dst, "hello", 4))); |
| 1521 | } |
Christopher Ferris | 71766c2 | 2016-02-12 17:24:27 -0800 | [diff] [blame] | 1522 | |
| 1523 | // clang depends on the fact that a memcpy where src and dst is the same |
| 1524 | // still operates correctly. This test verifies that this assumption |
| 1525 | // holds true. |
| 1526 | // See https://llvm.org/bugs/show_bug.cgi?id=11763 for more information. |
| 1527 | static std::vector<uint8_t> g_memcpy_same_buffer; |
| 1528 | |
| 1529 | static void DoMemcpySameTest(uint8_t* buffer, size_t len) { |
| 1530 | memcpy(buffer, g_memcpy_same_buffer.data(), len); |
| 1531 | ASSERT_EQ(buffer, memcpy(buffer, buffer, len)); |
| 1532 | ASSERT_TRUE(memcmp(buffer, g_memcpy_same_buffer.data(), len) == 0); |
| 1533 | } |
| 1534 | |
| 1535 | TEST(STRING_TEST, memcpy_src_dst_same) { |
| 1536 | g_memcpy_same_buffer.resize(MEDIUM); |
| 1537 | for (size_t i = 0; i < MEDIUM; i++) { |
| 1538 | g_memcpy_same_buffer[i] = i; |
| 1539 | } |
| 1540 | RunSingleBufferAlignTest(MEDIUM, DoMemcpySameTest); |
| 1541 | } |
Elliott Hughes | cae33ad | 2016-08-15 14:14:40 -0700 | [diff] [blame] | 1542 | |
| 1543 | TEST(STRING_TEST, memmem_strstr_empty_needle) { |
| 1544 | const char* some_haystack = "haystack"; |
| 1545 | const char* empty_haystack = ""; |
| 1546 | |
| 1547 | ASSERT_EQ(some_haystack, memmem(some_haystack, 8, "", 0)); |
| 1548 | ASSERT_EQ(empty_haystack, memmem(empty_haystack, 0, "", 0)); |
| 1549 | |
| 1550 | ASSERT_EQ(some_haystack, strstr(some_haystack, "")); |
| 1551 | ASSERT_EQ(empty_haystack, strstr(empty_haystack, "")); |
| 1552 | } |
| 1553 | |
| 1554 | TEST(STRING_TEST, memmem_smoke) { |
Elliott Hughes | 5633caa | 2020-08-06 14:32:43 -0700 | [diff] [blame] | 1555 | const char haystack[] = "big\0daddy/giant\0haystacks!"; |
| 1556 | |
| 1557 | // The current memmem() implementation has special cases for needles of |
| 1558 | // lengths 0, 1, 2, 3, and 4, plus a long needle case. We test matches at the |
| 1559 | // beginning, middle, and end of the haystack. |
| 1560 | |
| 1561 | ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "", 0)); |
| 1562 | |
Elliott Hughes | cae33ad | 2016-08-15 14:14:40 -0700 | [diff] [blame] | 1563 | ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "b", 1)); |
Elliott Hughes | 5633caa | 2020-08-06 14:32:43 -0700 | [diff] [blame] | 1564 | ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "bi", 2)); |
| 1565 | ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "big", 3)); |
| 1566 | ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "big\0", 4)); |
| 1567 | ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "big\0d", 5)); |
| 1568 | |
| 1569 | ASSERT_EQ(haystack + 2, memmem(haystack, sizeof(haystack), "g", 1)); |
| 1570 | ASSERT_EQ(haystack + 10, memmem(haystack, sizeof(haystack), "gi", 2)); |
| 1571 | ASSERT_EQ(haystack + 10, memmem(haystack, sizeof(haystack), "gia", 3)); |
| 1572 | ASSERT_EQ(haystack + 10, memmem(haystack, sizeof(haystack), "gian", 4)); |
| 1573 | ASSERT_EQ(haystack + 10, memmem(haystack, sizeof(haystack), "giant", 5)); |
| 1574 | |
| 1575 | ASSERT_EQ(haystack + 25, memmem(haystack, sizeof(haystack), "!", 1)); |
| 1576 | ASSERT_EQ(haystack + 24, memmem(haystack, sizeof(haystack), "s!", 2)); |
| 1577 | ASSERT_EQ(haystack + 23, memmem(haystack, sizeof(haystack), "ks!", 3)); |
| 1578 | ASSERT_EQ(haystack + 22, memmem(haystack, sizeof(haystack), "cks!", 4)); |
| 1579 | ASSERT_EQ(haystack + 21, memmem(haystack, sizeof(haystack), "acks!", 5)); |
Elliott Hughes | cae33ad | 2016-08-15 14:14:40 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | TEST(STRING_TEST, strstr_smoke) { |
Elliott Hughes | c6b38ae | 2019-11-22 11:15:42 -0800 | [diff] [blame] | 1583 | const char* haystack = "big daddy/giant haystacks!"; |
| 1584 | |
| 1585 | // The current strstr() implementation has special cases for needles of |
| 1586 | // lengths 0, 1, 2, 3, and 4, plus a long needle case. We test matches at the |
| 1587 | // beginning, middle, and end of the haystack. |
| 1588 | |
| 1589 | ASSERT_EQ(haystack + 0, strstr(haystack, "")); |
| 1590 | |
Elliott Hughes | cae33ad | 2016-08-15 14:14:40 -0700 | [diff] [blame] | 1591 | ASSERT_EQ(haystack + 0, strstr(haystack, "b")); |
Elliott Hughes | c6b38ae | 2019-11-22 11:15:42 -0800 | [diff] [blame] | 1592 | ASSERT_EQ(haystack + 0, strstr(haystack, "bi")); |
| 1593 | ASSERT_EQ(haystack + 0, strstr(haystack, "big")); |
| 1594 | ASSERT_EQ(haystack + 0, strstr(haystack, "big ")); |
| 1595 | ASSERT_EQ(haystack + 0, strstr(haystack, "big d")); |
| 1596 | |
| 1597 | ASSERT_EQ(haystack + 2, strstr(haystack, "g")); |
| 1598 | ASSERT_EQ(haystack + 10, strstr(haystack, "gi")); |
| 1599 | ASSERT_EQ(haystack + 10, strstr(haystack, "gia")); |
| 1600 | ASSERT_EQ(haystack + 10, strstr(haystack, "gian")); |
| 1601 | ASSERT_EQ(haystack + 10, strstr(haystack, "giant")); |
| 1602 | |
| 1603 | ASSERT_EQ(haystack + 25, strstr(haystack, "!")); |
| 1604 | ASSERT_EQ(haystack + 24, strstr(haystack, "s!")); |
| 1605 | ASSERT_EQ(haystack + 23, strstr(haystack, "ks!")); |
| 1606 | ASSERT_EQ(haystack + 22, strstr(haystack, "cks!")); |
| 1607 | ASSERT_EQ(haystack + 21, strstr(haystack, "acks!")); |
Elliott Hughes | cae33ad | 2016-08-15 14:14:40 -0700 | [diff] [blame] | 1608 | } |
Elliott Hughes | fbac97a | 2019-02-04 16:46:24 -0800 | [diff] [blame] | 1609 | |
| 1610 | TEST(STRING_TEST, strcasestr_smoke) { |
| 1611 | const char* haystack = "bIg dAdDy/gIaNt hAyStAcKs"; |
| 1612 | ASSERT_EQ(haystack, strcasestr(haystack, "")); |
| 1613 | ASSERT_EQ(haystack + 0, strcasestr(haystack, "B")); |
| 1614 | ASSERT_EQ(haystack + 1, strcasestr(haystack, "i")); |
| 1615 | ASSERT_EQ(haystack + 4, strcasestr(haystack, "Da")); |
| 1616 | } |
| 1617 | |
| 1618 | TEST(STRING_TEST, strcoll_smoke) { |
| 1619 | ASSERT_TRUE(strcoll("aab", "aac") < 0); |
| 1620 | ASSERT_TRUE(strcoll("aab", "aab") == 0); |
| 1621 | ASSERT_TRUE(strcoll("aac", "aab") > 0); |
| 1622 | } |
| 1623 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 1624 | TEST(STRING_TEST, strcoll_l_smoke) { |
| 1625 | // bionic just forwards to strcoll(3). |
| 1626 | ASSERT_TRUE(strcoll_l("aab", "aac", LC_GLOBAL_LOCALE) < 0); |
| 1627 | ASSERT_TRUE(strcoll_l("aab", "aab", LC_GLOBAL_LOCALE) == 0); |
| 1628 | ASSERT_TRUE(strcoll_l("aac", "aab", LC_GLOBAL_LOCALE) > 0); |
| 1629 | } |
| 1630 | |
Elliott Hughes | fbac97a | 2019-02-04 16:46:24 -0800 | [diff] [blame] | 1631 | TEST(STRING_TEST, strxfrm_smoke) { |
| 1632 | const char* src1 = "aab"; |
| 1633 | char dst1[16] = {}; |
Elliott Hughes | 2e71c17 | 2020-08-06 13:53:40 -0700 | [diff] [blame] | 1634 | // Dry run. |
| 1635 | ASSERT_EQ(strxfrm(dst1, src1, 0), 3U); |
| 1636 | ASSERT_STREQ(dst1, ""); |
| 1637 | // Really do it. |
| 1638 | ASSERT_EQ(strxfrm(dst1, src1, sizeof(dst1)), 3U); |
| 1639 | |
Elliott Hughes | fbac97a | 2019-02-04 16:46:24 -0800 | [diff] [blame] | 1640 | const char* src2 = "aac"; |
| 1641 | char dst2[16] = {}; |
Elliott Hughes | 2e71c17 | 2020-08-06 13:53:40 -0700 | [diff] [blame] | 1642 | // Dry run. |
| 1643 | ASSERT_EQ(strxfrm(dst2, src2, 0), 3U); |
| 1644 | ASSERT_STREQ(dst2, ""); |
| 1645 | // Really do it. |
| 1646 | ASSERT_EQ(strxfrm(dst2, src2, sizeof(dst2)), 3U); |
| 1647 | |
| 1648 | // The "transform" of two different strings should cause different outputs. |
Elliott Hughes | fbac97a | 2019-02-04 16:46:24 -0800 | [diff] [blame] | 1649 | ASSERT_TRUE(strcmp(dst1, dst2) < 0); |
| 1650 | } |
| 1651 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 1652 | TEST(STRING_TEST, strxfrm_l_smoke) { |
| 1653 | // bionic just forwards to strxfrm(3), so this is a subset of the |
| 1654 | // strxfrm test. |
| 1655 | const char* src1 = "aab"; |
| 1656 | char dst1[16] = {}; |
| 1657 | ASSERT_EQ(strxfrm_l(dst1, src1, 0, LC_GLOBAL_LOCALE), 3U); |
| 1658 | ASSERT_STREQ(dst1, ""); |
| 1659 | ASSERT_EQ(strxfrm_l(dst1, src1, sizeof(dst1), LC_GLOBAL_LOCALE), 3U); |
| 1660 | } |
| 1661 | |
Elliott Hughes | fbac97a | 2019-02-04 16:46:24 -0800 | [diff] [blame] | 1662 | TEST(STRING_TEST, memccpy_smoke) { |
| 1663 | char dst[32]; |
| 1664 | |
| 1665 | memset(dst, 0, sizeof(dst)); |
| 1666 | char* p = static_cast<char*>(memccpy(dst, "hello world", ' ', 32)); |
| 1667 | ASSERT_STREQ("hello ", dst); |
| 1668 | ASSERT_EQ(ptrdiff_t(6), p - dst); |
| 1669 | |
| 1670 | memset(dst, 0, sizeof(dst)); |
| 1671 | ASSERT_EQ(nullptr, memccpy(dst, "hello world", ' ', 4)); |
| 1672 | ASSERT_STREQ("hell", dst); |
| 1673 | } |
Elliott Hughes | 0d64243 | 2022-08-10 23:35:03 +0000 | [diff] [blame] | 1674 | |
| 1675 | TEST(STRING_TEST, memset_explicit_smoke) { |
| 1676 | #if defined(__BIONIC__) |
| 1677 | // We can't reliably test that the compiler won't optimize out calls to |
| 1678 | // memset_explicit(), but we can at least check that it behaves like memset. |
| 1679 | char buf[32]; |
| 1680 | memset_explicit(buf, 'x', sizeof(buf)); |
| 1681 | ASSERT_TRUE(memcmp(buf, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", sizeof(buf)) == 0); |
| 1682 | #else |
| 1683 | GTEST_SKIP() << "memset_explicit not available"; |
| 1684 | #endif |
| 1685 | } |
Elliott Hughes | 2109f12 | 2023-09-21 18:32:39 -0700 | [diff] [blame] | 1686 | |
| 1687 | TEST(STRING_TEST, strerrorname_np) { |
| 1688 | #if defined(__BIONIC__) |
| 1689 | ASSERT_STREQ("0", strerrorname_np(0)); |
| 1690 | ASSERT_STREQ("EINVAL", strerrorname_np(EINVAL)); |
| 1691 | ASSERT_STREQ("ENOSYS", strerrorname_np(ENOSYS)); |
| 1692 | |
| 1693 | ASSERT_EQ(nullptr, strerrorname_np(-1)); |
| 1694 | ASSERT_EQ(nullptr, strerrorname_np(666)); |
| 1695 | #else |
| 1696 | GTEST_SKIP() << "strerrorname_np not available"; |
| 1697 | #endif |
| 1698 | } |