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