blob: 2d9717f5cef3eb6dd26d1bb7152323dc8644ea6c [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);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800106 ASSERT_EQ(EBADF, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300107}
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);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800144 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300145 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);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800160 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300161
162 // The second argument can't be NULL.
163 errno = 0;
164 ASSERT_EQ(getline(&buffer, NULL, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800165 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300166
167 // The stream can't be closed.
168 fclose(fp);
169 errno = 0;
170 ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800171 ASSERT_EQ(EBADF, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300172}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000173
174TEST(stdio, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800175 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800176 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800177 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 Glaserc641caf2013-02-17 16:50:58 +0000179 // 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 Hughes6b3f49a2013-03-06 16:20:55 -0800185
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700186#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
Elliott Hughes1d13c642013-09-23 16:02:39 -0700198TEST(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 Hines6c7b3cb2013-10-11 16:03:21 -0700229 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700230 EXPECT_STREQ("a16b", buf);
231
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700232 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700233 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 Hines6c7b3cb2013-10-11 16:03:21 -0700284 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700285 EXPECT_STREQ("a_3.14_b", buf);
286}
287
288TEST(stdio, snprintf_d_INT_MAX) {
289 char buf[BUFSIZ];
290 snprintf(buf, sizeof(buf), "%d", INT_MAX);
291 EXPECT_STREQ("2147483647", buf);
292}
293
294TEST(stdio, snprintf_d_INT_MIN) {
295 char buf[BUFSIZ];
296 snprintf(buf, sizeof(buf), "%d", INT_MIN);
297 EXPECT_STREQ("-2147483648", buf);
298}
299
300TEST(stdio, snprintf_ld_LONG_MAX) {
301 char buf[BUFSIZ];
302 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Elliott Hughes925753a2013-10-18 13:17:18 -0700303#if __LP64__
304 EXPECT_STREQ("9223372036854775807", buf);
305#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700306 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700307#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700308}
309
310TEST(stdio, snprintf_ld_LONG_MIN) {
311 char buf[BUFSIZ];
312 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Elliott Hughes925753a2013-10-18 13:17:18 -0700313#if __LP64__
314 EXPECT_STREQ("-9223372036854775808", buf);
315#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700316 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700317#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700318}
319
320TEST(stdio, snprintf_lld_LLONG_MAX) {
321 char buf[BUFSIZ];
322 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
323 EXPECT_STREQ("9223372036854775807", buf);
324}
325
326TEST(stdio, snprintf_lld_LLONG_MIN) {
327 char buf[BUFSIZ];
328 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
329 EXPECT_STREQ("-9223372036854775808", buf);
330}
331
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800332TEST(stdio, popen) {
333 FILE* fp = popen("cat /proc/version", "r");
334 ASSERT_TRUE(fp != NULL);
335
336 char buf[16];
337 char* s = fgets(buf, sizeof(buf), fp);
338 buf[13] = '\0';
339 ASSERT_STREQ("Linux version", s);
340
341 ASSERT_EQ(0, pclose(fp));
342}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700343
344TEST(stdio, getc) {
345 FILE* fp = fopen("/proc/version", "r");
346 ASSERT_TRUE(fp != NULL);
347 ASSERT_EQ('L', getc(fp));
348 ASSERT_EQ('i', getc(fp));
349 ASSERT_EQ('n', getc(fp));
350 ASSERT_EQ('u', getc(fp));
351 ASSERT_EQ('x', getc(fp));
352 fclose(fp);
353}
354
355TEST(stdio, putc) {
356 FILE* fp = fopen("/proc/version", "r");
357 ASSERT_TRUE(fp != NULL);
358 ASSERT_EQ(EOF, putc('x', fp));
359 fclose(fp);
360}