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