aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/mkstemp.c
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-03-07 17:28:00 -0500
committerIan Moffett <ian@osmora.org>2024-03-07 17:28:32 -0500
commitbd5969fc876a10b18613302db7087ef3c40f18e1 (patch)
tree7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/tests/posix/mkstemp.c
parenta95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff)
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/tests/posix/mkstemp.c')
-rw-r--r--lib/mlibc/tests/posix/mkstemp.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/mlibc/tests/posix/mkstemp.c b/lib/mlibc/tests/posix/mkstemp.c
new file mode 100644
index 0000000..d06783e
--- /dev/null
+++ b/lib/mlibc/tests/posix/mkstemp.c
@@ -0,0 +1,70 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+
+void validate_pattern(char *p) {
+ assert(memcmp(p, "XXXXXX", 6));
+ assert(memchr(p, 0, 6) == NULL);
+
+ for (int i = 0; i < 6; i++) {
+ assert(isalnum(p[i]));
+ }
+}
+
+int main() {
+ int ret;
+
+ // Make sure the patterns themselves cannot be chosen. This
+ // *could* happen on glibc, or if we widen the character set
+ // used for generating random names. Odds are 1 in 60 billion,
+ // but I'd rather not worry about this.
+ ret = open("prefixXXXXXX", O_RDWR | O_CREAT | O_EXCL, 0600);
+ assert(ret >= 0 || (ret == -1 && errno == EEXIST));
+ ret = open("longprefixXXXXXXlongsuffix", O_RDWR | O_CREAT | O_EXCL, 0600);
+ assert(ret >= 0 || (ret == -1 && errno == EEXIST));
+
+ ret = mkstemp("short");
+ assert(ret == -1);
+ assert(errno == EINVAL);
+
+ ret = mkstemp("lessthan6XXX");
+ assert(ret == -1);
+ assert(errno == EINVAL);
+
+ ret = mkstemps("lessthan6XXXswithsuffix", 11);
+ assert(ret == -1);
+ assert(errno == EINVAL);
+
+ char *p = strdup("prefixXXXXXX");
+ ret = mkstemp(p);
+ // We can't really protect against EEXIST...
+ assert(ret >= 0 || (ret == -1 && errno == EEXIST));
+ assert(!memcmp(p, "prefix", 6));
+ assert(p[12] == 0);
+ validate_pattern(p + 6);
+
+ if (ret >= 0) {
+ ret = close(ret);
+ assert(!ret);
+ }
+ free(p);
+
+ p = strdup("longprefixXXXXXXlongsuffix");
+ ret = mkstemps(p, 10);
+ // We can't really protect against EEXIST...
+ assert(ret >= 0 || (ret == -1 && errno == EEXIST));
+ assert(!memcmp(p, "longprefix", 10));
+ assert(!memcmp(p + 16, "longsuffix", 10));
+ assert(p[26] == 0);
+ validate_pattern(p + 10);
+
+ if (ret >= 0) {
+ ret = close(ret);
+ assert(!ret);
+ }
+ free(p);
+}