Path: blob/main/lib/libc/tests/stdio/mkostemp_test.c
39530 views
/*-1* Copyright (c) 2013 Jilles Tjoelker2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526/*27* Test program for mkostemp().28*/2930#include <sys/stat.h>3132#include <errno.h>33#include <fcntl.h>34#include <paths.h>35#include <stdio.h>36#include <stdlib.h>37#include <string.h>38#include <unistd.h>3940#include <atf-c.h>4142static const char template[] = "mkostemp.XXXXXXXX";43static int testnum;4445#define MISCFLAGS (O_APPEND | O_DIRECT | O_SHLOCK | O_EXLOCK | O_SYNC)4647static void48test_one(int oflags)49{50char tmpf[sizeof(template)];51struct stat st1, st2;52int fd;5354memcpy(tmpf, template, sizeof(tmpf));55fd = mkostemp(tmpf, oflags);56if (fd < 0) {57printf("not ok %d - oflags=%#x "58"mkostemp() reported failure: %s\n",59testnum++, oflags, strerror(errno));60return;61}62if (memcmp(tmpf, template, sizeof(tmpf) - 8 - 1) != 0) {63printf("not ok %d - oflags=%#x "64"returned pathname does not match template: %s\n",65testnum++, oflags, tmpf);66return;67}68do {69if (fcntl(fd, F_GETFD) !=70(oflags & O_CLOEXEC ? FD_CLOEXEC : 0)) {71printf("not ok %d - oflags=%#x "72"close-on-exec flag incorrect\n",73testnum++, oflags);74break;75}76if ((fcntl(fd, F_GETFL) & MISCFLAGS) != (oflags & MISCFLAGS)) {77printf("not ok %d - oflags=%#x "78"open flags incorrect\n",79testnum++, oflags);80break;81}82if (stat(tmpf, &st1) == -1) {83printf("not ok %d - oflags=%#x "84"cannot stat returned pathname %s: %s\n",85testnum++, oflags, tmpf, strerror(errno));86break;87}88if (fstat(fd, &st2) == -1) {89printf("not ok %d - oflags=%#x "90"cannot fstat returned fd %d: %s\n",91testnum++, oflags, fd, strerror(errno));92break;93}94if (!S_ISREG(st1.st_mode) || (st1.st_mode & 0777) != 0600 ||95st1.st_nlink != 1 || st1.st_size != 0) {96printf("not ok %d - oflags=%#x "97"named file attributes incorrect\n",98testnum++, oflags);99break;100}101if (!S_ISREG(st2.st_mode) || (st2.st_mode & 0777) != 0600 ||102st2.st_nlink != 1 || st2.st_size != 0) {103printf("not ok %d - oflags=%#x "104"opened file attributes incorrect\n",105testnum++, oflags);106break;107}108if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) {109printf("not ok %d - oflags=%#x "110"named and opened file do not match\n",111testnum++, oflags);112break;113}114(void)unlink(tmpf);115if (fstat(fd, &st2) == -1)116printf("not ok %d - oflags=%#x "117"cannot fstat returned fd %d again: %s\n",118testnum++, oflags, fd, strerror(errno));119else if (st2.st_nlink != 0)120printf("not ok %d - oflags=%#x "121"st_nlink is not 0 after unlink\n",122testnum++, oflags);123else124printf("ok %d - oflags=%#x\n", testnum++, oflags);125(void)close(fd);126return;127} while (0);128(void)close(fd);129(void)unlink(tmpf);130}131132ATF_TC_WITHOUT_HEAD(zero);133ATF_TC_BODY(zero, tc)134{135136test_one(0);137}138139ATF_TC_WITHOUT_HEAD(O_CLOEXEC);140ATF_TC_BODY(O_CLOEXEC, tc)141{142143test_one(O_CLOEXEC);144}145146ATF_TC_WITHOUT_HEAD(O_APPEND);147ATF_TC_BODY(O_APPEND, tc)148{149150test_one(O_APPEND);151}152153ATF_TC_WITHOUT_HEAD(O_APPEND__O_CLOEXEC);154ATF_TC_BODY(O_APPEND__O_CLOEXEC, tc)155{156157test_one(O_APPEND|O_CLOEXEC);158}159160ATF_TC_WITHOUT_HEAD(bad_flags);161ATF_TC_BODY(bad_flags, tc)162{163164char tmpf[sizeof(template)];165166memcpy(tmpf, template, sizeof(tmpf));167ATF_REQUIRE_MSG(mkostemp(tmpf, O_CREAT) == -1,168"mkostemp(O_CREAT) succeeded unexpectedly");169}170171ATF_TP_ADD_TCS(tp)172{173174ATF_TP_ADD_TC(tp, zero);175ATF_TP_ADD_TC(tp, O_CLOEXEC);176ATF_TP_ADD_TC(tp, O_APPEND);177ATF_TP_ADD_TC(tp, O_APPEND__O_CLOEXEC);178ATF_TP_ADD_TC(tp, bad_flags);179180return (atf_no_error());181}182183184