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