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