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