tizen 2.0
[external/module-init-tools.git] / testing.h
1 #ifndef _TESTING_H
2 #define _TESTING_H
3
4 /* Testing code. */
5 #ifdef JUST_TESTING
6
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 <sys/stat.h>
17 #include <fcntl.h>
18 #include <dirent.h>
19 #include <time.h>
20
21 /* We don't use all of these. */
22 static int modtest_uname(struct utsname *buf) __attribute__((unused));
23 static long modtest_create_module(const char *name, size_t size)
24 __attribute__((unused));
25 static void *modtest_fopen(const char *path, const char *mode)
26 __attribute__((unused));
27 static int modtest_open(const char *path, int flags, mode_t mode)
28 __attribute__((unused));
29 static int modtest_stat(const char *file_name, struct stat *buf)
30 __attribute__((unused));
31 static int modtest_lstat(const char *file_name, struct stat *buf)
32 __attribute__((unused));
33 static DIR *modtest_opendir(const char *name) __attribute__((unused));
34 static int modtest_system(const char *string) __attribute__((unused));
35 static int modtest_rename(const char *oldpath, const char *newpath)
36 __attribute__((unused));
37 static long modtest_init_module(void *map, unsigned long size,
38                                 const char *optstring) __attribute__((unused));
39 static long modtest_delete_module(const char *modname, unsigned int flags)
40 __attribute__((unused));
41
42 static int modtest_readlink(const char *path, char *buf, size_t bufsiz)
43 __attribute__((unused));
44
45 static int modtest_uname(struct utsname *buf)
46 {
47         char *release = NULL;
48
49         strcpy(buf->sysname, "Linux");
50         strcpy(buf->nodename, "fakenodename");
51         if ((release = getenv("MODTEST_UNAME")))
52                 strcpy(buf->release, release);
53         else {
54                 printf("MODTEST_OVERRIDE_ROOT used but MODTEST_UNAME not set.\n");
55                 exit(1);
56         }
57         strcpy(buf->version, "Fakeversion");
58         strcpy(buf->machine, "fakemachine");
59         return 0;
60 }
61
62 static long modtest_create_module(const char *name, size_t size)
63 {
64         if (getenv("MODTEST_DO_CREATE_MODULE"))
65                 return 0;
66         errno = ENOSYS;
67         return -1;
68 }
69
70 static const struct timespec modtest_delay = {
71         .tv_sec = 0,
72         .tv_nsec = 500 * 1000 * 1000
73 };
74
75 static long modtest_init_module(void *map, unsigned long size,
76                                 const char *optstring)
77 {
78
79         if (getenv("MODPROBE_WAIT")) {
80                 int fd;
81                 const char *file = getenv("MODPROBE_WAIT");
82
83                 printf("Looping on %s\n", file);
84                 fflush(stdout);
85                 while ((fd = open(file, O_RDONLY)) < 0)
86                         nanosleep(&modtest_delay, NULL);
87                 close(fd);
88                 printf("Removing %s\n", file);
89                 unlink(file);
90         }
91         if (getenv("MODTEST_DUMP_INIT")) {
92                 while (size) {
93                         int ret;
94                         ret = write(2, map, size);
95                         if (ret < 0) exit(1);
96                         size -= ret;
97                         map += ret;
98                 }
99         } else          
100                 printf("INIT_MODULE: %lu %s\n", size, optstring);
101         
102         return 0;
103 }
104
105 static long modtest_delete_module(const char *modname, unsigned int flags)
106 {
107         char flagnames[100];
108
109         if (getenv("MODPROBE_WAIT")) {
110                 int fd;
111                 const char *file = getenv("MODPROBE_WAIT");
112
113                 printf("Looping on %s\n", file);
114                 fflush(stdout);
115                 while ((fd = open(file, O_RDONLY)) < 0)
116                         nanosleep(&modtest_delay, NULL);
117                 close(fd);
118                 printf("Removing %s\n", file);
119                 fflush(stdout);
120                 unlink(file);
121         }
122         flagnames[0] = '\0';
123         if (flags & O_EXCL)
124                 strcat(flagnames, "EXCL ");
125         if (flags & O_TRUNC)
126                 strcat(flagnames, "TRUNC ");
127         if (flags & O_NONBLOCK)
128                 strcat(flagnames, "NONBLOCK ");
129         if (flags & ~(O_EXCL|O_TRUNC|O_NONBLOCK))
130                 strcat(flagnames, "UNKNOWN ");
131
132         printf("DELETE_MODULE: %s %s\n", modname, flagnames);
133         return 0;
134 }
135
136 /* Add prefix to absolute paths; relative paths are left unchanged */
137 static const char *modtest_mapname(const char *path, char *buf, size_t buflen)
138 {
139         char *root; 
140
141         if (path[0] != '/')
142                 return path;
143
144         root = getenv("MODTEST_OVERRIDE_ROOT");
145         if (!root)
146                 return path;
147
148         snprintf(buf, buflen, "%s%s", root, path);
149         return buf;
150 }
151
152 static void *modtest_fopen(const char *path, const char *mode)
153 {
154         char path_buf[PATH_MAX];
155
156         path = modtest_mapname(path, path_buf, sizeof(path_buf));
157         return fopen(path, mode);
158 }
159
160 static int modtest_open(const char *path, int flags, mode_t mode)
161 {
162         char path_buf[PATH_MAX];
163
164         path = modtest_mapname(path, path_buf, sizeof(path_buf));
165         return open(path, flags, mode);
166 }
167
168 static int modtest_stat(const char *path, struct stat *buf)
169 {
170         char path_buf[PATH_MAX];
171
172         path = modtest_mapname(path, path_buf, sizeof(path_buf));
173         return stat(path, buf);
174 }
175
176 static int modtest_lstat(const char *path, struct stat *buf)
177 {
178         char path_buf[PATH_MAX];
179
180         path = modtest_mapname(path, path_buf, sizeof(path_buf));
181         return lstat(path, buf);
182 }
183
184 static DIR *modtest_opendir(const char *path)
185 {
186         char path_buf[PATH_MAX];
187
188         path = modtest_mapname(path, path_buf, sizeof(path_buf));
189         return opendir(path);
190 }
191
192 static int modtest_system(const char *string)
193 {
194         if (getenv("MODTEST_DO_SYSTEM"))
195                 return system(string);
196         printf("SYSTEM: %s\n", string);
197         return 0;
198 }
199
200 static int modtest_rename(const char *oldpath, const char *newpath)
201 {
202         char oldpath_buf[PATH_MAX];
203         char newpath_buf[PATH_MAX];
204
205         oldpath = modtest_mapname(oldpath, oldpath_buf, sizeof(oldpath_buf));
206         newpath = modtest_mapname(newpath, newpath_buf, sizeof(newpath_buf));
207         return rename(oldpath, newpath);
208 }
209
210 static int modtest_readlink(const char *path, char *buf, size_t bufsiz)
211 {
212         char path_buf[PATH_MAX];
213
214         path = modtest_mapname(path, path_buf, sizeof(path_buf));
215         return readlink(path, buf, bufsiz);
216 }
217
218 #ifdef CONFIG_USE_ZLIB
219 #include <zlib.h>
220 static gzFile *modtest_gzopen(const char *path, const char *mode)
221 __attribute__((unused));
222
223 static gzFile *modtest_gzopen(const char *path, const char *mode)
224 {
225         char path_buf[PATH_MAX];
226
227         path = modtest_mapname(path, path_buf, sizeof(path_buf));
228         return gzopen(path, mode);
229 }
230 #endif
231
232 /* create_module call */
233 #undef create_module
234 #define create_module modtest_create_module
235
236 #define uname modtest_uname
237 #define delete_module modtest_delete_module
238 #define init_module modtest_init_module
239 #define open modtest_open
240 #define fopen modtest_fopen
241 #define stat(name, ptr) modtest_stat(name, ptr)
242 #define lstat(name, ptr) modtest_lstat(name, ptr)
243 #define opendir modtest_opendir
244 #define system modtest_system
245 #define rename modtest_rename
246 #define readlink modtest_readlink
247 #define gzopen modtest_gzopen
248
249 #endif /* JUST_TESTING */
250 #endif /* _TESTING_H */
251