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 | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 30 | #include <vector> |
| 31 | |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 32 | #include "TemporaryFile.h" |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 33 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 34 | #if defined(NOFORTIFY) |
| 35 | #define STDIO_TEST stdio_nofortify |
| 36 | #else |
| 37 | #define STDIO_TEST stdio |
| 38 | #endif |
| 39 | |
| 40 | TEST(STDIO_TEST, flockfile_18208568_stderr) { |
Elliott Hughes | 6a03abc | 2014-11-03 12:32:17 -0800 | [diff] [blame] | 41 | // Check that we have a _recursive_ mutex for flockfile. |
| 42 | flockfile(stderr); |
| 43 | feof(stderr); // We don't care about the result, but this needs to take the lock. |
| 44 | funlockfile(stderr); |
| 45 | } |
| 46 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 47 | TEST(STDIO_TEST, flockfile_18208568_regular) { |
Elliott Hughes | 6a03abc | 2014-11-03 12:32:17 -0800 | [diff] [blame] | 48 | // We never had a bug for streams other than stdin/stdout/stderr, but test anyway. |
| 49 | FILE* fp = fopen("/dev/null", "w"); |
| 50 | ASSERT_TRUE(fp != NULL); |
| 51 | flockfile(fp); |
| 52 | feof(fp); |
| 53 | funlockfile(fp); |
| 54 | fclose(fp); |
| 55 | } |
| 56 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 57 | TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) { |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 58 | FILE* fp = tmpfile(); |
| 59 | ASSERT_TRUE(fp != NULL); |
| 60 | |
| 61 | int fd = fileno(fp); |
| 62 | ASSERT_NE(fd, -1); |
| 63 | |
| 64 | struct stat sb; |
| 65 | int rc = fstat(fd, &sb); |
| 66 | ASSERT_NE(rc, -1); |
| 67 | ASSERT_EQ(sb.st_mode & 0777, 0600U); |
| 68 | |
| 69 | rc = fprintf(fp, "hello\n"); |
| 70 | ASSERT_EQ(rc, 6); |
| 71 | |
| 72 | rewind(fp); |
| 73 | |
| 74 | char buf[16]; |
| 75 | char* s = fgets(buf, sizeof(buf), fp); |
| 76 | ASSERT_TRUE(s != NULL); |
| 77 | ASSERT_STREQ("hello\n", s); |
| 78 | |
| 79 | fclose(fp); |
| 80 | } |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 81 | |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 82 | TEST(STDIO_TEST, tmpfile64) { |
| 83 | FILE* fp = tmpfile64(); |
| 84 | ASSERT_TRUE(fp != nullptr); |
| 85 | fclose(fp); |
| 86 | } |
| 87 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 88 | TEST(STDIO_TEST, dprintf) { |
Calin Juravle | 6afb2a9 | 2014-05-22 11:47:47 +0100 | [diff] [blame] | 89 | TemporaryFile tf; |
| 90 | |
| 91 | int rc = dprintf(tf.fd, "hello\n"); |
| 92 | ASSERT_EQ(rc, 6); |
| 93 | |
Yabin Cui | 5ca4a9e | 2014-11-06 19:55:09 -0800 | [diff] [blame] | 94 | lseek(tf.fd, 0, SEEK_SET); |
Christopher Ferris | 9e01ea6 | 2014-05-29 12:49:35 -0700 | [diff] [blame] | 95 | FILE* tfile = fdopen(tf.fd, "r"); |
| 96 | ASSERT_TRUE(tfile != NULL); |
Calin Juravle | 6afb2a9 | 2014-05-22 11:47:47 +0100 | [diff] [blame] | 97 | |
Christopher Ferris | 9e01ea6 | 2014-05-29 12:49:35 -0700 | [diff] [blame] | 98 | char buf[7]; |
| 99 | ASSERT_EQ(buf, fgets(buf, sizeof(buf), tfile)); |
Calin Juravle | 6afb2a9 | 2014-05-22 11:47:47 +0100 | [diff] [blame] | 100 | ASSERT_STREQ("hello\n", buf); |
Christopher Ferris | 9e01ea6 | 2014-05-29 12:49:35 -0700 | [diff] [blame] | 101 | // Make sure there isn't anything else in the file. |
| 102 | ASSERT_EQ(NULL, fgets(buf, sizeof(buf), tfile)); |
| 103 | fclose(tfile); |
Calin Juravle | 6afb2a9 | 2014-05-22 11:47:47 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 106 | TEST(STDIO_TEST, getdelim) { |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 107 | FILE* fp = tmpfile(); |
| 108 | ASSERT_TRUE(fp != NULL); |
| 109 | |
| 110 | const char* line_written = "This is a test"; |
| 111 | int rc = fprintf(fp, "%s", line_written); |
| 112 | ASSERT_EQ(rc, static_cast<int>(strlen(line_written))); |
| 113 | |
| 114 | rewind(fp); |
| 115 | |
| 116 | char* word_read = NULL; |
| 117 | size_t allocated_length = 0; |
| 118 | |
| 119 | const char* expected[] = { "This ", " ", "is ", "a ", "test" }; |
| 120 | for (size_t i = 0; i < 5; ++i) { |
| 121 | ASSERT_FALSE(feof(fp)); |
| 122 | ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i]))); |
| 123 | ASSERT_GE(allocated_length, strlen(expected[i])); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 124 | ASSERT_STREQ(expected[i], word_read); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 125 | } |
| 126 | // The last read should have set the end-of-file indicator for the stream. |
| 127 | ASSERT_TRUE(feof(fp)); |
| 128 | clearerr(fp); |
| 129 | |
| 130 | // getdelim returns -1 but doesn't set errno if we're already at EOF. |
| 131 | // It should set the end-of-file indicator for the stream, though. |
| 132 | errno = 0; |
| 133 | ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 134 | ASSERT_EQ(0, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 135 | ASSERT_TRUE(feof(fp)); |
| 136 | |
| 137 | free(word_read); |
| 138 | fclose(fp); |
| 139 | } |
| 140 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 141 | TEST(STDIO_TEST, getdelim_invalid) { |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 142 | FILE* fp = tmpfile(); |
Elliott Hughes | 6ad8f76 | 2013-12-19 14:56:17 -0800 | [diff] [blame] | 143 | ASSERT_TRUE(fp != NULL); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 144 | |
| 145 | char* buffer = NULL; |
| 146 | size_t buffer_length = 0; |
| 147 | |
| 148 | // The first argument can't be NULL. |
| 149 | errno = 0; |
| 150 | ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 151 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 152 | |
| 153 | // The second argument can't be NULL. |
| 154 | errno = 0; |
| 155 | ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 156 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 157 | |
Elliott Hughes | 20f8aec | 2014-05-12 15:15:37 -0700 | [diff] [blame] | 158 | // The underlying fd can't be closed. |
| 159 | ASSERT_EQ(0, close(fileno(fp))); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 160 | errno = 0; |
| 161 | ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 162 | ASSERT_EQ(EBADF, errno); |
Elliott Hughes | 20f8aec | 2014-05-12 15:15:37 -0700 | [diff] [blame] | 163 | fclose(fp); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 164 | } |
| 165 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 166 | TEST(STDIO_TEST, getdelim_directory) { |
Elliott Hughes | 694fd2d | 2015-04-05 10:51:56 -0700 | [diff] [blame] | 167 | FILE* fp = fopen("/proc", "r"); |
| 168 | ASSERT_TRUE(fp != NULL); |
| 169 | char* word_read; |
| 170 | size_t allocated_length; |
| 171 | ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp)); |
| 172 | fclose(fp); |
| 173 | } |
| 174 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 175 | TEST(STDIO_TEST, getline) { |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 176 | FILE* fp = tmpfile(); |
| 177 | ASSERT_TRUE(fp != NULL); |
| 178 | |
| 179 | const char* line_written = "This is a test for getline\n"; |
| 180 | const size_t line_count = 5; |
| 181 | |
| 182 | for (size_t i = 0; i < line_count; ++i) { |
| 183 | int rc = fprintf(fp, "%s", line_written); |
| 184 | ASSERT_EQ(rc, static_cast<int>(strlen(line_written))); |
| 185 | } |
| 186 | |
| 187 | rewind(fp); |
| 188 | |
| 189 | char* line_read = NULL; |
| 190 | size_t allocated_length = 0; |
| 191 | |
| 192 | size_t read_line_count = 0; |
| 193 | ssize_t read_char_count; |
| 194 | while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) { |
| 195 | ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written))); |
| 196 | ASSERT_GE(allocated_length, strlen(line_written)); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 197 | ASSERT_STREQ(line_written, line_read); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 198 | ++read_line_count; |
| 199 | } |
| 200 | ASSERT_EQ(read_line_count, line_count); |
| 201 | |
| 202 | // The last read should have set the end-of-file indicator for the stream. |
| 203 | ASSERT_TRUE(feof(fp)); |
| 204 | clearerr(fp); |
| 205 | |
| 206 | // getline returns -1 but doesn't set errno if we're already at EOF. |
| 207 | // It should set the end-of-file indicator for the stream, though. |
| 208 | errno = 0; |
| 209 | ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 210 | ASSERT_EQ(0, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 211 | ASSERT_TRUE(feof(fp)); |
| 212 | |
| 213 | free(line_read); |
| 214 | fclose(fp); |
| 215 | } |
| 216 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 217 | TEST(STDIO_TEST, getline_invalid) { |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 218 | FILE* fp = tmpfile(); |
Elliott Hughes | 6ad8f76 | 2013-12-19 14:56:17 -0800 | [diff] [blame] | 219 | ASSERT_TRUE(fp != NULL); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 220 | |
| 221 | char* buffer = NULL; |
| 222 | size_t buffer_length = 0; |
| 223 | |
| 224 | // The first argument can't be NULL. |
| 225 | errno = 0; |
| 226 | ASSERT_EQ(getline(NULL, &buffer_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 227 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 228 | |
| 229 | // The second argument can't be NULL. |
| 230 | errno = 0; |
| 231 | ASSERT_EQ(getline(&buffer, NULL, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 232 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 233 | |
Elliott Hughes | 20f8aec | 2014-05-12 15:15:37 -0700 | [diff] [blame] | 234 | // The underlying fd can't be closed. |
| 235 | ASSERT_EQ(0, close(fileno(fp))); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 236 | errno = 0; |
| 237 | ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 238 | ASSERT_EQ(EBADF, errno); |
Elliott Hughes | 20f8aec | 2014-05-12 15:15:37 -0700 | [diff] [blame] | 239 | fclose(fp); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 240 | } |
Thorsten Glaser | c641caf | 2013-02-17 16:50:58 +0000 | [diff] [blame] | 241 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 242 | TEST(STDIO_TEST, printf_ssize_t) { |
Elliott Hughes | e255642 | 2013-02-28 10:51:14 -0800 | [diff] [blame] | 243 | // http://b/8253769 |
Elliott Hughes | e255642 | 2013-02-28 10:51:14 -0800 | [diff] [blame] | 244 | ASSERT_EQ(sizeof(ssize_t), sizeof(long int)); |
Elliott Hughes | b6e2248 | 2013-03-08 15:28:52 -0800 | [diff] [blame] | 245 | ASSERT_EQ(sizeof(ssize_t), sizeof(size_t)); |
| 246 | // 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] | 247 | // error: format '%zd' expects argument of type 'signed size_t', |
| 248 | // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format] |
| 249 | ssize_t v = 1; |
| 250 | char buf[32]; |
| 251 | snprintf(buf, sizeof(buf), "%zd", v); |
| 252 | } |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 253 | |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 254 | // https://code.google.com/p/android/issues/detail?id=64886 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 255 | TEST(STDIO_TEST, snprintf_a) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 256 | char buf[BUFSIZ]; |
| 257 | EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235)); |
| 258 | EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf); |
| 259 | } |
| 260 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 261 | TEST(STDIO_TEST, snprintf_lc) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 262 | char buf[BUFSIZ]; |
| 263 | wint_t wc = L'a'; |
| 264 | EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc)); |
| 265 | EXPECT_STREQ("<a>", buf); |
| 266 | } |
| 267 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 268 | TEST(STDIO_TEST, snprintf_ls) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 269 | char buf[BUFSIZ]; |
| 270 | wchar_t* ws = NULL; |
| 271 | EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws)); |
| 272 | EXPECT_STREQ("<(null)>", buf); |
| 273 | |
| 274 | wchar_t chars[] = { L'h', L'i', 0 }; |
| 275 | ws = chars; |
| 276 | EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws)); |
| 277 | EXPECT_STREQ("<hi>", buf); |
| 278 | } |
| 279 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 280 | TEST(STDIO_TEST, snprintf_n) { |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 281 | #if defined(__BIONIC__) |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 282 | // http://b/14492135 |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 283 | char buf[32]; |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 284 | int i = 1234; |
| 285 | EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i)); |
| 286 | EXPECT_EQ(1234, i); |
| 287 | EXPECT_STREQ("a n b", buf); |
| 288 | #else |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 289 | GTEST_LOG_(INFO) << "This test does nothing on glibc.\n"; |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 290 | #endif |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 291 | } |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 292 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 293 | TEST(STDIO_TEST, snprintf_smoke) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 294 | char buf[BUFSIZ]; |
| 295 | |
| 296 | snprintf(buf, sizeof(buf), "a"); |
| 297 | EXPECT_STREQ("a", buf); |
| 298 | |
| 299 | snprintf(buf, sizeof(buf), "%%"); |
| 300 | EXPECT_STREQ("%", buf); |
| 301 | |
| 302 | snprintf(buf, sizeof(buf), "01234"); |
| 303 | EXPECT_STREQ("01234", buf); |
| 304 | |
| 305 | snprintf(buf, sizeof(buf), "a%sb", "01234"); |
| 306 | EXPECT_STREQ("a01234b", buf); |
| 307 | |
| 308 | char* s = NULL; |
| 309 | snprintf(buf, sizeof(buf), "a%sb", s); |
| 310 | EXPECT_STREQ("a(null)b", buf); |
| 311 | |
| 312 | snprintf(buf, sizeof(buf), "aa%scc", "bb"); |
| 313 | EXPECT_STREQ("aabbcc", buf); |
| 314 | |
| 315 | snprintf(buf, sizeof(buf), "a%cc", 'b'); |
| 316 | EXPECT_STREQ("abc", buf); |
| 317 | |
| 318 | snprintf(buf, sizeof(buf), "a%db", 1234); |
| 319 | EXPECT_STREQ("a1234b", buf); |
| 320 | |
| 321 | snprintf(buf, sizeof(buf), "a%db", -8123); |
| 322 | EXPECT_STREQ("a-8123b", buf); |
| 323 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 324 | snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010)); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 325 | EXPECT_STREQ("a16b", buf); |
| 326 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 327 | snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10)); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 328 | EXPECT_STREQ("a16b", buf); |
| 329 | |
| 330 | snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL); |
| 331 | EXPECT_STREQ("a68719476736b", buf); |
| 332 | |
| 333 | snprintf(buf, sizeof(buf), "a%ldb", 70000L); |
| 334 | EXPECT_STREQ("a70000b", buf); |
| 335 | |
| 336 | snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234)); |
| 337 | EXPECT_STREQ("a0xb0001234b", buf); |
| 338 | |
| 339 | snprintf(buf, sizeof(buf), "a%xz", 0x12ab); |
| 340 | EXPECT_STREQ("a12abz", buf); |
| 341 | |
| 342 | snprintf(buf, sizeof(buf), "a%Xz", 0x12ab); |
| 343 | EXPECT_STREQ("a12ABz", buf); |
| 344 | |
| 345 | snprintf(buf, sizeof(buf), "a%08xz", 0x123456); |
| 346 | EXPECT_STREQ("a00123456z", buf); |
| 347 | |
| 348 | snprintf(buf, sizeof(buf), "a%5dz", 1234); |
| 349 | EXPECT_STREQ("a 1234z", buf); |
| 350 | |
| 351 | snprintf(buf, sizeof(buf), "a%05dz", 1234); |
| 352 | EXPECT_STREQ("a01234z", buf); |
| 353 | |
| 354 | snprintf(buf, sizeof(buf), "a%8dz", 1234); |
| 355 | EXPECT_STREQ("a 1234z", buf); |
| 356 | |
| 357 | snprintf(buf, sizeof(buf), "a%-8dz", 1234); |
| 358 | EXPECT_STREQ("a1234 z", buf); |
| 359 | |
| 360 | snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef"); |
| 361 | EXPECT_STREQ("Aabcdef Z", buf); |
| 362 | |
| 363 | snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234); |
| 364 | EXPECT_STREQ("Ahello:1234Z", buf); |
| 365 | |
| 366 | snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5); |
| 367 | EXPECT_STREQ("a005:5:05z", buf); |
| 368 | |
| 369 | void* p = NULL; |
| 370 | snprintf(buf, sizeof(buf), "a%d,%pz", 5, p); |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 371 | #if defined(__BIONIC__) |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 372 | EXPECT_STREQ("a5,0x0z", buf); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 373 | #else // __BIONIC__ |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 374 | EXPECT_STREQ("a5,(nil)z", buf); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 375 | #endif // __BIONIC__ |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 376 | |
| 377 | snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8); |
| 378 | EXPECT_STREQ("a68719476736,6,7,8z", buf); |
| 379 | |
| 380 | snprintf(buf, sizeof(buf), "a_%f_b", 1.23f); |
| 381 | EXPECT_STREQ("a_1.230000_b", buf); |
| 382 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 383 | snprintf(buf, sizeof(buf), "a_%g_b", 3.14); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 384 | EXPECT_STREQ("a_3.14_b", buf); |
Alexander Ivchenko | edd7c2e | 2014-04-01 17:01:39 +0400 | [diff] [blame] | 385 | |
| 386 | snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice"); |
| 387 | EXPECT_STREQ("print_me_twice print_me_twice", buf); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 390 | template <typename T> |
| 391 | void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...), |
| 392 | const T* fmt, const T* fmt_plus, |
| 393 | const T* minus_inf, const T* inf_, const T* plus_inf, |
| 394 | const T* minus_nan, const T* nan_, const T* plus_nan) { |
| 395 | T buf[BUFSIZ]; |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 396 | |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 397 | snprintf_fn(buf, sizeof(buf), fmt, nan("")); |
| 398 | EXPECT_STREQ(nan_, buf) << fmt; |
| 399 | snprintf_fn(buf, sizeof(buf), fmt, -nan("")); |
| 400 | EXPECT_STREQ(minus_nan, buf) << fmt; |
| 401 | snprintf_fn(buf, sizeof(buf), fmt_plus, nan("")); |
| 402 | EXPECT_STREQ(plus_nan, buf) << fmt_plus; |
| 403 | snprintf_fn(buf, sizeof(buf), fmt_plus, -nan("")); |
| 404 | EXPECT_STREQ(minus_nan, buf) << fmt_plus; |
| 405 | |
| 406 | snprintf_fn(buf, sizeof(buf), fmt, HUGE_VAL); |
| 407 | EXPECT_STREQ(inf_, buf) << fmt; |
| 408 | snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VAL); |
| 409 | EXPECT_STREQ(minus_inf, buf) << fmt; |
| 410 | snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VAL); |
| 411 | EXPECT_STREQ(plus_inf, buf) << fmt_plus; |
| 412 | snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VAL); |
| 413 | EXPECT_STREQ(minus_inf, buf) << fmt_plus; |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 416 | TEST(STDIO_TEST, snprintf_inf_nan) { |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 417 | CheckInfNan(snprintf, "%a", "%+a", "-inf", "inf", "+inf", "-nan", "nan", "+nan"); |
| 418 | CheckInfNan(snprintf, "%A", "%+A", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN"); |
| 419 | CheckInfNan(snprintf, "%e", "%+e", "-inf", "inf", "+inf", "-nan", "nan", "+nan"); |
| 420 | CheckInfNan(snprintf, "%E", "%+E", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN"); |
| 421 | CheckInfNan(snprintf, "%f", "%+f", "-inf", "inf", "+inf", "-nan", "nan", "+nan"); |
| 422 | CheckInfNan(snprintf, "%F", "%+F", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN"); |
| 423 | CheckInfNan(snprintf, "%g", "%+g", "-inf", "inf", "+inf", "-nan", "nan", "+nan"); |
| 424 | CheckInfNan(snprintf, "%G", "%+G", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN"); |
| 425 | } |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 426 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 427 | TEST(STDIO_TEST, wsprintf_inf_nan) { |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 428 | CheckInfNan(swprintf, L"%a", L"%+a", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan"); |
| 429 | CheckInfNan(swprintf, L"%A", L"%+A", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN"); |
| 430 | CheckInfNan(swprintf, L"%e", L"%+e", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan"); |
| 431 | CheckInfNan(swprintf, L"%E", L"%+E", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN"); |
| 432 | CheckInfNan(swprintf, L"%f", L"%+f", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan"); |
| 433 | CheckInfNan(swprintf, L"%F", L"%+F", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN"); |
| 434 | CheckInfNan(swprintf, L"%g", L"%+g", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan"); |
| 435 | CheckInfNan(swprintf, L"%G", L"%+G", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN"); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 438 | TEST(STDIO_TEST, snprintf_d_INT_MAX) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 439 | char buf[BUFSIZ]; |
| 440 | snprintf(buf, sizeof(buf), "%d", INT_MAX); |
| 441 | EXPECT_STREQ("2147483647", buf); |
| 442 | } |
| 443 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 444 | TEST(STDIO_TEST, snprintf_d_INT_MIN) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 445 | char buf[BUFSIZ]; |
| 446 | snprintf(buf, sizeof(buf), "%d", INT_MIN); |
| 447 | EXPECT_STREQ("-2147483648", buf); |
| 448 | } |
| 449 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 450 | TEST(STDIO_TEST, snprintf_ld_LONG_MAX) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 451 | char buf[BUFSIZ]; |
| 452 | snprintf(buf, sizeof(buf), "%ld", LONG_MAX); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 453 | #if __LP64__ |
| 454 | EXPECT_STREQ("9223372036854775807", buf); |
| 455 | #else |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 456 | EXPECT_STREQ("2147483647", buf); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 457 | #endif |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 460 | TEST(STDIO_TEST, snprintf_ld_LONG_MIN) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 461 | char buf[BUFSIZ]; |
| 462 | snprintf(buf, sizeof(buf), "%ld", LONG_MIN); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 463 | #if __LP64__ |
| 464 | EXPECT_STREQ("-9223372036854775808", buf); |
| 465 | #else |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 466 | EXPECT_STREQ("-2147483648", buf); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 467 | #endif |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 470 | TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 471 | char buf[BUFSIZ]; |
| 472 | snprintf(buf, sizeof(buf), "%lld", LLONG_MAX); |
| 473 | EXPECT_STREQ("9223372036854775807", buf); |
| 474 | } |
| 475 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 476 | TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) { |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 477 | char buf[BUFSIZ]; |
| 478 | snprintf(buf, sizeof(buf), "%lld", LLONG_MIN); |
| 479 | EXPECT_STREQ("-9223372036854775808", buf); |
| 480 | } |
| 481 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 482 | TEST(STDIO_TEST, snprintf_e) { |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 483 | char buf[BUFSIZ]; |
| 484 | |
| 485 | snprintf(buf, sizeof(buf), "%e", 1.5); |
| 486 | EXPECT_STREQ("1.500000e+00", buf); |
| 487 | |
| 488 | snprintf(buf, sizeof(buf), "%Le", 1.5l); |
| 489 | EXPECT_STREQ("1.500000e+00", buf); |
| 490 | } |
| 491 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 492 | TEST(STDIO_TEST, snprintf_negative_zero_5084292) { |
Elliott Hughes | e77f38f | 2014-05-14 12:39:12 -0700 | [diff] [blame] | 493 | char buf[BUFSIZ]; |
| 494 | |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 495 | snprintf(buf, sizeof(buf), "%e", -0.0); |
| 496 | EXPECT_STREQ("-0.000000e+00", buf); |
| 497 | snprintf(buf, sizeof(buf), "%E", -0.0); |
| 498 | EXPECT_STREQ("-0.000000E+00", buf); |
Elliott Hughes | e77f38f | 2014-05-14 12:39:12 -0700 | [diff] [blame] | 499 | snprintf(buf, sizeof(buf), "%f", -0.0); |
| 500 | EXPECT_STREQ("-0.000000", buf); |
Elliott Hughes | 1b18aff | 2014-12-16 14:45:32 -0800 | [diff] [blame] | 501 | snprintf(buf, sizeof(buf), "%F", -0.0); |
| 502 | EXPECT_STREQ("-0.000000", buf); |
| 503 | snprintf(buf, sizeof(buf), "%g", -0.0); |
| 504 | EXPECT_STREQ("-0", buf); |
| 505 | snprintf(buf, sizeof(buf), "%G", -0.0); |
| 506 | EXPECT_STREQ("-0", buf); |
| 507 | snprintf(buf, sizeof(buf), "%a", -0.0); |
| 508 | EXPECT_STREQ("-0x0p+0", buf); |
| 509 | snprintf(buf, sizeof(buf), "%A", -0.0); |
| 510 | EXPECT_STREQ("-0X0P+0", buf); |
Elliott Hughes | e77f38f | 2014-05-14 12:39:12 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 513 | TEST(STDIO_TEST, snprintf_utf8_15439554) { |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 514 | locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0); |
Wally Yau | a40fdbd | 2014-08-26 09:47:23 -0700 | [diff] [blame] | 515 | locale_t old_locale = uselocale(cloc); |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 516 | |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 517 | // http://b/15439554 |
| 518 | char buf[BUFSIZ]; |
| 519 | |
| 520 | // 1-byte character. |
| 521 | snprintf(buf, sizeof(buf), "%dx%d", 1, 2); |
| 522 | EXPECT_STREQ("1x2", buf); |
| 523 | // 2-byte character. |
| 524 | snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2); |
| 525 | EXPECT_STREQ("1¢2", buf); |
| 526 | // 3-byte character. |
| 527 | snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2); |
| 528 | EXPECT_STREQ("1€2", buf); |
| 529 | // 4-byte character. |
| 530 | snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2); |
| 531 | EXPECT_STREQ("1ð¤¢2", buf); |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 532 | |
Wally Yau | a40fdbd | 2014-08-26 09:47:23 -0700 | [diff] [blame] | 533 | uselocale(old_locale); |
Dan Albert | 1aec7c1 | 2014-07-30 10:53:48 -0700 | [diff] [blame] | 534 | freelocale(cloc); |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Elliott Hughes | 43f7c87 | 2016-02-05 11:18:41 -0800 | [diff] [blame^] | 537 | static void* snprintf_small_stack_fn(void*) { |
| 538 | // Make life (realistically) hard for ourselves by allocating our own buffer for the result. |
| 539 | char buf[PATH_MAX]; |
| 540 | snprintf(buf, sizeof(buf), "/proc/%d", getpid()); |
| 541 | return nullptr; |
| 542 | } |
| 543 | |
| 544 | TEST(STDIO_TEST, snprintf_small_stack) { |
| 545 | // Is it safe to call snprintf on a thread with a small stack? |
| 546 | // (The snprintf implementation puts some pretty large buffers on the stack.) |
| 547 | pthread_attr_t a; |
| 548 | ASSERT_EQ(0, pthread_attr_init(&a)); |
| 549 | ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN)); |
| 550 | |
| 551 | pthread_t t; |
| 552 | ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr)); |
| 553 | ASSERT_EQ(0, pthread_join(t, nullptr)); |
| 554 | } |
| 555 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 556 | TEST(STDIO_TEST, fprintf_failures_7229520) { |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 557 | // http://b/7229520 |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 558 | FILE* fp; |
| 559 | |
| 560 | // Unbuffered case where the fprintf(3) itself fails. |
| 561 | ASSERT_NE(nullptr, fp = tmpfile()); |
| 562 | setbuf(fp, NULL); |
| 563 | ASSERT_EQ(4, fprintf(fp, "epic")); |
| 564 | ASSERT_EQ(0, close(fileno(fp))); |
| 565 | ASSERT_EQ(-1, fprintf(fp, "fail")); |
| 566 | ASSERT_EQ(-1, fclose(fp)); |
| 567 | |
| 568 | // Buffered case where we won't notice until the fclose(3). |
| 569 | // It's likely this is what was actually seen in http://b/7229520, |
| 570 | // and that expecting fprintf to fail is setting yourself up for |
| 571 | // disappointment. Remember to check fclose(3)'s return value, kids! |
| 572 | ASSERT_NE(nullptr, fp = tmpfile()); |
| 573 | ASSERT_EQ(4, fprintf(fp, "epic")); |
| 574 | ASSERT_EQ(0, close(fileno(fp))); |
| 575 | ASSERT_EQ(4, fprintf(fp, "fail")); |
| 576 | ASSERT_EQ(-1, fclose(fp)); |
| 577 | } |
| 578 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 579 | TEST(STDIO_TEST, popen) { |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 580 | FILE* fp = popen("cat /proc/version", "r"); |
| 581 | ASSERT_TRUE(fp != NULL); |
| 582 | |
| 583 | char buf[16]; |
| 584 | char* s = fgets(buf, sizeof(buf), fp); |
| 585 | buf[13] = '\0'; |
| 586 | ASSERT_STREQ("Linux version", s); |
| 587 | |
| 588 | ASSERT_EQ(0, pclose(fp)); |
| 589 | } |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 590 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 591 | TEST(STDIO_TEST, getc) { |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 592 | FILE* fp = fopen("/proc/version", "r"); |
| 593 | ASSERT_TRUE(fp != NULL); |
| 594 | ASSERT_EQ('L', getc(fp)); |
| 595 | ASSERT_EQ('i', getc(fp)); |
| 596 | ASSERT_EQ('n', getc(fp)); |
| 597 | ASSERT_EQ('u', getc(fp)); |
| 598 | ASSERT_EQ('x', getc(fp)); |
| 599 | fclose(fp); |
| 600 | } |
| 601 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 602 | TEST(STDIO_TEST, putc) { |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 603 | FILE* fp = fopen("/proc/version", "r"); |
| 604 | ASSERT_TRUE(fp != NULL); |
| 605 | ASSERT_EQ(EOF, putc('x', fp)); |
| 606 | fclose(fp); |
| 607 | } |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 608 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 609 | TEST(STDIO_TEST, sscanf) { |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 610 | char s1[123]; |
| 611 | int i1; |
| 612 | double d1; |
| 613 | char s2[123]; |
| 614 | ASSERT_EQ(3, sscanf(" hello 123 1.23 ", "%s %i %lf %s", s1, &i1, &d1, s2)); |
| 615 | ASSERT_STREQ("hello", s1); |
| 616 | ASSERT_EQ(123, i1); |
Christopher Ferris | f171b34 | 2014-03-17 16:40:26 -0700 | [diff] [blame] | 617 | ASSERT_DOUBLE_EQ(1.23, d1); |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 618 | } |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 619 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 620 | TEST(STDIO_TEST, cantwrite_EBADF) { |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 621 | // If we open a file read-only... |
| 622 | FILE* fp = fopen("/proc/version", "r"); |
| 623 | |
| 624 | // ...all attempts to write to that file should return failure. |
| 625 | |
| 626 | // They should also set errno to EBADF. This isn't POSIX, but it's traditional. |
| 627 | // glibc gets the wide-character functions wrong. |
| 628 | |
| 629 | errno = 0; |
| 630 | EXPECT_EQ(EOF, putc('x', fp)); |
| 631 | EXPECT_EQ(EBADF, errno); |
| 632 | |
| 633 | errno = 0; |
| 634 | EXPECT_EQ(EOF, fprintf(fp, "hello")); |
| 635 | EXPECT_EQ(EBADF, errno); |
| 636 | |
| 637 | errno = 0; |
| 638 | EXPECT_EQ(EOF, fwprintf(fp, L"hello")); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 639 | #if defined(__BIONIC__) |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 640 | EXPECT_EQ(EBADF, errno); |
| 641 | #endif |
| 642 | |
| 643 | errno = 0; |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 644 | EXPECT_EQ(0U, fwrite("hello", 1, 2, fp)); |
| 645 | EXPECT_EQ(EBADF, errno); |
| 646 | |
| 647 | errno = 0; |
| 648 | EXPECT_EQ(EOF, fputs("hello", fp)); |
| 649 | EXPECT_EQ(EBADF, errno); |
| 650 | |
| 651 | errno = 0; |
| 652 | EXPECT_EQ(WEOF, fputwc(L'x', fp)); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 653 | #if defined(__BIONIC__) |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 654 | EXPECT_EQ(EBADF, errno); |
| 655 | #endif |
| 656 | } |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 657 | |
| 658 | // Tests that we can only have a consistent and correct fpos_t when using |
| 659 | // 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] | 660 | TEST(STDIO_TEST, consistent_fpos_t) { |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 661 | ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8")); |
| 662 | uselocale(LC_GLOBAL_LOCALE); |
| 663 | |
| 664 | FILE* fp = tmpfile(); |
| 665 | ASSERT_TRUE(fp != NULL); |
| 666 | |
| 667 | wchar_t mb_one_bytes = L'h'; |
| 668 | wchar_t mb_two_bytes = 0x00a2; |
| 669 | wchar_t mb_three_bytes = 0x20ac; |
| 670 | wchar_t mb_four_bytes = 0x24b62; |
| 671 | |
| 672 | // Write to file. |
| 673 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp))); |
| 674 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp))); |
| 675 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp))); |
| 676 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp))); |
| 677 | |
| 678 | rewind(fp); |
| 679 | |
| 680 | // Record each character position. |
| 681 | fpos_t pos1; |
| 682 | fpos_t pos2; |
| 683 | fpos_t pos3; |
| 684 | fpos_t pos4; |
| 685 | fpos_t pos5; |
| 686 | EXPECT_EQ(0, fgetpos(fp, &pos1)); |
| 687 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 688 | EXPECT_EQ(0, fgetpos(fp, &pos2)); |
| 689 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 690 | EXPECT_EQ(0, fgetpos(fp, &pos3)); |
| 691 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 692 | EXPECT_EQ(0, fgetpos(fp, &pos4)); |
| 693 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 694 | EXPECT_EQ(0, fgetpos(fp, &pos5)); |
| 695 | |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 696 | #if defined(__BIONIC__) |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 697 | // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD |
| 698 | // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In |
| 699 | // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE |
| 700 | // structure. |
| 701 | ASSERT_EQ(0, static_cast<off_t>(pos1)); |
| 702 | ASSERT_EQ(1, static_cast<off_t>(pos2)); |
| 703 | ASSERT_EQ(3, static_cast<off_t>(pos3)); |
| 704 | ASSERT_EQ(6, static_cast<off_t>(pos4)); |
| 705 | ASSERT_EQ(10, static_cast<off_t>(pos5)); |
| 706 | #endif |
| 707 | |
| 708 | // Exercise back and forth movements of the position. |
| 709 | ASSERT_EQ(0, fsetpos(fp, &pos2)); |
| 710 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 711 | ASSERT_EQ(0, fsetpos(fp, &pos1)); |
| 712 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 713 | ASSERT_EQ(0, fsetpos(fp, &pos4)); |
| 714 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 715 | ASSERT_EQ(0, fsetpos(fp, &pos3)); |
| 716 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 717 | ASSERT_EQ(0, fsetpos(fp, &pos5)); |
| 718 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 719 | |
| 720 | fclose(fp); |
| 721 | } |
| 722 | |
| 723 | // Exercise the interaction between fpos and seek. |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 724 | TEST(STDIO_TEST, fpos_t_and_seek) { |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 725 | ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8")); |
| 726 | uselocale(LC_GLOBAL_LOCALE); |
| 727 | |
Calin Juravle | 9b95ea9 | 2014-05-14 17:07:10 +0100 | [diff] [blame] | 728 | // In glibc-2.16 fseek doesn't work properly in wide mode |
| 729 | // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is |
| 730 | // to close and re-open the file. We do it in order to make the test pass |
| 731 | // with all glibcs. |
| 732 | |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 733 | TemporaryFile tf; |
| 734 | FILE* fp = fdopen(tf.fd, "w+"); |
| 735 | ASSERT_TRUE(fp != NULL); |
| 736 | |
| 737 | wchar_t mb_two_bytes = 0x00a2; |
| 738 | wchar_t mb_three_bytes = 0x20ac; |
| 739 | wchar_t mb_four_bytes = 0x24b62; |
| 740 | |
| 741 | // Write to file. |
| 742 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp))); |
| 743 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp))); |
| 744 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp))); |
| 745 | |
| 746 | fflush(fp); |
| 747 | fclose(fp); |
| 748 | |
| 749 | fp = fopen(tf.filename, "r"); |
| 750 | ASSERT_TRUE(fp != NULL); |
| 751 | |
| 752 | // Store a valid position. |
| 753 | fpos_t mb_two_bytes_pos; |
| 754 | ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos)); |
| 755 | |
| 756 | // Move inside mb_four_bytes with fseek. |
| 757 | long offset_inside_mb = 6; |
| 758 | ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET)); |
| 759 | |
| 760 | // Store the "inside multi byte" position. |
| 761 | fpos_t pos_inside_mb; |
| 762 | ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb)); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 763 | #if defined(__BIONIC__) |
| 764 | ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb)); |
| 765 | #endif |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 766 | |
| 767 | // Reading from within a byte should produce an error. |
| 768 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 769 | ASSERT_EQ(EILSEQ, errno); |
| 770 | |
| 771 | // Reverting to a valid position should work. |
| 772 | ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos)); |
| 773 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 774 | |
| 775 | // Moving withing a multi byte with fsetpos should work but reading should |
| 776 | // produce an error. |
| 777 | ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb)); |
| 778 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 779 | ASSERT_EQ(EILSEQ, errno); |
| 780 | |
| 781 | fclose(fp); |
| 782 | } |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 783 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 784 | TEST(STDIO_TEST, fmemopen) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 785 | char buf[16]; |
| 786 | memset(buf, 0, sizeof(buf)); |
| 787 | FILE* fp = fmemopen(buf, sizeof(buf), "r+"); |
| 788 | ASSERT_EQ('<', fputc('<', fp)); |
| 789 | ASSERT_NE(EOF, fputs("abc>\n", fp)); |
| 790 | fflush(fp); |
| 791 | |
| 792 | ASSERT_STREQ("<abc>\n", buf); |
| 793 | |
| 794 | rewind(fp); |
| 795 | |
| 796 | char line[16]; |
| 797 | char* s = fgets(line, sizeof(line), fp); |
| 798 | ASSERT_TRUE(s != NULL); |
| 799 | ASSERT_STREQ("<abc>\n", s); |
| 800 | |
| 801 | fclose(fp); |
| 802 | } |
| 803 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 804 | TEST(STDIO_TEST, fmemopen_NULL) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 805 | FILE* fp = fmemopen(nullptr, 128, "r+"); |
| 806 | ASSERT_NE(EOF, fputs("xyz\n", fp)); |
| 807 | |
| 808 | rewind(fp); |
| 809 | |
| 810 | char line[16]; |
| 811 | char* s = fgets(line, sizeof(line), fp); |
| 812 | ASSERT_TRUE(s != NULL); |
| 813 | ASSERT_STREQ("xyz\n", s); |
| 814 | |
| 815 | fclose(fp); |
| 816 | } |
| 817 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 818 | TEST(STDIO_TEST, fmemopen_EINVAL) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 819 | char buf[16]; |
| 820 | |
| 821 | // Invalid size. |
| 822 | errno = 0; |
| 823 | ASSERT_EQ(nullptr, fmemopen(buf, 0, "r+")); |
| 824 | ASSERT_EQ(EINVAL, errno); |
| 825 | |
| 826 | // No '+' with NULL buffer. |
| 827 | errno = 0; |
| 828 | ASSERT_EQ(nullptr, fmemopen(nullptr, 0, "r")); |
| 829 | ASSERT_EQ(EINVAL, errno); |
| 830 | } |
| 831 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 832 | TEST(STDIO_TEST, open_memstream) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 833 | char* p = nullptr; |
| 834 | size_t size = 0; |
| 835 | FILE* fp = open_memstream(&p, &size); |
| 836 | ASSERT_NE(EOF, fputs("hello, world!", fp)); |
| 837 | fclose(fp); |
| 838 | |
| 839 | ASSERT_STREQ("hello, world!", p); |
| 840 | ASSERT_EQ(strlen("hello, world!"), size); |
| 841 | free(p); |
| 842 | } |
| 843 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 844 | TEST(STDIO_TEST, open_memstream_EINVAL) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 845 | #if defined(__BIONIC__) |
| 846 | char* p; |
| 847 | size_t size; |
| 848 | |
| 849 | // Invalid buffer. |
| 850 | errno = 0; |
| 851 | ASSERT_EQ(nullptr, open_memstream(nullptr, &size)); |
| 852 | ASSERT_EQ(EINVAL, errno); |
| 853 | |
| 854 | // Invalid size. |
| 855 | errno = 0; |
| 856 | ASSERT_EQ(nullptr, open_memstream(&p, nullptr)); |
| 857 | ASSERT_EQ(EINVAL, errno); |
| 858 | #else |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 859 | GTEST_LOG_(INFO) << "This test does nothing on glibc.\n"; |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 860 | #endif |
| 861 | } |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 862 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 863 | TEST(STDIO_TEST, fdopen_CLOEXEC) { |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 864 | int fd = open("/proc/version", O_RDONLY); |
| 865 | ASSERT_TRUE(fd != -1); |
| 866 | |
| 867 | // This fd doesn't have O_CLOEXEC... |
| 868 | int flags = fcntl(fd, F_GETFD); |
| 869 | ASSERT_TRUE(flags != -1); |
| 870 | ASSERT_EQ(0, flags & FD_CLOEXEC); |
| 871 | |
| 872 | FILE* fp = fdopen(fd, "re"); |
| 873 | ASSERT_TRUE(fp != NULL); |
| 874 | |
| 875 | // ...but the new one does. |
| 876 | flags = fcntl(fileno(fp), F_GETFD); |
| 877 | ASSERT_TRUE(flags != -1); |
| 878 | ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC); |
| 879 | |
| 880 | fclose(fp); |
| 881 | close(fd); |
| 882 | } |
| 883 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 884 | TEST(STDIO_TEST, freopen_CLOEXEC) { |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 885 | FILE* fp = fopen("/proc/version", "r"); |
| 886 | ASSERT_TRUE(fp != NULL); |
| 887 | |
| 888 | // This FILE* doesn't have O_CLOEXEC... |
| 889 | int flags = fcntl(fileno(fp), F_GETFD); |
| 890 | ASSERT_TRUE(flags != -1); |
| 891 | ASSERT_EQ(0, flags & FD_CLOEXEC); |
| 892 | |
| 893 | fp = freopen("/proc/version", "re", fp); |
| 894 | |
| 895 | // ...but the new one does. |
| 896 | flags = fcntl(fileno(fp), F_GETFD); |
| 897 | ASSERT_TRUE(flags != -1); |
| 898 | ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC); |
| 899 | |
| 900 | fclose(fp); |
| 901 | } |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 902 | |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 903 | TEST(STDIO_TEST, fopen64_freopen64) { |
| 904 | FILE* fp = fopen64("/proc/version", "r"); |
| 905 | ASSERT_TRUE(fp != nullptr); |
| 906 | fp = freopen64("/proc/version", "re", fp); |
| 907 | ASSERT_TRUE(fp != nullptr); |
| 908 | fclose(fp); |
| 909 | } |
| 910 | |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 911 | // https://code.google.com/p/android/issues/detail?id=81155 |
| 912 | // http://b/18556607 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 913 | TEST(STDIO_TEST, fread_unbuffered_pathological_performance) { |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 914 | FILE* fp = fopen("/dev/zero", "r"); |
| 915 | ASSERT_TRUE(fp != NULL); |
| 916 | |
| 917 | // Make this stream unbuffered. |
| 918 | setvbuf(fp, 0, _IONBF, 0); |
| 919 | |
| 920 | char buf[65*1024]; |
| 921 | memset(buf, 0xff, sizeof(buf)); |
| 922 | |
| 923 | time_t t0 = time(NULL); |
| 924 | for (size_t i = 0; i < 1024; ++i) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 925 | ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp)); |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 926 | } |
| 927 | time_t t1 = time(NULL); |
| 928 | |
| 929 | fclose(fp); |
| 930 | |
| 931 | // 1024 64KiB reads should have been very quick. |
| 932 | ASSERT_LE(t1 - t0, 1); |
| 933 | |
| 934 | for (size_t i = 0; i < 64*1024; ++i) { |
| 935 | ASSERT_EQ('\0', buf[i]); |
| 936 | } |
| 937 | for (size_t i = 64*1024; i < 65*1024; ++i) { |
| 938 | ASSERT_EQ('\xff', buf[i]); |
| 939 | } |
| 940 | } |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 941 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 942 | TEST(STDIO_TEST, fread_EOF) { |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 943 | std::string digits("0123456789"); |
| 944 | FILE* fp = fmemopen(&digits[0], digits.size(), "r"); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 945 | |
| 946 | // Try to read too much, but little enough that it still fits in the FILE's internal buffer. |
| 947 | char buf1[4 * 4]; |
| 948 | memset(buf1, 0, sizeof(buf1)); |
| 949 | ASSERT_EQ(2U, fread(buf1, 4, 4, fp)); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 950 | ASSERT_STREQ("0123456789", buf1); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 951 | ASSERT_TRUE(feof(fp)); |
| 952 | |
| 953 | rewind(fp); |
| 954 | |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 955 | // Try to read way too much so stdio tries to read more direct from the stream. |
| 956 | char buf2[4 * 4096]; |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 957 | memset(buf2, 0, sizeof(buf2)); |
| 958 | ASSERT_EQ(2U, fread(buf2, 4, 4096, fp)); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 959 | ASSERT_STREQ("0123456789", buf2); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 960 | ASSERT_TRUE(feof(fp)); |
| 961 | |
| 962 | fclose(fp); |
| 963 | } |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 964 | |
| 965 | static void test_fread_from_write_only_stream(size_t n) { |
| 966 | FILE* fp = fopen("/dev/null", "w"); |
| 967 | std::vector<char> buf(n, 0); |
| 968 | errno = 0; |
| 969 | ASSERT_EQ(0U, fread(&buf[0], n, 1, fp)); |
| 970 | ASSERT_EQ(EBADF, errno); |
| 971 | ASSERT_TRUE(ferror(fp)); |
| 972 | ASSERT_FALSE(feof(fp)); |
| 973 | fclose(fp); |
| 974 | } |
| 975 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 976 | TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 977 | test_fread_from_write_only_stream(1); |
| 978 | } |
| 979 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 980 | TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 981 | test_fread_from_write_only_stream(64*1024); |
| 982 | } |
| 983 | |
| 984 | static void test_fwrite_after_fread(size_t n) { |
| 985 | TemporaryFile tf; |
| 986 | |
| 987 | FILE* fp = fdopen(tf.fd, "w+"); |
| 988 | ASSERT_EQ(1U, fwrite("1", 1, 1, fp)); |
| 989 | fflush(fp); |
| 990 | |
| 991 | // We've flushed but not rewound, so there's nothing to read. |
| 992 | std::vector<char> buf(n, 0); |
| 993 | ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp)); |
| 994 | ASSERT_TRUE(feof(fp)); |
| 995 | |
| 996 | // But hitting EOF doesn't prevent us from writing... |
| 997 | errno = 0; |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 998 | ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno); |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 999 | |
| 1000 | // And if we rewind, everything's there. |
| 1001 | rewind(fp); |
| 1002 | ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp)); |
| 1003 | ASSERT_EQ('1', buf[0]); |
| 1004 | ASSERT_EQ('2', buf[1]); |
| 1005 | |
| 1006 | fclose(fp); |
| 1007 | } |
| 1008 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1009 | TEST(STDIO_TEST, fwrite_after_fread_slow_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1010 | test_fwrite_after_fread(16); |
| 1011 | } |
| 1012 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1013 | TEST(STDIO_TEST, fwrite_after_fread_fast_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1014 | test_fwrite_after_fread(64*1024); |
| 1015 | } |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 1016 | |
| 1017 | // http://b/19172514 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1018 | TEST(STDIO_TEST, fread_after_fseek) { |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 1019 | TemporaryFile tf; |
| 1020 | |
| 1021 | FILE* fp = fopen(tf.filename, "w+"); |
| 1022 | ASSERT_TRUE(fp != nullptr); |
| 1023 | |
| 1024 | char file_data[12288]; |
| 1025 | for (size_t i = 0; i < 12288; i++) { |
| 1026 | file_data[i] = i; |
| 1027 | } |
| 1028 | ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp)); |
| 1029 | fclose(fp); |
| 1030 | |
| 1031 | fp = fopen(tf.filename, "r"); |
| 1032 | ASSERT_TRUE(fp != nullptr); |
| 1033 | |
| 1034 | char buffer[8192]; |
| 1035 | size_t cur_location = 0; |
| 1036 | // Small read to populate internal buffer. |
| 1037 | ASSERT_EQ(100U, fread(buffer, 1, 100, fp)); |
| 1038 | ASSERT_EQ(memcmp(file_data, buffer, 100), 0); |
| 1039 | |
| 1040 | cur_location = static_cast<size_t>(ftell(fp)); |
| 1041 | // Large read to force reading into the user supplied buffer and bypassing |
| 1042 | // the internal buffer. |
| 1043 | ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp)); |
| 1044 | ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0); |
| 1045 | |
| 1046 | // Small backwards seek to verify fseek does not reuse the internal buffer. |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1047 | ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno); |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 1048 | cur_location = static_cast<size_t>(ftell(fp)); |
| 1049 | ASSERT_EQ(22U, fread(buffer, 1, 22, fp)); |
| 1050 | ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0); |
| 1051 | |
| 1052 | fclose(fp); |
| 1053 | } |
Elliott Hughes | 8ab433d | 2015-10-09 17:57:26 -0700 | [diff] [blame] | 1054 | |
| 1055 | // https://code.google.com/p/android/issues/detail?id=184847 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1056 | TEST(STDIO_TEST, fread_EOF_184847) { |
Elliott Hughes | 8ab433d | 2015-10-09 17:57:26 -0700 | [diff] [blame] | 1057 | TemporaryFile tf; |
| 1058 | char buf[6] = {0}; |
| 1059 | |
| 1060 | FILE* fw = fopen(tf.filename, "w"); |
| 1061 | ASSERT_TRUE(fw != nullptr); |
| 1062 | |
| 1063 | FILE* fr = fopen(tf.filename, "r"); |
| 1064 | ASSERT_TRUE(fr != nullptr); |
| 1065 | |
| 1066 | fwrite("a", 1, 1, fw); |
| 1067 | fflush(fw); |
| 1068 | ASSERT_EQ(1U, fread(buf, 1, 1, fr)); |
| 1069 | ASSERT_STREQ("a", buf); |
| 1070 | |
| 1071 | // 'fr' is now at EOF. |
| 1072 | ASSERT_EQ(0U, fread(buf, 1, 1, fr)); |
| 1073 | ASSERT_TRUE(feof(fr)); |
| 1074 | |
| 1075 | // Write some more... |
| 1076 | fwrite("z", 1, 1, fw); |
| 1077 | fflush(fw); |
| 1078 | |
| 1079 | // ...and check that we can read it back. |
| 1080 | // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.) |
| 1081 | ASSERT_EQ(1U, fread(buf, 1, 1, fr)); |
| 1082 | ASSERT_STREQ("z", buf); |
| 1083 | |
| 1084 | // But now we're done. |
| 1085 | ASSERT_EQ(0U, fread(buf, 1, 1, fr)); |
| 1086 | |
| 1087 | fclose(fr); |
| 1088 | fclose(fw); |
| 1089 | } |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 1090 | |
| 1091 | TEST(STDIO_TEST, fclose_invalidates_fd) { |
| 1092 | // The typical error we're trying to help people catch involves accessing |
| 1093 | // memory after it's been freed. But we know that stdin/stdout/stderr are |
| 1094 | // special and don't get deallocated, so this test uses stdin. |
| 1095 | ASSERT_EQ(0, fclose(stdin)); |
| 1096 | |
| 1097 | // Even though using a FILE* after close is undefined behavior, I've closed |
| 1098 | // this bug as "WAI" too many times. We shouldn't hand out stale fds, |
| 1099 | // especially because they might actually correspond to a real stream. |
| 1100 | errno = 0; |
| 1101 | ASSERT_EQ(-1, fileno(stdin)); |
| 1102 | ASSERT_EQ(EBADF, errno); |
| 1103 | } |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 1104 | |
| 1105 | TEST(STDIO_TEST, fseek_ftell_unseekable) { |
| 1106 | #if defined(__BIONIC__) // glibc has fopencookie instead. |
| 1107 | auto read_fn = [](void*, char*, int) { return -1; }; |
| 1108 | FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr); |
| 1109 | ASSERT_TRUE(fp != nullptr); |
| 1110 | |
| 1111 | // Check that ftell balks on an unseekable FILE*. |
| 1112 | errno = 0; |
| 1113 | ASSERT_EQ(-1, ftell(fp)); |
| 1114 | ASSERT_EQ(ESPIPE, errno); |
| 1115 | |
| 1116 | // SEEK_CUR is rewritten as SEEK_SET internally... |
| 1117 | errno = 0; |
| 1118 | ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR)); |
| 1119 | ASSERT_EQ(ESPIPE, errno); |
| 1120 | |
| 1121 | // ...so it's worth testing the direct seek path too. |
| 1122 | errno = 0; |
| 1123 | ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET)); |
| 1124 | ASSERT_EQ(ESPIPE, errno); |
| 1125 | |
| 1126 | fclose(fp); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 1127 | #else |
| 1128 | GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; |
| 1129 | #endif |
| 1130 | } |
| 1131 | |
| 1132 | TEST(STDIO_TEST, funopen_EINVAL) { |
| 1133 | #if defined(__BIONIC__) |
| 1134 | errno = 0; |
| 1135 | ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr)); |
| 1136 | ASSERT_EQ(EINVAL, errno); |
| 1137 | #else |
| 1138 | GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; |
| 1139 | #endif |
| 1140 | } |
| 1141 | |
| 1142 | TEST(STDIO_TEST, funopen_seek) { |
| 1143 | #if defined(__BIONIC__) |
| 1144 | auto read_fn = [](void*, char*, int) { return -1; }; |
| 1145 | |
| 1146 | auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; }; |
| 1147 | auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; }; |
| 1148 | |
| 1149 | FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr); |
| 1150 | ASSERT_TRUE(fp != nullptr); |
| 1151 | fpos_t pos; |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 1152 | #if defined(__LP64__) |
| 1153 | EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno); |
| 1154 | EXPECT_EQ(0xfedcba12LL, pos); |
| 1155 | #else |
| 1156 | EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno); |
| 1157 | EXPECT_EQ(EOVERFLOW, errno); |
| 1158 | #endif |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 1159 | |
| 1160 | FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr); |
| 1161 | ASSERT_TRUE(fp64 != nullptr); |
| 1162 | fpos64_t pos64; |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 1163 | EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno); |
| 1164 | EXPECT_EQ(0xfedcba12345678, pos64); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 1165 | #else |
| 1166 | GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 1167 | #endif |
| 1168 | } |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 1169 | |
| 1170 | TEST(STDIO_TEST, lots_of_concurrent_files) { |
| 1171 | std::vector<TemporaryFile*> tfs; |
| 1172 | std::vector<FILE*> fps; |
| 1173 | |
| 1174 | for (size_t i = 0; i < 256; ++i) { |
| 1175 | TemporaryFile* tf = new TemporaryFile; |
| 1176 | tfs.push_back(tf); |
| 1177 | FILE* fp = fopen(tf->filename, "w+"); |
| 1178 | fps.push_back(fp); |
| 1179 | fprintf(fp, "hello %zu!\n", i); |
| 1180 | fflush(fp); |
| 1181 | } |
| 1182 | |
| 1183 | for (size_t i = 0; i < 256; ++i) { |
| 1184 | rewind(fps[i]); |
| 1185 | |
| 1186 | char buf[BUFSIZ]; |
| 1187 | ASSERT_TRUE(fgets(buf, sizeof(buf), fps[i]) != nullptr); |
| 1188 | |
| 1189 | char expected[BUFSIZ]; |
| 1190 | snprintf(expected, sizeof(expected), "hello %zu!\n", i); |
| 1191 | ASSERT_STREQ(expected, buf); |
| 1192 | |
| 1193 | fclose(fps[i]); |
| 1194 | delete tfs[i]; |
| 1195 | } |
| 1196 | } |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1197 | |
| 1198 | static void AssertFileOffsetAt(FILE* fp, off64_t offset) { |
| 1199 | EXPECT_EQ(offset, ftell(fp)); |
| 1200 | EXPECT_EQ(offset, ftello(fp)); |
Elliott Hughes | e4fa6e9 | 2016-02-02 22:39:15 -0800 | [diff] [blame] | 1201 | EXPECT_EQ(offset, ftello64(fp)); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1202 | fpos_t pos; |
| 1203 | fpos64_t pos64; |
| 1204 | EXPECT_EQ(0, fgetpos(fp, &pos)); |
| 1205 | EXPECT_EQ(0, fgetpos64(fp, &pos64)); |
| 1206 | #if defined(__BIONIC__) |
| 1207 | EXPECT_EQ(offset, static_cast<off64_t>(pos)); |
| 1208 | EXPECT_EQ(offset, static_cast<off64_t>(pos64)); |
| 1209 | #else |
| 1210 | GTEST_LOG_(INFO) << "glibc's fpos_t is opaque.\n"; |
| 1211 | #endif |
| 1212 | } |
| 1213 | |
| 1214 | TEST(STDIO_TEST, seek_tell_family_smoke) { |
| 1215 | TemporaryFile tf; |
| 1216 | FILE* fp = fdopen(tf.fd, "w+"); |
| 1217 | |
| 1218 | // Initially we should be at 0. |
| 1219 | AssertFileOffsetAt(fp, 0); |
| 1220 | |
| 1221 | // Seek to offset 8192. |
| 1222 | ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET)); |
| 1223 | AssertFileOffsetAt(fp, 8192); |
| 1224 | fpos_t eight_k_pos; |
| 1225 | ASSERT_EQ(0, fgetpos(fp, &eight_k_pos)); |
| 1226 | |
| 1227 | // Seek forward another 8192... |
| 1228 | ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR)); |
| 1229 | AssertFileOffsetAt(fp, 8192 + 8192); |
| 1230 | fpos64_t sixteen_k_pos64; |
| 1231 | ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64)); |
| 1232 | |
| 1233 | // Seek back 8192... |
| 1234 | ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR)); |
| 1235 | AssertFileOffsetAt(fp, 8192); |
| 1236 | |
| 1237 | // Since we haven't written anything, the end is also at 0. |
| 1238 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1239 | AssertFileOffsetAt(fp, 0); |
| 1240 | |
| 1241 | // Check that our fpos64_t from 16KiB works... |
| 1242 | ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64)); |
| 1243 | AssertFileOffsetAt(fp, 8192 + 8192); |
| 1244 | // ...as does our fpos_t from 8192. |
| 1245 | ASSERT_EQ(0, fsetpos(fp, &eight_k_pos)); |
| 1246 | AssertFileOffsetAt(fp, 8192); |
| 1247 | |
| 1248 | // Do fseeko and fseeko64 work too? |
| 1249 | ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET)); |
| 1250 | AssertFileOffsetAt(fp, 1234); |
| 1251 | ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET)); |
| 1252 | AssertFileOffsetAt(fp, 5678); |
| 1253 | |
| 1254 | fclose(fp); |
| 1255 | } |
| 1256 | |
| 1257 | TEST(STDIO_TEST, fseek_fseeko_EINVAL) { |
| 1258 | TemporaryFile tf; |
| 1259 | FILE* fp = fdopen(tf.fd, "w+"); |
| 1260 | |
| 1261 | // Bad whence. |
| 1262 | errno = 0; |
| 1263 | ASSERT_EQ(-1, fseek(fp, 0, 123)); |
| 1264 | ASSERT_EQ(EINVAL, errno); |
| 1265 | errno = 0; |
| 1266 | ASSERT_EQ(-1, fseeko(fp, 0, 123)); |
| 1267 | ASSERT_EQ(EINVAL, errno); |
| 1268 | errno = 0; |
| 1269 | ASSERT_EQ(-1, fseeko64(fp, 0, 123)); |
| 1270 | ASSERT_EQ(EINVAL, errno); |
| 1271 | |
| 1272 | // Bad offset. |
| 1273 | errno = 0; |
| 1274 | ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET)); |
| 1275 | ASSERT_EQ(EINVAL, errno); |
| 1276 | errno = 0; |
| 1277 | ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET)); |
| 1278 | ASSERT_EQ(EINVAL, errno); |
| 1279 | errno = 0; |
| 1280 | ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET)); |
| 1281 | ASSERT_EQ(EINVAL, errno); |
| 1282 | |
| 1283 | fclose(fp); |
| 1284 | } |