1*63d1fd59SEnji Cooper /* $NetBSD: t_rename.c,v 1.3 2017/01/13 21:30:41 christos Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*
457718be8SEnji Cooper * Copyright (c) 2001, 2008, 2010 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
857718be8SEnji Cooper * modification, are permitted provided that the following conditions
957718be8SEnji Cooper * are met:
1057718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1157718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1257718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1357718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1457718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1557718be8SEnji Cooper *
1657718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1757718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1857718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1957718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2057718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2157718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2257718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2357718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2457718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2557718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2657718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
2757718be8SEnji Cooper */
2857718be8SEnji Cooper #define _NETBSD_SOURCE /* strlcat/random */
2957718be8SEnji Cooper #include <sys/cdefs.h>
3057718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2008, 2010\
3157718be8SEnji Cooper The NetBSD Foundation, inc. All rights reserved.");
32*63d1fd59SEnji Cooper __RCSID("$NetBSD: t_rename.c,v 1.3 2017/01/13 21:30:41 christos Exp $");
3357718be8SEnji Cooper
3457718be8SEnji Cooper #include <sys/types.h>
3557718be8SEnji Cooper #include <sys/stat.h>
3657718be8SEnji Cooper
3757718be8SEnji Cooper #include <errno.h>
3857718be8SEnji Cooper #include <fcntl.h>
3957718be8SEnji Cooper #include <stdio.h>
4057718be8SEnji Cooper #include <stdlib.h>
4157718be8SEnji Cooper #include <string.h>
4257718be8SEnji Cooper #include <unistd.h>
4357718be8SEnji Cooper
4457718be8SEnji Cooper #include <atf-c.h>
4557718be8SEnji Cooper
46*63d1fd59SEnji Cooper #include "h_macros.h"
4757718be8SEnji Cooper
4857718be8SEnji Cooper ATF_TC(rename);
ATF_TC_HEAD(rename,tc)4957718be8SEnji Cooper ATF_TC_HEAD(rename, tc)
5057718be8SEnji Cooper {
5157718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Checks rename(2)");
5257718be8SEnji Cooper }
ATF_TC_BODY(rename,tc)5357718be8SEnji Cooper ATF_TC_BODY(rename, tc)
5457718be8SEnji Cooper {
5557718be8SEnji Cooper struct stat sb;
5657718be8SEnji Cooper
5757718be8SEnji Cooper REQUIRE_LIBC(open("t1", O_CREAT | O_TRUNC | O_WRONLY, 0600), -1);
5857718be8SEnji Cooper REQUIRE_LIBC(link("t1", "t2"), -1);
5957718be8SEnji Cooper
6057718be8SEnji Cooper /* Check if rename to same name works as expected */
6157718be8SEnji Cooper REQUIRE_LIBC(rename("t1", "t1"), -1);
6257718be8SEnji Cooper
6357718be8SEnji Cooper /* Rename removed file? */
6457718be8SEnji Cooper REQUIRE_LIBC(stat("t1", &sb), -1);
6557718be8SEnji Cooper
6657718be8SEnji Cooper REQUIRE_LIBC(rename("t1", "t2"), -1);
6757718be8SEnji Cooper
6857718be8SEnji Cooper #if BSD_RENAME
6957718be8SEnji Cooper /* check if rename of hardlinked file works the BSD way */
7057718be8SEnji Cooper ATF_REQUIRE_MSG(stat("t1", &sb) != 0, "BSD rename should remove file t1");
7157718be8SEnji Cooper ATF_REQUIRE_EQ(errno, ENOENT);
7257718be8SEnji Cooper #else
7357718be8SEnji Cooper /* check if rename of hardlinked file works as the standard says */
7457718be8SEnji Cooper REQUIRE_LIBC(stat("t1", &sb), -1);
7557718be8SEnji Cooper #endif
7657718be8SEnji Cooper /* check if we get the expected error */
7757718be8SEnji Cooper /* this also exercises icky shared libraries goo */
7857718be8SEnji Cooper ATF_REQUIRE_MSG(rename("no/such/file/or/dir", "no/such/file/or/dir") != 0,
7957718be8SEnji Cooper "No error renaming nonexistent file");
8057718be8SEnji Cooper ATF_REQUIRE_EQ(errno, ENOENT);
8157718be8SEnji Cooper }
8257718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)8357718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
8457718be8SEnji Cooper {
8557718be8SEnji Cooper
8657718be8SEnji Cooper ATF_TP_ADD_TC(tp, rename);
8757718be8SEnji Cooper
8857718be8SEnji Cooper return atf_no_error();
8957718be8SEnji Cooper }
90