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