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