3cab2761c1facbef18533b03f29c7c30a4b6bf3d
[yaffs2.git] / direct / timothy_tests / running_out_of_handles_error / yaffs_tester.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 /*
15  * yaffs_tester.c designed to stress test yaffs2 direct.
16  */
17
18
19 #include "yaffs_tester.h"
20
21
22
23 int random_seed;
24 int simulate_power_failure = 0;
25
26
27 buffer message_buffer;  /*create  message_buffer */
28
29
30
31 int main(){     
32         char yaffs_test_dir[] ="/yaffs2/test_dir";      /*the path to the directory where all of the testing will take place*/
33         char yaffs_mount_dir[]="/yaffs2/";              /*the path to the mount point which yaffs will mount*/
34         
35         init(yaffs_test_dir,yaffs_mount_dir);
36         test(yaffs_test_dir);
37         yaffs_unmount(yaffs_mount_dir);
38         return 0;
39 }
40
41
42 void init(char *yaffs_test_dir,char *yaffs_mount_dir){
43         char output=0;
44         /*these variables are already set to zero, but it is better not to take chances*/
45         message_buffer.head=0;                           
46         message_buffer.tail=0;
47         
48         add_to_buffer(&message_buffer,"welcome to the yaffs tester",MESSAGE_LEVEL_BASIC_TASKS,PRINT);/* print boot up message*/ 
49         yaffs_start_up();
50         yaffs_mount(yaffs_mount_dir);
51
52         if (yaffs_access(yaffs_test_dir,0))
53         {
54                 add_to_buffer(&message_buffer,"creating dir: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
55                 append_to_buffer(&message_buffer,yaffs_test_dir,MESSAGE_LEVEL_BASIC_TASKS,PRINT);       
56                 output=yaffs_mkdir(yaffs_test_dir,S_IREAD | S_IWRITE);
57                 yaffs_check_for_errors(output, &message_buffer,"could not create dir","created dir\n\n");
58         }
59         
60 }
61 void join_paths(char *path1,char *path2,char *new_path ){
62
63 /*      printf("strlen path1:%d\n",strlen(path1));
64         printf("strlen path2:%d\n",strlen(path2));
65         printf("path1; %s\n",path1);
66 */
67         add_to_buffer(&message_buffer, "joining paths:",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
68         append_to_buffer(&message_buffer,path1,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
69         append_to_buffer(&message_buffer, " and ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
70         append_to_buffer(&message_buffer, path2,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
71         if ( (path1[(sizeof(path1)/sizeof(char))-2]=='/') && path2[0]!='/') {
72                 /*paths are compatiable. concatanate them. note -2 is because of \0*/  
73                 strcat(new_path,path1);
74                 strcat(new_path,path2);         
75                 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))];
76                 //strcpy(new_path,strcat(path1,path2)); 
77                 //return new_path;
78         }       
79         else if ((path1[(sizeof(path1)/sizeof(char))-2]!='/') && path2[0]=='/') {
80                 /*paths are compatiable. concatanate them*/  
81                 strcat(new_path,path1);
82                 strcat(new_path,path2);         
83                 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))];
84                 //strcpy(new_path,strcat(path1,path2)); 
85                 //return new_path;
86         }
87         else if ((path1[(sizeof(path1)/sizeof(char))-2]!='/') && path2[0]!='/') {
88                         /*need to add a "/". */  
89                 strcat(new_path,path1);
90                 strcat(new_path,"/");
91                 strcat(new_path,path2);
92                 //strcpy(new_path,strcat(path1,strcat("/\0",path2)));
93
94 #if 0
95                 copy_array(path1,new_path,0,0);
96                 copy_array('\0',new_path,0,(sizeof(path1)/sizeof(char)));
97                 copy_array(path2,new_path,0,(sizeof(path1)/sizeof(char))+1);
98  old method now trying to use copy_array
99                 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))+1];
100                 for (x=0;x<=(sizeof(path1)/sizeof(char))-1;x++){ 
101                         new_path[x]=path1[x];
102                 }
103                 new_path[x+1]='/';
104                 for (x=(sizeof(path1)/sizeof(char)) ,i=0 ;i<=(sizeof(path2)/sizeof(char));x++,i++){ 
105                         new_path[x]=path2[i]; 
106                 }
107 #endif
108
109                 //return new_path;
110         }
111         else if ((path1[(sizeof(path1)/sizeof(char))-2]=='/') && path2[0]=='/') {
112                 /*need to remove a "/". */
113                 /*yaffs does not mind the extra slash. */
114                 //char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))-1];
115                 
116                 strcat(new_path,path1);
117                 strcat(new_path,path2);
118                 //strcpy(new_path,strcat(path1,strncat("",path2,(sizeof(path1)/sizeof(char))-1))); 
119                 //return new_path;
120         } 
121         else{
122                 //error 
123                 //return -1;
124         }
125 }
126
127
128
129 void test(char*yaffs_test_dir){
130         char output=0;
131         char name[MAX_FILE_NAME_SIZE+3 ]="apple\0";
132         char path[MAX_FILE_NAME_SIZE];
133         join_paths(yaffs_test_dir,name,path);
134         while(1)
135         {
136                 path[0]='\0';// this should clear the path
137                 generate_random_string(name);
138                 join_paths(yaffs_test_dir,name,path);
139                 add_to_buffer(&message_buffer,"trying to open file: ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
140                 append_to_buffer(&message_buffer,path,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
141                 output=yaffs_open(path,O_CREAT | O_TRUNC| O_RDWR, S_IREAD | S_IWRITE);
142                 yaffs_check_for_errors(output, &message_buffer,"failed to open file","opened file");
143         }
144 }
145 void  generate_random_string(char *ptr){
146         unsigned int x;
147         unsigned int length=((rand() %MAX_FILE_NAME_SIZE)+1);   /*creates a int with the number of charecters been between 1 and 51*/           
148         char letter='\0';
149         //printf("generating string\n");
150         //printf("string length is %d\n",length);
151         for (x=0; x <= (length-2) &&length>2 ; x++)
152         {
153                 //printf("x=%d\n",x);   
154                 /* keep generating a charecter until the charecter is legal*/
155                 while((letter=='\0' )||(letter=='/')||(letter=='\\')){
156                         letter=(rand() % 126-32)+32;    /*generate a number between 32 and 126 and uses it as a charecter (letter) */
157                 }       
158                 ptr[x]=letter;
159                 //printf("charecter generated is %c\n",ptr[x]);
160         }
161         ptr[x+1]='\0';  /*adds NULL charecter to turn it into a string*/
162         
163 }
164