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