blob: 2002928fe7f2c1e9ecd311dba85dae76a94d307c [file] [log] [blame]
Elliott Hughes91875dc2012-09-24 17:55:15 -07001/*
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 Hughes1d13c642013-09-23 16:02:39 -070020#include <limits.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070021#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}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +030050
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);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080079 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +030080 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);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080095 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +030096
97 // The second argument can't be NULL.
98 errno = 0;
99 ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800100 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300101
102 // The stream can't be closed.
103 fclose(fp);
104 errno = 0;
105 ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
Christopher Ferriscbd85b92013-11-15 15:16:01 -0800106 // glibc sometimes doesn't set errno in this particular case.
107#if defined(__BIONIC__)
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800108 ASSERT_EQ(EBADF, errno);
Christopher Ferriscbd85b92013-11-15 15:16:01 -0800109#endif
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300110}
111
112TEST(stdio, getline) {
113 FILE* fp = tmpfile();
114 ASSERT_TRUE(fp != NULL);
115
116 const char* line_written = "This is a test for getline\n";
117 const size_t line_count = 5;
118
119 for (size_t i = 0; i < line_count; ++i) {
120 int rc = fprintf(fp, "%s", line_written);
121 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
122 }
123
124 rewind(fp);
125
126 char* line_read = NULL;
127 size_t allocated_length = 0;
128
129 size_t read_line_count = 0;
130 ssize_t read_char_count;
131 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
132 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
133 ASSERT_GE(allocated_length, strlen(line_written));
134 ASSERT_STREQ(line_read, line_written);
135 ++read_line_count;
136 }
137 ASSERT_EQ(read_line_count, line_count);
138
139 // The last read should have set the end-of-file indicator for the stream.
140 ASSERT_TRUE(feof(fp));
141 clearerr(fp);
142
143 // getline returns -1 but doesn't set errno if we're already at EOF.
144 // It should set the end-of-file indicator for the stream, though.
145 errno = 0;
146 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800147 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300148 ASSERT_TRUE(feof(fp));
149
150 free(line_read);
151 fclose(fp);
152}
153
154TEST(stdio, getline_invalid) {
155 FILE* fp = tmpfile();
156
157 char* buffer = NULL;
158 size_t buffer_length = 0;
159
160 // The first argument can't be NULL.
161 errno = 0;
162 ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800163 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300164
165 // The second argument can't be NULL.
166 errno = 0;
167 ASSERT_EQ(getline(&buffer, NULL, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800168 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300169
170 // The stream can't be closed.
171 fclose(fp);
172 errno = 0;
173 ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
Christopher Ferriscbd85b92013-11-15 15:16:01 -0800174 // glibc sometimes doesn't set errno in this particular case.
175#if defined(__BIONIC__)
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800176 ASSERT_EQ(EBADF, errno);
Christopher Ferriscbd85b92013-11-15 15:16:01 -0800177#endif
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300178}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000179
180TEST(stdio, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800181 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800182 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800183 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
184 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000185 // error: format '%zd' expects argument of type 'signed size_t',
186 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
187 ssize_t v = 1;
188 char buf[32];
189 snprintf(buf, sizeof(buf), "%zd", v);
190}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800191
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700192#if !defined(__GLIBC__)
193TEST(stdio, snprintf_n_format_specifier_not_implemented) {
194 char buf[32];
195 int i = 0;
196 // We deliberately don't implement %n, so it's treated like
197 // any other unrecognized format specifier.
198 EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i));
199 EXPECT_EQ(0, i);
200 EXPECT_STREQ("a n b", buf);
201}
202#endif
203
Elliott Hughes1d13c642013-09-23 16:02:39 -0700204TEST(stdio, snprintf_smoke) {
205 char buf[BUFSIZ];
206
207 snprintf(buf, sizeof(buf), "a");
208 EXPECT_STREQ("a", buf);
209
210 snprintf(buf, sizeof(buf), "%%");
211 EXPECT_STREQ("%", buf);
212
213 snprintf(buf, sizeof(buf), "01234");
214 EXPECT_STREQ("01234", buf);
215
216 snprintf(buf, sizeof(buf), "a%sb", "01234");
217 EXPECT_STREQ("a01234b", buf);
218
219 char* s = NULL;
220 snprintf(buf, sizeof(buf), "a%sb", s);
221 EXPECT_STREQ("a(null)b", buf);
222
223 snprintf(buf, sizeof(buf), "aa%scc", "bb");
224 EXPECT_STREQ("aabbcc", buf);
225
226 snprintf(buf, sizeof(buf), "a%cc", 'b');
227 EXPECT_STREQ("abc", buf);
228
229 snprintf(buf, sizeof(buf), "a%db", 1234);
230 EXPECT_STREQ("a1234b", buf);
231
232 snprintf(buf, sizeof(buf), "a%db", -8123);
233 EXPECT_STREQ("a-8123b", buf);
234
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700235 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700236 EXPECT_STREQ("a16b", buf);
237
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700238 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700239 EXPECT_STREQ("a16b", buf);
240
241 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
242 EXPECT_STREQ("a68719476736b", buf);
243
244 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
245 EXPECT_STREQ("a70000b", buf);
246
247 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
248 EXPECT_STREQ("a0xb0001234b", buf);
249
250 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
251 EXPECT_STREQ("a12abz", buf);
252
253 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
254 EXPECT_STREQ("a12ABz", buf);
255
256 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
257 EXPECT_STREQ("a00123456z", buf);
258
259 snprintf(buf, sizeof(buf), "a%5dz", 1234);
260 EXPECT_STREQ("a 1234z", buf);
261
262 snprintf(buf, sizeof(buf), "a%05dz", 1234);
263 EXPECT_STREQ("a01234z", buf);
264
265 snprintf(buf, sizeof(buf), "a%8dz", 1234);
266 EXPECT_STREQ("a 1234z", buf);
267
268 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
269 EXPECT_STREQ("a1234 z", buf);
270
271 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
272 EXPECT_STREQ("Aabcdef Z", buf);
273
274 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
275 EXPECT_STREQ("Ahello:1234Z", buf);
276
277 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
278 EXPECT_STREQ("a005:5:05z", buf);
279
280 void* p = NULL;
281 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700282#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700283 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferris13613132013-10-28 15:24:04 -0700284#else
285 EXPECT_STREQ("a5,(nil)z", buf);
286#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700287
288 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
289 EXPECT_STREQ("a68719476736,6,7,8z", buf);
290
291 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
292 EXPECT_STREQ("a_1.230000_b", buf);
293
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700294 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700295 EXPECT_STREQ("a_3.14_b", buf);
296}
297
298TEST(stdio, snprintf_d_INT_MAX) {
299 char buf[BUFSIZ];
300 snprintf(buf, sizeof(buf), "%d", INT_MAX);
301 EXPECT_STREQ("2147483647", buf);
302}
303
304TEST(stdio, snprintf_d_INT_MIN) {
305 char buf[BUFSIZ];
306 snprintf(buf, sizeof(buf), "%d", INT_MIN);
307 EXPECT_STREQ("-2147483648", buf);
308}
309
310TEST(stdio, snprintf_ld_LONG_MAX) {
311 char buf[BUFSIZ];
312 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Elliott Hughes925753a2013-10-18 13:17:18 -0700313#if __LP64__
314 EXPECT_STREQ("9223372036854775807", buf);
315#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700316 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700317#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700318}
319
320TEST(stdio, snprintf_ld_LONG_MIN) {
321 char buf[BUFSIZ];
322 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Elliott Hughes925753a2013-10-18 13:17:18 -0700323#if __LP64__
324 EXPECT_STREQ("-9223372036854775808", buf);
325#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700326 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700327#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700328}
329
330TEST(stdio, snprintf_lld_LLONG_MAX) {
331 char buf[BUFSIZ];
332 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
333 EXPECT_STREQ("9223372036854775807", buf);
334}
335
336TEST(stdio, snprintf_lld_LLONG_MIN) {
337 char buf[BUFSIZ];
338 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
339 EXPECT_STREQ("-9223372036854775808", buf);
340}
341
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800342TEST(stdio, popen) {
343 FILE* fp = popen("cat /proc/version", "r");
344 ASSERT_TRUE(fp != NULL);
345
346 char buf[16];
347 char* s = fgets(buf, sizeof(buf), fp);
348 buf[13] = '\0';
349 ASSERT_STREQ("Linux version", s);
350
351 ASSERT_EQ(0, pclose(fp));
352}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700353
354TEST(stdio, getc) {
355 FILE* fp = fopen("/proc/version", "r");
356 ASSERT_TRUE(fp != NULL);
357 ASSERT_EQ('L', getc(fp));
358 ASSERT_EQ('i', getc(fp));
359 ASSERT_EQ('n', getc(fp));
360 ASSERT_EQ('u', getc(fp));
361 ASSERT_EQ('x', getc(fp));
362 fclose(fp);
363}
364
365TEST(stdio, putc) {
366 FILE* fp = fopen("/proc/version", "r");
367 ASSERT_TRUE(fp != NULL);
368 ASSERT_EQ(EOF, putc('x', fp));
369 fclose(fp);
370}