summaryrefslogtreecommitdiff
path: root/test/misc/tst-mkostemps.c
blob: 2f45bfd99dc89f8b45e5c776925c10b56455bef5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Test application for mkstemp/mkstemps/mkostemp/mkostemps
 * Copyright (C) 2015 by Romain Naour <romain.naour@openwide.fr>
 *
 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#define assert(x) \
  if (!(x)) \
    { \
      fputs ("test failed: " #x "\n", stderr); \
      retval = 1; \
      goto the_end; \
    }

int
main (int argc, char *argv[])
{
  char name[256];
  char name_suffix[256];
  FILE *fp = NULL;
  int retval = 0;
  int fd;
  int flags = O_RDONLY | O_CLOEXEC;
  struct stat sb_f1;
  struct stat sb_f2;

  /* mkstemp test */
  sprintf(name, "%s-uClibc-test.XXXXXX", __FILE__);

  fd = mkstemp(name);

  fstat(fd, &sb_f1);
  assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)

  stat(name, &sb_f2);
  assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)

  assert (sb_f1.st_ino == sb_f2.st_ino)

  close(fd);
  unlink (name);

  /* mkstemps test */
  sprintf(name_suffix, "%s-uClibc-test.XXXXXX.txt", __FILE__);

  fd = mkstemps(name_suffix, 4);

  fstat(fd, &sb_f1);
  assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)

  stat(name_suffix, &sb_f2);
  assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)

  assert (sb_f1.st_ino == sb_f2.st_ino)

  close(fd);
  unlink (name_suffix);

  /* mkostemp test */
  sprintf(name, "%s-uClibc-test.XXXXXX", __FILE__);

  fd = mkostemp(name, flags);

  fstat(fd, &sb_f1);
  assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)

  stat(name, &sb_f2);
  assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)

  assert (sb_f1.st_ino == sb_f2.st_ino)
  assert (sb_f1.st_mode == sb_f2.st_mode)

  close(fd);
  unlink (name);

  /* mkostemps test */
  sprintf(name_suffix, "%s-uClibc-test.XXXXXX.txt", __FILE__);

  fd = mkostemps(name_suffix, 4, flags);

  fstat(fd, &sb_f1);
  assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)

  stat(name, &sb_f2);
  assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)

  assert (sb_f1.st_ino == sb_f2.st_ino)
  assert (sb_f1.st_mode == sb_f2.st_mode)

  close(fd);
  unlink (name_suffix);

  /* suffixlen = 0 */
  sprintf(name_suffix, "%s-uClibc-test.XXXXXX", __FILE__);

  fd = mkostemps(name_suffix, 0, flags);

  fstat(fd, &sb_f1);
  assert ((sb_f1.st_mode & S_IFMT) == S_IFREG)

  stat(name, &sb_f2);
  assert ((sb_f2.st_mode & S_IFMT) == S_IFREG)

  assert (sb_f1.st_ino == sb_f2.st_ino)
  assert (sb_f1.st_mode == sb_f2.st_mode)

  close(fd);
  unlink (name_suffix);

  /* stress tests */

  /* template len < 6 */
  sprintf(name, "XXXXX");

  fd = mkstemp(name);

  assert(fd == -1);
  assert(errno == EINVAL);

  /* suffixlen < 0 */
  sprintf(name_suffix, "%s-uClibc-test.XXXXXX.txt", __FILE__);

  fd = mkostemps(name_suffix, -1, flags);

  assert(fd == -1);
  assert(errno == EINVAL);

  /* Missing one X */
  sprintf(name_suffix, "%s-uClibc-test.XXXXX.txt", __FILE__);

  fd = mkostemps(name_suffix, 4, flags);

  assert(fd == -1);
  assert(errno == EINVAL);

  /* wrong suffixlen */
  sprintf(name_suffix, "%s-uClibc-test.XXXXXX.txt", __FILE__);

  fd = mkostemps(name_suffix, 2, flags);

  assert(fd == -1);
  assert(errno == EINVAL);

the_end:
  if (fp != NULL)
    assert (fclose (fp) == 0);
  unlink (name);
  unlink (name_suffix);

  return retval;
}