Home | History | Annotate | Line # | Download | only in c063
t_utimensat.c revision 1.8
      1 /*	$NetBSD: t_utimensat.c,v 1.8 2024/08/10 15:10:17 riastradh Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Emmanuel Dreyfus.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: t_utimensat.c,v 1.8 2024/08/10 15:10:17 riastradh Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/stat.h>
     36 #include <sys/time.h>
     37 #include <atf-c.h>
     38 #include <errno.h>
     39 #include <fcntl.h>
     40 #include <limits.h>
     41 #include <paths.h>
     42 #include <stdio.h>
     43 #include <string.h>
     44 #include <unistd.h>
     45 
     46 #include "h_macros.h"
     47 
     48 #define DIR "dir"
     49 #define FILE "dir/utimensat"
     50 #define BASEFILE "utimensat"
     51 #define LINK "dir/symlink"
     52 #define BASELINK "symlink"
     53 #define FILEERR "dir/symlink"
     54 
     55 static const struct timespec tptr[] = {
     56 	{ 0x12345678, 987654321 },
     57 	{ 0x15263748, 123456789 },
     58 };
     59 
     60 static void
     61 checkstattime(const struct stat *st)
     62 {
     63 
     64 	ATF_CHECK_EQ_MSG(st->st_atimespec.tv_sec, tptr[0].tv_sec,
     65 	    "st->st_atimespec.tv_sec=%lld tptr[0].tv_sec=%lld",
     66 	    (long long)st->st_atimespec.tv_sec, (long long)tptr[0].tv_sec);
     67 	ATF_CHECK_EQ_MSG(st->st_atimespec.tv_nsec, tptr[0].tv_nsec,
     68 	    "st->st_atimespec.tv_nsec=%ld tptr[0].tv_nsec=%ld",
     69 	    (long)st->st_atimespec.tv_nsec, (long)tptr[0].tv_nsec);
     70 	ATF_CHECK_EQ_MSG(st->st_mtimespec.tv_sec, tptr[1].tv_sec,
     71 	    "st->st_mtimespec.tv_sec=%lld tptr[1].tv_sec=%lld",
     72 	    (long long)st->st_mtimespec.tv_sec, (long long)tptr[1].tv_sec);
     73 	ATF_CHECK_EQ_MSG(st->st_mtimespec.tv_nsec, tptr[1].tv_nsec,
     74 	    "st->st_mtimespec.tv_nsec=%ld tptr[1].tv_nsec=%ld",
     75 	    (long)st->st_mtimespec.tv_nsec, (long)tptr[1].tv_nsec);
     76 }
     77 
     78 ATF_TC(utimensat_fd);
     79 ATF_TC_HEAD(utimensat_fd, tc)
     80 {
     81 	atf_tc_set_md_var(tc, "descr", "See that utimensat works with fd");
     82 }
     83 ATF_TC_BODY(utimensat_fd, tc)
     84 {
     85 	int dfd;
     86 	int fd;
     87 	struct stat st;
     88 
     89 	RL(mkdir(DIR, 0755));
     90 	RL(fd = open(FILE, O_CREAT|O_RDWR, 0644));
     91 	RL(close(fd));
     92 
     93 	RL(dfd = open(DIR, O_RDONLY, 0));
     94 	RL(utimensat(dfd, BASEFILE, tptr, 0));
     95 	RL(close(dfd));
     96 
     97 	RL(stat(FILE, &st));
     98 	checkstattime(&st);
     99 }
    100 
    101 ATF_TC(utimensat_fdcwd);
    102 ATF_TC_HEAD(utimensat_fdcwd, tc)
    103 {
    104 	atf_tc_set_md_var(tc, "descr",
    105 	    "See that utimensat works with fd as AT_FDCWD");
    106 }
    107 ATF_TC_BODY(utimensat_fdcwd, tc)
    108 {
    109 	int fd;
    110 	struct stat st;
    111 
    112 	RL(mkdir(DIR, 0755));
    113 	RL(fd = open(FILE, O_CREAT|O_RDWR, 0644));
    114 	RL(close(fd));
    115 
    116 	RL(chdir(DIR));
    117 	RL(utimensat(AT_FDCWD, BASEFILE, tptr, 0));
    118 
    119 	RL(stat(BASEFILE, &st));
    120 	checkstattime(&st);
    121 }
    122 
    123 ATF_TC(utimensat_fdcwderr);
    124 ATF_TC_HEAD(utimensat_fdcwderr, tc)
    125 {
    126 	atf_tc_set_md_var(tc, "descr",
    127 	    "See that utimensat fails with fd as AT_FDCWD and bad path");
    128 }
    129 ATF_TC_BODY(utimensat_fdcwderr, tc)
    130 {
    131 	RL(mkdir(DIR, 0755));
    132 	ATF_CHECK_ERRNO(ENOENT, utimensat(AT_FDCWD, FILEERR, tptr, 0) == -1);
    133 }
    134 
    135 ATF_TC(utimensat_fderr1);
    136 ATF_TC_HEAD(utimensat_fderr1, tc)
    137 {
    138 	atf_tc_set_md_var(tc, "descr",
    139 	    "See that utimensat fail with bad path");
    140 }
    141 ATF_TC_BODY(utimensat_fderr1, tc)
    142 {
    143 	int dfd;
    144 
    145 	RL(mkdir(DIR, 0755));
    146 	RL(dfd = open(DIR, O_RDONLY, 0));
    147 	ATF_CHECK_ERRNO(ENOENT, utimensat(dfd, FILEERR, tptr, 0) == -1);
    148 	RL(close(dfd));
    149 }
    150 
    151 ATF_TC(utimensat_fderr2);
    152 ATF_TC_HEAD(utimensat_fderr2, tc)
    153 {
    154 	atf_tc_set_md_var(tc, "descr",
    155 	    "See that utimensat fails with bad fdat");
    156 }
    157 ATF_TC_BODY(utimensat_fderr2, tc)
    158 {
    159 	int dfd;
    160 	int fd;
    161 	char cwd[MAXPATHLEN];
    162 
    163 	RL(mkdir(DIR, 0755));
    164 	RL(fd = open(FILE, O_CREAT|O_RDWR, 0644));
    165 	RL(close(fd));
    166 
    167 	RL(dfd = open(getcwd(cwd, MAXPATHLEN), O_RDONLY, 0));
    168 	ATF_CHECK_ERRNO(ENOENT, utimensat(dfd, BASEFILE, tptr, 0) == -1);
    169 	RL(close(dfd));
    170 }
    171 
    172 ATF_TC(utimensat_fderr3);
    173 ATF_TC_HEAD(utimensat_fderr3, tc)
    174 {
    175 	atf_tc_set_md_var(tc, "descr",
    176 	    "See that utimensat fails with fd as -1");
    177 }
    178 ATF_TC_BODY(utimensat_fderr3, tc)
    179 {
    180 	int fd;
    181 
    182 	RL(mkdir(DIR, 0755));
    183 	RL(fd = open(FILE, O_CREAT|O_RDWR, 0644));
    184 	RL(close(fd));
    185 
    186 	ATF_CHECK_ERRNO(EBADF, utimensat(-1, FILE, tptr, 0) == -1);
    187 }
    188 
    189 ATF_TC(utimensat_fdlink);
    190 ATF_TC_HEAD(utimensat_fdlink, tc)
    191 {
    192 	atf_tc_set_md_var(tc, "descr", "See that utimensat works on symlink");
    193 }
    194 ATF_TC_BODY(utimensat_fdlink, tc)
    195 {
    196 	int dfd;
    197 	struct stat st;
    198 
    199 	RL(mkdir(DIR, 0755));
    200 	RL(symlink(FILE, LINK)); /* NB: FILE does not exists */
    201 
    202 	RL(dfd = open(DIR, O_RDONLY, 0));
    203 
    204 	ATF_CHECK_ERRNO(ENOENT, utimensat(dfd, BASELINK, tptr, 0) == -1);
    205 
    206 	RL(utimensat(dfd, BASELINK, tptr, AT_SYMLINK_NOFOLLOW));
    207 
    208 	RL(close(dfd));
    209 
    210 	RL(lstat(LINK, &st));
    211 	checkstattime(&st);
    212 }
    213 
    214 ATF_TP_ADD_TCS(tp)
    215 {
    216 
    217 	ATF_TP_ADD_TC(tp, utimensat_fd);
    218 	ATF_TP_ADD_TC(tp, utimensat_fdcwd);
    219 	ATF_TP_ADD_TC(tp, utimensat_fdcwderr);
    220 	ATF_TP_ADD_TC(tp, utimensat_fderr1);
    221 	ATF_TP_ADD_TC(tp, utimensat_fderr2);
    222 	ATF_TP_ADD_TC(tp, utimensat_fderr3);
    223 	ATF_TP_ADD_TC(tp, utimensat_fdlink);
    224 
    225 	return atf_no_error();
    226 }
    227