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