]> git.saurik.com Git - apple/libc.git/blob - tests/stdio.c
Libc-1244.1.7.tar.gz
[apple/libc.git] / tests / stdio.c
1 #include <stdio.h>
2 #include <err.h>
3 #include <errno.h>
4 #include <unistd.h>
5 #include <sys/resource.h>
6
7 #include <darwintest.h>
8
9 #define FILE_LIMIT 100
10
11 T_DECL(stdio_PR_22813396, "STREAM_MAX is affected by changes to RLIMIT_NOFILE")
12 {
13 struct rlimit theLimit;
14 getrlimit( RLIMIT_NOFILE, &theLimit );
15 theLimit.rlim_cur = FILE_LIMIT;
16 setrlimit( RLIMIT_NOFILE, &theLimit );
17
18 long stream_max = sysconf(_SC_STREAM_MAX);
19 T_EXPECT_EQ_LONG(stream_max, (long)FILE_LIMIT, "stream_max = FILE_LIMIT");
20
21 FILE *f;
22 for(int i = 3; i < stream_max; i++) {
23 if((f = fdopen(0, "r")) == NULL) {
24 T_FAIL("Failed after %d streams", i);
25 }
26 }
27
28 f = fdopen(0, "r");
29 T_EXPECT_NULL(f, "fdopen fail after stream_max streams");
30
31 theLimit.rlim_cur = FILE_LIMIT + 1;
32 setrlimit( RLIMIT_NOFILE, &theLimit );
33
34 f = fdopen(0, "r");
35 T_EXPECT_NOTNULL(f, "fdopen succeed after RLIMIT_NOFILE increased");
36 }
37
38 T_DECL(stdio_PR_22813396_close, "STREAM_MAX is enforced properly after fclose")
39 {
40 struct rlimit theLimit;
41 getrlimit( RLIMIT_NOFILE, &theLimit );
42 theLimit.rlim_cur = FILE_LIMIT;
43 setrlimit( RLIMIT_NOFILE, &theLimit );
44
45 long stream_max = sysconf(_SC_STREAM_MAX);
46 T_EXPECT_EQ_LONG(stream_max, (long)FILE_LIMIT, "stream_max = FILE_LIMIT");
47
48 FILE *f;
49 for(int i = 3; i < stream_max - 1; i++) {
50 if((f = fdopen(0, "r")) == NULL) {
51 T_FAIL("Failed after %d streams", i);
52 }
53 }
54
55 // the last stream is for dup(0), it needs to be fclose'd
56 FILE *dupf = NULL;
57 T_EXPECT_NOTNULL(dupf = fdopen(dup(0), "r"), NULL);
58
59 T_EXPECT_NULL(f = fdopen(0, "r"), "fdopen fail after stream_max streams");
60
61 T_EXPECT_POSIX_ZERO(fclose(dupf), "fclose succeeds");
62
63 f = fdopen(0, "r");
64 T_WITH_ERRNO; T_EXPECT_NOTNULL(f, "fdopen succeed after fclose");
65 }
66