stdio_test.cpp revision 13613137bc4266656bffce464e525eb9ae6371f0
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> 20#include <limits.h> 21#include <stdio.h> 22#include <sys/types.h> 23#include <sys/stat.h> 24#include <unistd.h> 25 26TEST(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} 50 51TEST(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); 79 ASSERT_EQ(0, errno); 80 ASSERT_TRUE(feof(fp)); 81 82 free(word_read); 83 fclose(fp); 84} 85 86TEST(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); 95 ASSERT_EQ(EINVAL, errno); 96 97 // The second argument can't be NULL. 98 errno = 0; 99 ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1); 100 ASSERT_EQ(EINVAL, errno); 101 102 // The stream can't be closed. 103 fclose(fp); 104 errno = 0; 105 ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1); 106 ASSERT_EQ(EBADF, errno); 107} 108 109TEST(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); 144 ASSERT_EQ(0, errno); 145 ASSERT_TRUE(feof(fp)); 146 147 free(line_read); 148 fclose(fp); 149} 150 151TEST(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); 160 ASSERT_EQ(EINVAL, errno); 161 162 // The second argument can't be NULL. 163 errno = 0; 164 ASSERT_EQ(getline(&buffer, NULL, fp), -1); 165 ASSERT_EQ(EINVAL, errno); 166 167 // The stream can't be closed. 168 fclose(fp); 169 errno = 0; 170 ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1); 171 ASSERT_EQ(EBADF, errno); 172} 173 174TEST(stdio, printf_ssize_t) { 175 // http://b/8253769 176 ASSERT_EQ(sizeof(ssize_t), sizeof(long int)); 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: 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} 185 186#if !defined(__GLIBC__) 187TEST(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 198TEST(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 229 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010)); 230 EXPECT_STREQ("a16b", buf); 231 232 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10)); 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#if defined(__BIONIC__) 277 EXPECT_STREQ("a5,0x0z", buf); 278#else 279 EXPECT_STREQ("a5,(nil)z", buf); 280#endif 281 282 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8); 283 EXPECT_STREQ("a68719476736,6,7,8z", buf); 284 285 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f); 286 EXPECT_STREQ("a_1.230000_b", buf); 287 288 snprintf(buf, sizeof(buf), "a_%g_b", 3.14); 289 EXPECT_STREQ("a_3.14_b", buf); 290} 291 292TEST(stdio, snprintf_d_INT_MAX) { 293 char buf[BUFSIZ]; 294 snprintf(buf, sizeof(buf), "%d", INT_MAX); 295 EXPECT_STREQ("2147483647", buf); 296} 297 298TEST(stdio, snprintf_d_INT_MIN) { 299 char buf[BUFSIZ]; 300 snprintf(buf, sizeof(buf), "%d", INT_MIN); 301 EXPECT_STREQ("-2147483648", buf); 302} 303 304TEST(stdio, snprintf_ld_LONG_MAX) { 305 char buf[BUFSIZ]; 306 snprintf(buf, sizeof(buf), "%ld", LONG_MAX); 307#if __LP64__ 308 EXPECT_STREQ("9223372036854775807", buf); 309#else 310 EXPECT_STREQ("2147483647", buf); 311#endif 312} 313 314TEST(stdio, snprintf_ld_LONG_MIN) { 315 char buf[BUFSIZ]; 316 snprintf(buf, sizeof(buf), "%ld", LONG_MIN); 317#if __LP64__ 318 EXPECT_STREQ("-9223372036854775808", buf); 319#else 320 EXPECT_STREQ("-2147483648", buf); 321#endif 322} 323 324TEST(stdio, snprintf_lld_LLONG_MAX) { 325 char buf[BUFSIZ]; 326 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX); 327 EXPECT_STREQ("9223372036854775807", buf); 328} 329 330TEST(stdio, snprintf_lld_LLONG_MIN) { 331 char buf[BUFSIZ]; 332 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN); 333 EXPECT_STREQ("-9223372036854775808", buf); 334} 335 336TEST(stdio, popen) { 337 FILE* fp = popen("cat /proc/version", "r"); 338 ASSERT_TRUE(fp != NULL); 339 340 char buf[16]; 341 char* s = fgets(buf, sizeof(buf), fp); 342 buf[13] = '\0'; 343 ASSERT_STREQ("Linux version", s); 344 345 ASSERT_EQ(0, pclose(fp)); 346} 347 348TEST(stdio, getc) { 349 FILE* fp = fopen("/proc/version", "r"); 350 ASSERT_TRUE(fp != NULL); 351 ASSERT_EQ('L', getc(fp)); 352 ASSERT_EQ('i', getc(fp)); 353 ASSERT_EQ('n', getc(fp)); 354 ASSERT_EQ('u', getc(fp)); 355 ASSERT_EQ('x', getc(fp)); 356 fclose(fp); 357} 358 359TEST(stdio, putc) { 360 FILE* fp = fopen("/proc/version", "r"); 361 ASSERT_TRUE(fp != NULL); 362 ASSERT_EQ(EOF, putc('x', fp)); 363 fclose(fp); 364} 365