yaffs Added some more tests to direct/timothy_tests/quick_tests
[yaffs2.git] / direct / tests / yaffs_test.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  */
14
15
16
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <signal.h>
23 #include <stdlib.h>
24
25 #include "yaffsfs.h"
26
27 #include "yaffs_trace.h"
28
29 #include "nor_stress.h"
30 #include "yaffs_fsx.h"
31
32 void (*ext_fatal)(void);
33
34
35 int random_seed;
36 int simulate_power_failure = 0;
37
38
39 int do_fsx;
40 int do_bash_around;
41
42 int init_test;
43 int do_upgrade;
44 int n_cycles = -1;
45 int fuzz_test=0;
46
47
48 int yaffs_test_max_mallocs;
49
50 extern int ops_multiplier;
51
52 char mount_point[200];
53
54 #define BASH_HANDLES 20
55 void yaffs_bash_around(const char *mountpt, int n_cycles)
56 {
57         char name[200];
58         char name1[200];
59         int  h[BASH_HANDLES];
60         int i;
61         int op;
62         int pos;
63         int n;
64         int n1;
65         int start_op;
66         int op_max = 99;
67         
68         int cycle = 0;
69         
70         sprintf(name,"%s/bashdir",mountpt);
71         
72         yaffs_mkdir(name,0666);
73
74         for(i = 0; i < BASH_HANDLES; i++)
75                 h[i] = -1;
76                 
77         while(n_cycles){
78                 if(cycle % 100 == 0){
79                         printf("CYCLE %8d mo %2d inodes %d space %d ",cycle,op_max,
80                                 yaffs_inodecount(mountpt),(int)yaffs_freespace(mountpt));
81                         for(i = 0; i < BASH_HANDLES; i++)
82                                 printf("%2d ",h[i]);
83                         printf("\n");
84                 }
85                 cycle++;
86
87                 if(n_cycles > 0)
88                         n_cycles--;
89                 i = rand() % BASH_HANDLES;
90                 op = rand() % op_max;
91                 pos = rand() & 20000000;
92                 n = rand() % 100;
93                 n1 = rand() % 100;
94                 
95                 sprintf(name,"%s/bashdir/xx%d",mountpt,n);
96                 sprintf(name1,"%s/bashdir/xx%d",mountpt,n1);
97
98                 start_op = op;
99                 
100                 op-=1;
101                 if(op < 0){
102                         if(h[i]>= 0){
103                                 yaffs_close(h[i]);
104                                 h[i] = -1;
105                         }
106                         continue;
107                 }
108                 op-=1;
109                 if(op < 0){
110                         if(h[i] < 0)
111                                 h[i] = yaffs_open(name,O_CREAT| O_RDWR, 0666);
112                         continue;
113                 }
114
115                 op-=5;
116                 if(op< 0){
117                         yaffs_lseek(h[i],pos,SEEK_SET);
118                         yaffs_write(h[i],name,n);
119                         continue;
120                 }
121                 op-=1;
122                 if(op < 0){
123                         yaffs_unlink(name);
124                         continue;
125                 }
126                 op-=1;
127                 if(op < 0){
128                         yaffs_rename(name,name1);
129                         continue;
130                 }
131                 op-=1;
132                 if(op < 0){
133                         yaffs_mkdir(name,0666);
134                         continue;
135                 }
136                 op-=1;
137                 if(op < 0){
138                         yaffs_rmdir(name);
139                         continue;
140                 }
141                 
142                 op_max = start_op-op;
143         }
144                 
145         
146 }
147
148 void BadUsage(void)
149 {
150         printf("Usage: yaffs_test [options] mountpoint\n");
151         printf("options\n");
152         printf(" f: do fsx testing\n");
153         printf(" i: initialise for upgrade testing\n");
154         printf(" l: multiply number of operations by 5\n");
155         printf(" m: random max number of mallocs\n");
156         printf(" n nnn: number of cycles\n");
157         printf(" p: simulate power fail testing\n");
158         printf(" s sss: set seed\n");
159         printf(" u: do upgrade test\n");
160         printf(" b: bash-about test\n");
161         printf(" z: fuzz test - ignore verification errors\n");
162         exit(2);
163 }
164
165 int sleep_exit = 0;
166 int sleep_time = 0;
167 void test_fatal(void)
168 {
169         printf("fatal yaffs test pid %d sleeping\n",getpid());
170         while(!sleep_exit){
171                 sleep(1);
172                 sleep_time++;
173         }
174         
175         signal(SIGSEGV,SIG_IGN);
176         signal(SIGBUS,SIG_IGN);
177         signal(SIGABRT,SIG_IGN);
178         sleep_exit = 0;
179         
180 }
181
182 void bad_ptr_handler(int sig)
183 {
184         printf("signal %d received\n",sig);
185         test_fatal();
186 }
187
188 int main(int argc, char **argv)
189 {
190         int ch;
191         int random_mallocs=0;
192         ext_fatal = test_fatal;
193
194 #if 1
195         signal(SIGSEGV,bad_ptr_handler);
196         signal(SIGBUS,bad_ptr_handler);
197         signal(SIGABRT,bad_ptr_handler);
198 #endif  
199         while ((ch = getopt(argc,argv, "bfilmn:ps:t:uz"))
200                != EOF)
201                 switch (ch) {
202                 case 's':
203                         random_seed = atoi(optarg);
204                         break;
205                 case 'p':
206                         simulate_power_failure =1;
207                         break;
208                 case 'i':
209                         init_test = 1;
210                         break;
211                 case 'b':
212                         do_bash_around = 1;
213                         break;
214                 case 'f':
215                         do_fsx = 1;
216                         break;
217                 case 'l':
218                         ops_multiplier *= 5;
219                         break;
220                 case 'u':
221                         do_upgrade = 1;
222                         break;
223                 case 'm':
224                         random_mallocs=1;
225                         break;
226                 case 'n':
227                         n_cycles = atoi(optarg);
228                         break;
229                 case 't':
230                         yaffs_trace_mask = strtol(optarg,NULL,0);
231                         break;
232                 case 'z':fuzz_test=1;
233                         break;
234                 default:
235                         BadUsage();
236                         /* NOTREACHED */
237                 }
238         argc -= optind;
239         argv += optind;
240         
241         if(random_mallocs){
242                 yaffs_test_max_mallocs = 0xFFF & random_seed;
243         }
244         
245         if(argc == 1) {
246                 int result;
247
248                 strcpy(mount_point,argv[0]);
249                 
250                 if(simulate_power_failure)
251                         n_cycles = -1;
252                 printf("Running test %s %s %s %s %s seed %d cycles %d\n",
253                         do_upgrade ? "fw_upgrade" : "",
254                         init_test ? "initialise":"",
255                         fuzz_test ? "fuzz-test" : "",
256                         do_fsx ? "fsx" :"",
257                         simulate_power_failure ? "power_fail" : "",
258                         random_seed, n_cycles);
259
260                 yaffs_start_up();
261                 result = yaffs_mount(mount_point);
262                 if(result < 0){
263                         printf("Mount of %s failed\n",mount_point);
264                         printf("pid %d sleeping\n",getpid());
265                         
266                         while(!sleep_exit){
267                                 sleep(1);
268                                 sleep_time++;
269                         }
270                 }
271                 printf("Mount complete\n");
272                         
273                 if(do_upgrade && init_test){
274                         simulate_power_failure = 0;
275                         NorStressTestInitialise(mount_point);
276                 } else if(do_upgrade){
277                         printf("Running stress on %s with seed %d\n",mount_point,random_seed);
278                         NorStressTestRun(mount_point,n_cycles,do_fsx,fuzz_test);
279                 } else if(do_fsx){
280                         yaffs_fsx_main(mount_point,n_cycles);
281                 } else if(do_bash_around){
282                         yaffs_bash_around(mount_point,n_cycles);
283                 }else {
284                         printf("No test to run!\n");
285                         BadUsage();
286                 }
287                 yaffs_unmount(mount_point);
288                 
289                 printf("Test run completed!\n");
290         }
291         else
292                 BadUsage();
293         return 0;
294 }
295
296