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