xref: /freebsd/contrib/pam-krb5/tests/portable/mkstemp-t.c (revision bf6873c5786e333d679a7838d28812febf479a8a)
1*bf6873c5SCy Schubert /*
2*bf6873c5SCy Schubert  * mkstemp test suite.
3*bf6873c5SCy Schubert  *
4*bf6873c5SCy Schubert  * The canonical version of this file is maintained in the rra-c-util package,
5*bf6873c5SCy Schubert  * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
6*bf6873c5SCy Schubert  *
7*bf6873c5SCy Schubert  * Written by Russ Allbery <eagle@eyrie.org>
8*bf6873c5SCy Schubert  * Copyright 2009, 2011
9*bf6873c5SCy Schubert  *     The Board of Trustees of the Leland Stanford Junior University
10*bf6873c5SCy Schubert  *
11*bf6873c5SCy Schubert  * Copying and distribution of this file, with or without modification, are
12*bf6873c5SCy Schubert  * permitted in any medium without royalty provided the copyright notice and
13*bf6873c5SCy Schubert  * this notice are preserved.  This file is offered as-is, without any
14*bf6873c5SCy Schubert  * warranty.
15*bf6873c5SCy Schubert  *
16*bf6873c5SCy Schubert  * SPDX-License-Identifier: FSFAP
17*bf6873c5SCy Schubert  */
18*bf6873c5SCy Schubert 
19*bf6873c5SCy Schubert #include <config.h>
20*bf6873c5SCy Schubert #include <portable/system.h>
21*bf6873c5SCy Schubert 
22*bf6873c5SCy Schubert #include <errno.h>
23*bf6873c5SCy Schubert #include <sys/stat.h>
24*bf6873c5SCy Schubert 
25*bf6873c5SCy Schubert #include <tests/tap/basic.h>
26*bf6873c5SCy Schubert 
27*bf6873c5SCy Schubert int test_mkstemp(char *template);
28*bf6873c5SCy Schubert 
29*bf6873c5SCy Schubert int
main(void)30*bf6873c5SCy Schubert main(void)
31*bf6873c5SCy Schubert {
32*bf6873c5SCy Schubert     int fd;
33*bf6873c5SCy Schubert     char template[] = "tsXXXXXXX";
34*bf6873c5SCy Schubert     char tooshort[] = "XXXXX";
35*bf6873c5SCy Schubert     char bad1[] = "/foo/barXXXXX";
36*bf6873c5SCy Schubert     char bad2[] = "/foo/barXXXXXX.out";
37*bf6873c5SCy Schubert     char buffer[256];
38*bf6873c5SCy Schubert     struct stat st1, st2;
39*bf6873c5SCy Schubert     ssize_t length;
40*bf6873c5SCy Schubert 
41*bf6873c5SCy Schubert     plan(20);
42*bf6873c5SCy Schubert 
43*bf6873c5SCy Schubert     /* First, test a few error messages. */
44*bf6873c5SCy Schubert     errno = 0;
45*bf6873c5SCy Schubert     is_int(-1, test_mkstemp(tooshort), "too short of template");
46*bf6873c5SCy Schubert     is_int(EINVAL, errno, "...with correct errno");
47*bf6873c5SCy Schubert     is_string("XXXXX", tooshort, "...and template didn't change");
48*bf6873c5SCy Schubert     errno = 0;
49*bf6873c5SCy Schubert     is_int(-1, test_mkstemp(bad1), "bad template");
50*bf6873c5SCy Schubert     is_int(EINVAL, errno, "...with correct errno");
51*bf6873c5SCy Schubert     is_string("/foo/barXXXXX", bad1, "...and template didn't change");
52*bf6873c5SCy Schubert     errno = 0;
53*bf6873c5SCy Schubert     is_int(-1, test_mkstemp(bad2), "template doesn't end in XXXXXX");
54*bf6873c5SCy Schubert     is_int(EINVAL, errno, "...with correct errno");
55*bf6873c5SCy Schubert     is_string("/foo/barXXXXXX.out", bad2, "...and template didn't change");
56*bf6873c5SCy Schubert     errno = 0;
57*bf6873c5SCy Schubert 
58*bf6873c5SCy Schubert     /* Now try creating a real file. */
59*bf6873c5SCy Schubert     fd = test_mkstemp(template);
60*bf6873c5SCy Schubert     ok(fd >= 0, "mkstemp works with valid template");
61*bf6873c5SCy Schubert     ok(strcmp(template, "tsXXXXXXX") != 0, "...and template changed");
62*bf6873c5SCy Schubert     ok(strncmp(template, "tsX", 3) == 0, "...and didn't touch first X");
63*bf6873c5SCy Schubert     ok(access(template, F_OK) == 0, "...and the file exists");
64*bf6873c5SCy Schubert 
65*bf6873c5SCy Schubert     /* Make sure that it's the same file as template refers to now. */
66*bf6873c5SCy Schubert     ok(stat(template, &st1) == 0, "...and stat of template works");
67*bf6873c5SCy Schubert     ok(fstat(fd, &st2) == 0, "...and stat of open file descriptor works");
68*bf6873c5SCy Schubert     ok(st1.st_ino == st2.st_ino, "...and they're the same file");
69*bf6873c5SCy Schubert     unlink(template);
70*bf6873c5SCy Schubert 
71*bf6873c5SCy Schubert     /* Make sure the open mode is correct. */
72*bf6873c5SCy Schubert     length = strlen(template);
73*bf6873c5SCy Schubert     is_int(length, write(fd, template, length), "write to open file works");
74*bf6873c5SCy Schubert     ok(lseek(fd, 0, SEEK_SET) == 0, "...and rewind works");
75*bf6873c5SCy Schubert     is_int(length, read(fd, buffer, length), "...and the data is there");
76*bf6873c5SCy Schubert     buffer[length] = '\0';
77*bf6873c5SCy Schubert     is_string(template, buffer, "...and matches what we wrote");
78*bf6873c5SCy Schubert     close(fd);
79*bf6873c5SCy Schubert 
80*bf6873c5SCy Schubert     return 0;
81*bf6873c5SCy Schubert }
82