OSDN Git Service

add tests for mkostemps()
[uclinux-h8/uclibc-ng.git] / test / misc / tst-mkostemps.c
1 /*
2  * Test application for mkstemp/mkstemps/mkostemp/mkostemps
3  * Copyright (C) 2015 by Romain Naour <romain.naour@openwide.fr>
4  *
5  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <errno.h>
14
15 #define assert(x) \
16   if (!(x)) \
17     { \
18       fputs ("test failed: " #x "\n", stderr); \
19       retval = 1; \
20       goto the_end; \
21     }
22
23 int
24 main (int argc, char *argv[])
25 {
26   char name[256];
27   char name_suffix[256];
28   FILE *fp = NULL;
29   int retval = 0;
30   int fd;
31   int flags = O_RDONLY | O_CLOEXEC;
32   struct stat sb_f1;
33   struct stat sb_f2;
34
35   /* mkstemp test */
36   sprintf(name, "%s-uClibc-test.XXXXXX", __FILE__);
37
38   fd = mkstemp(name);
39
40   fstat(fd, &sb_f1);
41   assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)
42
43   stat(name, &sb_f2);
44   assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)
45
46   assert (sb_f1.st_ino == sb_f2.st_ino)
47
48   close(fd);
49   unlink (name);
50
51   /* mkstemps test */
52   sprintf(name_suffix, "%s-uClibc-test.XXXXXX.txt", __FILE__);
53
54   fd = mkstemps(name_suffix, 4);
55
56   fstat(fd, &sb_f1);
57   assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)
58
59   stat(name_suffix, &sb_f2);
60   assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)
61
62   assert (sb_f1.st_ino == sb_f2.st_ino)
63
64   close(fd);
65   unlink (name_suffix);
66
67   /* mkostemp test */
68   sprintf(name, "%s-uClibc-test.XXXXXX", __FILE__);
69
70   fd = mkostemp(name, flags);
71
72   fstat(fd, &sb_f1);
73   assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)
74
75   stat(name, &sb_f2);
76   assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)
77
78   assert (sb_f1.st_ino == sb_f2.st_ino)
79   assert (sb_f1.st_mode == sb_f2.st_mode)
80
81   close(fd);
82   unlink (name);
83
84   /* mkostemps test */
85   sprintf(name_suffix, "%s-uClibc-test.XXXXXX.txt", __FILE__);
86
87   fd = mkostemps(name_suffix, 4, flags);
88
89   fstat(fd, &sb_f1);
90   assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)
91
92   stat(name, &sb_f2);
93   assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)
94
95   assert (sb_f1.st_ino == sb_f2.st_ino)
96   assert (sb_f1.st_mode == sb_f2.st_mode)
97
98   close(fd);
99   unlink (name_suffix);
100
101   /* suffixlen = 0 */
102   sprintf(name_suffix, "%s-uClibc-test.XXXXXX", __FILE__);
103
104   fd = mkostemps(name_suffix, 0, flags);
105
106   fstat(fd, &sb_f1);
107   assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)
108
109   stat(name, &sb_f2);
110   assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)
111
112   assert (sb_f1.st_ino == sb_f2.st_ino)
113   assert (sb_f1.st_mode == sb_f2.st_mode)
114
115   close(fd);
116   unlink (name_suffix);
117
118   /* stress tests */
119
120   /* template len < 6 */
121   sprintf(name, "XXXXX");
122
123   fd = mkstemp(name);
124
125   assert(fd == -1);
126   assert(errno == EINVAL);
127
128   /* suffixlen < 0 */
129   sprintf(name_suffix, "%s-uClibc-test.XXXXXX.txt", __FILE__);
130
131   fd = mkostemps(name_suffix, -1, flags);
132
133   assert(fd == -1);
134   assert(errno == EINVAL);
135
136   /* Missing one X */
137   sprintf(name_suffix, "%s-uClibc-test.XXXXX.txt", __FILE__);
138
139   fd = mkostemps(name_suffix, 4, flags);
140
141   assert(fd == -1);
142   assert(errno == EINVAL);
143
144   /* wrong suffixlen */
145   sprintf(name_suffix, "%s-uClibc-test.XXXXXX.txt", __FILE__);
146
147   fd = mkostemps(name_suffix, 2, flags);
148
149   assert(fd == -1);
150   assert(errno == EINVAL);
151
152 the_end:
153   if (fp != NULL)
154     assert (fclose (fp) == 0);
155   unlink (name);
156   unlink (name_suffix);
157
158   return retval;
159 }