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