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