yaffs Still trying to find the bug.
[yaffs2.git] / direct / timothy_tests / quick_tests / test_yaffs_read_EINVAL.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 "test_yaffs_read_EINVAL.h"
15
16 static int handle = -1;
17 static char *file_name = NULL;
18
19 int test_yaffs_read_EINVAL(void)
20 {
21         int error_code = 0;
22         handle=yaffs_open(FILE_PATH,O_CREAT | O_RDWR, FILE_MODE);
23         printf("newly opend handle = %d handle\n",handle);
24         char text[2000000]="\0";
25         int output=0;   
26         
27         if (handle<0){
28                 print_message("could not open file\n",2);
29                 return -1;
30         }       
31
32         /*there needs a large amout of test in the file in order to trigger EINVAL */
33         output=test_yaffs_read_EINVAL_init();
34         if (output<0){
35                 print_message("could not write text to the file\n",2);
36                 return -1; 
37         }
38
39         if (handle>=0){
40                 output=yaffs_read(handle, text, -1);
41                 if (output<0){ 
42                         error_code=yaffs_get_error();
43                         if (abs(error_code)== EINVAL){
44                                 return 1;
45                         } else {
46                                 print_message("different error than expected\n",2);
47                                 return -1;
48                         }
49                 } else{
50                         print_message("read a negative number of bytes (which is a bad thing)\n",2);
51                         return -1;
52                 }
53         } else {
54                 print_message("error opening file\n",2);
55                 return -1;
56         }
57 }
58
59 int test_yaffs_read_EINVAL_clean(void)
60 {
61         int output=0;
62         if (handle>=0){
63                 if(file_name){
64                         free(file_name);
65                         file_name = NULL;
66                 }
67
68                 
69                 output= yaffs_truncate(FILE_PATH,FILE_SIZE );   
70                 if (output>=0){
71                         output=test_yaffs_write();
72                         if (output<0){
73                                 print_message("failed to write to file\n",2);
74                                 return -1;
75                         } else {
76                                 output=test_yaffs_write_clean();
77                                 if (output<0){
78                                         print_message("failed to clean the write_to_file function\n",2);
79                                 }
80                         }
81                 } else {
82                         print_message("failed to truncate file\n",2);
83                         return -1;
84                 }
85
86                 if(output>=0){
87                         output=yaffs_close(handle);
88                         if (output>=0){
89                                 printf("closed the file, handle %d\n",handle);
90                                 return 1;
91                         } else {
92                                 print_message("could not close the handle\n",2);
93                                 return -1;
94                         }
95                 } else {
96                         print_message("failed to fix the file\n",2);
97                         return -1;
98                 }
99         } else {
100                 print_message("no open handle\n",2);
101                 return -1;      
102         }
103 }
104
105 int test_yaffs_read_EINVAL_init(void)
106 {
107         int output=0;
108         int x=0;
109         
110         int file_name_length=1000000;
111
112         file_name = malloc(file_name_length);
113         if(!file_name){
114                 print_message("unable to create file text\n",2);
115                 return -1;
116         }
117         
118         strcat(file_name,YAFFS_MOUNT_POINT);
119         for (x=strlen(YAFFS_MOUNT_POINT); x<file_name_length -1; x++){
120                 file_name[x]='a';
121         }
122         file_name[file_name_length-2]='\0';
123
124
125
126         if (handle>=0){
127                 output= yaffs_write(handle, file_name, file_name_length-1);
128                 if (output<0){
129                         print_message("could not write text to file\n",2);
130                         return -1;
131                 } else {
132                         
133                         return 1;
134                 }
135
136         } else {
137                 print_message("error opening file\n",2);
138                 return -1;
139         }
140         
141 }
142
143