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 | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 20 | #include <limits.h> |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | TEST(stdio, tmpfile_fileno_fprintf_rewind_fgets) { |
| 27 | FILE* fp = tmpfile(); |
| 28 | ASSERT_TRUE(fp != NULL); |
| 29 | |
| 30 | int fd = fileno(fp); |
| 31 | ASSERT_NE(fd, -1); |
| 32 | |
| 33 | struct stat sb; |
| 34 | int rc = fstat(fd, &sb); |
| 35 | ASSERT_NE(rc, -1); |
| 36 | ASSERT_EQ(sb.st_mode & 0777, 0600U); |
| 37 | |
| 38 | rc = fprintf(fp, "hello\n"); |
| 39 | ASSERT_EQ(rc, 6); |
| 40 | |
| 41 | rewind(fp); |
| 42 | |
| 43 | char buf[16]; |
| 44 | char* s = fgets(buf, sizeof(buf), fp); |
| 45 | ASSERT_TRUE(s != NULL); |
| 46 | ASSERT_STREQ("hello\n", s); |
| 47 | |
| 48 | fclose(fp); |
| 49 | } |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 50 | |
| 51 | TEST(stdio, getdelim) { |
| 52 | FILE* fp = tmpfile(); |
| 53 | ASSERT_TRUE(fp != NULL); |
| 54 | |
| 55 | const char* line_written = "This is a test"; |
| 56 | int rc = fprintf(fp, "%s", line_written); |
| 57 | ASSERT_EQ(rc, static_cast<int>(strlen(line_written))); |
| 58 | |
| 59 | rewind(fp); |
| 60 | |
| 61 | char* word_read = NULL; |
| 62 | size_t allocated_length = 0; |
| 63 | |
| 64 | const char* expected[] = { "This ", " ", "is ", "a ", "test" }; |
| 65 | for (size_t i = 0; i < 5; ++i) { |
| 66 | ASSERT_FALSE(feof(fp)); |
| 67 | ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i]))); |
| 68 | ASSERT_GE(allocated_length, strlen(expected[i])); |
| 69 | ASSERT_STREQ(word_read, expected[i]); |
| 70 | } |
| 71 | // The last read should have set the end-of-file indicator for the stream. |
| 72 | ASSERT_TRUE(feof(fp)); |
| 73 | clearerr(fp); |
| 74 | |
| 75 | // getdelim returns -1 but doesn't set errno if we're already at EOF. |
| 76 | // It should set the end-of-file indicator for the stream, though. |
| 77 | errno = 0; |
| 78 | ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 79 | ASSERT_EQ(0, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 80 | ASSERT_TRUE(feof(fp)); |
| 81 | |
| 82 | free(word_read); |
| 83 | fclose(fp); |
| 84 | } |
| 85 | |
| 86 | TEST(stdio, getdelim_invalid) { |
| 87 | FILE* fp = tmpfile(); |
| 88 | |
| 89 | char* buffer = NULL; |
| 90 | size_t buffer_length = 0; |
| 91 | |
| 92 | // The first argument can't be NULL. |
| 93 | errno = 0; |
| 94 | ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 95 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 96 | |
| 97 | // The second argument can't be NULL. |
| 98 | errno = 0; |
| 99 | ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 100 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 101 | |
| 102 | // The stream can't be closed. |
| 103 | fclose(fp); |
| 104 | errno = 0; |
| 105 | ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 106 | ASSERT_EQ(EBADF, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST(stdio, getline) { |
| 110 | FILE* fp = tmpfile(); |
| 111 | ASSERT_TRUE(fp != NULL); |
| 112 | |
| 113 | const char* line_written = "This is a test for getline\n"; |
| 114 | const size_t line_count = 5; |
| 115 | |
| 116 | for (size_t i = 0; i < line_count; ++i) { |
| 117 | int rc = fprintf(fp, "%s", line_written); |
| 118 | ASSERT_EQ(rc, static_cast<int>(strlen(line_written))); |
| 119 | } |
| 120 | |
| 121 | rewind(fp); |
| 122 | |
| 123 | char* line_read = NULL; |
| 124 | size_t allocated_length = 0; |
| 125 | |
| 126 | size_t read_line_count = 0; |
| 127 | ssize_t read_char_count; |
| 128 | while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) { |
| 129 | ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written))); |
| 130 | ASSERT_GE(allocated_length, strlen(line_written)); |
| 131 | ASSERT_STREQ(line_read, line_written); |
| 132 | ++read_line_count; |
| 133 | } |
| 134 | ASSERT_EQ(read_line_count, line_count); |
| 135 | |
| 136 | // The last read should have set the end-of-file indicator for the stream. |
| 137 | ASSERT_TRUE(feof(fp)); |
| 138 | clearerr(fp); |
| 139 | |
| 140 | // getline returns -1 but doesn't set errno if we're already at EOF. |
| 141 | // It should set the end-of-file indicator for the stream, though. |
| 142 | errno = 0; |
| 143 | ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 144 | ASSERT_EQ(0, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 145 | ASSERT_TRUE(feof(fp)); |
| 146 | |
| 147 | free(line_read); |
| 148 | fclose(fp); |
| 149 | } |
| 150 | |
| 151 | TEST(stdio, getline_invalid) { |
| 152 | FILE* fp = tmpfile(); |
| 153 | |
| 154 | char* buffer = NULL; |
| 155 | size_t buffer_length = 0; |
| 156 | |
| 157 | // The first argument can't be NULL. |
| 158 | errno = 0; |
| 159 | ASSERT_EQ(getline(NULL, &buffer_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 160 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 161 | |
| 162 | // The second argument can't be NULL. |
| 163 | errno = 0; |
| 164 | ASSERT_EQ(getline(&buffer, NULL, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 165 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 166 | |
| 167 | // The stream can't be closed. |
| 168 | fclose(fp); |
| 169 | errno = 0; |
| 170 | ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 171 | ASSERT_EQ(EBADF, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 172 | } |
Thorsten Glaser | c641caf | 2013-02-17 16:50:58 +0000 | [diff] [blame] | 173 | |
| 174 | TEST(stdio, printf_ssize_t) { |
Elliott Hughes | e255642 | 2013-02-28 10:51:14 -0800 | [diff] [blame] | 175 | // http://b/8253769 |
Elliott Hughes | e255642 | 2013-02-28 10:51:14 -0800 | [diff] [blame] | 176 | ASSERT_EQ(sizeof(ssize_t), sizeof(long int)); |
Elliott Hughes | b6e2248 | 2013-03-08 15:28:52 -0800 | [diff] [blame] | 177 | ASSERT_EQ(sizeof(ssize_t), sizeof(size_t)); |
| 178 | // 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] | 179 | // error: format '%zd' expects argument of type 'signed size_t', |
| 180 | // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format] |
| 181 | ssize_t v = 1; |
| 182 | char buf[32]; |
| 183 | snprintf(buf, sizeof(buf), "%zd", v); |
| 184 | } |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 185 | |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 186 | #if !defined(__GLIBC__) |
| 187 | TEST(stdio, snprintf_n_format_specifier_not_implemented) { |
| 188 | char buf[32]; |
| 189 | int i = 0; |
| 190 | // We deliberately don't implement %n, so it's treated like |
| 191 | // any other unrecognized format specifier. |
| 192 | EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i)); |
| 193 | EXPECT_EQ(0, i); |
| 194 | EXPECT_STREQ("a n b", buf); |
| 195 | } |
| 196 | #endif |
| 197 | |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 198 | TEST(stdio, snprintf_smoke) { |
| 199 | char buf[BUFSIZ]; |
| 200 | |
| 201 | snprintf(buf, sizeof(buf), "a"); |
| 202 | EXPECT_STREQ("a", buf); |
| 203 | |
| 204 | snprintf(buf, sizeof(buf), "%%"); |
| 205 | EXPECT_STREQ("%", buf); |
| 206 | |
| 207 | snprintf(buf, sizeof(buf), "01234"); |
| 208 | EXPECT_STREQ("01234", buf); |
| 209 | |
| 210 | snprintf(buf, sizeof(buf), "a%sb", "01234"); |
| 211 | EXPECT_STREQ("a01234b", buf); |
| 212 | |
| 213 | char* s = NULL; |
| 214 | snprintf(buf, sizeof(buf), "a%sb", s); |
| 215 | EXPECT_STREQ("a(null)b", buf); |
| 216 | |
| 217 | snprintf(buf, sizeof(buf), "aa%scc", "bb"); |
| 218 | EXPECT_STREQ("aabbcc", buf); |
| 219 | |
| 220 | snprintf(buf, sizeof(buf), "a%cc", 'b'); |
| 221 | EXPECT_STREQ("abc", buf); |
| 222 | |
| 223 | snprintf(buf, sizeof(buf), "a%db", 1234); |
| 224 | EXPECT_STREQ("a1234b", buf); |
| 225 | |
| 226 | snprintf(buf, sizeof(buf), "a%db", -8123); |
| 227 | EXPECT_STREQ("a-8123b", buf); |
| 228 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame^] | 229 | snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010)); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 230 | EXPECT_STREQ("a16b", buf); |
| 231 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame^] | 232 | snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10)); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 233 | EXPECT_STREQ("a16b", buf); |
| 234 | |
| 235 | snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL); |
| 236 | EXPECT_STREQ("a68719476736b", buf); |
| 237 | |
| 238 | snprintf(buf, sizeof(buf), "a%ldb", 70000L); |
| 239 | EXPECT_STREQ("a70000b", buf); |
| 240 | |
| 241 | snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234)); |
| 242 | EXPECT_STREQ("a0xb0001234b", buf); |
| 243 | |
| 244 | snprintf(buf, sizeof(buf), "a%xz", 0x12ab); |
| 245 | EXPECT_STREQ("a12abz", buf); |
| 246 | |
| 247 | snprintf(buf, sizeof(buf), "a%Xz", 0x12ab); |
| 248 | EXPECT_STREQ("a12ABz", buf); |
| 249 | |
| 250 | snprintf(buf, sizeof(buf), "a%08xz", 0x123456); |
| 251 | EXPECT_STREQ("a00123456z", buf); |
| 252 | |
| 253 | snprintf(buf, sizeof(buf), "a%5dz", 1234); |
| 254 | EXPECT_STREQ("a 1234z", buf); |
| 255 | |
| 256 | snprintf(buf, sizeof(buf), "a%05dz", 1234); |
| 257 | EXPECT_STREQ("a01234z", buf); |
| 258 | |
| 259 | snprintf(buf, sizeof(buf), "a%8dz", 1234); |
| 260 | EXPECT_STREQ("a 1234z", buf); |
| 261 | |
| 262 | snprintf(buf, sizeof(buf), "a%-8dz", 1234); |
| 263 | EXPECT_STREQ("a1234 z", buf); |
| 264 | |
| 265 | snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef"); |
| 266 | EXPECT_STREQ("Aabcdef Z", buf); |
| 267 | |
| 268 | snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234); |
| 269 | EXPECT_STREQ("Ahello:1234Z", buf); |
| 270 | |
| 271 | snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5); |
| 272 | EXPECT_STREQ("a005:5:05z", buf); |
| 273 | |
| 274 | void* p = NULL; |
| 275 | snprintf(buf, sizeof(buf), "a%d,%pz", 5, p); |
| 276 | EXPECT_STREQ("a5,0x0z", buf); |
| 277 | |
| 278 | snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8); |
| 279 | EXPECT_STREQ("a68719476736,6,7,8z", buf); |
| 280 | |
| 281 | snprintf(buf, sizeof(buf), "a_%f_b", 1.23f); |
| 282 | EXPECT_STREQ("a_1.230000_b", buf); |
| 283 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame^] | 284 | snprintf(buf, sizeof(buf), "a_%g_b", 3.14); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 285 | EXPECT_STREQ("a_3.14_b", buf); |
| 286 | } |
| 287 | |
| 288 | TEST(stdio, snprintf_d_INT_MAX) { |
| 289 | char buf[BUFSIZ]; |
| 290 | snprintf(buf, sizeof(buf), "%d", INT_MAX); |
| 291 | EXPECT_STREQ("2147483647", buf); |
| 292 | } |
| 293 | |
| 294 | TEST(stdio, snprintf_d_INT_MIN) { |
| 295 | char buf[BUFSIZ]; |
| 296 | snprintf(buf, sizeof(buf), "%d", INT_MIN); |
| 297 | EXPECT_STREQ("-2147483648", buf); |
| 298 | } |
| 299 | |
| 300 | TEST(stdio, snprintf_ld_LONG_MAX) { |
| 301 | char buf[BUFSIZ]; |
| 302 | snprintf(buf, sizeof(buf), "%ld", LONG_MAX); |
| 303 | EXPECT_STREQ("2147483647", buf); |
| 304 | } |
| 305 | |
| 306 | TEST(stdio, snprintf_ld_LONG_MIN) { |
| 307 | char buf[BUFSIZ]; |
| 308 | snprintf(buf, sizeof(buf), "%ld", LONG_MIN); |
| 309 | EXPECT_STREQ("-2147483648", buf); |
| 310 | } |
| 311 | |
| 312 | TEST(stdio, snprintf_lld_LLONG_MAX) { |
| 313 | char buf[BUFSIZ]; |
| 314 | snprintf(buf, sizeof(buf), "%lld", LLONG_MAX); |
| 315 | EXPECT_STREQ("9223372036854775807", buf); |
| 316 | } |
| 317 | |
| 318 | TEST(stdio, snprintf_lld_LLONG_MIN) { |
| 319 | char buf[BUFSIZ]; |
| 320 | snprintf(buf, sizeof(buf), "%lld", LLONG_MIN); |
| 321 | EXPECT_STREQ("-9223372036854775808", buf); |
| 322 | } |
| 323 | |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 324 | TEST(stdio, popen) { |
| 325 | FILE* fp = popen("cat /proc/version", "r"); |
| 326 | ASSERT_TRUE(fp != NULL); |
| 327 | |
| 328 | char buf[16]; |
| 329 | char* s = fgets(buf, sizeof(buf), fp); |
| 330 | buf[13] = '\0'; |
| 331 | ASSERT_STREQ("Linux version", s); |
| 332 | |
| 333 | ASSERT_EQ(0, pclose(fp)); |
| 334 | } |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 335 | |
| 336 | TEST(stdio, getc) { |
| 337 | FILE* fp = fopen("/proc/version", "r"); |
| 338 | ASSERT_TRUE(fp != NULL); |
| 339 | ASSERT_EQ('L', getc(fp)); |
| 340 | ASSERT_EQ('i', getc(fp)); |
| 341 | ASSERT_EQ('n', getc(fp)); |
| 342 | ASSERT_EQ('u', getc(fp)); |
| 343 | ASSERT_EQ('x', getc(fp)); |
| 344 | fclose(fp); |
| 345 | } |
| 346 | |
| 347 | TEST(stdio, putc) { |
| 348 | FILE* fp = fopen("/proc/version", "r"); |
| 349 | ASSERT_TRUE(fp != NULL); |
| 350 | ASSERT_EQ(EOF, putc('x', fp)); |
| 351 | fclose(fp); |
| 352 | } |