Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [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 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include <errno.h> |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 21 | #include <limits.h> |
Colin Cross | 14d1507 | 2021-08-16 16:35:27 -0700 | [diff] [blame] | 22 | #include <locale.h> |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 23 | #include <math.h> |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 24 | #include <stdio.h> |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 25 | #include <sys/cdefs.h> |
Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 26 | #include <sys/socket.h> |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 27 | #include <sys/stat.h> |
Elliott Hughes | be78fc9 | 2022-07-28 20:58:45 +0000 | [diff] [blame] | 28 | #include <sys/sysinfo.h> |
Colin Cross | 14d1507 | 2021-08-16 16:35:27 -0700 | [diff] [blame] | 29 | #include <sys/types.h> |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 30 | #include <unistd.h> |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 31 | #include <wchar.h> |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 32 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 33 | #include <string> |
Ryan Prichard | c485cdb | 2019-04-30 14:47:34 -0700 | [diff] [blame] | 34 | #include <thread> |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 35 | #include <vector> |
| 36 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 37 | #include <android-base/file.h> |
Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 38 | #include <android-base/silent_death_test.h> |
Elliott Hughes | 439ebbd | 2020-12-04 18:51:42 -0800 | [diff] [blame] | 39 | #include <android-base/test_utils.h> |
Elliott Hughes | 05b675e | 2019-04-17 13:01:06 -0700 | [diff] [blame] | 40 | #include <android-base/unique_fd.h> |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 41 | |
Josh Gao | 2f06e10 | 2017-01-10 13:00:37 -0800 | [diff] [blame] | 42 | #include "utils.h" |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | 05b675e | 2019-04-17 13:01:06 -0700 | [diff] [blame] | 44 | // This #include is actually a test too. We have to duplicate the |
| 45 | // definitions of the RENAME_ constants because <linux/fs.h> also contains |
| 46 | // pollution such as BLOCK_SIZE which conflicts with lots of user code. |
| 47 | // Important to check that we have matching definitions. |
| 48 | // There's no _MAX to test that we have all the constants, sadly. |
| 49 | #include <linux/fs.h> |
| 50 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 51 | #if defined(NOFORTIFY) |
| 52 | #define STDIO_TEST stdio_nofortify |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 53 | #define STDIO_DEATHTEST stdio_nofortify_DeathTest |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 54 | #else |
| 55 | #define STDIO_TEST stdio |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 56 | #define STDIO_DEATHTEST stdio_DeathTest |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 57 | #endif |
| 58 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 59 | using namespace std::string_literals; |
| 60 | |
Elliott Hughes | 141b917 | 2021-04-09 17:13:09 -0700 | [diff] [blame] | 61 | using stdio_DeathTest = SilentDeathTest; |
| 62 | using stdio_nofortify_DeathTest = SilentDeathTest; |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 63 | |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 64 | static void SetFileTo(const char* path, const char* content) { |
| 65 | FILE* fp; |
| 66 | ASSERT_NE(nullptr, fp = fopen(path, "w")); |
| 67 | ASSERT_NE(EOF, fputs(content, fp)); |
| 68 | ASSERT_EQ(0, fclose(fp)); |
| 69 | } |
| 70 | |
| 71 | static void AssertFileIs(const char* path, const char* expected) { |
| 72 | FILE* fp; |
| 73 | ASSERT_NE(nullptr, fp = fopen(path, "r")); |
| 74 | char* line = nullptr; |
| 75 | size_t length; |
| 76 | ASSERT_NE(EOF, getline(&line, &length, fp)); |
| 77 | ASSERT_EQ(0, fclose(fp)); |
| 78 | ASSERT_STREQ(expected, line); |
| 79 | free(line); |
| 80 | } |
| 81 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 82 | static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) { |
| 83 | rewind(fp); |
| 84 | |
| 85 | char line[1024]; |
Josh Gao | 2f06e10 | 2017-01-10 13:00:37 -0800 | [diff] [blame] | 86 | memset(line, 0xff, sizeof(line)); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 87 | ASSERT_EQ(line, fgets(line, sizeof(line), fp)); |
| 88 | ASSERT_STREQ(expected, line); |
| 89 | |
| 90 | if (is_fmemopen) { |
| 91 | // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an |
| 92 | // extra empty line, but does on every C library I tested... |
| 93 | ASSERT_EQ(line, fgets(line, sizeof(line), fp)); |
| 94 | ASSERT_STREQ("", line); |
| 95 | } |
| 96 | |
| 97 | // Make sure there isn't anything else in the file. |
| 98 | ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line; |
| 99 | } |
| 100 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 101 | TEST(STDIO_TEST, flockfile_18208568_stderr) { |
Elliott Hughes | 6a03abc | 2014-11-03 12:32:17 -0800 | [diff] [blame] | 102 | // Check that we have a _recursive_ mutex for flockfile. |
| 103 | flockfile(stderr); |
| 104 | feof(stderr); // We don't care about the result, but this needs to take the lock. |
| 105 | funlockfile(stderr); |
| 106 | } |
| 107 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 108 | TEST(STDIO_TEST, flockfile_18208568_regular) { |
Elliott Hughes | 6a03abc | 2014-11-03 12:32:17 -0800 | [diff] [blame] | 109 | // We never had a bug for streams other than stdin/stdout/stderr, but test anyway. |
| 110 | FILE* fp = fopen("/dev/null", "w"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 111 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 6a03abc | 2014-11-03 12:32:17 -0800 | [diff] [blame] | 112 | flockfile(fp); |
| 113 | feof(fp); |
| 114 | funlockfile(fp); |
| 115 | fclose(fp); |
| 116 | } |
| 117 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 118 | TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) { |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 119 | FILE* fp = tmpfile(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 120 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 121 | |
| 122 | int fd = fileno(fp); |
| 123 | ASSERT_NE(fd, -1); |
| 124 | |
| 125 | struct stat sb; |
| 126 | int rc = fstat(fd, &sb); |
| 127 | ASSERT_NE(rc, -1); |
| 128 | ASSERT_EQ(sb.st_mode & 0777, 0600U); |
| 129 | |
| 130 | rc = fprintf(fp, "hello\n"); |
| 131 | ASSERT_EQ(rc, 6); |
| 132 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 133 | AssertFileIs(fp, "hello\n"); |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 134 | fclose(fp); |
| 135 | } |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 136 | |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 137 | TEST(STDIO_TEST, tmpfile64) { |
| 138 | FILE* fp = tmpfile64(); |
| 139 | ASSERT_TRUE(fp != nullptr); |
| 140 | fclose(fp); |
| 141 | } |
| 142 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 143 | TEST(STDIO_TEST, dprintf) { |
Calin Juravle | 6afb2a9 | 2014-05-22 11:47:47 +0100 | [diff] [blame] | 144 | TemporaryFile tf; |
| 145 | |
| 146 | int rc = dprintf(tf.fd, "hello\n"); |
| 147 | ASSERT_EQ(rc, 6); |
| 148 | |
Yabin Cui | 5ca4a9e | 2014-11-06 19:55:09 -0800 | [diff] [blame] | 149 | lseek(tf.fd, 0, SEEK_SET); |
Christopher Ferris | 9e01ea6 | 2014-05-29 12:49:35 -0700 | [diff] [blame] | 150 | FILE* tfile = fdopen(tf.fd, "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 151 | ASSERT_TRUE(tfile != nullptr); |
Calin Juravle | 6afb2a9 | 2014-05-22 11:47:47 +0100 | [diff] [blame] | 152 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 153 | AssertFileIs(tfile, "hello\n"); |
Christopher Ferris | 9e01ea6 | 2014-05-29 12:49:35 -0700 | [diff] [blame] | 154 | fclose(tfile); |
Calin Juravle | 6afb2a9 | 2014-05-22 11:47:47 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 157 | TEST(STDIO_TEST, getdelim) { |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 158 | FILE* fp = tmpfile(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 159 | ASSERT_TRUE(fp != nullptr); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 160 | |
| 161 | const char* line_written = "This is a test"; |
| 162 | int rc = fprintf(fp, "%s", line_written); |
| 163 | ASSERT_EQ(rc, static_cast<int>(strlen(line_written))); |
| 164 | |
| 165 | rewind(fp); |
| 166 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 167 | char* word_read = nullptr; |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 168 | size_t allocated_length = 0; |
| 169 | |
| 170 | const char* expected[] = { "This ", " ", "is ", "a ", "test" }; |
| 171 | for (size_t i = 0; i < 5; ++i) { |
| 172 | ASSERT_FALSE(feof(fp)); |
| 173 | ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i]))); |
| 174 | ASSERT_GE(allocated_length, strlen(expected[i])); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 175 | ASSERT_STREQ(expected[i], word_read); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 176 | } |
| 177 | // The last read should have set the end-of-file indicator for the stream. |
| 178 | ASSERT_TRUE(feof(fp)); |
| 179 | clearerr(fp); |
| 180 | |
| 181 | // getdelim returns -1 but doesn't set errno if we're already at EOF. |
| 182 | // It should set the end-of-file indicator for the stream, though. |
| 183 | errno = 0; |
| 184 | ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 185 | ASSERT_EQ(0, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 186 | ASSERT_TRUE(feof(fp)); |
| 187 | |
| 188 | free(word_read); |
| 189 | fclose(fp); |
| 190 | } |
| 191 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 192 | TEST(STDIO_TEST, getdelim_invalid) { |
zijunzhao | 00a3dba | 2023-03-01 00:07:40 +0000 | [diff] [blame] | 193 | #pragma clang diagnostic push |
| 194 | #pragma clang diagnostic ignored "-Wnonnull" |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 195 | FILE* fp = tmpfile(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 196 | ASSERT_TRUE(fp != nullptr); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 197 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 198 | char* buffer = nullptr; |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 199 | size_t buffer_length = 0; |
| 200 | |
| 201 | // The first argument can't be NULL. |
| 202 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 203 | ASSERT_EQ(getdelim(nullptr, &buffer_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 204 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 205 | |
| 206 | // The second argument can't be NULL. |
| 207 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 208 | ASSERT_EQ(getdelim(&buffer, nullptr, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 209 | ASSERT_EQ(EINVAL, errno); |
Elliott Hughes | 20f8aec | 2014-05-12 15:15:37 -0700 | [diff] [blame] | 210 | fclose(fp); |
zijunzhao | 00a3dba | 2023-03-01 00:07:40 +0000 | [diff] [blame] | 211 | #pragma clang diagnostic pop |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 212 | } |
| 213 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 214 | TEST(STDIO_TEST, getdelim_directory) { |
Elliott Hughes | 694fd2d | 2015-04-05 10:51:56 -0700 | [diff] [blame] | 215 | FILE* fp = fopen("/proc", "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 216 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 694fd2d | 2015-04-05 10:51:56 -0700 | [diff] [blame] | 217 | char* word_read; |
| 218 | size_t allocated_length; |
| 219 | ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp)); |
| 220 | fclose(fp); |
| 221 | } |
| 222 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 223 | TEST(STDIO_TEST, getline) { |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 224 | FILE* fp = tmpfile(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 225 | ASSERT_TRUE(fp != nullptr); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 226 | |
| 227 | const char* line_written = "This is a test for getline\n"; |
| 228 | const size_t line_count = 5; |
| 229 | |
| 230 | for (size_t i = 0; i < line_count; ++i) { |
| 231 | int rc = fprintf(fp, "%s", line_written); |
| 232 | ASSERT_EQ(rc, static_cast<int>(strlen(line_written))); |
| 233 | } |
| 234 | |
| 235 | rewind(fp); |
| 236 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 237 | char* line_read = nullptr; |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 238 | size_t allocated_length = 0; |
| 239 | |
| 240 | size_t read_line_count = 0; |
| 241 | ssize_t read_char_count; |
| 242 | while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) { |
| 243 | ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written))); |
| 244 | ASSERT_GE(allocated_length, strlen(line_written)); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 245 | ASSERT_STREQ(line_written, line_read); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 246 | ++read_line_count; |
| 247 | } |
| 248 | ASSERT_EQ(read_line_count, line_count); |
| 249 | |
| 250 | // The last read should have set the end-of-file indicator for the stream. |
| 251 | ASSERT_TRUE(feof(fp)); |
| 252 | clearerr(fp); |
| 253 | |
| 254 | // getline returns -1 but doesn't set errno if we're already at EOF. |
| 255 | // It should set the end-of-file indicator for the stream, though. |
| 256 | errno = 0; |
| 257 | ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 258 | ASSERT_EQ(0, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 259 | ASSERT_TRUE(feof(fp)); |
| 260 | |
| 261 | free(line_read); |
| 262 | fclose(fp); |
| 263 | } |
| 264 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 265 | TEST(STDIO_TEST, getline_invalid) { |
zijunzhao | 00a3dba | 2023-03-01 00:07:40 +0000 | [diff] [blame] | 266 | #pragma clang diagnostic push |
| 267 | #pragma clang diagnostic ignored "-Wnonnull" |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 268 | FILE* fp = tmpfile(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 269 | ASSERT_TRUE(fp != nullptr); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 270 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 271 | char* buffer = nullptr; |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 272 | size_t buffer_length = 0; |
| 273 | |
| 274 | // The first argument can't be NULL. |
| 275 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 276 | ASSERT_EQ(getline(nullptr, &buffer_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 277 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 278 | |
| 279 | // The second argument can't be NULL. |
| 280 | errno = 0; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 281 | ASSERT_EQ(getline(&buffer, nullptr, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 282 | ASSERT_EQ(EINVAL, errno); |
Elliott Hughes | 20f8aec | 2014-05-12 15:15:37 -0700 | [diff] [blame] | 283 | fclose(fp); |
zijunzhao | 00a3dba | 2023-03-01 00:07:40 +0000 | [diff] [blame] | 284 | #pragma clang diagnostic pop |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 285 | } |
Thorsten Glaser | c641caf | 2013-02-17 16:50:58 +0000 | [diff] [blame] | 286 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 287 | TEST(STDIO_TEST, printf_ssize_t) { |
Elliott Hughes | e255642 | 2013-02-28 10:51:14 -0800 | [diff] [blame] | 288 | // http://b/8253769 |
Elliott Hughes | e255642 | 2013-02-28 10:51:14 -0800 | [diff] [blame] | 289 | ASSERT_EQ(sizeof(ssize_t), sizeof(long int)); |
Elliott Hughes | b6e2248 | 2013-03-08 15:28:52 -0800 | [diff] [blame] | 290 | ASSERT_EQ(sizeof(ssize_t), sizeof(size_t)); |
| 291 | // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying: |
Thorsten Glaser | c641caf | 2013-02-17 16:50:58 +0000 | [diff] [blame] | 292 | // error: format '%zd' expects argument of type 'signed size_t', |
| 293 | // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format] |
| 294 | ssize_t v = 1; |
| 295 | char buf[32]; |
| 296 | snprintf(buf, sizeof(buf), "%zd", v); |
| 297 | } |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 298 | |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 299 | // https://code.google.com/p/android/issues/detail?id=64886 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 300 | TEST(STDIO_TEST, snprintf_a) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 301 | char buf[BUFSIZ]; |
| 302 | EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235)); |
| 303 | EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf); |
| 304 | } |
| 305 | |
Elliott Hughes | cdb4a26 | 2020-06-05 16:56:53 -0700 | [diff] [blame] | 306 | // http://b/152588929 |
| 307 | TEST(STDIO_TEST, snprintf_La) { |
| 308 | #if defined(__LP64__) |
| 309 | char buf[BUFSIZ]; |
| 310 | union { |
| 311 | uint64_t a[2]; |
| 312 | long double v; |
| 313 | } u; |
| 314 | |
| 315 | u.a[0] = UINT64_C(0x9b9b9b9b9b9b9b9b); |
| 316 | u.a[1] = UINT64_C(0xdfdfdfdfdfdfdfdf); |
| 317 | EXPECT_EQ(41, snprintf(buf, sizeof(buf), "<%La>", u.v)); |
| 318 | EXPECT_STREQ("<-0x1.dfdfdfdfdfdf9b9b9b9b9b9b9b9bp+8160>", buf); |
| 319 | |
| 320 | u.a[0] = UINT64_C(0xffffffffffffffff); |
| 321 | u.a[1] = UINT64_C(0x7ffeffffffffffff); |
| 322 | EXPECT_EQ(41, snprintf(buf, sizeof(buf), "<%La>", u.v)); |
| 323 | EXPECT_STREQ("<0x1.ffffffffffffffffffffffffffffp+16383>", buf); |
| 324 | |
| 325 | u.a[0] = UINT64_C(0x0000000000000000); |
| 326 | u.a[1] = UINT64_C(0x0000000000000000); |
| 327 | EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%La>", u.v)); |
| 328 | EXPECT_STREQ("<0x0p+0>", buf); |
| 329 | #else |
| 330 | GTEST_SKIP() << "no ld128"; |
| 331 | #endif |
| 332 | } |
| 333 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 334 | TEST(STDIO_TEST, snprintf_lc) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 335 | char buf[BUFSIZ]; |
| 336 | wint_t wc = L'a'; |
| 337 | EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc)); |
| 338 | EXPECT_STREQ("<a>", buf); |
| 339 | } |
| 340 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 341 | TEST(STDIO_TEST, snprintf_C) { // Synonym for %lc. |
| 342 | char buf[BUFSIZ]; |
| 343 | wchar_t wc = L'a'; |
| 344 | EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%C>", wc)); |
| 345 | EXPECT_STREQ("<a>", buf); |
| 346 | } |
| 347 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 348 | TEST(STDIO_TEST, snprintf_ls) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 349 | char buf[BUFSIZ]; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 350 | wchar_t* ws = nullptr; |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 351 | EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws)); |
| 352 | EXPECT_STREQ("<(null)>", buf); |
| 353 | |
| 354 | wchar_t chars[] = { L'h', L'i', 0 }; |
| 355 | ws = chars; |
| 356 | EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws)); |
| 357 | EXPECT_STREQ("<hi>", buf); |
| 358 | } |
| 359 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 360 | TEST(STDIO_TEST, snprintf_S) { // Synonym for %ls. |
| 361 | char buf[BUFSIZ]; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 362 | wchar_t* ws = nullptr; |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 363 | EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%S>", ws)); |
| 364 | EXPECT_STREQ("<(null)>", buf); |
| 365 | |
| 366 | wchar_t chars[] = { L'h', L'i', 0 }; |
| 367 | ws = chars; |
| 368 | EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%S>", ws)); |
| 369 | EXPECT_STREQ("<hi>", buf); |
| 370 | } |
| 371 | |
Elliott Hughes | e657eb4 | 2021-02-18 17:11:56 -0800 | [diff] [blame] | 372 | TEST_F(STDIO_DEATHTEST, snprintf_n) { |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 373 | #if defined(__BIONIC__) |
Elliott Hughes | b813a6a | 2022-08-01 22:18:40 +0000 | [diff] [blame] | 374 | #pragma clang diagnostic push |
| 375 | #pragma clang diagnostic ignored "-Wformat" |
Elliott Hughes | 41398d0 | 2018-03-07 13:32:58 -0800 | [diff] [blame] | 376 | // http://b/14492135 and http://b/31832608. |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 377 | char buf[32]; |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 378 | int i = 1234; |
Elliott Hughes | 41398d0 | 2018-03-07 13:32:58 -0800 | [diff] [blame] | 379 | EXPECT_DEATH(snprintf(buf, sizeof(buf), "a %n b", &i), "%n not allowed on Android"); |
Elliott Hughes | b813a6a | 2022-08-01 22:18:40 +0000 | [diff] [blame] | 380 | #pragma clang diagnostic pop |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 381 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 382 | GTEST_SKIP() << "glibc does allow %n"; |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 383 | #endif |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 384 | } |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 385 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 386 | TEST(STDIO_TEST, snprintf_measure) { |
| 387 | char buf[16]; |
| 388 | ASSERT_EQ(11, snprintf(buf, 0, "Hello %s", "world")); |
| 389 | } |
| 390 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 391 | TEST(STDIO_TEST, snprintf_smoke) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 392 | char buf[BUFSIZ]; |
| 393 | |
| 394 | snprintf(buf, sizeof(buf), "a"); |
| 395 | EXPECT_STREQ("a", buf); |
| 396 | |
| 397 | snprintf(buf, sizeof(buf), "%%"); |
| 398 | EXPECT_STREQ("%", buf); |
| 399 | |
| 400 | snprintf(buf, sizeof(buf), "01234"); |
| 401 | EXPECT_STREQ("01234", buf); |
| 402 | |
| 403 | snprintf(buf, sizeof(buf), "a%sb", "01234"); |
| 404 | EXPECT_STREQ("a01234b", buf); |
| 405 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 406 | char* s = nullptr; |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 407 | snprintf(buf, sizeof(buf), "a%sb", s); |
| 408 | EXPECT_STREQ("a(null)b", buf); |
| 409 | |
| 410 | snprintf(buf, sizeof(buf), "aa%scc", "bb"); |
| 411 | EXPECT_STREQ("aabbcc", buf); |
| 412 | |
| 413 | snprintf(buf, sizeof(buf), "a%cc", 'b'); |
| 414 | EXPECT_STREQ("abc", buf); |
| 415 | |
| 416 | snprintf(buf, sizeof(buf), "a%db", 1234); |
| 417 | EXPECT_STREQ("a1234b", buf); |
| 418 | |
| 419 | snprintf(buf, sizeof(buf), "a%db", -8123); |
| 420 | EXPECT_STREQ("a-8123b", buf); |
| 421 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 422 | snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010)); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 423 | EXPECT_STREQ("a16b", buf); |
| 424 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 425 | snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10)); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 426 | EXPECT_STREQ("a16b", buf); |
| 427 | |
| 428 | snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL); |
| 429 | EXPECT_STREQ("a68719476736b", buf); |
| 430 | |
| 431 | snprintf(buf, sizeof(buf), "a%ldb", 70000L); |
| 432 | EXPECT_STREQ("a70000b", buf); |
| 433 | |
| 434 | snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234)); |
| 435 | EXPECT_STREQ("a0xb0001234b", buf); |
| 436 | |
| 437 | snprintf(buf, sizeof(buf), "a%xz", 0x12ab); |
| 438 | EXPECT_STREQ("a12abz", buf); |
| 439 | |
| 440 | snprintf(buf, sizeof(buf), "a%Xz", 0x12ab); |
| 441 | EXPECT_STREQ("a12ABz", buf); |
| 442 | |
| 443 | snprintf(buf, sizeof(buf), "a%08xz", 0x123456); |
| 444 | EXPECT_STREQ("a00123456z", buf); |
| 445 | |
| 446 | snprintf(buf, sizeof(buf), "a%5dz", 1234); |
| 447 | EXPECT_STREQ("a 1234z", buf); |
| 448 | |
| 449 | snprintf(buf, sizeof(buf), "a%05dz", 1234); |
| 450 | EXPECT_STREQ("a01234z", buf); |
| 451 | |
| 452 | snprintf(buf, sizeof(buf), "a%8dz", 1234); |
| 453 | EXPECT_STREQ("a 1234z", buf); |
| 454 | |
| 455 | snprintf(buf, sizeof(buf), "a%-8dz", 1234); |
| 456 | EXPECT_STREQ("a1234 z", buf); |
| 457 | |
| 458 | snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef"); |
| 459 | EXPECT_STREQ("Aabcdef Z", buf); |
| 460 | |
| 461 | snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234); |
| 462 | EXPECT_STREQ("Ahello:1234Z", buf); |
| 463 | |
| 464 | snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5); |
| 465 | EXPECT_STREQ("a005:5:05z", buf); |
| 466 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 467 | void* p = nullptr; |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 468 | snprintf(buf, sizeof(buf), "a%d,%pz", 5, p); |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 469 | #if defined(__BIONIC__) |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 470 | EXPECT_STREQ("a5,0x0z", buf); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 471 | #else // __BIONIC__ |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 472 | EXPECT_STREQ("a5,(nil)z", buf); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 473 | #endif // __BIONIC__ |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 474 | |
| 475 | snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8); |
| 476 | EXPECT_STREQ("a68719476736,6,7,8z", buf); |
| 477 | |
| 478 | snprintf(buf, sizeof(buf), "a_%f_b", 1.23f); |
| 479 | EXPECT_STREQ("a_1.230000_b", buf); |
| 480 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 481 | snprintf(buf, sizeof(buf), "a_%g_b", 3.14); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 482 | EXPECT_STREQ("a_3.14_b", buf); |
Alexander Ivchenko | edd7c2e | 2014-04-01 17:01:39 +0400 | [diff] [blame] | 483 | |
| 484 | snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice"); |
| 485 | EXPECT_STREQ("print_me_twice print_me_twice", buf); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 488 | template <typename T> |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 489 | static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...), |
| 490 | int sscanf_fn(const T*, const T*, ...), |
| 491 | const T* fmt_string, const T* fmt, const T* fmt_plus, |
| 492 | const T* minus_inf, const T* inf_, const T* plus_inf, |
| 493 | const T* minus_nan, const T* nan_, const T* plus_nan) { |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 494 | T buf[BUFSIZ]; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 495 | float f; |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 496 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 497 | // NaN. |
| 498 | |
Elliott Hughes | e0a9a38 | 2022-10-25 22:56:43 +0000 | [diff] [blame] | 499 | snprintf_fn(buf, sizeof(buf), fmt, nan("")); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 500 | EXPECT_STREQ(nan_, buf) << fmt; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 501 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 502 | EXPECT_TRUE(isnan(f)); |
| 503 | |
Elliott Hughes | e0a9a38 | 2022-10-25 22:56:43 +0000 | [diff] [blame] | 504 | snprintf_fn(buf, sizeof(buf), fmt, -nan("")); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 505 | EXPECT_STREQ(minus_nan, buf) << fmt; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 506 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 507 | EXPECT_TRUE(isnan(f)); |
| 508 | |
Elliott Hughes | e0a9a38 | 2022-10-25 22:56:43 +0000 | [diff] [blame] | 509 | snprintf_fn(buf, sizeof(buf), fmt_plus, nan("")); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 510 | EXPECT_STREQ(plus_nan, buf) << fmt_plus; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 511 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 512 | EXPECT_TRUE(isnan(f)); |
| 513 | |
Elliott Hughes | e0a9a38 | 2022-10-25 22:56:43 +0000 | [diff] [blame] | 514 | snprintf_fn(buf, sizeof(buf), fmt_plus, -nan("")); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 515 | EXPECT_STREQ(minus_nan, buf) << fmt_plus; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 516 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 517 | EXPECT_TRUE(isnan(f)); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 518 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 519 | // Inf. |
| 520 | |
| 521 | snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 522 | EXPECT_STREQ(inf_, buf) << fmt; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 523 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 524 | EXPECT_EQ(HUGE_VALF, f); |
| 525 | |
| 526 | snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 527 | EXPECT_STREQ(minus_inf, buf) << fmt; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 528 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 529 | EXPECT_EQ(-HUGE_VALF, f); |
| 530 | |
| 531 | snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 532 | EXPECT_STREQ(plus_inf, buf) << fmt_plus; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 533 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 534 | EXPECT_EQ(HUGE_VALF, f); |
| 535 | |
| 536 | snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 537 | EXPECT_STREQ(minus_inf, buf) << fmt_plus; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 538 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 539 | EXPECT_EQ(-HUGE_VALF, f); |
| 540 | |
| 541 | // Check case-insensitivity. |
| 542 | snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]"); |
| 543 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf; |
| 544 | EXPECT_EQ(HUGE_VALF, f); |
| 545 | snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]"); |
| 546 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf; |
| 547 | EXPECT_TRUE(isnan(f)); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 550 | TEST(STDIO_TEST, snprintf_sscanf_inf_nan) { |
| 551 | CheckInfNan(snprintf, sscanf, "%s", |
| 552 | "[%a]", "[%+a]", |
| 553 | "[-inf]", "[inf]", "[+inf]", |
| 554 | "[-nan]", "[nan]", "[+nan]"); |
| 555 | CheckInfNan(snprintf, sscanf, "%s", |
| 556 | "[%A]", "[%+A]", |
| 557 | "[-INF]", "[INF]", "[+INF]", |
| 558 | "[-NAN]", "[NAN]", "[+NAN]"); |
| 559 | CheckInfNan(snprintf, sscanf, "%s", |
| 560 | "[%e]", "[%+e]", |
| 561 | "[-inf]", "[inf]", "[+inf]", |
| 562 | "[-nan]", "[nan]", "[+nan]"); |
| 563 | CheckInfNan(snprintf, sscanf, "%s", |
| 564 | "[%E]", "[%+E]", |
| 565 | "[-INF]", "[INF]", "[+INF]", |
| 566 | "[-NAN]", "[NAN]", "[+NAN]"); |
| 567 | CheckInfNan(snprintf, sscanf, "%s", |
| 568 | "[%f]", "[%+f]", |
| 569 | "[-inf]", "[inf]", "[+inf]", |
| 570 | "[-nan]", "[nan]", "[+nan]"); |
| 571 | CheckInfNan(snprintf, sscanf, "%s", |
| 572 | "[%F]", "[%+F]", |
| 573 | "[-INF]", "[INF]", "[+INF]", |
| 574 | "[-NAN]", "[NAN]", "[+NAN]"); |
| 575 | CheckInfNan(snprintf, sscanf, "%s", |
| 576 | "[%g]", "[%+g]", |
| 577 | "[-inf]", "[inf]", "[+inf]", |
| 578 | "[-nan]", "[nan]", "[+nan]"); |
| 579 | CheckInfNan(snprintf, sscanf, "%s", |
| 580 | "[%G]", "[%+G]", |
| 581 | "[-INF]", "[INF]", "[+INF]", |
| 582 | "[-NAN]", "[NAN]", "[+NAN]"); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 583 | } |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 584 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 585 | TEST(STDIO_TEST, swprintf_swscanf_inf_nan) { |
| 586 | CheckInfNan(swprintf, swscanf, L"%s", |
| 587 | L"[%a]", L"[%+a]", |
| 588 | L"[-inf]", L"[inf]", L"[+inf]", |
| 589 | L"[-nan]", L"[nan]", L"[+nan]"); |
| 590 | CheckInfNan(swprintf, swscanf, L"%s", |
| 591 | L"[%A]", L"[%+A]", |
| 592 | L"[-INF]", L"[INF]", L"[+INF]", |
| 593 | L"[-NAN]", L"[NAN]", L"[+NAN]"); |
| 594 | CheckInfNan(swprintf, swscanf, L"%s", |
| 595 | L"[%e]", L"[%+e]", |
| 596 | L"[-inf]", L"[inf]", L"[+inf]", |
| 597 | L"[-nan]", L"[nan]", L"[+nan]"); |
| 598 | CheckInfNan(swprintf, swscanf, L"%s", |
| 599 | L"[%E]", L"[%+E]", |
| 600 | L"[-INF]", L"[INF]", L"[+INF]", |
| 601 | L"[-NAN]", L"[NAN]", L"[+NAN]"); |
| 602 | CheckInfNan(swprintf, swscanf, L"%s", |
| 603 | L"[%f]", L"[%+f]", |
| 604 | L"[-inf]", L"[inf]", L"[+inf]", |
| 605 | L"[-nan]", L"[nan]", L"[+nan]"); |
| 606 | CheckInfNan(swprintf, swscanf, L"%s", |
| 607 | L"[%F]", L"[%+F]", |
| 608 | L"[-INF]", L"[INF]", L"[+INF]", |
| 609 | L"[-NAN]", L"[NAN]", L"[+NAN]"); |
| 610 | CheckInfNan(swprintf, swscanf, L"%s", |
| 611 | L"[%g]", L"[%+g]", |
| 612 | L"[-inf]", L"[inf]", L"[+inf]", |
| 613 | L"[-nan]", L"[nan]", L"[+nan]"); |
| 614 | CheckInfNan(swprintf, swscanf, L"%s", |
| 615 | L"[%G]", L"[%+G]", |
| 616 | L"[-INF]", L"[INF]", L"[+INF]", |
| 617 | L"[-NAN]", L"[NAN]", L"[+NAN]"); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Dan Albert | 9601f16 | 2017-08-09 14:59:06 -0700 | [diff] [blame] | 620 | TEST(STDIO_TEST, swprintf) { |
| 621 | constexpr size_t nchars = 32; |
| 622 | wchar_t buf[nchars]; |
| 623 | |
| 624 | ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno); |
| 625 | ASSERT_EQ(std::wstring(L"ab"), buf); |
| 626 | ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde")); |
| 627 | ASSERT_EQ(std::wstring(L"abcde"), buf); |
| 628 | |
| 629 | // Unlike swprintf(), swprintf() returns -1 in case of truncation |
| 630 | // and doesn't necessarily zero-terminate the output! |
| 631 | ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde")); |
| 632 | |
| 633 | const char kString[] = "Hello, World"; |
| 634 | ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString)); |
| 635 | ASSERT_EQ(std::wstring(L"Hello, World"), buf); |
| 636 | ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString)); |
| 637 | ASSERT_EQ(std::wstring(L"Hello, World"), buf); |
| 638 | } |
| 639 | |
| 640 | TEST(STDIO_TEST, swprintf_a) { |
| 641 | constexpr size_t nchars = 32; |
| 642 | wchar_t buf[nchars]; |
| 643 | |
| 644 | ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535)); |
| 645 | ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf); |
| 646 | } |
| 647 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 648 | TEST(STDIO_TEST, swprintf_lc) { |
| 649 | constexpr size_t nchars = 32; |
| 650 | wchar_t buf[nchars]; |
| 651 | |
| 652 | wint_t wc = L'a'; |
| 653 | EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc)); |
| 654 | EXPECT_EQ(std::wstring(L"<a>"), buf); |
| 655 | } |
| 656 | |
| 657 | TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc. |
| 658 | constexpr size_t nchars = 32; |
| 659 | wchar_t buf[nchars]; |
| 660 | |
| 661 | wint_t wc = L'a'; |
| 662 | EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc)); |
| 663 | EXPECT_EQ(std::wstring(L"<a>"), buf); |
| 664 | } |
| 665 | |
Elliott Hughes | 618303c | 2017-11-02 16:58:44 -0700 | [diff] [blame] | 666 | TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) { |
| 667 | constexpr size_t nchars = 32; |
| 668 | wchar_t buf[nchars]; |
| 669 | |
| 670 | swprintf(buf, nchars, L"%jd", INTMAX_MAX); |
| 671 | EXPECT_EQ(std::wstring(L"9223372036854775807"), buf); |
| 672 | } |
| 673 | |
| 674 | TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) { |
| 675 | constexpr size_t nchars = 32; |
| 676 | wchar_t buf[nchars]; |
| 677 | |
| 678 | swprintf(buf, nchars, L"%jd", INTMAX_MIN); |
| 679 | EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf); |
| 680 | } |
| 681 | |
| 682 | TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) { |
| 683 | constexpr size_t nchars = 32; |
| 684 | wchar_t buf[nchars]; |
| 685 | |
| 686 | swprintf(buf, nchars, L"%ju", UINTMAX_MAX); |
| 687 | EXPECT_EQ(std::wstring(L"18446744073709551615"), buf); |
| 688 | } |
| 689 | |
| 690 | TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) { |
| 691 | constexpr size_t nchars = 32; |
| 692 | wchar_t buf[nchars]; |
| 693 | |
| 694 | swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX); |
| 695 | EXPECT_EQ(std::wstring(L"18446744073709551615"), buf); |
| 696 | } |
| 697 | |
Dan Albert | 9601f16 | 2017-08-09 14:59:06 -0700 | [diff] [blame] | 698 | TEST(STDIO_TEST, swprintf_ls) { |
| 699 | constexpr size_t nchars = 32; |
| 700 | wchar_t buf[nchars]; |
| 701 | |
| 702 | static const wchar_t kWideString[] = L"Hello\uff41 World"; |
| 703 | ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString)); |
| 704 | ASSERT_EQ(std::wstring(kWideString), buf); |
| 705 | ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString)); |
| 706 | ASSERT_EQ(std::wstring(kWideString), buf); |
| 707 | } |
| 708 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 709 | TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls. |
| 710 | constexpr size_t nchars = 32; |
| 711 | wchar_t buf[nchars]; |
| 712 | |
| 713 | static const wchar_t kWideString[] = L"Hello\uff41 World"; |
| 714 | ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString)); |
| 715 | ASSERT_EQ(std::wstring(kWideString), buf); |
| 716 | ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString)); |
| 717 | ASSERT_EQ(std::wstring(kWideString), buf); |
| 718 | } |
| 719 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 720 | TEST(STDIO_TEST, snprintf_d_INT_MAX) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 721 | char buf[BUFSIZ]; |
| 722 | snprintf(buf, sizeof(buf), "%d", INT_MAX); |
| 723 | EXPECT_STREQ("2147483647", buf); |
| 724 | } |
| 725 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 726 | TEST(STDIO_TEST, snprintf_d_INT_MIN) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 727 | char buf[BUFSIZ]; |
| 728 | snprintf(buf, sizeof(buf), "%d", INT_MIN); |
| 729 | EXPECT_STREQ("-2147483648", buf); |
| 730 | } |
| 731 | |
Elliott Hughes | 618303c | 2017-11-02 16:58:44 -0700 | [diff] [blame] | 732 | TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) { |
| 733 | char buf[BUFSIZ]; |
| 734 | snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX); |
| 735 | EXPECT_STREQ("9223372036854775807", buf); |
| 736 | } |
| 737 | |
| 738 | TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) { |
| 739 | char buf[BUFSIZ]; |
| 740 | snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN); |
| 741 | EXPECT_STREQ("-9223372036854775808", buf); |
| 742 | } |
| 743 | |
| 744 | TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) { |
| 745 | char buf[BUFSIZ]; |
| 746 | snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX); |
| 747 | EXPECT_STREQ("18446744073709551615", buf); |
| 748 | } |
| 749 | |
| 750 | TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) { |
| 751 | char buf[BUFSIZ]; |
| 752 | snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX); |
| 753 | EXPECT_STREQ("18446744073709551615", buf); |
| 754 | } |
| 755 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 756 | TEST(STDIO_TEST, snprintf_ld_LONG_MAX) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 757 | char buf[BUFSIZ]; |
| 758 | snprintf(buf, sizeof(buf), "%ld", LONG_MAX); |
Josh Gao | b36efa4 | 2016-09-15 13:55:41 -0700 | [diff] [blame] | 759 | #if defined(__LP64__) |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 760 | EXPECT_STREQ("9223372036854775807", buf); |
| 761 | #else |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 762 | EXPECT_STREQ("2147483647", buf); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 763 | #endif |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 764 | } |
| 765 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 766 | TEST(STDIO_TEST, snprintf_ld_LONG_MIN) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 767 | char buf[BUFSIZ]; |
| 768 | snprintf(buf, sizeof(buf), "%ld", LONG_MIN); |
Josh Gao | b36efa4 | 2016-09-15 13:55:41 -0700 | [diff] [blame] | 769 | #if defined(__LP64__) |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 770 | EXPECT_STREQ("-9223372036854775808", buf); |
| 771 | #else |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 772 | EXPECT_STREQ("-2147483648", buf); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 773 | #endif |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 776 | TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 777 | char buf[BUFSIZ]; |
| 778 | snprintf(buf, sizeof(buf), "%lld", LLONG_MAX); |
| 779 | EXPECT_STREQ("9223372036854775807", buf); |
| 780 | } |
| 781 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 782 | TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 783 | char buf[BUFSIZ]; |
| 784 | snprintf(buf, sizeof(buf), "%lld", LLONG_MIN); |
| 785 | EXPECT_STREQ("-9223372036854775808", buf); |
| 786 | } |
| 787 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 788 | TEST(STDIO_TEST, snprintf_o_UINT_MAX) { |
| 789 | char buf[BUFSIZ]; |
| 790 | snprintf(buf, sizeof(buf), "%o", UINT_MAX); |
| 791 | EXPECT_STREQ("37777777777", buf); |
| 792 | } |
| 793 | |
| 794 | TEST(STDIO_TEST, snprintf_u_UINT_MAX) { |
| 795 | char buf[BUFSIZ]; |
| 796 | snprintf(buf, sizeof(buf), "%u", UINT_MAX); |
| 797 | EXPECT_STREQ("4294967295", buf); |
| 798 | } |
| 799 | |
| 800 | TEST(STDIO_TEST, snprintf_x_UINT_MAX) { |
| 801 | char buf[BUFSIZ]; |
| 802 | snprintf(buf, sizeof(buf), "%x", UINT_MAX); |
| 803 | EXPECT_STREQ("ffffffff", buf); |
| 804 | } |
| 805 | |
| 806 | TEST(STDIO_TEST, snprintf_X_UINT_MAX) { |
| 807 | char buf[BUFSIZ]; |
| 808 | snprintf(buf, sizeof(buf), "%X", UINT_MAX); |
| 809 | EXPECT_STREQ("FFFFFFFF", buf); |
| 810 | } |
| 811 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 812 | TEST(STDIO_TEST, snprintf_e) { |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 813 | char buf[BUFSIZ]; |
| 814 | |
| 815 | snprintf(buf, sizeof(buf), "%e", 1.5); |
| 816 | EXPECT_STREQ("1.500000e+00", buf); |
| 817 | |
Chih-Hung Hsieh | c2edae3 | 2018-12-11 15:16:24 -0800 | [diff] [blame] | 818 | snprintf(buf, sizeof(buf), "%Le", 1.5L); |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 819 | EXPECT_STREQ("1.500000e+00", buf); |
| 820 | } |
| 821 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 822 | TEST(STDIO_TEST, snprintf_negative_zero_5084292) { |
Elliott Hughes | e77f38f | 2014-05-14 12:39:12 -0700 | [diff] [blame] | 823 | char buf[BUFSIZ]; |
| 824 | |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 825 | snprintf(buf, sizeof(buf), "%e", -0.0); |
| 826 | EXPECT_STREQ("-0.000000e+00", buf); |
| 827 | snprintf(buf, sizeof(buf), "%E", -0.0); |
| 828 | EXPECT_STREQ("-0.000000E+00", buf); |
Elliott Hughes | e77f38f | 2014-05-14 12:39:12 -0700 | [diff] [blame] | 829 | snprintf(buf, sizeof(buf), "%f", -0.0); |
| 830 | EXPECT_STREQ("-0.000000", buf); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 831 | snprintf(buf, sizeof(buf), "%F", -0.0); |
| 832 | EXPECT_STREQ("-0.000000", buf); |
| 833 | snprintf(buf, sizeof(buf), "%g", -0.0); |
| 834 | EXPECT_STREQ("-0", buf); |
| 835 | snprintf(buf, sizeof(buf), "%G", -0.0); |
| 836 | EXPECT_STREQ("-0", buf); |
| 837 | snprintf(buf, sizeof(buf), "%a", -0.0); |
| 838 | EXPECT_STREQ("-0x0p+0", buf); |
| 839 | snprintf(buf, sizeof(buf), "%A", -0.0); |
| 840 | EXPECT_STREQ("-0X0P+0", buf); |
Elliott Hughes | e77f38f | 2014-05-14 12:39:12 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 843 | TEST(STDIO_TEST, snprintf_utf8_15439554) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 844 | locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr); |
Wally Yau | a40fdbd | 2014-08-26 09:47:23 -0700 | [diff] [blame] | 845 | locale_t old_locale = uselocale(cloc); |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 846 | |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 847 | // http://b/15439554 |
| 848 | char buf[BUFSIZ]; |
| 849 | |
| 850 | // 1-byte character. |
| 851 | snprintf(buf, sizeof(buf), "%dx%d", 1, 2); |
| 852 | EXPECT_STREQ("1x2", buf); |
| 853 | // 2-byte character. |
| 854 | snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2); |
| 855 | EXPECT_STREQ("1¢2", buf); |
| 856 | // 3-byte character. |
| 857 | snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2); |
| 858 | EXPECT_STREQ("1€2", buf); |
| 859 | // 4-byte character. |
| 860 | snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2); |
| 861 | EXPECT_STREQ("1ð¤¢2", buf); |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 862 | |
Wally Yau | a40fdbd | 2014-08-26 09:47:23 -0700 | [diff] [blame] | 863 | uselocale(old_locale); |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 864 | freelocale(cloc); |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Elliott Hughes | 43f7c87 | 2016-02-05 11:18:41 -0800 | [diff] [blame] | 867 | static void* snprintf_small_stack_fn(void*) { |
| 868 | // Make life (realistically) hard for ourselves by allocating our own buffer for the result. |
| 869 | char buf[PATH_MAX]; |
| 870 | snprintf(buf, sizeof(buf), "/proc/%d", getpid()); |
| 871 | return nullptr; |
| 872 | } |
| 873 | |
| 874 | TEST(STDIO_TEST, snprintf_small_stack) { |
| 875 | // Is it safe to call snprintf on a thread with a small stack? |
| 876 | // (The snprintf implementation puts some pretty large buffers on the stack.) |
| 877 | pthread_attr_t a; |
| 878 | ASSERT_EQ(0, pthread_attr_init(&a)); |
| 879 | ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN)); |
| 880 | |
| 881 | pthread_t t; |
| 882 | ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr)); |
| 883 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 884 | } |
| 885 | |
Elliott Hughes | 8200e55 | 2016-02-05 21:57:37 -0800 | [diff] [blame] | 886 | TEST(STDIO_TEST, snprintf_asterisk_overflow) { |
| 887 | char buf[128]; |
| 888 | ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!')); |
| 889 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!')); |
| 890 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!')); |
| 891 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!')); |
| 892 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!')); |
| 893 | |
| 894 | // INT_MAX-1, INT_MAX, INT_MAX+1. |
| 895 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!')); |
| 896 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!')); |
| 897 | ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!')); |
| 898 | ASSERT_EQ(ENOMEM, errno); |
| 899 | } |
| 900 | |
Elliott Hughes | 5dc3130 | 2020-01-07 08:48:10 -0800 | [diff] [blame] | 901 | // Inspired by https://github.com/landley/toybox/issues/163. |
| 902 | TEST(STDIO_TEST, printf_NULL) { |
| 903 | char buf[128]; |
| 904 | char* null = nullptr; |
| 905 | EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%*.*s>", 2, 2, null)); |
| 906 | EXPECT_STREQ("<(n>", buf); |
| 907 | EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%*.*s>", 2, 8, null)); |
| 908 | EXPECT_STREQ("<(null)>", buf); |
| 909 | EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%*.*s>", 8, 2, null)); |
| 910 | EXPECT_STREQ("< (n>", buf); |
| 911 | EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%*.*s>", 8, 8, null)); |
| 912 | EXPECT_STREQ("< (null)>", buf); |
| 913 | } |
| 914 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 915 | TEST(STDIO_TEST, fprintf) { |
| 916 | TemporaryFile tf; |
| 917 | |
| 918 | FILE* tfile = fdopen(tf.fd, "r+"); |
| 919 | ASSERT_TRUE(tfile != nullptr); |
| 920 | |
| 921 | ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc")); |
| 922 | AssertFileIs(tfile, "123 abc"); |
| 923 | fclose(tfile); |
| 924 | } |
| 925 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 926 | TEST(STDIO_TEST, fprintf_failures_7229520) { |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 927 | // http://b/7229520 |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 928 | FILE* fp; |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 929 | int fd_rdonly = open("/dev/null", O_RDONLY); |
| 930 | ASSERT_NE(-1, fd_rdonly); |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 931 | |
| 932 | // Unbuffered case where the fprintf(3) itself fails. |
| 933 | ASSERT_NE(nullptr, fp = tmpfile()); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 934 | setbuf(fp, nullptr); |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 935 | ASSERT_EQ(4, fprintf(fp, "epic")); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 936 | ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp))); |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 937 | ASSERT_EQ(-1, fprintf(fp, "fail")); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 938 | ASSERT_EQ(0, fclose(fp)); |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 939 | |
| 940 | // Buffered case where we won't notice until the fclose(3). |
| 941 | // It's likely this is what was actually seen in http://b/7229520, |
| 942 | // and that expecting fprintf to fail is setting yourself up for |
| 943 | // disappointment. Remember to check fclose(3)'s return value, kids! |
| 944 | ASSERT_NE(nullptr, fp = tmpfile()); |
| 945 | ASSERT_EQ(4, fprintf(fp, "epic")); |
Josh Gao | f6e5b58 | 2018-06-01 15:30:54 -0700 | [diff] [blame] | 946 | ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp))); |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 947 | ASSERT_EQ(4, fprintf(fp, "fail")); |
| 948 | ASSERT_EQ(-1, fclose(fp)); |
| 949 | } |
| 950 | |
Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 951 | TEST(STDIO_TEST, popen_r) { |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 952 | FILE* fp = popen("cat /proc/version", "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 953 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 954 | |
| 955 | char buf[16]; |
| 956 | char* s = fgets(buf, sizeof(buf), fp); |
| 957 | buf[13] = '\0'; |
| 958 | ASSERT_STREQ("Linux version", s); |
| 959 | |
| 960 | ASSERT_EQ(0, pclose(fp)); |
| 961 | } |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 962 | |
Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 963 | TEST(STDIO_TEST, popen_socketpair) { |
| 964 | FILE* fp = popen("cat", "r+"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 965 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 966 | |
| 967 | fputs("hello\nworld\n", fp); |
| 968 | fflush(fp); |
| 969 | |
| 970 | char buf[16]; |
| 971 | ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp)); |
| 972 | EXPECT_STREQ("hello\n", buf); |
| 973 | ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp)); |
| 974 | EXPECT_STREQ("world\n", buf); |
| 975 | |
| 976 | ASSERT_EQ(0, pclose(fp)); |
| 977 | } |
| 978 | |
| 979 | TEST(STDIO_TEST, popen_socketpair_shutdown) { |
| 980 | FILE* fp = popen("uniq -c", "r+"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 981 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 982 | |
| 983 | fputs("a\na\na\na\nb\n", fp); |
| 984 | fflush(fp); |
| 985 | ASSERT_EQ(0, shutdown(fileno(fp), SHUT_WR)); |
| 986 | |
| 987 | char buf[16]; |
| 988 | ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp)); |
| 989 | EXPECT_STREQ(" 4 a\n", buf); |
| 990 | ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp)); |
| 991 | EXPECT_STREQ(" 1 b\n", buf); |
| 992 | |
| 993 | ASSERT_EQ(0, pclose(fp)); |
| 994 | } |
| 995 | |
| 996 | TEST(STDIO_TEST, popen_return_value_0) { |
| 997 | FILE* fp = popen("true", "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 998 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 999 | int status = pclose(fp); |
| 1000 | EXPECT_TRUE(WIFEXITED(status)); |
| 1001 | EXPECT_EQ(0, WEXITSTATUS(status)); |
| 1002 | } |
| 1003 | |
| 1004 | TEST(STDIO_TEST, popen_return_value_1) { |
| 1005 | FILE* fp = popen("false", "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1006 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 1007 | int status = pclose(fp); |
| 1008 | EXPECT_TRUE(WIFEXITED(status)); |
| 1009 | EXPECT_EQ(1, WEXITSTATUS(status)); |
| 1010 | } |
| 1011 | |
| 1012 | TEST(STDIO_TEST, popen_return_value_signal) { |
| 1013 | FILE* fp = popen("kill -7 $$", "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1014 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 468efc8 | 2018-07-10 14:39:49 -0700 | [diff] [blame] | 1015 | int status = pclose(fp); |
| 1016 | EXPECT_TRUE(WIFSIGNALED(status)); |
| 1017 | EXPECT_EQ(7, WTERMSIG(status)); |
| 1018 | } |
| 1019 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1020 | TEST(STDIO_TEST, getc) { |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 1021 | FILE* fp = fopen("/proc/version", "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1022 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 1023 | ASSERT_EQ('L', getc(fp)); |
| 1024 | ASSERT_EQ('i', getc(fp)); |
| 1025 | ASSERT_EQ('n', getc(fp)); |
| 1026 | ASSERT_EQ('u', getc(fp)); |
| 1027 | ASSERT_EQ('x', getc(fp)); |
| 1028 | fclose(fp); |
| 1029 | } |
| 1030 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1031 | TEST(STDIO_TEST, putc) { |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 1032 | FILE* fp = fopen("/proc/version", "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1033 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 1034 | ASSERT_EQ(EOF, putc('x', fp)); |
| 1035 | fclose(fp); |
| 1036 | } |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 1037 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 1038 | TEST(STDIO_TEST, sscanf_swscanf) { |
| 1039 | struct stuff { |
| 1040 | char s1[123]; |
Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 1041 | int i1, i2; |
| 1042 | char cs1[3]; |
| 1043 | char s2[3]; |
| 1044 | char c1; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 1045 | double d1; |
| 1046 | float f1; |
Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 1047 | char s3[123]; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 1048 | |
| 1049 | void Check() { |
Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 1050 | EXPECT_STREQ("hello", s1); |
| 1051 | EXPECT_EQ(123, i1); |
| 1052 | EXPECT_EQ(456, i2); |
| 1053 | EXPECT_EQ('a', cs1[0]); |
| 1054 | EXPECT_EQ('b', cs1[1]); |
| 1055 | EXPECT_EQ('x', cs1[2]); // No terminating NUL. |
| 1056 | EXPECT_STREQ("AB", s2); // Terminating NUL. |
| 1057 | EXPECT_EQ('!', c1); |
| 1058 | EXPECT_DOUBLE_EQ(1.23, d1); |
| 1059 | EXPECT_FLOAT_EQ(9.0f, f1); |
| 1060 | EXPECT_STREQ("world", s3); |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 1061 | } |
| 1062 | } s; |
| 1063 | |
Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 1064 | memset(&s, 'x', sizeof(s)); |
| 1065 | ASSERT_EQ(9, sscanf(" hello 123 456abAB! 1.23 0x1.2p3 world", |
| 1066 | "%s %i%i%2c%[A-Z]%c %lf %f %s", |
| 1067 | s.s1, &s.i1, &s.i2, s.cs1, s.s2, &s.c1, &s.d1, &s.f1, s.s3)); |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 1068 | s.Check(); |
| 1069 | |
Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 1070 | memset(&s, 'x', sizeof(s)); |
| 1071 | ASSERT_EQ(9, swscanf(L" hello 123 456abAB! 1.23 0x1.2p3 world", |
| 1072 | L"%s %i%i%2c%[A-Z]%c %lf %f %s", |
| 1073 | s.s1, &s.i1, &s.i2, s.cs1, s.s2, &s.c1, &s.d1, &s.f1, s.s3)); |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 1074 | s.Check(); |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 1075 | } |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 1076 | |
Elliott Hughes | 0d3ba1f | 2017-12-06 16:41:35 -0800 | [diff] [blame] | 1077 | template <typename T> |
| 1078 | static void CheckScanf(int sscanf_fn(const T*, const T*, ...), |
| 1079 | const T* input, const T* fmt, |
| 1080 | int expected_count, const char* expected_string) { |
| 1081 | char buf[256] = {}; |
| 1082 | ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &buf)) << fmt; |
| 1083 | ASSERT_STREQ(expected_string, buf) << fmt; |
| 1084 | } |
| 1085 | |
| 1086 | TEST(STDIO_TEST, sscanf_ccl) { |
| 1087 | // `abc` is just those characters. |
| 1088 | CheckScanf(sscanf, "abcd", "%[abc]", 1, "abc"); |
| 1089 | // `a-c` is the range 'a' .. 'c'. |
| 1090 | CheckScanf(sscanf, "abcd", "%[a-c]", 1, "abc"); |
| 1091 | CheckScanf(sscanf, "-d", "%[a-c]", 0, ""); |
| 1092 | CheckScanf(sscanf, "ac-bAd", "%[a--c]", 1, "ac-bA"); |
| 1093 | // `a-c-e` is equivalent to `a-e`. |
| 1094 | CheckScanf(sscanf, "abcdefg", "%[a-c-e]", 1, "abcde"); |
| 1095 | // `e-a` is equivalent to `ae-` (because 'e' > 'a'). |
| 1096 | CheckScanf(sscanf, "-a-e-b", "%[e-a]", 1, "-a-e-"); |
| 1097 | // An initial '^' negates the set. |
| 1098 | CheckScanf(sscanf, "abcde", "%[^d]", 1, "abc"); |
| 1099 | CheckScanf(sscanf, "abcdefgh", "%[^c-d]", 1, "ab"); |
| 1100 | CheckScanf(sscanf, "hgfedcba", "%[^c-d]", 1, "hgfe"); |
| 1101 | // The first character may be ']' or '-' without being special. |
| 1102 | CheckScanf(sscanf, "[[]]x", "%[][]", 1, "[[]]"); |
| 1103 | CheckScanf(sscanf, "-a-x", "%[-a]", 1, "-a-"); |
| 1104 | // The last character may be '-' without being special. |
| 1105 | CheckScanf(sscanf, "-a-x", "%[a-]", 1, "-a-"); |
| 1106 | // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation). |
| 1107 | CheckScanf(sscanf, "+,-/.", "%[+--/]", 1, "+,-/"); |
| 1108 | } |
| 1109 | |
| 1110 | TEST(STDIO_TEST, swscanf_ccl) { |
| 1111 | // `abc` is just those characters. |
| 1112 | CheckScanf(swscanf, L"abcd", L"%[abc]", 1, "abc"); |
| 1113 | // `a-c` is the range 'a' .. 'c'. |
| 1114 | CheckScanf(swscanf, L"abcd", L"%[a-c]", 1, "abc"); |
| 1115 | CheckScanf(swscanf, L"-d", L"%[a-c]", 0, ""); |
| 1116 | CheckScanf(swscanf, L"ac-bAd", L"%[a--c]", 1, "ac-bA"); |
| 1117 | // `a-c-e` is equivalent to `a-e`. |
| 1118 | CheckScanf(swscanf, L"abcdefg", L"%[a-c-e]", 1, "abcde"); |
| 1119 | // `e-a` is equivalent to `ae-` (because 'e' > 'a'). |
| 1120 | CheckScanf(swscanf, L"-a-e-b", L"%[e-a]", 1, "-a-e-"); |
| 1121 | // An initial '^' negates the set. |
| 1122 | CheckScanf(swscanf, L"abcde", L"%[^d]", 1, "abc"); |
| 1123 | CheckScanf(swscanf, L"abcdefgh", L"%[^c-d]", 1, "ab"); |
| 1124 | CheckScanf(swscanf, L"hgfedcba", L"%[^c-d]", 1, "hgfe"); |
| 1125 | // The first character may be ']' or '-' without being special. |
| 1126 | CheckScanf(swscanf, L"[[]]x", L"%[][]", 1, "[[]]"); |
| 1127 | CheckScanf(swscanf, L"-a-x", L"%[-a]", 1, "-a-"); |
| 1128 | // The last character may be '-' without being special. |
| 1129 | CheckScanf(swscanf, L"-a-x", L"%[a-]", 1, "-a-"); |
| 1130 | // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation). |
| 1131 | CheckScanf(swscanf, L"+,-/.", L"%[+--/]", 1, "+,-/"); |
| 1132 | } |
| 1133 | |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 1134 | template <typename T1, typename T2> |
| 1135 | static void CheckScanfM(int sscanf_fn(const T1*, const T1*, ...), |
| 1136 | const T1* input, const T1* fmt, |
| 1137 | int expected_count, const T2* expected_string) { |
| 1138 | T2* result = nullptr; |
| 1139 | ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &result)) << fmt; |
| 1140 | if (expected_string == nullptr) { |
| 1141 | ASSERT_EQ(nullptr, result); |
| 1142 | } else { |
| 1143 | ASSERT_STREQ(expected_string, result) << fmt; |
| 1144 | } |
| 1145 | free(result); |
| 1146 | } |
| 1147 | |
| 1148 | TEST(STDIO_TEST, sscanf_mc) { |
| 1149 | char* p1 = nullptr; |
| 1150 | char* p2 = nullptr; |
| 1151 | ASSERT_EQ(2, sscanf("hello", "%mc%mc", &p1, &p2)); |
| 1152 | ASSERT_EQ('h', *p1); |
| 1153 | ASSERT_EQ('e', *p2); |
| 1154 | free(p1); |
| 1155 | free(p2); |
| 1156 | |
| 1157 | p1 = nullptr; |
| 1158 | ASSERT_EQ(1, sscanf("hello", "%4mc", &p1)); |
| 1159 | ASSERT_EQ('h', p1[0]); |
| 1160 | ASSERT_EQ('e', p1[1]); |
| 1161 | ASSERT_EQ('l', p1[2]); |
| 1162 | ASSERT_EQ('l', p1[3]); |
| 1163 | free(p1); |
| 1164 | |
| 1165 | p1 = nullptr; |
| 1166 | ASSERT_EQ(1, sscanf("hello world", "%30mc", &p1)); |
| 1167 | ASSERT_EQ('h', p1[0]); |
| 1168 | ASSERT_EQ('e', p1[1]); |
| 1169 | ASSERT_EQ('l', p1[2]); |
| 1170 | ASSERT_EQ('l', p1[3]); |
| 1171 | ASSERT_EQ('o', p1[4]); |
| 1172 | free(p1); |
| 1173 | } |
| 1174 | |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 1175 | TEST(STDIO_TEST, sscanf_mlc) { |
| 1176 | // This is so useless that clang doesn't even believe it exists... |
| 1177 | #pragma clang diagnostic push |
| 1178 | #pragma clang diagnostic ignored "-Wformat-invalid-specifier" |
| 1179 | #pragma clang diagnostic ignored "-Wformat-extra-args" |
| 1180 | |
| 1181 | wchar_t* p1 = nullptr; |
| 1182 | wchar_t* p2 = nullptr; |
| 1183 | ASSERT_EQ(2, sscanf("hello", "%mlc%mlc", &p1, &p2)); |
| 1184 | ASSERT_EQ(L'h', *p1); |
| 1185 | ASSERT_EQ(L'e', *p2); |
| 1186 | free(p1); |
| 1187 | free(p2); |
| 1188 | |
| 1189 | p1 = nullptr; |
| 1190 | ASSERT_EQ(1, sscanf("hello", "%4mlc", &p1)); |
| 1191 | ASSERT_EQ(L'h', p1[0]); |
| 1192 | ASSERT_EQ(L'e', p1[1]); |
| 1193 | ASSERT_EQ(L'l', p1[2]); |
| 1194 | ASSERT_EQ(L'l', p1[3]); |
| 1195 | free(p1); |
| 1196 | |
| 1197 | p1 = nullptr; |
| 1198 | ASSERT_EQ(1, sscanf("hello world", "%30mlc", &p1)); |
| 1199 | ASSERT_EQ(L'h', p1[0]); |
| 1200 | ASSERT_EQ(L'e', p1[1]); |
| 1201 | ASSERT_EQ(L'l', p1[2]); |
| 1202 | ASSERT_EQ(L'l', p1[3]); |
| 1203 | ASSERT_EQ(L'o', p1[4]); |
| 1204 | free(p1); |
| 1205 | #pragma clang diagnostic pop |
| 1206 | } |
| 1207 | |
Elliott Hughes | 38e4aef | 2018-01-18 10:21:29 -0800 | [diff] [blame] | 1208 | TEST(STDIO_TEST, sscanf_ms) { |
| 1209 | CheckScanfM(sscanf, "hello", "%ms", 1, "hello"); |
| 1210 | CheckScanfM(sscanf, "hello", "%4ms", 1, "hell"); |
| 1211 | CheckScanfM(sscanf, "hello world", "%30ms", 1, "hello"); |
| 1212 | } |
| 1213 | |
| 1214 | TEST(STDIO_TEST, sscanf_mls) { |
| 1215 | CheckScanfM(sscanf, "hello", "%mls", 1, L"hello"); |
| 1216 | CheckScanfM(sscanf, "hello", "%4mls", 1, L"hell"); |
| 1217 | CheckScanfM(sscanf, "hello world", "%30mls", 1, L"hello"); |
| 1218 | } |
| 1219 | |
| 1220 | TEST(STDIO_TEST, sscanf_m_ccl) { |
| 1221 | CheckScanfM(sscanf, "hello", "%m[a-z]", 1, "hello"); |
| 1222 | CheckScanfM(sscanf, "hello", "%4m[a-z]", 1, "hell"); |
| 1223 | CheckScanfM(sscanf, "hello world", "%30m[a-z]", 1, "hello"); |
| 1224 | } |
| 1225 | |
| 1226 | TEST(STDIO_TEST, sscanf_ml_ccl) { |
| 1227 | CheckScanfM(sscanf, "hello", "%ml[a-z]", 1, L"hello"); |
| 1228 | CheckScanfM(sscanf, "hello", "%4ml[a-z]", 1, L"hell"); |
| 1229 | CheckScanfM(sscanf, "hello world", "%30ml[a-z]", 1, L"hello"); |
| 1230 | } |
| 1231 | |
| 1232 | TEST(STDIO_TEST, sscanf_ls) { |
| 1233 | wchar_t w[32] = {}; |
| 1234 | ASSERT_EQ(1, sscanf("hello world", "%ls", w)); |
| 1235 | ASSERT_EQ(L"hello", std::wstring(w)); |
| 1236 | } |
| 1237 | |
| 1238 | TEST(STDIO_TEST, sscanf_ls_suppress) { |
| 1239 | ASSERT_EQ(0, sscanf("hello world", "%*ls %*ls")); |
| 1240 | } |
| 1241 | |
| 1242 | TEST(STDIO_TEST, sscanf_ls_n) { |
| 1243 | setlocale(LC_ALL, "C.UTF-8"); |
| 1244 | wchar_t w[32] = {}; |
| 1245 | int pos = 0; |
| 1246 | ASSERT_EQ(1, sscanf("\xc4\x80", "%ls%n", w, &pos)); |
| 1247 | ASSERT_EQ(static_cast<wchar_t>(256), w[0]); |
| 1248 | ASSERT_EQ(2, pos); |
| 1249 | } |
| 1250 | |
| 1251 | TEST(STDIO_TEST, sscanf_ls_realloc) { |
| 1252 | // This is so useless that clang doesn't even believe it exists... |
| 1253 | #pragma clang diagnostic push |
| 1254 | #pragma clang diagnostic ignored "-Wformat-invalid-specifier" |
| 1255 | #pragma clang diagnostic ignored "-Wformat-extra-args" |
| 1256 | wchar_t* p1 = nullptr; |
| 1257 | wchar_t* p2 = nullptr; |
| 1258 | ASSERT_EQ(2, sscanf("1234567890123456789012345678901234567890 world", "%mls %mls", &p1, &p2)); |
| 1259 | ASSERT_EQ(L"1234567890123456789012345678901234567890", std::wstring(p1)); |
| 1260 | ASSERT_EQ(L"world", std::wstring(p2)); |
| 1261 | #pragma clang diagnostic pop |
| 1262 | } |
| 1263 | |
Elliott Hughes | bf9cb9e | 2017-12-11 12:39:01 -0800 | [diff] [blame] | 1264 | // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240 |
| 1265 | TEST(STDIO_TEST, scanf_wscanf_EOF) { |
| 1266 | EXPECT_EQ(0, sscanf("b", "ab")); |
| 1267 | EXPECT_EQ(EOF, sscanf("", "a")); |
| 1268 | EXPECT_EQ(0, swscanf(L"b", L"ab")); |
| 1269 | EXPECT_EQ(EOF, swscanf(L"", L"a")); |
| 1270 | } |
| 1271 | |
| 1272 | TEST(STDIO_TEST, scanf_invalid_UTF8) { |
| 1273 | #if 0 // TODO: more tests invented during code review; no regressions, so fix later. |
| 1274 | char buf[BUFSIZ]; |
| 1275 | wchar_t wbuf[BUFSIZ]; |
| 1276 | |
| 1277 | memset(buf, 0, sizeof(buf)); |
| 1278 | memset(wbuf, 0, sizeof(wbuf)); |
| 1279 | EXPECT_EQ(0, sscanf("\xc0" " foo", "%ls %s", wbuf, buf)); |
| 1280 | #endif |
| 1281 | } |
| 1282 | |
| 1283 | TEST(STDIO_TEST, scanf_no_match_no_termination) { |
| 1284 | char buf[4] = "x"; |
| 1285 | EXPECT_EQ(0, sscanf("d", "%[abc]", buf)); |
| 1286 | EXPECT_EQ('x', buf[0]); |
| 1287 | EXPECT_EQ(0, swscanf(L"d", L"%[abc]", buf)); |
| 1288 | EXPECT_EQ('x', buf[0]); |
| 1289 | |
| 1290 | wchar_t wbuf[4] = L"x"; |
| 1291 | EXPECT_EQ(0, swscanf(L"d", L"%l[abc]", wbuf)); |
| 1292 | EXPECT_EQ(L'x', wbuf[0]); |
| 1293 | |
| 1294 | EXPECT_EQ(EOF, sscanf("", "%s", buf)); |
| 1295 | EXPECT_EQ('x', buf[0]); |
| 1296 | |
| 1297 | EXPECT_EQ(EOF, swscanf(L"", L"%ls", wbuf)); |
| 1298 | EXPECT_EQ(L'x', wbuf[0]); |
| 1299 | } |
| 1300 | |
| 1301 | TEST(STDIO_TEST, scanf_wscanf_wide_character_class) { |
| 1302 | #if 0 // TODO: more tests invented during code review; no regressions, so fix later. |
| 1303 | wchar_t buf[BUFSIZ]; |
| 1304 | |
| 1305 | // A wide character shouldn't match an ASCII-only class for scanf or wscanf. |
| 1306 | memset(buf, 0, sizeof(buf)); |
| 1307 | EXPECT_EQ(1, sscanf("xĀyz", "%l[xy]", buf)); |
| 1308 | EXPECT_EQ(L"x"s, std::wstring(buf)); |
| 1309 | memset(buf, 0, sizeof(buf)); |
| 1310 | EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xy]", buf)); |
| 1311 | EXPECT_EQ(L"x"s, std::wstring(buf)); |
| 1312 | |
| 1313 | // Even if scanf has wide characters in a class, they won't match... |
| 1314 | // TODO: is that a bug? |
| 1315 | memset(buf, 0, sizeof(buf)); |
| 1316 | EXPECT_EQ(1, sscanf("xĀyz", "%l[xĀy]", buf)); |
| 1317 | EXPECT_EQ(L"x"s, std::wstring(buf)); |
| 1318 | // ...unless you use wscanf. |
| 1319 | memset(buf, 0, sizeof(buf)); |
| 1320 | EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xĀy]", buf)); |
| 1321 | EXPECT_EQ(L"xĀy"s, std::wstring(buf)); |
| 1322 | |
| 1323 | // Negation only covers ASCII for scanf... |
| 1324 | memset(buf, 0, sizeof(buf)); |
| 1325 | EXPECT_EQ(1, sscanf("xĀyz", "%l[^ab]", buf)); |
| 1326 | EXPECT_EQ(L"x"s, std::wstring(buf)); |
| 1327 | // ...but covers wide characters for wscanf. |
| 1328 | memset(buf, 0, sizeof(buf)); |
| 1329 | EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[^ab]", buf)); |
| 1330 | EXPECT_EQ(L"xĀyz"s, std::wstring(buf)); |
| 1331 | |
| 1332 | // We already determined that non-ASCII characters are ignored in scanf classes. |
| 1333 | memset(buf, 0, sizeof(buf)); |
| 1334 | EXPECT_EQ(1, sscanf("x" |
| 1335 | "\xc4\x80" // Matches a byte from each wide char in the class. |
| 1336 | "\xc6\x82" // Neither byte is in the class. |
| 1337 | "yz", |
| 1338 | "%l[xy" "\xc5\x80" "\xc4\x81" "]", buf)); |
| 1339 | EXPECT_EQ(L"x", std::wstring(buf)); |
| 1340 | // bionic and glibc both behave badly for wscanf, so let's call it right for now... |
| 1341 | memset(buf, 0, sizeof(buf)); |
| 1342 | EXPECT_EQ(1, swscanf(L"x" |
| 1343 | L"\xc4\x80" |
| 1344 | L"\xc6\x82" |
| 1345 | L"yz", |
| 1346 | L"%l[xy" L"\xc5\x80" L"\xc4\x81" L"]", buf)); |
| 1347 | // Note that this isn't L"xĀ" --- although the *bytes* matched, they're |
| 1348 | // not put back together as a wide character. |
| 1349 | EXPECT_EQ(L"x" L"\xc4" L"\x80", std::wstring(buf)); |
| 1350 | #endif |
| 1351 | } |
| 1352 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1353 | TEST(STDIO_TEST, cantwrite_EBADF) { |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 1354 | // If we open a file read-only... |
| 1355 | FILE* fp = fopen("/proc/version", "r"); |
| 1356 | |
| 1357 | // ...all attempts to write to that file should return failure. |
| 1358 | |
| 1359 | // They should also set errno to EBADF. This isn't POSIX, but it's traditional. |
| 1360 | // glibc gets the wide-character functions wrong. |
| 1361 | |
| 1362 | errno = 0; |
| 1363 | EXPECT_EQ(EOF, putc('x', fp)); |
| 1364 | EXPECT_EQ(EBADF, errno); |
| 1365 | |
| 1366 | errno = 0; |
| 1367 | EXPECT_EQ(EOF, fprintf(fp, "hello")); |
| 1368 | EXPECT_EQ(EBADF, errno); |
| 1369 | |
| 1370 | errno = 0; |
| 1371 | EXPECT_EQ(EOF, fwprintf(fp, L"hello")); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 1372 | #if defined(__BIONIC__) |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 1373 | EXPECT_EQ(EBADF, errno); |
| 1374 | #endif |
| 1375 | |
| 1376 | errno = 0; |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 1377 | EXPECT_EQ(0U, fwrite("hello", 1, 2, fp)); |
| 1378 | EXPECT_EQ(EBADF, errno); |
| 1379 | |
| 1380 | errno = 0; |
| 1381 | EXPECT_EQ(EOF, fputs("hello", fp)); |
| 1382 | EXPECT_EQ(EBADF, errno); |
| 1383 | |
| 1384 | errno = 0; |
| 1385 | EXPECT_EQ(WEOF, fputwc(L'x', fp)); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 1386 | #if defined(__BIONIC__) |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 1387 | EXPECT_EQ(EBADF, errno); |
| 1388 | #endif |
| 1389 | } |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1390 | |
| 1391 | // Tests that we can only have a consistent and correct fpos_t when using |
| 1392 | // f*pos functions (i.e. fpos doesn't get inside a multi byte character). |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1393 | TEST(STDIO_TEST, consistent_fpos_t) { |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1394 | ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8")); |
| 1395 | uselocale(LC_GLOBAL_LOCALE); |
| 1396 | |
| 1397 | FILE* fp = tmpfile(); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1398 | ASSERT_TRUE(fp != nullptr); |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1399 | |
| 1400 | wchar_t mb_one_bytes = L'h'; |
| 1401 | wchar_t mb_two_bytes = 0x00a2; |
| 1402 | wchar_t mb_three_bytes = 0x20ac; |
| 1403 | wchar_t mb_four_bytes = 0x24b62; |
| 1404 | |
| 1405 | // Write to file. |
| 1406 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp))); |
| 1407 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp))); |
| 1408 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp))); |
| 1409 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp))); |
| 1410 | |
| 1411 | rewind(fp); |
| 1412 | |
| 1413 | // Record each character position. |
| 1414 | fpos_t pos1; |
| 1415 | fpos_t pos2; |
| 1416 | fpos_t pos3; |
| 1417 | fpos_t pos4; |
| 1418 | fpos_t pos5; |
| 1419 | EXPECT_EQ(0, fgetpos(fp, &pos1)); |
| 1420 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1421 | EXPECT_EQ(0, fgetpos(fp, &pos2)); |
| 1422 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1423 | EXPECT_EQ(0, fgetpos(fp, &pos3)); |
| 1424 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1425 | EXPECT_EQ(0, fgetpos(fp, &pos4)); |
| 1426 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1427 | EXPECT_EQ(0, fgetpos(fp, &pos5)); |
| 1428 | |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 1429 | #if defined(__BIONIC__) |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1430 | // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD |
| 1431 | // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In |
| 1432 | // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE |
| 1433 | // structure. |
| 1434 | ASSERT_EQ(0, static_cast<off_t>(pos1)); |
| 1435 | ASSERT_EQ(1, static_cast<off_t>(pos2)); |
| 1436 | ASSERT_EQ(3, static_cast<off_t>(pos3)); |
| 1437 | ASSERT_EQ(6, static_cast<off_t>(pos4)); |
| 1438 | ASSERT_EQ(10, static_cast<off_t>(pos5)); |
| 1439 | #endif |
| 1440 | |
| 1441 | // Exercise back and forth movements of the position. |
| 1442 | ASSERT_EQ(0, fsetpos(fp, &pos2)); |
| 1443 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1444 | ASSERT_EQ(0, fsetpos(fp, &pos1)); |
| 1445 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1446 | ASSERT_EQ(0, fsetpos(fp, &pos4)); |
| 1447 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1448 | ASSERT_EQ(0, fsetpos(fp, &pos3)); |
| 1449 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1450 | ASSERT_EQ(0, fsetpos(fp, &pos5)); |
| 1451 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 1452 | |
| 1453 | fclose(fp); |
| 1454 | } |
| 1455 | |
| 1456 | // Exercise the interaction between fpos and seek. |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1457 | TEST(STDIO_TEST, fpos_t_and_seek) { |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1458 | ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8")); |
| 1459 | uselocale(LC_GLOBAL_LOCALE); |
| 1460 | |
Calin Juravle | 9b95ea9 | 2014-05-14 17:07:10 +0100 | [diff] [blame] | 1461 | // In glibc-2.16 fseek doesn't work properly in wide mode |
| 1462 | // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is |
| 1463 | // to close and re-open the file. We do it in order to make the test pass |
| 1464 | // with all glibcs. |
| 1465 | |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1466 | TemporaryFile tf; |
| 1467 | FILE* fp = fdopen(tf.fd, "w+"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1468 | ASSERT_TRUE(fp != nullptr); |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1469 | |
| 1470 | wchar_t mb_two_bytes = 0x00a2; |
| 1471 | wchar_t mb_three_bytes = 0x20ac; |
| 1472 | wchar_t mb_four_bytes = 0x24b62; |
| 1473 | |
| 1474 | // Write to file. |
| 1475 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp))); |
| 1476 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp))); |
| 1477 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp))); |
| 1478 | |
| 1479 | fflush(fp); |
| 1480 | fclose(fp); |
| 1481 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 1482 | fp = fopen(tf.path, "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 1483 | ASSERT_TRUE(fp != nullptr); |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1484 | |
| 1485 | // Store a valid position. |
| 1486 | fpos_t mb_two_bytes_pos; |
| 1487 | ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos)); |
| 1488 | |
| 1489 | // Move inside mb_four_bytes with fseek. |
| 1490 | long offset_inside_mb = 6; |
| 1491 | ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET)); |
| 1492 | |
| 1493 | // Store the "inside multi byte" position. |
| 1494 | fpos_t pos_inside_mb; |
| 1495 | ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb)); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 1496 | #if defined(__BIONIC__) |
| 1497 | ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb)); |
| 1498 | #endif |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1499 | |
| 1500 | // Reading from within a byte should produce an error. |
| 1501 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 1502 | ASSERT_EQ(EILSEQ, errno); |
| 1503 | |
| 1504 | // Reverting to a valid position should work. |
| 1505 | ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos)); |
| 1506 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1507 | |
| 1508 | // Moving withing a multi byte with fsetpos should work but reading should |
| 1509 | // produce an error. |
| 1510 | ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb)); |
| 1511 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 1512 | ASSERT_EQ(EILSEQ, errno); |
| 1513 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1514 | ASSERT_EQ(0, fclose(fp)); |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1515 | } |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1516 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1517 | TEST(STDIO_TEST, fmemopen) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1518 | char buf[16]; |
| 1519 | memset(buf, 0, sizeof(buf)); |
| 1520 | FILE* fp = fmemopen(buf, sizeof(buf), "r+"); |
| 1521 | ASSERT_EQ('<', fputc('<', fp)); |
| 1522 | ASSERT_NE(EOF, fputs("abc>\n", fp)); |
| 1523 | fflush(fp); |
| 1524 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1525 | // We wrote to the buffer... |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1526 | ASSERT_STREQ("<abc>\n", buf); |
| 1527 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1528 | // And can read back from the file. |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 1529 | AssertFileIs(fp, "<abc>\n", true); |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1530 | ASSERT_EQ(0, fclose(fp)); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1533 | TEST(STDIO_TEST, fmemopen_nullptr) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1534 | FILE* fp = fmemopen(nullptr, 128, "r+"); |
| 1535 | ASSERT_NE(EOF, fputs("xyz\n", fp)); |
| 1536 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 1537 | AssertFileIs(fp, "xyz\n", true); |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1538 | ASSERT_EQ(0, fclose(fp)); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1541 | TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) { |
| 1542 | FILE* fp; |
| 1543 | char buf[8]; |
| 1544 | |
| 1545 | // POSIX: "When a stream open for writing is flushed or closed, a null byte |
| 1546 | // shall be written at the current position or at the end of the buffer, |
| 1547 | // depending on the size of the contents." |
| 1548 | memset(buf, 'x', sizeof(buf)); |
| 1549 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w")); |
| 1550 | // Even with nothing written (and not in truncate mode), we'll flush a NUL... |
| 1551 | ASSERT_EQ(0, fflush(fp)); |
| 1552 | EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf))); |
| 1553 | // Now write and check that the NUL moves along with our writes... |
| 1554 | ASSERT_NE(EOF, fputs("hello", fp)); |
| 1555 | ASSERT_EQ(0, fflush(fp)); |
| 1556 | EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf))); |
| 1557 | ASSERT_NE(EOF, fputs("wo", fp)); |
| 1558 | ASSERT_EQ(0, fflush(fp)); |
| 1559 | EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf))); |
| 1560 | ASSERT_EQ(0, fclose(fp)); |
| 1561 | |
| 1562 | // "If a stream open for update is flushed or closed and the last write has |
| 1563 | // advanced the current buffer size, a null byte shall be written at the end |
| 1564 | // of the buffer if it fits." |
| 1565 | memset(buf, 'x', sizeof(buf)); |
| 1566 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+")); |
| 1567 | // Nothing written yet, so no advance... |
| 1568 | ASSERT_EQ(0, fflush(fp)); |
| 1569 | EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf))); |
| 1570 | ASSERT_NE(EOF, fputs("hello", fp)); |
| 1571 | ASSERT_EQ(0, fclose(fp)); |
| 1572 | } |
| 1573 | |
| 1574 | TEST(STDIO_TEST, fmemopen_size) { |
| 1575 | FILE* fp; |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1576 | char buf[16]; |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1577 | memset(buf, 'x', sizeof(buf)); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1578 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1579 | // POSIX: "The stream shall also maintain the size of the current buffer |
| 1580 | // contents; use of fseek() or fseeko() on the stream with SEEK_END shall |
| 1581 | // seek relative to this size." |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1582 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1583 | // "For modes r and r+ the size shall be set to the value given by the size |
| 1584 | // argument." |
| 1585 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r")); |
| 1586 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1587 | EXPECT_EQ(16, ftell(fp)); |
| 1588 | EXPECT_EQ(16, ftello(fp)); |
| 1589 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1590 | EXPECT_EQ(16, ftell(fp)); |
| 1591 | EXPECT_EQ(16, ftello(fp)); |
| 1592 | ASSERT_EQ(0, fclose(fp)); |
| 1593 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+")); |
| 1594 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1595 | EXPECT_EQ(16, ftell(fp)); |
| 1596 | EXPECT_EQ(16, ftello(fp)); |
| 1597 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1598 | EXPECT_EQ(16, ftell(fp)); |
| 1599 | EXPECT_EQ(16, ftello(fp)); |
| 1600 | ASSERT_EQ(0, fclose(fp)); |
| 1601 | |
| 1602 | // "For modes w and w+ the initial size shall be zero..." |
| 1603 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w")); |
| 1604 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1605 | EXPECT_EQ(0, ftell(fp)); |
| 1606 | EXPECT_EQ(0, ftello(fp)); |
| 1607 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1608 | EXPECT_EQ(0, ftell(fp)); |
| 1609 | EXPECT_EQ(0, ftello(fp)); |
| 1610 | ASSERT_EQ(0, fclose(fp)); |
| 1611 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+")); |
| 1612 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1613 | EXPECT_EQ(0, ftell(fp)); |
| 1614 | EXPECT_EQ(0, ftello(fp)); |
| 1615 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1616 | EXPECT_EQ(0, ftell(fp)); |
| 1617 | EXPECT_EQ(0, ftello(fp)); |
| 1618 | ASSERT_EQ(0, fclose(fp)); |
| 1619 | |
| 1620 | // "...and for modes a and a+ the initial size shall be: |
| 1621 | // 1. Zero, if buf is a null pointer |
| 1622 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a")); |
| 1623 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1624 | EXPECT_EQ(0, ftell(fp)); |
| 1625 | EXPECT_EQ(0, ftello(fp)); |
| 1626 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1627 | EXPECT_EQ(0, ftell(fp)); |
| 1628 | EXPECT_EQ(0, ftello(fp)); |
| 1629 | ASSERT_EQ(0, fclose(fp)); |
| 1630 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+")); |
| 1631 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1632 | EXPECT_EQ(0, ftell(fp)); |
| 1633 | EXPECT_EQ(0, ftello(fp)); |
| 1634 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1635 | EXPECT_EQ(0, ftell(fp)); |
| 1636 | EXPECT_EQ(0, ftello(fp)); |
| 1637 | ASSERT_EQ(0, fclose(fp)); |
| 1638 | |
| 1639 | // 2. The position of the first null byte in the buffer, if one is found |
| 1640 | memset(buf, 'x', sizeof(buf)); |
| 1641 | buf[3] = '\0'; |
| 1642 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a")); |
| 1643 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1644 | EXPECT_EQ(3, ftell(fp)); |
| 1645 | EXPECT_EQ(3, ftello(fp)); |
| 1646 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1647 | EXPECT_EQ(3, ftell(fp)); |
| 1648 | EXPECT_EQ(3, ftello(fp)); |
| 1649 | ASSERT_EQ(0, fclose(fp)); |
| 1650 | memset(buf, 'x', sizeof(buf)); |
| 1651 | buf[3] = '\0'; |
| 1652 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+")); |
| 1653 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1654 | EXPECT_EQ(3, ftell(fp)); |
| 1655 | EXPECT_EQ(3, ftello(fp)); |
| 1656 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1657 | EXPECT_EQ(3, ftell(fp)); |
| 1658 | EXPECT_EQ(3, ftello(fp)); |
| 1659 | ASSERT_EQ(0, fclose(fp)); |
| 1660 | |
| 1661 | // 3. The value of the size argument, if buf is not a null pointer and no |
| 1662 | // null byte is found. |
| 1663 | memset(buf, 'x', sizeof(buf)); |
| 1664 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a")); |
| 1665 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1666 | EXPECT_EQ(16, ftell(fp)); |
| 1667 | EXPECT_EQ(16, ftello(fp)); |
| 1668 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1669 | EXPECT_EQ(16, ftell(fp)); |
| 1670 | EXPECT_EQ(16, ftello(fp)); |
| 1671 | ASSERT_EQ(0, fclose(fp)); |
| 1672 | memset(buf, 'x', sizeof(buf)); |
| 1673 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+")); |
| 1674 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1675 | EXPECT_EQ(16, ftell(fp)); |
| 1676 | EXPECT_EQ(16, ftello(fp)); |
| 1677 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1678 | EXPECT_EQ(16, ftell(fp)); |
| 1679 | EXPECT_EQ(16, ftello(fp)); |
| 1680 | ASSERT_EQ(0, fclose(fp)); |
| 1681 | } |
| 1682 | |
| 1683 | TEST(STDIO_TEST, fmemopen_SEEK_END) { |
| 1684 | // fseek SEEK_END is relative to the current string length, not the buffer size. |
| 1685 | FILE* fp; |
| 1686 | char buf[8]; |
| 1687 | memset(buf, 'x', sizeof(buf)); |
| 1688 | strcpy(buf, "str"); |
| 1689 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+")); |
| 1690 | ASSERT_NE(EOF, fputs("string", fp)); |
| 1691 | EXPECT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1692 | EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp)); |
| 1693 | EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp)); |
| 1694 | EXPECT_EQ(0, fclose(fp)); |
| 1695 | |
| 1696 | // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather |
| 1697 | // than adding). |
| 1698 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+")); |
| 1699 | ASSERT_NE(EOF, fputs("54321", fp)); |
| 1700 | EXPECT_EQ(0, fseek(fp, -2, SEEK_END)); |
| 1701 | EXPECT_EQ('2', fgetc(fp)); |
| 1702 | EXPECT_EQ(0, fclose(fp)); |
| 1703 | } |
| 1704 | |
| 1705 | TEST(STDIO_TEST, fmemopen_seek_invalid) { |
| 1706 | char buf[8]; |
| 1707 | memset(buf, 'x', sizeof(buf)); |
| 1708 | FILE* fp = fmemopen(buf, sizeof(buf), "w"); |
| 1709 | ASSERT_TRUE(fp != nullptr); |
| 1710 | |
| 1711 | // POSIX: "An attempt to seek ... to a negative position or to a position |
| 1712 | // larger than the buffer size given in the size argument shall fail." |
| 1713 | // (There's no mention of what errno should be set to, and glibc doesn't |
| 1714 | // set errno in any of these cases.) |
| 1715 | EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET)); |
| 1716 | EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET)); |
| 1717 | EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET)); |
| 1718 | EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET)); |
| 1719 | } |
| 1720 | |
| 1721 | TEST(STDIO_TEST, fmemopen_read_EOF) { |
| 1722 | // POSIX: "A read operation on the stream shall not advance the current |
| 1723 | // buffer position beyond the current buffer size." |
| 1724 | char buf[8]; |
| 1725 | memset(buf, 'x', sizeof(buf)); |
| 1726 | FILE* fp = fmemopen(buf, sizeof(buf), "r"); |
| 1727 | ASSERT_TRUE(fp != nullptr); |
| 1728 | char buf2[BUFSIZ]; |
| 1729 | ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp)); |
| 1730 | // POSIX: "Reaching the buffer size in a read operation shall count as |
| 1731 | // end-of-file. |
| 1732 | ASSERT_TRUE(feof(fp)); |
| 1733 | ASSERT_EQ(EOF, fgetc(fp)); |
| 1734 | ASSERT_EQ(0, fclose(fp)); |
| 1735 | } |
| 1736 | |
| 1737 | TEST(STDIO_TEST, fmemopen_read_null_bytes) { |
| 1738 | // POSIX: "Null bytes in the buffer shall have no special meaning for reads." |
| 1739 | char buf[] = "h\0e\0l\0l\0o"; |
| 1740 | FILE* fp = fmemopen(buf, sizeof(buf), "r"); |
| 1741 | ASSERT_TRUE(fp != nullptr); |
| 1742 | ASSERT_EQ('h', fgetc(fp)); |
| 1743 | ASSERT_EQ(0, fgetc(fp)); |
| 1744 | ASSERT_EQ('e', fgetc(fp)); |
| 1745 | ASSERT_EQ(0, fgetc(fp)); |
| 1746 | ASSERT_EQ('l', fgetc(fp)); |
| 1747 | ASSERT_EQ(0, fgetc(fp)); |
| 1748 | // POSIX: "The read operation shall start at the current buffer position of |
| 1749 | // the stream." |
| 1750 | char buf2[8]; |
| 1751 | memset(buf2, 'x', sizeof(buf2)); |
| 1752 | ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp)); |
| 1753 | ASSERT_EQ('l', buf2[0]); |
| 1754 | ASSERT_EQ(0, buf2[1]); |
| 1755 | ASSERT_EQ('o', buf2[2]); |
| 1756 | ASSERT_EQ(0, buf2[3]); |
| 1757 | for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i; |
| 1758 | ASSERT_TRUE(feof(fp)); |
| 1759 | ASSERT_EQ(0, fclose(fp)); |
| 1760 | } |
| 1761 | |
| 1762 | TEST(STDIO_TEST, fmemopen_write) { |
| 1763 | FILE* fp; |
| 1764 | char buf[8]; |
| 1765 | |
| 1766 | // POSIX: "A write operation shall start either at the current position of |
| 1767 | // the stream (if mode has not specified 'a' as the first character)..." |
| 1768 | memset(buf, 'x', sizeof(buf)); |
| 1769 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+")); |
| 1770 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1771 | ASSERT_EQ(0, fseek(fp, 2, SEEK_SET)); |
| 1772 | ASSERT_EQ(' ', fputc(' ', fp)); |
| 1773 | EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf))); |
| 1774 | ASSERT_EQ(0, fclose(fp)); |
| 1775 | |
| 1776 | // "...or at the current size of the stream (if mode had 'a' as the first |
| 1777 | // character)." (See the fmemopen_size test for what "size" means, but for |
| 1778 | // mode "a", it's the first NUL byte.) |
| 1779 | memset(buf, 'x', sizeof(buf)); |
| 1780 | buf[3] = '\0'; |
| 1781 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+")); |
| 1782 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1783 | ASSERT_EQ(' ', fputc(' ', fp)); |
| 1784 | EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf))); |
| 1785 | ASSERT_EQ(0, fclose(fp)); |
| 1786 | |
| 1787 | // "If the current position at the end of the write is larger than the |
| 1788 | // current buffer size, the current buffer size shall be set to the current |
| 1789 | // position." (See the fmemopen_size test for what "size" means, but to |
| 1790 | // query it we SEEK_END with offset 0, and then ftell.) |
| 1791 | memset(buf, 'x', sizeof(buf)); |
| 1792 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+")); |
| 1793 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1794 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1795 | EXPECT_EQ(0, ftell(fp)); |
| 1796 | ASSERT_EQ(' ', fputc(' ', fp)); |
| 1797 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1798 | EXPECT_EQ(1, ftell(fp)); |
| 1799 | ASSERT_NE(EOF, fputs("123", fp)); |
| 1800 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1801 | EXPECT_EQ(4, ftell(fp)); |
| 1802 | EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf))); |
| 1803 | ASSERT_EQ(0, fclose(fp)); |
| 1804 | } |
| 1805 | |
| 1806 | TEST(STDIO_TEST, fmemopen_write_EOF) { |
| 1807 | // POSIX: "A write operation on the stream shall not advance the current |
| 1808 | // buffer size beyond the size given in the size argument." |
| 1809 | FILE* fp; |
| 1810 | |
| 1811 | // Scalar writes... |
| 1812 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w")); |
| 1813 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1814 | ASSERT_EQ('x', fputc('x', fp)); |
| 1815 | ASSERT_EQ('x', fputc('x', fp)); |
| 1816 | ASSERT_EQ('x', fputc('x', fp)); |
| 1817 | ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL. |
| 1818 | ASSERT_EQ(0, fclose(fp)); |
| 1819 | |
| 1820 | // Vector writes... |
| 1821 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w")); |
| 1822 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1823 | ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp)); |
| 1824 | ASSERT_EQ(0, fclose(fp)); |
| 1825 | } |
| 1826 | |
| 1827 | TEST(STDIO_TEST, fmemopen_initial_position) { |
| 1828 | // POSIX: "The ... current position in the buffer ... shall be initially |
| 1829 | // set to either the beginning of the buffer (for r and w modes) ..." |
| 1830 | char buf[] = "hello\0world"; |
| 1831 | FILE* fp; |
| 1832 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r")); |
| 1833 | EXPECT_EQ(0L, ftell(fp)); |
| 1834 | EXPECT_EQ(0, fclose(fp)); |
| 1835 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w")); |
| 1836 | EXPECT_EQ(0L, ftell(fp)); |
| 1837 | EXPECT_EQ(0, fclose(fp)); |
| 1838 | buf[0] = 'h'; // (Undo the effects of the above.) |
| 1839 | |
| 1840 | // POSIX: "...or to the first null byte in the buffer (for a modes)." |
| 1841 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a")); |
| 1842 | EXPECT_EQ(5L, ftell(fp)); |
| 1843 | EXPECT_EQ(0, fclose(fp)); |
| 1844 | |
| 1845 | // POSIX: "If no null byte is found in append mode, the initial position |
| 1846 | // shall be set to one byte after the end of the buffer." |
| 1847 | memset(buf, 'x', sizeof(buf)); |
| 1848 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a")); |
| 1849 | EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp)); |
| 1850 | EXPECT_EQ(0, fclose(fp)); |
| 1851 | } |
| 1852 | |
| 1853 | TEST(STDIO_TEST, fmemopen_initial_position_allocated) { |
| 1854 | // POSIX: "If buf is a null pointer, the initial position shall always be |
| 1855 | // set to the beginning of the buffer." |
| 1856 | FILE* fp = fmemopen(nullptr, 128, "a+"); |
| 1857 | ASSERT_TRUE(fp != nullptr); |
| 1858 | EXPECT_EQ(0L, ftell(fp)); |
| 1859 | EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET)); |
| 1860 | EXPECT_EQ(0, fclose(fp)); |
| 1861 | } |
| 1862 | |
| 1863 | TEST(STDIO_TEST, fmemopen_zero_length) { |
| 1864 | // POSIX says it's up to the implementation whether or not you can have a |
| 1865 | // zero-length buffer (but "A future version of this standard may require |
| 1866 | // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22 |
| 1867 | // agreed that you couldn't, but glibc >= 2.22 allows it for consistency. |
| 1868 | FILE* fp; |
| 1869 | char buf[16]; |
| 1870 | ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+")); |
| 1871 | ASSERT_EQ(EOF, fgetc(fp)); |
| 1872 | ASSERT_TRUE(feof(fp)); |
| 1873 | ASSERT_EQ(0, fclose(fp)); |
| 1874 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+")); |
| 1875 | ASSERT_EQ(EOF, fgetc(fp)); |
| 1876 | ASSERT_TRUE(feof(fp)); |
| 1877 | ASSERT_EQ(0, fclose(fp)); |
| 1878 | |
| 1879 | ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+")); |
| 1880 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1881 | ASSERT_EQ(EOF, fputc('x', fp)); |
| 1882 | ASSERT_EQ(0, fclose(fp)); |
| 1883 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+")); |
| 1884 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1885 | ASSERT_EQ(EOF, fputc('x', fp)); |
| 1886 | ASSERT_EQ(0, fclose(fp)); |
| 1887 | } |
| 1888 | |
Elliott Hughes | 288465d | 2019-02-05 15:00:13 -0800 | [diff] [blame] | 1889 | TEST(STDIO_TEST, fmemopen_zero_length_buffer_overrun) { |
| 1890 | char buf[2] = "x"; |
| 1891 | ASSERT_EQ('x', buf[0]); |
| 1892 | FILE* fp = fmemopen(buf, 0, "w"); |
| 1893 | ASSERT_EQ('x', buf[0]); |
| 1894 | ASSERT_EQ(0, fclose(fp)); |
| 1895 | } |
| 1896 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1897 | TEST(STDIO_TEST, fmemopen_write_only_allocated) { |
| 1898 | // POSIX says fmemopen "may fail if the mode argument does not include a '+'". |
| 1899 | // BSD fails, glibc doesn't. We side with the more lenient. |
| 1900 | FILE* fp; |
| 1901 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r")); |
| 1902 | ASSERT_EQ(0, fclose(fp)); |
| 1903 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w")); |
| 1904 | ASSERT_EQ(0, fclose(fp)); |
| 1905 | } |
| 1906 | |
| 1907 | TEST(STDIO_TEST, fmemopen_fileno) { |
| 1908 | // There's no fd backing an fmemopen FILE*. |
| 1909 | FILE* fp = fmemopen(nullptr, 16, "r"); |
| 1910 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1911 | errno = 0; |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1912 | ASSERT_EQ(-1, fileno(fp)); |
| 1913 | ASSERT_EQ(EBADF, errno); |
| 1914 | ASSERT_EQ(0, fclose(fp)); |
| 1915 | } |
| 1916 | |
| 1917 | TEST(STDIO_TEST, fmemopen_append_after_seek) { |
| 1918 | // In BSD and glibc < 2.22, append mode didn't force writes to append if |
| 1919 | // there had been an intervening seek. |
| 1920 | |
| 1921 | FILE* fp; |
| 1922 | char buf[] = "hello\0world"; |
| 1923 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a")); |
| 1924 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1925 | ASSERT_EQ(0, fseek(fp, 0, SEEK_SET)); |
| 1926 | ASSERT_NE(EOF, fputc('!', fp)); |
| 1927 | EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf))); |
| 1928 | ASSERT_EQ(0, fclose(fp)); |
| 1929 | |
| 1930 | memcpy(buf, "hello\0world", sizeof(buf)); |
| 1931 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+")); |
| 1932 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1933 | ASSERT_EQ(0, fseek(fp, 0, SEEK_SET)); |
| 1934 | ASSERT_NE(EOF, fputc('!', fp)); |
| 1935 | EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf))); |
| 1936 | ASSERT_EQ(0, fclose(fp)); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1937 | } |
| 1938 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1939 | TEST(STDIO_TEST, open_memstream) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1940 | char* p = nullptr; |
| 1941 | size_t size = 0; |
| 1942 | FILE* fp = open_memstream(&p, &size); |
| 1943 | ASSERT_NE(EOF, fputs("hello, world!", fp)); |
| 1944 | fclose(fp); |
| 1945 | |
| 1946 | ASSERT_STREQ("hello, world!", p); |
| 1947 | ASSERT_EQ(strlen("hello, world!"), size); |
| 1948 | free(p); |
| 1949 | } |
| 1950 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1951 | TEST(STDIO_TEST, open_memstream_EINVAL) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1952 | #if defined(__BIONIC__) |
zijunzhao | 00a3dba | 2023-03-01 00:07:40 +0000 | [diff] [blame] | 1953 | #pragma clang diagnostic push |
| 1954 | #pragma clang diagnostic ignored "-Wnonnull" |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1955 | char* p; |
| 1956 | size_t size; |
| 1957 | |
| 1958 | // Invalid buffer. |
| 1959 | errno = 0; |
| 1960 | ASSERT_EQ(nullptr, open_memstream(nullptr, &size)); |
| 1961 | ASSERT_EQ(EINVAL, errno); |
| 1962 | |
| 1963 | // Invalid size. |
| 1964 | errno = 0; |
| 1965 | ASSERT_EQ(nullptr, open_memstream(&p, nullptr)); |
| 1966 | ASSERT_EQ(EINVAL, errno); |
zijunzhao | 00a3dba | 2023-03-01 00:07:40 +0000 | [diff] [blame] | 1967 | #pragma clang diagnostic pop |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1968 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 1969 | GTEST_SKIP() << "glibc is broken"; |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1970 | #endif |
| 1971 | } |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 1972 | |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 1973 | TEST(STDIO_TEST, fdopen_add_CLOEXEC) { |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 1974 | // This fd doesn't have O_CLOEXEC... |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 1975 | int fd = open("/proc/version", O_RDONLY); |
| 1976 | ASSERT_FALSE(CloseOnExec(fd)); |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 1977 | // ...but the new one does. |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 1978 | FILE* fp = fdopen(fd, "re"); |
| 1979 | ASSERT_TRUE(CloseOnExec(fileno(fp))); |
| 1980 | fclose(fp); |
| 1981 | } |
| 1982 | |
| 1983 | TEST(STDIO_TEST, fdopen_remove_CLOEXEC) { |
| 1984 | // This fd has O_CLOEXEC... |
| 1985 | int fd = open("/proc/version", O_RDONLY | O_CLOEXEC); |
| 1986 | ASSERT_TRUE(CloseOnExec(fd)); |
| 1987 | // ...but the new one doesn't. |
| 1988 | FILE* fp = fdopen(fd, "r"); |
| 1989 | ASSERT_TRUE(CloseOnExec(fileno(fp))); |
| 1990 | fclose(fp); |
| 1991 | } |
| 1992 | |
| 1993 | TEST(STDIO_TEST, freopen_add_CLOEXEC) { |
| 1994 | // This FILE* doesn't have O_CLOEXEC... |
| 1995 | FILE* fp = fopen("/proc/version", "r"); |
| 1996 | ASSERT_FALSE(CloseOnExec(fileno(fp))); |
| 1997 | // ...but the new one does. |
| 1998 | fp = freopen("/proc/version", "re", fp); |
| 1999 | ASSERT_TRUE(CloseOnExec(fileno(fp))); |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 2000 | |
| 2001 | fclose(fp); |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 2002 | } |
| 2003 | |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 2004 | TEST(STDIO_TEST, freopen_remove_CLOEXEC) { |
| 2005 | // This FILE* has O_CLOEXEC... |
| 2006 | FILE* fp = fopen("/proc/version", "re"); |
| 2007 | ASSERT_TRUE(CloseOnExec(fileno(fp))); |
| 2008 | // ...but the new one doesn't. |
| 2009 | fp = freopen("/proc/version", "r", fp); |
| 2010 | ASSERT_FALSE(CloseOnExec(fileno(fp))); |
| 2011 | fclose(fp); |
| 2012 | } |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 2013 | |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 2014 | TEST(STDIO_TEST, freopen_null_filename_add_CLOEXEC) { |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 2015 | // This FILE* doesn't have O_CLOEXEC... |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 2016 | FILE* fp = fopen("/proc/version", "r"); |
| 2017 | ASSERT_FALSE(CloseOnExec(fileno(fp))); |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 2018 | // ...but the new one does. |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 2019 | fp = freopen(nullptr, "re", fp); |
| 2020 | ASSERT_TRUE(CloseOnExec(fileno(fp))); |
| 2021 | fclose(fp); |
| 2022 | } |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 2023 | |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 2024 | TEST(STDIO_TEST, freopen_null_filename_remove_CLOEXEC) { |
| 2025 | // This FILE* has O_CLOEXEC... |
| 2026 | FILE* fp = fopen("/proc/version", "re"); |
| 2027 | ASSERT_TRUE(CloseOnExec(fileno(fp))); |
| 2028 | // ...but the new one doesn't. |
| 2029 | fp = freopen(nullptr, "r", fp); |
| 2030 | ASSERT_FALSE(CloseOnExec(fileno(fp))); |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 2031 | fclose(fp); |
| 2032 | } |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 2033 | |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 2034 | TEST(STDIO_TEST, fopen64_freopen64) { |
| 2035 | FILE* fp = fopen64("/proc/version", "r"); |
| 2036 | ASSERT_TRUE(fp != nullptr); |
| 2037 | fp = freopen64("/proc/version", "re", fp); |
| 2038 | ASSERT_TRUE(fp != nullptr); |
| 2039 | fclose(fp); |
| 2040 | } |
| 2041 | |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 2042 | // https://code.google.com/p/android/issues/detail?id=81155 |
| 2043 | // http://b/18556607 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 2044 | TEST(STDIO_TEST, fread_unbuffered_pathological_performance) { |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 2045 | FILE* fp = fopen("/dev/zero", "r"); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2046 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 2047 | |
| 2048 | // Make this stream unbuffered. |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2049 | setvbuf(fp, nullptr, _IONBF, 0); |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 2050 | |
| 2051 | char buf[65*1024]; |
| 2052 | memset(buf, 0xff, sizeof(buf)); |
| 2053 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2054 | time_t t0 = time(nullptr); |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 2055 | for (size_t i = 0; i < 1024; ++i) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 2056 | ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp)); |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 2057 | } |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 2058 | time_t t1 = time(nullptr); |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 2059 | |
| 2060 | fclose(fp); |
| 2061 | |
| 2062 | // 1024 64KiB reads should have been very quick. |
| 2063 | ASSERT_LE(t1 - t0, 1); |
| 2064 | |
| 2065 | for (size_t i = 0; i < 64*1024; ++i) { |
| 2066 | ASSERT_EQ('\0', buf[i]); |
| 2067 | } |
| 2068 | for (size_t i = 64*1024; i < 65*1024; ++i) { |
| 2069 | ASSERT_EQ('\xff', buf[i]); |
| 2070 | } |
| 2071 | } |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 2072 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 2073 | TEST(STDIO_TEST, fread_EOF) { |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 2074 | std::string digits("0123456789"); |
| 2075 | FILE* fp = fmemopen(&digits[0], digits.size(), "r"); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 2076 | |
| 2077 | // Try to read too much, but little enough that it still fits in the FILE's internal buffer. |
| 2078 | char buf1[4 * 4]; |
| 2079 | memset(buf1, 0, sizeof(buf1)); |
| 2080 | ASSERT_EQ(2U, fread(buf1, 4, 4, fp)); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 2081 | ASSERT_STREQ("0123456789", buf1); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 2082 | ASSERT_TRUE(feof(fp)); |
| 2083 | |
| 2084 | rewind(fp); |
| 2085 | |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 2086 | // Try to read way too much so stdio tries to read more direct from the stream. |
| 2087 | char buf2[4 * 4096]; |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 2088 | memset(buf2, 0, sizeof(buf2)); |
| 2089 | ASSERT_EQ(2U, fread(buf2, 4, 4096, fp)); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 2090 | ASSERT_STREQ("0123456789", buf2); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 2091 | ASSERT_TRUE(feof(fp)); |
| 2092 | |
| 2093 | fclose(fp); |
| 2094 | } |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 2095 | |
| 2096 | static void test_fread_from_write_only_stream(size_t n) { |
| 2097 | FILE* fp = fopen("/dev/null", "w"); |
| 2098 | std::vector<char> buf(n, 0); |
| 2099 | errno = 0; |
| 2100 | ASSERT_EQ(0U, fread(&buf[0], n, 1, fp)); |
| 2101 | ASSERT_EQ(EBADF, errno); |
| 2102 | ASSERT_TRUE(ferror(fp)); |
| 2103 | ASSERT_FALSE(feof(fp)); |
| 2104 | fclose(fp); |
| 2105 | } |
| 2106 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 2107 | TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 2108 | test_fread_from_write_only_stream(1); |
| 2109 | } |
| 2110 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 2111 | TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 2112 | test_fread_from_write_only_stream(64*1024); |
| 2113 | } |
| 2114 | |
| 2115 | static void test_fwrite_after_fread(size_t n) { |
| 2116 | TemporaryFile tf; |
| 2117 | |
| 2118 | FILE* fp = fdopen(tf.fd, "w+"); |
| 2119 | ASSERT_EQ(1U, fwrite("1", 1, 1, fp)); |
| 2120 | fflush(fp); |
| 2121 | |
| 2122 | // We've flushed but not rewound, so there's nothing to read. |
| 2123 | std::vector<char> buf(n, 0); |
| 2124 | ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp)); |
| 2125 | ASSERT_TRUE(feof(fp)); |
| 2126 | |
| 2127 | // But hitting EOF doesn't prevent us from writing... |
| 2128 | errno = 0; |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 2129 | ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno); |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 2130 | |
| 2131 | // And if we rewind, everything's there. |
| 2132 | rewind(fp); |
| 2133 | ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp)); |
| 2134 | ASSERT_EQ('1', buf[0]); |
| 2135 | ASSERT_EQ('2', buf[1]); |
| 2136 | |
| 2137 | fclose(fp); |
| 2138 | } |
| 2139 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 2140 | TEST(STDIO_TEST, fwrite_after_fread_slow_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 2141 | test_fwrite_after_fread(16); |
| 2142 | } |
| 2143 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 2144 | TEST(STDIO_TEST, fwrite_after_fread_fast_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 2145 | test_fwrite_after_fread(64*1024); |
| 2146 | } |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 2147 | |
| 2148 | // http://b/19172514 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 2149 | TEST(STDIO_TEST, fread_after_fseek) { |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 2150 | TemporaryFile tf; |
| 2151 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2152 | FILE* fp = fopen(tf.path, "w+"); |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 2153 | ASSERT_TRUE(fp != nullptr); |
| 2154 | |
| 2155 | char file_data[12288]; |
| 2156 | for (size_t i = 0; i < 12288; i++) { |
| 2157 | file_data[i] = i; |
| 2158 | } |
| 2159 | ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp)); |
| 2160 | fclose(fp); |
| 2161 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2162 | fp = fopen(tf.path, "r"); |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 2163 | ASSERT_TRUE(fp != nullptr); |
| 2164 | |
| 2165 | char buffer[8192]; |
| 2166 | size_t cur_location = 0; |
| 2167 | // Small read to populate internal buffer. |
| 2168 | ASSERT_EQ(100U, fread(buffer, 1, 100, fp)); |
| 2169 | ASSERT_EQ(memcmp(file_data, buffer, 100), 0); |
| 2170 | |
| 2171 | cur_location = static_cast<size_t>(ftell(fp)); |
| 2172 | // Large read to force reading into the user supplied buffer and bypassing |
| 2173 | // the internal buffer. |
| 2174 | ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp)); |
| 2175 | ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0); |
| 2176 | |
| 2177 | // Small backwards seek to verify fseek does not reuse the internal buffer. |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 2178 | ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno); |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 2179 | cur_location = static_cast<size_t>(ftell(fp)); |
| 2180 | ASSERT_EQ(22U, fread(buffer, 1, 22, fp)); |
| 2181 | ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0); |
| 2182 | |
| 2183 | fclose(fp); |
| 2184 | } |
Elliott Hughes | 8ab433d | 2015-10-09 17:57:26 -0700 | [diff] [blame] | 2185 | |
| 2186 | // https://code.google.com/p/android/issues/detail?id=184847 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 2187 | TEST(STDIO_TEST, fread_EOF_184847) { |
Elliott Hughes | 8ab433d | 2015-10-09 17:57:26 -0700 | [diff] [blame] | 2188 | TemporaryFile tf; |
| 2189 | char buf[6] = {0}; |
| 2190 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2191 | FILE* fw = fopen(tf.path, "w"); |
Elliott Hughes | 8ab433d | 2015-10-09 17:57:26 -0700 | [diff] [blame] | 2192 | ASSERT_TRUE(fw != nullptr); |
| 2193 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2194 | FILE* fr = fopen(tf.path, "r"); |
Elliott Hughes | 8ab433d | 2015-10-09 17:57:26 -0700 | [diff] [blame] | 2195 | ASSERT_TRUE(fr != nullptr); |
| 2196 | |
| 2197 | fwrite("a", 1, 1, fw); |
| 2198 | fflush(fw); |
| 2199 | ASSERT_EQ(1U, fread(buf, 1, 1, fr)); |
| 2200 | ASSERT_STREQ("a", buf); |
| 2201 | |
| 2202 | // 'fr' is now at EOF. |
| 2203 | ASSERT_EQ(0U, fread(buf, 1, 1, fr)); |
| 2204 | ASSERT_TRUE(feof(fr)); |
| 2205 | |
| 2206 | // Write some more... |
| 2207 | fwrite("z", 1, 1, fw); |
| 2208 | fflush(fw); |
| 2209 | |
| 2210 | // ...and check that we can read it back. |
| 2211 | // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.) |
| 2212 | ASSERT_EQ(1U, fread(buf, 1, 1, fr)); |
| 2213 | ASSERT_STREQ("z", buf); |
| 2214 | |
| 2215 | // But now we're done. |
| 2216 | ASSERT_EQ(0U, fread(buf, 1, 1, fr)); |
| 2217 | |
| 2218 | fclose(fr); |
| 2219 | fclose(fw); |
| 2220 | } |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 2221 | |
| 2222 | TEST(STDIO_TEST, fclose_invalidates_fd) { |
| 2223 | // The typical error we're trying to help people catch involves accessing |
| 2224 | // memory after it's been freed. But we know that stdin/stdout/stderr are |
| 2225 | // special and don't get deallocated, so this test uses stdin. |
| 2226 | ASSERT_EQ(0, fclose(stdin)); |
| 2227 | |
| 2228 | // Even though using a FILE* after close is undefined behavior, I've closed |
| 2229 | // this bug as "WAI" too many times. We shouldn't hand out stale fds, |
| 2230 | // especially because they might actually correspond to a real stream. |
| 2231 | errno = 0; |
| 2232 | ASSERT_EQ(-1, fileno(stdin)); |
| 2233 | ASSERT_EQ(EBADF, errno); |
| 2234 | } |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 2235 | |
| 2236 | TEST(STDIO_TEST, fseek_ftell_unseekable) { |
| 2237 | #if defined(__BIONIC__) // glibc has fopencookie instead. |
| 2238 | auto read_fn = [](void*, char*, int) { return -1; }; |
| 2239 | FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr); |
| 2240 | ASSERT_TRUE(fp != nullptr); |
| 2241 | |
| 2242 | // Check that ftell balks on an unseekable FILE*. |
| 2243 | errno = 0; |
| 2244 | ASSERT_EQ(-1, ftell(fp)); |
| 2245 | ASSERT_EQ(ESPIPE, errno); |
| 2246 | |
| 2247 | // SEEK_CUR is rewritten as SEEK_SET internally... |
| 2248 | errno = 0; |
| 2249 | ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR)); |
| 2250 | ASSERT_EQ(ESPIPE, errno); |
| 2251 | |
| 2252 | // ...so it's worth testing the direct seek path too. |
| 2253 | errno = 0; |
| 2254 | ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET)); |
| 2255 | ASSERT_EQ(ESPIPE, errno); |
| 2256 | |
| 2257 | fclose(fp); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 2258 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2259 | GTEST_SKIP() << "glibc uses fopencookie instead"; |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 2260 | #endif |
| 2261 | } |
| 2262 | |
| 2263 | TEST(STDIO_TEST, funopen_EINVAL) { |
| 2264 | #if defined(__BIONIC__) |
| 2265 | errno = 0; |
| 2266 | ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr)); |
| 2267 | ASSERT_EQ(EINVAL, errno); |
| 2268 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2269 | GTEST_SKIP() << "glibc uses fopencookie instead"; |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 2270 | #endif |
| 2271 | } |
| 2272 | |
| 2273 | TEST(STDIO_TEST, funopen_seek) { |
| 2274 | #if defined(__BIONIC__) |
| 2275 | auto read_fn = [](void*, char*, int) { return -1; }; |
| 2276 | |
| 2277 | auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; }; |
| 2278 | auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; }; |
| 2279 | |
| 2280 | FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr); |
| 2281 | ASSERT_TRUE(fp != nullptr); |
| 2282 | fpos_t pos; |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 2283 | #if defined(__LP64__) |
| 2284 | EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno); |
| 2285 | EXPECT_EQ(0xfedcba12LL, pos); |
| 2286 | #else |
| 2287 | EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno); |
| 2288 | EXPECT_EQ(EOVERFLOW, errno); |
| 2289 | #endif |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 2290 | |
| 2291 | FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr); |
| 2292 | ASSERT_TRUE(fp64 != nullptr); |
| 2293 | fpos64_t pos64; |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 2294 | EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno); |
| 2295 | EXPECT_EQ(0xfedcba12345678, pos64); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 2296 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2297 | GTEST_SKIP() << "glibc uses fopencookie instead"; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 2298 | #endif |
| 2299 | } |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 2300 | |
| 2301 | TEST(STDIO_TEST, lots_of_concurrent_files) { |
| 2302 | std::vector<TemporaryFile*> tfs; |
| 2303 | std::vector<FILE*> fps; |
| 2304 | |
| 2305 | for (size_t i = 0; i < 256; ++i) { |
| 2306 | TemporaryFile* tf = new TemporaryFile; |
| 2307 | tfs.push_back(tf); |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2308 | FILE* fp = fopen(tf->path, "w+"); |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 2309 | fps.push_back(fp); |
| 2310 | fprintf(fp, "hello %zu!\n", i); |
| 2311 | fflush(fp); |
| 2312 | } |
| 2313 | |
| 2314 | for (size_t i = 0; i < 256; ++i) { |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 2315 | char expected[BUFSIZ]; |
| 2316 | snprintf(expected, sizeof(expected), "hello %zu!\n", i); |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 2317 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 2318 | AssertFileIs(fps[i], expected); |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 2319 | fclose(fps[i]); |
| 2320 | delete tfs[i]; |
| 2321 | } |
| 2322 | } |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 2323 | |
| 2324 | static void AssertFileOffsetAt(FILE* fp, off64_t offset) { |
| 2325 | EXPECT_EQ(offset, ftell(fp)); |
| 2326 | EXPECT_EQ(offset, ftello(fp)); |
Elliott Hughes | e4fa6e9 | 2016-02-02 22:39:15 -0800 | [diff] [blame] | 2327 | EXPECT_EQ(offset, ftello64(fp)); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 2328 | fpos_t pos; |
| 2329 | fpos64_t pos64; |
| 2330 | EXPECT_EQ(0, fgetpos(fp, &pos)); |
| 2331 | EXPECT_EQ(0, fgetpos64(fp, &pos64)); |
| 2332 | #if defined(__BIONIC__) |
| 2333 | EXPECT_EQ(offset, static_cast<off64_t>(pos)); |
| 2334 | EXPECT_EQ(offset, static_cast<off64_t>(pos64)); |
| 2335 | #else |
Elliott Hughes | bcaa454 | 2019-03-08 15:20:23 -0800 | [diff] [blame] | 2336 | GTEST_SKIP() << "glibc's fpos_t is opaque"; |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 2337 | #endif |
| 2338 | } |
| 2339 | |
| 2340 | TEST(STDIO_TEST, seek_tell_family_smoke) { |
| 2341 | TemporaryFile tf; |
| 2342 | FILE* fp = fdopen(tf.fd, "w+"); |
| 2343 | |
| 2344 | // Initially we should be at 0. |
| 2345 | AssertFileOffsetAt(fp, 0); |
| 2346 | |
| 2347 | // Seek to offset 8192. |
| 2348 | ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET)); |
| 2349 | AssertFileOffsetAt(fp, 8192); |
| 2350 | fpos_t eight_k_pos; |
| 2351 | ASSERT_EQ(0, fgetpos(fp, &eight_k_pos)); |
| 2352 | |
| 2353 | // Seek forward another 8192... |
| 2354 | ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR)); |
| 2355 | AssertFileOffsetAt(fp, 8192 + 8192); |
| 2356 | fpos64_t sixteen_k_pos64; |
| 2357 | ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64)); |
| 2358 | |
| 2359 | // Seek back 8192... |
| 2360 | ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR)); |
| 2361 | AssertFileOffsetAt(fp, 8192); |
| 2362 | |
| 2363 | // Since we haven't written anything, the end is also at 0. |
| 2364 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 2365 | AssertFileOffsetAt(fp, 0); |
| 2366 | |
| 2367 | // Check that our fpos64_t from 16KiB works... |
| 2368 | ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64)); |
| 2369 | AssertFileOffsetAt(fp, 8192 + 8192); |
| 2370 | // ...as does our fpos_t from 8192. |
| 2371 | ASSERT_EQ(0, fsetpos(fp, &eight_k_pos)); |
| 2372 | AssertFileOffsetAt(fp, 8192); |
| 2373 | |
| 2374 | // Do fseeko and fseeko64 work too? |
| 2375 | ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET)); |
| 2376 | AssertFileOffsetAt(fp, 1234); |
| 2377 | ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET)); |
| 2378 | AssertFileOffsetAt(fp, 5678); |
| 2379 | |
| 2380 | fclose(fp); |
| 2381 | } |
| 2382 | |
| 2383 | TEST(STDIO_TEST, fseek_fseeko_EINVAL) { |
| 2384 | TemporaryFile tf; |
| 2385 | FILE* fp = fdopen(tf.fd, "w+"); |
| 2386 | |
| 2387 | // Bad whence. |
| 2388 | errno = 0; |
| 2389 | ASSERT_EQ(-1, fseek(fp, 0, 123)); |
| 2390 | ASSERT_EQ(EINVAL, errno); |
| 2391 | errno = 0; |
| 2392 | ASSERT_EQ(-1, fseeko(fp, 0, 123)); |
| 2393 | ASSERT_EQ(EINVAL, errno); |
| 2394 | errno = 0; |
| 2395 | ASSERT_EQ(-1, fseeko64(fp, 0, 123)); |
| 2396 | ASSERT_EQ(EINVAL, errno); |
| 2397 | |
| 2398 | // Bad offset. |
| 2399 | errno = 0; |
| 2400 | ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET)); |
| 2401 | ASSERT_EQ(EINVAL, errno); |
| 2402 | errno = 0; |
| 2403 | ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET)); |
| 2404 | ASSERT_EQ(EINVAL, errno); |
| 2405 | errno = 0; |
| 2406 | ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET)); |
| 2407 | ASSERT_EQ(EINVAL, errno); |
| 2408 | |
| 2409 | fclose(fp); |
| 2410 | } |
Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 2411 | |
| 2412 | TEST(STDIO_TEST, ctermid) { |
| 2413 | ASSERT_STREQ("/dev/tty", ctermid(nullptr)); |
| 2414 | |
| 2415 | char buf[L_ctermid] = {}; |
| 2416 | ASSERT_EQ(buf, ctermid(buf)); |
| 2417 | ASSERT_STREQ("/dev/tty", buf); |
| 2418 | } |
Elliott Hughes | d1f25a7 | 2016-08-05 15:53:03 -0700 | [diff] [blame] | 2419 | |
| 2420 | TEST(STDIO_TEST, remove) { |
| 2421 | struct stat sb; |
| 2422 | |
| 2423 | TemporaryFile tf; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2424 | ASSERT_EQ(0, remove(tf.path)); |
| 2425 | ASSERT_EQ(-1, lstat(tf.path, &sb)); |
Elliott Hughes | d1f25a7 | 2016-08-05 15:53:03 -0700 | [diff] [blame] | 2426 | ASSERT_EQ(ENOENT, errno); |
| 2427 | |
| 2428 | TemporaryDir td; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2429 | ASSERT_EQ(0, remove(td.path)); |
| 2430 | ASSERT_EQ(-1, lstat(td.path, &sb)); |
Elliott Hughes | d1f25a7 | 2016-08-05 15:53:03 -0700 | [diff] [blame] | 2431 | ASSERT_EQ(ENOENT, errno); |
| 2432 | |
| 2433 | errno = 0; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2434 | ASSERT_EQ(-1, remove(tf.path)); |
Elliott Hughes | d1f25a7 | 2016-08-05 15:53:03 -0700 | [diff] [blame] | 2435 | ASSERT_EQ(ENOENT, errno); |
| 2436 | |
| 2437 | errno = 0; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2438 | ASSERT_EQ(-1, remove(td.path)); |
Elliott Hughes | d1f25a7 | 2016-08-05 15:53:03 -0700 | [diff] [blame] | 2439 | ASSERT_EQ(ENOENT, errno); |
| 2440 | } |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 2441 | |
Elliott Hughes | e657eb4 | 2021-02-18 17:11:56 -0800 | [diff] [blame] | 2442 | TEST_F(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) { |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 2443 | char buf[16]; |
| 2444 | ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"), |
| 2445 | testing::KilledBySignal(SIGABRT), |
| 2446 | #if defined(NOFORTIFY) |
| 2447 | "FORTIFY: vsnprintf: size .* > SSIZE_MAX" |
| 2448 | #else |
| 2449 | "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer" |
| 2450 | #endif |
| 2451 | ); |
| 2452 | } |
| 2453 | |
Elliott Hughes | e657eb4 | 2021-02-18 17:11:56 -0800 | [diff] [blame] | 2454 | TEST_F(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) { |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 2455 | std::string buf = "world"; |
| 2456 | ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"), |
| 2457 | testing::KilledBySignal(SIGABRT), |
| 2458 | "FORTIFY: vsnprintf: size .* > SSIZE_MAX"); |
| 2459 | } |
| 2460 | |
| 2461 | TEST(STDIO_TEST, sprintf_30445072) { |
| 2462 | std::string buf = "world"; |
| 2463 | sprintf(&buf[0], "hello"); |
| 2464 | ASSERT_EQ(buf, "hello"); |
| 2465 | } |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 2466 | |
Elliott Hughes | 654cd83 | 2018-08-30 16:00:42 -0700 | [diff] [blame] | 2467 | TEST(STDIO_TEST, printf_m) { |
| 2468 | char buf[BUFSIZ]; |
| 2469 | errno = 0; |
| 2470 | snprintf(buf, sizeof(buf), "<%m>"); |
| 2471 | ASSERT_STREQ("<Success>", buf); |
| 2472 | errno = -1; |
| 2473 | snprintf(buf, sizeof(buf), "<%m>"); |
| 2474 | ASSERT_STREQ("<Unknown error -1>", buf); |
| 2475 | errno = EINVAL; |
| 2476 | snprintf(buf, sizeof(buf), "<%m>"); |
Steven Moreland | 4ef83d6 | 2021-10-07 00:19:18 +0000 | [diff] [blame] | 2477 | ASSERT_STREQ("<Invalid argument>", buf); |
Elliott Hughes | 654cd83 | 2018-08-30 16:00:42 -0700 | [diff] [blame] | 2478 | } |
| 2479 | |
Elliott Hughes | f340a56 | 2018-09-06 10:42:40 -0700 | [diff] [blame] | 2480 | TEST(STDIO_TEST, printf_m_does_not_clobber_strerror) { |
| 2481 | char buf[BUFSIZ]; |
| 2482 | const char* m = strerror(-1); |
| 2483 | ASSERT_STREQ("Unknown error -1", m); |
| 2484 | errno = -2; |
| 2485 | snprintf(buf, sizeof(buf), "<%m>"); |
| 2486 | ASSERT_STREQ("<Unknown error -2>", buf); |
| 2487 | ASSERT_STREQ("Unknown error -1", m); |
| 2488 | } |
| 2489 | |
Elliott Hughes | 654cd83 | 2018-08-30 16:00:42 -0700 | [diff] [blame] | 2490 | TEST(STDIO_TEST, wprintf_m) { |
| 2491 | wchar_t buf[BUFSIZ]; |
| 2492 | errno = 0; |
| 2493 | swprintf(buf, sizeof(buf), L"<%m>"); |
| 2494 | ASSERT_EQ(std::wstring(L"<Success>"), buf); |
| 2495 | errno = -1; |
| 2496 | swprintf(buf, sizeof(buf), L"<%m>"); |
| 2497 | ASSERT_EQ(std::wstring(L"<Unknown error -1>"), buf); |
| 2498 | errno = EINVAL; |
| 2499 | swprintf(buf, sizeof(buf), L"<%m>"); |
Steven Moreland | 4ef83d6 | 2021-10-07 00:19:18 +0000 | [diff] [blame] | 2500 | ASSERT_EQ(std::wstring(L"<Invalid argument>"), buf); |
Elliott Hughes | 654cd83 | 2018-08-30 16:00:42 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
Elliott Hughes | f340a56 | 2018-09-06 10:42:40 -0700 | [diff] [blame] | 2503 | TEST(STDIO_TEST, wprintf_m_does_not_clobber_strerror) { |
| 2504 | wchar_t buf[BUFSIZ]; |
| 2505 | const char* m = strerror(-1); |
| 2506 | ASSERT_STREQ("Unknown error -1", m); |
| 2507 | errno = -2; |
| 2508 | swprintf(buf, sizeof(buf), L"<%m>"); |
| 2509 | ASSERT_EQ(std::wstring(L"<Unknown error -2>"), buf); |
| 2510 | ASSERT_STREQ("Unknown error -1", m); |
| 2511 | } |
| 2512 | |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 2513 | TEST(STDIO_TEST, fopen_append_mode_and_ftell) { |
| 2514 | TemporaryFile tf; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2515 | SetFileTo(tf.path, "0123456789"); |
| 2516 | FILE* fp = fopen(tf.path, "a"); |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 2517 | EXPECT_EQ(10, ftell(fp)); |
| 2518 | ASSERT_EQ(0, fseek(fp, 2, SEEK_SET)); |
| 2519 | EXPECT_EQ(2, ftell(fp)); |
| 2520 | ASSERT_NE(EOF, fputs("xxx", fp)); |
| 2521 | ASSERT_EQ(0, fflush(fp)); |
| 2522 | EXPECT_EQ(13, ftell(fp)); |
| 2523 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 2524 | EXPECT_EQ(13, ftell(fp)); |
| 2525 | ASSERT_EQ(0, fclose(fp)); |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2526 | AssertFileIs(tf.path, "0123456789xxx"); |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | TEST(STDIO_TEST, fdopen_append_mode_and_ftell) { |
| 2530 | TemporaryFile tf; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2531 | SetFileTo(tf.path, "0123456789"); |
| 2532 | int fd = open(tf.path, O_RDWR); |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 2533 | ASSERT_NE(-1, fd); |
| 2534 | // POSIX: "The file position indicator associated with the new stream is set to the position |
| 2535 | // indicated by the file offset associated with the file descriptor." |
| 2536 | ASSERT_EQ(4, lseek(fd, 4, SEEK_SET)); |
| 2537 | FILE* fp = fdopen(fd, "a"); |
| 2538 | EXPECT_EQ(4, ftell(fp)); |
| 2539 | ASSERT_EQ(0, fseek(fp, 2, SEEK_SET)); |
| 2540 | EXPECT_EQ(2, ftell(fp)); |
| 2541 | ASSERT_NE(EOF, fputs("xxx", fp)); |
| 2542 | ASSERT_EQ(0, fflush(fp)); |
| 2543 | EXPECT_EQ(13, ftell(fp)); |
| 2544 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 2545 | EXPECT_EQ(13, ftell(fp)); |
| 2546 | ASSERT_EQ(0, fclose(fp)); |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2547 | AssertFileIs(tf.path, "0123456789xxx"); |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 2548 | } |
| 2549 | |
| 2550 | TEST(STDIO_TEST, freopen_append_mode_and_ftell) { |
| 2551 | TemporaryFile tf; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2552 | SetFileTo(tf.path, "0123456789"); |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 2553 | FILE* other_fp = fopen("/proc/version", "r"); |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2554 | FILE* fp = freopen(tf.path, "a", other_fp); |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 2555 | EXPECT_EQ(10, ftell(fp)); |
| 2556 | ASSERT_EQ(0, fseek(fp, 2, SEEK_SET)); |
| 2557 | EXPECT_EQ(2, ftell(fp)); |
| 2558 | ASSERT_NE(EOF, fputs("xxx", fp)); |
| 2559 | ASSERT_EQ(0, fflush(fp)); |
| 2560 | EXPECT_EQ(13, ftell(fp)); |
| 2561 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 2562 | EXPECT_EQ(13, ftell(fp)); |
| 2563 | ASSERT_EQ(0, fclose(fp)); |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2564 | AssertFileIs(tf.path, "0123456789xxx"); |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 2565 | } |
Elliott Hughes | b15feb7 | 2017-07-31 17:20:18 -0700 | [diff] [blame] | 2566 | |
| 2567 | TEST(STDIO_TEST, constants) { |
| 2568 | ASSERT_LE(FILENAME_MAX, PATH_MAX); |
| 2569 | ASSERT_EQ(L_tmpnam, PATH_MAX); |
| 2570 | } |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 2571 | |
| 2572 | TEST(STDIO_TEST, perror) { |
| 2573 | ExecTestHelper eth; |
Steven Moreland | 4ef83d6 | 2021-10-07 00:19:18 +0000 | [diff] [blame] | 2574 | eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n"); |
| 2575 | eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n"); |
| 2576 | eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n"); |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | TEST(STDIO_TEST, puts) { |
| 2580 | ExecTestHelper eth; |
| 2581 | eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n"); |
| 2582 | } |
| 2583 | |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 2584 | TEST(STDIO_TEST, putchar) { |
| 2585 | ExecTestHelper eth; |
| 2586 | eth.Run([&]() { exit(putchar('A')); }, 65, "A"); |
| 2587 | } |
| 2588 | |
| 2589 | TEST(STDIO_TEST, putchar_unlocked) { |
| 2590 | ExecTestHelper eth; |
| 2591 | eth.Run([&]() { exit(putchar('B')); }, 66, "B"); |
| 2592 | } |
| 2593 | |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 2594 | TEST(STDIO_TEST, unlocked) { |
| 2595 | TemporaryFile tf; |
| 2596 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2597 | FILE* fp = fopen(tf.path, "w+"); |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 2598 | ASSERT_TRUE(fp != nullptr); |
| 2599 | |
| 2600 | clearerr_unlocked(fp); |
| 2601 | ASSERT_FALSE(feof_unlocked(fp)); |
| 2602 | ASSERT_FALSE(ferror_unlocked(fp)); |
| 2603 | |
| 2604 | ASSERT_EQ(fileno(fp), fileno_unlocked(fp)); |
| 2605 | |
| 2606 | ASSERT_NE(EOF, putc_unlocked('a', fp)); |
| 2607 | ASSERT_NE(EOF, putc('b', fp)); |
| 2608 | ASSERT_NE(EOF, fputc_unlocked('c', fp)); |
| 2609 | ASSERT_NE(EOF, fputc('d', fp)); |
| 2610 | |
| 2611 | rewind(fp); |
| 2612 | ASSERT_EQ('a', getc_unlocked(fp)); |
| 2613 | ASSERT_EQ('b', getc(fp)); |
| 2614 | ASSERT_EQ('c', fgetc_unlocked(fp)); |
| 2615 | ASSERT_EQ('d', fgetc(fp)); |
| 2616 | |
| 2617 | rewind(fp); |
| 2618 | ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp)); |
| 2619 | ASSERT_EQ(2U, fwrite("CD", 1, 2, fp)); |
| 2620 | ASSERT_EQ(0, fflush_unlocked(fp)); |
| 2621 | |
| 2622 | rewind(fp); |
| 2623 | char buf[BUFSIZ] = {}; |
| 2624 | ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp)); |
| 2625 | ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp)); |
| 2626 | ASSERT_STREQ("ABCD", buf); |
| 2627 | |
| 2628 | rewind(fp); |
| 2629 | ASSERT_NE(EOF, fputs("hello ", fp)); |
| 2630 | ASSERT_NE(EOF, fputs_unlocked("world", fp)); |
| 2631 | ASSERT_NE(EOF, fputc('\n', fp)); |
| 2632 | |
| 2633 | rewind(fp); |
| 2634 | ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr); |
| 2635 | ASSERT_STREQ("hello world\n", buf); |
| 2636 | |
| 2637 | ASSERT_EQ(0, fclose(fp)); |
| 2638 | } |
Ryan Prichard | bf54986 | 2017-11-07 15:30:32 -0800 | [diff] [blame] | 2639 | |
| 2640 | TEST(STDIO_TEST, fseek_64bit) { |
| 2641 | TemporaryFile tf; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2642 | FILE* fp = fopen64(tf.path, "w+"); |
Ryan Prichard | bf54986 | 2017-11-07 15:30:32 -0800 | [diff] [blame] | 2643 | ASSERT_TRUE(fp != nullptr); |
| 2644 | ASSERT_EQ(0, fseeko64(fp, 0x2'0000'0000, SEEK_SET)); |
| 2645 | ASSERT_EQ(0x2'0000'0000, ftello64(fp)); |
| 2646 | ASSERT_EQ(0, fseeko64(fp, 0x1'0000'0000, SEEK_CUR)); |
| 2647 | ASSERT_EQ(0x3'0000'0000, ftello64(fp)); |
| 2648 | ASSERT_EQ(0, fclose(fp)); |
| 2649 | } |
| 2650 | |
| 2651 | // POSIX requires that fseek/fseeko fail with EOVERFLOW if the new file offset |
| 2652 | // isn't representable in long/off_t. |
| 2653 | TEST(STDIO_TEST, fseek_overflow_32bit) { |
| 2654 | TemporaryFile tf; |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 2655 | FILE* fp = fopen64(tf.path, "w+"); |
Ryan Prichard | bf54986 | 2017-11-07 15:30:32 -0800 | [diff] [blame] | 2656 | ASSERT_EQ(0, ftruncate64(fileno(fp), 0x2'0000'0000)); |
| 2657 | |
| 2658 | // Bionic implements overflow checking for SEEK_CUR, but glibc doesn't. |
| 2659 | #if defined(__BIONIC__) && !defined(__LP64__) |
| 2660 | ASSERT_EQ(0, fseek(fp, 0x7fff'ffff, SEEK_SET)); |
| 2661 | ASSERT_EQ(-1, fseek(fp, 1, SEEK_CUR)); |
| 2662 | ASSERT_EQ(EOVERFLOW, errno); |
| 2663 | #endif |
| 2664 | |
| 2665 | // Neither Bionic nor glibc implement the overflow checking for SEEK_END. |
| 2666 | // (Aside: FreeBSD's libc is an example of a libc that checks both SEEK_CUR |
| 2667 | // and SEEK_END -- many C libraries check neither.) |
| 2668 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 2669 | ASSERT_EQ(0x2'0000'0000, ftello64(fp)); |
| 2670 | |
| 2671 | fclose(fp); |
| 2672 | } |
Elliott Hughes | f1a3838 | 2018-08-22 15:38:07 -0700 | [diff] [blame] | 2673 | |
| 2674 | TEST(STDIO_TEST, dev_std_files) { |
| 2675 | // POSIX only mentions /dev/stdout, but we should have all three (http://b/31824379). |
| 2676 | char path[PATH_MAX]; |
Christopher Ferris | 2c4ec7e | 2018-08-23 11:17:55 -0700 | [diff] [blame] | 2677 | ssize_t length = readlink("/dev/stdin", path, sizeof(path)); |
| 2678 | ASSERT_LT(0, length); |
| 2679 | ASSERT_EQ("/proc/self/fd/0", std::string(path, length)); |
| 2680 | |
| 2681 | length = readlink("/dev/stdout", path, sizeof(path)); |
| 2682 | ASSERT_LT(0, length); |
| 2683 | ASSERT_EQ("/proc/self/fd/1", std::string(path, length)); |
| 2684 | |
| 2685 | length = readlink("/dev/stderr", path, sizeof(path)); |
| 2686 | ASSERT_LT(0, length); |
| 2687 | ASSERT_EQ("/proc/self/fd/2", std::string(path, length)); |
Elliott Hughes | f1a3838 | 2018-08-22 15:38:07 -0700 | [diff] [blame] | 2688 | } |
Ryan Prichard | c485cdb | 2019-04-30 14:47:34 -0700 | [diff] [blame] | 2689 | |
| 2690 | TEST(STDIO_TEST, fread_with_locked_file) { |
| 2691 | // Reading an unbuffered/line-buffered file from one thread shouldn't block on |
| 2692 | // files locked on other threads, even if it flushes some line-buffered files. |
| 2693 | FILE* fp1 = fopen("/dev/zero", "r"); |
| 2694 | ASSERT_TRUE(fp1 != nullptr); |
| 2695 | flockfile(fp1); |
| 2696 | |
| 2697 | std::thread([] { |
| 2698 | for (int mode : { _IONBF, _IOLBF }) { |
| 2699 | FILE* fp2 = fopen("/dev/zero", "r"); |
| 2700 | ASSERT_TRUE(fp2 != nullptr); |
| 2701 | setvbuf(fp2, nullptr, mode, 0); |
| 2702 | ASSERT_EQ('\0', fgetc(fp2)); |
| 2703 | fclose(fp2); |
| 2704 | } |
| 2705 | }).join(); |
| 2706 | |
| 2707 | funlockfile(fp1); |
| 2708 | fclose(fp1); |
| 2709 | } |
Elliott Hughes | 31c7309 | 2019-05-07 10:03:02 -0700 | [diff] [blame] | 2710 | |
| 2711 | TEST(STDIO_TEST, SEEK_macros) { |
| 2712 | ASSERT_EQ(0, SEEK_SET); |
| 2713 | ASSERT_EQ(1, SEEK_CUR); |
| 2714 | ASSERT_EQ(2, SEEK_END); |
| 2715 | ASSERT_EQ(3, SEEK_DATA); |
| 2716 | ASSERT_EQ(4, SEEK_HOLE); |
| 2717 | // So we'll notice if Linux grows another constant in <linux/fs.h>... |
| 2718 | ASSERT_EQ(SEEK_MAX, SEEK_HOLE); |
| 2719 | } |
Elliott Hughes | 05b675e | 2019-04-17 13:01:06 -0700 | [diff] [blame] | 2720 | |
| 2721 | TEST(STDIO_TEST, rename) { |
| 2722 | TemporaryDir td; |
| 2723 | std::string old_path = td.path + "/old"s; |
| 2724 | std::string new_path = td.path + "/new"s; |
| 2725 | |
| 2726 | // Create the file, check it exists. |
| 2727 | ASSERT_EQ(0, close(creat(old_path.c_str(), 0666))); |
| 2728 | struct stat sb; |
| 2729 | ASSERT_EQ(0, stat(old_path.c_str(), &sb)); |
| 2730 | ASSERT_EQ(-1, stat(new_path.c_str(), &sb)); |
| 2731 | |
| 2732 | // Rename and check it moved. |
| 2733 | ASSERT_EQ(0, rename(old_path.c_str(), new_path.c_str())); |
| 2734 | ASSERT_EQ(-1, stat(old_path.c_str(), &sb)); |
| 2735 | ASSERT_EQ(0, stat(new_path.c_str(), &sb)); |
| 2736 | } |
| 2737 | |
| 2738 | TEST(STDIO_TEST, renameat) { |
| 2739 | TemporaryDir td; |
| 2740 | android::base::unique_fd dirfd{open(td.path, O_PATH)}; |
| 2741 | std::string old_path = td.path + "/old"s; |
| 2742 | std::string new_path = td.path + "/new"s; |
| 2743 | |
| 2744 | // Create the file, check it exists. |
| 2745 | ASSERT_EQ(0, close(creat(old_path.c_str(), 0666))); |
| 2746 | struct stat sb; |
| 2747 | ASSERT_EQ(0, stat(old_path.c_str(), &sb)); |
| 2748 | ASSERT_EQ(-1, stat(new_path.c_str(), &sb)); |
| 2749 | |
| 2750 | // Rename and check it moved. |
| 2751 | ASSERT_EQ(0, renameat(dirfd, "old", dirfd, "new")); |
| 2752 | ASSERT_EQ(-1, stat(old_path.c_str(), &sb)); |
| 2753 | ASSERT_EQ(0, stat(new_path.c_str(), &sb)); |
| 2754 | } |
| 2755 | |
| 2756 | TEST(STDIO_TEST, renameat2) { |
Colin Cross | 4c5595c | 2021-08-16 15:51:59 -0700 | [diff] [blame] | 2757 | #if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL) |
Colin Cross | 7da2034 | 2021-07-28 11:18:11 -0700 | [diff] [blame] | 2758 | GTEST_SKIP() << "glibc doesn't have renameat2 until 2.28 and musl doesn't have renameat2"; |
Elliott Hughes | 05b675e | 2019-04-17 13:01:06 -0700 | [diff] [blame] | 2759 | #else |
| 2760 | TemporaryDir td; |
| 2761 | android::base::unique_fd dirfd{open(td.path, O_PATH)}; |
| 2762 | std::string old_path = td.path + "/old"s; |
| 2763 | std::string new_path = td.path + "/new"s; |
| 2764 | |
| 2765 | // Create the file, check it exists. |
| 2766 | ASSERT_EQ(0, close(creat(old_path.c_str(), 0666))); |
| 2767 | struct stat sb; |
| 2768 | ASSERT_EQ(0, stat(old_path.c_str(), &sb)); |
| 2769 | ASSERT_EQ(-1, stat(new_path.c_str(), &sb)); |
| 2770 | |
| 2771 | // Rename and check it moved. |
| 2772 | ASSERT_EQ(0, renameat2(dirfd, "old", dirfd, "new", 0)); |
| 2773 | ASSERT_EQ(-1, stat(old_path.c_str(), &sb)); |
| 2774 | ASSERT_EQ(0, stat(new_path.c_str(), &sb)); |
| 2775 | |
| 2776 | // After this, both "old" and "new" exist. |
| 2777 | ASSERT_EQ(0, close(creat(old_path.c_str(), 0666))); |
| 2778 | |
| 2779 | // Rename and check it moved. |
| 2780 | ASSERT_EQ(-1, renameat2(dirfd, "old", dirfd, "new", RENAME_NOREPLACE)); |
| 2781 | ASSERT_EQ(EEXIST, errno); |
| 2782 | #endif |
| 2783 | } |
| 2784 | |
| 2785 | TEST(STDIO_TEST, renameat2_flags) { |
| 2786 | #if defined(__GLIBC__) |
| 2787 | GTEST_SKIP() << "glibc doesn't have renameat2 until 2.28"; |
| 2788 | #else |
| 2789 | ASSERT_NE(0, RENAME_EXCHANGE); |
| 2790 | ASSERT_NE(0, RENAME_NOREPLACE); |
| 2791 | ASSERT_NE(0, RENAME_WHITEOUT); |
| 2792 | #endif |
| 2793 | } |
Elliott Hughes | 7cebf83 | 2020-08-12 14:25:41 -0700 | [diff] [blame] | 2794 | |
| 2795 | TEST(STDIO_TEST, fdopen_failures) { |
| 2796 | FILE* fp; |
| 2797 | int fd = open("/proc/version", O_RDONLY); |
| 2798 | ASSERT_TRUE(fd != -1); |
| 2799 | |
| 2800 | // Nonsense mode. |
| 2801 | errno = 0; |
| 2802 | fp = fdopen(fd, "nonsense"); |
| 2803 | ASSERT_TRUE(fp == nullptr); |
| 2804 | ASSERT_EQ(EINVAL, errno); |
| 2805 | |
| 2806 | // Mode that isn't a subset of the fd's actual mode. |
| 2807 | errno = 0; |
| 2808 | fp = fdopen(fd, "w"); |
| 2809 | ASSERT_TRUE(fp == nullptr); |
| 2810 | ASSERT_EQ(EINVAL, errno); |
| 2811 | |
| 2812 | // Can't set append on the underlying fd. |
| 2813 | errno = 0; |
| 2814 | fp = fdopen(fd, "a"); |
| 2815 | ASSERT_TRUE(fp == nullptr); |
| 2816 | ASSERT_EQ(EINVAL, errno); |
| 2817 | |
| 2818 | // Bad fd. |
| 2819 | errno = 0; |
| 2820 | fp = fdopen(-1, "re"); |
| 2821 | ASSERT_TRUE(fp == nullptr); |
| 2822 | ASSERT_EQ(EBADF, errno); |
| 2823 | |
| 2824 | close(fd); |
| 2825 | } |
| 2826 | |
| 2827 | TEST(STDIO_TEST, fmemopen_invalid_mode) { |
| 2828 | errno = 0; |
| 2829 | FILE* fp = fmemopen(nullptr, 16, "nonsense"); |
| 2830 | ASSERT_TRUE(fp == nullptr); |
| 2831 | ASSERT_EQ(EINVAL, errno); |
| 2832 | } |
| 2833 | |
| 2834 | TEST(STDIO_TEST, fopen_invalid_mode) { |
| 2835 | errno = 0; |
| 2836 | FILE* fp = fopen("/proc/version", "nonsense"); |
| 2837 | ASSERT_TRUE(fp == nullptr); |
| 2838 | ASSERT_EQ(EINVAL, errno); |
| 2839 | } |
| 2840 | |
| 2841 | TEST(STDIO_TEST, freopen_invalid_mode) { |
| 2842 | FILE* fp = fopen("/proc/version", "re"); |
| 2843 | ASSERT_TRUE(fp != nullptr); |
| 2844 | |
| 2845 | errno = 0; |
| 2846 | fp = freopen("/proc/version", "nonsense", fp); |
| 2847 | ASSERT_TRUE(fp == nullptr); |
| 2848 | ASSERT_EQ(EINVAL, errno); |
| 2849 | } |
| 2850 | |
| 2851 | TEST(STDIO_TEST, asprintf_smoke) { |
| 2852 | char* p = nullptr; |
| 2853 | ASSERT_EQ(11, asprintf(&p, "hello %s", "world")); |
| 2854 | ASSERT_STREQ("hello world", p); |
| 2855 | free(p); |
| 2856 | } |
| 2857 | |
| 2858 | TEST(STDIO_TEST, fopen_ENOENT) { |
| 2859 | errno = 0; |
| 2860 | FILE* fp = fopen("/proc/does-not-exist", "re"); |
| 2861 | ASSERT_TRUE(fp == nullptr); |
| 2862 | ASSERT_EQ(ENOENT, errno); |
| 2863 | } |
Elliott Hughes | 439ebbd | 2020-12-04 18:51:42 -0800 | [diff] [blame] | 2864 | |
| 2865 | static void tempnam_test(bool has_TMPDIR, const char* dir, const char* prefix, const char* re) { |
| 2866 | if (has_TMPDIR) { |
| 2867 | setenv("TMPDIR", "/my/tmp/dir", 1); |
| 2868 | } else { |
| 2869 | unsetenv("TMPDIR"); |
| 2870 | } |
| 2871 | char* s1 = tempnam(dir, prefix); |
| 2872 | char* s2 = tempnam(dir, prefix); |
| 2873 | ASSERT_MATCH(s1, re); |
| 2874 | ASSERT_MATCH(s2, re); |
| 2875 | ASSERT_STRNE(s1, s2); |
| 2876 | free(s1); |
| 2877 | free(s2); |
| 2878 | } |
| 2879 | |
| 2880 | TEST(STDIO_TEST, tempnam__system_directory_system_prefix_with_TMPDIR) { |
| 2881 | tempnam_test(true, nullptr, nullptr, "^/my/tmp/dir/.*"); |
| 2882 | } |
| 2883 | |
| 2884 | TEST(STDIO_TEST, tempnam__system_directory_system_prefix_without_TMPDIR) { |
| 2885 | tempnam_test(false, nullptr, nullptr, "^/data/local/tmp/.*"); |
| 2886 | } |
| 2887 | |
| 2888 | TEST(STDIO_TEST, tempnam__system_directory_user_prefix_with_TMPDIR) { |
| 2889 | tempnam_test(true, nullptr, "prefix", "^/my/tmp/dir/prefix.*"); |
| 2890 | } |
| 2891 | |
| 2892 | TEST(STDIO_TEST, tempnam__system_directory_user_prefix_without_TMPDIR) { |
| 2893 | tempnam_test(false, nullptr, "prefix", "^/data/local/tmp/prefix.*"); |
| 2894 | } |
| 2895 | |
| 2896 | TEST(STDIO_TEST, tempnam__user_directory_system_prefix_with_TMPDIR) { |
| 2897 | tempnam_test(true, "/a/b/c", nullptr, "^/my/tmp/dir/.*"); |
| 2898 | } |
| 2899 | |
| 2900 | TEST(STDIO_TEST, tempnam__user_directory_system_prefix_without_TMPDIR) { |
| 2901 | tempnam_test(false, "/a/b/c", nullptr, "^/a/b/c/.*"); |
| 2902 | } |
| 2903 | |
| 2904 | TEST(STDIO_TEST, tempnam__user_directory_user_prefix_with_TMPDIR) { |
| 2905 | tempnam_test(true, "/a/b/c", "prefix", "^/my/tmp/dir/prefix.*"); |
| 2906 | } |
| 2907 | |
| 2908 | TEST(STDIO_TEST, tempnam__user_directory_user_prefix_without_TMPDIR) { |
| 2909 | tempnam_test(false, "/a/b/c", "prefix", "^/a/b/c/prefix.*"); |
| 2910 | } |
| 2911 | |
| 2912 | static void tmpnam_test(char* s) { |
| 2913 | char s1[L_tmpnam], s2[L_tmpnam]; |
| 2914 | |
| 2915 | strcpy(s1, tmpnam(s)); |
| 2916 | strcpy(s2, tmpnam(s)); |
| 2917 | ASSERT_MATCH(s1, "/tmp/.*"); |
| 2918 | ASSERT_MATCH(s2, "/tmp/.*"); |
| 2919 | ASSERT_STRNE(s1, s2); |
| 2920 | } |
| 2921 | |
| 2922 | TEST(STDIO_TEST, tmpnam) { |
| 2923 | tmpnam_test(nullptr); |
| 2924 | } |
| 2925 | |
| 2926 | TEST(STDIO_TEST, tmpnam_buf) { |
| 2927 | char buf[L_tmpnam]; |
| 2928 | tmpnam_test(buf); |
| 2929 | } |
Elliott Hughes | f9cfecf | 2021-02-04 16:58:13 -0800 | [diff] [blame] | 2930 | |
| 2931 | TEST(STDIO_TEST, freopen_null_filename_mode) { |
| 2932 | TemporaryFile tf; |
| 2933 | FILE* fp = fopen(tf.path, "r"); |
| 2934 | ASSERT_TRUE(fp != nullptr); |
| 2935 | |
| 2936 | // "r" = O_RDONLY |
| 2937 | char buf[1]; |
| 2938 | ASSERT_EQ(0, read(fileno(fp), buf, 1)); |
| 2939 | ASSERT_EQ(-1, write(fileno(fp), "hello", 1)); |
| 2940 | // "r+" = O_RDWR |
| 2941 | fp = freopen(nullptr, "r+", fp); |
| 2942 | ASSERT_EQ(0, read(fileno(fp), buf, 1)); |
| 2943 | ASSERT_EQ(1, write(fileno(fp), "hello", 1)); |
| 2944 | // "w" = O_WRONLY |
| 2945 | fp = freopen(nullptr, "w", fp); |
| 2946 | ASSERT_EQ(-1, read(fileno(fp), buf, 1)); |
| 2947 | ASSERT_EQ(1, write(fileno(fp), "hello", 1)); |
| 2948 | |
| 2949 | fclose(fp); |
| 2950 | } |
Elliott Hughes | be78fc9 | 2022-07-28 20:58:45 +0000 | [diff] [blame] | 2951 | |
Elliott Hughes | 0cac291 | 2022-08-02 18:25:22 +0000 | [diff] [blame] | 2952 | #if defined(__LP64__) |
Elliott Hughes | be78fc9 | 2022-07-28 20:58:45 +0000 | [diff] [blame] | 2953 | static int64_t GetTotalRamGiB() { |
| 2954 | struct sysinfo si; |
| 2955 | sysinfo(&si); |
| 2956 | return (static_cast<int64_t>(si.totalram) * si.mem_unit) / 1024 / 1024 / 1024; |
| 2957 | } |
Elliott Hughes | 0cac291 | 2022-08-02 18:25:22 +0000 | [diff] [blame] | 2958 | #endif |
Elliott Hughes | be78fc9 | 2022-07-28 20:58:45 +0000 | [diff] [blame] | 2959 | |
| 2960 | TEST(STDIO_TEST, fread_int_overflow) { |
Elliott Hughes | 0cac291 | 2022-08-02 18:25:22 +0000 | [diff] [blame] | 2961 | #if defined(__LP64__) |
Elliott Hughes | be78fc9 | 2022-07-28 20:58:45 +0000 | [diff] [blame] | 2962 | if (GetTotalRamGiB() <= 4) GTEST_SKIP() << "not enough memory"; |
| 2963 | |
| 2964 | const size_t too_big_for_an_int = 0x80000000ULL; |
| 2965 | std::vector<char> buf(too_big_for_an_int); |
| 2966 | std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/dev/zero", "re"), fclose}; |
| 2967 | ASSERT_EQ(too_big_for_an_int, fread(&buf[0], 1, too_big_for_an_int, fp.get())); |
Elliott Hughes | 0cac291 | 2022-08-02 18:25:22 +0000 | [diff] [blame] | 2968 | #else |
| 2969 | GTEST_SKIP() << "32-bit can't allocate 2GiB"; |
| 2970 | #endif |
Elliott Hughes | be78fc9 | 2022-07-28 20:58:45 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | TEST(STDIO_TEST, fwrite_int_overflow) { |
Elliott Hughes | 0cac291 | 2022-08-02 18:25:22 +0000 | [diff] [blame] | 2974 | #if defined(__LP64__) |
Elliott Hughes | be78fc9 | 2022-07-28 20:58:45 +0000 | [diff] [blame] | 2975 | if (GetTotalRamGiB() <= 4) GTEST_SKIP() << "not enough memory"; |
| 2976 | |
| 2977 | const size_t too_big_for_an_int = 0x80000000ULL; |
| 2978 | std::vector<char> buf(too_big_for_an_int); |
| 2979 | std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/dev/null", "we"), fclose}; |
| 2980 | ASSERT_EQ(too_big_for_an_int, fwrite(&buf[0], 1, too_big_for_an_int, fp.get())); |
Elliott Hughes | 0cac291 | 2022-08-02 18:25:22 +0000 | [diff] [blame] | 2981 | #else |
| 2982 | GTEST_SKIP() << "32-bit can't allocate 2GiB"; |
| 2983 | #endif |
Elliott Hughes | be78fc9 | 2022-07-28 20:58:45 +0000 | [diff] [blame] | 2984 | } |
Elliott Hughes | b813a6a | 2022-08-01 22:18:40 +0000 | [diff] [blame] | 2985 | |
| 2986 | TEST(STDIO_TEST, snprintf_b) { |
Elliott Hughes | b813a6a | 2022-08-01 22:18:40 +0000 | [diff] [blame] | 2987 | char buf[BUFSIZ]; |
| 2988 | EXPECT_EQ(5, snprintf(buf, sizeof(buf), "<%b>", 5)); |
| 2989 | EXPECT_STREQ("<101>", buf); |
| 2990 | EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%08b>", 5)); |
| 2991 | EXPECT_STREQ("<00000101>", buf); |
| 2992 | EXPECT_EQ(34, snprintf(buf, sizeof(buf), "<%b>", 0xaaaaaaaa)); |
| 2993 | EXPECT_STREQ("<10101010101010101010101010101010>", buf); |
| 2994 | EXPECT_EQ(36, snprintf(buf, sizeof(buf), "<%#b>", 0xaaaaaaaa)); |
| 2995 | EXPECT_STREQ("<0b10101010101010101010101010101010>", buf); |
| 2996 | EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%#b>", 0)); |
| 2997 | EXPECT_STREQ("<0>", buf); |
Elliott Hughes | b813a6a | 2022-08-01 22:18:40 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | TEST(STDIO_TEST, snprintf_B) { |
Elliott Hughes | b813a6a | 2022-08-01 22:18:40 +0000 | [diff] [blame] | 3001 | char buf[BUFSIZ]; |
| 3002 | EXPECT_EQ(5, snprintf(buf, sizeof(buf), "<%B>", 5)); |
| 3003 | EXPECT_STREQ("<101>", buf); |
| 3004 | EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%08B>", 5)); |
| 3005 | EXPECT_STREQ("<00000101>", buf); |
| 3006 | EXPECT_EQ(34, snprintf(buf, sizeof(buf), "<%B>", 0xaaaaaaaa)); |
| 3007 | EXPECT_STREQ("<10101010101010101010101010101010>", buf); |
| 3008 | EXPECT_EQ(36, snprintf(buf, sizeof(buf), "<%#B>", 0xaaaaaaaa)); |
| 3009 | EXPECT_STREQ("<0B10101010101010101010101010101010>", buf); |
| 3010 | EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%#B>", 0)); |
| 3011 | EXPECT_STREQ("<0>", buf); |
Elliott Hughes | b813a6a | 2022-08-01 22:18:40 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
| 3014 | TEST(STDIO_TEST, swprintf_b) { |
Elliott Hughes | b813a6a | 2022-08-01 22:18:40 +0000 | [diff] [blame] | 3015 | wchar_t buf[BUFSIZ]; |
| 3016 | EXPECT_EQ(5, swprintf(buf, sizeof(buf), L"<%b>", 5)); |
| 3017 | EXPECT_EQ(std::wstring(L"<101>"), buf); |
| 3018 | EXPECT_EQ(10, swprintf(buf, sizeof(buf), L"<%08b>", 5)); |
| 3019 | EXPECT_EQ(std::wstring(L"<00000101>"), buf); |
| 3020 | EXPECT_EQ(34, swprintf(buf, sizeof(buf), L"<%b>", 0xaaaaaaaa)); |
| 3021 | EXPECT_EQ(std::wstring(L"<10101010101010101010101010101010>"), buf); |
| 3022 | EXPECT_EQ(36, swprintf(buf, sizeof(buf), L"<%#b>", 0xaaaaaaaa)); |
| 3023 | EXPECT_EQ(std::wstring(L"<0b10101010101010101010101010101010>"), buf); |
| 3024 | EXPECT_EQ(3, swprintf(buf, sizeof(buf), L"<%#b>", 0)); |
| 3025 | EXPECT_EQ(std::wstring(L"<0>"), buf); |
Elliott Hughes | b813a6a | 2022-08-01 22:18:40 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
| 3028 | TEST(STDIO_TEST, swprintf_B) { |
Elliott Hughes | b813a6a | 2022-08-01 22:18:40 +0000 | [diff] [blame] | 3029 | wchar_t buf[BUFSIZ]; |
| 3030 | EXPECT_EQ(5, swprintf(buf, sizeof(buf), L"<%B>", 5)); |
| 3031 | EXPECT_EQ(std::wstring(L"<101>"), buf); |
| 3032 | EXPECT_EQ(10, swprintf(buf, sizeof(buf), L"<%08B>", 5)); |
| 3033 | EXPECT_EQ(std::wstring(L"<00000101>"), buf); |
| 3034 | EXPECT_EQ(34, swprintf(buf, sizeof(buf), L"<%B>", 0xaaaaaaaa)); |
| 3035 | EXPECT_EQ(std::wstring(L"<10101010101010101010101010101010>"), buf); |
| 3036 | EXPECT_EQ(36, swprintf(buf, sizeof(buf), L"<%#B>", 0xaaaaaaaa)); |
| 3037 | EXPECT_EQ(std::wstring(L"<0B10101010101010101010101010101010>"), buf); |
| 3038 | EXPECT_EQ(3, swprintf(buf, sizeof(buf), L"<%#B>", 0)); |
| 3039 | EXPECT_EQ(std::wstring(L"<0>"), buf); |
Elliott Hughes | b813a6a | 2022-08-01 22:18:40 +0000 | [diff] [blame] | 3040 | } |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 3041 | |
| 3042 | TEST(STDIO_TEST, scanf_i_decimal) { |
| 3043 | int i; |
| 3044 | EXPECT_EQ(1, sscanf("<123789>", "<%i>", &i)); |
| 3045 | EXPECT_EQ(123789, i); |
| 3046 | |
| 3047 | long long int lli; |
| 3048 | char ch; |
| 3049 | EXPECT_EQ(2, sscanf("1234567890abcdefg", "%lli%c", &lli, &ch)); |
| 3050 | EXPECT_EQ(1234567890, lli); |
| 3051 | EXPECT_EQ('a', ch); |
| 3052 | } |
| 3053 | |
| 3054 | TEST(STDIO_TEST, scanf_i_hex) { |
| 3055 | int i; |
| 3056 | EXPECT_EQ(1, sscanf("<0x123abf>", "<%i>", &i)); |
| 3057 | EXPECT_EQ(0x123abf, i); |
| 3058 | |
| 3059 | long long int lli; |
| 3060 | char ch; |
| 3061 | EXPECT_EQ(2, sscanf("0x1234567890abcdefg", "%lli%c", &lli, &ch)); |
| 3062 | EXPECT_EQ(0x1234567890abcdefLL, lli); |
| 3063 | EXPECT_EQ('g', ch); |
| 3064 | } |
| 3065 | |
| 3066 | TEST(STDIO_TEST, scanf_i_octal) { |
| 3067 | int i; |
| 3068 | EXPECT_EQ(1, sscanf("<01234567>", "<%i>", &i)); |
| 3069 | EXPECT_EQ(01234567, i); |
| 3070 | |
| 3071 | long long int lli; |
| 3072 | char ch; |
| 3073 | EXPECT_EQ(2, sscanf("010234567890abcdefg", "%lli%c", &lli, &ch)); |
| 3074 | EXPECT_EQ(010234567, lli); |
| 3075 | EXPECT_EQ('8', ch); |
| 3076 | } |
| 3077 | |
| 3078 | TEST(STDIO_TEST, scanf_i_binary) { |
| 3079 | int i; |
| 3080 | EXPECT_EQ(1, sscanf("<0b101>", "<%i>", &i)); |
| 3081 | EXPECT_EQ(0b101, i); |
| 3082 | |
| 3083 | long long int lli; |
| 3084 | char ch; |
| 3085 | EXPECT_EQ(2, sscanf("0b10234567890abcdefg", "%lli%c", &lli, &ch)); |
| 3086 | EXPECT_EQ(0b10, lli); |
| 3087 | EXPECT_EQ('2', ch); |
| 3088 | } |
| 3089 | |
| 3090 | TEST(STDIO_TEST, wscanf_i_decimal) { |
| 3091 | int i; |
| 3092 | EXPECT_EQ(1, swscanf(L"<123789>", L"<%i>", &i)); |
| 3093 | EXPECT_EQ(123789, i); |
| 3094 | |
| 3095 | long long int lli; |
| 3096 | char ch; |
| 3097 | EXPECT_EQ(2, swscanf(L"1234567890abcdefg", L"%lli%c", &lli, &ch)); |
| 3098 | EXPECT_EQ(1234567890, lli); |
| 3099 | EXPECT_EQ('a', ch); |
| 3100 | } |
| 3101 | |
| 3102 | TEST(STDIO_TEST, wscanf_i_hex) { |
| 3103 | int i; |
| 3104 | EXPECT_EQ(1, swscanf(L"<0x123abf>", L"<%i>", &i)); |
| 3105 | EXPECT_EQ(0x123abf, i); |
| 3106 | |
| 3107 | long long int lli; |
| 3108 | char ch; |
| 3109 | EXPECT_EQ(2, swscanf(L"0x1234567890abcdefg", L"%lli%c", &lli, &ch)); |
| 3110 | EXPECT_EQ(0x1234567890abcdefLL, lli); |
| 3111 | EXPECT_EQ('g', ch); |
| 3112 | } |
| 3113 | |
| 3114 | TEST(STDIO_TEST, wscanf_i_octal) { |
| 3115 | int i; |
| 3116 | EXPECT_EQ(1, swscanf(L"<01234567>", L"<%i>", &i)); |
| 3117 | EXPECT_EQ(01234567, i); |
| 3118 | |
| 3119 | long long int lli; |
| 3120 | char ch; |
| 3121 | EXPECT_EQ(2, swscanf(L"010234567890abcdefg", L"%lli%c", &lli, &ch)); |
| 3122 | EXPECT_EQ(010234567, lli); |
| 3123 | EXPECT_EQ('8', ch); |
| 3124 | } |
| 3125 | |
| 3126 | TEST(STDIO_TEST, wscanf_i_binary) { |
| 3127 | int i; |
| 3128 | EXPECT_EQ(1, swscanf(L"<0b101>", L"<%i>", &i)); |
| 3129 | EXPECT_EQ(0b101, i); |
| 3130 | |
| 3131 | long long int lli; |
| 3132 | char ch; |
| 3133 | EXPECT_EQ(2, swscanf(L"0b10234567890abcdefg", L"%lli%c", &lli, &ch)); |
| 3134 | EXPECT_EQ(0b10, lli); |
| 3135 | EXPECT_EQ('2', ch); |
| 3136 | } |
| 3137 | |
| 3138 | TEST(STDIO_TEST, scanf_b) { |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 3139 | int i; |
| 3140 | char ch; |
| 3141 | EXPECT_EQ(2, sscanf("<1012>", "<%b%c>", &i, &ch)); |
| 3142 | EXPECT_EQ(0b101, i); |
| 3143 | EXPECT_EQ('2', ch); |
| 3144 | EXPECT_EQ(1, sscanf("<00000101>", "<%08b>", &i)); |
| 3145 | EXPECT_EQ(0b00000101, i); |
| 3146 | EXPECT_EQ(1, sscanf("<0b1010>", "<%b>", &i)); |
| 3147 | EXPECT_EQ(0b1010, i); |
| 3148 | EXPECT_EQ(2, sscanf("-0b", "%i%c", &i, &ch)); |
| 3149 | EXPECT_EQ(0, i); |
| 3150 | EXPECT_EQ('b', ch); |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | TEST(STDIO_TEST, swscanf_b) { |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 3154 | int i; |
| 3155 | char ch; |
| 3156 | EXPECT_EQ(2, swscanf(L"<1012>", L"<%b%c>", &i, &ch)); |
| 3157 | EXPECT_EQ(0b101, i); |
| 3158 | EXPECT_EQ('2', ch); |
| 3159 | EXPECT_EQ(1, swscanf(L"<00000101>", L"<%08b>", &i)); |
| 3160 | EXPECT_EQ(0b00000101, i); |
| 3161 | EXPECT_EQ(1, swscanf(L"<0b1010>", L"<%b>", &i)); |
| 3162 | EXPECT_EQ(0b1010, i); |
| 3163 | EXPECT_EQ(2, swscanf(L"-0b", L"%i%c", &i, &ch)); |
| 3164 | EXPECT_EQ(0, i); |
| 3165 | EXPECT_EQ('b', ch); |
Elliott Hughes | 1f462de | 2022-08-05 22:51:05 +0000 | [diff] [blame] | 3166 | } |