os161-1.99
 All Data Structures
common_buf.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 transfer buffers
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 int buf_fd;
00048 
00049 struct buftest {
00050         int (*setup)(void);
00051         int (*op)(void *);
00052         void (*cleanup)(void);
00053         const char *name;
00054 };
00055 
00056 ////////////////////////////////////////////////////////////
00057 
00058 static
00059 int
00060 read_setup(void)
00061 {
00062         buf_fd = open_testfile("i do not like green eggs and ham");
00063         if (buf_fd<0) {
00064                 return -1;
00065         }
00066         return 0;
00067 }
00068 
00069 static
00070 int
00071 read_badbuf(void *buf)
00072 {
00073         return read(buf_fd, buf, 128);
00074 }
00075 
00076 static
00077 void
00078 read_cleanup(void)
00079 {
00080         close(buf_fd);
00081         remove(TESTFILE);
00082 }
00083 
00084 //////////
00085 
00086 static
00087 int
00088 write_setup(void)
00089 {
00090         buf_fd = open_testfile(NULL);
00091         if (buf_fd<0) {
00092                 return -1;
00093         }
00094         return 0;
00095 }
00096 
00097 static
00098 int
00099 write_badbuf(void *ptr)
00100 {
00101         return write(buf_fd, ptr, 128);
00102 }
00103 
00104 static
00105 void
00106 write_cleanup(void)
00107 {
00108         close(buf_fd);
00109         remove(TESTFILE);
00110 }
00111 
00112 //////////
00113 
00114 static
00115 int
00116 getdirentry_setup(void)
00117 {
00118         buf_fd = open(".", O_RDONLY);
00119         if (buf_fd < 0) {
00120                 warn("UH-OH: couldn't open .");
00121                 return -1;
00122         }
00123         return 0;
00124 }
00125 
00126 static
00127 int
00128 getdirentry_badbuf(void *ptr)
00129 {
00130         return getdirentry(buf_fd, ptr, 1024);
00131 }
00132 
00133 static
00134 void
00135 getdirentry_cleanup(void)
00136 {
00137         close(buf_fd);
00138 }
00139 
00140 //////////
00141 
00142 static
00143 int
00144 readlink_setup(void)
00145 {
00146         return create_testlink();
00147 }
00148 
00149 static
00150 int
00151 readlink_badbuf(void *buf)
00152 {
00153         return readlink(TESTLINK, buf, 168);
00154 }
00155 
00156 static
00157 void
00158 readlink_cleanup(void)
00159 {
00160         remove(TESTLINK);
00161 }
00162 
00163 //////////
00164 
00165 static int getcwd_setup(void) { return 0; }
00166 static void getcwd_cleanup(void) {}
00167 
00168 static
00169 int
00170 getcwd_badbuf(void *buf)
00171 {
00172         return __getcwd(buf, 408);
00173 }
00174 
00175 ////////////////////////////////////////////////////////////
00176 
00177 static
00178 void
00179 common_badbuf(struct buftest *info, void *buf, const char *bufdesc)
00180 {
00181         char mydesc[128];
00182         int rv;
00183 
00184         snprintf(mydesc, sizeof(mydesc), "%s with %s buffer", 
00185                  info->name, bufdesc);
00186         info->setup();
00187         rv = info->op(buf);
00188         report_test(rv, errno, EFAULT, mydesc);
00189         info->cleanup();
00190 }
00191 
00192 static
00193 void
00194 any_badbuf(struct buftest *info)
00195 {
00196         common_badbuf(info, NULL, "NULL");
00197         common_badbuf(info, INVAL_PTR, "invalid");
00198         common_badbuf(info, KERN_PTR, "kernel-space");
00199 }
00200 
00201 ////////////////////////////////////////////////////////////
00202 
00203 #define T(call) \
00204   void                                  \
00205   test_##call##_buf(void)               \
00206   {                                     \
00207         static struct buftest info = {  \
00208                 call##_setup,           \
00209                 call##_badbuf,          \
00210                 call##_cleanup,         \
00211                 #call,                  \
00212         };                              \
00213         any_badbuf(&info);              \
00214   }
00215 
00216 T(read);
00217 T(write);
00218 T(getdirentry);
00219 T(readlink);
00220 T(getcwd);
 All Data Structures