/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.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* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* Calls with invalid pathnames31*/3233#include <sys/types.h>34#include <sys/stat.h>35#include <stdio.h>36#include <stdlib.h>37#include <string.h>38#include <unistd.h>39#include <limits.h>40#include <errno.h>41#include <err.h>4243#include "config.h"44#include "test.h"4546static47int48open_badpath(const char *path)49{50return open(path, O_RDONLY);51}5253static54int55remove_badpath(const char *path)56{57return remove(path);58}5960static61int62rename_badpath1(const char *path)63{64return rename(path, TESTFILE);65}6667static68int69rename_badpath2(const char *path)70{71return rename(TESTFILE, path);72}7374static75int76link_badpath1(const char *path)77{78return link(path, TESTFILE);79}8081static82int83link_badpath2(const char *path)84{85return link(TESTFILE, path);86}8788static89int90mkdir_badpath(const char *path)91{92return mkdir(path, 0775);93}9495static96int97rmdir_badpath(const char *path)98{99return rmdir(path);100}101102static103int104chdir_badpath(const char *path)105{106return chdir(path);107}108109static110int111symlink_badpath1(const char *path)112{113return symlink(path, TESTFILE);114}115116static117int118symlink_badpath2(const char *path)119{120return symlink(TESTFILE, path);121}122123static124int125readlink_badpath(const char *path)126{127char buf[128];128return readlink(path, buf, sizeof(buf));129}130131static132int133lstat_badpath(const char *name)134{135struct stat sb;136return lstat(name, &sb);137}138139static140int141stat_badpath(const char *name)142{143struct stat sb;144return stat(name, &sb);145}146147////////////////////////////////////////////////////////////148149static150void151common_badpath(int (*func)(const char *path), int mk, int rm, const char *path,152const char *call, const char *pathdesc)153{154char mydesc[128];155int rv;156157if (mk) {158if (create_testfile()<0) {159return;160}161}162163snprintf(mydesc, sizeof(mydesc), "%s with %s path", call, pathdesc);164rv = func(path);165report_test(rv, errno, EFAULT, mydesc);166167if (mk || rm) {168remove(TESTFILE);169}170}171172static173void174any_badpath(int (*func)(const char *path), const char *call, int mk, int rm)175{176common_badpath(func, mk, rm, NULL, call, "NULL");177common_badpath(func, mk, rm, INVAL_PTR, call, "invalid-pointer");178common_badpath(func, mk, rm, KERN_PTR, call, "kernel-pointer");179}180181////////////////////////////////////////////////////////////182183/* functions with one pathname */184#define T(call) \185void \186test_##call##_path(void) \187{ \188any_badpath(call##_badpath, #call, 0, 0); \189}190191T(open);192T(remove);193T(mkdir);194T(rmdir);195T(chdir);196T(readlink);197T(stat);198T(lstat);199200/* functions with two pathnames */201#define T2(call) \202void \203test_##call##_paths(void) \204{ \205any_badpath(call##_badpath1, #call "(arg1)", 0, 1); \206any_badpath(call##_badpath2, #call "(arg2)", 1, 1); \207}208209T2(rename);210T2(link);211T2(symlink);212213214