chiark / gitweb /
rename recentact to lowvol
[inn-innduct.git] / tests / lib / mkstemp-t.c
1 /* $Id: mkstemp-t.c 5329 2002-03-17 07:39:14Z rra $ */
2 /* mkstemp test suite */
3
4 #include "config.h"
5 #include "clibrary.h"
6 #include <errno.h>
7 #include <sys/stat.h>
8
9 #include "libtest.h"
10
11 int test_mkstemp(char *template);
12
13 int
14 main(void)
15 {
16     int fd;
17     char template[] = "tsXXXXXXX";
18     char tooshort[] = "XXXXX";
19     char bad1[] = "/foo/barXXXXX";
20     char bad2[] = "/foo/barXXXXXX.out";
21     char buffer[256];
22     struct stat st1, st2;
23     ssize_t length;
24
25     puts("20");
26
27     /* First, test a few error messages. */
28     errno = 0;
29     ok_int(1, -1, test_mkstemp(tooshort));
30     ok(2, errno == EINVAL);
31     ok_string(3, "XXXXX", tooshort);
32     errno = 0;
33     ok_int(4, -1, test_mkstemp(bad1));
34     ok(5, errno == EINVAL);
35     ok_string(6, "/foo/barXXXXX", bad1);
36     errno = 0;
37     ok_int(7, -1, test_mkstemp(bad2));
38     ok(8, errno == EINVAL);
39     ok_string(9, "/foo/barXXXXXX.out", bad2);
40     errno = 0;
41
42     /* Now try creating a real file. */
43     fd = test_mkstemp(template);
44     ok(10, fd >= 0);
45     ok(11, strcmp(template, "tsXXXXXXX") != 0);
46     ok(12, strncmp(template, "tsX", 3) == 0);
47     ok(13, access(template, F_OK) == 0);
48
49     /* Make sure that it's the same file as template refers to now. */
50     ok(14, stat(template, &st1) == 0);
51     ok(15, fstat(fd, &st2) == 0);
52     ok(16, st1.st_ino == st2.st_ino);
53     unlink(template);
54
55     /* Make sure the open mode is correct. */
56     length = strlen(template);
57     ok(17, write(fd, template, length) == length);
58     ok(18, lseek(fd, 0, SEEK_SET) == 0);
59     ok(19, read(fd, buffer, length) == length);
60     buffer[length] = '\0';
61     ok_string(20, template, buffer);
62     close(fd);
63
64     return 0;
65 }