index: fix copy/paste error
[mit.git] / testing.h
blob54573a9855031d9e3f8895c80154f294baced745
1 #ifndef _TESTING_H
2 #define _TESTING_H
4 /* Testing code. */
5 #ifdef JUST_TESTING
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <stdarg.h>
13 #include <sys/utsname.h>
14 #include <asm/unistd.h>
15 #include <sys/types.h>
16 #include <dirent.h>
18 /* We don't use all of these. */
19 static int modtest_uname(struct utsname *buf) __attribute__((unused));
20 static long modtest_create_module(const char *name, size_t size)
21 __attribute__((unused));
22 static void *modtest_fopen(const char *path, const char *mode)
23 __attribute__((unused));
24 static int modtest_open(const char *path, int flags, mode_t mode)
25 __attribute__((unused));
26 static int modtest_stat(const char *file_name, struct stat *buf)
27 __attribute__((unused));
28 static int modtest_lstat(const char *file_name, struct stat *buf)
29 __attribute__((unused));
30 static DIR *modtest_opendir(const char *name) __attribute__((unused));
31 static int modtest_system(const char *string) __attribute__((unused));
32 static int modtest_rename(const char *oldpath, const char *newpath)
33 __attribute__((unused));
34 static long modtest_init_module(void *map, unsigned long size,
35 const char *optstring) __attribute__((unused));
36 static long modtest_delete_module(const char *modname, unsigned int flags)
37 __attribute__((unused));
39 static int modtest_readlink(const char *path, char *buf, size_t bufsiz)
40 __attribute__((unused));
42 static int modtest_uname(struct utsname *buf)
44 char *release = NULL;
46 strcpy(buf->sysname, "Linux");
47 strcpy(buf->nodename, "fakenodename");
48 if ((release = getenv("MODTEST_UNAME")))
49 strcpy(buf->release, release);
50 else {
51 printf("MODTEST_OVERRIDE used but MODTEST_UNAME not set.\n");
52 exit(1);
54 strcpy(buf->version, "Fakeversion");
55 strcpy(buf->machine, "fakemachine");
56 return 0;
59 static long modtest_create_module(const char *name, size_t size)
61 if (getenv("MODTEST_DO_CREATE_MODULE"))
62 return 0;
63 errno = ENOSYS;
64 return -1;
67 static long modtest_init_module(void *map, unsigned long size,
68 const char *optstring)
70 if (getenv("MODPROBE_WAIT")) {
71 int fd;
72 const char *file = getenv("MODPROBE_WAIT");
74 printf("Looping on %s\n", file);
75 fflush(stdout);
76 while ((fd = open(file, O_RDONLY)) < 0)
77 sleep(1);
78 close(fd);
79 printf("Removing %s\n", file);
80 unlink(file);
82 if (getenv("MODTEST_DUMP_INIT")) {
83 while (size) {
84 int ret;
85 ret = write(2, map, size);
86 if (ret < 0) exit(1);
87 size -= ret;
88 map += ret;
90 } else
91 printf("INIT_MODULE: %lu %s\n", size, optstring);
93 return 0;
96 static long modtest_delete_module(const char *modname, unsigned int flags)
98 char flagnames[100];
100 if (getenv("MODPROBE_WAIT")) {
101 int fd;
102 const char *file = getenv("MODPROBE_WAIT");
104 printf("Looping on %s\n", file);
105 fflush(stdout);
106 while ((fd = open(file, O_RDONLY)) < 0)
107 sleep(1);
108 close(fd);
109 printf("Removing %s\n", file);
110 fflush(stdout);
111 unlink(file);
113 flagnames[0] = '\0';
114 if (flags & O_EXCL)
115 strcat(flagnames, "EXCL ");
116 if (flags & O_TRUNC)
117 strcat(flagnames, "TRUNC ");
118 if (flags & O_NONBLOCK)
119 strcat(flagnames, "NONBLOCK ");
120 if (flags & ~(O_EXCL|O_TRUNC|O_NONBLOCK))
121 strcat(flagnames, "UNKNOWN ");
123 printf("DELETE_MODULE: %s %s\n", modname, flagnames);
124 return 0;
127 static const char *modtest_mapname(const char *path)
129 unsigned int i;
130 char envname[64];
132 for (i = 0; ; i++) {
133 char *name;
134 sprintf(envname, "MODTEST_OVERRIDE%u", i);
135 name = getenv(envname);
136 if (!name)
137 break;
139 if (strcmp(path, name) == 0) {
140 sprintf(envname, "MODTEST_OVERRIDE_WITH%u", i);
141 return getenv(envname);
144 return path;
147 static void *modtest_fopen(const char *path, const char *mode)
149 return fopen(modtest_mapname(path), mode);
152 static int modtest_open(const char *path, int flags, mode_t mode)
154 return open(modtest_mapname(path), flags, mode);
157 static int modtest_stat(const char *file_name, struct stat *buf)
159 return stat(modtest_mapname(file_name), buf);
162 static int modtest_lstat(const char *file_name, struct stat *buf)
164 return lstat(modtest_mapname(file_name), buf);
167 static DIR *modtest_opendir(const char *name)
169 return opendir(modtest_mapname(name));
172 static int modtest_system(const char *string)
174 if (getenv("MODTEST_DO_SYSTEM"))
175 return system(string);
176 printf("SYSTEM: %s\n", string);
177 return 0;
180 static int modtest_rename(const char *oldpath, const char *newpath)
182 return rename(modtest_mapname(oldpath), modtest_mapname(newpath));
185 static int modtest_readlink(const char *path, char *buf, size_t bufsiz)
187 return readlink(modtest_mapname(path), buf, bufsiz);
190 #ifdef CONFIG_USE_ZLIB
191 #include <zlib.h>
192 static gzFile *modtest_gzopen(const char *filename, const char *mode)
193 __attribute__((unused));
195 static gzFile *modtest_gzopen(const char *filename, const char *mode)
197 return gzopen(modtest_mapname(filename), mode);
199 #endif
201 /* create_module call */
202 #undef create_module
203 #define create_module modtest_create_module
205 #define uname modtest_uname
206 #define delete_module modtest_delete_module
207 #define init_module modtest_init_module
208 #define open modtest_open
209 #define fopen modtest_fopen
210 #define stat(name, ptr) modtest_stat(name, ptr)
211 #define lstat(name, ptr) modtest_lstat(name, ptr)
212 #define opendir modtest_opendir
213 #define system modtest_system
214 #define rename modtest_rename
215 #define readlink modtest_readlink
216 #define gzopen modtest_gzopen
218 #endif /* JUST_TESTING */
219 #endif /* _TESTING_H */