Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include <errno.h> |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 21 | #include <limits.h> |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 22 | #include <math.h> |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <unistd.h> |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 27 | #include <wchar.h> |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 28 | #include <locale.h> |
| 29 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 30 | #include <string> |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 33 | #include "BionicDeathTest.h" |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 34 | #include "TemporaryFile.h" |
Josh Gao | 2f06e10 | 2017-01-10 13:00:37 -0800 | [diff] [blame] | 35 | #include "utils.h" |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 36 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 37 | #if defined(NOFORTIFY) |
| 38 | #define STDIO_TEST stdio_nofortify |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 39 | #define STDIO_DEATHTEST stdio_nofortify_DeathTest |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 40 | #else |
| 41 | #define STDIO_TEST stdio |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 42 | #define STDIO_DEATHTEST stdio_DeathTest |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 43 | #endif |
| 44 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 45 | using namespace std::string_literals; |
| 46 | |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 47 | class stdio_DeathTest : public BionicDeathTest {}; |
| 48 | class stdio_nofortify_DeathTest : public BionicDeathTest {}; |
| 49 | |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 50 | static void SetFileTo(const char* path, const char* content) { |
| 51 | FILE* fp; |
| 52 | ASSERT_NE(nullptr, fp = fopen(path, "w")); |
| 53 | ASSERT_NE(EOF, fputs(content, fp)); |
| 54 | ASSERT_EQ(0, fclose(fp)); |
| 55 | } |
| 56 | |
| 57 | static void AssertFileIs(const char* path, const char* expected) { |
| 58 | FILE* fp; |
| 59 | ASSERT_NE(nullptr, fp = fopen(path, "r")); |
| 60 | char* line = nullptr; |
| 61 | size_t length; |
| 62 | ASSERT_NE(EOF, getline(&line, &length, fp)); |
| 63 | ASSERT_EQ(0, fclose(fp)); |
| 64 | ASSERT_STREQ(expected, line); |
| 65 | free(line); |
| 66 | } |
| 67 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 68 | static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) { |
| 69 | rewind(fp); |
| 70 | |
| 71 | char line[1024]; |
Josh Gao | 2f06e10 | 2017-01-10 13:00:37 -0800 | [diff] [blame] | 72 | memset(line, 0xff, sizeof(line)); |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 73 | ASSERT_EQ(line, fgets(line, sizeof(line), fp)); |
| 74 | ASSERT_STREQ(expected, line); |
| 75 | |
| 76 | if (is_fmemopen) { |
| 77 | // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an |
| 78 | // extra empty line, but does on every C library I tested... |
| 79 | ASSERT_EQ(line, fgets(line, sizeof(line), fp)); |
| 80 | ASSERT_STREQ("", line); |
| 81 | } |
| 82 | |
| 83 | // Make sure there isn't anything else in the file. |
| 84 | ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line; |
| 85 | } |
| 86 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 87 | TEST(STDIO_TEST, flockfile_18208568_stderr) { |
Elliott Hughes | 6a03abc | 2014-11-03 12:32:17 -0800 | [diff] [blame] | 88 | // Check that we have a _recursive_ mutex for flockfile. |
| 89 | flockfile(stderr); |
| 90 | feof(stderr); // We don't care about the result, but this needs to take the lock. |
| 91 | funlockfile(stderr); |
| 92 | } |
| 93 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 94 | TEST(STDIO_TEST, flockfile_18208568_regular) { |
Elliott Hughes | 6a03abc | 2014-11-03 12:32:17 -0800 | [diff] [blame] | 95 | // We never had a bug for streams other than stdin/stdout/stderr, but test anyway. |
| 96 | FILE* fp = fopen("/dev/null", "w"); |
| 97 | ASSERT_TRUE(fp != NULL); |
| 98 | flockfile(fp); |
| 99 | feof(fp); |
| 100 | funlockfile(fp); |
| 101 | fclose(fp); |
| 102 | } |
| 103 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 104 | TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) { |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 105 | FILE* fp = tmpfile(); |
| 106 | ASSERT_TRUE(fp != NULL); |
| 107 | |
| 108 | int fd = fileno(fp); |
| 109 | ASSERT_NE(fd, -1); |
| 110 | |
| 111 | struct stat sb; |
| 112 | int rc = fstat(fd, &sb); |
| 113 | ASSERT_NE(rc, -1); |
| 114 | ASSERT_EQ(sb.st_mode & 0777, 0600U); |
| 115 | |
| 116 | rc = fprintf(fp, "hello\n"); |
| 117 | ASSERT_EQ(rc, 6); |
| 118 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 119 | AssertFileIs(fp, "hello\n"); |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 120 | fclose(fp); |
| 121 | } |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 122 | |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 123 | TEST(STDIO_TEST, tmpfile64) { |
| 124 | FILE* fp = tmpfile64(); |
| 125 | ASSERT_TRUE(fp != nullptr); |
| 126 | fclose(fp); |
| 127 | } |
| 128 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 129 | TEST(STDIO_TEST, dprintf) { |
Calin Juravle | 6afb2a9 | 2014-05-22 11:47:47 +0100 | [diff] [blame] | 130 | TemporaryFile tf; |
| 131 | |
| 132 | int rc = dprintf(tf.fd, "hello\n"); |
| 133 | ASSERT_EQ(rc, 6); |
| 134 | |
Yabin Cui | 5ca4a9e | 2014-11-06 19:55:09 -0800 | [diff] [blame] | 135 | lseek(tf.fd, 0, SEEK_SET); |
Christopher Ferris | 9e01ea6 | 2014-05-29 12:49:35 -0700 | [diff] [blame] | 136 | FILE* tfile = fdopen(tf.fd, "r"); |
| 137 | ASSERT_TRUE(tfile != NULL); |
Calin Juravle | 6afb2a9 | 2014-05-22 11:47:47 +0100 | [diff] [blame] | 138 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 139 | AssertFileIs(tfile, "hello\n"); |
Christopher Ferris | 9e01ea6 | 2014-05-29 12:49:35 -0700 | [diff] [blame] | 140 | fclose(tfile); |
Calin Juravle | 6afb2a9 | 2014-05-22 11:47:47 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 143 | TEST(STDIO_TEST, getdelim) { |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 144 | FILE* fp = tmpfile(); |
| 145 | ASSERT_TRUE(fp != NULL); |
| 146 | |
| 147 | const char* line_written = "This is a test"; |
| 148 | int rc = fprintf(fp, "%s", line_written); |
| 149 | ASSERT_EQ(rc, static_cast<int>(strlen(line_written))); |
| 150 | |
| 151 | rewind(fp); |
| 152 | |
| 153 | char* word_read = NULL; |
| 154 | size_t allocated_length = 0; |
| 155 | |
| 156 | const char* expected[] = { "This ", " ", "is ", "a ", "test" }; |
| 157 | for (size_t i = 0; i < 5; ++i) { |
| 158 | ASSERT_FALSE(feof(fp)); |
| 159 | ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i]))); |
| 160 | ASSERT_GE(allocated_length, strlen(expected[i])); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 161 | ASSERT_STREQ(expected[i], word_read); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 162 | } |
| 163 | // The last read should have set the end-of-file indicator for the stream. |
| 164 | ASSERT_TRUE(feof(fp)); |
| 165 | clearerr(fp); |
| 166 | |
| 167 | // getdelim returns -1 but doesn't set errno if we're already at EOF. |
| 168 | // It should set the end-of-file indicator for the stream, though. |
| 169 | errno = 0; |
| 170 | ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 171 | ASSERT_EQ(0, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 172 | ASSERT_TRUE(feof(fp)); |
| 173 | |
| 174 | free(word_read); |
| 175 | fclose(fp); |
| 176 | } |
| 177 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 178 | TEST(STDIO_TEST, getdelim_invalid) { |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 179 | FILE* fp = tmpfile(); |
Elliott Hughes | 6ad8f76 | 2013-12-19 14:56:17 -0800 | [diff] [blame] | 180 | ASSERT_TRUE(fp != NULL); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 181 | |
| 182 | char* buffer = NULL; |
| 183 | size_t buffer_length = 0; |
| 184 | |
| 185 | // The first argument can't be NULL. |
| 186 | errno = 0; |
| 187 | ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 188 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 189 | |
| 190 | // The second argument can't be NULL. |
| 191 | errno = 0; |
| 192 | ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 193 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 194 | |
Elliott Hughes | 20f8aec | 2014-05-12 15:15:37 -0700 | [diff] [blame] | 195 | // The underlying fd can't be closed. |
| 196 | ASSERT_EQ(0, close(fileno(fp))); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 197 | errno = 0; |
| 198 | ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 199 | ASSERT_EQ(EBADF, errno); |
Elliott Hughes | 20f8aec | 2014-05-12 15:15:37 -0700 | [diff] [blame] | 200 | fclose(fp); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 201 | } |
| 202 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 203 | TEST(STDIO_TEST, getdelim_directory) { |
Elliott Hughes | 694fd2d | 2015-04-05 10:51:56 -0700 | [diff] [blame] | 204 | FILE* fp = fopen("/proc", "r"); |
| 205 | ASSERT_TRUE(fp != NULL); |
| 206 | char* word_read; |
| 207 | size_t allocated_length; |
| 208 | ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp)); |
| 209 | fclose(fp); |
| 210 | } |
| 211 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 212 | TEST(STDIO_TEST, getline) { |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 213 | FILE* fp = tmpfile(); |
| 214 | ASSERT_TRUE(fp != NULL); |
| 215 | |
| 216 | const char* line_written = "This is a test for getline\n"; |
| 217 | const size_t line_count = 5; |
| 218 | |
| 219 | for (size_t i = 0; i < line_count; ++i) { |
| 220 | int rc = fprintf(fp, "%s", line_written); |
| 221 | ASSERT_EQ(rc, static_cast<int>(strlen(line_written))); |
| 222 | } |
| 223 | |
| 224 | rewind(fp); |
| 225 | |
| 226 | char* line_read = NULL; |
| 227 | size_t allocated_length = 0; |
| 228 | |
| 229 | size_t read_line_count = 0; |
| 230 | ssize_t read_char_count; |
| 231 | while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) { |
| 232 | ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written))); |
| 233 | ASSERT_GE(allocated_length, strlen(line_written)); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 234 | ASSERT_STREQ(line_written, line_read); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 235 | ++read_line_count; |
| 236 | } |
| 237 | ASSERT_EQ(read_line_count, line_count); |
| 238 | |
| 239 | // The last read should have set the end-of-file indicator for the stream. |
| 240 | ASSERT_TRUE(feof(fp)); |
| 241 | clearerr(fp); |
| 242 | |
| 243 | // getline returns -1 but doesn't set errno if we're already at EOF. |
| 244 | // It should set the end-of-file indicator for the stream, though. |
| 245 | errno = 0; |
| 246 | ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 247 | ASSERT_EQ(0, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 248 | ASSERT_TRUE(feof(fp)); |
| 249 | |
| 250 | free(line_read); |
| 251 | fclose(fp); |
| 252 | } |
| 253 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 254 | TEST(STDIO_TEST, getline_invalid) { |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 255 | FILE* fp = tmpfile(); |
Elliott Hughes | 6ad8f76 | 2013-12-19 14:56:17 -0800 | [diff] [blame] | 256 | ASSERT_TRUE(fp != NULL); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 257 | |
| 258 | char* buffer = NULL; |
| 259 | size_t buffer_length = 0; |
| 260 | |
| 261 | // The first argument can't be NULL. |
| 262 | errno = 0; |
| 263 | ASSERT_EQ(getline(NULL, &buffer_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 264 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 265 | |
| 266 | // The second argument can't be NULL. |
| 267 | errno = 0; |
| 268 | ASSERT_EQ(getline(&buffer, NULL, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 269 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 270 | |
Elliott Hughes | 20f8aec | 2014-05-12 15:15:37 -0700 | [diff] [blame] | 271 | // The underlying fd can't be closed. |
| 272 | ASSERT_EQ(0, close(fileno(fp))); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 273 | errno = 0; |
| 274 | ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 275 | ASSERT_EQ(EBADF, errno); |
Elliott Hughes | 20f8aec | 2014-05-12 15:15:37 -0700 | [diff] [blame] | 276 | fclose(fp); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 277 | } |
Thorsten Glaser | c641caf | 2013-02-17 16:50:58 +0000 | [diff] [blame] | 278 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 279 | TEST(STDIO_TEST, printf_ssize_t) { |
Elliott Hughes | e255642 | 2013-02-28 10:51:14 -0800 | [diff] [blame] | 280 | // http://b/8253769 |
Elliott Hughes | e255642 | 2013-02-28 10:51:14 -0800 | [diff] [blame] | 281 | ASSERT_EQ(sizeof(ssize_t), sizeof(long int)); |
Elliott Hughes | b6e2248 | 2013-03-08 15:28:52 -0800 | [diff] [blame] | 282 | ASSERT_EQ(sizeof(ssize_t), sizeof(size_t)); |
| 283 | // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying: |
Thorsten Glaser | c641caf | 2013-02-17 16:50:58 +0000 | [diff] [blame] | 284 | // error: format '%zd' expects argument of type 'signed size_t', |
| 285 | // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format] |
| 286 | ssize_t v = 1; |
| 287 | char buf[32]; |
| 288 | snprintf(buf, sizeof(buf), "%zd", v); |
| 289 | } |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 290 | |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 291 | // https://code.google.com/p/android/issues/detail?id=64886 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 292 | TEST(STDIO_TEST, snprintf_a) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 293 | char buf[BUFSIZ]; |
| 294 | EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235)); |
| 295 | EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf); |
| 296 | } |
| 297 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 298 | TEST(STDIO_TEST, snprintf_lc) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 299 | char buf[BUFSIZ]; |
| 300 | wint_t wc = L'a'; |
| 301 | EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc)); |
| 302 | EXPECT_STREQ("<a>", buf); |
| 303 | } |
| 304 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 305 | TEST(STDIO_TEST, snprintf_ls) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 306 | char buf[BUFSIZ]; |
| 307 | wchar_t* ws = NULL; |
| 308 | EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws)); |
| 309 | EXPECT_STREQ("<(null)>", buf); |
| 310 | |
| 311 | wchar_t chars[] = { L'h', L'i', 0 }; |
| 312 | ws = chars; |
| 313 | EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws)); |
| 314 | EXPECT_STREQ("<hi>", buf); |
| 315 | } |
| 316 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 317 | TEST(STDIO_TEST, snprintf_n) { |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 318 | #if defined(__BIONIC__) |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 319 | // http://b/14492135 |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 320 | char buf[32]; |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 321 | int i = 1234; |
| 322 | EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i)); |
| 323 | EXPECT_EQ(1234, i); |
| 324 | EXPECT_STREQ("a n b", buf); |
| 325 | #else |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 326 | GTEST_LOG_(INFO) << "This test does nothing on glibc.\n"; |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 327 | #endif |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 328 | } |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 329 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 330 | TEST(STDIO_TEST, snprintf_smoke) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 331 | char buf[BUFSIZ]; |
| 332 | |
| 333 | snprintf(buf, sizeof(buf), "a"); |
| 334 | EXPECT_STREQ("a", buf); |
| 335 | |
| 336 | snprintf(buf, sizeof(buf), "%%"); |
| 337 | EXPECT_STREQ("%", buf); |
| 338 | |
| 339 | snprintf(buf, sizeof(buf), "01234"); |
| 340 | EXPECT_STREQ("01234", buf); |
| 341 | |
| 342 | snprintf(buf, sizeof(buf), "a%sb", "01234"); |
| 343 | EXPECT_STREQ("a01234b", buf); |
| 344 | |
| 345 | char* s = NULL; |
| 346 | snprintf(buf, sizeof(buf), "a%sb", s); |
| 347 | EXPECT_STREQ("a(null)b", buf); |
| 348 | |
| 349 | snprintf(buf, sizeof(buf), "aa%scc", "bb"); |
| 350 | EXPECT_STREQ("aabbcc", buf); |
| 351 | |
| 352 | snprintf(buf, sizeof(buf), "a%cc", 'b'); |
| 353 | EXPECT_STREQ("abc", buf); |
| 354 | |
| 355 | snprintf(buf, sizeof(buf), "a%db", 1234); |
| 356 | EXPECT_STREQ("a1234b", buf); |
| 357 | |
| 358 | snprintf(buf, sizeof(buf), "a%db", -8123); |
| 359 | EXPECT_STREQ("a-8123b", buf); |
| 360 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 361 | snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010)); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 362 | EXPECT_STREQ("a16b", buf); |
| 363 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 364 | snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10)); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 365 | EXPECT_STREQ("a16b", buf); |
| 366 | |
| 367 | snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL); |
| 368 | EXPECT_STREQ("a68719476736b", buf); |
| 369 | |
| 370 | snprintf(buf, sizeof(buf), "a%ldb", 70000L); |
| 371 | EXPECT_STREQ("a70000b", buf); |
| 372 | |
| 373 | snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234)); |
| 374 | EXPECT_STREQ("a0xb0001234b", buf); |
| 375 | |
| 376 | snprintf(buf, sizeof(buf), "a%xz", 0x12ab); |
| 377 | EXPECT_STREQ("a12abz", buf); |
| 378 | |
| 379 | snprintf(buf, sizeof(buf), "a%Xz", 0x12ab); |
| 380 | EXPECT_STREQ("a12ABz", buf); |
| 381 | |
| 382 | snprintf(buf, sizeof(buf), "a%08xz", 0x123456); |
| 383 | EXPECT_STREQ("a00123456z", buf); |
| 384 | |
| 385 | snprintf(buf, sizeof(buf), "a%5dz", 1234); |
| 386 | EXPECT_STREQ("a 1234z", buf); |
| 387 | |
| 388 | snprintf(buf, sizeof(buf), "a%05dz", 1234); |
| 389 | EXPECT_STREQ("a01234z", buf); |
| 390 | |
| 391 | snprintf(buf, sizeof(buf), "a%8dz", 1234); |
| 392 | EXPECT_STREQ("a 1234z", buf); |
| 393 | |
| 394 | snprintf(buf, sizeof(buf), "a%-8dz", 1234); |
| 395 | EXPECT_STREQ("a1234 z", buf); |
| 396 | |
| 397 | snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef"); |
| 398 | EXPECT_STREQ("Aabcdef Z", buf); |
| 399 | |
| 400 | snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234); |
| 401 | EXPECT_STREQ("Ahello:1234Z", buf); |
| 402 | |
| 403 | snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5); |
| 404 | EXPECT_STREQ("a005:5:05z", buf); |
| 405 | |
| 406 | void* p = NULL; |
| 407 | snprintf(buf, sizeof(buf), "a%d,%pz", 5, p); |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 408 | #if defined(__BIONIC__) |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 409 | EXPECT_STREQ("a5,0x0z", buf); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 410 | #else // __BIONIC__ |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 411 | EXPECT_STREQ("a5,(nil)z", buf); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 412 | #endif // __BIONIC__ |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 413 | |
| 414 | snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8); |
| 415 | EXPECT_STREQ("a68719476736,6,7,8z", buf); |
| 416 | |
| 417 | snprintf(buf, sizeof(buf), "a_%f_b", 1.23f); |
| 418 | EXPECT_STREQ("a_1.230000_b", buf); |
| 419 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 420 | snprintf(buf, sizeof(buf), "a_%g_b", 3.14); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 421 | EXPECT_STREQ("a_3.14_b", buf); |
Alexander Ivchenko | edd7c2e | 2014-04-01 17:01:39 +0400 | [diff] [blame] | 422 | |
| 423 | snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice"); |
| 424 | EXPECT_STREQ("print_me_twice print_me_twice", buf); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 427 | template <typename T> |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 428 | static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...), |
| 429 | int sscanf_fn(const T*, const T*, ...), |
| 430 | const T* fmt_string, const T* fmt, const T* fmt_plus, |
| 431 | const T* minus_inf, const T* inf_, const T* plus_inf, |
| 432 | const T* minus_nan, const T* nan_, const T* plus_nan) { |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 433 | T buf[BUFSIZ]; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 434 | float f; |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 435 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 436 | // NaN. |
| 437 | |
| 438 | snprintf_fn(buf, sizeof(buf), fmt, nanf("")); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 439 | EXPECT_STREQ(nan_, buf) << fmt; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 440 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 441 | EXPECT_TRUE(isnan(f)); |
| 442 | |
| 443 | snprintf_fn(buf, sizeof(buf), fmt, -nanf("")); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 444 | EXPECT_STREQ(minus_nan, buf) << fmt; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 445 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 446 | EXPECT_TRUE(isnan(f)); |
| 447 | |
| 448 | snprintf_fn(buf, sizeof(buf), fmt_plus, nanf("")); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 449 | EXPECT_STREQ(plus_nan, buf) << fmt_plus; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 450 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 451 | EXPECT_TRUE(isnan(f)); |
| 452 | |
| 453 | snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf("")); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 454 | EXPECT_STREQ(minus_nan, buf) << fmt_plus; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 455 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 456 | EXPECT_TRUE(isnan(f)); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 457 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 458 | // Inf. |
| 459 | |
| 460 | snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 461 | EXPECT_STREQ(inf_, buf) << fmt; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 462 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 463 | EXPECT_EQ(HUGE_VALF, f); |
| 464 | |
| 465 | snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 466 | EXPECT_STREQ(minus_inf, buf) << fmt; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 467 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 468 | EXPECT_EQ(-HUGE_VALF, f); |
| 469 | |
| 470 | snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 471 | EXPECT_STREQ(plus_inf, buf) << fmt_plus; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 472 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 473 | EXPECT_EQ(HUGE_VALF, f); |
| 474 | |
| 475 | snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 476 | EXPECT_STREQ(minus_inf, buf) << fmt_plus; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 477 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 478 | EXPECT_EQ(-HUGE_VALF, f); |
| 479 | |
| 480 | // Check case-insensitivity. |
| 481 | snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]"); |
| 482 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf; |
| 483 | EXPECT_EQ(HUGE_VALF, f); |
| 484 | snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]"); |
| 485 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf; |
| 486 | EXPECT_TRUE(isnan(f)); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 489 | TEST(STDIO_TEST, snprintf_sscanf_inf_nan) { |
| 490 | CheckInfNan(snprintf, sscanf, "%s", |
| 491 | "[%a]", "[%+a]", |
| 492 | "[-inf]", "[inf]", "[+inf]", |
| 493 | "[-nan]", "[nan]", "[+nan]"); |
| 494 | CheckInfNan(snprintf, sscanf, "%s", |
| 495 | "[%A]", "[%+A]", |
| 496 | "[-INF]", "[INF]", "[+INF]", |
| 497 | "[-NAN]", "[NAN]", "[+NAN]"); |
| 498 | CheckInfNan(snprintf, sscanf, "%s", |
| 499 | "[%e]", "[%+e]", |
| 500 | "[-inf]", "[inf]", "[+inf]", |
| 501 | "[-nan]", "[nan]", "[+nan]"); |
| 502 | CheckInfNan(snprintf, sscanf, "%s", |
| 503 | "[%E]", "[%+E]", |
| 504 | "[-INF]", "[INF]", "[+INF]", |
| 505 | "[-NAN]", "[NAN]", "[+NAN]"); |
| 506 | CheckInfNan(snprintf, sscanf, "%s", |
| 507 | "[%f]", "[%+f]", |
| 508 | "[-inf]", "[inf]", "[+inf]", |
| 509 | "[-nan]", "[nan]", "[+nan]"); |
| 510 | CheckInfNan(snprintf, sscanf, "%s", |
| 511 | "[%F]", "[%+F]", |
| 512 | "[-INF]", "[INF]", "[+INF]", |
| 513 | "[-NAN]", "[NAN]", "[+NAN]"); |
| 514 | CheckInfNan(snprintf, sscanf, "%s", |
| 515 | "[%g]", "[%+g]", |
| 516 | "[-inf]", "[inf]", "[+inf]", |
| 517 | "[-nan]", "[nan]", "[+nan]"); |
| 518 | CheckInfNan(snprintf, sscanf, "%s", |
| 519 | "[%G]", "[%+G]", |
| 520 | "[-INF]", "[INF]", "[+INF]", |
| 521 | "[-NAN]", "[NAN]", "[+NAN]"); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 522 | } |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 523 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 524 | TEST(STDIO_TEST, swprintf_swscanf_inf_nan) { |
| 525 | CheckInfNan(swprintf, swscanf, L"%s", |
| 526 | L"[%a]", L"[%+a]", |
| 527 | L"[-inf]", L"[inf]", L"[+inf]", |
| 528 | L"[-nan]", L"[nan]", L"[+nan]"); |
| 529 | CheckInfNan(swprintf, swscanf, L"%s", |
| 530 | L"[%A]", L"[%+A]", |
| 531 | L"[-INF]", L"[INF]", L"[+INF]", |
| 532 | L"[-NAN]", L"[NAN]", L"[+NAN]"); |
| 533 | CheckInfNan(swprintf, swscanf, L"%s", |
| 534 | L"[%e]", L"[%+e]", |
| 535 | L"[-inf]", L"[inf]", L"[+inf]", |
| 536 | L"[-nan]", L"[nan]", L"[+nan]"); |
| 537 | CheckInfNan(swprintf, swscanf, L"%s", |
| 538 | L"[%E]", L"[%+E]", |
| 539 | L"[-INF]", L"[INF]", L"[+INF]", |
| 540 | L"[-NAN]", L"[NAN]", L"[+NAN]"); |
| 541 | CheckInfNan(swprintf, swscanf, L"%s", |
| 542 | L"[%f]", L"[%+f]", |
| 543 | L"[-inf]", L"[inf]", L"[+inf]", |
| 544 | L"[-nan]", L"[nan]", L"[+nan]"); |
| 545 | CheckInfNan(swprintf, swscanf, L"%s", |
| 546 | L"[%F]", L"[%+F]", |
| 547 | L"[-INF]", L"[INF]", L"[+INF]", |
| 548 | L"[-NAN]", L"[NAN]", L"[+NAN]"); |
| 549 | CheckInfNan(swprintf, swscanf, L"%s", |
| 550 | L"[%g]", L"[%+g]", |
| 551 | L"[-inf]", L"[inf]", L"[+inf]", |
| 552 | L"[-nan]", L"[nan]", L"[+nan]"); |
| 553 | CheckInfNan(swprintf, swscanf, L"%s", |
| 554 | L"[%G]", L"[%+G]", |
| 555 | L"[-INF]", L"[INF]", L"[+INF]", |
| 556 | L"[-NAN]", L"[NAN]", L"[+NAN]"); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 559 | TEST(STDIO_TEST, snprintf_d_INT_MAX) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 560 | char buf[BUFSIZ]; |
| 561 | snprintf(buf, sizeof(buf), "%d", INT_MAX); |
| 562 | EXPECT_STREQ("2147483647", buf); |
| 563 | } |
| 564 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 565 | TEST(STDIO_TEST, snprintf_d_INT_MIN) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 566 | char buf[BUFSIZ]; |
| 567 | snprintf(buf, sizeof(buf), "%d", INT_MIN); |
| 568 | EXPECT_STREQ("-2147483648", buf); |
| 569 | } |
| 570 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 571 | TEST(STDIO_TEST, snprintf_ld_LONG_MAX) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 572 | char buf[BUFSIZ]; |
| 573 | snprintf(buf, sizeof(buf), "%ld", LONG_MAX); |
Josh Gao | b36efa4 | 2016-09-15 13:55:41 -0700 | [diff] [blame] | 574 | #if defined(__LP64__) |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 575 | EXPECT_STREQ("9223372036854775807", buf); |
| 576 | #else |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 577 | EXPECT_STREQ("2147483647", buf); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 578 | #endif |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 581 | TEST(STDIO_TEST, snprintf_ld_LONG_MIN) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 582 | char buf[BUFSIZ]; |
| 583 | snprintf(buf, sizeof(buf), "%ld", LONG_MIN); |
Josh Gao | b36efa4 | 2016-09-15 13:55:41 -0700 | [diff] [blame] | 584 | #if defined(__LP64__) |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 585 | EXPECT_STREQ("-9223372036854775808", buf); |
| 586 | #else |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 587 | EXPECT_STREQ("-2147483648", buf); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 588 | #endif |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 591 | TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 592 | char buf[BUFSIZ]; |
| 593 | snprintf(buf, sizeof(buf), "%lld", LLONG_MAX); |
| 594 | EXPECT_STREQ("9223372036854775807", buf); |
| 595 | } |
| 596 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 597 | TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 598 | char buf[BUFSIZ]; |
| 599 | snprintf(buf, sizeof(buf), "%lld", LLONG_MIN); |
| 600 | EXPECT_STREQ("-9223372036854775808", buf); |
| 601 | } |
| 602 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 603 | TEST(STDIO_TEST, snprintf_e) { |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 604 | char buf[BUFSIZ]; |
| 605 | |
| 606 | snprintf(buf, sizeof(buf), "%e", 1.5); |
| 607 | EXPECT_STREQ("1.500000e+00", buf); |
| 608 | |
| 609 | snprintf(buf, sizeof(buf), "%Le", 1.5l); |
| 610 | EXPECT_STREQ("1.500000e+00", buf); |
| 611 | } |
| 612 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 613 | TEST(STDIO_TEST, snprintf_negative_zero_5084292) { |
Elliott Hughes | e77f38f | 2014-05-14 12:39:12 -0700 | [diff] [blame] | 614 | char buf[BUFSIZ]; |
| 615 | |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 616 | snprintf(buf, sizeof(buf), "%e", -0.0); |
| 617 | EXPECT_STREQ("-0.000000e+00", buf); |
| 618 | snprintf(buf, sizeof(buf), "%E", -0.0); |
| 619 | EXPECT_STREQ("-0.000000E+00", buf); |
Elliott Hughes | e77f38f | 2014-05-14 12:39:12 -0700 | [diff] [blame] | 620 | snprintf(buf, sizeof(buf), "%f", -0.0); |
| 621 | EXPECT_STREQ("-0.000000", buf); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 622 | snprintf(buf, sizeof(buf), "%F", -0.0); |
| 623 | EXPECT_STREQ("-0.000000", buf); |
| 624 | snprintf(buf, sizeof(buf), "%g", -0.0); |
| 625 | EXPECT_STREQ("-0", buf); |
| 626 | snprintf(buf, sizeof(buf), "%G", -0.0); |
| 627 | EXPECT_STREQ("-0", buf); |
| 628 | snprintf(buf, sizeof(buf), "%a", -0.0); |
| 629 | EXPECT_STREQ("-0x0p+0", buf); |
| 630 | snprintf(buf, sizeof(buf), "%A", -0.0); |
| 631 | EXPECT_STREQ("-0X0P+0", buf); |
Elliott Hughes | e77f38f | 2014-05-14 12:39:12 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 634 | TEST(STDIO_TEST, snprintf_utf8_15439554) { |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 635 | locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0); |
Wally Yau | a40fdbd | 2014-08-26 09:47:23 -0700 | [diff] [blame] | 636 | locale_t old_locale = uselocale(cloc); |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 637 | |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 638 | // http://b/15439554 |
| 639 | char buf[BUFSIZ]; |
| 640 | |
| 641 | // 1-byte character. |
| 642 | snprintf(buf, sizeof(buf), "%dx%d", 1, 2); |
| 643 | EXPECT_STREQ("1x2", buf); |
| 644 | // 2-byte character. |
| 645 | snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2); |
| 646 | EXPECT_STREQ("1¢2", buf); |
| 647 | // 3-byte character. |
| 648 | snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2); |
| 649 | EXPECT_STREQ("1€2", buf); |
| 650 | // 4-byte character. |
| 651 | snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2); |
| 652 | EXPECT_STREQ("1ð¤¢2", buf); |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 653 | |
Wally Yau | a40fdbd | 2014-08-26 09:47:23 -0700 | [diff] [blame] | 654 | uselocale(old_locale); |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 655 | freelocale(cloc); |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Elliott Hughes | 43f7c87 | 2016-02-05 11:18:41 -0800 | [diff] [blame] | 658 | static void* snprintf_small_stack_fn(void*) { |
| 659 | // Make life (realistically) hard for ourselves by allocating our own buffer for the result. |
| 660 | char buf[PATH_MAX]; |
| 661 | snprintf(buf, sizeof(buf), "/proc/%d", getpid()); |
| 662 | return nullptr; |
| 663 | } |
| 664 | |
| 665 | TEST(STDIO_TEST, snprintf_small_stack) { |
| 666 | // Is it safe to call snprintf on a thread with a small stack? |
| 667 | // (The snprintf implementation puts some pretty large buffers on the stack.) |
| 668 | pthread_attr_t a; |
| 669 | ASSERT_EQ(0, pthread_attr_init(&a)); |
| 670 | ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN)); |
| 671 | |
| 672 | pthread_t t; |
| 673 | ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr)); |
| 674 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 675 | } |
| 676 | |
Elliott Hughes | 8200e55 | 2016-02-05 21:57:37 -0800 | [diff] [blame] | 677 | TEST(STDIO_TEST, snprintf_asterisk_overflow) { |
| 678 | char buf[128]; |
| 679 | ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!')); |
| 680 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!')); |
| 681 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!')); |
| 682 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!')); |
| 683 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!')); |
| 684 | |
| 685 | // INT_MAX-1, INT_MAX, INT_MAX+1. |
| 686 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!')); |
| 687 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!')); |
| 688 | ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!')); |
| 689 | ASSERT_EQ(ENOMEM, errno); |
| 690 | } |
| 691 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 692 | TEST(STDIO_TEST, fprintf) { |
| 693 | TemporaryFile tf; |
| 694 | |
| 695 | FILE* tfile = fdopen(tf.fd, "r+"); |
| 696 | ASSERT_TRUE(tfile != nullptr); |
| 697 | |
| 698 | ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc")); |
| 699 | AssertFileIs(tfile, "123 abc"); |
| 700 | fclose(tfile); |
| 701 | } |
| 702 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 703 | TEST(STDIO_TEST, fprintf_failures_7229520) { |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 704 | // http://b/7229520 |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 705 | FILE* fp; |
| 706 | |
| 707 | // Unbuffered case where the fprintf(3) itself fails. |
| 708 | ASSERT_NE(nullptr, fp = tmpfile()); |
| 709 | setbuf(fp, NULL); |
| 710 | ASSERT_EQ(4, fprintf(fp, "epic")); |
| 711 | ASSERT_EQ(0, close(fileno(fp))); |
| 712 | ASSERT_EQ(-1, fprintf(fp, "fail")); |
| 713 | ASSERT_EQ(-1, fclose(fp)); |
| 714 | |
| 715 | // Buffered case where we won't notice until the fclose(3). |
| 716 | // It's likely this is what was actually seen in http://b/7229520, |
| 717 | // and that expecting fprintf to fail is setting yourself up for |
| 718 | // disappointment. Remember to check fclose(3)'s return value, kids! |
| 719 | ASSERT_NE(nullptr, fp = tmpfile()); |
| 720 | ASSERT_EQ(4, fprintf(fp, "epic")); |
| 721 | ASSERT_EQ(0, close(fileno(fp))); |
| 722 | ASSERT_EQ(4, fprintf(fp, "fail")); |
| 723 | ASSERT_EQ(-1, fclose(fp)); |
| 724 | } |
| 725 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 726 | TEST(STDIO_TEST, popen) { |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 727 | FILE* fp = popen("cat /proc/version", "r"); |
| 728 | ASSERT_TRUE(fp != NULL); |
| 729 | |
| 730 | char buf[16]; |
| 731 | char* s = fgets(buf, sizeof(buf), fp); |
| 732 | buf[13] = '\0'; |
| 733 | ASSERT_STREQ("Linux version", s); |
| 734 | |
| 735 | ASSERT_EQ(0, pclose(fp)); |
| 736 | } |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 737 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 738 | TEST(STDIO_TEST, getc) { |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 739 | FILE* fp = fopen("/proc/version", "r"); |
| 740 | ASSERT_TRUE(fp != NULL); |
| 741 | ASSERT_EQ('L', getc(fp)); |
| 742 | ASSERT_EQ('i', getc(fp)); |
| 743 | ASSERT_EQ('n', getc(fp)); |
| 744 | ASSERT_EQ('u', getc(fp)); |
| 745 | ASSERT_EQ('x', getc(fp)); |
| 746 | fclose(fp); |
| 747 | } |
| 748 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 749 | TEST(STDIO_TEST, putc) { |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 750 | FILE* fp = fopen("/proc/version", "r"); |
| 751 | ASSERT_TRUE(fp != NULL); |
| 752 | ASSERT_EQ(EOF, putc('x', fp)); |
| 753 | fclose(fp); |
| 754 | } |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 755 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 756 | TEST(STDIO_TEST, sscanf_swscanf) { |
| 757 | struct stuff { |
| 758 | char s1[123]; |
| 759 | int i1; |
| 760 | double d1; |
| 761 | float f1; |
| 762 | char s2[123]; |
| 763 | |
| 764 | void Check() { |
| 765 | ASSERT_STREQ("hello", s1); |
| 766 | ASSERT_EQ(123, i1); |
| 767 | ASSERT_DOUBLE_EQ(1.23, d1); |
| 768 | ASSERT_FLOAT_EQ(9.0f, f1); |
| 769 | ASSERT_STREQ("world", s2); |
| 770 | } |
| 771 | } s; |
| 772 | |
| 773 | memset(&s, 0, sizeof(s)); |
| 774 | ASSERT_EQ(5, sscanf(" hello 123 1.23 0x1.2p3 world", |
| 775 | "%s %i %lf %f %s", |
| 776 | s.s1, &s.i1, &s.d1, &s.f1, s.s2)); |
| 777 | s.Check(); |
| 778 | |
| 779 | memset(&s, 0, sizeof(s)); |
| 780 | ASSERT_EQ(5, swscanf(L" hello 123 1.23 0x1.2p3 world", |
| 781 | L"%s %i %lf %f %s", |
| 782 | s.s1, &s.i1, &s.d1, &s.f1, s.s2)); |
| 783 | s.Check(); |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 784 | } |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 785 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 786 | TEST(STDIO_TEST, cantwrite_EBADF) { |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 787 | // If we open a file read-only... |
| 788 | FILE* fp = fopen("/proc/version", "r"); |
| 789 | |
| 790 | // ...all attempts to write to that file should return failure. |
| 791 | |
| 792 | // They should also set errno to EBADF. This isn't POSIX, but it's traditional. |
| 793 | // glibc gets the wide-character functions wrong. |
| 794 | |
| 795 | errno = 0; |
| 796 | EXPECT_EQ(EOF, putc('x', fp)); |
| 797 | EXPECT_EQ(EBADF, errno); |
| 798 | |
| 799 | errno = 0; |
| 800 | EXPECT_EQ(EOF, fprintf(fp, "hello")); |
| 801 | EXPECT_EQ(EBADF, errno); |
| 802 | |
| 803 | errno = 0; |
| 804 | EXPECT_EQ(EOF, fwprintf(fp, L"hello")); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 805 | #if defined(__BIONIC__) |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 806 | EXPECT_EQ(EBADF, errno); |
| 807 | #endif |
| 808 | |
| 809 | errno = 0; |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 810 | EXPECT_EQ(0U, fwrite("hello", 1, 2, fp)); |
| 811 | EXPECT_EQ(EBADF, errno); |
| 812 | |
| 813 | errno = 0; |
| 814 | EXPECT_EQ(EOF, fputs("hello", fp)); |
| 815 | EXPECT_EQ(EBADF, errno); |
| 816 | |
| 817 | errno = 0; |
| 818 | EXPECT_EQ(WEOF, fputwc(L'x', fp)); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 819 | #if defined(__BIONIC__) |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 820 | EXPECT_EQ(EBADF, errno); |
| 821 | #endif |
| 822 | } |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 823 | |
| 824 | // Tests that we can only have a consistent and correct fpos_t when using |
| 825 | // 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] | 826 | TEST(STDIO_TEST, consistent_fpos_t) { |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 827 | ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8")); |
| 828 | uselocale(LC_GLOBAL_LOCALE); |
| 829 | |
| 830 | FILE* fp = tmpfile(); |
| 831 | ASSERT_TRUE(fp != NULL); |
| 832 | |
| 833 | wchar_t mb_one_bytes = L'h'; |
| 834 | wchar_t mb_two_bytes = 0x00a2; |
| 835 | wchar_t mb_three_bytes = 0x20ac; |
| 836 | wchar_t mb_four_bytes = 0x24b62; |
| 837 | |
| 838 | // Write to file. |
| 839 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp))); |
| 840 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp))); |
| 841 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp))); |
| 842 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp))); |
| 843 | |
| 844 | rewind(fp); |
| 845 | |
| 846 | // Record each character position. |
| 847 | fpos_t pos1; |
| 848 | fpos_t pos2; |
| 849 | fpos_t pos3; |
| 850 | fpos_t pos4; |
| 851 | fpos_t pos5; |
| 852 | EXPECT_EQ(0, fgetpos(fp, &pos1)); |
| 853 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 854 | EXPECT_EQ(0, fgetpos(fp, &pos2)); |
| 855 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 856 | EXPECT_EQ(0, fgetpos(fp, &pos3)); |
| 857 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 858 | EXPECT_EQ(0, fgetpos(fp, &pos4)); |
| 859 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 860 | EXPECT_EQ(0, fgetpos(fp, &pos5)); |
| 861 | |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 862 | #if defined(__BIONIC__) |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 863 | // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD |
| 864 | // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In |
| 865 | // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE |
| 866 | // structure. |
| 867 | ASSERT_EQ(0, static_cast<off_t>(pos1)); |
| 868 | ASSERT_EQ(1, static_cast<off_t>(pos2)); |
| 869 | ASSERT_EQ(3, static_cast<off_t>(pos3)); |
| 870 | ASSERT_EQ(6, static_cast<off_t>(pos4)); |
| 871 | ASSERT_EQ(10, static_cast<off_t>(pos5)); |
| 872 | #endif |
| 873 | |
| 874 | // Exercise back and forth movements of the position. |
| 875 | ASSERT_EQ(0, fsetpos(fp, &pos2)); |
| 876 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 877 | ASSERT_EQ(0, fsetpos(fp, &pos1)); |
| 878 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 879 | ASSERT_EQ(0, fsetpos(fp, &pos4)); |
| 880 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 881 | ASSERT_EQ(0, fsetpos(fp, &pos3)); |
| 882 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 883 | ASSERT_EQ(0, fsetpos(fp, &pos5)); |
| 884 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 885 | |
| 886 | fclose(fp); |
| 887 | } |
| 888 | |
| 889 | // Exercise the interaction between fpos and seek. |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 890 | TEST(STDIO_TEST, fpos_t_and_seek) { |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 891 | ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8")); |
| 892 | uselocale(LC_GLOBAL_LOCALE); |
| 893 | |
Calin Juravle | 9b95ea9 | 2014-05-14 17:07:10 +0100 | [diff] [blame] | 894 | // In glibc-2.16 fseek doesn't work properly in wide mode |
| 895 | // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is |
| 896 | // to close and re-open the file. We do it in order to make the test pass |
| 897 | // with all glibcs. |
| 898 | |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 899 | TemporaryFile tf; |
| 900 | FILE* fp = fdopen(tf.fd, "w+"); |
| 901 | ASSERT_TRUE(fp != NULL); |
| 902 | |
| 903 | wchar_t mb_two_bytes = 0x00a2; |
| 904 | wchar_t mb_three_bytes = 0x20ac; |
| 905 | wchar_t mb_four_bytes = 0x24b62; |
| 906 | |
| 907 | // Write to file. |
| 908 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp))); |
| 909 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp))); |
| 910 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp))); |
| 911 | |
| 912 | fflush(fp); |
| 913 | fclose(fp); |
| 914 | |
| 915 | fp = fopen(tf.filename, "r"); |
| 916 | ASSERT_TRUE(fp != NULL); |
| 917 | |
| 918 | // Store a valid position. |
| 919 | fpos_t mb_two_bytes_pos; |
| 920 | ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos)); |
| 921 | |
| 922 | // Move inside mb_four_bytes with fseek. |
| 923 | long offset_inside_mb = 6; |
| 924 | ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET)); |
| 925 | |
| 926 | // Store the "inside multi byte" position. |
| 927 | fpos_t pos_inside_mb; |
| 928 | ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb)); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 929 | #if defined(__BIONIC__) |
| 930 | ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb)); |
| 931 | #endif |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 932 | |
| 933 | // Reading from within a byte should produce an error. |
| 934 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 935 | ASSERT_EQ(EILSEQ, errno); |
| 936 | |
| 937 | // Reverting to a valid position should work. |
| 938 | ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos)); |
| 939 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 940 | |
| 941 | // Moving withing a multi byte with fsetpos should work but reading should |
| 942 | // produce an error. |
| 943 | ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb)); |
| 944 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 945 | ASSERT_EQ(EILSEQ, errno); |
| 946 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 947 | ASSERT_EQ(0, fclose(fp)); |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 948 | } |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 949 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 950 | TEST(STDIO_TEST, fmemopen) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 951 | char buf[16]; |
| 952 | memset(buf, 0, sizeof(buf)); |
| 953 | FILE* fp = fmemopen(buf, sizeof(buf), "r+"); |
| 954 | ASSERT_EQ('<', fputc('<', fp)); |
| 955 | ASSERT_NE(EOF, fputs("abc>\n", fp)); |
| 956 | fflush(fp); |
| 957 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 958 | // We wrote to the buffer... |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 959 | ASSERT_STREQ("<abc>\n", buf); |
| 960 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 961 | // And can read back from the file. |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 962 | AssertFileIs(fp, "<abc>\n", true); |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 963 | ASSERT_EQ(0, fclose(fp)); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 966 | TEST(STDIO_TEST, fmemopen_nullptr) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 967 | FILE* fp = fmemopen(nullptr, 128, "r+"); |
| 968 | ASSERT_NE(EOF, fputs("xyz\n", fp)); |
| 969 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 970 | AssertFileIs(fp, "xyz\n", true); |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 971 | ASSERT_EQ(0, fclose(fp)); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 974 | TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) { |
| 975 | FILE* fp; |
| 976 | char buf[8]; |
| 977 | |
| 978 | // POSIX: "When a stream open for writing is flushed or closed, a null byte |
| 979 | // shall be written at the current position or at the end of the buffer, |
| 980 | // depending on the size of the contents." |
| 981 | memset(buf, 'x', sizeof(buf)); |
| 982 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w")); |
| 983 | // Even with nothing written (and not in truncate mode), we'll flush a NUL... |
| 984 | ASSERT_EQ(0, fflush(fp)); |
| 985 | EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf))); |
| 986 | // Now write and check that the NUL moves along with our writes... |
| 987 | ASSERT_NE(EOF, fputs("hello", fp)); |
| 988 | ASSERT_EQ(0, fflush(fp)); |
| 989 | EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf))); |
| 990 | ASSERT_NE(EOF, fputs("wo", fp)); |
| 991 | ASSERT_EQ(0, fflush(fp)); |
| 992 | EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf))); |
| 993 | ASSERT_EQ(0, fclose(fp)); |
| 994 | |
| 995 | // "If a stream open for update is flushed or closed and the last write has |
| 996 | // advanced the current buffer size, a null byte shall be written at the end |
| 997 | // of the buffer if it fits." |
| 998 | memset(buf, 'x', sizeof(buf)); |
| 999 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+")); |
| 1000 | // Nothing written yet, so no advance... |
| 1001 | ASSERT_EQ(0, fflush(fp)); |
| 1002 | EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf))); |
| 1003 | ASSERT_NE(EOF, fputs("hello", fp)); |
| 1004 | ASSERT_EQ(0, fclose(fp)); |
| 1005 | } |
| 1006 | |
| 1007 | TEST(STDIO_TEST, fmemopen_size) { |
| 1008 | FILE* fp; |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1009 | char buf[16]; |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1010 | memset(buf, 'x', sizeof(buf)); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1011 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1012 | // POSIX: "The stream shall also maintain the size of the current buffer |
| 1013 | // contents; use of fseek() or fseeko() on the stream with SEEK_END shall |
| 1014 | // seek relative to this size." |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1015 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1016 | // "For modes r and r+ the size shall be set to the value given by the size |
| 1017 | // argument." |
| 1018 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r")); |
| 1019 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1020 | EXPECT_EQ(16, ftell(fp)); |
| 1021 | EXPECT_EQ(16, ftello(fp)); |
| 1022 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1023 | EXPECT_EQ(16, ftell(fp)); |
| 1024 | EXPECT_EQ(16, ftello(fp)); |
| 1025 | ASSERT_EQ(0, fclose(fp)); |
| 1026 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+")); |
| 1027 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1028 | EXPECT_EQ(16, ftell(fp)); |
| 1029 | EXPECT_EQ(16, ftello(fp)); |
| 1030 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1031 | EXPECT_EQ(16, ftell(fp)); |
| 1032 | EXPECT_EQ(16, ftello(fp)); |
| 1033 | ASSERT_EQ(0, fclose(fp)); |
| 1034 | |
| 1035 | // "For modes w and w+ the initial size shall be zero..." |
| 1036 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w")); |
| 1037 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1038 | EXPECT_EQ(0, ftell(fp)); |
| 1039 | EXPECT_EQ(0, ftello(fp)); |
| 1040 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1041 | EXPECT_EQ(0, ftell(fp)); |
| 1042 | EXPECT_EQ(0, ftello(fp)); |
| 1043 | ASSERT_EQ(0, fclose(fp)); |
| 1044 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+")); |
| 1045 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1046 | EXPECT_EQ(0, ftell(fp)); |
| 1047 | EXPECT_EQ(0, ftello(fp)); |
| 1048 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1049 | EXPECT_EQ(0, ftell(fp)); |
| 1050 | EXPECT_EQ(0, ftello(fp)); |
| 1051 | ASSERT_EQ(0, fclose(fp)); |
| 1052 | |
| 1053 | // "...and for modes a and a+ the initial size shall be: |
| 1054 | // 1. Zero, if buf is a null pointer |
| 1055 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a")); |
| 1056 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1057 | EXPECT_EQ(0, ftell(fp)); |
| 1058 | EXPECT_EQ(0, ftello(fp)); |
| 1059 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1060 | EXPECT_EQ(0, ftell(fp)); |
| 1061 | EXPECT_EQ(0, ftello(fp)); |
| 1062 | ASSERT_EQ(0, fclose(fp)); |
| 1063 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+")); |
| 1064 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1065 | EXPECT_EQ(0, ftell(fp)); |
| 1066 | EXPECT_EQ(0, ftello(fp)); |
| 1067 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1068 | EXPECT_EQ(0, ftell(fp)); |
| 1069 | EXPECT_EQ(0, ftello(fp)); |
| 1070 | ASSERT_EQ(0, fclose(fp)); |
| 1071 | |
| 1072 | // 2. The position of the first null byte in the buffer, if one is found |
| 1073 | memset(buf, 'x', sizeof(buf)); |
| 1074 | buf[3] = '\0'; |
| 1075 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a")); |
| 1076 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1077 | EXPECT_EQ(3, ftell(fp)); |
| 1078 | EXPECT_EQ(3, ftello(fp)); |
| 1079 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1080 | EXPECT_EQ(3, ftell(fp)); |
| 1081 | EXPECT_EQ(3, ftello(fp)); |
| 1082 | ASSERT_EQ(0, fclose(fp)); |
| 1083 | memset(buf, 'x', sizeof(buf)); |
| 1084 | buf[3] = '\0'; |
| 1085 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+")); |
| 1086 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1087 | EXPECT_EQ(3, ftell(fp)); |
| 1088 | EXPECT_EQ(3, ftello(fp)); |
| 1089 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1090 | EXPECT_EQ(3, ftell(fp)); |
| 1091 | EXPECT_EQ(3, ftello(fp)); |
| 1092 | ASSERT_EQ(0, fclose(fp)); |
| 1093 | |
| 1094 | // 3. The value of the size argument, if buf is not a null pointer and no |
| 1095 | // null byte is found. |
| 1096 | memset(buf, 'x', sizeof(buf)); |
| 1097 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a")); |
| 1098 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1099 | EXPECT_EQ(16, ftell(fp)); |
| 1100 | EXPECT_EQ(16, ftello(fp)); |
| 1101 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1102 | EXPECT_EQ(16, ftell(fp)); |
| 1103 | EXPECT_EQ(16, ftello(fp)); |
| 1104 | ASSERT_EQ(0, fclose(fp)); |
| 1105 | memset(buf, 'x', sizeof(buf)); |
| 1106 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+")); |
| 1107 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1108 | EXPECT_EQ(16, ftell(fp)); |
| 1109 | EXPECT_EQ(16, ftello(fp)); |
| 1110 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1111 | EXPECT_EQ(16, ftell(fp)); |
| 1112 | EXPECT_EQ(16, ftello(fp)); |
| 1113 | ASSERT_EQ(0, fclose(fp)); |
| 1114 | } |
| 1115 | |
| 1116 | TEST(STDIO_TEST, fmemopen_SEEK_END) { |
| 1117 | // fseek SEEK_END is relative to the current string length, not the buffer size. |
| 1118 | FILE* fp; |
| 1119 | char buf[8]; |
| 1120 | memset(buf, 'x', sizeof(buf)); |
| 1121 | strcpy(buf, "str"); |
| 1122 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+")); |
| 1123 | ASSERT_NE(EOF, fputs("string", fp)); |
| 1124 | EXPECT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1125 | EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp)); |
| 1126 | EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp)); |
| 1127 | EXPECT_EQ(0, fclose(fp)); |
| 1128 | |
| 1129 | // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather |
| 1130 | // than adding). |
| 1131 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+")); |
| 1132 | ASSERT_NE(EOF, fputs("54321", fp)); |
| 1133 | EXPECT_EQ(0, fseek(fp, -2, SEEK_END)); |
| 1134 | EXPECT_EQ('2', fgetc(fp)); |
| 1135 | EXPECT_EQ(0, fclose(fp)); |
| 1136 | } |
| 1137 | |
| 1138 | TEST(STDIO_TEST, fmemopen_seek_invalid) { |
| 1139 | char buf[8]; |
| 1140 | memset(buf, 'x', sizeof(buf)); |
| 1141 | FILE* fp = fmemopen(buf, sizeof(buf), "w"); |
| 1142 | ASSERT_TRUE(fp != nullptr); |
| 1143 | |
| 1144 | // POSIX: "An attempt to seek ... to a negative position or to a position |
| 1145 | // larger than the buffer size given in the size argument shall fail." |
| 1146 | // (There's no mention of what errno should be set to, and glibc doesn't |
| 1147 | // set errno in any of these cases.) |
| 1148 | EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET)); |
| 1149 | EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET)); |
| 1150 | EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET)); |
| 1151 | EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET)); |
| 1152 | } |
| 1153 | |
| 1154 | TEST(STDIO_TEST, fmemopen_read_EOF) { |
| 1155 | // POSIX: "A read operation on the stream shall not advance the current |
| 1156 | // buffer position beyond the current buffer size." |
| 1157 | char buf[8]; |
| 1158 | memset(buf, 'x', sizeof(buf)); |
| 1159 | FILE* fp = fmemopen(buf, sizeof(buf), "r"); |
| 1160 | ASSERT_TRUE(fp != nullptr); |
| 1161 | char buf2[BUFSIZ]; |
| 1162 | ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp)); |
| 1163 | // POSIX: "Reaching the buffer size in a read operation shall count as |
| 1164 | // end-of-file. |
| 1165 | ASSERT_TRUE(feof(fp)); |
| 1166 | ASSERT_EQ(EOF, fgetc(fp)); |
| 1167 | ASSERT_EQ(0, fclose(fp)); |
| 1168 | } |
| 1169 | |
| 1170 | TEST(STDIO_TEST, fmemopen_read_null_bytes) { |
| 1171 | // POSIX: "Null bytes in the buffer shall have no special meaning for reads." |
| 1172 | char buf[] = "h\0e\0l\0l\0o"; |
| 1173 | FILE* fp = fmemopen(buf, sizeof(buf), "r"); |
| 1174 | ASSERT_TRUE(fp != nullptr); |
| 1175 | ASSERT_EQ('h', fgetc(fp)); |
| 1176 | ASSERT_EQ(0, fgetc(fp)); |
| 1177 | ASSERT_EQ('e', fgetc(fp)); |
| 1178 | ASSERT_EQ(0, fgetc(fp)); |
| 1179 | ASSERT_EQ('l', fgetc(fp)); |
| 1180 | ASSERT_EQ(0, fgetc(fp)); |
| 1181 | // POSIX: "The read operation shall start at the current buffer position of |
| 1182 | // the stream." |
| 1183 | char buf2[8]; |
| 1184 | memset(buf2, 'x', sizeof(buf2)); |
| 1185 | ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp)); |
| 1186 | ASSERT_EQ('l', buf2[0]); |
| 1187 | ASSERT_EQ(0, buf2[1]); |
| 1188 | ASSERT_EQ('o', buf2[2]); |
| 1189 | ASSERT_EQ(0, buf2[3]); |
| 1190 | for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i; |
| 1191 | ASSERT_TRUE(feof(fp)); |
| 1192 | ASSERT_EQ(0, fclose(fp)); |
| 1193 | } |
| 1194 | |
| 1195 | TEST(STDIO_TEST, fmemopen_write) { |
| 1196 | FILE* fp; |
| 1197 | char buf[8]; |
| 1198 | |
| 1199 | // POSIX: "A write operation shall start either at the current position of |
| 1200 | // the stream (if mode has not specified 'a' as the first character)..." |
| 1201 | memset(buf, 'x', sizeof(buf)); |
| 1202 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+")); |
| 1203 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1204 | ASSERT_EQ(0, fseek(fp, 2, SEEK_SET)); |
| 1205 | ASSERT_EQ(' ', fputc(' ', fp)); |
| 1206 | EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf))); |
| 1207 | ASSERT_EQ(0, fclose(fp)); |
| 1208 | |
| 1209 | // "...or at the current size of the stream (if mode had 'a' as the first |
| 1210 | // character)." (See the fmemopen_size test for what "size" means, but for |
| 1211 | // mode "a", it's the first NUL byte.) |
| 1212 | memset(buf, 'x', sizeof(buf)); |
| 1213 | buf[3] = '\0'; |
| 1214 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+")); |
| 1215 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1216 | ASSERT_EQ(' ', fputc(' ', fp)); |
| 1217 | EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf))); |
| 1218 | ASSERT_EQ(0, fclose(fp)); |
| 1219 | |
| 1220 | // "If the current position at the end of the write is larger than the |
| 1221 | // current buffer size, the current buffer size shall be set to the current |
| 1222 | // position." (See the fmemopen_size test for what "size" means, but to |
| 1223 | // query it we SEEK_END with offset 0, and then ftell.) |
| 1224 | memset(buf, 'x', sizeof(buf)); |
| 1225 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+")); |
| 1226 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1227 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1228 | EXPECT_EQ(0, ftell(fp)); |
| 1229 | ASSERT_EQ(' ', fputc(' ', fp)); |
| 1230 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1231 | EXPECT_EQ(1, ftell(fp)); |
| 1232 | ASSERT_NE(EOF, fputs("123", fp)); |
| 1233 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1234 | EXPECT_EQ(4, ftell(fp)); |
| 1235 | EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf))); |
| 1236 | ASSERT_EQ(0, fclose(fp)); |
| 1237 | } |
| 1238 | |
| 1239 | TEST(STDIO_TEST, fmemopen_write_EOF) { |
| 1240 | // POSIX: "A write operation on the stream shall not advance the current |
| 1241 | // buffer size beyond the size given in the size argument." |
| 1242 | FILE* fp; |
| 1243 | |
| 1244 | // Scalar writes... |
| 1245 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w")); |
| 1246 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1247 | ASSERT_EQ('x', fputc('x', fp)); |
| 1248 | ASSERT_EQ('x', fputc('x', fp)); |
| 1249 | ASSERT_EQ('x', fputc('x', fp)); |
| 1250 | ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL. |
| 1251 | ASSERT_EQ(0, fclose(fp)); |
| 1252 | |
| 1253 | // Vector writes... |
| 1254 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w")); |
| 1255 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1256 | ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp)); |
| 1257 | ASSERT_EQ(0, fclose(fp)); |
| 1258 | } |
| 1259 | |
| 1260 | TEST(STDIO_TEST, fmemopen_initial_position) { |
| 1261 | // POSIX: "The ... current position in the buffer ... shall be initially |
| 1262 | // set to either the beginning of the buffer (for r and w modes) ..." |
| 1263 | char buf[] = "hello\0world"; |
| 1264 | FILE* fp; |
| 1265 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r")); |
| 1266 | EXPECT_EQ(0L, ftell(fp)); |
| 1267 | EXPECT_EQ(0, fclose(fp)); |
| 1268 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w")); |
| 1269 | EXPECT_EQ(0L, ftell(fp)); |
| 1270 | EXPECT_EQ(0, fclose(fp)); |
| 1271 | buf[0] = 'h'; // (Undo the effects of the above.) |
| 1272 | |
| 1273 | // POSIX: "...or to the first null byte in the buffer (for a modes)." |
| 1274 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a")); |
| 1275 | EXPECT_EQ(5L, ftell(fp)); |
| 1276 | EXPECT_EQ(0, fclose(fp)); |
| 1277 | |
| 1278 | // POSIX: "If no null byte is found in append mode, the initial position |
| 1279 | // shall be set to one byte after the end of the buffer." |
| 1280 | memset(buf, 'x', sizeof(buf)); |
| 1281 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a")); |
| 1282 | EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp)); |
| 1283 | EXPECT_EQ(0, fclose(fp)); |
| 1284 | } |
| 1285 | |
| 1286 | TEST(STDIO_TEST, fmemopen_initial_position_allocated) { |
| 1287 | // POSIX: "If buf is a null pointer, the initial position shall always be |
| 1288 | // set to the beginning of the buffer." |
| 1289 | FILE* fp = fmemopen(nullptr, 128, "a+"); |
| 1290 | ASSERT_TRUE(fp != nullptr); |
| 1291 | EXPECT_EQ(0L, ftell(fp)); |
| 1292 | EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET)); |
| 1293 | EXPECT_EQ(0, fclose(fp)); |
| 1294 | } |
| 1295 | |
| 1296 | TEST(STDIO_TEST, fmemopen_zero_length) { |
| 1297 | // POSIX says it's up to the implementation whether or not you can have a |
| 1298 | // zero-length buffer (but "A future version of this standard may require |
| 1299 | // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22 |
| 1300 | // agreed that you couldn't, but glibc >= 2.22 allows it for consistency. |
| 1301 | FILE* fp; |
| 1302 | char buf[16]; |
| 1303 | ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+")); |
| 1304 | ASSERT_EQ(EOF, fgetc(fp)); |
| 1305 | ASSERT_TRUE(feof(fp)); |
| 1306 | ASSERT_EQ(0, fclose(fp)); |
| 1307 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+")); |
| 1308 | ASSERT_EQ(EOF, fgetc(fp)); |
| 1309 | ASSERT_TRUE(feof(fp)); |
| 1310 | ASSERT_EQ(0, fclose(fp)); |
| 1311 | |
| 1312 | ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+")); |
| 1313 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1314 | ASSERT_EQ(EOF, fputc('x', fp)); |
| 1315 | ASSERT_EQ(0, fclose(fp)); |
| 1316 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+")); |
| 1317 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1318 | ASSERT_EQ(EOF, fputc('x', fp)); |
| 1319 | ASSERT_EQ(0, fclose(fp)); |
| 1320 | } |
| 1321 | |
| 1322 | TEST(STDIO_TEST, fmemopen_write_only_allocated) { |
| 1323 | // POSIX says fmemopen "may fail if the mode argument does not include a '+'". |
| 1324 | // BSD fails, glibc doesn't. We side with the more lenient. |
| 1325 | FILE* fp; |
| 1326 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r")); |
| 1327 | ASSERT_EQ(0, fclose(fp)); |
| 1328 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w")); |
| 1329 | ASSERT_EQ(0, fclose(fp)); |
| 1330 | } |
| 1331 | |
| 1332 | TEST(STDIO_TEST, fmemopen_fileno) { |
| 1333 | // There's no fd backing an fmemopen FILE*. |
| 1334 | FILE* fp = fmemopen(nullptr, 16, "r"); |
| 1335 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1336 | errno = 0; |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1337 | ASSERT_EQ(-1, fileno(fp)); |
| 1338 | ASSERT_EQ(EBADF, errno); |
| 1339 | ASSERT_EQ(0, fclose(fp)); |
| 1340 | } |
| 1341 | |
| 1342 | TEST(STDIO_TEST, fmemopen_append_after_seek) { |
| 1343 | // In BSD and glibc < 2.22, append mode didn't force writes to append if |
| 1344 | // there had been an intervening seek. |
| 1345 | |
| 1346 | FILE* fp; |
| 1347 | char buf[] = "hello\0world"; |
| 1348 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a")); |
| 1349 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1350 | ASSERT_EQ(0, fseek(fp, 0, SEEK_SET)); |
| 1351 | ASSERT_NE(EOF, fputc('!', fp)); |
| 1352 | EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf))); |
| 1353 | ASSERT_EQ(0, fclose(fp)); |
| 1354 | |
| 1355 | memcpy(buf, "hello\0world", sizeof(buf)); |
| 1356 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+")); |
| 1357 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1358 | ASSERT_EQ(0, fseek(fp, 0, SEEK_SET)); |
| 1359 | ASSERT_NE(EOF, fputc('!', fp)); |
| 1360 | EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf))); |
| 1361 | ASSERT_EQ(0, fclose(fp)); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1362 | } |
| 1363 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1364 | TEST(STDIO_TEST, open_memstream) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1365 | char* p = nullptr; |
| 1366 | size_t size = 0; |
| 1367 | FILE* fp = open_memstream(&p, &size); |
| 1368 | ASSERT_NE(EOF, fputs("hello, world!", fp)); |
| 1369 | fclose(fp); |
| 1370 | |
| 1371 | ASSERT_STREQ("hello, world!", p); |
| 1372 | ASSERT_EQ(strlen("hello, world!"), size); |
| 1373 | free(p); |
| 1374 | } |
| 1375 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1376 | TEST(STDIO_TEST, open_memstream_EINVAL) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1377 | #if defined(__BIONIC__) |
| 1378 | char* p; |
| 1379 | size_t size; |
| 1380 | |
| 1381 | // Invalid buffer. |
| 1382 | errno = 0; |
| 1383 | ASSERT_EQ(nullptr, open_memstream(nullptr, &size)); |
| 1384 | ASSERT_EQ(EINVAL, errno); |
| 1385 | |
| 1386 | // Invalid size. |
| 1387 | errno = 0; |
| 1388 | ASSERT_EQ(nullptr, open_memstream(&p, nullptr)); |
| 1389 | ASSERT_EQ(EINVAL, errno); |
| 1390 | #else |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1391 | GTEST_LOG_(INFO) << "This test does nothing on glibc.\n"; |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1392 | #endif |
| 1393 | } |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 1394 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1395 | TEST(STDIO_TEST, fdopen_CLOEXEC) { |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 1396 | int fd = open("/proc/version", O_RDONLY); |
| 1397 | ASSERT_TRUE(fd != -1); |
| 1398 | |
| 1399 | // This fd doesn't have O_CLOEXEC... |
| 1400 | int flags = fcntl(fd, F_GETFD); |
| 1401 | ASSERT_TRUE(flags != -1); |
| 1402 | ASSERT_EQ(0, flags & FD_CLOEXEC); |
| 1403 | |
| 1404 | FILE* fp = fdopen(fd, "re"); |
| 1405 | ASSERT_TRUE(fp != NULL); |
| 1406 | |
| 1407 | // ...but the new one does. |
| 1408 | flags = fcntl(fileno(fp), F_GETFD); |
| 1409 | ASSERT_TRUE(flags != -1); |
| 1410 | ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC); |
| 1411 | |
| 1412 | fclose(fp); |
| 1413 | close(fd); |
| 1414 | } |
| 1415 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1416 | TEST(STDIO_TEST, freopen_CLOEXEC) { |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 1417 | FILE* fp = fopen("/proc/version", "r"); |
| 1418 | ASSERT_TRUE(fp != NULL); |
| 1419 | |
| 1420 | // This FILE* doesn't have O_CLOEXEC... |
| 1421 | int flags = fcntl(fileno(fp), F_GETFD); |
| 1422 | ASSERT_TRUE(flags != -1); |
| 1423 | ASSERT_EQ(0, flags & FD_CLOEXEC); |
| 1424 | |
| 1425 | fp = freopen("/proc/version", "re", fp); |
| 1426 | |
| 1427 | // ...but the new one does. |
| 1428 | flags = fcntl(fileno(fp), F_GETFD); |
| 1429 | ASSERT_TRUE(flags != -1); |
| 1430 | ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC); |
| 1431 | |
| 1432 | fclose(fp); |
| 1433 | } |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 1434 | |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 1435 | TEST(STDIO_TEST, fopen64_freopen64) { |
| 1436 | FILE* fp = fopen64("/proc/version", "r"); |
| 1437 | ASSERT_TRUE(fp != nullptr); |
| 1438 | fp = freopen64("/proc/version", "re", fp); |
| 1439 | ASSERT_TRUE(fp != nullptr); |
| 1440 | fclose(fp); |
| 1441 | } |
| 1442 | |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 1443 | // https://code.google.com/p/android/issues/detail?id=81155 |
| 1444 | // http://b/18556607 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1445 | TEST(STDIO_TEST, fread_unbuffered_pathological_performance) { |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 1446 | FILE* fp = fopen("/dev/zero", "r"); |
| 1447 | ASSERT_TRUE(fp != NULL); |
| 1448 | |
| 1449 | // Make this stream unbuffered. |
| 1450 | setvbuf(fp, 0, _IONBF, 0); |
| 1451 | |
| 1452 | char buf[65*1024]; |
| 1453 | memset(buf, 0xff, sizeof(buf)); |
| 1454 | |
| 1455 | time_t t0 = time(NULL); |
| 1456 | for (size_t i = 0; i < 1024; ++i) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1457 | ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp)); |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 1458 | } |
| 1459 | time_t t1 = time(NULL); |
| 1460 | |
| 1461 | fclose(fp); |
| 1462 | |
| 1463 | // 1024 64KiB reads should have been very quick. |
| 1464 | ASSERT_LE(t1 - t0, 1); |
| 1465 | |
| 1466 | for (size_t i = 0; i < 64*1024; ++i) { |
| 1467 | ASSERT_EQ('\0', buf[i]); |
| 1468 | } |
| 1469 | for (size_t i = 64*1024; i < 65*1024; ++i) { |
| 1470 | ASSERT_EQ('\xff', buf[i]); |
| 1471 | } |
| 1472 | } |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 1473 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1474 | TEST(STDIO_TEST, fread_EOF) { |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 1475 | std::string digits("0123456789"); |
| 1476 | FILE* fp = fmemopen(&digits[0], digits.size(), "r"); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 1477 | |
| 1478 | // Try to read too much, but little enough that it still fits in the FILE's internal buffer. |
| 1479 | char buf1[4 * 4]; |
| 1480 | memset(buf1, 0, sizeof(buf1)); |
| 1481 | ASSERT_EQ(2U, fread(buf1, 4, 4, fp)); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 1482 | ASSERT_STREQ("0123456789", buf1); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 1483 | ASSERT_TRUE(feof(fp)); |
| 1484 | |
| 1485 | rewind(fp); |
| 1486 | |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 1487 | // Try to read way too much so stdio tries to read more direct from the stream. |
| 1488 | char buf2[4 * 4096]; |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 1489 | memset(buf2, 0, sizeof(buf2)); |
| 1490 | ASSERT_EQ(2U, fread(buf2, 4, 4096, fp)); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 1491 | ASSERT_STREQ("0123456789", buf2); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 1492 | ASSERT_TRUE(feof(fp)); |
| 1493 | |
| 1494 | fclose(fp); |
| 1495 | } |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1496 | |
| 1497 | static void test_fread_from_write_only_stream(size_t n) { |
| 1498 | FILE* fp = fopen("/dev/null", "w"); |
| 1499 | std::vector<char> buf(n, 0); |
| 1500 | errno = 0; |
| 1501 | ASSERT_EQ(0U, fread(&buf[0], n, 1, fp)); |
| 1502 | ASSERT_EQ(EBADF, errno); |
| 1503 | ASSERT_TRUE(ferror(fp)); |
| 1504 | ASSERT_FALSE(feof(fp)); |
| 1505 | fclose(fp); |
| 1506 | } |
| 1507 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1508 | TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1509 | test_fread_from_write_only_stream(1); |
| 1510 | } |
| 1511 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1512 | TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1513 | test_fread_from_write_only_stream(64*1024); |
| 1514 | } |
| 1515 | |
| 1516 | static void test_fwrite_after_fread(size_t n) { |
| 1517 | TemporaryFile tf; |
| 1518 | |
| 1519 | FILE* fp = fdopen(tf.fd, "w+"); |
| 1520 | ASSERT_EQ(1U, fwrite("1", 1, 1, fp)); |
| 1521 | fflush(fp); |
| 1522 | |
| 1523 | // We've flushed but not rewound, so there's nothing to read. |
| 1524 | std::vector<char> buf(n, 0); |
| 1525 | ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp)); |
| 1526 | ASSERT_TRUE(feof(fp)); |
| 1527 | |
| 1528 | // But hitting EOF doesn't prevent us from writing... |
| 1529 | errno = 0; |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1530 | ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno); |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1531 | |
| 1532 | // And if we rewind, everything's there. |
| 1533 | rewind(fp); |
| 1534 | ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp)); |
| 1535 | ASSERT_EQ('1', buf[0]); |
| 1536 | ASSERT_EQ('2', buf[1]); |
| 1537 | |
| 1538 | fclose(fp); |
| 1539 | } |
| 1540 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1541 | TEST(STDIO_TEST, fwrite_after_fread_slow_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1542 | test_fwrite_after_fread(16); |
| 1543 | } |
| 1544 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1545 | TEST(STDIO_TEST, fwrite_after_fread_fast_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1546 | test_fwrite_after_fread(64*1024); |
| 1547 | } |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 1548 | |
| 1549 | // http://b/19172514 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1550 | TEST(STDIO_TEST, fread_after_fseek) { |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 1551 | TemporaryFile tf; |
| 1552 | |
| 1553 | FILE* fp = fopen(tf.filename, "w+"); |
| 1554 | ASSERT_TRUE(fp != nullptr); |
| 1555 | |
| 1556 | char file_data[12288]; |
| 1557 | for (size_t i = 0; i < 12288; i++) { |
| 1558 | file_data[i] = i; |
| 1559 | } |
| 1560 | ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp)); |
| 1561 | fclose(fp); |
| 1562 | |
| 1563 | fp = fopen(tf.filename, "r"); |
| 1564 | ASSERT_TRUE(fp != nullptr); |
| 1565 | |
| 1566 | char buffer[8192]; |
| 1567 | size_t cur_location = 0; |
| 1568 | // Small read to populate internal buffer. |
| 1569 | ASSERT_EQ(100U, fread(buffer, 1, 100, fp)); |
| 1570 | ASSERT_EQ(memcmp(file_data, buffer, 100), 0); |
| 1571 | |
| 1572 | cur_location = static_cast<size_t>(ftell(fp)); |
| 1573 | // Large read to force reading into the user supplied buffer and bypassing |
| 1574 | // the internal buffer. |
| 1575 | ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp)); |
| 1576 | ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0); |
| 1577 | |
| 1578 | // Small backwards seek to verify fseek does not reuse the internal buffer. |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1579 | ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno); |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 1580 | cur_location = static_cast<size_t>(ftell(fp)); |
| 1581 | ASSERT_EQ(22U, fread(buffer, 1, 22, fp)); |
| 1582 | ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0); |
| 1583 | |
| 1584 | fclose(fp); |
| 1585 | } |
Elliott Hughes | 8ab433d | 2015-10-09 17:57:26 -0700 | [diff] [blame] | 1586 | |
| 1587 | // https://code.google.com/p/android/issues/detail?id=184847 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1588 | TEST(STDIO_TEST, fread_EOF_184847) { |
Elliott Hughes | 8ab433d | 2015-10-09 17:57:26 -0700 | [diff] [blame] | 1589 | TemporaryFile tf; |
| 1590 | char buf[6] = {0}; |
| 1591 | |
| 1592 | FILE* fw = fopen(tf.filename, "w"); |
| 1593 | ASSERT_TRUE(fw != nullptr); |
| 1594 | |
| 1595 | FILE* fr = fopen(tf.filename, "r"); |
| 1596 | ASSERT_TRUE(fr != nullptr); |
| 1597 | |
| 1598 | fwrite("a", 1, 1, fw); |
| 1599 | fflush(fw); |
| 1600 | ASSERT_EQ(1U, fread(buf, 1, 1, fr)); |
| 1601 | ASSERT_STREQ("a", buf); |
| 1602 | |
| 1603 | // 'fr' is now at EOF. |
| 1604 | ASSERT_EQ(0U, fread(buf, 1, 1, fr)); |
| 1605 | ASSERT_TRUE(feof(fr)); |
| 1606 | |
| 1607 | // Write some more... |
| 1608 | fwrite("z", 1, 1, fw); |
| 1609 | fflush(fw); |
| 1610 | |
| 1611 | // ...and check that we can read it back. |
| 1612 | // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.) |
| 1613 | ASSERT_EQ(1U, fread(buf, 1, 1, fr)); |
| 1614 | ASSERT_STREQ("z", buf); |
| 1615 | |
| 1616 | // But now we're done. |
| 1617 | ASSERT_EQ(0U, fread(buf, 1, 1, fr)); |
| 1618 | |
| 1619 | fclose(fr); |
| 1620 | fclose(fw); |
| 1621 | } |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 1622 | |
| 1623 | TEST(STDIO_TEST, fclose_invalidates_fd) { |
| 1624 | // The typical error we're trying to help people catch involves accessing |
| 1625 | // memory after it's been freed. But we know that stdin/stdout/stderr are |
| 1626 | // special and don't get deallocated, so this test uses stdin. |
| 1627 | ASSERT_EQ(0, fclose(stdin)); |
| 1628 | |
| 1629 | // Even though using a FILE* after close is undefined behavior, I've closed |
| 1630 | // this bug as "WAI" too many times. We shouldn't hand out stale fds, |
| 1631 | // especially because they might actually correspond to a real stream. |
| 1632 | errno = 0; |
| 1633 | ASSERT_EQ(-1, fileno(stdin)); |
| 1634 | ASSERT_EQ(EBADF, errno); |
| 1635 | } |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 1636 | |
| 1637 | TEST(STDIO_TEST, fseek_ftell_unseekable) { |
| 1638 | #if defined(__BIONIC__) // glibc has fopencookie instead. |
| 1639 | auto read_fn = [](void*, char*, int) { return -1; }; |
| 1640 | FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr); |
| 1641 | ASSERT_TRUE(fp != nullptr); |
| 1642 | |
| 1643 | // Check that ftell balks on an unseekable FILE*. |
| 1644 | errno = 0; |
| 1645 | ASSERT_EQ(-1, ftell(fp)); |
| 1646 | ASSERT_EQ(ESPIPE, errno); |
| 1647 | |
| 1648 | // SEEK_CUR is rewritten as SEEK_SET internally... |
| 1649 | errno = 0; |
| 1650 | ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR)); |
| 1651 | ASSERT_EQ(ESPIPE, errno); |
| 1652 | |
| 1653 | // ...so it's worth testing the direct seek path too. |
| 1654 | errno = 0; |
| 1655 | ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET)); |
| 1656 | ASSERT_EQ(ESPIPE, errno); |
| 1657 | |
| 1658 | fclose(fp); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 1659 | #else |
| 1660 | GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; |
| 1661 | #endif |
| 1662 | } |
| 1663 | |
| 1664 | TEST(STDIO_TEST, funopen_EINVAL) { |
| 1665 | #if defined(__BIONIC__) |
| 1666 | errno = 0; |
| 1667 | ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr)); |
| 1668 | ASSERT_EQ(EINVAL, errno); |
| 1669 | #else |
| 1670 | GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; |
| 1671 | #endif |
| 1672 | } |
| 1673 | |
| 1674 | TEST(STDIO_TEST, funopen_seek) { |
| 1675 | #if defined(__BIONIC__) |
| 1676 | auto read_fn = [](void*, char*, int) { return -1; }; |
| 1677 | |
| 1678 | auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; }; |
| 1679 | auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; }; |
| 1680 | |
| 1681 | FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr); |
| 1682 | ASSERT_TRUE(fp != nullptr); |
| 1683 | fpos_t pos; |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 1684 | #if defined(__LP64__) |
| 1685 | EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno); |
| 1686 | EXPECT_EQ(0xfedcba12LL, pos); |
| 1687 | #else |
| 1688 | EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno); |
| 1689 | EXPECT_EQ(EOVERFLOW, errno); |
| 1690 | #endif |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 1691 | |
| 1692 | FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr); |
| 1693 | ASSERT_TRUE(fp64 != nullptr); |
| 1694 | fpos64_t pos64; |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 1695 | EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno); |
| 1696 | EXPECT_EQ(0xfedcba12345678, pos64); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 1697 | #else |
| 1698 | GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 1699 | #endif |
| 1700 | } |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 1701 | |
| 1702 | TEST(STDIO_TEST, lots_of_concurrent_files) { |
| 1703 | std::vector<TemporaryFile*> tfs; |
| 1704 | std::vector<FILE*> fps; |
| 1705 | |
| 1706 | for (size_t i = 0; i < 256; ++i) { |
| 1707 | TemporaryFile* tf = new TemporaryFile; |
| 1708 | tfs.push_back(tf); |
| 1709 | FILE* fp = fopen(tf->filename, "w+"); |
| 1710 | fps.push_back(fp); |
| 1711 | fprintf(fp, "hello %zu!\n", i); |
| 1712 | fflush(fp); |
| 1713 | } |
| 1714 | |
| 1715 | for (size_t i = 0; i < 256; ++i) { |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 1716 | char expected[BUFSIZ]; |
| 1717 | snprintf(expected, sizeof(expected), "hello %zu!\n", i); |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 1718 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 1719 | AssertFileIs(fps[i], expected); |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 1720 | fclose(fps[i]); |
| 1721 | delete tfs[i]; |
| 1722 | } |
| 1723 | } |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1724 | |
| 1725 | static void AssertFileOffsetAt(FILE* fp, off64_t offset) { |
| 1726 | EXPECT_EQ(offset, ftell(fp)); |
| 1727 | EXPECT_EQ(offset, ftello(fp)); |
Elliott Hughes | e4fa6e9 | 2016-02-02 22:39:15 -0800 | [diff] [blame] | 1728 | EXPECT_EQ(offset, ftello64(fp)); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1729 | fpos_t pos; |
| 1730 | fpos64_t pos64; |
| 1731 | EXPECT_EQ(0, fgetpos(fp, &pos)); |
| 1732 | EXPECT_EQ(0, fgetpos64(fp, &pos64)); |
| 1733 | #if defined(__BIONIC__) |
| 1734 | EXPECT_EQ(offset, static_cast<off64_t>(pos)); |
| 1735 | EXPECT_EQ(offset, static_cast<off64_t>(pos64)); |
| 1736 | #else |
| 1737 | GTEST_LOG_(INFO) << "glibc's fpos_t is opaque.\n"; |
| 1738 | #endif |
| 1739 | } |
| 1740 | |
| 1741 | TEST(STDIO_TEST, seek_tell_family_smoke) { |
| 1742 | TemporaryFile tf; |
| 1743 | FILE* fp = fdopen(tf.fd, "w+"); |
| 1744 | |
| 1745 | // Initially we should be at 0. |
| 1746 | AssertFileOffsetAt(fp, 0); |
| 1747 | |
| 1748 | // Seek to offset 8192. |
| 1749 | ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET)); |
| 1750 | AssertFileOffsetAt(fp, 8192); |
| 1751 | fpos_t eight_k_pos; |
| 1752 | ASSERT_EQ(0, fgetpos(fp, &eight_k_pos)); |
| 1753 | |
| 1754 | // Seek forward another 8192... |
| 1755 | ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR)); |
| 1756 | AssertFileOffsetAt(fp, 8192 + 8192); |
| 1757 | fpos64_t sixteen_k_pos64; |
| 1758 | ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64)); |
| 1759 | |
| 1760 | // Seek back 8192... |
| 1761 | ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR)); |
| 1762 | AssertFileOffsetAt(fp, 8192); |
| 1763 | |
| 1764 | // Since we haven't written anything, the end is also at 0. |
| 1765 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1766 | AssertFileOffsetAt(fp, 0); |
| 1767 | |
| 1768 | // Check that our fpos64_t from 16KiB works... |
| 1769 | ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64)); |
| 1770 | AssertFileOffsetAt(fp, 8192 + 8192); |
| 1771 | // ...as does our fpos_t from 8192. |
| 1772 | ASSERT_EQ(0, fsetpos(fp, &eight_k_pos)); |
| 1773 | AssertFileOffsetAt(fp, 8192); |
| 1774 | |
| 1775 | // Do fseeko and fseeko64 work too? |
| 1776 | ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET)); |
| 1777 | AssertFileOffsetAt(fp, 1234); |
| 1778 | ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET)); |
| 1779 | AssertFileOffsetAt(fp, 5678); |
| 1780 | |
| 1781 | fclose(fp); |
| 1782 | } |
| 1783 | |
| 1784 | TEST(STDIO_TEST, fseek_fseeko_EINVAL) { |
| 1785 | TemporaryFile tf; |
| 1786 | FILE* fp = fdopen(tf.fd, "w+"); |
| 1787 | |
| 1788 | // Bad whence. |
| 1789 | errno = 0; |
| 1790 | ASSERT_EQ(-1, fseek(fp, 0, 123)); |
| 1791 | ASSERT_EQ(EINVAL, errno); |
| 1792 | errno = 0; |
| 1793 | ASSERT_EQ(-1, fseeko(fp, 0, 123)); |
| 1794 | ASSERT_EQ(EINVAL, errno); |
| 1795 | errno = 0; |
| 1796 | ASSERT_EQ(-1, fseeko64(fp, 0, 123)); |
| 1797 | ASSERT_EQ(EINVAL, errno); |
| 1798 | |
| 1799 | // Bad offset. |
| 1800 | errno = 0; |
| 1801 | ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET)); |
| 1802 | ASSERT_EQ(EINVAL, errno); |
| 1803 | errno = 0; |
| 1804 | ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET)); |
| 1805 | ASSERT_EQ(EINVAL, errno); |
| 1806 | errno = 0; |
| 1807 | ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET)); |
| 1808 | ASSERT_EQ(EINVAL, errno); |
| 1809 | |
| 1810 | fclose(fp); |
| 1811 | } |
Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 1812 | |
| 1813 | TEST(STDIO_TEST, ctermid) { |
| 1814 | ASSERT_STREQ("/dev/tty", ctermid(nullptr)); |
| 1815 | |
| 1816 | char buf[L_ctermid] = {}; |
| 1817 | ASSERT_EQ(buf, ctermid(buf)); |
| 1818 | ASSERT_STREQ("/dev/tty", buf); |
| 1819 | } |
Elliott Hughes | d1f25a7 | 2016-08-05 15:53:03 -0700 | [diff] [blame] | 1820 | |
| 1821 | TEST(STDIO_TEST, remove) { |
| 1822 | struct stat sb; |
| 1823 | |
| 1824 | TemporaryFile tf; |
| 1825 | ASSERT_EQ(0, remove(tf.filename)); |
| 1826 | ASSERT_EQ(-1, lstat(tf.filename, &sb)); |
| 1827 | ASSERT_EQ(ENOENT, errno); |
| 1828 | |
| 1829 | TemporaryDir td; |
| 1830 | ASSERT_EQ(0, remove(td.dirname)); |
| 1831 | ASSERT_EQ(-1, lstat(td.dirname, &sb)); |
| 1832 | ASSERT_EQ(ENOENT, errno); |
| 1833 | |
| 1834 | errno = 0; |
| 1835 | ASSERT_EQ(-1, remove(tf.filename)); |
| 1836 | ASSERT_EQ(ENOENT, errno); |
| 1837 | |
| 1838 | errno = 0; |
| 1839 | ASSERT_EQ(-1, remove(td.dirname)); |
| 1840 | ASSERT_EQ(ENOENT, errno); |
| 1841 | } |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 1842 | |
| 1843 | TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) { |
| 1844 | char buf[16]; |
| 1845 | ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"), |
| 1846 | testing::KilledBySignal(SIGABRT), |
| 1847 | #if defined(NOFORTIFY) |
| 1848 | "FORTIFY: vsnprintf: size .* > SSIZE_MAX" |
| 1849 | #else |
| 1850 | "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer" |
| 1851 | #endif |
| 1852 | ); |
| 1853 | } |
| 1854 | |
| 1855 | TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) { |
| 1856 | std::string buf = "world"; |
| 1857 | ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"), |
| 1858 | testing::KilledBySignal(SIGABRT), |
| 1859 | "FORTIFY: vsnprintf: size .* > SSIZE_MAX"); |
| 1860 | } |
| 1861 | |
| 1862 | TEST(STDIO_TEST, sprintf_30445072) { |
| 1863 | std::string buf = "world"; |
| 1864 | sprintf(&buf[0], "hello"); |
| 1865 | ASSERT_EQ(buf, "hello"); |
| 1866 | } |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 1867 | |
| 1868 | TEST(STDIO_TEST, fopen_append_mode_and_ftell) { |
| 1869 | TemporaryFile tf; |
| 1870 | SetFileTo(tf.filename, "0123456789"); |
| 1871 | FILE* fp = fopen(tf.filename, "a"); |
| 1872 | EXPECT_EQ(10, ftell(fp)); |
| 1873 | ASSERT_EQ(0, fseek(fp, 2, SEEK_SET)); |
| 1874 | EXPECT_EQ(2, ftell(fp)); |
| 1875 | ASSERT_NE(EOF, fputs("xxx", fp)); |
| 1876 | ASSERT_EQ(0, fflush(fp)); |
| 1877 | EXPECT_EQ(13, ftell(fp)); |
| 1878 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1879 | EXPECT_EQ(13, ftell(fp)); |
| 1880 | ASSERT_EQ(0, fclose(fp)); |
| 1881 | AssertFileIs(tf.filename, "0123456789xxx"); |
| 1882 | } |
| 1883 | |
| 1884 | TEST(STDIO_TEST, fdopen_append_mode_and_ftell) { |
| 1885 | TemporaryFile tf; |
| 1886 | SetFileTo(tf.filename, "0123456789"); |
| 1887 | int fd = open(tf.filename, O_RDWR); |
| 1888 | ASSERT_NE(-1, fd); |
| 1889 | // POSIX: "The file position indicator associated with the new stream is set to the position |
| 1890 | // indicated by the file offset associated with the file descriptor." |
| 1891 | ASSERT_EQ(4, lseek(fd, 4, SEEK_SET)); |
| 1892 | FILE* fp = fdopen(fd, "a"); |
| 1893 | EXPECT_EQ(4, ftell(fp)); |
| 1894 | ASSERT_EQ(0, fseek(fp, 2, SEEK_SET)); |
| 1895 | EXPECT_EQ(2, ftell(fp)); |
| 1896 | ASSERT_NE(EOF, fputs("xxx", fp)); |
| 1897 | ASSERT_EQ(0, fflush(fp)); |
| 1898 | EXPECT_EQ(13, ftell(fp)); |
| 1899 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1900 | EXPECT_EQ(13, ftell(fp)); |
| 1901 | ASSERT_EQ(0, fclose(fp)); |
| 1902 | AssertFileIs(tf.filename, "0123456789xxx"); |
| 1903 | } |
| 1904 | |
| 1905 | TEST(STDIO_TEST, freopen_append_mode_and_ftell) { |
| 1906 | TemporaryFile tf; |
| 1907 | SetFileTo(tf.filename, "0123456789"); |
| 1908 | FILE* other_fp = fopen("/proc/version", "r"); |
| 1909 | FILE* fp = freopen(tf.filename, "a", other_fp); |
| 1910 | EXPECT_EQ(10, ftell(fp)); |
| 1911 | ASSERT_EQ(0, fseek(fp, 2, SEEK_SET)); |
| 1912 | EXPECT_EQ(2, ftell(fp)); |
| 1913 | ASSERT_NE(EOF, fputs("xxx", fp)); |
| 1914 | ASSERT_EQ(0, fflush(fp)); |
| 1915 | EXPECT_EQ(13, ftell(fp)); |
| 1916 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1917 | EXPECT_EQ(13, ftell(fp)); |
| 1918 | ASSERT_EQ(0, fclose(fp)); |
| 1919 | AssertFileIs(tf.filename, "0123456789xxx"); |
| 1920 | } |