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 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 305 | TEST(STDIO_TEST, snprintf_C) { // Synonym for %lc. |
| 306 | char buf[BUFSIZ]; |
| 307 | wchar_t wc = L'a'; |
| 308 | EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%C>", wc)); |
| 309 | EXPECT_STREQ("<a>", buf); |
| 310 | } |
| 311 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 312 | TEST(STDIO_TEST, snprintf_ls) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 313 | char buf[BUFSIZ]; |
| 314 | wchar_t* ws = NULL; |
| 315 | EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws)); |
| 316 | EXPECT_STREQ("<(null)>", buf); |
| 317 | |
| 318 | wchar_t chars[] = { L'h', L'i', 0 }; |
| 319 | ws = chars; |
| 320 | EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws)); |
| 321 | EXPECT_STREQ("<hi>", buf); |
| 322 | } |
| 323 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 324 | TEST(STDIO_TEST, snprintf_S) { // Synonym for %ls. |
| 325 | char buf[BUFSIZ]; |
| 326 | wchar_t* ws = NULL; |
| 327 | EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%S>", ws)); |
| 328 | EXPECT_STREQ("<(null)>", buf); |
| 329 | |
| 330 | wchar_t chars[] = { L'h', L'i', 0 }; |
| 331 | ws = chars; |
| 332 | EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%S>", ws)); |
| 333 | EXPECT_STREQ("<hi>", buf); |
| 334 | } |
| 335 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 336 | TEST(STDIO_TEST, snprintf_n) { |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 337 | #if defined(__BIONIC__) |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 338 | // http://b/14492135 |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 339 | char buf[32]; |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 340 | int i = 1234; |
| 341 | EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i)); |
| 342 | EXPECT_EQ(1234, i); |
| 343 | EXPECT_STREQ("a n b", buf); |
| 344 | #else |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 345 | GTEST_LOG_(INFO) << "This test does nothing on glibc.\n"; |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 346 | #endif |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 347 | } |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 348 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 349 | TEST(STDIO_TEST, snprintf_smoke) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 350 | char buf[BUFSIZ]; |
| 351 | |
| 352 | snprintf(buf, sizeof(buf), "a"); |
| 353 | EXPECT_STREQ("a", buf); |
| 354 | |
| 355 | snprintf(buf, sizeof(buf), "%%"); |
| 356 | EXPECT_STREQ("%", buf); |
| 357 | |
| 358 | snprintf(buf, sizeof(buf), "01234"); |
| 359 | EXPECT_STREQ("01234", buf); |
| 360 | |
| 361 | snprintf(buf, sizeof(buf), "a%sb", "01234"); |
| 362 | EXPECT_STREQ("a01234b", buf); |
| 363 | |
| 364 | char* s = NULL; |
| 365 | snprintf(buf, sizeof(buf), "a%sb", s); |
| 366 | EXPECT_STREQ("a(null)b", buf); |
| 367 | |
| 368 | snprintf(buf, sizeof(buf), "aa%scc", "bb"); |
| 369 | EXPECT_STREQ("aabbcc", buf); |
| 370 | |
| 371 | snprintf(buf, sizeof(buf), "a%cc", 'b'); |
| 372 | EXPECT_STREQ("abc", buf); |
| 373 | |
| 374 | snprintf(buf, sizeof(buf), "a%db", 1234); |
| 375 | EXPECT_STREQ("a1234b", buf); |
| 376 | |
| 377 | snprintf(buf, sizeof(buf), "a%db", -8123); |
| 378 | EXPECT_STREQ("a-8123b", buf); |
| 379 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 380 | snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010)); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 381 | EXPECT_STREQ("a16b", buf); |
| 382 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 383 | snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10)); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 384 | EXPECT_STREQ("a16b", buf); |
| 385 | |
| 386 | snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL); |
| 387 | EXPECT_STREQ("a68719476736b", buf); |
| 388 | |
| 389 | snprintf(buf, sizeof(buf), "a%ldb", 70000L); |
| 390 | EXPECT_STREQ("a70000b", buf); |
| 391 | |
| 392 | snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234)); |
| 393 | EXPECT_STREQ("a0xb0001234b", buf); |
| 394 | |
| 395 | snprintf(buf, sizeof(buf), "a%xz", 0x12ab); |
| 396 | EXPECT_STREQ("a12abz", buf); |
| 397 | |
| 398 | snprintf(buf, sizeof(buf), "a%Xz", 0x12ab); |
| 399 | EXPECT_STREQ("a12ABz", buf); |
| 400 | |
| 401 | snprintf(buf, sizeof(buf), "a%08xz", 0x123456); |
| 402 | EXPECT_STREQ("a00123456z", buf); |
| 403 | |
| 404 | snprintf(buf, sizeof(buf), "a%5dz", 1234); |
| 405 | EXPECT_STREQ("a 1234z", buf); |
| 406 | |
| 407 | snprintf(buf, sizeof(buf), "a%05dz", 1234); |
| 408 | EXPECT_STREQ("a01234z", buf); |
| 409 | |
| 410 | snprintf(buf, sizeof(buf), "a%8dz", 1234); |
| 411 | EXPECT_STREQ("a 1234z", buf); |
| 412 | |
| 413 | snprintf(buf, sizeof(buf), "a%-8dz", 1234); |
| 414 | EXPECT_STREQ("a1234 z", buf); |
| 415 | |
| 416 | snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef"); |
| 417 | EXPECT_STREQ("Aabcdef Z", buf); |
| 418 | |
| 419 | snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234); |
| 420 | EXPECT_STREQ("Ahello:1234Z", buf); |
| 421 | |
| 422 | snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5); |
| 423 | EXPECT_STREQ("a005:5:05z", buf); |
| 424 | |
| 425 | void* p = NULL; |
| 426 | snprintf(buf, sizeof(buf), "a%d,%pz", 5, p); |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 427 | #if defined(__BIONIC__) |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 428 | EXPECT_STREQ("a5,0x0z", buf); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 429 | #else // __BIONIC__ |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 430 | EXPECT_STREQ("a5,(nil)z", buf); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 431 | #endif // __BIONIC__ |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 432 | |
| 433 | snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8); |
| 434 | EXPECT_STREQ("a68719476736,6,7,8z", buf); |
| 435 | |
| 436 | snprintf(buf, sizeof(buf), "a_%f_b", 1.23f); |
| 437 | EXPECT_STREQ("a_1.230000_b", buf); |
| 438 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 439 | snprintf(buf, sizeof(buf), "a_%g_b", 3.14); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 440 | EXPECT_STREQ("a_3.14_b", buf); |
Alexander Ivchenko | edd7c2e | 2014-04-01 17:01:39 +0400 | [diff] [blame] | 441 | |
| 442 | snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice"); |
| 443 | EXPECT_STREQ("print_me_twice print_me_twice", buf); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 446 | template <typename T> |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 447 | static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...), |
| 448 | int sscanf_fn(const T*, const T*, ...), |
| 449 | const T* fmt_string, const T* fmt, const T* fmt_plus, |
| 450 | const T* minus_inf, const T* inf_, const T* plus_inf, |
| 451 | const T* minus_nan, const T* nan_, const T* plus_nan) { |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 452 | T buf[BUFSIZ]; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 453 | float f; |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 454 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 455 | // NaN. |
| 456 | |
| 457 | snprintf_fn(buf, sizeof(buf), fmt, nanf("")); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 458 | EXPECT_STREQ(nan_, buf) << fmt; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 459 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 460 | EXPECT_TRUE(isnan(f)); |
| 461 | |
| 462 | snprintf_fn(buf, sizeof(buf), fmt, -nanf("")); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 463 | EXPECT_STREQ(minus_nan, buf) << fmt; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 464 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 465 | EXPECT_TRUE(isnan(f)); |
| 466 | |
| 467 | snprintf_fn(buf, sizeof(buf), fmt_plus, nanf("")); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 468 | EXPECT_STREQ(plus_nan, buf) << fmt_plus; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 469 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 470 | EXPECT_TRUE(isnan(f)); |
| 471 | |
| 472 | snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf("")); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 473 | EXPECT_STREQ(minus_nan, buf) << fmt_plus; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 474 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 475 | EXPECT_TRUE(isnan(f)); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 476 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 477 | // Inf. |
| 478 | |
| 479 | snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 480 | EXPECT_STREQ(inf_, buf) << fmt; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 481 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 482 | EXPECT_EQ(HUGE_VALF, f); |
| 483 | |
| 484 | snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 485 | EXPECT_STREQ(minus_inf, buf) << fmt; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 486 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 487 | EXPECT_EQ(-HUGE_VALF, f); |
| 488 | |
| 489 | snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 490 | EXPECT_STREQ(plus_inf, buf) << fmt_plus; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 491 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 492 | EXPECT_EQ(HUGE_VALF, f); |
| 493 | |
| 494 | snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 495 | EXPECT_STREQ(minus_inf, buf) << fmt_plus; |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 496 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)); |
| 497 | EXPECT_EQ(-HUGE_VALF, f); |
| 498 | |
| 499 | // Check case-insensitivity. |
| 500 | snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]"); |
| 501 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf; |
| 502 | EXPECT_EQ(HUGE_VALF, f); |
| 503 | snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]"); |
| 504 | EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf; |
| 505 | EXPECT_TRUE(isnan(f)); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 508 | TEST(STDIO_TEST, snprintf_sscanf_inf_nan) { |
| 509 | CheckInfNan(snprintf, sscanf, "%s", |
| 510 | "[%a]", "[%+a]", |
| 511 | "[-inf]", "[inf]", "[+inf]", |
| 512 | "[-nan]", "[nan]", "[+nan]"); |
| 513 | CheckInfNan(snprintf, sscanf, "%s", |
| 514 | "[%A]", "[%+A]", |
| 515 | "[-INF]", "[INF]", "[+INF]", |
| 516 | "[-NAN]", "[NAN]", "[+NAN]"); |
| 517 | CheckInfNan(snprintf, sscanf, "%s", |
| 518 | "[%e]", "[%+e]", |
| 519 | "[-inf]", "[inf]", "[+inf]", |
| 520 | "[-nan]", "[nan]", "[+nan]"); |
| 521 | CheckInfNan(snprintf, sscanf, "%s", |
| 522 | "[%E]", "[%+E]", |
| 523 | "[-INF]", "[INF]", "[+INF]", |
| 524 | "[-NAN]", "[NAN]", "[+NAN]"); |
| 525 | CheckInfNan(snprintf, sscanf, "%s", |
| 526 | "[%f]", "[%+f]", |
| 527 | "[-inf]", "[inf]", "[+inf]", |
| 528 | "[-nan]", "[nan]", "[+nan]"); |
| 529 | CheckInfNan(snprintf, sscanf, "%s", |
| 530 | "[%F]", "[%+F]", |
| 531 | "[-INF]", "[INF]", "[+INF]", |
| 532 | "[-NAN]", "[NAN]", "[+NAN]"); |
| 533 | CheckInfNan(snprintf, sscanf, "%s", |
| 534 | "[%g]", "[%+g]", |
| 535 | "[-inf]", "[inf]", "[+inf]", |
| 536 | "[-nan]", "[nan]", "[+nan]"); |
| 537 | CheckInfNan(snprintf, sscanf, "%s", |
| 538 | "[%G]", "[%+G]", |
| 539 | "[-INF]", "[INF]", "[+INF]", |
| 540 | "[-NAN]", "[NAN]", "[+NAN]"); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 541 | } |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 542 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 543 | TEST(STDIO_TEST, swprintf_swscanf_inf_nan) { |
| 544 | CheckInfNan(swprintf, swscanf, L"%s", |
| 545 | L"[%a]", L"[%+a]", |
| 546 | L"[-inf]", L"[inf]", L"[+inf]", |
| 547 | L"[-nan]", L"[nan]", L"[+nan]"); |
| 548 | CheckInfNan(swprintf, swscanf, L"%s", |
| 549 | L"[%A]", L"[%+A]", |
| 550 | L"[-INF]", L"[INF]", L"[+INF]", |
| 551 | L"[-NAN]", L"[NAN]", L"[+NAN]"); |
| 552 | CheckInfNan(swprintf, swscanf, L"%s", |
| 553 | L"[%e]", L"[%+e]", |
| 554 | L"[-inf]", L"[inf]", L"[+inf]", |
| 555 | L"[-nan]", L"[nan]", L"[+nan]"); |
| 556 | CheckInfNan(swprintf, swscanf, L"%s", |
| 557 | L"[%E]", L"[%+E]", |
| 558 | L"[-INF]", L"[INF]", L"[+INF]", |
| 559 | L"[-NAN]", L"[NAN]", L"[+NAN]"); |
| 560 | CheckInfNan(swprintf, swscanf, L"%s", |
| 561 | L"[%f]", L"[%+f]", |
| 562 | L"[-inf]", L"[inf]", L"[+inf]", |
| 563 | L"[-nan]", L"[nan]", L"[+nan]"); |
| 564 | CheckInfNan(swprintf, swscanf, L"%s", |
| 565 | L"[%F]", L"[%+F]", |
| 566 | L"[-INF]", L"[INF]", L"[+INF]", |
| 567 | L"[-NAN]", L"[NAN]", L"[+NAN]"); |
| 568 | CheckInfNan(swprintf, swscanf, L"%s", |
| 569 | L"[%g]", L"[%+g]", |
| 570 | L"[-inf]", L"[inf]", L"[+inf]", |
| 571 | L"[-nan]", L"[nan]", L"[+nan]"); |
| 572 | CheckInfNan(swprintf, swscanf, L"%s", |
| 573 | L"[%G]", L"[%+G]", |
| 574 | L"[-INF]", L"[INF]", L"[+INF]", |
| 575 | L"[-NAN]", L"[NAN]", L"[+NAN]"); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Dan Albert | 9601f16 | 2017-08-09 14:59:06 -0700 | [diff] [blame] | 578 | TEST(STDIO_TEST, swprintf) { |
| 579 | constexpr size_t nchars = 32; |
| 580 | wchar_t buf[nchars]; |
| 581 | |
| 582 | ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno); |
| 583 | ASSERT_EQ(std::wstring(L"ab"), buf); |
| 584 | ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde")); |
| 585 | ASSERT_EQ(std::wstring(L"abcde"), buf); |
| 586 | |
| 587 | // Unlike swprintf(), swprintf() returns -1 in case of truncation |
| 588 | // and doesn't necessarily zero-terminate the output! |
| 589 | ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde")); |
| 590 | |
| 591 | const char kString[] = "Hello, World"; |
| 592 | ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString)); |
| 593 | ASSERT_EQ(std::wstring(L"Hello, World"), buf); |
| 594 | ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString)); |
| 595 | ASSERT_EQ(std::wstring(L"Hello, World"), buf); |
| 596 | } |
| 597 | |
| 598 | TEST(STDIO_TEST, swprintf_a) { |
| 599 | constexpr size_t nchars = 32; |
| 600 | wchar_t buf[nchars]; |
| 601 | |
| 602 | ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535)); |
| 603 | ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf); |
| 604 | } |
| 605 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 606 | TEST(STDIO_TEST, swprintf_lc) { |
| 607 | constexpr size_t nchars = 32; |
| 608 | wchar_t buf[nchars]; |
| 609 | |
| 610 | wint_t wc = L'a'; |
| 611 | EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc)); |
| 612 | EXPECT_EQ(std::wstring(L"<a>"), buf); |
| 613 | } |
| 614 | |
| 615 | TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc. |
| 616 | constexpr size_t nchars = 32; |
| 617 | wchar_t buf[nchars]; |
| 618 | |
| 619 | wint_t wc = L'a'; |
| 620 | EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc)); |
| 621 | EXPECT_EQ(std::wstring(L"<a>"), buf); |
| 622 | } |
| 623 | |
Elliott Hughes | 618303c | 2017-11-02 16:58:44 -0700 | [diff] [blame^] | 624 | TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) { |
| 625 | constexpr size_t nchars = 32; |
| 626 | wchar_t buf[nchars]; |
| 627 | |
| 628 | swprintf(buf, nchars, L"%jd", INTMAX_MAX); |
| 629 | EXPECT_EQ(std::wstring(L"9223372036854775807"), buf); |
| 630 | } |
| 631 | |
| 632 | TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) { |
| 633 | constexpr size_t nchars = 32; |
| 634 | wchar_t buf[nchars]; |
| 635 | |
| 636 | swprintf(buf, nchars, L"%jd", INTMAX_MIN); |
| 637 | EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf); |
| 638 | } |
| 639 | |
| 640 | TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) { |
| 641 | constexpr size_t nchars = 32; |
| 642 | wchar_t buf[nchars]; |
| 643 | |
| 644 | swprintf(buf, nchars, L"%ju", UINTMAX_MAX); |
| 645 | EXPECT_EQ(std::wstring(L"18446744073709551615"), buf); |
| 646 | } |
| 647 | |
| 648 | TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) { |
| 649 | constexpr size_t nchars = 32; |
| 650 | wchar_t buf[nchars]; |
| 651 | |
| 652 | swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX); |
| 653 | EXPECT_EQ(std::wstring(L"18446744073709551615"), buf); |
| 654 | } |
| 655 | |
Dan Albert | 9601f16 | 2017-08-09 14:59:06 -0700 | [diff] [blame] | 656 | TEST(STDIO_TEST, swprintf_ls) { |
| 657 | constexpr size_t nchars = 32; |
| 658 | wchar_t buf[nchars]; |
| 659 | |
| 660 | static const wchar_t kWideString[] = L"Hello\uff41 World"; |
| 661 | ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString)); |
| 662 | ASSERT_EQ(std::wstring(kWideString), buf); |
| 663 | ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString)); |
| 664 | ASSERT_EQ(std::wstring(kWideString), buf); |
| 665 | } |
| 666 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 667 | TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls. |
| 668 | constexpr size_t nchars = 32; |
| 669 | wchar_t buf[nchars]; |
| 670 | |
| 671 | static const wchar_t kWideString[] = L"Hello\uff41 World"; |
| 672 | ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString)); |
| 673 | ASSERT_EQ(std::wstring(kWideString), buf); |
| 674 | ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString)); |
| 675 | ASSERT_EQ(std::wstring(kWideString), buf); |
| 676 | } |
| 677 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 678 | TEST(STDIO_TEST, snprintf_d_INT_MAX) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 679 | char buf[BUFSIZ]; |
| 680 | snprintf(buf, sizeof(buf), "%d", INT_MAX); |
| 681 | EXPECT_STREQ("2147483647", buf); |
| 682 | } |
| 683 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 684 | TEST(STDIO_TEST, snprintf_d_INT_MIN) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 685 | char buf[BUFSIZ]; |
| 686 | snprintf(buf, sizeof(buf), "%d", INT_MIN); |
| 687 | EXPECT_STREQ("-2147483648", buf); |
| 688 | } |
| 689 | |
Elliott Hughes | 618303c | 2017-11-02 16:58:44 -0700 | [diff] [blame^] | 690 | TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) { |
| 691 | char buf[BUFSIZ]; |
| 692 | snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX); |
| 693 | EXPECT_STREQ("9223372036854775807", buf); |
| 694 | } |
| 695 | |
| 696 | TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) { |
| 697 | char buf[BUFSIZ]; |
| 698 | snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN); |
| 699 | EXPECT_STREQ("-9223372036854775808", buf); |
| 700 | } |
| 701 | |
| 702 | TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) { |
| 703 | char buf[BUFSIZ]; |
| 704 | snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX); |
| 705 | EXPECT_STREQ("18446744073709551615", buf); |
| 706 | } |
| 707 | |
| 708 | TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) { |
| 709 | char buf[BUFSIZ]; |
| 710 | snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX); |
| 711 | EXPECT_STREQ("18446744073709551615", buf); |
| 712 | } |
| 713 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 714 | TEST(STDIO_TEST, snprintf_ld_LONG_MAX) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 715 | char buf[BUFSIZ]; |
| 716 | snprintf(buf, sizeof(buf), "%ld", LONG_MAX); |
Josh Gao | b36efa4 | 2016-09-15 13:55:41 -0700 | [diff] [blame] | 717 | #if defined(__LP64__) |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 718 | EXPECT_STREQ("9223372036854775807", buf); |
| 719 | #else |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 720 | EXPECT_STREQ("2147483647", buf); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 721 | #endif |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 724 | TEST(STDIO_TEST, snprintf_ld_LONG_MIN) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 725 | char buf[BUFSIZ]; |
| 726 | snprintf(buf, sizeof(buf), "%ld", LONG_MIN); |
Josh Gao | b36efa4 | 2016-09-15 13:55:41 -0700 | [diff] [blame] | 727 | #if defined(__LP64__) |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 728 | EXPECT_STREQ("-9223372036854775808", buf); |
| 729 | #else |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 730 | EXPECT_STREQ("-2147483648", buf); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 731 | #endif |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 734 | TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 735 | char buf[BUFSIZ]; |
| 736 | snprintf(buf, sizeof(buf), "%lld", LLONG_MAX); |
| 737 | EXPECT_STREQ("9223372036854775807", buf); |
| 738 | } |
| 739 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 740 | TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 741 | char buf[BUFSIZ]; |
| 742 | snprintf(buf, sizeof(buf), "%lld", LLONG_MIN); |
| 743 | EXPECT_STREQ("-9223372036854775808", buf); |
| 744 | } |
| 745 | |
Elliott Hughes | 2f9c8ce | 2017-11-01 13:54:47 -0700 | [diff] [blame] | 746 | TEST(STDIO_TEST, snprintf_o_UINT_MAX) { |
| 747 | char buf[BUFSIZ]; |
| 748 | snprintf(buf, sizeof(buf), "%o", UINT_MAX); |
| 749 | EXPECT_STREQ("37777777777", buf); |
| 750 | } |
| 751 | |
| 752 | TEST(STDIO_TEST, snprintf_u_UINT_MAX) { |
| 753 | char buf[BUFSIZ]; |
| 754 | snprintf(buf, sizeof(buf), "%u", UINT_MAX); |
| 755 | EXPECT_STREQ("4294967295", buf); |
| 756 | } |
| 757 | |
| 758 | TEST(STDIO_TEST, snprintf_x_UINT_MAX) { |
| 759 | char buf[BUFSIZ]; |
| 760 | snprintf(buf, sizeof(buf), "%x", UINT_MAX); |
| 761 | EXPECT_STREQ("ffffffff", buf); |
| 762 | } |
| 763 | |
| 764 | TEST(STDIO_TEST, snprintf_X_UINT_MAX) { |
| 765 | char buf[BUFSIZ]; |
| 766 | snprintf(buf, sizeof(buf), "%X", UINT_MAX); |
| 767 | EXPECT_STREQ("FFFFFFFF", buf); |
| 768 | } |
| 769 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 770 | TEST(STDIO_TEST, snprintf_e) { |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 771 | char buf[BUFSIZ]; |
| 772 | |
| 773 | snprintf(buf, sizeof(buf), "%e", 1.5); |
| 774 | EXPECT_STREQ("1.500000e+00", buf); |
| 775 | |
| 776 | snprintf(buf, sizeof(buf), "%Le", 1.5l); |
| 777 | EXPECT_STREQ("1.500000e+00", buf); |
| 778 | } |
| 779 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 780 | TEST(STDIO_TEST, snprintf_negative_zero_5084292) { |
Elliott Hughes | e77f38f | 2014-05-14 12:39:12 -0700 | [diff] [blame] | 781 | char buf[BUFSIZ]; |
| 782 | |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 783 | snprintf(buf, sizeof(buf), "%e", -0.0); |
| 784 | EXPECT_STREQ("-0.000000e+00", buf); |
| 785 | snprintf(buf, sizeof(buf), "%E", -0.0); |
| 786 | EXPECT_STREQ("-0.000000E+00", buf); |
Elliott Hughes | e77f38f | 2014-05-14 12:39:12 -0700 | [diff] [blame] | 787 | snprintf(buf, sizeof(buf), "%f", -0.0); |
| 788 | EXPECT_STREQ("-0.000000", buf); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 789 | snprintf(buf, sizeof(buf), "%F", -0.0); |
| 790 | EXPECT_STREQ("-0.000000", buf); |
| 791 | snprintf(buf, sizeof(buf), "%g", -0.0); |
| 792 | EXPECT_STREQ("-0", buf); |
| 793 | snprintf(buf, sizeof(buf), "%G", -0.0); |
| 794 | EXPECT_STREQ("-0", buf); |
| 795 | snprintf(buf, sizeof(buf), "%a", -0.0); |
| 796 | EXPECT_STREQ("-0x0p+0", buf); |
| 797 | snprintf(buf, sizeof(buf), "%A", -0.0); |
| 798 | EXPECT_STREQ("-0X0P+0", buf); |
Elliott Hughes | e77f38f | 2014-05-14 12:39:12 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 801 | TEST(STDIO_TEST, snprintf_utf8_15439554) { |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 802 | locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0); |
Wally Yau | a40fdbd | 2014-08-26 09:47:23 -0700 | [diff] [blame] | 803 | locale_t old_locale = uselocale(cloc); |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 804 | |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 805 | // http://b/15439554 |
| 806 | char buf[BUFSIZ]; |
| 807 | |
| 808 | // 1-byte character. |
| 809 | snprintf(buf, sizeof(buf), "%dx%d", 1, 2); |
| 810 | EXPECT_STREQ("1x2", buf); |
| 811 | // 2-byte character. |
| 812 | snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2); |
| 813 | EXPECT_STREQ("1¢2", buf); |
| 814 | // 3-byte character. |
| 815 | snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2); |
| 816 | EXPECT_STREQ("1€2", buf); |
| 817 | // 4-byte character. |
| 818 | snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2); |
| 819 | EXPECT_STREQ("1ð¤¢2", buf); |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 820 | |
Wally Yau | a40fdbd | 2014-08-26 09:47:23 -0700 | [diff] [blame] | 821 | uselocale(old_locale); |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 822 | freelocale(cloc); |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Elliott Hughes | 43f7c87 | 2016-02-05 11:18:41 -0800 | [diff] [blame] | 825 | static void* snprintf_small_stack_fn(void*) { |
| 826 | // Make life (realistically) hard for ourselves by allocating our own buffer for the result. |
| 827 | char buf[PATH_MAX]; |
| 828 | snprintf(buf, sizeof(buf), "/proc/%d", getpid()); |
| 829 | return nullptr; |
| 830 | } |
| 831 | |
| 832 | TEST(STDIO_TEST, snprintf_small_stack) { |
| 833 | // Is it safe to call snprintf on a thread with a small stack? |
| 834 | // (The snprintf implementation puts some pretty large buffers on the stack.) |
| 835 | pthread_attr_t a; |
| 836 | ASSERT_EQ(0, pthread_attr_init(&a)); |
| 837 | ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN)); |
| 838 | |
| 839 | pthread_t t; |
| 840 | ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr)); |
| 841 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 842 | } |
| 843 | |
Elliott Hughes | 8200e55 | 2016-02-05 21:57:37 -0800 | [diff] [blame] | 844 | TEST(STDIO_TEST, snprintf_asterisk_overflow) { |
| 845 | char buf[128]; |
| 846 | ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!')); |
| 847 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!')); |
| 848 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!')); |
| 849 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!')); |
| 850 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!')); |
| 851 | |
| 852 | // INT_MAX-1, INT_MAX, INT_MAX+1. |
| 853 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!')); |
| 854 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!')); |
| 855 | ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!')); |
| 856 | ASSERT_EQ(ENOMEM, errno); |
| 857 | } |
| 858 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 859 | TEST(STDIO_TEST, fprintf) { |
| 860 | TemporaryFile tf; |
| 861 | |
| 862 | FILE* tfile = fdopen(tf.fd, "r+"); |
| 863 | ASSERT_TRUE(tfile != nullptr); |
| 864 | |
| 865 | ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc")); |
| 866 | AssertFileIs(tfile, "123 abc"); |
| 867 | fclose(tfile); |
| 868 | } |
| 869 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 870 | TEST(STDIO_TEST, fprintf_failures_7229520) { |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 871 | // http://b/7229520 |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 872 | FILE* fp; |
| 873 | |
| 874 | // Unbuffered case where the fprintf(3) itself fails. |
| 875 | ASSERT_NE(nullptr, fp = tmpfile()); |
| 876 | setbuf(fp, NULL); |
| 877 | ASSERT_EQ(4, fprintf(fp, "epic")); |
| 878 | ASSERT_EQ(0, close(fileno(fp))); |
| 879 | ASSERT_EQ(-1, fprintf(fp, "fail")); |
| 880 | ASSERT_EQ(-1, fclose(fp)); |
| 881 | |
| 882 | // Buffered case where we won't notice until the fclose(3). |
| 883 | // It's likely this is what was actually seen in http://b/7229520, |
| 884 | // and that expecting fprintf to fail is setting yourself up for |
| 885 | // disappointment. Remember to check fclose(3)'s return value, kids! |
| 886 | ASSERT_NE(nullptr, fp = tmpfile()); |
| 887 | ASSERT_EQ(4, fprintf(fp, "epic")); |
| 888 | ASSERT_EQ(0, close(fileno(fp))); |
| 889 | ASSERT_EQ(4, fprintf(fp, "fail")); |
| 890 | ASSERT_EQ(-1, fclose(fp)); |
| 891 | } |
| 892 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 893 | TEST(STDIO_TEST, popen) { |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 894 | FILE* fp = popen("cat /proc/version", "r"); |
| 895 | ASSERT_TRUE(fp != NULL); |
| 896 | |
| 897 | char buf[16]; |
| 898 | char* s = fgets(buf, sizeof(buf), fp); |
| 899 | buf[13] = '\0'; |
| 900 | ASSERT_STREQ("Linux version", s); |
| 901 | |
| 902 | ASSERT_EQ(0, pclose(fp)); |
| 903 | } |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 904 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 905 | TEST(STDIO_TEST, getc) { |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 906 | FILE* fp = fopen("/proc/version", "r"); |
| 907 | ASSERT_TRUE(fp != NULL); |
| 908 | ASSERT_EQ('L', getc(fp)); |
| 909 | ASSERT_EQ('i', getc(fp)); |
| 910 | ASSERT_EQ('n', getc(fp)); |
| 911 | ASSERT_EQ('u', getc(fp)); |
| 912 | ASSERT_EQ('x', getc(fp)); |
| 913 | fclose(fp); |
| 914 | } |
| 915 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 916 | TEST(STDIO_TEST, putc) { |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 917 | FILE* fp = fopen("/proc/version", "r"); |
| 918 | ASSERT_TRUE(fp != NULL); |
| 919 | ASSERT_EQ(EOF, putc('x', fp)); |
| 920 | fclose(fp); |
| 921 | } |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 922 | |
Elliott Hughes | 7f0849f | 2016-08-26 16:17:17 -0700 | [diff] [blame] | 923 | TEST(STDIO_TEST, sscanf_swscanf) { |
| 924 | struct stuff { |
| 925 | char s1[123]; |
| 926 | int i1; |
| 927 | double d1; |
| 928 | float f1; |
| 929 | char s2[123]; |
| 930 | |
| 931 | void Check() { |
| 932 | ASSERT_STREQ("hello", s1); |
| 933 | ASSERT_EQ(123, i1); |
| 934 | ASSERT_DOUBLE_EQ(1.23, d1); |
| 935 | ASSERT_FLOAT_EQ(9.0f, f1); |
| 936 | ASSERT_STREQ("world", s2); |
| 937 | } |
| 938 | } s; |
| 939 | |
| 940 | memset(&s, 0, sizeof(s)); |
| 941 | ASSERT_EQ(5, sscanf(" hello 123 1.23 0x1.2p3 world", |
| 942 | "%s %i %lf %f %s", |
| 943 | s.s1, &s.i1, &s.d1, &s.f1, s.s2)); |
| 944 | s.Check(); |
| 945 | |
| 946 | memset(&s, 0, sizeof(s)); |
| 947 | ASSERT_EQ(5, swscanf(L" hello 123 1.23 0x1.2p3 world", |
| 948 | L"%s %i %lf %f %s", |
| 949 | s.s1, &s.i1, &s.d1, &s.f1, s.s2)); |
| 950 | s.Check(); |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 951 | } |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 952 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 953 | TEST(STDIO_TEST, cantwrite_EBADF) { |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 954 | // If we open a file read-only... |
| 955 | FILE* fp = fopen("/proc/version", "r"); |
| 956 | |
| 957 | // ...all attempts to write to that file should return failure. |
| 958 | |
| 959 | // They should also set errno to EBADF. This isn't POSIX, but it's traditional. |
| 960 | // glibc gets the wide-character functions wrong. |
| 961 | |
| 962 | errno = 0; |
| 963 | EXPECT_EQ(EOF, putc('x', fp)); |
| 964 | EXPECT_EQ(EBADF, errno); |
| 965 | |
| 966 | errno = 0; |
| 967 | EXPECT_EQ(EOF, fprintf(fp, "hello")); |
| 968 | EXPECT_EQ(EBADF, errno); |
| 969 | |
| 970 | errno = 0; |
| 971 | EXPECT_EQ(EOF, fwprintf(fp, L"hello")); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 972 | #if defined(__BIONIC__) |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 973 | EXPECT_EQ(EBADF, errno); |
| 974 | #endif |
| 975 | |
| 976 | errno = 0; |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 977 | EXPECT_EQ(0U, fwrite("hello", 1, 2, fp)); |
| 978 | EXPECT_EQ(EBADF, errno); |
| 979 | |
| 980 | errno = 0; |
| 981 | EXPECT_EQ(EOF, fputs("hello", fp)); |
| 982 | EXPECT_EQ(EBADF, errno); |
| 983 | |
| 984 | errno = 0; |
| 985 | EXPECT_EQ(WEOF, fputwc(L'x', fp)); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 986 | #if defined(__BIONIC__) |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 987 | EXPECT_EQ(EBADF, errno); |
| 988 | #endif |
| 989 | } |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 990 | |
| 991 | // Tests that we can only have a consistent and correct fpos_t when using |
| 992 | // 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] | 993 | TEST(STDIO_TEST, consistent_fpos_t) { |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 994 | ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8")); |
| 995 | uselocale(LC_GLOBAL_LOCALE); |
| 996 | |
| 997 | FILE* fp = tmpfile(); |
| 998 | ASSERT_TRUE(fp != NULL); |
| 999 | |
| 1000 | wchar_t mb_one_bytes = L'h'; |
| 1001 | wchar_t mb_two_bytes = 0x00a2; |
| 1002 | wchar_t mb_three_bytes = 0x20ac; |
| 1003 | wchar_t mb_four_bytes = 0x24b62; |
| 1004 | |
| 1005 | // Write to file. |
| 1006 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp))); |
| 1007 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp))); |
| 1008 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp))); |
| 1009 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp))); |
| 1010 | |
| 1011 | rewind(fp); |
| 1012 | |
| 1013 | // Record each character position. |
| 1014 | fpos_t pos1; |
| 1015 | fpos_t pos2; |
| 1016 | fpos_t pos3; |
| 1017 | fpos_t pos4; |
| 1018 | fpos_t pos5; |
| 1019 | EXPECT_EQ(0, fgetpos(fp, &pos1)); |
| 1020 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1021 | EXPECT_EQ(0, fgetpos(fp, &pos2)); |
| 1022 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1023 | EXPECT_EQ(0, fgetpos(fp, &pos3)); |
| 1024 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1025 | EXPECT_EQ(0, fgetpos(fp, &pos4)); |
| 1026 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1027 | EXPECT_EQ(0, fgetpos(fp, &pos5)); |
| 1028 | |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 1029 | #if defined(__BIONIC__) |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1030 | // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD |
| 1031 | // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In |
| 1032 | // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE |
| 1033 | // structure. |
| 1034 | ASSERT_EQ(0, static_cast<off_t>(pos1)); |
| 1035 | ASSERT_EQ(1, static_cast<off_t>(pos2)); |
| 1036 | ASSERT_EQ(3, static_cast<off_t>(pos3)); |
| 1037 | ASSERT_EQ(6, static_cast<off_t>(pos4)); |
| 1038 | ASSERT_EQ(10, static_cast<off_t>(pos5)); |
| 1039 | #endif |
| 1040 | |
| 1041 | // Exercise back and forth movements of the position. |
| 1042 | ASSERT_EQ(0, fsetpos(fp, &pos2)); |
| 1043 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1044 | ASSERT_EQ(0, fsetpos(fp, &pos1)); |
| 1045 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1046 | ASSERT_EQ(0, fsetpos(fp, &pos4)); |
| 1047 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1048 | ASSERT_EQ(0, fsetpos(fp, &pos3)); |
| 1049 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1050 | ASSERT_EQ(0, fsetpos(fp, &pos5)); |
| 1051 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 1052 | |
| 1053 | fclose(fp); |
| 1054 | } |
| 1055 | |
| 1056 | // Exercise the interaction between fpos and seek. |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1057 | TEST(STDIO_TEST, fpos_t_and_seek) { |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1058 | ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8")); |
| 1059 | uselocale(LC_GLOBAL_LOCALE); |
| 1060 | |
Calin Juravle | 9b95ea9 | 2014-05-14 17:07:10 +0100 | [diff] [blame] | 1061 | // In glibc-2.16 fseek doesn't work properly in wide mode |
| 1062 | // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is |
| 1063 | // to close and re-open the file. We do it in order to make the test pass |
| 1064 | // with all glibcs. |
| 1065 | |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1066 | TemporaryFile tf; |
| 1067 | FILE* fp = fdopen(tf.fd, "w+"); |
| 1068 | ASSERT_TRUE(fp != NULL); |
| 1069 | |
| 1070 | wchar_t mb_two_bytes = 0x00a2; |
| 1071 | wchar_t mb_three_bytes = 0x20ac; |
| 1072 | wchar_t mb_four_bytes = 0x24b62; |
| 1073 | |
| 1074 | // Write to file. |
| 1075 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp))); |
| 1076 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp))); |
| 1077 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp))); |
| 1078 | |
| 1079 | fflush(fp); |
| 1080 | fclose(fp); |
| 1081 | |
| 1082 | fp = fopen(tf.filename, "r"); |
| 1083 | ASSERT_TRUE(fp != NULL); |
| 1084 | |
| 1085 | // Store a valid position. |
| 1086 | fpos_t mb_two_bytes_pos; |
| 1087 | ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos)); |
| 1088 | |
| 1089 | // Move inside mb_four_bytes with fseek. |
| 1090 | long offset_inside_mb = 6; |
| 1091 | ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET)); |
| 1092 | |
| 1093 | // Store the "inside multi byte" position. |
| 1094 | fpos_t pos_inside_mb; |
| 1095 | ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb)); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 1096 | #if defined(__BIONIC__) |
| 1097 | ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb)); |
| 1098 | #endif |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1099 | |
| 1100 | // Reading from within a byte should produce an error. |
| 1101 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 1102 | ASSERT_EQ(EILSEQ, errno); |
| 1103 | |
| 1104 | // Reverting to a valid position should work. |
| 1105 | ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos)); |
| 1106 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 1107 | |
| 1108 | // Moving withing a multi byte with fsetpos should work but reading should |
| 1109 | // produce an error. |
| 1110 | ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb)); |
| 1111 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 1112 | ASSERT_EQ(EILSEQ, errno); |
| 1113 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1114 | ASSERT_EQ(0, fclose(fp)); |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 1115 | } |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1116 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1117 | TEST(STDIO_TEST, fmemopen) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1118 | char buf[16]; |
| 1119 | memset(buf, 0, sizeof(buf)); |
| 1120 | FILE* fp = fmemopen(buf, sizeof(buf), "r+"); |
| 1121 | ASSERT_EQ('<', fputc('<', fp)); |
| 1122 | ASSERT_NE(EOF, fputs("abc>\n", fp)); |
| 1123 | fflush(fp); |
| 1124 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1125 | // We wrote to the buffer... |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1126 | ASSERT_STREQ("<abc>\n", buf); |
| 1127 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1128 | // And can read back from the file. |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 1129 | AssertFileIs(fp, "<abc>\n", true); |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1130 | ASSERT_EQ(0, fclose(fp)); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1133 | TEST(STDIO_TEST, fmemopen_nullptr) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1134 | FILE* fp = fmemopen(nullptr, 128, "r+"); |
| 1135 | ASSERT_NE(EOF, fputs("xyz\n", fp)); |
| 1136 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 1137 | AssertFileIs(fp, "xyz\n", true); |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1138 | ASSERT_EQ(0, fclose(fp)); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1141 | TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) { |
| 1142 | FILE* fp; |
| 1143 | char buf[8]; |
| 1144 | |
| 1145 | // POSIX: "When a stream open for writing is flushed or closed, a null byte |
| 1146 | // shall be written at the current position or at the end of the buffer, |
| 1147 | // depending on the size of the contents." |
| 1148 | memset(buf, 'x', sizeof(buf)); |
| 1149 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w")); |
| 1150 | // Even with nothing written (and not in truncate mode), we'll flush a NUL... |
| 1151 | ASSERT_EQ(0, fflush(fp)); |
| 1152 | EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf))); |
| 1153 | // Now write and check that the NUL moves along with our writes... |
| 1154 | ASSERT_NE(EOF, fputs("hello", fp)); |
| 1155 | ASSERT_EQ(0, fflush(fp)); |
| 1156 | EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf))); |
| 1157 | ASSERT_NE(EOF, fputs("wo", fp)); |
| 1158 | ASSERT_EQ(0, fflush(fp)); |
| 1159 | EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf))); |
| 1160 | ASSERT_EQ(0, fclose(fp)); |
| 1161 | |
| 1162 | // "If a stream open for update is flushed or closed and the last write has |
| 1163 | // advanced the current buffer size, a null byte shall be written at the end |
| 1164 | // of the buffer if it fits." |
| 1165 | memset(buf, 'x', sizeof(buf)); |
| 1166 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+")); |
| 1167 | // Nothing written yet, so no advance... |
| 1168 | ASSERT_EQ(0, fflush(fp)); |
| 1169 | EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf))); |
| 1170 | ASSERT_NE(EOF, fputs("hello", fp)); |
| 1171 | ASSERT_EQ(0, fclose(fp)); |
| 1172 | } |
| 1173 | |
| 1174 | TEST(STDIO_TEST, fmemopen_size) { |
| 1175 | FILE* fp; |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1176 | char buf[16]; |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1177 | memset(buf, 'x', sizeof(buf)); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1178 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1179 | // POSIX: "The stream shall also maintain the size of the current buffer |
| 1180 | // contents; use of fseek() or fseeko() on the stream with SEEK_END shall |
| 1181 | // seek relative to this size." |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1182 | |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1183 | // "For modes r and r+ the size shall be set to the value given by the size |
| 1184 | // argument." |
| 1185 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r")); |
| 1186 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1187 | EXPECT_EQ(16, ftell(fp)); |
| 1188 | EXPECT_EQ(16, ftello(fp)); |
| 1189 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1190 | EXPECT_EQ(16, ftell(fp)); |
| 1191 | EXPECT_EQ(16, ftello(fp)); |
| 1192 | ASSERT_EQ(0, fclose(fp)); |
| 1193 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+")); |
| 1194 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1195 | EXPECT_EQ(16, ftell(fp)); |
| 1196 | EXPECT_EQ(16, ftello(fp)); |
| 1197 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1198 | EXPECT_EQ(16, ftell(fp)); |
| 1199 | EXPECT_EQ(16, ftello(fp)); |
| 1200 | ASSERT_EQ(0, fclose(fp)); |
| 1201 | |
| 1202 | // "For modes w and w+ the initial size shall be zero..." |
| 1203 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w")); |
| 1204 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1205 | EXPECT_EQ(0, ftell(fp)); |
| 1206 | EXPECT_EQ(0, ftello(fp)); |
| 1207 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1208 | EXPECT_EQ(0, ftell(fp)); |
| 1209 | EXPECT_EQ(0, ftello(fp)); |
| 1210 | ASSERT_EQ(0, fclose(fp)); |
| 1211 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+")); |
| 1212 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1213 | EXPECT_EQ(0, ftell(fp)); |
| 1214 | EXPECT_EQ(0, ftello(fp)); |
| 1215 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1216 | EXPECT_EQ(0, ftell(fp)); |
| 1217 | EXPECT_EQ(0, ftello(fp)); |
| 1218 | ASSERT_EQ(0, fclose(fp)); |
| 1219 | |
| 1220 | // "...and for modes a and a+ the initial size shall be: |
| 1221 | // 1. Zero, if buf is a null pointer |
| 1222 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a")); |
| 1223 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1224 | EXPECT_EQ(0, ftell(fp)); |
| 1225 | EXPECT_EQ(0, ftello(fp)); |
| 1226 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1227 | EXPECT_EQ(0, ftell(fp)); |
| 1228 | EXPECT_EQ(0, ftello(fp)); |
| 1229 | ASSERT_EQ(0, fclose(fp)); |
| 1230 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+")); |
| 1231 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1232 | EXPECT_EQ(0, ftell(fp)); |
| 1233 | EXPECT_EQ(0, ftello(fp)); |
| 1234 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1235 | EXPECT_EQ(0, ftell(fp)); |
| 1236 | EXPECT_EQ(0, ftello(fp)); |
| 1237 | ASSERT_EQ(0, fclose(fp)); |
| 1238 | |
| 1239 | // 2. The position of the first null byte in the buffer, if one is found |
| 1240 | memset(buf, 'x', sizeof(buf)); |
| 1241 | buf[3] = '\0'; |
| 1242 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a")); |
| 1243 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1244 | EXPECT_EQ(3, ftell(fp)); |
| 1245 | EXPECT_EQ(3, ftello(fp)); |
| 1246 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1247 | EXPECT_EQ(3, ftell(fp)); |
| 1248 | EXPECT_EQ(3, ftello(fp)); |
| 1249 | ASSERT_EQ(0, fclose(fp)); |
| 1250 | memset(buf, 'x', sizeof(buf)); |
| 1251 | buf[3] = '\0'; |
| 1252 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+")); |
| 1253 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1254 | EXPECT_EQ(3, ftell(fp)); |
| 1255 | EXPECT_EQ(3, ftello(fp)); |
| 1256 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1257 | EXPECT_EQ(3, ftell(fp)); |
| 1258 | EXPECT_EQ(3, ftello(fp)); |
| 1259 | ASSERT_EQ(0, fclose(fp)); |
| 1260 | |
| 1261 | // 3. The value of the size argument, if buf is not a null pointer and no |
| 1262 | // null byte is found. |
| 1263 | memset(buf, 'x', sizeof(buf)); |
| 1264 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a")); |
| 1265 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1266 | EXPECT_EQ(16, ftell(fp)); |
| 1267 | EXPECT_EQ(16, ftello(fp)); |
| 1268 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1269 | EXPECT_EQ(16, ftell(fp)); |
| 1270 | EXPECT_EQ(16, ftello(fp)); |
| 1271 | ASSERT_EQ(0, fclose(fp)); |
| 1272 | memset(buf, 'x', sizeof(buf)); |
| 1273 | ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+")); |
| 1274 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1275 | EXPECT_EQ(16, ftell(fp)); |
| 1276 | EXPECT_EQ(16, ftello(fp)); |
| 1277 | ASSERT_EQ(0, fseeko(fp, 0, SEEK_END)); |
| 1278 | EXPECT_EQ(16, ftell(fp)); |
| 1279 | EXPECT_EQ(16, ftello(fp)); |
| 1280 | ASSERT_EQ(0, fclose(fp)); |
| 1281 | } |
| 1282 | |
| 1283 | TEST(STDIO_TEST, fmemopen_SEEK_END) { |
| 1284 | // fseek SEEK_END is relative to the current string length, not the buffer size. |
| 1285 | FILE* fp; |
| 1286 | char buf[8]; |
| 1287 | memset(buf, 'x', sizeof(buf)); |
| 1288 | strcpy(buf, "str"); |
| 1289 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+")); |
| 1290 | ASSERT_NE(EOF, fputs("string", fp)); |
| 1291 | EXPECT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1292 | EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp)); |
| 1293 | EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp)); |
| 1294 | EXPECT_EQ(0, fclose(fp)); |
| 1295 | |
| 1296 | // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather |
| 1297 | // than adding). |
| 1298 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+")); |
| 1299 | ASSERT_NE(EOF, fputs("54321", fp)); |
| 1300 | EXPECT_EQ(0, fseek(fp, -2, SEEK_END)); |
| 1301 | EXPECT_EQ('2', fgetc(fp)); |
| 1302 | EXPECT_EQ(0, fclose(fp)); |
| 1303 | } |
| 1304 | |
| 1305 | TEST(STDIO_TEST, fmemopen_seek_invalid) { |
| 1306 | char buf[8]; |
| 1307 | memset(buf, 'x', sizeof(buf)); |
| 1308 | FILE* fp = fmemopen(buf, sizeof(buf), "w"); |
| 1309 | ASSERT_TRUE(fp != nullptr); |
| 1310 | |
| 1311 | // POSIX: "An attempt to seek ... to a negative position or to a position |
| 1312 | // larger than the buffer size given in the size argument shall fail." |
| 1313 | // (There's no mention of what errno should be set to, and glibc doesn't |
| 1314 | // set errno in any of these cases.) |
| 1315 | EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET)); |
| 1316 | EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET)); |
| 1317 | EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET)); |
| 1318 | EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET)); |
| 1319 | } |
| 1320 | |
| 1321 | TEST(STDIO_TEST, fmemopen_read_EOF) { |
| 1322 | // POSIX: "A read operation on the stream shall not advance the current |
| 1323 | // buffer position beyond the current buffer size." |
| 1324 | char buf[8]; |
| 1325 | memset(buf, 'x', sizeof(buf)); |
| 1326 | FILE* fp = fmemopen(buf, sizeof(buf), "r"); |
| 1327 | ASSERT_TRUE(fp != nullptr); |
| 1328 | char buf2[BUFSIZ]; |
| 1329 | ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp)); |
| 1330 | // POSIX: "Reaching the buffer size in a read operation shall count as |
| 1331 | // end-of-file. |
| 1332 | ASSERT_TRUE(feof(fp)); |
| 1333 | ASSERT_EQ(EOF, fgetc(fp)); |
| 1334 | ASSERT_EQ(0, fclose(fp)); |
| 1335 | } |
| 1336 | |
| 1337 | TEST(STDIO_TEST, fmemopen_read_null_bytes) { |
| 1338 | // POSIX: "Null bytes in the buffer shall have no special meaning for reads." |
| 1339 | char buf[] = "h\0e\0l\0l\0o"; |
| 1340 | FILE* fp = fmemopen(buf, sizeof(buf), "r"); |
| 1341 | ASSERT_TRUE(fp != nullptr); |
| 1342 | ASSERT_EQ('h', fgetc(fp)); |
| 1343 | ASSERT_EQ(0, fgetc(fp)); |
| 1344 | ASSERT_EQ('e', fgetc(fp)); |
| 1345 | ASSERT_EQ(0, fgetc(fp)); |
| 1346 | ASSERT_EQ('l', fgetc(fp)); |
| 1347 | ASSERT_EQ(0, fgetc(fp)); |
| 1348 | // POSIX: "The read operation shall start at the current buffer position of |
| 1349 | // the stream." |
| 1350 | char buf2[8]; |
| 1351 | memset(buf2, 'x', sizeof(buf2)); |
| 1352 | ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp)); |
| 1353 | ASSERT_EQ('l', buf2[0]); |
| 1354 | ASSERT_EQ(0, buf2[1]); |
| 1355 | ASSERT_EQ('o', buf2[2]); |
| 1356 | ASSERT_EQ(0, buf2[3]); |
| 1357 | for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i; |
| 1358 | ASSERT_TRUE(feof(fp)); |
| 1359 | ASSERT_EQ(0, fclose(fp)); |
| 1360 | } |
| 1361 | |
| 1362 | TEST(STDIO_TEST, fmemopen_write) { |
| 1363 | FILE* fp; |
| 1364 | char buf[8]; |
| 1365 | |
| 1366 | // POSIX: "A write operation shall start either at the current position of |
| 1367 | // the stream (if mode has not specified 'a' as the first character)..." |
| 1368 | memset(buf, 'x', sizeof(buf)); |
| 1369 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+")); |
| 1370 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1371 | ASSERT_EQ(0, fseek(fp, 2, SEEK_SET)); |
| 1372 | ASSERT_EQ(' ', fputc(' ', fp)); |
| 1373 | EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf))); |
| 1374 | ASSERT_EQ(0, fclose(fp)); |
| 1375 | |
| 1376 | // "...or at the current size of the stream (if mode had 'a' as the first |
| 1377 | // character)." (See the fmemopen_size test for what "size" means, but for |
| 1378 | // mode "a", it's the first NUL byte.) |
| 1379 | memset(buf, 'x', sizeof(buf)); |
| 1380 | buf[3] = '\0'; |
| 1381 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+")); |
| 1382 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1383 | ASSERT_EQ(' ', fputc(' ', fp)); |
| 1384 | EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf))); |
| 1385 | ASSERT_EQ(0, fclose(fp)); |
| 1386 | |
| 1387 | // "If the current position at the end of the write is larger than the |
| 1388 | // current buffer size, the current buffer size shall be set to the current |
| 1389 | // position." (See the fmemopen_size test for what "size" means, but to |
| 1390 | // query it we SEEK_END with offset 0, and then ftell.) |
| 1391 | memset(buf, 'x', sizeof(buf)); |
| 1392 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+")); |
| 1393 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1394 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1395 | EXPECT_EQ(0, ftell(fp)); |
| 1396 | ASSERT_EQ(' ', fputc(' ', fp)); |
| 1397 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1398 | EXPECT_EQ(1, ftell(fp)); |
| 1399 | ASSERT_NE(EOF, fputs("123", fp)); |
| 1400 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1401 | EXPECT_EQ(4, ftell(fp)); |
| 1402 | EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf))); |
| 1403 | ASSERT_EQ(0, fclose(fp)); |
| 1404 | } |
| 1405 | |
| 1406 | TEST(STDIO_TEST, fmemopen_write_EOF) { |
| 1407 | // POSIX: "A write operation on the stream shall not advance the current |
| 1408 | // buffer size beyond the size given in the size argument." |
| 1409 | FILE* fp; |
| 1410 | |
| 1411 | // Scalar writes... |
| 1412 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w")); |
| 1413 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1414 | ASSERT_EQ('x', fputc('x', fp)); |
| 1415 | ASSERT_EQ('x', fputc('x', fp)); |
| 1416 | ASSERT_EQ('x', fputc('x', fp)); |
| 1417 | ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL. |
| 1418 | ASSERT_EQ(0, fclose(fp)); |
| 1419 | |
| 1420 | // Vector writes... |
| 1421 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w")); |
| 1422 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1423 | ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp)); |
| 1424 | ASSERT_EQ(0, fclose(fp)); |
| 1425 | } |
| 1426 | |
| 1427 | TEST(STDIO_TEST, fmemopen_initial_position) { |
| 1428 | // POSIX: "The ... current position in the buffer ... shall be initially |
| 1429 | // set to either the beginning of the buffer (for r and w modes) ..." |
| 1430 | char buf[] = "hello\0world"; |
| 1431 | FILE* fp; |
| 1432 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r")); |
| 1433 | EXPECT_EQ(0L, ftell(fp)); |
| 1434 | EXPECT_EQ(0, fclose(fp)); |
| 1435 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w")); |
| 1436 | EXPECT_EQ(0L, ftell(fp)); |
| 1437 | EXPECT_EQ(0, fclose(fp)); |
| 1438 | buf[0] = 'h'; // (Undo the effects of the above.) |
| 1439 | |
| 1440 | // POSIX: "...or to the first null byte in the buffer (for a modes)." |
| 1441 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a")); |
| 1442 | EXPECT_EQ(5L, ftell(fp)); |
| 1443 | EXPECT_EQ(0, fclose(fp)); |
| 1444 | |
| 1445 | // POSIX: "If no null byte is found in append mode, the initial position |
| 1446 | // shall be set to one byte after the end of the buffer." |
| 1447 | memset(buf, 'x', sizeof(buf)); |
| 1448 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a")); |
| 1449 | EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp)); |
| 1450 | EXPECT_EQ(0, fclose(fp)); |
| 1451 | } |
| 1452 | |
| 1453 | TEST(STDIO_TEST, fmemopen_initial_position_allocated) { |
| 1454 | // POSIX: "If buf is a null pointer, the initial position shall always be |
| 1455 | // set to the beginning of the buffer." |
| 1456 | FILE* fp = fmemopen(nullptr, 128, "a+"); |
| 1457 | ASSERT_TRUE(fp != nullptr); |
| 1458 | EXPECT_EQ(0L, ftell(fp)); |
| 1459 | EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET)); |
| 1460 | EXPECT_EQ(0, fclose(fp)); |
| 1461 | } |
| 1462 | |
| 1463 | TEST(STDIO_TEST, fmemopen_zero_length) { |
| 1464 | // POSIX says it's up to the implementation whether or not you can have a |
| 1465 | // zero-length buffer (but "A future version of this standard may require |
| 1466 | // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22 |
| 1467 | // agreed that you couldn't, but glibc >= 2.22 allows it for consistency. |
| 1468 | FILE* fp; |
| 1469 | char buf[16]; |
| 1470 | ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+")); |
| 1471 | ASSERT_EQ(EOF, fgetc(fp)); |
| 1472 | ASSERT_TRUE(feof(fp)); |
| 1473 | ASSERT_EQ(0, fclose(fp)); |
| 1474 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+")); |
| 1475 | ASSERT_EQ(EOF, fgetc(fp)); |
| 1476 | ASSERT_TRUE(feof(fp)); |
| 1477 | ASSERT_EQ(0, fclose(fp)); |
| 1478 | |
| 1479 | ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+")); |
| 1480 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1481 | ASSERT_EQ(EOF, fputc('x', fp)); |
| 1482 | ASSERT_EQ(0, fclose(fp)); |
| 1483 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+")); |
| 1484 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1485 | ASSERT_EQ(EOF, fputc('x', fp)); |
| 1486 | ASSERT_EQ(0, fclose(fp)); |
| 1487 | } |
| 1488 | |
| 1489 | TEST(STDIO_TEST, fmemopen_write_only_allocated) { |
| 1490 | // POSIX says fmemopen "may fail if the mode argument does not include a '+'". |
| 1491 | // BSD fails, glibc doesn't. We side with the more lenient. |
| 1492 | FILE* fp; |
| 1493 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r")); |
| 1494 | ASSERT_EQ(0, fclose(fp)); |
| 1495 | ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w")); |
| 1496 | ASSERT_EQ(0, fclose(fp)); |
| 1497 | } |
| 1498 | |
| 1499 | TEST(STDIO_TEST, fmemopen_fileno) { |
| 1500 | // There's no fd backing an fmemopen FILE*. |
| 1501 | FILE* fp = fmemopen(nullptr, 16, "r"); |
| 1502 | ASSERT_TRUE(fp != nullptr); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1503 | errno = 0; |
Elliott Hughes | 3a4c454 | 2017-07-19 17:20:24 -0700 | [diff] [blame] | 1504 | ASSERT_EQ(-1, fileno(fp)); |
| 1505 | ASSERT_EQ(EBADF, errno); |
| 1506 | ASSERT_EQ(0, fclose(fp)); |
| 1507 | } |
| 1508 | |
| 1509 | TEST(STDIO_TEST, fmemopen_append_after_seek) { |
| 1510 | // In BSD and glibc < 2.22, append mode didn't force writes to append if |
| 1511 | // there had been an intervening seek. |
| 1512 | |
| 1513 | FILE* fp; |
| 1514 | char buf[] = "hello\0world"; |
| 1515 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a")); |
| 1516 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1517 | ASSERT_EQ(0, fseek(fp, 0, SEEK_SET)); |
| 1518 | ASSERT_NE(EOF, fputc('!', fp)); |
| 1519 | EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf))); |
| 1520 | ASSERT_EQ(0, fclose(fp)); |
| 1521 | |
| 1522 | memcpy(buf, "hello\0world", sizeof(buf)); |
| 1523 | ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+")); |
| 1524 | setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens. |
| 1525 | ASSERT_EQ(0, fseek(fp, 0, SEEK_SET)); |
| 1526 | ASSERT_NE(EOF, fputc('!', fp)); |
| 1527 | EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf))); |
| 1528 | ASSERT_EQ(0, fclose(fp)); |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1531 | TEST(STDIO_TEST, open_memstream) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1532 | char* p = nullptr; |
| 1533 | size_t size = 0; |
| 1534 | FILE* fp = open_memstream(&p, &size); |
| 1535 | ASSERT_NE(EOF, fputs("hello, world!", fp)); |
| 1536 | fclose(fp); |
| 1537 | |
| 1538 | ASSERT_STREQ("hello, world!", p); |
| 1539 | ASSERT_EQ(strlen("hello, world!"), size); |
| 1540 | free(p); |
| 1541 | } |
| 1542 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1543 | TEST(STDIO_TEST, open_memstream_EINVAL) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1544 | #if defined(__BIONIC__) |
| 1545 | char* p; |
| 1546 | size_t size; |
| 1547 | |
| 1548 | // Invalid buffer. |
| 1549 | errno = 0; |
| 1550 | ASSERT_EQ(nullptr, open_memstream(nullptr, &size)); |
| 1551 | ASSERT_EQ(EINVAL, errno); |
| 1552 | |
| 1553 | // Invalid size. |
| 1554 | errno = 0; |
| 1555 | ASSERT_EQ(nullptr, open_memstream(&p, nullptr)); |
| 1556 | ASSERT_EQ(EINVAL, errno); |
| 1557 | #else |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1558 | GTEST_LOG_(INFO) << "This test does nothing on glibc.\n"; |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 1559 | #endif |
| 1560 | } |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 1561 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1562 | TEST(STDIO_TEST, fdopen_CLOEXEC) { |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 1563 | int fd = open("/proc/version", O_RDONLY); |
| 1564 | ASSERT_TRUE(fd != -1); |
| 1565 | |
| 1566 | // This fd doesn't have O_CLOEXEC... |
| 1567 | int flags = fcntl(fd, F_GETFD); |
| 1568 | ASSERT_TRUE(flags != -1); |
| 1569 | ASSERT_EQ(0, flags & FD_CLOEXEC); |
| 1570 | |
| 1571 | FILE* fp = fdopen(fd, "re"); |
| 1572 | ASSERT_TRUE(fp != NULL); |
| 1573 | |
| 1574 | // ...but the new one does. |
| 1575 | flags = fcntl(fileno(fp), F_GETFD); |
| 1576 | ASSERT_TRUE(flags != -1); |
| 1577 | ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC); |
| 1578 | |
| 1579 | fclose(fp); |
| 1580 | close(fd); |
| 1581 | } |
| 1582 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1583 | TEST(STDIO_TEST, freopen_CLOEXEC) { |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 1584 | FILE* fp = fopen("/proc/version", "r"); |
| 1585 | ASSERT_TRUE(fp != NULL); |
| 1586 | |
| 1587 | // This FILE* doesn't have O_CLOEXEC... |
| 1588 | int flags = fcntl(fileno(fp), F_GETFD); |
| 1589 | ASSERT_TRUE(flags != -1); |
| 1590 | ASSERT_EQ(0, flags & FD_CLOEXEC); |
| 1591 | |
| 1592 | fp = freopen("/proc/version", "re", fp); |
| 1593 | |
| 1594 | // ...but the new one does. |
| 1595 | flags = fcntl(fileno(fp), F_GETFD); |
| 1596 | ASSERT_TRUE(flags != -1); |
| 1597 | ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC); |
| 1598 | |
| 1599 | fclose(fp); |
| 1600 | } |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 1601 | |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 1602 | TEST(STDIO_TEST, fopen64_freopen64) { |
| 1603 | FILE* fp = fopen64("/proc/version", "r"); |
| 1604 | ASSERT_TRUE(fp != nullptr); |
| 1605 | fp = freopen64("/proc/version", "re", fp); |
| 1606 | ASSERT_TRUE(fp != nullptr); |
| 1607 | fclose(fp); |
| 1608 | } |
| 1609 | |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 1610 | // https://code.google.com/p/android/issues/detail?id=81155 |
| 1611 | // http://b/18556607 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1612 | TEST(STDIO_TEST, fread_unbuffered_pathological_performance) { |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 1613 | FILE* fp = fopen("/dev/zero", "r"); |
| 1614 | ASSERT_TRUE(fp != NULL); |
| 1615 | |
| 1616 | // Make this stream unbuffered. |
| 1617 | setvbuf(fp, 0, _IONBF, 0); |
| 1618 | |
| 1619 | char buf[65*1024]; |
| 1620 | memset(buf, 0xff, sizeof(buf)); |
| 1621 | |
| 1622 | time_t t0 = time(NULL); |
| 1623 | for (size_t i = 0; i < 1024; ++i) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1624 | ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp)); |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 1625 | } |
| 1626 | time_t t1 = time(NULL); |
| 1627 | |
| 1628 | fclose(fp); |
| 1629 | |
| 1630 | // 1024 64KiB reads should have been very quick. |
| 1631 | ASSERT_LE(t1 - t0, 1); |
| 1632 | |
| 1633 | for (size_t i = 0; i < 64*1024; ++i) { |
| 1634 | ASSERT_EQ('\0', buf[i]); |
| 1635 | } |
| 1636 | for (size_t i = 64*1024; i < 65*1024; ++i) { |
| 1637 | ASSERT_EQ('\xff', buf[i]); |
| 1638 | } |
| 1639 | } |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 1640 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1641 | TEST(STDIO_TEST, fread_EOF) { |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 1642 | std::string digits("0123456789"); |
| 1643 | FILE* fp = fmemopen(&digits[0], digits.size(), "r"); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 1644 | |
| 1645 | // Try to read too much, but little enough that it still fits in the FILE's internal buffer. |
| 1646 | char buf1[4 * 4]; |
| 1647 | memset(buf1, 0, sizeof(buf1)); |
| 1648 | ASSERT_EQ(2U, fread(buf1, 4, 4, fp)); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 1649 | ASSERT_STREQ("0123456789", buf1); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 1650 | ASSERT_TRUE(feof(fp)); |
| 1651 | |
| 1652 | rewind(fp); |
| 1653 | |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 1654 | // Try to read way too much so stdio tries to read more direct from the stream. |
| 1655 | char buf2[4 * 4096]; |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 1656 | memset(buf2, 0, sizeof(buf2)); |
| 1657 | ASSERT_EQ(2U, fread(buf2, 4, 4096, fp)); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 1658 | ASSERT_STREQ("0123456789", buf2); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 1659 | ASSERT_TRUE(feof(fp)); |
| 1660 | |
| 1661 | fclose(fp); |
| 1662 | } |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1663 | |
| 1664 | static void test_fread_from_write_only_stream(size_t n) { |
| 1665 | FILE* fp = fopen("/dev/null", "w"); |
| 1666 | std::vector<char> buf(n, 0); |
| 1667 | errno = 0; |
| 1668 | ASSERT_EQ(0U, fread(&buf[0], n, 1, fp)); |
| 1669 | ASSERT_EQ(EBADF, errno); |
| 1670 | ASSERT_TRUE(ferror(fp)); |
| 1671 | ASSERT_FALSE(feof(fp)); |
| 1672 | fclose(fp); |
| 1673 | } |
| 1674 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1675 | TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1676 | test_fread_from_write_only_stream(1); |
| 1677 | } |
| 1678 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1679 | TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1680 | test_fread_from_write_only_stream(64*1024); |
| 1681 | } |
| 1682 | |
| 1683 | static void test_fwrite_after_fread(size_t n) { |
| 1684 | TemporaryFile tf; |
| 1685 | |
| 1686 | FILE* fp = fdopen(tf.fd, "w+"); |
| 1687 | ASSERT_EQ(1U, fwrite("1", 1, 1, fp)); |
| 1688 | fflush(fp); |
| 1689 | |
| 1690 | // We've flushed but not rewound, so there's nothing to read. |
| 1691 | std::vector<char> buf(n, 0); |
| 1692 | ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp)); |
| 1693 | ASSERT_TRUE(feof(fp)); |
| 1694 | |
| 1695 | // But hitting EOF doesn't prevent us from writing... |
| 1696 | errno = 0; |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1697 | ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno); |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1698 | |
| 1699 | // And if we rewind, everything's there. |
| 1700 | rewind(fp); |
| 1701 | ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp)); |
| 1702 | ASSERT_EQ('1', buf[0]); |
| 1703 | ASSERT_EQ('2', buf[1]); |
| 1704 | |
| 1705 | fclose(fp); |
| 1706 | } |
| 1707 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1708 | TEST(STDIO_TEST, fwrite_after_fread_slow_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1709 | test_fwrite_after_fread(16); |
| 1710 | } |
| 1711 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1712 | TEST(STDIO_TEST, fwrite_after_fread_fast_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1713 | test_fwrite_after_fread(64*1024); |
| 1714 | } |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 1715 | |
| 1716 | // http://b/19172514 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1717 | TEST(STDIO_TEST, fread_after_fseek) { |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 1718 | TemporaryFile tf; |
| 1719 | |
| 1720 | FILE* fp = fopen(tf.filename, "w+"); |
| 1721 | ASSERT_TRUE(fp != nullptr); |
| 1722 | |
| 1723 | char file_data[12288]; |
| 1724 | for (size_t i = 0; i < 12288; i++) { |
| 1725 | file_data[i] = i; |
| 1726 | } |
| 1727 | ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp)); |
| 1728 | fclose(fp); |
| 1729 | |
| 1730 | fp = fopen(tf.filename, "r"); |
| 1731 | ASSERT_TRUE(fp != nullptr); |
| 1732 | |
| 1733 | char buffer[8192]; |
| 1734 | size_t cur_location = 0; |
| 1735 | // Small read to populate internal buffer. |
| 1736 | ASSERT_EQ(100U, fread(buffer, 1, 100, fp)); |
| 1737 | ASSERT_EQ(memcmp(file_data, buffer, 100), 0); |
| 1738 | |
| 1739 | cur_location = static_cast<size_t>(ftell(fp)); |
| 1740 | // Large read to force reading into the user supplied buffer and bypassing |
| 1741 | // the internal buffer. |
| 1742 | ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp)); |
| 1743 | ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0); |
| 1744 | |
| 1745 | // Small backwards seek to verify fseek does not reuse the internal buffer. |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1746 | ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno); |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 1747 | cur_location = static_cast<size_t>(ftell(fp)); |
| 1748 | ASSERT_EQ(22U, fread(buffer, 1, 22, fp)); |
| 1749 | ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0); |
| 1750 | |
| 1751 | fclose(fp); |
| 1752 | } |
Elliott Hughes | 8ab433d | 2015-10-09 17:57:26 -0700 | [diff] [blame] | 1753 | |
| 1754 | // https://code.google.com/p/android/issues/detail?id=184847 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1755 | TEST(STDIO_TEST, fread_EOF_184847) { |
Elliott Hughes | 8ab433d | 2015-10-09 17:57:26 -0700 | [diff] [blame] | 1756 | TemporaryFile tf; |
| 1757 | char buf[6] = {0}; |
| 1758 | |
| 1759 | FILE* fw = fopen(tf.filename, "w"); |
| 1760 | ASSERT_TRUE(fw != nullptr); |
| 1761 | |
| 1762 | FILE* fr = fopen(tf.filename, "r"); |
| 1763 | ASSERT_TRUE(fr != nullptr); |
| 1764 | |
| 1765 | fwrite("a", 1, 1, fw); |
| 1766 | fflush(fw); |
| 1767 | ASSERT_EQ(1U, fread(buf, 1, 1, fr)); |
| 1768 | ASSERT_STREQ("a", buf); |
| 1769 | |
| 1770 | // 'fr' is now at EOF. |
| 1771 | ASSERT_EQ(0U, fread(buf, 1, 1, fr)); |
| 1772 | ASSERT_TRUE(feof(fr)); |
| 1773 | |
| 1774 | // Write some more... |
| 1775 | fwrite("z", 1, 1, fw); |
| 1776 | fflush(fw); |
| 1777 | |
| 1778 | // ...and check that we can read it back. |
| 1779 | // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.) |
| 1780 | ASSERT_EQ(1U, fread(buf, 1, 1, fr)); |
| 1781 | ASSERT_STREQ("z", buf); |
| 1782 | |
| 1783 | // But now we're done. |
| 1784 | ASSERT_EQ(0U, fread(buf, 1, 1, fr)); |
| 1785 | |
| 1786 | fclose(fr); |
| 1787 | fclose(fw); |
| 1788 | } |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 1789 | |
| 1790 | TEST(STDIO_TEST, fclose_invalidates_fd) { |
| 1791 | // The typical error we're trying to help people catch involves accessing |
| 1792 | // memory after it's been freed. But we know that stdin/stdout/stderr are |
| 1793 | // special and don't get deallocated, so this test uses stdin. |
| 1794 | ASSERT_EQ(0, fclose(stdin)); |
| 1795 | |
| 1796 | // Even though using a FILE* after close is undefined behavior, I've closed |
| 1797 | // this bug as "WAI" too many times. We shouldn't hand out stale fds, |
| 1798 | // especially because they might actually correspond to a real stream. |
| 1799 | errno = 0; |
| 1800 | ASSERT_EQ(-1, fileno(stdin)); |
| 1801 | ASSERT_EQ(EBADF, errno); |
| 1802 | } |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 1803 | |
| 1804 | TEST(STDIO_TEST, fseek_ftell_unseekable) { |
| 1805 | #if defined(__BIONIC__) // glibc has fopencookie instead. |
| 1806 | auto read_fn = [](void*, char*, int) { return -1; }; |
| 1807 | FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr); |
| 1808 | ASSERT_TRUE(fp != nullptr); |
| 1809 | |
| 1810 | // Check that ftell balks on an unseekable FILE*. |
| 1811 | errno = 0; |
| 1812 | ASSERT_EQ(-1, ftell(fp)); |
| 1813 | ASSERT_EQ(ESPIPE, errno); |
| 1814 | |
| 1815 | // SEEK_CUR is rewritten as SEEK_SET internally... |
| 1816 | errno = 0; |
| 1817 | ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR)); |
| 1818 | ASSERT_EQ(ESPIPE, errno); |
| 1819 | |
| 1820 | // ...so it's worth testing the direct seek path too. |
| 1821 | errno = 0; |
| 1822 | ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET)); |
| 1823 | ASSERT_EQ(ESPIPE, errno); |
| 1824 | |
| 1825 | fclose(fp); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 1826 | #else |
| 1827 | GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; |
| 1828 | #endif |
| 1829 | } |
| 1830 | |
| 1831 | TEST(STDIO_TEST, funopen_EINVAL) { |
| 1832 | #if defined(__BIONIC__) |
| 1833 | errno = 0; |
| 1834 | ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr)); |
| 1835 | ASSERT_EQ(EINVAL, errno); |
| 1836 | #else |
| 1837 | GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; |
| 1838 | #endif |
| 1839 | } |
| 1840 | |
| 1841 | TEST(STDIO_TEST, funopen_seek) { |
| 1842 | #if defined(__BIONIC__) |
| 1843 | auto read_fn = [](void*, char*, int) { return -1; }; |
| 1844 | |
| 1845 | auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; }; |
| 1846 | auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; }; |
| 1847 | |
| 1848 | FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr); |
| 1849 | ASSERT_TRUE(fp != nullptr); |
| 1850 | fpos_t pos; |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 1851 | #if defined(__LP64__) |
| 1852 | EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno); |
| 1853 | EXPECT_EQ(0xfedcba12LL, pos); |
| 1854 | #else |
| 1855 | EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno); |
| 1856 | EXPECT_EQ(EOVERFLOW, errno); |
| 1857 | #endif |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 1858 | |
| 1859 | FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr); |
| 1860 | ASSERT_TRUE(fp64 != nullptr); |
| 1861 | fpos64_t pos64; |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 1862 | EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno); |
| 1863 | EXPECT_EQ(0xfedcba12345678, pos64); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 1864 | #else |
| 1865 | GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 1866 | #endif |
| 1867 | } |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 1868 | |
| 1869 | TEST(STDIO_TEST, lots_of_concurrent_files) { |
| 1870 | std::vector<TemporaryFile*> tfs; |
| 1871 | std::vector<FILE*> fps; |
| 1872 | |
| 1873 | for (size_t i = 0; i < 256; ++i) { |
| 1874 | TemporaryFile* tf = new TemporaryFile; |
| 1875 | tfs.push_back(tf); |
| 1876 | FILE* fp = fopen(tf->filename, "w+"); |
| 1877 | fps.push_back(fp); |
| 1878 | fprintf(fp, "hello %zu!\n", i); |
| 1879 | fflush(fp); |
| 1880 | } |
| 1881 | |
| 1882 | for (size_t i = 0; i < 256; ++i) { |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 1883 | char expected[BUFSIZ]; |
| 1884 | snprintf(expected, sizeof(expected), "hello %zu!\n", i); |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 1885 | |
Elliott Hughes | 70715da | 2016-08-01 16:35:17 -0700 | [diff] [blame] | 1886 | AssertFileIs(fps[i], expected); |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 1887 | fclose(fps[i]); |
| 1888 | delete tfs[i]; |
| 1889 | } |
| 1890 | } |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1891 | |
| 1892 | static void AssertFileOffsetAt(FILE* fp, off64_t offset) { |
| 1893 | EXPECT_EQ(offset, ftell(fp)); |
| 1894 | EXPECT_EQ(offset, ftello(fp)); |
Elliott Hughes | e4fa6e9 | 2016-02-02 22:39:15 -0800 | [diff] [blame] | 1895 | EXPECT_EQ(offset, ftello64(fp)); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1896 | fpos_t pos; |
| 1897 | fpos64_t pos64; |
| 1898 | EXPECT_EQ(0, fgetpos(fp, &pos)); |
| 1899 | EXPECT_EQ(0, fgetpos64(fp, &pos64)); |
| 1900 | #if defined(__BIONIC__) |
| 1901 | EXPECT_EQ(offset, static_cast<off64_t>(pos)); |
| 1902 | EXPECT_EQ(offset, static_cast<off64_t>(pos64)); |
| 1903 | #else |
| 1904 | GTEST_LOG_(INFO) << "glibc's fpos_t is opaque.\n"; |
| 1905 | #endif |
| 1906 | } |
| 1907 | |
| 1908 | TEST(STDIO_TEST, seek_tell_family_smoke) { |
| 1909 | TemporaryFile tf; |
| 1910 | FILE* fp = fdopen(tf.fd, "w+"); |
| 1911 | |
| 1912 | // Initially we should be at 0. |
| 1913 | AssertFileOffsetAt(fp, 0); |
| 1914 | |
| 1915 | // Seek to offset 8192. |
| 1916 | ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET)); |
| 1917 | AssertFileOffsetAt(fp, 8192); |
| 1918 | fpos_t eight_k_pos; |
| 1919 | ASSERT_EQ(0, fgetpos(fp, &eight_k_pos)); |
| 1920 | |
| 1921 | // Seek forward another 8192... |
| 1922 | ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR)); |
| 1923 | AssertFileOffsetAt(fp, 8192 + 8192); |
| 1924 | fpos64_t sixteen_k_pos64; |
| 1925 | ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64)); |
| 1926 | |
| 1927 | // Seek back 8192... |
| 1928 | ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR)); |
| 1929 | AssertFileOffsetAt(fp, 8192); |
| 1930 | |
| 1931 | // Since we haven't written anything, the end is also at 0. |
| 1932 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1933 | AssertFileOffsetAt(fp, 0); |
| 1934 | |
| 1935 | // Check that our fpos64_t from 16KiB works... |
| 1936 | ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64)); |
| 1937 | AssertFileOffsetAt(fp, 8192 + 8192); |
| 1938 | // ...as does our fpos_t from 8192. |
| 1939 | ASSERT_EQ(0, fsetpos(fp, &eight_k_pos)); |
| 1940 | AssertFileOffsetAt(fp, 8192); |
| 1941 | |
| 1942 | // Do fseeko and fseeko64 work too? |
| 1943 | ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET)); |
| 1944 | AssertFileOffsetAt(fp, 1234); |
| 1945 | ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET)); |
| 1946 | AssertFileOffsetAt(fp, 5678); |
| 1947 | |
| 1948 | fclose(fp); |
| 1949 | } |
| 1950 | |
| 1951 | TEST(STDIO_TEST, fseek_fseeko_EINVAL) { |
| 1952 | TemporaryFile tf; |
| 1953 | FILE* fp = fdopen(tf.fd, "w+"); |
| 1954 | |
| 1955 | // Bad whence. |
| 1956 | errno = 0; |
| 1957 | ASSERT_EQ(-1, fseek(fp, 0, 123)); |
| 1958 | ASSERT_EQ(EINVAL, errno); |
| 1959 | errno = 0; |
| 1960 | ASSERT_EQ(-1, fseeko(fp, 0, 123)); |
| 1961 | ASSERT_EQ(EINVAL, errno); |
| 1962 | errno = 0; |
| 1963 | ASSERT_EQ(-1, fseeko64(fp, 0, 123)); |
| 1964 | ASSERT_EQ(EINVAL, errno); |
| 1965 | |
| 1966 | // Bad offset. |
| 1967 | errno = 0; |
| 1968 | ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET)); |
| 1969 | ASSERT_EQ(EINVAL, errno); |
| 1970 | errno = 0; |
| 1971 | ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET)); |
| 1972 | ASSERT_EQ(EINVAL, errno); |
| 1973 | errno = 0; |
| 1974 | ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET)); |
| 1975 | ASSERT_EQ(EINVAL, errno); |
| 1976 | |
| 1977 | fclose(fp); |
| 1978 | } |
Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 1979 | |
| 1980 | TEST(STDIO_TEST, ctermid) { |
| 1981 | ASSERT_STREQ("/dev/tty", ctermid(nullptr)); |
| 1982 | |
| 1983 | char buf[L_ctermid] = {}; |
| 1984 | ASSERT_EQ(buf, ctermid(buf)); |
| 1985 | ASSERT_STREQ("/dev/tty", buf); |
| 1986 | } |
Elliott Hughes | d1f25a7 | 2016-08-05 15:53:03 -0700 | [diff] [blame] | 1987 | |
| 1988 | TEST(STDIO_TEST, remove) { |
| 1989 | struct stat sb; |
| 1990 | |
| 1991 | TemporaryFile tf; |
| 1992 | ASSERT_EQ(0, remove(tf.filename)); |
| 1993 | ASSERT_EQ(-1, lstat(tf.filename, &sb)); |
| 1994 | ASSERT_EQ(ENOENT, errno); |
| 1995 | |
| 1996 | TemporaryDir td; |
| 1997 | ASSERT_EQ(0, remove(td.dirname)); |
| 1998 | ASSERT_EQ(-1, lstat(td.dirname, &sb)); |
| 1999 | ASSERT_EQ(ENOENT, errno); |
| 2000 | |
| 2001 | errno = 0; |
| 2002 | ASSERT_EQ(-1, remove(tf.filename)); |
| 2003 | ASSERT_EQ(ENOENT, errno); |
| 2004 | |
| 2005 | errno = 0; |
| 2006 | ASSERT_EQ(-1, remove(td.dirname)); |
| 2007 | ASSERT_EQ(ENOENT, errno); |
| 2008 | } |
Elliott Hughes | fb3873d | 2016-08-10 11:07:54 -0700 | [diff] [blame] | 2009 | |
| 2010 | TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) { |
| 2011 | char buf[16]; |
| 2012 | ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"), |
| 2013 | testing::KilledBySignal(SIGABRT), |
| 2014 | #if defined(NOFORTIFY) |
| 2015 | "FORTIFY: vsnprintf: size .* > SSIZE_MAX" |
| 2016 | #else |
| 2017 | "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer" |
| 2018 | #endif |
| 2019 | ); |
| 2020 | } |
| 2021 | |
| 2022 | TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) { |
| 2023 | std::string buf = "world"; |
| 2024 | ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"), |
| 2025 | testing::KilledBySignal(SIGABRT), |
| 2026 | "FORTIFY: vsnprintf: size .* > SSIZE_MAX"); |
| 2027 | } |
| 2028 | |
| 2029 | TEST(STDIO_TEST, sprintf_30445072) { |
| 2030 | std::string buf = "world"; |
| 2031 | sprintf(&buf[0], "hello"); |
| 2032 | ASSERT_EQ(buf, "hello"); |
| 2033 | } |
Elliott Hughes | 33a8cb1 | 2017-07-25 18:06:46 -0700 | [diff] [blame] | 2034 | |
| 2035 | TEST(STDIO_TEST, fopen_append_mode_and_ftell) { |
| 2036 | TemporaryFile tf; |
| 2037 | SetFileTo(tf.filename, "0123456789"); |
| 2038 | FILE* fp = fopen(tf.filename, "a"); |
| 2039 | EXPECT_EQ(10, ftell(fp)); |
| 2040 | ASSERT_EQ(0, fseek(fp, 2, SEEK_SET)); |
| 2041 | EXPECT_EQ(2, ftell(fp)); |
| 2042 | ASSERT_NE(EOF, fputs("xxx", fp)); |
| 2043 | ASSERT_EQ(0, fflush(fp)); |
| 2044 | EXPECT_EQ(13, ftell(fp)); |
| 2045 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 2046 | EXPECT_EQ(13, ftell(fp)); |
| 2047 | ASSERT_EQ(0, fclose(fp)); |
| 2048 | AssertFileIs(tf.filename, "0123456789xxx"); |
| 2049 | } |
| 2050 | |
| 2051 | TEST(STDIO_TEST, fdopen_append_mode_and_ftell) { |
| 2052 | TemporaryFile tf; |
| 2053 | SetFileTo(tf.filename, "0123456789"); |
| 2054 | int fd = open(tf.filename, O_RDWR); |
| 2055 | ASSERT_NE(-1, fd); |
| 2056 | // POSIX: "The file position indicator associated with the new stream is set to the position |
| 2057 | // indicated by the file offset associated with the file descriptor." |
| 2058 | ASSERT_EQ(4, lseek(fd, 4, SEEK_SET)); |
| 2059 | FILE* fp = fdopen(fd, "a"); |
| 2060 | EXPECT_EQ(4, ftell(fp)); |
| 2061 | ASSERT_EQ(0, fseek(fp, 2, SEEK_SET)); |
| 2062 | EXPECT_EQ(2, ftell(fp)); |
| 2063 | ASSERT_NE(EOF, fputs("xxx", fp)); |
| 2064 | ASSERT_EQ(0, fflush(fp)); |
| 2065 | EXPECT_EQ(13, ftell(fp)); |
| 2066 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 2067 | EXPECT_EQ(13, ftell(fp)); |
| 2068 | ASSERT_EQ(0, fclose(fp)); |
| 2069 | AssertFileIs(tf.filename, "0123456789xxx"); |
| 2070 | } |
| 2071 | |
| 2072 | TEST(STDIO_TEST, freopen_append_mode_and_ftell) { |
| 2073 | TemporaryFile tf; |
| 2074 | SetFileTo(tf.filename, "0123456789"); |
| 2075 | FILE* other_fp = fopen("/proc/version", "r"); |
| 2076 | FILE* fp = freopen(tf.filename, "a", other_fp); |
| 2077 | EXPECT_EQ(10, ftell(fp)); |
| 2078 | ASSERT_EQ(0, fseek(fp, 2, SEEK_SET)); |
| 2079 | EXPECT_EQ(2, ftell(fp)); |
| 2080 | ASSERT_NE(EOF, fputs("xxx", fp)); |
| 2081 | ASSERT_EQ(0, fflush(fp)); |
| 2082 | EXPECT_EQ(13, ftell(fp)); |
| 2083 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 2084 | EXPECT_EQ(13, ftell(fp)); |
| 2085 | ASSERT_EQ(0, fclose(fp)); |
| 2086 | AssertFileIs(tf.filename, "0123456789xxx"); |
| 2087 | } |
Elliott Hughes | b15feb7 | 2017-07-31 17:20:18 -0700 | [diff] [blame] | 2088 | |
| 2089 | TEST(STDIO_TEST, constants) { |
| 2090 | ASSERT_LE(FILENAME_MAX, PATH_MAX); |
| 2091 | ASSERT_EQ(L_tmpnam, PATH_MAX); |
| 2092 | } |
Elliott Hughes | 37ad959 | 2017-10-30 17:47:12 -0700 | [diff] [blame] | 2093 | |
| 2094 | TEST(STDIO_TEST, perror) { |
| 2095 | ExecTestHelper eth; |
| 2096 | eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n"); |
| 2097 | eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n"); |
| 2098 | eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n"); |
| 2099 | } |
| 2100 | |
| 2101 | TEST(STDIO_TEST, puts) { |
| 2102 | ExecTestHelper eth; |
| 2103 | eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n"); |
| 2104 | } |
| 2105 | |
| 2106 | TEST(STDIO_TEST, unlocked) { |
| 2107 | TemporaryFile tf; |
| 2108 | |
| 2109 | FILE* fp = fopen(tf.filename, "w+"); |
| 2110 | ASSERT_TRUE(fp != nullptr); |
| 2111 | |
| 2112 | clearerr_unlocked(fp); |
| 2113 | ASSERT_FALSE(feof_unlocked(fp)); |
| 2114 | ASSERT_FALSE(ferror_unlocked(fp)); |
| 2115 | |
| 2116 | ASSERT_EQ(fileno(fp), fileno_unlocked(fp)); |
| 2117 | |
| 2118 | ASSERT_NE(EOF, putc_unlocked('a', fp)); |
| 2119 | ASSERT_NE(EOF, putc('b', fp)); |
| 2120 | ASSERT_NE(EOF, fputc_unlocked('c', fp)); |
| 2121 | ASSERT_NE(EOF, fputc('d', fp)); |
| 2122 | |
| 2123 | rewind(fp); |
| 2124 | ASSERT_EQ('a', getc_unlocked(fp)); |
| 2125 | ASSERT_EQ('b', getc(fp)); |
| 2126 | ASSERT_EQ('c', fgetc_unlocked(fp)); |
| 2127 | ASSERT_EQ('d', fgetc(fp)); |
| 2128 | |
| 2129 | rewind(fp); |
| 2130 | ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp)); |
| 2131 | ASSERT_EQ(2U, fwrite("CD", 1, 2, fp)); |
| 2132 | ASSERT_EQ(0, fflush_unlocked(fp)); |
| 2133 | |
| 2134 | rewind(fp); |
| 2135 | char buf[BUFSIZ] = {}; |
| 2136 | ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp)); |
| 2137 | ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp)); |
| 2138 | ASSERT_STREQ("ABCD", buf); |
| 2139 | |
| 2140 | rewind(fp); |
| 2141 | ASSERT_NE(EOF, fputs("hello ", fp)); |
| 2142 | ASSERT_NE(EOF, fputs_unlocked("world", fp)); |
| 2143 | ASSERT_NE(EOF, fputc('\n', fp)); |
| 2144 | |
| 2145 | rewind(fp); |
| 2146 | ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr); |
| 2147 | ASSERT_STREQ("hello world\n", buf); |
| 2148 | |
| 2149 | ASSERT_EQ(0, fclose(fp)); |
| 2150 | } |