]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - tools/regression/lib/libc/stdio/test-fdopen.c
MFC r290537,r290540,r290560,r290856,r290871,r291839:
[FreeBSD/stable/10.git] / tools / regression / lib / libc / stdio / test-fdopen.c
1 /*-
2  * Copyright (c) 2014 Jilles Tjoelker
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include        <fcntl.h>
31 #include        <stdbool.h>
32 #include        <stdio.h>
33 #include        <string.h>
34 #include        <unistd.h>
35
36 static int testnum = 1;
37
38 static void
39 runtest(const char *fname, int intmode, const char *strmode, bool success)
40 {
41         FILE *fp;
42         int fd;
43
44         fd = open(fname, intmode);
45         if (fd == -1) {
46                 printf("not ok %d - open(\"%s\", %#x) failed\n",
47                     testnum++, fname, intmode);
48                 return;
49         }
50         fp = fdopen(fd, strmode);
51         if (fp == NULL) {
52                 close(fd);
53                 if (success)
54                         printf("not ok %d - "
55                             "fdopen(open(\"%s\", %#x), \"%s\") failed\n",
56                             testnum++, fname, intmode, strmode);
57                 else
58                         printf("ok %d - "
59                             "fdopen(open(\"%s\", %#x), \"%s\") failed\n",
60                             testnum++, fname, intmode, strmode);
61                 return;
62         }
63         if (success)
64                 printf("ok %d - "
65                     "fdopen(open(\"%s\", %#x), \"%s\") succeeded\n",
66                     testnum++, fname, intmode, strmode);
67         else
68                 printf("not ok %d - "
69                     "fdopen(open(\"%s\", %#x), \"%s\") succeeded\n",
70                     testnum++, fname, intmode, strmode);
71         fclose(fp);
72 }
73
74 /*
75  * Test program for fdopen().
76  */
77 int
78 main(int argc, char *argv[])
79 {
80         printf("1..19\n");
81         runtest("/dev/null", O_RDONLY, "r", true);
82         runtest("/dev/null", O_WRONLY, "r", false);
83         runtest("/dev/null", O_RDWR, "r", true);
84         runtest("/dev/null", O_RDONLY, "w", false);
85         runtest("/dev/null", O_WRONLY, "w", true);
86         runtest("/dev/null", O_RDWR, "w", true);
87         runtest("/dev/null", O_RDONLY, "a", false);
88         runtest("/dev/null", O_WRONLY, "a", true);
89         runtest("/dev/null", O_RDWR, "a", true);
90         runtest("/dev/null", O_RDONLY, "r+", false);
91         runtest("/dev/null", O_WRONLY, "r+", false);
92         runtest("/dev/null", O_RDWR, "r+", true);
93         runtest("/dev/null", O_RDONLY, "w+", false);
94         runtest("/dev/null", O_WRONLY, "w+", false);
95         runtest("/dev/null", O_RDWR, "w+", true);
96         runtest("/bin/sh", O_EXEC, "r", false);
97         runtest("/bin/sh", O_EXEC, "w", false);
98         runtest("/bin/sh", O_EXEC, "r+", false);
99         runtest("/bin/sh", O_EXEC, "w+", false);
100
101         return 0;
102 }
103
104 /* vim:ts=8:cin:sw=8
105  *  */