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