e450236560b92cb03ae3617e662f691d307780ae
[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         if (open_dir < 0){
153                 sprintf(message,"failed to find the directory: %s",ROOT_PATH);
154                 print_message(1,message);
155         }
156         dir_data=readdir(open_dir);
157         while(dir_data){
158                 dir->path_list=linked_list_add_node(HEAD,dir->path_list);
159                 dir->path_list->string=malloc(strlen(dir_data->d_name)+1);
160                 strcpy(dir->path_list->string,dir_data->d_name);
161                 sprintf(message,"opened file: %s\n",dir->path_list->string);
162                 print_message(5,message);
163                 dir_data=readdir(open_dir);
164         }
165         closedir(open_dir);
166         //node_print_pointers(dir->path_list);
167         return dir;
168 }
169
170 int check_dir(dir_struct *old_dir)
171 {
172         print_message(3,"scanning new dir\n");
173         dir_struct *new_dir=scan_dir();
174         node *new_list=new_dir->path_list;
175         node *old_list=old_dir->path_list;
176         int exit_loop=0,error=0;
177         print_message(3,"checking dir\n");
178         for (;old_list!= NULL;old_list=old_list->next){
179                 //sprintf(message,"new_list=!NULL= %d, exit_loop !=1 = %d\n",(new_list!=NULL),(exit_loop !=1));
180                 //print_message(3,message);
181                 for (;(new_list!=NULL) && (exit_loop !=1);new_list=new_list->next){
182                         //sprintf(message,"comparing %s  and %s\n",old_list->string,new_list->string);
183                         //print_message(3,message);
184                         if (strcmp( new_list->string ,old_list->string)==0){
185                                 //files match -now compare the modes and contents of the files.
186                                 //and set the paths to NULL.
187                                 exit_loop=1;
188                         }
189                         /*if (new_list->next==NULL){
190                                 print_message(3,"next is null\n");
191                                 
192                         }*/
193                 }
194                 if (exit_loop !=1){
195                         //failed to find a matching file
196                         sprintf(message,"a file has disappeared: %s\n",old_list->string); 
197                         print_message(3,message);
198                         error=1;
199                         
200                 }
201                 new_list=new_dir->path_list;
202                 exit_loop=0;
203         }
204         //now check if there are any old unmatched files 
205         
206         //free both data structs
207         delete_linked_list(old_dir->path_list);
208         delete_linked_list(new_dir->path_list);
209         new_dir->path_list=NULL;
210         old_dir->path_list=NULL;
211         free(old_dir);
212         free(new_dir);
213         if (error ==1){
214                 print_message(3,"checking dir failed\n");
215                 if (get_exit_on_error()==1){
216                         print_message(3,"exiting_program\n");
217                         exit(0);
218                 }
219         }
220
221         else if (error !=1){
222                 print_message(3,"checking dir passed\n");
223         }
224         return error;
225 }
226
227 int remount_test(void)
228 {
229         int output;
230         print_message(3,"\nunmounting\n");
231         output=umount2("/mnt/y",1);
232         check_function(output);
233         print_message(3,"mounting\n");
234         mount("/dev/mtdblock0","/mnt/y","yaffs2",0,NULL);
235         check_function(output);
236         return output;
237 }
238
239 int mkdir_test(void)
240 {
241
242         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
243         int mode=0,output=0;
244         strcpy(string,ROOT_PATH);
245         strcat(string,generate_random_string(FILE_NAME_LENGTH));
246         mode = ((S_IREAD|S_IWRITE)&random_int());
247         sprintf(message,"\nmaking directory: %s, with mode %d\n",string,mode);
248         print_message(3,message);
249         output= mkdir(string,mode);
250         return output;
251 }
252
253 int rmdir_test(void)
254 {
255         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
256         int output=0;
257         strcpy(string,ROOT_PATH);
258         strcat(string,generate_random_string(FILE_NAME_LENGTH));
259
260         sprintf(message,"\nremoving directory: %s\n",string);
261         print_message(3,message);
262         output= rmdir(string);
263         return output;
264 }
265 int symlink_test(void)
266 {
267         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
268         char string2[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
269         int output;
270         strcpy(string,ROOT_PATH);
271         strcat(string,generate_random_string(FILE_NAME_LENGTH));
272         strcpy(string2,ROOT_PATH);
273         strcat(string2,generate_random_string(FILE_NAME_LENGTH));
274         sprintf(message,"\nsymlink from: %s, to %s\n",string,string2);
275         print_message(3,message);
276         output= symlink(string,string2);
277         return output;
278 }
279 int rename_test(void)
280 {
281         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
282         char string2[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
283         int output;
284         strcpy(string,ROOT_PATH);
285         strcat(string,generate_random_string(FILE_NAME_LENGTH));
286         strcpy(string2,ROOT_PATH);
287         strcat(string2,generate_random_string(FILE_NAME_LENGTH));
288         sprintf(message,"\nrenaming from: %s, to %s\n",string,string2);
289         print_message(3,message);
290         output= rename(string,string2);
291         return output;
292 }
293 int link_test(void)
294 {
295         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
296         char string2[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
297         int output=0;
298         strcpy(string,ROOT_PATH);
299         strcat(string,generate_random_string(FILE_NAME_LENGTH));
300         strcpy(string2,ROOT_PATH);
301         strcat(string2,generate_random_string(FILE_NAME_LENGTH));
302         sprintf(message,"\nlink from: %s, to %s\n",string,string2);
303         print_message(3,message);
304         output= link(string,string2);
305         return output;
306 }
307 int mknod_test(void)
308 {
309         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
310         int mode=0,dev=0,output=0;
311         strcpy(string,ROOT_PATH);
312         strcat(string,generate_random_string(FILE_NAME_LENGTH));
313         mode = ((S_IREAD|S_IWRITE)&random_int());
314         dev = random_int();
315         sprintf(message,"\nmaking node: %s, with mode %d, dev %d\n",string,mode,dev);
316         print_message(3,message);
317         output= mknod(string,mode,dev);
318         return output;
319 }