cb3a30018fddd47028a6502762480ecb454bb5ba
[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 int main()
22 {
23         dir_struct *scanned_dir=NULL;
24         int output=0;
25         int break_bool=0;
26         int x=5;
27         while( 1){
28                 while (break_bool!=1){
29                         //printf("x %d\n",x);
30                         x--;
31                         if (x<0 &&(break_bool!=1)){
32                                 output=mkdir_test();
33                                 break_bool=1;
34                         } 
35                         x--;
36                         if (x<0 &&(break_bool!=1)){
37                                 output=rmdir_test();
38                                 break_bool=1;
39                         } 
40                         x--;
41                         if (x<0 &&(break_bool!=1)){
42                                 output=mknod_test();
43                                 break_bool=1;
44                         } 
45                         x--;
46                         if (x<0 &&(break_bool!=1)){
47                                 output=symlink_test();
48                                 break_bool=1;
49                         } 
50                         x--;
51                         if (x<0 &&(break_bool!=1)){
52                                 output=link_test();
53                                 break_bool=1;
54                         } 
55                         x--;
56                         if (x<0 &&(break_bool!=1)){
57                                 output=rename_test();
58                                 break_bool=1;
59                         } 
60                         x--;
61                         if (x<0 &&(break_bool!=1)){
62                                 scanned_dir=scan_dir();
63                                 
64                                 output=remount_test();
65                                 check_dir(scanned_dir);
66                                 scanned_dir=NULL;       //the scanned dir has been freed in check_dir.
67                                 break_bool=1;
68                         } 
69                 }       
70                 //printf("resetting x\n"); 
71                 check_function(output);
72                 break_bool=0;
73                 x=(rand()% 99);
74         }
75         return 0;
76 }
77
78 dir_struct * scan_dir(void)
79 {       
80         struct dirent *dir_data;        
81         dir_struct *dir=NULL;
82         dir=malloc(sizeof(dir_struct));
83         memset(dir, NULL, sizeof(dir_struct));
84         DIR *open_dir=NULL;
85
86
87         open_dir=opendir(ROOT_PATH);
88         dir_data=readdir(open_dir);
89         while(dir_data){
90                 dir->path_list=linked_list_add_node(HEAD,dir->path_list);
91                 dir->path_list->string=malloc(strlen(dir_data->d_name)+1);
92                 strcpy(dir->path_list->string,dir_data->d_name);
93                 sprintf(message,"opened file: %s\n",dir->path_list->string);
94                 print_message(3,message);
95                 dir_data=readdir(open_dir);
96         }
97         closedir(open_dir);
98         node_print_pointers(dir->path_list);
99         return dir;
100 }
101
102 int check_dir(dir_struct *old_dir)
103 {
104         dir_struct *new_dir=scan_dir();
105         node *new_list=new_dir->path_list;
106         node *old_list=old_dir->path_list;
107         int exit_loop=0;
108         print_message(3,"checking dir\n");
109         for (;old_list!= NULL;old_list=old_list->next){
110                 
111                 for (;(new_list=NULL) && (exit_loop !=1);new_list=new_list->next){
112                         sprintf(message,"comparing %s  and %s\n",old_list->string,new_list->string);
113                         print_message(3,message);
114                         if (strcmp( new_list->string ,old_list->string)==0){
115                                 //files match -now compare the modes and contents of the files.
116                                 //and set the paths to NULL.
117                                 exit_loop=1;
118                         }
119                         /*if (new_list->next==NULL){
120                                 print_message(3,"next is null\n");
121                                 
122                         }*/
123                 }
124                 if (exit_loop !=1){
125                         //failed to find a matching file
126                         sprintf(message,"a file has disappeared: %s\n",old_list->string); 
127                         print_message(3,message);
128                         
129                 }
130                 new_list=new_dir->path_list;
131                 exit_loop=0;
132         }
133         //now check if there are any old unmatched files 
134         
135         //free both data structs
136         delete_linked_list(old_dir->path_list);
137         delete_linked_list(new_dir->path_list);
138         new_dir->path_list=NULL;
139         old_dir->path_list=NULL;
140         free(old_dir);
141         free(new_dir);
142 }
143
144 int remount_test(void)
145 {
146         int output;
147         print_message(3,"\nunmounting\n");
148         output=umount2("/mnt/y",MNT_FORCE);
149         check_function(output);
150         print_message(3,"mounting\n");
151         mount("/dev/mtdblock0","/mnt/y","yaffs2",0,NULL);
152         check_function(output);
153 }
154
155 int mkdir_test(void)
156 {
157
158         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
159         int mode=0,output=0;
160         strcpy(string,ROOT_PATH);
161         strcat(string,generate_random_string(FILE_NAME_LENGTH));
162         mode = ((S_IREAD|S_IWRITE)&random_int());
163         sprintf(message,"\nmaking directory: %s, with mode %d\n",string,mode);
164         print_message(3,message);
165         output= mkdir(string,mode);
166         return output;
167 }
168
169 int rmdir_test(void)
170 {
171         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
172         int output=0;
173         strcpy(string,ROOT_PATH);
174         strcat(string,generate_random_string(FILE_NAME_LENGTH));
175
176         sprintf(message,"\nremoving directory: %s\n",string);
177         print_message(3,message);
178         output= rmdir(string);
179         return output;
180 }
181 int symlink_test(void)
182 {
183         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
184         char string2[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
185         int output;
186         strcpy(string,ROOT_PATH);
187         strcat(string,generate_random_string(FILE_NAME_LENGTH));
188         strcpy(string2,ROOT_PATH);
189         strcat(string2,generate_random_string(FILE_NAME_LENGTH));
190         sprintf(message,"\nsymlink from: %s, to %s\n",string,string2);
191         print_message(3,message);
192         output= symlink(string,string2);
193         return output;
194 }
195 int rename_test(void)
196 {
197         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
198         char string2[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
199         int output;
200         strcpy(string,ROOT_PATH);
201         strcat(string,generate_random_string(FILE_NAME_LENGTH));
202         strcpy(string2,ROOT_PATH);
203         strcat(string2,generate_random_string(FILE_NAME_LENGTH));
204         sprintf(message,"\nrenaming from: %s, to %s\n",string,string2);
205         print_message(3,message);
206         output= rename(string,string2);
207         return output;
208 }
209 int link_test(void)
210 {
211         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
212         char string2[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
213         int output=0;
214         strcpy(string,ROOT_PATH);
215         strcat(string,generate_random_string(FILE_NAME_LENGTH));
216         strcpy(string2,ROOT_PATH);
217         strcat(string2,generate_random_string(FILE_NAME_LENGTH));
218         sprintf(message,"\nlink from: %s, to %s\n",string,string2);
219         print_message(3,message);
220         output= link(string,string2);
221         return output;
222 }
223 int mknod_test(void)
224 {
225         char string[FILE_NAME_LENGTH+strlen(ROOT_PATH)];
226         int mode=0,dev=0,output=0;
227         strcpy(string,ROOT_PATH);
228         strcat(string,generate_random_string(FILE_NAME_LENGTH));
229         mode = ((S_IREAD|S_IWRITE)&random_int());
230         dev = random_int();
231         sprintf(message,"\nmaking node: %s, with mode %d, dev %d\n",string,mode,dev);
232         print_message(3,message);
233         output= mknod(string,mode,dev);
234         return output;
235 }