From bd5969fc876a10b18613302db7087ef3c40f18e1 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 7 Mar 2024 17:28:00 -0500 Subject: lib: Add mlibc Signed-off-by: Ian Moffett --- lib/mlibc/tests/posix/mkstemp.c | 70 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 lib/mlibc/tests/posix/mkstemp.c (limited to 'lib/mlibc/tests/posix/mkstemp.c') 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 +#include +#include +#include +#include +#include +#include + +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); +} -- cgit v1.2.3