os161-1.99
 All Data Structures
common_path.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Calls with invalid pathnames
00032  */
00033 
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <unistd.h>
00040 #include <limits.h>
00041 #include <errno.h>
00042 #include <err.h>
00043 
00044 #include "config.h"
00045 #include "test.h"
00046 
00047 static
00048 int
00049 open_badpath(const char *path)
00050 {
00051         return open(path, O_RDONLY);
00052 }
00053 
00054 static
00055 int
00056 remove_badpath(const char *path)
00057 {
00058         return remove(path);
00059 }
00060 
00061 static
00062 int
00063 rename_badpath1(const char *path)
00064 {
00065         return rename(path, TESTFILE);
00066 }
00067 
00068 static
00069 int
00070 rename_badpath2(const char *path)
00071 {
00072         return rename(TESTFILE, path);
00073 }
00074 
00075 static
00076 int
00077 link_badpath1(const char *path)
00078 {
00079         return link(path, TESTFILE);
00080 }
00081 
00082 static
00083 int
00084 link_badpath2(const char *path)
00085 {
00086         return link(TESTFILE, path);
00087 }
00088 
00089 static
00090 int
00091 mkdir_badpath(const char *path)
00092 {
00093         return mkdir(path, 0775);
00094 }
00095 
00096 static
00097 int
00098 rmdir_badpath(const char *path)
00099 {
00100         return rmdir(path);
00101 }
00102 
00103 static
00104 int
00105 chdir_badpath(const char *path)
00106 {
00107         return chdir(path);
00108 }
00109 
00110 static
00111 int
00112 symlink_badpath1(const char *path)
00113 {
00114         return symlink(path, TESTFILE);
00115 }
00116 
00117 static
00118 int
00119 symlink_badpath2(const char *path)
00120 {
00121         return symlink(TESTFILE, path);
00122 }
00123 
00124 static
00125 int
00126 readlink_badpath(const char *path)
00127 {
00128         char buf[128];
00129         return readlink(path, buf, sizeof(buf));
00130 }
00131 
00132 static
00133 int
00134 lstat_badpath(const char *name)
00135 {
00136         struct stat sb;
00137         return lstat(name, &sb);
00138 }
00139 
00140 static
00141 int
00142 stat_badpath(const char *name)
00143 {
00144         struct stat sb;
00145         return stat(name, &sb);
00146 }
00147 
00148 ////////////////////////////////////////////////////////////
00149 
00150 static
00151 void
00152 common_badpath(int (*func)(const char *path), int mk, int rm, const char *path,
00153                const char *call, const char *pathdesc)
00154 {
00155         char mydesc[128];
00156         int rv;
00157 
00158         if (mk) {
00159                 if (create_testfile()<0) {
00160                         return;
00161                 }
00162         }
00163 
00164         snprintf(mydesc, sizeof(mydesc), "%s with %s path", call, pathdesc);
00165         rv = func(path);
00166         report_test(rv, errno, EFAULT, mydesc);
00167 
00168         if (mk || rm) {
00169                 remove(TESTFILE);
00170         }
00171 }
00172 
00173 static
00174 void
00175 any_badpath(int (*func)(const char *path), const char *call, int mk, int rm)
00176 {
00177         common_badpath(func, mk, rm, NULL, call, "NULL");
00178         common_badpath(func, mk, rm, INVAL_PTR, call, "invalid-pointer");
00179         common_badpath(func, mk, rm, KERN_PTR, call, "kernel-pointer");
00180 }
00181 
00182 ////////////////////////////////////////////////////////////
00183 
00184 /* functions with one pathname */
00185 #define T(call) \
00186   void                                  \
00187   test_##call##_path(void)              \
00188   {                                     \
00189         any_badpath(call##_badpath, #call, 0, 0); \
00190   }
00191 
00192 T(open);
00193 T(remove);
00194 T(mkdir);
00195 T(rmdir);
00196 T(chdir);
00197 T(readlink);
00198 T(stat);
00199 T(lstat);
00200 
00201 /* functions with two pathnames */
00202 #define T2(call) \
00203   void                                  \
00204   test_##call##_paths(void)             \
00205   {                                     \
00206         any_badpath(call##_badpath1, #call "(arg1)", 0, 1); \
00207         any_badpath(call##_badpath2, #call "(arg2)", 1, 1); \
00208   }
00209 
00210 T2(rename);
00211 T2(link);
00212 T2(symlink);
 All Data Structures