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