summaryrefslogtreecommitdiff
path: root/test/misc/tst-mkostemps.c
diff options
context:
space:
mode:
authorRomain Naour <romain.naour@openwide.fr>2015-08-22 21:08:36 +0200
committerWaldemar Brodkorb <wbx@uclibc-ng.org>2015-08-22 21:34:38 +0200
commitc60ccea5aa3fee31e21e32c4e4f6bebb782618cc (patch)
treed6fc1e6ea03a9cc355471f1a109905c1639d1380 /test/misc/tst-mkostemps.c
parent33e3dbe62c420cc1cdee09d60735d20b5eff9a3d (diff)
add tests for mkostemps()
Signed-off-by: Romain Naour <romain.naour@openwide.fr>
Diffstat (limited to 'test/misc/tst-mkostemps.c')
-rw-r--r--test/misc/tst-mkostemps.c159
1 files changed, 159 insertions, 0 deletions
diff --git a/test/misc/tst-mkostemps.c b/test/misc/tst-mkostemps.c
new file mode 100644
index 000000000..2f45bfd99
--- /dev/null
+++ b/test/misc/tst-mkostemps.c
@@ -0,0 +1,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;
+}