]> eyrie.org Git - kerberos/krb5-strength.git/blob - tests/portable/mkstemp-t.c
Update to rra-c-util 5.3 and C TAP Harness 3.0
[kerberos/krb5-strength.git] / tests / portable / mkstemp-t.c
1 /*
2  * mkstemp test suite.
3  *
4  * The canonical version of this file is maintained in the rra-c-util package,
5  * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
6  *
7  * Written by Russ Allbery <eagle@eyrie.org>
8  *
9  * The authors hereby relinquish any claim to any copyright that they may have
10  * in this work, whether granted under contract or by operation of law or
11  * international treaty, and hereby commit to the public, at large, that they
12  * shall not, at any time in the future, seek to enforce any copyright in this
13  * work against any person or entity, or prevent any person or entity from
14  * copying, publishing, distributing or creating derivative works of this
15  * work.
16  */
17
18 #include <config.h>
19 #include <portable/system.h>
20
21 #include <errno.h>
22 #include <sys/stat.h>
23
24 #include <tests/tap/basic.h>
25
26 int test_mkstemp(char *template);
27
28 int
29 main(void)
30 {
31     int fd;
32     char template[] = "tsXXXXXXX";
33     char tooshort[] = "XXXXX";
34     char bad1[] = "/foo/barXXXXX";
35     char bad2[] = "/foo/barXXXXXX.out";
36     char buffer[256];
37     struct stat st1, st2;
38     ssize_t length;
39
40     plan(20);
41
42     /* First, test a few error messages. */
43     errno = 0;
44     is_int(-1, test_mkstemp(tooshort), "too short of template");
45     is_int(EINVAL, errno, "...with correct errno");
46     is_string("XXXXX", tooshort, "...and template didn't change");
47     errno = 0;
48     is_int(-1, test_mkstemp(bad1), "bad template");
49     is_int(EINVAL, errno, "...with correct errno");
50     is_string("/foo/barXXXXX", bad1, "...and template didn't change");
51     errno = 0;
52     is_int(-1, test_mkstemp(bad2), "template doesn't end in XXXXXX");
53     is_int(EINVAL, errno, "...with correct errno");
54     is_string("/foo/barXXXXXX.out", bad2, "...and template didn't change");
55     errno = 0;
56
57     /* Now try creating a real file. */
58     fd = test_mkstemp(template);
59     ok(fd >= 0, "mkstemp works with valid template");
60     ok(strcmp(template, "tsXXXXXXX") != 0, "...and template changed");
61     ok(strncmp(template, "tsX", 3) == 0, "...and didn't touch first X");
62     ok(access(template, F_OK) == 0, "...and the file exists");
63
64     /* Make sure that it's the same file as template refers to now. */
65     ok(stat(template, &st1) == 0, "...and stat of template works");
66     ok(fstat(fd, &st2) == 0, "...and stat of open file descriptor works");
67     ok(st1.st_ino == st2.st_ino, "...and they're the same file");
68     unlink(template);
69
70     /* Make sure the open mode is correct. */
71     length = strlen(template);
72     is_int(length, write(fd, template, length), "write to open file works");
73     ok(lseek(fd, 0, SEEK_SET) == 0, "...and rewind works");
74     is_int(length, read(fd, buffer, length), "...and the data is there");
75     buffer[length] = '\0';
76     is_string(template, buffer, "...and matches what we wrote");
77     close(fd);
78
79     return 0;
80 }