yaffs Fixing some minor warnings in direct/timothy_tests/linux_tests
[yaffs2.git] / direct / timothy_tests / linux_tests / linux_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 Timothy Manning <timothy@yaffs.net>
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 #include "linux_test.h"
15
16 int random_seed;
17 int simulate_power_failure = 1;
18
19 char message[400];      //this is used for storing print messages.
20
21
22
23 const struct option long_options[]={
24         {"help",        0,NULL,'h'},
25
26
27         {"print_level", 1,NULL,'p'},
28         {"quiet",       0,NULL,'q'},
29
30         {"seed",        1,NULL,'s'},
31
32         {"verbose",     0,NULL,'v'}
33 };
34
35 const char short_options[]="hp:qs:v";
36
37
38 void init(int argc, char *argv[])
39 {
40         char dir[200];
41         dir[0]='\0';
42         int x=-1;
43         char message[100];
44         int new_option;
45
46         x=(unsigned)time(NULL);
47         sprintf(message,"seeding srand with: %d\n",x);
48         print_message(2,message);
49         srand(x);
50
51
52
53         do {
54                 new_option=getopt_long(argc,argv,short_options,long_options,NULL);              
55                 if (new_option=='h'){
56                         printf("mirror_tests help\n");
57                         printf("arguments:\n");
58                         printf("\t-p [NUMBER] //sets the print level for mirror_tests.\n");
59                         printf("\t-v //verbose mode everything is printed\n");
60                         printf("\t-q //quiet mode nothing is printed.\n");
61                         printf("\t-s [number] //seeds rand with the number\n");
62
63                         exit(0);
64         
65                 } else if (new_option=='p'){
66                         set_print_level(atoi(optarg));
67                 } else if (new_option=='v'){
68                         set_print_level(5);
69                 } else if (new_option=='q'){
70                         set_print_level(-1);
71                 } else if (new_option=='s'){
72                         srand(atoi(argv[x+1]));
73
74                 } else if (new_option==-1){
75
76                 } else if (new_option=='?') {
77                         printf("bad argument\n");
78                         exit(0);
79                 }
80         }while(new_option!=-1);
81 }
82
83 int main(int argc, char *argv[])
84 {
85         dir_struct *scanned_dir=NULL;
86         int output=0;
87         int break_bool=0;
88         int x=5;
89
90         init(argc,argv); 
91         while( 1){
92                 while (break_bool!=1){
93                         //printf("x %d\n",x);
94                         x--;
95                         if (x<0 &&(break_bool!=1)){
96                                 output=mkdir_test();
97                                 break_bool=1;
98                         } 
99                         x--;
100                         if (x<0 &&(break_bool!=1)){
101                                 output=rmdir_test();
102                                 break_bool=1;
103                         } 
104                         x--;
105                         if (x<0 &&(break_bool!=1)){
106                                 output=mknod_test();
107                                 break_bool=1;
108                         } 
109                         x--;
110                         if (x<0 &&(break_bool!=1)){
111                                 output=symlink_test();
112                                 break_bool=1;
113                         } 
114                         x--;
115                         if (x<0 &&(break_bool!=1)){
116                                 output=link_test();
117                                 break_bool=1;
118                         } 
119                         x--;
120                         if (x<0 &&(break_bool!=1)){
121                                 output=rename_test();
122                                 break_bool=1;
123                         } 
124                         x--;
125                         if (x<0 &&(break_bool!=1)){
126                                 scanned_dir=scan_dir();
127                                 
128                                 output=remount_test();
129                                 check_dir(scanned_dir);
130                                 scanned_dir=NULL;       //the scanned dir has been freed in check_dir.
131                                 break_bool=1;
132                         } 
133                 }       
134                 //printf("resetting x\n"); 
135                 check_function(output);
136                 break_bool=0;
137                 x=(rand()% 99);
138         }
139         return 0;
140 }
141
142 dir_struct * scan_dir(void)
143 {       
144         struct dirent *dir_data;        
145         dir_struct *dir=NULL;
146         dir=malloc(sizeof(dir_struct));
147         memset(dir, 0, sizeof(dir_struct));
148         DIR *open_dir=NULL;
149
150
151         open_dir=opendir(ROOT_PATH);
152         dir_data=readdir(open_dir);
153         while(dir_data){
154                 dir->path_list=linked_list_add_node(HEAD,dir->path_list);
155                 dir->path_list->string=malloc(strlen(dir_data->d_name)+1);
156                 strcpy(dir->path_list->string,dir_data->d_name);
157                 sprintf(message,"opened file: %s\n",dir->path_list->string);
158                 print_message(5,message);
159                 dir_data=readdir(open_dir);
160         }
161         closedir(open_dir);
162         //node_print_pointers(dir->path_list);
163         return dir;
164 }
165
166 int check_dir(dir_struct *old_dir)
167 {
168         print_message(3,"scanning new dir\n");
169         dir_struct *new_dir=scan_dir();
170         node *new_list=new_dir->path_list;
171         node *old_list=old_dir->path_list;
172         int exit_loop=0,error=0;
173         print_message(3,"checking dir\n");
174         for (;old_list!= NULL;old_list=old_list->next){
175                 //sprintf(message,"new_list=!NULL= %d, exit_loop !=1 = %d\n",(new_list!=NULL),(exit_loop !=1));
176                 //print_message(3,message);
177                 for (;(new_list!=NULL) && (exit_loop !=1);new_list=new_list->next){
178                         //sprintf(message,"comparing %s  and %s\n",old_list->string,new_list->string);
179                         //print_message(3,message);
180                         if (strcmp( new_list->string ,old_list->string)==0){
181                                 //files match -now compare the modes and contents of the files.
182                                 //and set the paths to NULL.
183                                 exit_loop=1;
184                         }
185                         /*if (new_list->next==NULL){
186                                 print_message(3,"next is null\n");
187                                 
188                         }*/
189                 }
190                 if (exit_loop !=1){
191                         //failed to find a matching file
192                         sprintf(message,"a file has disappeared: %s\n",old_list->string); 
193                         print_message(3,message);
194                         error=1;
195                         
196                 }
197                 new_list=new_dir->path_list;
198                 exit_loop=0;
199         }
200         //now check if there are any old unmatched files 
201         
202         //free both data structs
203         delete_linked_list(old_dir->path_list);
204         delete_linked_list(new_dir->path_list);
205         new_dir->path_list=NULL;
206         old_dir->path_list=NULL;
207         free(old_dir);
208         free(new_dir);
209         if (error ==1){
210                 print_message(3,"checking dir failed\n");
211                 if (get_exit_on_error()==1){
212                         print_message(3,"exiting_program\n");
213                         exit(0);
214                 }
215         }
216
217         else if (error !=1){
218                 print_message(3,"checking dir passed\n");
219         }
220         return error;
221 }
222
223 int remount_test(void)
224 {
225         int output;
226         print_message(3,"\nunmounting\n");
227         output=umount2("/mnt/y",1);
228         check_function(output);
229         print_message(3,"mounting\n");
230         mount("/dev/mtdblock0","/mnt/y","yaffs2",0,NULL);
231         check_function(output);
232         return output;
233 }
234
235 int mkdir_test(void)
236 {
237
238         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
239         int mode=0,output=0;
240         strcpy(string,ROOT_PATH);
241         strcat(string,generate_random_string(FILE_NAME_LENGTH));
242         mode = ((S_IREAD|S_IWRITE)&random_int());
243         sprintf(message,"\nmaking directory: %s, with mode %d\n",string,mode);
244         print_message(3,message);
245         output= mkdir(string,mode);
246         return output;
247 }
248
249 int rmdir_test(void)
250 {
251         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
252         int output=0;
253         strcpy(string,ROOT_PATH);
254         strcat(string,generate_random_string(FILE_NAME_LENGTH));
255
256         sprintf(message,"\nremoving directory: %s\n",string);
257         print_message(3,message);
258         output= rmdir(string);
259         return output;
260 }
261 int symlink_test(void)
262 {
263         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
264         char string2[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
265         int output;
266         strcpy(string,ROOT_PATH);
267         strcat(string,generate_random_string(FILE_NAME_LENGTH));
268         strcpy(string2,ROOT_PATH);
269         strcat(string2,generate_random_string(FILE_NAME_LENGTH));
270         sprintf(message,"\nsymlink from: %s, to %s\n",string,string2);
271         print_message(3,message);
272         output= symlink(string,string2);
273         return output;
274 }
275 int rename_test(void)
276 {
277         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
278         char string2[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
279         int output;
280         strcpy(string,ROOT_PATH);
281         strcat(string,generate_random_string(FILE_NAME_LENGTH));
282         strcpy(string2,ROOT_PATH);
283         strcat(string2,generate_random_string(FILE_NAME_LENGTH));
284         sprintf(message,"\nrenaming from: %s, to %s\n",string,string2);
285         print_message(3,message);
286         output= rename(string,string2);
287         return output;
288 }
289 int link_test(void)
290 {
291         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
292         char string2[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
293         int output=0;
294         strcpy(string,ROOT_PATH);
295         strcat(string,generate_random_string(FILE_NAME_LENGTH));
296         strcpy(string2,ROOT_PATH);
297         strcat(string2,generate_random_string(FILE_NAME_LENGTH));
298         sprintf(message,"\nlink from: %s, to %s\n",string,string2);
299         print_message(3,message);
300         output= link(string,string2);
301         return output;
302 }
303 int mknod_test(void)
304 {
305         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
306         int mode=0,dev=0,output=0;
307         strcpy(string,ROOT_PATH);
308         strcat(string,generate_random_string(FILE_NAME_LENGTH));
309         mode = ((S_IREAD|S_IWRITE)&random_int());
310         dev = random_int();
311         sprintf(message,"\nmaking node: %s, with mode %d, dev %d\n",string,mode,dev);
312         print_message(3,message);
313         output= mknod(string,mode,dev);
314         return output;
315 }