More test code
[yaffs2.git] / direct / tests / yaffs_test.c
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
3  *
4  * Copyright (C) 2002 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 "nor_stress.h"
28 #include "yaffs_fsx.h"
29
30 void (*ext_fatal)(void);
31
32
33 int random_seed;
34 int simulate_power_failure = 0;
35
36
37 int do_fsx;
38 int do_bash_around;
39
40 int init_test;
41 int do_upgrade;
42 int n_cycles = -1;
43 int yaffs_test_maxMallocs;
44
45 extern int ops_multiplier;
46
47 char mount_point[200];
48
49 #define BASH_HANDLES 20
50 void yaffs_bash_around(const char *mountpt, int n_cycles)
51 {
52         char name[200];
53         char name1[200];
54         int  h[BASH_HANDLES];
55         int i;
56         int op;
57         int pos;
58         int n;
59         int n1;
60         int start_op;
61         int op_max = 99;
62         
63         int cycle = 0;
64         
65         sprintf(name,"%s/bashdir",mountpt);
66         
67         yaffs_mkdir(name,0666);
68
69         for(i = 0; i < BASH_HANDLES; i++)
70                 h[i] = -1;
71                 
72         while(n_cycles){
73                 if(cycle % 100 == 0){
74                         printf("CYCLE %8d mo %2d space %d ",cycle,op_max,yaffs_freespace(mountpt));
75                         for(i = 0; i < BASH_HANDLES; i++)
76                                 printf("%2d ",h[i]);
77                         printf("\n");
78                 }
79                 cycle++;
80
81                 if(n_cycles > 0)
82                         n_cycles--;
83                 i = rand() % BASH_HANDLES;
84                 op = rand() % op_max;
85                 pos = rand() & 20000000;
86                 n = rand() % 100;
87                 n1 = rand() % 100;
88                 
89                 sprintf(name,"%s/bashdir/xx%d",mountpt,n);
90                 sprintf(name1,"%s/bashdir/xx%d",mountpt,n1);
91
92                 start_op = op;
93                 
94                 op-=1;
95                 if(op < 0){
96                         if(h[i]>= 0){
97                                 yaffs_close(h[i]);
98                                 h[i] = -1;
99                         }
100                         continue;
101                 }
102                 op-=1;
103                 if(op < 0){
104                         if(h[i] < 0)
105                                 h[i] = yaffs_open(name,O_CREAT| O_RDWR, 0666);
106                         continue;
107                 }
108
109                 op-=5;
110                 if(op< 0){
111                         yaffs_lseek(h[i],pos,SEEK_SET);
112                         yaffs_write(h[i],name,n);
113                         continue;
114                 }
115                 op-=1;
116                 if(op < 0){
117                         yaffs_unlink(name);
118                         continue;
119                 }
120                 op-=1;
121                 if(op < 0){
122                         yaffs_rename(name,name1);
123                         continue;
124                 }
125                 op-=1;
126                 if(op < 0){
127                         yaffs_mkdir(name,0666);
128                         continue;
129                 }
130                 op-=1;
131                 if(op < 0){
132                         yaffs_rmdir(name);
133                         continue;
134                 }
135                 
136                 op_max = start_op-op;
137         }
138                 
139         
140 }
141
142 void BadUsage(void)
143 {
144         printf("Usage: yaffs_test [options] mountpoint\n");
145         printf("options\n");
146         printf(" f: do fsx testing\n");
147         printf(" i: initialise for upgrade testing\n");
148         printf(" l: multiply number of operations by 5\n");
149         printf(" m: random max number of mallocs\n");
150         printf(" n nnn: number of cycles\n");
151         printf(" p: simulate power fail testing\n");
152         printf(" s sss: set seed\n");
153         printf(" u: do upgrade test\n");
154         printf(" b: bash-about test\n");
155         exit(2);
156 }
157
158 int sleep_exit = 0;
159 int sleep_time = 0;
160 void test_fatal(void)
161 {
162         printf("fatal yaffs test pid %d sleeping\n",getpid());
163         while(!sleep_exit){
164                 sleep(1);
165                 sleep_time++;
166         }
167         
168         signal(SIGSEGV,SIG_IGN);
169         signal(SIGBUS,SIG_IGN);
170         signal(SIGABRT,SIG_IGN);
171         sleep_exit = 0;
172         
173 }
174
175 void bad_ptr_handler(int sig)
176 {
177         printf("signal %d received\n",sig);
178         test_fatal();
179 }
180
181 int main(int argc, char **argv)
182 {
183         int ch;
184         int random_mallocs=0;
185         ext_fatal = test_fatal;
186
187 #if 1
188         signal(SIGSEGV,bad_ptr_handler);
189         signal(SIGBUS,bad_ptr_handler);
190         signal(SIGABRT,bad_ptr_handler);
191 #endif  
192         while ((ch = getopt(argc,argv, "bfilmn:ps:u"))
193                != EOF)
194                 switch (ch) {
195                 case 's':
196                         random_seed = atoi(optarg);
197                         break;
198                 case 'p':
199                         simulate_power_failure =1;
200                         break;
201                 case 'i':
202                         init_test = 1;
203                         break;
204                 case 'b':
205                         do_bash_around = 1;
206                         break;
207                 case 'f':
208                         do_fsx = 1;
209                         break;
210                 case 'l':
211                         ops_multiplier *= 5;
212                         break;
213                 case 'u':
214                         do_upgrade = 1;
215                         break;
216                 case 'm':
217                         random_mallocs=1;
218                         break;
219                 case 'n':
220                         n_cycles = atoi(optarg);
221                         break;
222                 default:
223                         BadUsage();
224                         /* NOTREACHED */
225                 }
226         argc -= optind;
227         argv += optind;
228         
229         if(random_mallocs){
230                 yaffs_test_maxMallocs = 0xFFF & random_seed;
231         }
232         
233         if(argc == 1) {
234                 strcpy(mount_point,argv[0]);
235                 
236                 if(simulate_power_failure)
237                         n_cycles = -1;
238                 printf("Running test %s %s %s %s seed %d cycles %d\n",
239                         do_upgrade ? "fw_upgrade" : "",
240                         init_test ? "initialise":"",
241                         do_fsx ? "fsx" :"",
242                         simulate_power_failure ? "power_fail" : "",
243                         random_seed, n_cycles);
244
245                 yaffs_StartUp();
246                 yaffs_mount(mount_point);
247                 printf("Mount complete\n");
248                         
249                 if(do_upgrade && init_test){
250                         simulate_power_failure = 0;
251                         NorStressTestInitialise(mount_point);
252                 } else if(do_upgrade){
253                         printf("Running stress on %s with seed %d\n",mount_point,random_seed);
254                         NorStressTestRun(mount_point,n_cycles,do_fsx);
255                 } else if(do_fsx){
256                         yaffs_fsx_main(mount_point,n_cycles);
257                 } else if(do_bash_around){
258                         yaffs_bash_around(mount_point,n_cycles);
259                 }else {
260                         printf("No test to run!\n");
261                         BadUsage();
262                 }
263                 yaffs_unmount(mount_point);
264                 
265                 printf("Test run completed!\n");
266         }
267         else
268                 BadUsage();
269         return 0;
270 }
271
272