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