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]; |
| 322 | char* orig = strdup(""); |
| 323 | ASSERT_EQ(buf, stpcpy(buf, orig)); |
| 324 | ASSERT_EQ('\0', buf[0]); |
| 325 | free(orig); |
| 326 | } |
| 327 | |
| 328 | // multibyte target where we under fill target |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 329 | TEST(STRING_TEST, stpcpy3) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 330 | char buf[10]; |
| 331 | char* orig = strdup("12345"); |
| 332 | memset(buf, 'A', sizeof(buf)); |
| 333 | ASSERT_EQ(buf+strlen(orig), stpcpy(buf, orig)); |
| 334 | ASSERT_STREQ("12345", buf); |
| 335 | ASSERT_EQ('A', buf[6]); |
| 336 | ASSERT_EQ('A', buf[7]); |
| 337 | ASSERT_EQ('A', buf[8]); |
| 338 | ASSERT_EQ('A', buf[9]); |
| 339 | free(orig); |
| 340 | } |
| 341 | |
| 342 | // multibyte target where we fill target exactly |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 343 | TEST(STRING_TEST, stpcpy4) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 344 | char buf[10]; |
| 345 | char* orig = strdup("123456789"); |
| 346 | memset(buf, 'A', sizeof(buf)); |
| 347 | ASSERT_EQ(buf+strlen(orig), stpcpy(buf, orig)); |
| 348 | ASSERT_STREQ("123456789", buf); |
Nick Kralevich | 13476de | 2013-06-03 10:58:06 -0700 | [diff] [blame] | 349 | free(orig); |
| 350 | } |
| 351 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 352 | TEST(STRING_TEST, strcat2) { |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 353 | char buf[10]; |
| 354 | memset(buf, 'A', sizeof(buf)); |
| 355 | buf[0] = 'a'; |
| 356 | buf[1] = '\0'; |
| 357 | char* res = strcat(buf, "01234"); |
| 358 | ASSERT_EQ(buf, res); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 359 | ASSERT_STREQ("a01234", buf); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 360 | ASSERT_EQ('A', buf[7]); |
| 361 | ASSERT_EQ('A', buf[8]); |
| 362 | ASSERT_EQ('A', buf[9]); |
| 363 | } |
| 364 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 365 | TEST(STRING_TEST, strcat3) { |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 366 | char buf[10]; |
| 367 | memset(buf, 'A', sizeof(buf)); |
| 368 | buf[0] = 'a'; |
| 369 | buf[1] = '\0'; |
| 370 | char* res = strcat(buf, "01234567"); |
| 371 | ASSERT_EQ(buf, res); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 372 | ASSERT_STREQ("a01234567", buf); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 375 | TEST(STRING_TEST, strncat2) { |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 376 | char buf[10]; |
| 377 | memset(buf, 'A', sizeof(buf)); |
| 378 | buf[0] = 'a'; |
| 379 | buf[1] = '\0'; |
| 380 | char* res = strncat(buf, "01234", sizeof(buf) - strlen(buf) - 1); |
| 381 | ASSERT_EQ(buf, res); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 382 | ASSERT_STREQ("a01234", buf); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 383 | ASSERT_EQ('A', buf[7]); |
| 384 | ASSERT_EQ('A', buf[8]); |
| 385 | ASSERT_EQ('A', buf[9]); |
| 386 | } |
| 387 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 388 | TEST(STRING_TEST, strncat3) { |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 389 | char buf[10]; |
| 390 | memset(buf, 'A', sizeof(buf)); |
| 391 | buf[0] = 'a'; |
| 392 | buf[1] = '\0'; |
| 393 | char* res = strncat(buf, "0123456789", 5); |
| 394 | ASSERT_EQ(buf, res); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 395 | ASSERT_STREQ("a01234", buf); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 396 | ASSERT_EQ('A', buf[7]); |
| 397 | ASSERT_EQ('A', buf[8]); |
| 398 | ASSERT_EQ('A', buf[9]); |
| 399 | } |
| 400 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 401 | TEST(STRING_TEST, strncat4) { |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 402 | char buf[10]; |
| 403 | memset(buf, 'A', sizeof(buf)); |
| 404 | buf[0] = 'a'; |
| 405 | buf[1] = '\0'; |
| 406 | char* res = strncat(buf, "01234567", 8); |
| 407 | ASSERT_EQ(buf, res); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 408 | ASSERT_STREQ("a01234567", buf); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 411 | TEST(STRING_TEST, strncat5) { |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 412 | char buf[10]; |
| 413 | memset(buf, 'A', sizeof(buf)); |
| 414 | buf[0] = 'a'; |
| 415 | buf[1] = '\0'; |
| 416 | char* res = strncat(buf, "01234567", 9); |
| 417 | ASSERT_EQ(buf, res); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 418 | ASSERT_STREQ("a01234567", buf); |
Nick Kralevich | cf87019 | 2013-05-30 16:48:53 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 421 | TEST(STRING_TEST, strchr_with_0) { |
Nick Kralevich | 4f40e51 | 2013-04-19 16:54:22 -0700 | [diff] [blame] | 422 | char buf[10]; |
| 423 | const char* s = "01234"; |
| 424 | memcpy(buf, s, strlen(s) + 1); |
| 425 | EXPECT_TRUE(strchr(buf, '\0') == (buf + strlen(s))); |
| 426 | } |
| 427 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 428 | TEST(STRING_TEST, strchr_multiple) { |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 429 | char str[128]; |
| 430 | memset(str, 'a', sizeof(str) - 1); |
| 431 | str[sizeof(str)-1] = '\0'; |
| 432 | |
| 433 | // Verify that strchr finds the first occurrence of 'a' in a string |
| 434 | // filled with 'a' characters. Iterate over the string putting |
| 435 | // non 'a' characters at the front of the string during each iteration |
| 436 | // and continue to verify that strchr can find the first occurrence |
| 437 | // properly. The idea is to cover all possible alignments of the location |
| 438 | // of the first occurrence of the 'a' character and which includes |
| 439 | // other 'a' characters close by. |
| 440 | for (size_t i = 0; i < sizeof(str) - 1; i++) { |
| 441 | EXPECT_EQ(&str[i], strchr(str, 'a')); |
| 442 | str[i] = 'b'; |
| 443 | } |
| 444 | } |
| 445 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 446 | TEST(STRING_TEST, strchr) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 447 | int seek_char = 'R'; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 448 | |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 449 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 450 | for (size_t i = 1; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 451 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 452 | if (~seek_char > 0) { |
| 453 | memset(state.ptr1, ~seek_char, state.len[i]); |
| 454 | } else { |
| 455 | memset(state.ptr1, '\1', state.len[i]); |
| 456 | } |
| 457 | state.ptr1[state.len[i] - 1] = '\0'; |
| 458 | |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 459 | size_t pos = random() % state.MAX_LEN; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 460 | char* expected; |
| 461 | if (pos >= state.len[i] - 1) { |
| 462 | if (seek_char == 0) { |
| 463 | expected = state.ptr1 + state.len[i] - 1; |
| 464 | } else { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 465 | expected = nullptr; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 466 | } |
| 467 | } else { |
| 468 | state.ptr1[pos] = seek_char; |
| 469 | expected = state.ptr1 + pos; |
| 470 | } |
| 471 | |
| 472 | ASSERT_TRUE(strchr(state.ptr1, seek_char) == expected); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 477 | TEST(STRING_TEST, strchrnul) { |
Elliott Hughes | 7ac3c12 | 2015-08-26 09:59:29 -0700 | [diff] [blame] | 478 | const char* s = "01234222"; |
| 479 | EXPECT_TRUE(strchrnul(s, '2') == &s[2]); |
| 480 | EXPECT_TRUE(strchrnul(s, '8') == (s + strlen(s))); |
| 481 | EXPECT_TRUE(strchrnul(s, '\0') == (s + strlen(s))); |
| 482 | } |
| 483 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 484 | TEST(STRING_TEST, strcmp) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 485 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 486 | for (size_t i = 1; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 487 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 488 | memset(state.ptr1, 'v', state.MAX_LEN); |
| 489 | memset(state.ptr2, 'n', state.MAX_LEN); |
| 490 | state.ptr1[state.len[i] - 1] = '\0'; |
| 491 | state.ptr2[state.len[i] - 1] = '\0'; |
| 492 | |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 493 | size_t pos = 1 + (random() % (state.MAX_LEN - 1)); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 494 | int actual; |
| 495 | int expected; |
| 496 | if (pos >= state.len[i] - 1) { |
| 497 | memcpy(state.ptr1, state.ptr2, state.len[i]); |
| 498 | expected = 0; |
| 499 | actual = strcmp(state.ptr1, state.ptr2); |
| 500 | } else { |
| 501 | memcpy(state.ptr1, state.ptr2, pos); |
| 502 | if (state.ptr1[pos] > state.ptr2[pos]) { |
| 503 | expected = 1; |
| 504 | } else if (state.ptr1[pos] == state.ptr2[pos]) { |
| 505 | state.ptr1[pos + 1] = '\0'; |
| 506 | state.ptr2[pos + 1] = '\0'; |
| 507 | expected = 0; |
| 508 | } else { |
| 509 | expected = -1; |
| 510 | } |
| 511 | actual = strcmp(state.ptr1, state.ptr2); |
| 512 | } |
| 513 | |
| 514 | ASSERT_EQ(expected, signum(actual)); |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 519 | TEST(STRING_TEST, stpcpy) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 520 | StringTestState<char> state(SMALL); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 521 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 522 | size_t pos = random() % state.MAX_LEN; |
| 523 | |
| 524 | memset(state.ptr1, '\2', pos); |
| 525 | state.ptr1[pos] = '\0'; |
| 526 | state.ptr1[state.MAX_LEN - 1] = '\0'; |
| 527 | |
| 528 | memcpy(state.ptr, state.ptr1, state.MAX_LEN); |
| 529 | |
| 530 | memset(state.ptr2, '\1', state.MAX_LEN); |
| 531 | state.ptr2[state.MAX_LEN - 1] = '\0'; |
| 532 | |
| 533 | memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN); |
| 534 | memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1); |
| 535 | state.ptr[2 * state.MAX_LEN - 1] = '\0'; |
| 536 | |
| 537 | ASSERT_TRUE(stpcpy(state.ptr2, state.ptr1) == state.ptr2 + strlen(state.ptr1)); |
| 538 | ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN)) != 0 || |
| 539 | (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0)); |
| 540 | } |
| 541 | } |
| 542 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 543 | TEST(STRING_TEST, strcpy) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 544 | StringTestState<char> state(SMALL); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 545 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 546 | size_t pos = random() % state.MAX_LEN; |
| 547 | |
| 548 | memset(state.ptr1, '\2', pos); |
| 549 | state.ptr1[pos] = '\0'; |
| 550 | state.ptr1[state.MAX_LEN - 1] = '\0'; |
| 551 | |
| 552 | memcpy(state.ptr, state.ptr1, state.MAX_LEN); |
| 553 | |
| 554 | memset(state.ptr2, '\1', state.MAX_LEN); |
| 555 | state.ptr2[state.MAX_LEN - 1] = '\0'; |
| 556 | |
| 557 | memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN); |
| 558 | memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1); |
| 559 | state.ptr[2 * state.MAX_LEN - 1] = '\0'; |
| 560 | |
| 561 | ASSERT_TRUE(strcpy(state.ptr2, state.ptr1) == state.ptr2); |
| 562 | ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN)) != 0 || |
| 563 | (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0)); |
| 564 | } |
| 565 | } |
| 566 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 567 | TEST(STRING_TEST, strlcat) { |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 568 | #if defined(STRLCAT_SUPPORTED) |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 569 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 570 | for (size_t i = 0; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 571 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 572 | memset(state.ptr2, '\2', state.MAX_LEN + state.len[i]); |
| 573 | state.ptr2[state.MAX_LEN - 1] = '\0'; |
| 574 | memcpy(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]); |
| 575 | |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 576 | size_t pos = random() % state.MAX_LEN; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 577 | memset(state.ptr1, '\3', pos); |
| 578 | state.ptr1[pos] = '\0'; |
| 579 | if (pos < state.len[i]) { |
| 580 | memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, pos + 1); |
| 581 | } else { |
| 582 | memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, state.len[i]); |
| 583 | state.ptr[state.MAX_LEN + state.len[i] - 1] = '\0'; |
| 584 | } |
| 585 | |
| 586 | strlcat(state.ptr2, state.ptr1, state.MAX_LEN + state.len[i]); |
| 587 | |
| 588 | ASSERT_TRUE(memcmp(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]) == 0); |
| 589 | } |
| 590 | } |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 591 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 592 | GTEST_SKIP() << "strlcat not available"; |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 593 | #endif |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 594 | } |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 595 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 596 | TEST(STRING_TEST, strlcpy) { |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 597 | #if defined(STRLCPY_SUPPORTED) |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 598 | StringTestState<char> state(SMALL); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 599 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
| 600 | int rand = 'O'; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 601 | memset(state.ptr1, rand, state.MAX_LEN); |
| 602 | |
| 603 | size_t pos = random() % state.MAX_LEN; |
| 604 | if (pos < state.MAX_LEN) { |
| 605 | state.ptr1[pos] = '\0'; |
| 606 | } |
| 607 | memcpy(state.ptr, state.ptr1, state.MAX_LEN); |
| 608 | |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 609 | memset(state.ptr2, 'I', state.MAX_LEN); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 610 | memcpy(state.ptr + state.MAX_LEN, state.ptr2, state.MAX_LEN); |
| 611 | |
| 612 | if (pos > state.MAX_LEN - 1) { |
| 613 | memcpy(state.ptr + state.MAX_LEN, state.ptr1, state.MAX_LEN); |
| 614 | state.ptr[2 * state.MAX_LEN - 1] = '\0'; |
| 615 | } else { |
| 616 | memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1); |
| 617 | } |
| 618 | |
| 619 | ASSERT_EQ(strlcpy(state.ptr2, state.ptr1, state.MAX_LEN), strlen(state.ptr1)); |
| 620 | ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN) != 0) || |
| 621 | (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0)); |
| 622 | } |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 623 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 624 | GTEST_SKIP() << "strlcpy not available"; |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 625 | #endif |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 626 | } |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 627 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 628 | TEST(STRING_TEST, strncat) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 629 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 630 | for (size_t i = 1; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 631 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 632 | memset(state.ptr2, '\2', state.MAX_LEN); |
| 633 | state.ptr2[state.MAX_LEN - 1] = '\0'; |
| 634 | memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN); |
| 635 | |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 636 | memset(state.ptr1, 'I', state.len[i]); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 637 | state.ptr1[random() % state.len[i]] = '\0'; |
| 638 | state.ptr1[state.len[i] - 1] = '\0'; |
| 639 | |
| 640 | size_t pos = strlen(state.ptr1); |
| 641 | |
| 642 | size_t actual = random() % state.len[i]; |
| 643 | strncpy(state.ptr + state.MAX_LEN - 1, state.ptr1, std::min(actual, pos)); |
| 644 | state.ptr[state.MAX_LEN + std::min(actual, pos) - 1] = '\0'; |
| 645 | |
| 646 | ASSERT_TRUE(strncat(state.ptr2, state.ptr1, actual) == state.ptr2); |
| 647 | ASSERT_EQ(memcmp(state.ptr, state.ptr2, 2 * state.MAX_LEN), 0); |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 652 | TEST(STRING_TEST, strncmp) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 653 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 654 | for (size_t i = 1; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 655 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 656 | memset(state.ptr1, 'v', state.MAX_LEN); |
| 657 | memset(state.ptr2, 'n', state.MAX_LEN); |
| 658 | state.ptr1[state.len[i] - 1] = '\0'; |
| 659 | state.ptr2[state.len[i] - 1] = '\0'; |
| 660 | |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 661 | size_t pos = 1 + (random() % (state.MAX_LEN - 1)); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 662 | int actual; |
| 663 | int expected; |
| 664 | if (pos >= state.len[i] - 1) { |
| 665 | memcpy(state.ptr1, state.ptr2, state.len[i]); |
| 666 | expected = 0; |
| 667 | actual = strncmp(state.ptr1, state.ptr2, state.len[i]); |
| 668 | } else { |
| 669 | memcpy(state.ptr1, state.ptr2, pos); |
| 670 | if (state.ptr1[pos] > state.ptr2[pos]) { |
| 671 | expected = 1; |
| 672 | } else if (state.ptr1[pos] == state.ptr2[pos]) { |
| 673 | state.ptr1[pos + 1] = '\0'; |
| 674 | state.ptr2[pos + 1] = '\0'; |
| 675 | expected = 0; |
| 676 | } else { |
| 677 | expected = -1; |
| 678 | } |
| 679 | actual = strncmp(state.ptr1, state.ptr2, state.len[i]); |
| 680 | } |
| 681 | |
| 682 | ASSERT_EQ(expected, signum(actual)); |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 687 | TEST(STRING_TEST, stpncpy) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 688 | StringTestState<char> state(SMALL); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 689 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
| 690 | memset(state.ptr1, 'J', state.MAX_LEN); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 691 | // Choose a random size for our src buffer. |
| 692 | size_t ptr1_len = random() % state.MAX_LEN; |
| 693 | state.ptr1[ptr1_len] = '\0'; |
| 694 | // Copy ptr1 into ptr, used to verify that ptr1 does not get modified. |
| 695 | memcpy(state.ptr, state.ptr1, state.MAX_LEN); |
| 696 | // Init ptr2 to a set value. |
| 697 | memset(state.ptr2, '\1', state.MAX_LEN); |
| 698 | |
| 699 | // Choose a random amount of data to copy. |
| 700 | size_t copy_len = random() % state.MAX_LEN; |
| 701 | |
| 702 | // Set the second half of ptr to the expected pattern in ptr2. |
| 703 | memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN); |
| 704 | memcpy(state.ptr + state.MAX_LEN, state.ptr1, copy_len); |
| 705 | size_t expected_end; |
| 706 | if (copy_len > ptr1_len) { |
| 707 | memset(state.ptr + state.MAX_LEN + ptr1_len, '\0', copy_len - ptr1_len); |
| 708 | expected_end = ptr1_len; |
| 709 | } else { |
| 710 | expected_end = copy_len; |
| 711 | } |
| 712 | |
| 713 | ASSERT_EQ(state.ptr2 + expected_end, stpncpy(state.ptr2, state.ptr1, copy_len)); |
| 714 | |
| 715 | // Verify ptr1 was not modified. |
| 716 | ASSERT_EQ(0, memcmp(state.ptr1, state.ptr, state.MAX_LEN)); |
| 717 | // Verify ptr2 contains the expected data. |
| 718 | ASSERT_EQ(0, memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN)); |
| 719 | } |
| 720 | } |
| 721 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 722 | TEST(STRING_TEST, strncpy) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 723 | StringTestState<char> state(SMALL); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 724 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 725 | // Choose a random value to fill the string, except \0 (string terminator), |
| 726 | // or \1 (guarantees it's different from anything in ptr2). |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 727 | memset(state.ptr1, 'K', state.MAX_LEN); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 728 | // Choose a random size for our src buffer. |
| 729 | size_t ptr1_len = random() % state.MAX_LEN; |
| 730 | state.ptr1[ptr1_len] = '\0'; |
| 731 | // 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] | 732 | memcpy(state.ptr, state.ptr1, state.MAX_LEN); |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 733 | // Init ptr2 to a set value. |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 734 | memset(state.ptr2, '\1', state.MAX_LEN); |
| 735 | |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 736 | // Choose a random amount of data to copy. |
| 737 | size_t copy_len = random() % state.MAX_LEN; |
| 738 | |
| 739 | // Set the second half of ptr to the expected pattern in ptr2. |
| 740 | memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN); |
| 741 | memcpy(state.ptr + state.MAX_LEN, state.ptr1, copy_len); |
| 742 | size_t expected_end; |
| 743 | if (copy_len > ptr1_len) { |
| 744 | memset(state.ptr + state.MAX_LEN + ptr1_len, '\0', copy_len - ptr1_len); |
| 745 | expected_end = ptr1_len; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 746 | } else { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 747 | expected_end = copy_len; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 748 | } |
| 749 | |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 750 | ASSERT_EQ(state.ptr2 + expected_end, stpncpy(state.ptr2, state.ptr1, copy_len)); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 751 | |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 752 | // Verify ptr1 was not modified. |
| 753 | ASSERT_EQ(0, memcmp(state.ptr1, state.ptr, state.MAX_LEN)); |
| 754 | // Verify ptr2 contains the expected data. |
| 755 | 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] | 756 | } |
| 757 | } |
| 758 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 759 | TEST(STRING_TEST, strrchr) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 760 | int seek_char = 'M'; |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 761 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 762 | for (size_t i = 1; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 763 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 764 | if (~seek_char > 0) { |
| 765 | memset(state.ptr1, ~seek_char, state.len[i]); |
| 766 | } else { |
| 767 | memset(state.ptr1, '\1', state.len[i]); |
| 768 | } |
| 769 | state.ptr1[state.len[i] - 1] = '\0'; |
| 770 | |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 771 | size_t pos = random() % state.MAX_LEN; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 772 | char* expected; |
| 773 | if (pos >= state.len[i] - 1) { |
| 774 | if (seek_char == 0) { |
| 775 | expected = state.ptr1 + state.len[i] - 1; |
| 776 | } else { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 777 | expected = nullptr; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 778 | } |
| 779 | } else { |
| 780 | state.ptr1[pos] = seek_char; |
| 781 | expected = state.ptr1 + pos; |
| 782 | } |
| 783 | |
| 784 | ASSERT_TRUE(strrchr(state.ptr1, seek_char) == expected); |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 789 | TEST(STRING_TEST, memchr) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 790 | int seek_char = 'N'; |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 791 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 792 | for (size_t i = 0; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 793 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 794 | memset(state.ptr1, ~seek_char, state.len[i]); |
| 795 | |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 796 | size_t pos = random() % state.MAX_LEN; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 797 | char* expected; |
| 798 | if (pos >= state.len[i]) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 799 | expected = nullptr; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 800 | } else { |
| 801 | state.ptr1[pos] = seek_char; |
| 802 | expected = state.ptr1 + pos; |
| 803 | } |
| 804 | |
| 805 | ASSERT_TRUE(memchr(state.ptr1, seek_char, state.len[i]) == expected); |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 810 | TEST(STRING_TEST, memchr_zero) { |
Christopher Ferris | e03e1ea | 2014-07-30 16:06:56 -0700 | [diff] [blame] | 811 | uint8_t* buffer; |
| 812 | ASSERT_EQ(0, posix_memalign(reinterpret_cast<void**>(&buffer), 64, 64)); |
| 813 | memset(buffer, 10, 64); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 814 | ASSERT_TRUE(nullptr == memchr(buffer, 5, 0)); |
| 815 | ASSERT_TRUE(nullptr == memchr(buffer, 10, 0)); |
Christopher Ferris | e03e1ea | 2014-07-30 16:06:56 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 818 | TEST(STRING_TEST, memrchr) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 819 | int seek_char = 'P'; |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 820 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 821 | for (size_t i = 0; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 822 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 823 | memset(state.ptr1, ~seek_char, state.len[i]); |
| 824 | |
Dmitriy Ivanov | 7b956ed | 2014-09-04 12:47:07 -0700 | [diff] [blame] | 825 | size_t pos = random() % state.MAX_LEN; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 826 | char* expected; |
| 827 | if (pos >= state.len[i]) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 828 | expected = nullptr; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 829 | } else { |
| 830 | state.ptr1[pos] = seek_char; |
| 831 | expected = state.ptr1 + pos; |
| 832 | } |
| 833 | |
| 834 | ASSERT_TRUE(memrchr(state.ptr1, seek_char, state.len[i]) == expected); |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 839 | TEST(STRING_TEST, memcmp) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 840 | StringTestState<char> state(SMALL); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 841 | for (size_t i = 0; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 842 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
| 843 | int c1 = 'A'; |
| 844 | int c2 = 'N'; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 845 | memset(state.ptr1, c1, state.MAX_LEN); |
| 846 | memset(state.ptr2, c1, state.MAX_LEN); |
| 847 | |
| 848 | int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]); |
| 849 | state.ptr2[pos] = c2; |
| 850 | |
| 851 | int expected = (static_cast<int>(c1) - static_cast<int>(c2)); |
| 852 | int actual = memcmp(state.ptr1, state.ptr2, state.MAX_LEN); |
| 853 | |
| 854 | ASSERT_EQ(signum(expected), signum(actual)); |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 859 | TEST(STRING_TEST, wmemcmp) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 860 | StringTestState<wchar_t> state(SMALL); |
| 861 | |
| 862 | for (size_t i = 0; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 863 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 864 | long long mask = ((long long) 1 << 8 * sizeof(wchar_t)) - 1; |
| 865 | int c1 = rand() & mask; |
| 866 | int c2 = rand() & mask; |
| 867 | wmemset(state.ptr1, c1, state.MAX_LEN); |
| 868 | wmemset(state.ptr2, c1, state.MAX_LEN); |
| 869 | |
| 870 | int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]); |
| 871 | state.ptr2[pos] = c2; |
| 872 | |
| 873 | int expected = (static_cast<int>(c1) - static_cast<int>(c2)); |
| 874 | int actual = wmemcmp(state.ptr1, state.ptr2, (size_t) state.MAX_LEN); |
| 875 | |
| 876 | ASSERT_EQ(signum(expected), signum(actual)); |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 881 | TEST(STRING_TEST, memcpy) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 882 | StringTestState<char> state(LARGE); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 883 | int rand = 4; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 884 | for (size_t i = 0; i < state.n - 1; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 885 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 886 | size_t pos = random() % (state.MAX_LEN - state.len[i]); |
| 887 | |
| 888 | memset(state.ptr1, rand, state.len[i]); |
| 889 | memset(state.ptr1 + state.len[i], ~rand, state.MAX_LEN - state.len[i]); |
| 890 | |
| 891 | memset(state.ptr2, rand, state.len[i]); |
| 892 | memset(state.ptr2 + state.len[i], ~rand, state.MAX_LEN - state.len[i]); |
| 893 | memset(state.ptr2 + pos, '\0', state.len[i]); |
| 894 | |
| 895 | ASSERT_FALSE(memcpy(state.ptr2 + pos, state.ptr1 + pos, state.len[i]) != state.ptr2 + pos); |
| 896 | ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN)); |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 901 | TEST(STRING_TEST, memset) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 902 | StringTestState<char> state(LARGE); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 903 | char ch = 'P'; |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 904 | for (size_t i = 0; i < state.n - 1; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 905 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 906 | memset(state.ptr1, ~ch, state.MAX_LEN); |
| 907 | memcpy(state.ptr2, state.ptr1, state.MAX_LEN); |
| 908 | |
| 909 | size_t pos = random () % (state.MAX_LEN - state.len[i]); |
| 910 | for (size_t k = pos; k < pos + state.len[i]; k++) { |
| 911 | state.ptr1[k] = ch; |
| 912 | } |
| 913 | |
| 914 | ASSERT_TRUE(memset(state.ptr2 + pos, ch, state.len[i]) == state.ptr2 + pos); |
| 915 | |
| 916 | ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN)); |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 921 | TEST(STRING_TEST, memmove) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 922 | StringTestState<char> state(LARGE); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 923 | for (size_t i = 0; i < state.n - 1; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 924 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
| 925 | memset(state.ptr1, 'Q', 2 * state.MAX_LEN); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 926 | |
| 927 | size_t pos = random() % (state.MAX_LEN - state.len[i]); |
| 928 | |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 929 | memset(state.ptr1, 'R', state.len[i]); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 930 | memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN); |
| 931 | memcpy(state.ptr, state.ptr1, state.len[i]); |
| 932 | memcpy(state.ptr1 + pos, state.ptr, state.len[i]); |
| 933 | |
| 934 | ASSERT_TRUE(memmove(state.ptr2 + pos, state.ptr2, state.len[i]) == state.ptr2 + pos); |
| 935 | ASSERT_EQ(0, memcmp(state.ptr2, state.ptr1, 2 * state.MAX_LEN)); |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 940 | TEST(STRING_TEST, memmove_cache_size) { |
Varvara Rainchik | fce8614 | 2014-05-27 12:41:55 +0400 | [diff] [blame] | 941 | size_t len = 600000; |
| 942 | int max_alignment = 31; |
| 943 | int alignments[] = {0, 5, 11, 29, 30}; |
| 944 | char* ptr = reinterpret_cast<char*>(malloc(sizeof(char) * len)); |
| 945 | char* ptr1 = reinterpret_cast<char*>(malloc(2 * sizeof(char) * len)); |
| 946 | char* glob_ptr2 = reinterpret_cast<char*>(malloc(2 * sizeof(char) * len + max_alignment)); |
| 947 | size_t pos = 64; |
| 948 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 949 | ASSERT_TRUE(ptr != nullptr); |
| 950 | ASSERT_TRUE(ptr1 != nullptr); |
| 951 | ASSERT_TRUE(glob_ptr2 != nullptr); |
Varvara Rainchik | fce8614 | 2014-05-27 12:41:55 +0400 | [diff] [blame] | 952 | |
| 953 | for (int i = 0; i < 5; i++) { |
| 954 | char* ptr2 = glob_ptr2 + alignments[i]; |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 955 | memset(ptr1, 'S', 2 * len); |
| 956 | memset(ptr1, 'T', len); |
Varvara Rainchik | fce8614 | 2014-05-27 12:41:55 +0400 | [diff] [blame] | 957 | memcpy(ptr2, ptr1, 2 * len); |
| 958 | memcpy(ptr, ptr1, len); |
| 959 | memcpy(ptr1 + pos, ptr, len); |
| 960 | |
| 961 | ASSERT_TRUE(memmove(ptr2 + pos, ptr, len) == ptr2 + pos); |
| 962 | ASSERT_EQ(0, memcmp(ptr2, ptr1, 2 * len)); |
| 963 | } |
| 964 | free(ptr); |
| 965 | free(ptr1); |
| 966 | free(glob_ptr2); |
| 967 | } |
| 968 | |
Shu Zhang | 6c80ccd | 2014-05-12 18:12:15 +0800 | [diff] [blame] | 969 | static void verify_memmove(char* src_copy, char* dst, char* src, size_t size) { |
| 970 | memset(dst, 0, size); |
| 971 | memcpy(src, src_copy, size); |
| 972 | ASSERT_EQ(dst, memmove(dst, src, size)); |
| 973 | ASSERT_EQ(0, memcmp(dst, src_copy, size)); |
| 974 | } |
| 975 | |
| 976 | #define MEMMOVE_DATA_SIZE (1024*1024*3) |
| 977 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 978 | TEST(STRING_TEST, memmove_check) { |
Shu Zhang | 6c80ccd | 2014-05-12 18:12:15 +0800 | [diff] [blame] | 979 | char* buffer = reinterpret_cast<char*>(malloc(MEMMOVE_DATA_SIZE)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 980 | ASSERT_TRUE(buffer != nullptr); |
Shu Zhang | 6c80ccd | 2014-05-12 18:12:15 +0800 | [diff] [blame] | 981 | |
| 982 | char* src_data = reinterpret_cast<char*>(malloc(MEMMOVE_DATA_SIZE)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 983 | ASSERT_TRUE(src_data != nullptr); |
Shu Zhang | 6c80ccd | 2014-05-12 18:12:15 +0800 | [diff] [blame] | 984 | // Initialize to a known pattern to copy into src for each test and |
| 985 | // to compare dst against. |
| 986 | for (size_t i = 0; i < MEMMOVE_DATA_SIZE; i++) { |
| 987 | src_data[i] = (i + 1) % 255; |
| 988 | } |
| 989 | |
| 990 | // Check all different dst offsets between 0 and 127 inclusive. |
| 991 | char* src = buffer; |
| 992 | for (size_t i = 0; i < 127; i++) { |
| 993 | char* dst = buffer + 256 + i; |
| 994 | // Small copy. |
| 995 | verify_memmove(src_data, dst, src, 1024); |
| 996 | |
| 997 | // Medium copy. |
| 998 | verify_memmove(src_data, dst, src, 64 * 1024); |
| 999 | |
| 1000 | // Medium copy. |
| 1001 | verify_memmove(src_data, dst, src, 1024 * 1024 + 128 * 1024); |
| 1002 | } |
| 1003 | |
| 1004 | // Check all leftover size offsets between 1 and 127 inclusive. |
| 1005 | char* dst = buffer + 256; |
| 1006 | src = buffer; |
| 1007 | for (size_t size = 1; size < 127; size++) { |
| 1008 | // Small copy. |
| 1009 | verify_memmove(src_data, dst, src, 1024); |
| 1010 | |
| 1011 | // Medium copy. |
| 1012 | verify_memmove(src_data, dst, src, 64 * 1024); |
| 1013 | |
| 1014 | // Large copy. |
| 1015 | verify_memmove(src_data, dst, src, 1024 * 1024 + 128 * 1024); |
| 1016 | } |
| 1017 | } |
| 1018 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1019 | TEST(STRING_TEST, bcopy) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 1020 | StringTestState<char> state(LARGE); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 1021 | for (size_t i = 0; i < state.n; i++) { |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 1022 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
| 1023 | memset(state.ptr1, '4', state.MAX_LEN); |
| 1024 | memset(state.ptr1 + state.MAX_LEN, 'a', state.MAX_LEN); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 1025 | memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN); |
| 1026 | |
| 1027 | size_t start = random() % (2 * state.MAX_LEN - state.len[i]); |
| 1028 | memcpy(state.ptr2 + start, state.ptr1, state.len[i]); |
| 1029 | |
| 1030 | bcopy(state.ptr1, state.ptr1 + start, state.len[i]); |
| 1031 | ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, 2 * state.MAX_LEN)); |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1036 | TEST(STRING_TEST, bzero) { |
Alexander Ivchenko | baa91f4 | 2013-06-27 12:55:46 +0400 | [diff] [blame] | 1037 | StringTestState<char> state(LARGE); |
Dmitriy Ivanov | 1467dfe | 2014-08-13 11:24:37 -0700 | [diff] [blame] | 1038 | for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) { |
| 1039 | memset(state.ptr1, 'R', state.MAX_LEN); |
Anna Tikhonova | 036154b | 2012-10-05 15:21:11 +0400 | [diff] [blame] | 1040 | |
| 1041 | size_t start = random() % state.MAX_LEN; |
| 1042 | size_t end = start + random() % (state.MAX_LEN - start); |
| 1043 | |
| 1044 | memcpy(state.ptr2, state.ptr1, start); |
| 1045 | memset(state.ptr2 + start, '\0', end - start); |
| 1046 | memcpy(state.ptr2 + end, state.ptr1 + end, state.MAX_LEN - end); |
| 1047 | |
| 1048 | bzero(state.ptr1 + start, end - start); |
| 1049 | |
| 1050 | ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN)); |
| 1051 | } |
| 1052 | } |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1053 | |
| 1054 | static void DoMemcpyTest(uint8_t* src, uint8_t* dst, size_t len) { |
| 1055 | memset(src, (len % 255) + 1, len); |
| 1056 | memset(dst, 0, len); |
| 1057 | |
| 1058 | ASSERT_EQ(dst, memcpy(dst, src, len)); |
| 1059 | ASSERT_TRUE(memcmp(src, dst, len) == 0); |
| 1060 | } |
| 1061 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1062 | TEST(STRING_TEST, memcpy_align) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1063 | RunSrcDstBufferAlignTest(LARGE, DoMemcpyTest); |
| 1064 | } |
| 1065 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1066 | TEST(STRING_TEST, memcpy_overread) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1067 | RunSrcDstBufferOverreadTest(DoMemcpyTest); |
| 1068 | } |
| 1069 | |
Shu Zhang | 6c80ccd | 2014-05-12 18:12:15 +0800 | [diff] [blame] | 1070 | static void DoMemmoveTest(uint8_t* src, uint8_t* dst, size_t len) { |
| 1071 | memset(src, (len % 255) + 1, len); |
| 1072 | memset(dst, 0, len); |
| 1073 | |
| 1074 | ASSERT_EQ(dst, memmove(dst, src, len)); |
| 1075 | ASSERT_TRUE(memcmp(src, dst, len) == 0); |
| 1076 | } |
| 1077 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1078 | TEST(STRING_TEST, memmove_align) { |
Shu Zhang | 6c80ccd | 2014-05-12 18:12:15 +0800 | [diff] [blame] | 1079 | RunSrcDstBufferAlignTest(LARGE, DoMemmoveTest); |
| 1080 | } |
| 1081 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1082 | TEST(STRING_TEST, memmove_overread) { |
Shu Zhang | 6c80ccd | 2014-05-12 18:12:15 +0800 | [diff] [blame] | 1083 | RunSrcDstBufferOverreadTest(DoMemmoveTest); |
| 1084 | } |
| 1085 | |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1086 | static void DoMemsetTest(uint8_t* buf, size_t len) { |
| 1087 | for (size_t i = 0; i < len; i++) { |
| 1088 | buf[i] = 0; |
| 1089 | } |
| 1090 | int value = (len % 255) + 1; |
| 1091 | ASSERT_EQ(buf, memset(buf, value, len)); |
| 1092 | for (size_t i = 0; i < len; i++) { |
| 1093 | ASSERT_EQ(value, buf[i]); |
| 1094 | } |
| 1095 | } |
| 1096 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1097 | TEST(STRING_TEST, memset_align) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1098 | RunSingleBufferAlignTest(LARGE, DoMemsetTest); |
| 1099 | } |
| 1100 | |
| 1101 | static void DoStrlenTest(uint8_t* buf, size_t len) { |
| 1102 | if (len >= 1) { |
| 1103 | memset(buf, (32 + (len % 96)), len - 1); |
| 1104 | buf[len-1] = '\0'; |
| 1105 | ASSERT_EQ(len-1, strlen(reinterpret_cast<char*>(buf))); |
| 1106 | } |
| 1107 | } |
| 1108 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1109 | TEST(STRING_TEST, strlen_align) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1110 | RunSingleBufferAlignTest(LARGE, DoStrlenTest); |
| 1111 | } |
| 1112 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1113 | TEST(STRING_TEST, strlen_overread) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1114 | RunSingleBufferOverreadTest(DoStrlenTest); |
| 1115 | } |
| 1116 | |
| 1117 | static void DoStrcpyTest(uint8_t* src, uint8_t* dst, size_t len) { |
| 1118 | if (len >= 1) { |
| 1119 | memset(src, (32 + (len % 96)), len - 1); |
| 1120 | src[len-1] = '\0'; |
| 1121 | memset(dst, 0, len); |
| 1122 | ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcpy(reinterpret_cast<char*>(dst), |
| 1123 | reinterpret_cast<char*>(src)))); |
| 1124 | ASSERT_TRUE(memcmp(src, dst, len) == 0); |
| 1125 | } |
| 1126 | } |
| 1127 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1128 | TEST(STRING_TEST, strcpy_align) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1129 | RunSrcDstBufferAlignTest(LARGE, DoStrcpyTest); |
| 1130 | } |
| 1131 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1132 | TEST(STRING_TEST, strcpy_overread) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1133 | RunSrcDstBufferOverreadTest(DoStrcpyTest); |
| 1134 | } |
| 1135 | |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1136 | #if defined(STRLCPY_SUPPORTED) |
| 1137 | static void DoStrlcpyTest(uint8_t* src, uint8_t* dst, size_t len) { |
| 1138 | if (len >= 1) { |
| 1139 | memset(src, (32 + (len % 96)), len - 1); |
| 1140 | src[len-1] = '\0'; |
| 1141 | memset(dst, 0, len); |
| 1142 | ASSERT_EQ(len-1, strlcpy(reinterpret_cast<char*>(dst), |
| 1143 | reinterpret_cast<char*>(src), len)); |
| 1144 | ASSERT_TRUE(memcmp(src, dst, len) == 0); |
| 1145 | } |
| 1146 | } |
| 1147 | #endif |
| 1148 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1149 | TEST(STRING_TEST, strlcpy_align) { |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1150 | #if defined(STRLCPY_SUPPORTED) |
| 1151 | RunSrcDstBufferAlignTest(LARGE, DoStrlcpyTest); |
| 1152 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1153 | GTEST_SKIP() << "strlcpy not available"; |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1154 | #endif |
| 1155 | } |
| 1156 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1157 | TEST(STRING_TEST, strlcpy_overread) { |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1158 | #if defined(STRLCPY_SUPPORTED) |
| 1159 | RunSrcDstBufferOverreadTest(DoStrlcpyTest); |
| 1160 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1161 | GTEST_SKIP() << "strlcpy not available"; |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1162 | #endif |
| 1163 | } |
| 1164 | |
| 1165 | |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 1166 | static void DoStpcpyTest(uint8_t* src, uint8_t* dst, size_t len) { |
| 1167 | if (len >= 1) { |
| 1168 | memset(src, (32 + (len % 96)), len - 1); |
| 1169 | src[len-1] = '\0'; |
| 1170 | memset(dst, 0, len); |
| 1171 | ASSERT_EQ(dst+len-1, reinterpret_cast<uint8_t*>(stpcpy(reinterpret_cast<char*>(dst), |
| 1172 | reinterpret_cast<char*>(src)))); |
| 1173 | ASSERT_TRUE(memcmp(src, dst, len) == 0); |
| 1174 | } |
| 1175 | } |
| 1176 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1177 | TEST(STRING_TEST, stpcpy_align) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 1178 | RunSrcDstBufferAlignTest(LARGE, DoStpcpyTest); |
| 1179 | } |
| 1180 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1181 | TEST(STRING_TEST, stpcpy_overread) { |
Christopher Ferris | 950a58e | 2014-04-04 14:38:18 -0700 | [diff] [blame] | 1182 | RunSrcDstBufferOverreadTest(DoStpcpyTest); |
| 1183 | } |
| 1184 | |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1185 | // 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] | 1186 | static size_t LargeSetIncrement(size_t len) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1187 | if (len >= 4096) { |
| 1188 | return 4096; |
| 1189 | } else if (len >= 1024) { |
| 1190 | return 1024; |
| 1191 | } else if (len >= 256) { |
| 1192 | return 256; |
| 1193 | } |
| 1194 | return 1; |
| 1195 | } |
| 1196 | |
Christopher Ferris | fdfcfce | 2015-09-23 22:09:09 -0700 | [diff] [blame] | 1197 | #define STRCAT_DST_LEN 64 |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1198 | |
| 1199 | static void DoStrcatTest(uint8_t* src, uint8_t* dst, size_t len) { |
| 1200 | if (len >= 1) { |
| 1201 | int value = 32 + (len % 96); |
| 1202 | memset(src, value, len - 1); |
| 1203 | src[len-1] = '\0'; |
| 1204 | |
| 1205 | if (len >= STRCAT_DST_LEN) { |
| 1206 | // Create a small buffer for doing quick compares in each loop. |
| 1207 | uint8_t cmp_buf[STRCAT_DST_LEN]; |
| 1208 | // Make sure dst string contains a different value then the src string. |
| 1209 | int value2 = 32 + (value + 2) % 96; |
| 1210 | memset(cmp_buf, value2, sizeof(cmp_buf)); |
| 1211 | |
Christopher Ferris | fdfcfce | 2015-09-23 22:09:09 -0700 | [diff] [blame] | 1212 | for (size_t i = 1; i <= STRCAT_DST_LEN;) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1213 | memset(dst, value2, i-1); |
| 1214 | memset(dst+i-1, 0, len-i); |
| 1215 | src[len-i] = '\0'; |
| 1216 | ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcat(reinterpret_cast<char*>(dst), |
| 1217 | reinterpret_cast<char*>(src)))); |
| 1218 | ASSERT_TRUE(memcmp(dst, cmp_buf, i-1) == 0); |
| 1219 | ASSERT_TRUE(memcmp(src, dst+i-1, len-i+1) == 0); |
Christopher Ferris | fdfcfce | 2015-09-23 22:09:09 -0700 | [diff] [blame] | 1220 | // This is an expensive loop, so don't loop through every value, |
| 1221 | // get to a certain size and then start doubling. |
| 1222 | if (i < 16) { |
| 1223 | i++; |
| 1224 | } else { |
| 1225 | i <<= 1; |
| 1226 | } |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1227 | } |
| 1228 | } else { |
| 1229 | dst[0] = '\0'; |
| 1230 | ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcat(reinterpret_cast<char*>(dst), |
| 1231 | reinterpret_cast<char*>(src)))); |
| 1232 | ASSERT_TRUE(memcmp(src, dst, len) == 0); |
| 1233 | } |
| 1234 | } |
| 1235 | } |
| 1236 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1237 | TEST(STRING_TEST, strcat_align) { |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1238 | RunSrcDstBufferAlignTest(MEDIUM, DoStrcatTest, LargeSetIncrement); |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1239 | } |
| 1240 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1241 | TEST(STRING_TEST, strcat_overread) { |
Christopher Ferris | b687ad3 | 2013-11-06 17:32:11 -0800 | [diff] [blame] | 1242 | RunSrcDstBufferOverreadTest(DoStrcatTest); |
| 1243 | } |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1244 | |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1245 | #if defined(STRLCAT_SUPPORTED) |
| 1246 | static void DoStrlcatTest(uint8_t* src, uint8_t* dst, size_t len) { |
| 1247 | if (len >= 1) { |
| 1248 | int value = 32 + (len % 96); |
| 1249 | memset(src, value, len - 1); |
| 1250 | src[len-1] = '\0'; |
| 1251 | |
| 1252 | if (len >= STRCAT_DST_LEN) { |
| 1253 | // Create a small buffer for doing quick compares in each loop. |
| 1254 | uint8_t cmp_buf[STRCAT_DST_LEN]; |
| 1255 | // Make sure dst string contains a different value then the src string. |
| 1256 | int value2 = 32 + (value + 2) % 96; |
| 1257 | memset(cmp_buf, value2, sizeof(cmp_buf)); |
| 1258 | |
Christopher Ferris | fdfcfce | 2015-09-23 22:09:09 -0700 | [diff] [blame] | 1259 | for (size_t i = 1; i <= STRCAT_DST_LEN;) { |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1260 | memset(dst, value2, i-1); |
| 1261 | memset(dst+i-1, 0, len-i); |
| 1262 | src[len-i] = '\0'; |
| 1263 | ASSERT_EQ(len-1, strlcat(reinterpret_cast<char*>(dst), |
| 1264 | reinterpret_cast<char*>(src), len)); |
| 1265 | ASSERT_TRUE(memcmp(dst, cmp_buf, i-1) == 0); |
| 1266 | ASSERT_TRUE(memcmp(src, dst+i-1, len-i+1) == 0); |
Christopher Ferris | fdfcfce | 2015-09-23 22:09:09 -0700 | [diff] [blame] | 1267 | // This is an expensive loop, so don't loop through every value, |
| 1268 | // get to a certain size and then start doubling. |
| 1269 | if (i < 16) { |
| 1270 | i++; |
| 1271 | } else { |
| 1272 | i <<= 1; |
| 1273 | } |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1274 | } |
| 1275 | } else { |
| 1276 | dst[0] = '\0'; |
| 1277 | ASSERT_EQ(len-1, strlcat(reinterpret_cast<char*>(dst), |
| 1278 | reinterpret_cast<char*>(src), len)); |
| 1279 | ASSERT_TRUE(memcmp(src, dst, len) == 0); |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | #endif |
| 1284 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1285 | TEST(STRING_TEST, strlcat_align) { |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1286 | #if defined(STRLCAT_SUPPORTED) |
| 1287 | RunSrcDstBufferAlignTest(MEDIUM, DoStrlcatTest, LargeSetIncrement); |
| 1288 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1289 | GTEST_SKIP() << "strlcat not available"; |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1290 | #endif |
| 1291 | } |
| 1292 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1293 | TEST(STRING_TEST, strlcat_overread) { |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1294 | #if defined(STRLCAT_SUPPORTED) |
| 1295 | RunSrcDstBufferOverreadTest(DoStrlcatTest); |
| 1296 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1297 | GTEST_SKIP() << "strlcat not available"; |
Christopher Ferris | 1468765 | 2014-11-10 13:58:17 -0800 | [diff] [blame] | 1298 | #endif |
| 1299 | } |
| 1300 | |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1301 | static void DoStrcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) { |
| 1302 | if (len >= 1) { |
| 1303 | memset(buf1, (32 + (len % 96)), len - 1); |
| 1304 | buf1[len-1] = '\0'; |
| 1305 | memset(buf2, (32 + (len % 96)), len - 1); |
| 1306 | buf2[len-1] = '\0'; |
| 1307 | ASSERT_EQ(0, strcmp(reinterpret_cast<char*>(buf1), |
| 1308 | reinterpret_cast<char*>(buf2))); |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | static void DoStrcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) { |
| 1313 | // Do string length differences. |
| 1314 | int c = (32 + (len1 % 96)); |
| 1315 | memset(buf1, c, len1 - 1); |
| 1316 | buf1[len1-1] = '\0'; |
| 1317 | memset(buf2, c, len2 - 1); |
| 1318 | buf2[len2-1] = '\0'; |
| 1319 | ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1), |
| 1320 | reinterpret_cast<char*>(buf2))); |
| 1321 | |
| 1322 | // Do single character differences. |
| 1323 | size_t len; |
| 1324 | if (len1 > len2) { |
| 1325 | len = len2; |
| 1326 | } else { |
| 1327 | len = len1; |
| 1328 | } |
| 1329 | // Need at least a two character buffer to do this test. |
| 1330 | if (len > 1) { |
| 1331 | buf1[len-1] = '\0'; |
| 1332 | buf2[len-1] = '\0'; |
| 1333 | int diff_c = (c + 1) % 96; |
| 1334 | |
| 1335 | buf1[len-2] = diff_c; |
| 1336 | ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1), |
| 1337 | reinterpret_cast<char*>(buf2))); |
| 1338 | |
| 1339 | buf1[len-2] = c; |
| 1340 | buf2[len-2] = diff_c; |
| 1341 | ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1), |
| 1342 | reinterpret_cast<char*>(buf2))); |
| 1343 | } |
| 1344 | } |
| 1345 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1346 | TEST(STRING_TEST, strcmp_align) { |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1347 | RunCmpBufferAlignTest(MEDIUM, DoStrcmpTest, DoStrcmpFailTest, LargeSetIncrement); |
| 1348 | } |
| 1349 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1350 | TEST(STRING_TEST, strcmp_overread) { |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1351 | RunCmpBufferOverreadTest(DoStrcmpTest, DoStrcmpFailTest); |
| 1352 | } |
| 1353 | |
| 1354 | static void DoMemcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) { |
| 1355 | memset(buf1, len+1, len); |
| 1356 | memset(buf2, len+1, len); |
| 1357 | ASSERT_EQ(0, memcmp(buf1, buf2, len)); |
| 1358 | } |
| 1359 | |
| 1360 | static void DoMemcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) { |
| 1361 | size_t len; |
| 1362 | if (len1 > len2) { |
| 1363 | len = len2; |
| 1364 | } else { |
| 1365 | len = len1; |
| 1366 | } |
| 1367 | |
| 1368 | memset(buf1, len2+1, len); |
| 1369 | buf1[len-1] = len2; |
| 1370 | memset(buf2, len2+1, len); |
| 1371 | ASSERT_NE(0, memcmp(buf1, buf2, len)); |
| 1372 | |
| 1373 | buf1[len-1] = len2+1; |
| 1374 | buf2[len-1] = len2; |
| 1375 | ASSERT_NE(0, memcmp(buf1, buf2, len)); |
| 1376 | } |
| 1377 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1378 | TEST(STRING_TEST, memcmp_align) { |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1379 | RunCmpBufferAlignTest(MEDIUM, DoMemcmpTest, DoMemcmpFailTest, LargeSetIncrement); |
| 1380 | } |
| 1381 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1382 | TEST(STRING_TEST, memcmp_overread) { |
Christopher Ferris | e5bbb6b | 2013-12-03 18:39:10 -0800 | [diff] [blame] | 1383 | RunCmpBufferOverreadTest(DoMemcmpTest, DoMemcmpFailTest); |
| 1384 | } |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 1385 | |
Christopher Ferris | 2f030af | 2017-05-08 14:24:29 -0700 | [diff] [blame] | 1386 | static void DoMemchrTest(uint8_t* buf, size_t len) { |
| 1387 | if (len >= 1) { |
| 1388 | int value = len % 128; |
| 1389 | int search_value = (len % 128) + 1; |
| 1390 | memset(buf, value, len); |
| 1391 | // The buffer does not contain the search value. |
| 1392 | ASSERT_EQ(nullptr, memchr(buf, search_value, len)); |
| 1393 | if (len >= 2) { |
| 1394 | buf[0] = search_value; |
| 1395 | // The search value is the first element in the buffer. |
| 1396 | ASSERT_EQ(&buf[0], memchr(buf, search_value, len)); |
| 1397 | |
| 1398 | buf[0] = value; |
| 1399 | buf[len - 1] = search_value; |
| 1400 | // The search value is the last element in the buffer. |
| 1401 | ASSERT_EQ(&buf[len - 1], memchr(buf, search_value, len)); |
| 1402 | } |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | TEST(STRING_TEST, memchr_align) { |
| 1407 | RunSingleBufferAlignTest(MEDIUM, DoMemchrTest); |
| 1408 | } |
| 1409 | |
| 1410 | TEST(STRING_TEST, memchr_overread) { |
| 1411 | RunSingleBufferOverreadTest(DoMemchrTest); |
| 1412 | } |
| 1413 | |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 1414 | static void DoStrchrTest(uint8_t* buf, size_t len) { |
| 1415 | if (len >= 1) { |
| 1416 | char value = 32 + (len % 96); |
| 1417 | char search_value = 33 + (len % 96); |
| 1418 | memset(buf, value, len - 1); |
Christopher Ferris | 2f030af | 2017-05-08 14:24:29 -0700 | [diff] [blame] | 1419 | buf[len - 1] = '\0'; |
| 1420 | // The buffer does not contain the search value. |
| 1421 | ASSERT_EQ(nullptr, strchr(reinterpret_cast<char*>(buf), search_value)); |
| 1422 | // Search for the special '\0' character. |
| 1423 | 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] | 1424 | if (len >= 2) { |
| 1425 | buf[0] = search_value; |
Christopher Ferris | 2f030af | 2017-05-08 14:24:29 -0700 | [diff] [blame] | 1426 | // The search value is the first element in the buffer. |
| 1427 | ASSERT_EQ(reinterpret_cast<char*>(&buf[0]), strchr(reinterpret_cast<char*>(buf), |
| 1428 | search_value)); |
| 1429 | |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 1430 | buf[0] = value; |
Christopher Ferris | 2f030af | 2017-05-08 14:24:29 -0700 | [diff] [blame] | 1431 | buf[len - 2] = search_value; |
| 1432 | // The search value is the second to last element in the buffer. |
| 1433 | // The last element is the '\0' character. |
| 1434 | ASSERT_EQ(reinterpret_cast<char*>(&buf[len - 2]), strchr(reinterpret_cast<char*>(buf), |
| 1435 | search_value)); |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1440 | TEST(STRING_TEST, strchr_align) { |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 1441 | RunSingleBufferAlignTest(MEDIUM, DoStrchrTest); |
| 1442 | } |
| 1443 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1444 | TEST(STRING_TEST, strchr_overread) { |
Christopher Ferris | 3a657d0 | 2014-06-27 12:33:22 -0700 | [diff] [blame] | 1445 | RunSingleBufferOverreadTest(DoStrchrTest); |
| 1446 | } |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 1447 | |
Christopher Ferris | 2f030af | 2017-05-08 14:24:29 -0700 | [diff] [blame] | 1448 | static void DoStrrchrTest(uint8_t* buf, size_t len) { |
| 1449 | if (len >= 1) { |
| 1450 | char value = 32 + (len % 96); |
| 1451 | char search_value = 33 + (len % 96); |
| 1452 | memset(buf, value, len - 1); |
| 1453 | buf[len - 1] = '\0'; |
| 1454 | // The buffer does not contain the search value. |
| 1455 | ASSERT_EQ(nullptr, strrchr(reinterpret_cast<char*>(buf), search_value)); |
| 1456 | // Search for the special '\0' character. |
| 1457 | ASSERT_EQ(reinterpret_cast<char*>(&buf[len - 1]), strrchr(reinterpret_cast<char*>(buf), '\0')); |
| 1458 | if (len >= 2) { |
| 1459 | buf[0] = search_value; |
| 1460 | // The search value is the first element in the buffer. |
| 1461 | ASSERT_EQ(reinterpret_cast<char*>(&buf[0]), strrchr(reinterpret_cast<char*>(buf), |
| 1462 | search_value)); |
| 1463 | |
| 1464 | buf[0] = value; |
| 1465 | buf[len - 2] = search_value; |
| 1466 | // The search value is the second to last element in the buffer. |
| 1467 | // The last element is the '\0' character. |
| 1468 | ASSERT_EQ(reinterpret_cast<char*>(&buf[len - 2]), strrchr(reinterpret_cast<char*>(buf), |
| 1469 | search_value)); |
| 1470 | } |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | TEST(STRING_TEST, strrchr_align) { |
| 1475 | RunSingleBufferAlignTest(MEDIUM, DoStrrchrTest); |
| 1476 | } |
| 1477 | |
| 1478 | TEST(STRING_TEST, strrchr_overread) { |
| 1479 | RunSingleBufferOverreadTest(DoStrrchrTest); |
| 1480 | } |
| 1481 | |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 1482 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 1483 | static void TestBasename(const char* in, const char* expected_out) { |
| 1484 | errno = 0; |
| 1485 | const char* out = basename(in); |
| 1486 | ASSERT_STREQ(expected_out, out) << in; |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame^] | 1487 | ASSERT_ERRNO(0) << in; |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 1488 | } |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 1489 | #endif |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 1490 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1491 | TEST(STRING_TEST, __gnu_basename) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 1492 | #if !defined(ANDROID_HOST_MUSL) |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 1493 | TestBasename("", ""); |
| 1494 | TestBasename("/usr/lib", "lib"); |
| 1495 | TestBasename("/usr/", ""); |
| 1496 | TestBasename("usr", "usr"); |
| 1497 | TestBasename("/", ""); |
| 1498 | TestBasename(".", "."); |
| 1499 | TestBasename("..", ".."); |
| 1500 | TestBasename("///", ""); |
| 1501 | TestBasename("//usr//lib//", ""); |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 1502 | #else |
| 1503 | GTEST_SKIP() << "musl doesn't have GNU basename"; |
| 1504 | #endif |
Elliott Hughes | 09c39d6 | 2014-08-19 14:30:30 -0700 | [diff] [blame] | 1505 | } |
Elliott Hughes | 41ef902 | 2015-02-14 13:21:22 -0800 | [diff] [blame] | 1506 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1507 | TEST(STRING_TEST, strnlen_147048) { |
Elliott Hughes | 41ef902 | 2015-02-14 13:21:22 -0800 | [diff] [blame] | 1508 | // https://code.google.com/p/android/issues/detail?id=147048 |
| 1509 | char stack_src[64] = {0}; |
| 1510 | EXPECT_EQ(0U, strnlen(stack_src, 1024*1024*1024)); |
| 1511 | char* heap_src = new char[1]; |
| 1512 | *heap_src = '\0'; |
| 1513 | EXPECT_EQ(0U, strnlen(heap_src, 1024*1024*1024)); |
| 1514 | delete[] heap_src; |
| 1515 | } |
Elliott Hughes | 3cfb52a | 2015-02-18 21:29:13 -0800 | [diff] [blame] | 1516 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1517 | TEST(STRING_TEST, strnlen_74741) { |
Elliott Hughes | d2a9fb3 | 2015-07-27 20:55:03 -0700 | [diff] [blame] | 1518 | ASSERT_EQ(4U, strnlen("test", SIZE_MAX)); |
| 1519 | } |
| 1520 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1521 | TEST(STRING_TEST, mempcpy) { |
Elliott Hughes | 3cfb52a | 2015-02-18 21:29:13 -0800 | [diff] [blame] | 1522 | char dst[6]; |
| 1523 | ASSERT_EQ(&dst[4], reinterpret_cast<char*>(mempcpy(dst, "hello", 4))); |
| 1524 | } |
Christopher Ferris | 71766c2 | 2016-02-12 17:24:27 -0800 | [diff] [blame] | 1525 | |
| 1526 | // clang depends on the fact that a memcpy where src and dst is the same |
| 1527 | // still operates correctly. This test verifies that this assumption |
| 1528 | // holds true. |
| 1529 | // See https://llvm.org/bugs/show_bug.cgi?id=11763 for more information. |
| 1530 | static std::vector<uint8_t> g_memcpy_same_buffer; |
| 1531 | |
| 1532 | static void DoMemcpySameTest(uint8_t* buffer, size_t len) { |
| 1533 | memcpy(buffer, g_memcpy_same_buffer.data(), len); |
| 1534 | ASSERT_EQ(buffer, memcpy(buffer, buffer, len)); |
| 1535 | ASSERT_TRUE(memcmp(buffer, g_memcpy_same_buffer.data(), len) == 0); |
| 1536 | } |
| 1537 | |
| 1538 | TEST(STRING_TEST, memcpy_src_dst_same) { |
| 1539 | g_memcpy_same_buffer.resize(MEDIUM); |
| 1540 | for (size_t i = 0; i < MEDIUM; i++) { |
| 1541 | g_memcpy_same_buffer[i] = i; |
| 1542 | } |
| 1543 | RunSingleBufferAlignTest(MEDIUM, DoMemcpySameTest); |
| 1544 | } |
Elliott Hughes | cae33ad | 2016-08-15 14:14:40 -0700 | [diff] [blame] | 1545 | |
| 1546 | TEST(STRING_TEST, memmem_strstr_empty_needle) { |
| 1547 | const char* some_haystack = "haystack"; |
| 1548 | const char* empty_haystack = ""; |
| 1549 | |
| 1550 | ASSERT_EQ(some_haystack, memmem(some_haystack, 8, "", 0)); |
| 1551 | ASSERT_EQ(empty_haystack, memmem(empty_haystack, 0, "", 0)); |
| 1552 | |
| 1553 | ASSERT_EQ(some_haystack, strstr(some_haystack, "")); |
| 1554 | ASSERT_EQ(empty_haystack, strstr(empty_haystack, "")); |
| 1555 | } |
| 1556 | |
| 1557 | TEST(STRING_TEST, memmem_smoke) { |
Elliott Hughes | 5633caa | 2020-08-06 14:32:43 -0700 | [diff] [blame] | 1558 | const char haystack[] = "big\0daddy/giant\0haystacks!"; |
| 1559 | |
| 1560 | // The current memmem() implementation has special cases for needles of |
| 1561 | // lengths 0, 1, 2, 3, and 4, plus a long needle case. We test matches at the |
| 1562 | // beginning, middle, and end of the haystack. |
| 1563 | |
| 1564 | ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "", 0)); |
| 1565 | |
Elliott Hughes | cae33ad | 2016-08-15 14:14:40 -0700 | [diff] [blame] | 1566 | ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "b", 1)); |
Elliott Hughes | 5633caa | 2020-08-06 14:32:43 -0700 | [diff] [blame] | 1567 | ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "bi", 2)); |
| 1568 | ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "big", 3)); |
| 1569 | ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "big\0", 4)); |
| 1570 | ASSERT_EQ(haystack + 0, memmem(haystack, sizeof(haystack), "big\0d", 5)); |
| 1571 | |
| 1572 | ASSERT_EQ(haystack + 2, memmem(haystack, sizeof(haystack), "g", 1)); |
| 1573 | ASSERT_EQ(haystack + 10, memmem(haystack, sizeof(haystack), "gi", 2)); |
| 1574 | ASSERT_EQ(haystack + 10, memmem(haystack, sizeof(haystack), "gia", 3)); |
| 1575 | ASSERT_EQ(haystack + 10, memmem(haystack, sizeof(haystack), "gian", 4)); |
| 1576 | ASSERT_EQ(haystack + 10, memmem(haystack, sizeof(haystack), "giant", 5)); |
| 1577 | |
| 1578 | ASSERT_EQ(haystack + 25, memmem(haystack, sizeof(haystack), "!", 1)); |
| 1579 | ASSERT_EQ(haystack + 24, memmem(haystack, sizeof(haystack), "s!", 2)); |
| 1580 | ASSERT_EQ(haystack + 23, memmem(haystack, sizeof(haystack), "ks!", 3)); |
| 1581 | ASSERT_EQ(haystack + 22, memmem(haystack, sizeof(haystack), "cks!", 4)); |
| 1582 | ASSERT_EQ(haystack + 21, memmem(haystack, sizeof(haystack), "acks!", 5)); |
Elliott Hughes | cae33ad | 2016-08-15 14:14:40 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | TEST(STRING_TEST, strstr_smoke) { |
Elliott Hughes | c6b38ae | 2019-11-22 11:15:42 -0800 | [diff] [blame] | 1586 | const char* haystack = "big daddy/giant haystacks!"; |
| 1587 | |
| 1588 | // The current strstr() implementation has special cases for needles of |
| 1589 | // lengths 0, 1, 2, 3, and 4, plus a long needle case. We test matches at the |
| 1590 | // beginning, middle, and end of the haystack. |
| 1591 | |
| 1592 | ASSERT_EQ(haystack + 0, strstr(haystack, "")); |
| 1593 | |
Elliott Hughes | cae33ad | 2016-08-15 14:14:40 -0700 | [diff] [blame] | 1594 | ASSERT_EQ(haystack + 0, strstr(haystack, "b")); |
Elliott Hughes | c6b38ae | 2019-11-22 11:15:42 -0800 | [diff] [blame] | 1595 | ASSERT_EQ(haystack + 0, strstr(haystack, "bi")); |
| 1596 | ASSERT_EQ(haystack + 0, strstr(haystack, "big")); |
| 1597 | ASSERT_EQ(haystack + 0, strstr(haystack, "big ")); |
| 1598 | ASSERT_EQ(haystack + 0, strstr(haystack, "big d")); |
| 1599 | |
| 1600 | ASSERT_EQ(haystack + 2, strstr(haystack, "g")); |
| 1601 | ASSERT_EQ(haystack + 10, strstr(haystack, "gi")); |
| 1602 | ASSERT_EQ(haystack + 10, strstr(haystack, "gia")); |
| 1603 | ASSERT_EQ(haystack + 10, strstr(haystack, "gian")); |
| 1604 | ASSERT_EQ(haystack + 10, strstr(haystack, "giant")); |
| 1605 | |
| 1606 | ASSERT_EQ(haystack + 25, strstr(haystack, "!")); |
| 1607 | ASSERT_EQ(haystack + 24, strstr(haystack, "s!")); |
| 1608 | ASSERT_EQ(haystack + 23, strstr(haystack, "ks!")); |
| 1609 | ASSERT_EQ(haystack + 22, strstr(haystack, "cks!")); |
| 1610 | ASSERT_EQ(haystack + 21, strstr(haystack, "acks!")); |
Elliott Hughes | cae33ad | 2016-08-15 14:14:40 -0700 | [diff] [blame] | 1611 | } |
Elliott Hughes | fbac97a | 2019-02-04 16:46:24 -0800 | [diff] [blame] | 1612 | |
| 1613 | TEST(STRING_TEST, strcasestr_smoke) { |
| 1614 | const char* haystack = "bIg dAdDy/gIaNt hAyStAcKs"; |
| 1615 | ASSERT_EQ(haystack, strcasestr(haystack, "")); |
| 1616 | ASSERT_EQ(haystack + 0, strcasestr(haystack, "B")); |
| 1617 | ASSERT_EQ(haystack + 1, strcasestr(haystack, "i")); |
| 1618 | ASSERT_EQ(haystack + 4, strcasestr(haystack, "Da")); |
| 1619 | } |
| 1620 | |
| 1621 | TEST(STRING_TEST, strcoll_smoke) { |
| 1622 | ASSERT_TRUE(strcoll("aab", "aac") < 0); |
| 1623 | ASSERT_TRUE(strcoll("aab", "aab") == 0); |
| 1624 | ASSERT_TRUE(strcoll("aac", "aab") > 0); |
| 1625 | } |
| 1626 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 1627 | TEST(STRING_TEST, strcoll_l_smoke) { |
| 1628 | // bionic just forwards to strcoll(3). |
| 1629 | ASSERT_TRUE(strcoll_l("aab", "aac", LC_GLOBAL_LOCALE) < 0); |
| 1630 | ASSERT_TRUE(strcoll_l("aab", "aab", LC_GLOBAL_LOCALE) == 0); |
| 1631 | ASSERT_TRUE(strcoll_l("aac", "aab", LC_GLOBAL_LOCALE) > 0); |
| 1632 | } |
| 1633 | |
Elliott Hughes | fbac97a | 2019-02-04 16:46:24 -0800 | [diff] [blame] | 1634 | TEST(STRING_TEST, strxfrm_smoke) { |
| 1635 | const char* src1 = "aab"; |
| 1636 | char dst1[16] = {}; |
Elliott Hughes | 2e71c17 | 2020-08-06 13:53:40 -0700 | [diff] [blame] | 1637 | // Dry run. |
| 1638 | ASSERT_EQ(strxfrm(dst1, src1, 0), 3U); |
| 1639 | ASSERT_STREQ(dst1, ""); |
| 1640 | // Really do it. |
| 1641 | ASSERT_EQ(strxfrm(dst1, src1, sizeof(dst1)), 3U); |
| 1642 | |
Elliott Hughes | fbac97a | 2019-02-04 16:46:24 -0800 | [diff] [blame] | 1643 | const char* src2 = "aac"; |
| 1644 | char dst2[16] = {}; |
Elliott Hughes | 2e71c17 | 2020-08-06 13:53:40 -0700 | [diff] [blame] | 1645 | // Dry run. |
| 1646 | ASSERT_EQ(strxfrm(dst2, src2, 0), 3U); |
| 1647 | ASSERT_STREQ(dst2, ""); |
| 1648 | // Really do it. |
| 1649 | ASSERT_EQ(strxfrm(dst2, src2, sizeof(dst2)), 3U); |
| 1650 | |
| 1651 | // The "transform" of two different strings should cause different outputs. |
Elliott Hughes | fbac97a | 2019-02-04 16:46:24 -0800 | [diff] [blame] | 1652 | ASSERT_TRUE(strcmp(dst1, dst2) < 0); |
| 1653 | } |
| 1654 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 1655 | TEST(STRING_TEST, strxfrm_l_smoke) { |
| 1656 | // bionic just forwards to strxfrm(3), so this is a subset of the |
| 1657 | // strxfrm test. |
| 1658 | const char* src1 = "aab"; |
| 1659 | char dst1[16] = {}; |
| 1660 | ASSERT_EQ(strxfrm_l(dst1, src1, 0, LC_GLOBAL_LOCALE), 3U); |
| 1661 | ASSERT_STREQ(dst1, ""); |
| 1662 | ASSERT_EQ(strxfrm_l(dst1, src1, sizeof(dst1), LC_GLOBAL_LOCALE), 3U); |
| 1663 | } |
| 1664 | |
Elliott Hughes | fbac97a | 2019-02-04 16:46:24 -0800 | [diff] [blame] | 1665 | TEST(STRING_TEST, memccpy_smoke) { |
| 1666 | char dst[32]; |
| 1667 | |
| 1668 | memset(dst, 0, sizeof(dst)); |
| 1669 | char* p = static_cast<char*>(memccpy(dst, "hello world", ' ', 32)); |
| 1670 | ASSERT_STREQ("hello ", dst); |
| 1671 | ASSERT_EQ(ptrdiff_t(6), p - dst); |
| 1672 | |
| 1673 | memset(dst, 0, sizeof(dst)); |
| 1674 | ASSERT_EQ(nullptr, memccpy(dst, "hello world", ' ', 4)); |
| 1675 | ASSERT_STREQ("hell", dst); |
| 1676 | } |
Elliott Hughes | 0d64243 | 2022-08-10 23:35:03 +0000 | [diff] [blame] | 1677 | |
| 1678 | TEST(STRING_TEST, memset_explicit_smoke) { |
| 1679 | #if defined(__BIONIC__) |
| 1680 | // We can't reliably test that the compiler won't optimize out calls to |
| 1681 | // memset_explicit(), but we can at least check that it behaves like memset. |
| 1682 | char buf[32]; |
| 1683 | memset_explicit(buf, 'x', sizeof(buf)); |
| 1684 | ASSERT_TRUE(memcmp(buf, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", sizeof(buf)) == 0); |
| 1685 | #else |
| 1686 | GTEST_SKIP() << "memset_explicit not available"; |
| 1687 | #endif |
| 1688 | } |