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