summaryrefslogtreecommitdiff
path: root/tests/portable
diff options
context:
space:
mode:
Diffstat (limited to 'tests/portable')
-rw-r--r--tests/portable/asprintf-t.c1
-rw-r--r--tests/portable/mkstemp-t.c80
-rw-r--r--tests/portable/mkstemp.c2
-rw-r--r--tests/portable/reallocarray-t.c92
-rw-r--r--tests/portable/reallocarray.c2
-rw-r--r--tests/portable/snprintf-t.c4
6 files changed, 180 insertions, 1 deletions
diff --git a/tests/portable/asprintf-t.c b/tests/portable/asprintf-t.c
index c61c14a..e556d95 100644
--- a/tests/portable/asprintf-t.c
+++ b/tests/portable/asprintf-t.c
@@ -16,6 +16,7 @@
*/
#include <config.h>
+#include <portable/macros.h>
#include <portable/system.h>
#include <tests/tap/basic.h>
diff --git a/tests/portable/mkstemp-t.c b/tests/portable/mkstemp-t.c
new file mode 100644
index 0000000..20a83fc
--- /dev/null
+++ b/tests/portable/mkstemp-t.c
@@ -0,0 +1,80 @@
+/*
+ * mkstemp test suite.
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <eagle@eyrie.org>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+
+#include <config.h>
+#include <portable/system.h>
+
+#include <errno.h>
+#include <sys/stat.h>
+
+#include <tests/tap/basic.h>
+
+int test_mkstemp(char *template);
+
+int
+main(void)
+{
+ int fd;
+ char template[] = "tsXXXXXXX";
+ char tooshort[] = "XXXXX";
+ char bad1[] = "/foo/barXXXXX";
+ char bad2[] = "/foo/barXXXXXX.out";
+ char buffer[256];
+ struct stat st1, st2;
+ ssize_t length;
+
+ plan(20);
+
+ /* First, test a few error messages. */
+ errno = 0;
+ is_int(-1, test_mkstemp(tooshort), "too short of template");
+ is_int(EINVAL, errno, "...with correct errno");
+ is_string("XXXXX", tooshort, "...and template didn't change");
+ errno = 0;
+ is_int(-1, test_mkstemp(bad1), "bad template");
+ is_int(EINVAL, errno, "...with correct errno");
+ is_string("/foo/barXXXXX", bad1, "...and template didn't change");
+ errno = 0;
+ is_int(-1, test_mkstemp(bad2), "template doesn't end in XXXXXX");
+ is_int(EINVAL, errno, "...with correct errno");
+ is_string("/foo/barXXXXXX.out", bad2, "...and template didn't change");
+ errno = 0;
+
+ /* Now try creating a real file. */
+ fd = test_mkstemp(template);
+ ok(fd >= 0, "mkstemp works with valid template");
+ ok(strcmp(template, "tsXXXXXXX") != 0, "...and template changed");
+ ok(strncmp(template, "tsX", 3) == 0, "...and didn't touch first X");
+ ok(access(template, F_OK) == 0, "...and the file exists");
+
+ /* Make sure that it's the same file as template refers to now. */
+ ok(stat(template, &st1) == 0, "...and stat of template works");
+ ok(fstat(fd, &st2) == 0, "...and stat of open file descriptor works");
+ ok(st1.st_ino == st2.st_ino, "...and they're the same file");
+ unlink(template);
+
+ /* Make sure the open mode is correct. */
+ length = strlen(template);
+ is_int(length, write(fd, template, length), "write to open file works");
+ ok(lseek(fd, 0, SEEK_SET) == 0, "...and rewind works");
+ is_int(length, read(fd, buffer, length), "...and the data is there");
+ buffer[length] = '\0';
+ is_string(template, buffer, "...and matches what we wrote");
+ close(fd);
+
+ return 0;
+}
diff --git a/tests/portable/mkstemp.c b/tests/portable/mkstemp.c
new file mode 100644
index 0000000..4632d3d
--- /dev/null
+++ b/tests/portable/mkstemp.c
@@ -0,0 +1,2 @@
+#define TESTING 1
+#include <portable/mkstemp.c>
diff --git a/tests/portable/reallocarray-t.c b/tests/portable/reallocarray-t.c
new file mode 100644
index 0000000..481da58
--- /dev/null
+++ b/tests/portable/reallocarray-t.c
@@ -0,0 +1,92 @@
+/*
+ * reallocarray test suite.
+ *
+ * This does some simple sanity checks and checks some of the overflow
+ * detection, but isn't particularly thorough.
+ *
+ * The canonical version of this file is maintained in the rra-c-util package,
+ * which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
+ *
+ * Written by Russ Allbery <eagle@eyrie.org>
+ *
+ * The authors hereby relinquish any claim to any copyright that they may have
+ * in this work, whether granted under contract or by operation of law or
+ * international treaty, and hereby commit to the public, at large, that they
+ * shall not, at any time in the future, seek to enforce any copyright in this
+ * work against any person or entity, or prevent any person or entity from
+ * copying, publishing, distributing or creating derivative works of this
+ * work.
+ */
+
+#include <config.h>
+#include <portable/system.h>
+
+#include <errno.h>
+
+#include <tests/tap/basic.h>
+
+void *test_reallocarray(void *, size_t, size_t);
+
+
+int
+main(void)
+{
+ char *p, *base;
+ size_t sqrt_max;
+ int oerrno;
+
+ plan(15);
+
+ /* Test success cases and write to the memory for valgrind checks. */
+ p = test_reallocarray(NULL, 2, 5);
+ memcpy(p, "123456789", 10);
+ is_string("123456789", p, "reallocarray of NULL");
+ p = test_reallocarray(p, 4, 5);
+ is_string("123456789", p, "reallocarray after resize");
+ memcpy(p + 9, "0123456789", 11);
+ is_string("1234567890123456789", p, "write to larger memory segment");
+ free(p);
+
+ /*
+ * If nmemb or size are 0, we should either get NULL or a pointer we can
+ * free. Make sure we don't get something weird, like division by zero.
+ */
+ p = test_reallocarray(NULL, 0, 100);
+ if (p != NULL)
+ free(p);
+ p = test_reallocarray(NULL, 100, 0);
+ if (p != NULL)
+ free(p);
+
+ /* Test the range-checking error cases. */
+ p = test_reallocarray(NULL, 2, SIZE_MAX / 2);
+ oerrno = errno;
+ ok(p == NULL, "reallocarray fails for 2, SIZE_MAX / 2");
+ is_int(ENOMEM, oerrno, "...with correct errno");
+ base = malloc(10);
+ p = test_reallocarray(base, 3, SIZE_MAX / 3);
+ oerrno = errno;
+ ok(p == NULL, "reallocarray fails for 3, SIZE_MAX / 3");
+ is_int(ENOMEM, oerrno, "...with correct errno");
+ sqrt_max = (1UL << (sizeof(size_t) * 4));
+ p = test_reallocarray(base, sqrt_max, sqrt_max);
+ oerrno = errno;
+ ok(p == NULL, "reallocarray fails for sqrt(SIZE_MAX), sqrt(SIZE_MAX)");
+ is_int(ENOMEM, oerrno, "...with correct errno");
+ p = test_reallocarray(base, 1, SIZE_MAX);
+ oerrno = errno;
+ ok(p == NULL, "reallocarray fails for 1, SIZE_MAX");
+ is_int(ENOMEM, oerrno, "...with correct errno");
+ p = test_reallocarray(base, SIZE_MAX, 1);
+ oerrno = errno;
+ ok(p == NULL, "reallocarray fails for SIZE_MAX, 1");
+ is_int(ENOMEM, oerrno, "...with correct errno");
+ p = test_reallocarray(base, 2, SIZE_MAX);
+ oerrno = errno;
+ ok(p == NULL, "reallocarray fails for 2, SIZE_MAX");
+ is_int(ENOMEM, oerrno, "...with correct errno");
+
+ /* Clean up and exit. */
+ free(base);
+ return 0;
+}
diff --git a/tests/portable/reallocarray.c b/tests/portable/reallocarray.c
new file mode 100644
index 0000000..7cd29e2
--- /dev/null
+++ b/tests/portable/reallocarray.c
@@ -0,0 +1,2 @@
+#define TESTING 1
+#include <portable/reallocarray.c>
diff --git a/tests/portable/snprintf-t.c b/tests/portable/snprintf-t.c
index 270d2e1..cc8cf00 100644
--- a/tests/portable/snprintf-t.c
+++ b/tests/portable/snprintf-t.c
@@ -26,7 +26,9 @@
* Disable the requirement that format strings be literals. We need variable
* formats for easy testing.
*/
-#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2) || defined(__clang__)
+# pragma GCC diagnostic ignored "-Wformat-nonliteral"
+#endif
/*
* Intentionally don't add the printf attribute here since we pass a