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