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 | |
Elliott Hughes | 8200e55 | 2016-02-05 21:57:37 -0800 | [diff] [blame] | 556 | TEST(STDIO_TEST, snprintf_asterisk_overflow) { |
| 557 | char buf[128]; |
| 558 | ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!')); |
| 559 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!')); |
| 560 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!')); |
| 561 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!')); |
| 562 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!')); |
| 563 | |
| 564 | // INT_MAX-1, INT_MAX, INT_MAX+1. |
| 565 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!')); |
| 566 | ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!')); |
| 567 | ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!')); |
| 568 | ASSERT_EQ(ENOMEM, errno); |
| 569 | } |
| 570 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 571 | TEST(STDIO_TEST, fprintf_failures_7229520) { |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 572 | // http://b/7229520 |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 573 | FILE* fp; |
| 574 | |
| 575 | // Unbuffered case where the fprintf(3) itself fails. |
| 576 | ASSERT_NE(nullptr, fp = tmpfile()); |
| 577 | setbuf(fp, NULL); |
| 578 | ASSERT_EQ(4, fprintf(fp, "epic")); |
| 579 | ASSERT_EQ(0, close(fileno(fp))); |
| 580 | ASSERT_EQ(-1, fprintf(fp, "fail")); |
| 581 | ASSERT_EQ(-1, fclose(fp)); |
| 582 | |
| 583 | // Buffered case where we won't notice until the fclose(3). |
| 584 | // It's likely this is what was actually seen in http://b/7229520, |
| 585 | // and that expecting fprintf to fail is setting yourself up for |
| 586 | // disappointment. Remember to check fclose(3)'s return value, kids! |
| 587 | ASSERT_NE(nullptr, fp = tmpfile()); |
| 588 | ASSERT_EQ(4, fprintf(fp, "epic")); |
| 589 | ASSERT_EQ(0, close(fileno(fp))); |
| 590 | ASSERT_EQ(4, fprintf(fp, "fail")); |
| 591 | ASSERT_EQ(-1, fclose(fp)); |
| 592 | } |
| 593 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 594 | TEST(STDIO_TEST, popen) { |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 595 | FILE* fp = popen("cat /proc/version", "r"); |
| 596 | ASSERT_TRUE(fp != NULL); |
| 597 | |
| 598 | char buf[16]; |
| 599 | char* s = fgets(buf, sizeof(buf), fp); |
| 600 | buf[13] = '\0'; |
| 601 | ASSERT_STREQ("Linux version", s); |
| 602 | |
| 603 | ASSERT_EQ(0, pclose(fp)); |
| 604 | } |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 605 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 606 | TEST(STDIO_TEST, getc) { |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 607 | FILE* fp = fopen("/proc/version", "r"); |
| 608 | ASSERT_TRUE(fp != NULL); |
| 609 | ASSERT_EQ('L', getc(fp)); |
| 610 | ASSERT_EQ('i', getc(fp)); |
| 611 | ASSERT_EQ('n', getc(fp)); |
| 612 | ASSERT_EQ('u', getc(fp)); |
| 613 | ASSERT_EQ('x', getc(fp)); |
| 614 | fclose(fp); |
| 615 | } |
| 616 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 617 | TEST(STDIO_TEST, putc) { |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 618 | FILE* fp = fopen("/proc/version", "r"); |
| 619 | ASSERT_TRUE(fp != NULL); |
| 620 | ASSERT_EQ(EOF, putc('x', fp)); |
| 621 | fclose(fp); |
| 622 | } |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 623 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 624 | TEST(STDIO_TEST, sscanf) { |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 625 | char s1[123]; |
| 626 | int i1; |
| 627 | double d1; |
| 628 | char s2[123]; |
| 629 | ASSERT_EQ(3, sscanf(" hello 123 1.23 ", "%s %i %lf %s", s1, &i1, &d1, s2)); |
| 630 | ASSERT_STREQ("hello", s1); |
| 631 | ASSERT_EQ(123, i1); |
Christopher Ferris | f171b34 | 2014-03-17 16:40:26 -0700 | [diff] [blame] | 632 | ASSERT_DOUBLE_EQ(1.23, d1); |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 633 | } |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 634 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 635 | TEST(STDIO_TEST, cantwrite_EBADF) { |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 636 | // If we open a file read-only... |
| 637 | FILE* fp = fopen("/proc/version", "r"); |
| 638 | |
| 639 | // ...all attempts to write to that file should return failure. |
| 640 | |
| 641 | // They should also set errno to EBADF. This isn't POSIX, but it's traditional. |
| 642 | // glibc gets the wide-character functions wrong. |
| 643 | |
| 644 | errno = 0; |
| 645 | EXPECT_EQ(EOF, putc('x', fp)); |
| 646 | EXPECT_EQ(EBADF, errno); |
| 647 | |
| 648 | errno = 0; |
| 649 | EXPECT_EQ(EOF, fprintf(fp, "hello")); |
| 650 | EXPECT_EQ(EBADF, errno); |
| 651 | |
| 652 | errno = 0; |
| 653 | EXPECT_EQ(EOF, fwprintf(fp, L"hello")); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 654 | #if defined(__BIONIC__) |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 655 | EXPECT_EQ(EBADF, errno); |
| 656 | #endif |
| 657 | |
| 658 | errno = 0; |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 659 | EXPECT_EQ(0U, fwrite("hello", 1, 2, fp)); |
| 660 | EXPECT_EQ(EBADF, errno); |
| 661 | |
| 662 | errno = 0; |
| 663 | EXPECT_EQ(EOF, fputs("hello", fp)); |
| 664 | EXPECT_EQ(EBADF, errno); |
| 665 | |
| 666 | errno = 0; |
| 667 | EXPECT_EQ(WEOF, fputwc(L'x', fp)); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 668 | #if defined(__BIONIC__) |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 669 | EXPECT_EQ(EBADF, errno); |
| 670 | #endif |
| 671 | } |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 672 | |
| 673 | // Tests that we can only have a consistent and correct fpos_t when using |
| 674 | // 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] | 675 | TEST(STDIO_TEST, consistent_fpos_t) { |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 676 | ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8")); |
| 677 | uselocale(LC_GLOBAL_LOCALE); |
| 678 | |
| 679 | FILE* fp = tmpfile(); |
| 680 | ASSERT_TRUE(fp != NULL); |
| 681 | |
| 682 | wchar_t mb_one_bytes = L'h'; |
| 683 | wchar_t mb_two_bytes = 0x00a2; |
| 684 | wchar_t mb_three_bytes = 0x20ac; |
| 685 | wchar_t mb_four_bytes = 0x24b62; |
| 686 | |
| 687 | // Write to file. |
| 688 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp))); |
| 689 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp))); |
| 690 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp))); |
| 691 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp))); |
| 692 | |
| 693 | rewind(fp); |
| 694 | |
| 695 | // Record each character position. |
| 696 | fpos_t pos1; |
| 697 | fpos_t pos2; |
| 698 | fpos_t pos3; |
| 699 | fpos_t pos4; |
| 700 | fpos_t pos5; |
| 701 | EXPECT_EQ(0, fgetpos(fp, &pos1)); |
| 702 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 703 | EXPECT_EQ(0, fgetpos(fp, &pos2)); |
| 704 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 705 | EXPECT_EQ(0, fgetpos(fp, &pos3)); |
| 706 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 707 | EXPECT_EQ(0, fgetpos(fp, &pos4)); |
| 708 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 709 | EXPECT_EQ(0, fgetpos(fp, &pos5)); |
| 710 | |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 711 | #if defined(__BIONIC__) |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 712 | // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD |
| 713 | // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In |
| 714 | // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE |
| 715 | // structure. |
| 716 | ASSERT_EQ(0, static_cast<off_t>(pos1)); |
| 717 | ASSERT_EQ(1, static_cast<off_t>(pos2)); |
| 718 | ASSERT_EQ(3, static_cast<off_t>(pos3)); |
| 719 | ASSERT_EQ(6, static_cast<off_t>(pos4)); |
| 720 | ASSERT_EQ(10, static_cast<off_t>(pos5)); |
| 721 | #endif |
| 722 | |
| 723 | // Exercise back and forth movements of the position. |
| 724 | ASSERT_EQ(0, fsetpos(fp, &pos2)); |
| 725 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 726 | ASSERT_EQ(0, fsetpos(fp, &pos1)); |
| 727 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 728 | ASSERT_EQ(0, fsetpos(fp, &pos4)); |
| 729 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 730 | ASSERT_EQ(0, fsetpos(fp, &pos3)); |
| 731 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 732 | ASSERT_EQ(0, fsetpos(fp, &pos5)); |
| 733 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 734 | |
| 735 | fclose(fp); |
| 736 | } |
| 737 | |
| 738 | // Exercise the interaction between fpos and seek. |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 739 | TEST(STDIO_TEST, fpos_t_and_seek) { |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 740 | ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8")); |
| 741 | uselocale(LC_GLOBAL_LOCALE); |
| 742 | |
Calin Juravle | 9b95ea9 | 2014-05-14 17:07:10 +0100 | [diff] [blame] | 743 | // In glibc-2.16 fseek doesn't work properly in wide mode |
| 744 | // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is |
| 745 | // to close and re-open the file. We do it in order to make the test pass |
| 746 | // with all glibcs. |
| 747 | |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 748 | TemporaryFile tf; |
| 749 | FILE* fp = fdopen(tf.fd, "w+"); |
| 750 | ASSERT_TRUE(fp != NULL); |
| 751 | |
| 752 | wchar_t mb_two_bytes = 0x00a2; |
| 753 | wchar_t mb_three_bytes = 0x20ac; |
| 754 | wchar_t mb_four_bytes = 0x24b62; |
| 755 | |
| 756 | // Write to file. |
| 757 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp))); |
| 758 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp))); |
| 759 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp))); |
| 760 | |
| 761 | fflush(fp); |
| 762 | fclose(fp); |
| 763 | |
| 764 | fp = fopen(tf.filename, "r"); |
| 765 | ASSERT_TRUE(fp != NULL); |
| 766 | |
| 767 | // Store a valid position. |
| 768 | fpos_t mb_two_bytes_pos; |
| 769 | ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos)); |
| 770 | |
| 771 | // Move inside mb_four_bytes with fseek. |
| 772 | long offset_inside_mb = 6; |
| 773 | ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET)); |
| 774 | |
| 775 | // Store the "inside multi byte" position. |
| 776 | fpos_t pos_inside_mb; |
| 777 | ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb)); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 778 | #if defined(__BIONIC__) |
| 779 | ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb)); |
| 780 | #endif |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 781 | |
| 782 | // Reading from within a byte should produce an error. |
| 783 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 784 | ASSERT_EQ(EILSEQ, errno); |
| 785 | |
| 786 | // Reverting to a valid position should work. |
| 787 | ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos)); |
| 788 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 789 | |
| 790 | // Moving withing a multi byte with fsetpos should work but reading should |
| 791 | // produce an error. |
| 792 | ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb)); |
| 793 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 794 | ASSERT_EQ(EILSEQ, errno); |
| 795 | |
| 796 | fclose(fp); |
| 797 | } |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 798 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 799 | TEST(STDIO_TEST, fmemopen) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 800 | char buf[16]; |
| 801 | memset(buf, 0, sizeof(buf)); |
| 802 | FILE* fp = fmemopen(buf, sizeof(buf), "r+"); |
| 803 | ASSERT_EQ('<', fputc('<', fp)); |
| 804 | ASSERT_NE(EOF, fputs("abc>\n", fp)); |
| 805 | fflush(fp); |
| 806 | |
| 807 | ASSERT_STREQ("<abc>\n", buf); |
| 808 | |
| 809 | rewind(fp); |
| 810 | |
| 811 | char line[16]; |
| 812 | char* s = fgets(line, sizeof(line), fp); |
| 813 | ASSERT_TRUE(s != NULL); |
| 814 | ASSERT_STREQ("<abc>\n", s); |
| 815 | |
| 816 | fclose(fp); |
| 817 | } |
| 818 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 819 | TEST(STDIO_TEST, fmemopen_NULL) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 820 | FILE* fp = fmemopen(nullptr, 128, "r+"); |
| 821 | ASSERT_NE(EOF, fputs("xyz\n", fp)); |
| 822 | |
| 823 | rewind(fp); |
| 824 | |
| 825 | char line[16]; |
| 826 | char* s = fgets(line, sizeof(line), fp); |
| 827 | ASSERT_TRUE(s != NULL); |
| 828 | ASSERT_STREQ("xyz\n", s); |
| 829 | |
| 830 | fclose(fp); |
| 831 | } |
| 832 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 833 | TEST(STDIO_TEST, fmemopen_EINVAL) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 834 | char buf[16]; |
| 835 | |
| 836 | // Invalid size. |
| 837 | errno = 0; |
| 838 | ASSERT_EQ(nullptr, fmemopen(buf, 0, "r+")); |
| 839 | ASSERT_EQ(EINVAL, errno); |
| 840 | |
| 841 | // No '+' with NULL buffer. |
| 842 | errno = 0; |
| 843 | ASSERT_EQ(nullptr, fmemopen(nullptr, 0, "r")); |
| 844 | ASSERT_EQ(EINVAL, errno); |
| 845 | } |
| 846 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 847 | TEST(STDIO_TEST, open_memstream) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 848 | char* p = nullptr; |
| 849 | size_t size = 0; |
| 850 | FILE* fp = open_memstream(&p, &size); |
| 851 | ASSERT_NE(EOF, fputs("hello, world!", fp)); |
| 852 | fclose(fp); |
| 853 | |
| 854 | ASSERT_STREQ("hello, world!", p); |
| 855 | ASSERT_EQ(strlen("hello, world!"), size); |
| 856 | free(p); |
| 857 | } |
| 858 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 859 | TEST(STDIO_TEST, open_memstream_EINVAL) { |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 860 | #if defined(__BIONIC__) |
| 861 | char* p; |
| 862 | size_t size; |
| 863 | |
| 864 | // Invalid buffer. |
| 865 | errno = 0; |
| 866 | ASSERT_EQ(nullptr, open_memstream(nullptr, &size)); |
| 867 | ASSERT_EQ(EINVAL, errno); |
| 868 | |
| 869 | // Invalid size. |
| 870 | errno = 0; |
| 871 | ASSERT_EQ(nullptr, open_memstream(&p, nullptr)); |
| 872 | ASSERT_EQ(EINVAL, errno); |
| 873 | #else |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 874 | GTEST_LOG_(INFO) << "This test does nothing on glibc.\n"; |
Elliott Hughes | 6b841db | 2014-08-20 16:10:49 -0700 | [diff] [blame] | 875 | #endif |
| 876 | } |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 877 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 878 | TEST(STDIO_TEST, fdopen_CLOEXEC) { |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 879 | int fd = open("/proc/version", O_RDONLY); |
| 880 | ASSERT_TRUE(fd != -1); |
| 881 | |
| 882 | // This fd doesn't have O_CLOEXEC... |
| 883 | int flags = fcntl(fd, F_GETFD); |
| 884 | ASSERT_TRUE(flags != -1); |
| 885 | ASSERT_EQ(0, flags & FD_CLOEXEC); |
| 886 | |
| 887 | FILE* fp = fdopen(fd, "re"); |
| 888 | ASSERT_TRUE(fp != NULL); |
| 889 | |
| 890 | // ...but the new one does. |
| 891 | flags = fcntl(fileno(fp), F_GETFD); |
| 892 | ASSERT_TRUE(flags != -1); |
| 893 | ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC); |
| 894 | |
| 895 | fclose(fp); |
| 896 | close(fd); |
| 897 | } |
| 898 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 899 | TEST(STDIO_TEST, freopen_CLOEXEC) { |
Elliott Hughes | 31165ed | 2014-09-23 17:34:29 -0700 | [diff] [blame] | 900 | FILE* fp = fopen("/proc/version", "r"); |
| 901 | ASSERT_TRUE(fp != NULL); |
| 902 | |
| 903 | // This FILE* doesn't have O_CLOEXEC... |
| 904 | int flags = fcntl(fileno(fp), F_GETFD); |
| 905 | ASSERT_TRUE(flags != -1); |
| 906 | ASSERT_EQ(0, flags & FD_CLOEXEC); |
| 907 | |
| 908 | fp = freopen("/proc/version", "re", fp); |
| 909 | |
| 910 | // ...but the new one does. |
| 911 | flags = fcntl(fileno(fp), F_GETFD); |
| 912 | ASSERT_TRUE(flags != -1); |
| 913 | ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC); |
| 914 | |
| 915 | fclose(fp); |
| 916 | } |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 917 | |
Elliott Hughes | f226ee5 | 2016-02-03 11:24:28 -0800 | [diff] [blame] | 918 | TEST(STDIO_TEST, fopen64_freopen64) { |
| 919 | FILE* fp = fopen64("/proc/version", "r"); |
| 920 | ASSERT_TRUE(fp != nullptr); |
| 921 | fp = freopen64("/proc/version", "re", fp); |
| 922 | ASSERT_TRUE(fp != nullptr); |
| 923 | fclose(fp); |
| 924 | } |
| 925 | |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 926 | // https://code.google.com/p/android/issues/detail?id=81155 |
| 927 | // http://b/18556607 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 928 | TEST(STDIO_TEST, fread_unbuffered_pathological_performance) { |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 929 | FILE* fp = fopen("/dev/zero", "r"); |
| 930 | ASSERT_TRUE(fp != NULL); |
| 931 | |
| 932 | // Make this stream unbuffered. |
| 933 | setvbuf(fp, 0, _IONBF, 0); |
| 934 | |
| 935 | char buf[65*1024]; |
| 936 | memset(buf, 0xff, sizeof(buf)); |
| 937 | |
| 938 | time_t t0 = time(NULL); |
| 939 | for (size_t i = 0; i < 1024; ++i) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 940 | ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp)); |
Elliott Hughes | 20841a1 | 2014-12-01 16:13:30 -0800 | [diff] [blame] | 941 | } |
| 942 | time_t t1 = time(NULL); |
| 943 | |
| 944 | fclose(fp); |
| 945 | |
| 946 | // 1024 64KiB reads should have been very quick. |
| 947 | ASSERT_LE(t1 - t0, 1); |
| 948 | |
| 949 | for (size_t i = 0; i < 64*1024; ++i) { |
| 950 | ASSERT_EQ('\0', buf[i]); |
| 951 | } |
| 952 | for (size_t i = 64*1024; i < 65*1024; ++i) { |
| 953 | ASSERT_EQ('\xff', buf[i]); |
| 954 | } |
| 955 | } |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 956 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 957 | TEST(STDIO_TEST, fread_EOF) { |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 958 | std::string digits("0123456789"); |
| 959 | FILE* fp = fmemopen(&digits[0], digits.size(), "r"); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 960 | |
| 961 | // Try to read too much, but little enough that it still fits in the FILE's internal buffer. |
| 962 | char buf1[4 * 4]; |
| 963 | memset(buf1, 0, sizeof(buf1)); |
| 964 | ASSERT_EQ(2U, fread(buf1, 4, 4, fp)); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 965 | ASSERT_STREQ("0123456789", buf1); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 966 | ASSERT_TRUE(feof(fp)); |
| 967 | |
| 968 | rewind(fp); |
| 969 | |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 970 | // Try to read way too much so stdio tries to read more direct from the stream. |
| 971 | char buf2[4 * 4096]; |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 972 | memset(buf2, 0, sizeof(buf2)); |
| 973 | ASSERT_EQ(2U, fread(buf2, 4, 4096, fp)); |
Elliott Hughes | 0ed7e08 | 2015-01-22 15:13:38 -0800 | [diff] [blame] | 974 | ASSERT_STREQ("0123456789", buf2); |
Elliott Hughes | 75b9938 | 2015-01-20 11:23:50 -0800 | [diff] [blame] | 975 | ASSERT_TRUE(feof(fp)); |
| 976 | |
| 977 | fclose(fp); |
| 978 | } |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 979 | |
| 980 | static void test_fread_from_write_only_stream(size_t n) { |
| 981 | FILE* fp = fopen("/dev/null", "w"); |
| 982 | std::vector<char> buf(n, 0); |
| 983 | errno = 0; |
| 984 | ASSERT_EQ(0U, fread(&buf[0], n, 1, fp)); |
| 985 | ASSERT_EQ(EBADF, errno); |
| 986 | ASSERT_TRUE(ferror(fp)); |
| 987 | ASSERT_FALSE(feof(fp)); |
| 988 | fclose(fp); |
| 989 | } |
| 990 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 991 | TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 992 | test_fread_from_write_only_stream(1); |
| 993 | } |
| 994 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 995 | TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 996 | test_fread_from_write_only_stream(64*1024); |
| 997 | } |
| 998 | |
| 999 | static void test_fwrite_after_fread(size_t n) { |
| 1000 | TemporaryFile tf; |
| 1001 | |
| 1002 | FILE* fp = fdopen(tf.fd, "w+"); |
| 1003 | ASSERT_EQ(1U, fwrite("1", 1, 1, fp)); |
| 1004 | fflush(fp); |
| 1005 | |
| 1006 | // We've flushed but not rewound, so there's nothing to read. |
| 1007 | std::vector<char> buf(n, 0); |
| 1008 | ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp)); |
| 1009 | ASSERT_TRUE(feof(fp)); |
| 1010 | |
| 1011 | // But hitting EOF doesn't prevent us from writing... |
| 1012 | errno = 0; |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1013 | ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno); |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1014 | |
| 1015 | // And if we rewind, everything's there. |
| 1016 | rewind(fp); |
| 1017 | ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp)); |
| 1018 | ASSERT_EQ('1', buf[0]); |
| 1019 | ASSERT_EQ('2', buf[1]); |
| 1020 | |
| 1021 | fclose(fp); |
| 1022 | } |
| 1023 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1024 | TEST(STDIO_TEST, fwrite_after_fread_slow_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1025 | test_fwrite_after_fread(16); |
| 1026 | } |
| 1027 | |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1028 | TEST(STDIO_TEST, fwrite_after_fread_fast_path) { |
Elliott Hughes | e6bb5a2 | 2015-01-23 17:48:15 -0800 | [diff] [blame] | 1029 | test_fwrite_after_fread(64*1024); |
| 1030 | } |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 1031 | |
| 1032 | // http://b/19172514 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1033 | TEST(STDIO_TEST, fread_after_fseek) { |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 1034 | TemporaryFile tf; |
| 1035 | |
| 1036 | FILE* fp = fopen(tf.filename, "w+"); |
| 1037 | ASSERT_TRUE(fp != nullptr); |
| 1038 | |
| 1039 | char file_data[12288]; |
| 1040 | for (size_t i = 0; i < 12288; i++) { |
| 1041 | file_data[i] = i; |
| 1042 | } |
| 1043 | ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp)); |
| 1044 | fclose(fp); |
| 1045 | |
| 1046 | fp = fopen(tf.filename, "r"); |
| 1047 | ASSERT_TRUE(fp != nullptr); |
| 1048 | |
| 1049 | char buffer[8192]; |
| 1050 | size_t cur_location = 0; |
| 1051 | // Small read to populate internal buffer. |
| 1052 | ASSERT_EQ(100U, fread(buffer, 1, 100, fp)); |
| 1053 | ASSERT_EQ(memcmp(file_data, buffer, 100), 0); |
| 1054 | |
| 1055 | cur_location = static_cast<size_t>(ftell(fp)); |
| 1056 | // Large read to force reading into the user supplied buffer and bypassing |
| 1057 | // the internal buffer. |
| 1058 | ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp)); |
| 1059 | ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0); |
| 1060 | |
| 1061 | // Small backwards seek to verify fseek does not reuse the internal buffer. |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1062 | ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno); |
Christopher Ferris | cc9ca10 | 2015-02-27 18:22:45 -0800 | [diff] [blame] | 1063 | cur_location = static_cast<size_t>(ftell(fp)); |
| 1064 | ASSERT_EQ(22U, fread(buffer, 1, 22, fp)); |
| 1065 | ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0); |
| 1066 | |
| 1067 | fclose(fp); |
| 1068 | } |
Elliott Hughes | 8ab433d | 2015-10-09 17:57:26 -0700 | [diff] [blame] | 1069 | |
| 1070 | // https://code.google.com/p/android/issues/detail?id=184847 |
Christopher Ferris | 13f26a7 | 2016-01-13 13:47:58 -0800 | [diff] [blame] | 1071 | TEST(STDIO_TEST, fread_EOF_184847) { |
Elliott Hughes | 8ab433d | 2015-10-09 17:57:26 -0700 | [diff] [blame] | 1072 | TemporaryFile tf; |
| 1073 | char buf[6] = {0}; |
| 1074 | |
| 1075 | FILE* fw = fopen(tf.filename, "w"); |
| 1076 | ASSERT_TRUE(fw != nullptr); |
| 1077 | |
| 1078 | FILE* fr = fopen(tf.filename, "r"); |
| 1079 | ASSERT_TRUE(fr != nullptr); |
| 1080 | |
| 1081 | fwrite("a", 1, 1, fw); |
| 1082 | fflush(fw); |
| 1083 | ASSERT_EQ(1U, fread(buf, 1, 1, fr)); |
| 1084 | ASSERT_STREQ("a", buf); |
| 1085 | |
| 1086 | // 'fr' is now at EOF. |
| 1087 | ASSERT_EQ(0U, fread(buf, 1, 1, fr)); |
| 1088 | ASSERT_TRUE(feof(fr)); |
| 1089 | |
| 1090 | // Write some more... |
| 1091 | fwrite("z", 1, 1, fw); |
| 1092 | fflush(fw); |
| 1093 | |
| 1094 | // ...and check that we can read it back. |
| 1095 | // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.) |
| 1096 | ASSERT_EQ(1U, fread(buf, 1, 1, fr)); |
| 1097 | ASSERT_STREQ("z", buf); |
| 1098 | |
| 1099 | // But now we're done. |
| 1100 | ASSERT_EQ(0U, fread(buf, 1, 1, fr)); |
| 1101 | |
| 1102 | fclose(fr); |
| 1103 | fclose(fw); |
| 1104 | } |
Elliott Hughes | 923f165 | 2016-01-19 15:46:05 -0800 | [diff] [blame] | 1105 | |
| 1106 | TEST(STDIO_TEST, fclose_invalidates_fd) { |
| 1107 | // The typical error we're trying to help people catch involves accessing |
| 1108 | // memory after it's been freed. But we know that stdin/stdout/stderr are |
| 1109 | // special and don't get deallocated, so this test uses stdin. |
| 1110 | ASSERT_EQ(0, fclose(stdin)); |
| 1111 | |
| 1112 | // Even though using a FILE* after close is undefined behavior, I've closed |
| 1113 | // this bug as "WAI" too many times. We shouldn't hand out stale fds, |
| 1114 | // especially because they might actually correspond to a real stream. |
| 1115 | errno = 0; |
| 1116 | ASSERT_EQ(-1, fileno(stdin)); |
| 1117 | ASSERT_EQ(EBADF, errno); |
| 1118 | } |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 1119 | |
| 1120 | TEST(STDIO_TEST, fseek_ftell_unseekable) { |
| 1121 | #if defined(__BIONIC__) // glibc has fopencookie instead. |
| 1122 | auto read_fn = [](void*, char*, int) { return -1; }; |
| 1123 | FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr); |
| 1124 | ASSERT_TRUE(fp != nullptr); |
| 1125 | |
| 1126 | // Check that ftell balks on an unseekable FILE*. |
| 1127 | errno = 0; |
| 1128 | ASSERT_EQ(-1, ftell(fp)); |
| 1129 | ASSERT_EQ(ESPIPE, errno); |
| 1130 | |
| 1131 | // SEEK_CUR is rewritten as SEEK_SET internally... |
| 1132 | errno = 0; |
| 1133 | ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR)); |
| 1134 | ASSERT_EQ(ESPIPE, errno); |
| 1135 | |
| 1136 | // ...so it's worth testing the direct seek path too. |
| 1137 | errno = 0; |
| 1138 | ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET)); |
| 1139 | ASSERT_EQ(ESPIPE, errno); |
| 1140 | |
| 1141 | fclose(fp); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 1142 | #else |
| 1143 | GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; |
| 1144 | #endif |
| 1145 | } |
| 1146 | |
| 1147 | TEST(STDIO_TEST, funopen_EINVAL) { |
| 1148 | #if defined(__BIONIC__) |
| 1149 | errno = 0; |
| 1150 | ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr)); |
| 1151 | ASSERT_EQ(EINVAL, errno); |
| 1152 | #else |
| 1153 | GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; |
| 1154 | #endif |
| 1155 | } |
| 1156 | |
| 1157 | TEST(STDIO_TEST, funopen_seek) { |
| 1158 | #if defined(__BIONIC__) |
| 1159 | auto read_fn = [](void*, char*, int) { return -1; }; |
| 1160 | |
| 1161 | auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; }; |
| 1162 | auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; }; |
| 1163 | |
| 1164 | FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr); |
| 1165 | ASSERT_TRUE(fp != nullptr); |
| 1166 | fpos_t pos; |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 1167 | #if defined(__LP64__) |
| 1168 | EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno); |
| 1169 | EXPECT_EQ(0xfedcba12LL, pos); |
| 1170 | #else |
| 1171 | EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno); |
| 1172 | EXPECT_EQ(EOVERFLOW, errno); |
| 1173 | #endif |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 1174 | |
| 1175 | FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr); |
| 1176 | ASSERT_TRUE(fp64 != nullptr); |
| 1177 | fpos64_t pos64; |
Elliott Hughes | 955426e | 2016-01-26 18:25:52 -0800 | [diff] [blame] | 1178 | EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno); |
| 1179 | EXPECT_EQ(0xfedcba12345678, pos64); |
Elliott Hughes | 03e65eb | 2016-01-26 14:13:04 -0800 | [diff] [blame] | 1180 | #else |
| 1181 | GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n"; |
Elliott Hughes | 2704bd1 | 2016-01-20 17:14:53 -0800 | [diff] [blame] | 1182 | #endif |
| 1183 | } |
Elliott Hughes | 71288cb | 2016-01-22 19:22:44 -0800 | [diff] [blame] | 1184 | |
| 1185 | TEST(STDIO_TEST, lots_of_concurrent_files) { |
| 1186 | std::vector<TemporaryFile*> tfs; |
| 1187 | std::vector<FILE*> fps; |
| 1188 | |
| 1189 | for (size_t i = 0; i < 256; ++i) { |
| 1190 | TemporaryFile* tf = new TemporaryFile; |
| 1191 | tfs.push_back(tf); |
| 1192 | FILE* fp = fopen(tf->filename, "w+"); |
| 1193 | fps.push_back(fp); |
| 1194 | fprintf(fp, "hello %zu!\n", i); |
| 1195 | fflush(fp); |
| 1196 | } |
| 1197 | |
| 1198 | for (size_t i = 0; i < 256; ++i) { |
| 1199 | rewind(fps[i]); |
| 1200 | |
| 1201 | char buf[BUFSIZ]; |
| 1202 | ASSERT_TRUE(fgets(buf, sizeof(buf), fps[i]) != nullptr); |
| 1203 | |
| 1204 | char expected[BUFSIZ]; |
| 1205 | snprintf(expected, sizeof(expected), "hello %zu!\n", i); |
| 1206 | ASSERT_STREQ(expected, buf); |
| 1207 | |
| 1208 | fclose(fps[i]); |
| 1209 | delete tfs[i]; |
| 1210 | } |
| 1211 | } |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1212 | |
| 1213 | static void AssertFileOffsetAt(FILE* fp, off64_t offset) { |
| 1214 | EXPECT_EQ(offset, ftell(fp)); |
| 1215 | EXPECT_EQ(offset, ftello(fp)); |
Elliott Hughes | e4fa6e9 | 2016-02-02 22:39:15 -0800 | [diff] [blame] | 1216 | EXPECT_EQ(offset, ftello64(fp)); |
Elliott Hughes | 9677fab | 2016-01-25 15:50:59 -0800 | [diff] [blame] | 1217 | fpos_t pos; |
| 1218 | fpos64_t pos64; |
| 1219 | EXPECT_EQ(0, fgetpos(fp, &pos)); |
| 1220 | EXPECT_EQ(0, fgetpos64(fp, &pos64)); |
| 1221 | #if defined(__BIONIC__) |
| 1222 | EXPECT_EQ(offset, static_cast<off64_t>(pos)); |
| 1223 | EXPECT_EQ(offset, static_cast<off64_t>(pos64)); |
| 1224 | #else |
| 1225 | GTEST_LOG_(INFO) << "glibc's fpos_t is opaque.\n"; |
| 1226 | #endif |
| 1227 | } |
| 1228 | |
| 1229 | TEST(STDIO_TEST, seek_tell_family_smoke) { |
| 1230 | TemporaryFile tf; |
| 1231 | FILE* fp = fdopen(tf.fd, "w+"); |
| 1232 | |
| 1233 | // Initially we should be at 0. |
| 1234 | AssertFileOffsetAt(fp, 0); |
| 1235 | |
| 1236 | // Seek to offset 8192. |
| 1237 | ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET)); |
| 1238 | AssertFileOffsetAt(fp, 8192); |
| 1239 | fpos_t eight_k_pos; |
| 1240 | ASSERT_EQ(0, fgetpos(fp, &eight_k_pos)); |
| 1241 | |
| 1242 | // Seek forward another 8192... |
| 1243 | ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR)); |
| 1244 | AssertFileOffsetAt(fp, 8192 + 8192); |
| 1245 | fpos64_t sixteen_k_pos64; |
| 1246 | ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64)); |
| 1247 | |
| 1248 | // Seek back 8192... |
| 1249 | ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR)); |
| 1250 | AssertFileOffsetAt(fp, 8192); |
| 1251 | |
| 1252 | // Since we haven't written anything, the end is also at 0. |
| 1253 | ASSERT_EQ(0, fseek(fp, 0, SEEK_END)); |
| 1254 | AssertFileOffsetAt(fp, 0); |
| 1255 | |
| 1256 | // Check that our fpos64_t from 16KiB works... |
| 1257 | ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64)); |
| 1258 | AssertFileOffsetAt(fp, 8192 + 8192); |
| 1259 | // ...as does our fpos_t from 8192. |
| 1260 | ASSERT_EQ(0, fsetpos(fp, &eight_k_pos)); |
| 1261 | AssertFileOffsetAt(fp, 8192); |
| 1262 | |
| 1263 | // Do fseeko and fseeko64 work too? |
| 1264 | ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET)); |
| 1265 | AssertFileOffsetAt(fp, 1234); |
| 1266 | ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET)); |
| 1267 | AssertFileOffsetAt(fp, 5678); |
| 1268 | |
| 1269 | fclose(fp); |
| 1270 | } |
| 1271 | |
| 1272 | TEST(STDIO_TEST, fseek_fseeko_EINVAL) { |
| 1273 | TemporaryFile tf; |
| 1274 | FILE* fp = fdopen(tf.fd, "w+"); |
| 1275 | |
| 1276 | // Bad whence. |
| 1277 | errno = 0; |
| 1278 | ASSERT_EQ(-1, fseek(fp, 0, 123)); |
| 1279 | ASSERT_EQ(EINVAL, errno); |
| 1280 | errno = 0; |
| 1281 | ASSERT_EQ(-1, fseeko(fp, 0, 123)); |
| 1282 | ASSERT_EQ(EINVAL, errno); |
| 1283 | errno = 0; |
| 1284 | ASSERT_EQ(-1, fseeko64(fp, 0, 123)); |
| 1285 | ASSERT_EQ(EINVAL, errno); |
| 1286 | |
| 1287 | // Bad offset. |
| 1288 | errno = 0; |
| 1289 | ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET)); |
| 1290 | ASSERT_EQ(EINVAL, errno); |
| 1291 | errno = 0; |
| 1292 | ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET)); |
| 1293 | ASSERT_EQ(EINVAL, errno); |
| 1294 | errno = 0; |
| 1295 | ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET)); |
| 1296 | ASSERT_EQ(EINVAL, errno); |
| 1297 | |
| 1298 | fclose(fp); |
| 1299 | } |
Elliott Hughes | 20788ae | 2016-06-09 15:16:32 -0700 | [diff] [blame] | 1300 | |
| 1301 | TEST(STDIO_TEST, ctermid) { |
| 1302 | ASSERT_STREQ("/dev/tty", ctermid(nullptr)); |
| 1303 | |
| 1304 | char buf[L_ctermid] = {}; |
| 1305 | ASSERT_EQ(buf, ctermid(buf)); |
| 1306 | ASSERT_STREQ("/dev/tty", buf); |
| 1307 | } |