Fix unmatched temporary buffer allocations
[yaffs2.git] / direct / test-framework / timothy_tests / handle_tests / handle_test.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 #include "handle_test.h"
14
15 int random_seed;
16 int simulate_power_failure = 0;
17
18 int main()
19 {
20         
21         int test_batch = 1000000;
22         int output =0;
23         unsigned int total_number_of_tests_run=0;
24         yaffs_start_up();
25         yaffs_mount("yaffs2");
26
27         printf("running tests\n");
28
29         output=dup_test();
30         printf("dup test: %d\n ",output);
31
32         while (1){
33                 output = open_close_handle_test(test_batch);
34                 if (output>=0){
35                         total_number_of_tests_run ++;
36                 } else {
37                         get_error();
38                         return -1;
39                 }
40                 printf("total number of tests = %d\n",(total_number_of_tests_run*test_batch));
41         }
42
43
44         return 0;
45 }
46 int dup_test(void){
47         int handle = -1;
48         int handle2 = -1;
49         int output =-1;
50
51         handle =open_handle();
52         if (handle<0){
53                 printf("error: failed to open handle\n");
54                 return -1;
55         }
56         handle2=yaffs_dup(handle);
57         if (handle2<0){
58                 printf("error: failed to open handle2\n");
59                 return -1;
60         }
61         
62         output=yaffs_lseek(handle,20,SEEK_SET);
63         if (output >= 0) {
64                 output = yaffs_lseek(handle,0,SEEK_CUR);
65                 if (output = 20){
66                         printf("dup is working\n");
67                         return 1;
68                 } else if (output <0){
69                         printf("failed to lseek the second time\n");
70                         return -1;
71                 } else {
72                         printf("lseek position is diffrent than expected\n");
73                         return -1;
74                 }
75         } else {
76                 printf("failed to lseek the first time\n");
77                 return -1;
78         }
79
80 }
81
82 int open_close_handle_test(int num_of_tests)
83 {
84         int handle_number=open_handle();
85         int handle=0;
86         int x =0;
87         int output=0;
88         yaffs_close(handle_number);
89         for (x=0;x<num_of_tests;x++){
90                 handle=open_handle();
91                 if (handle != handle_number){
92                         printf("handle number changed\n");
93                         printf("test number= %d\n",x);
94                         printf("expected number = %d\n",handle_number);
95                         printf("handle_number = %d\n",handle);
96                         return -1;
97                 }
98                 output=yaffs_close(handle);
99                 if (output <0){
100                         printf("failed to close file\n");
101                         return -1;
102                 }
103         }
104         return 1;
105 }
106
107 void get_error(void)
108 {
109         int error_code=0;
110         error_code=yaffs_get_error();
111         printf("yaffs_error code %d\n",error_code);
112         printf("error is : %s\n",yaffs_error_to_str(error_code));
113 }
114
115 int open_handle(void)
116 {
117         return yaffs_open(FILE_PATH,O_CREAT | O_RDWR,S_IREAD | S_IWRITE);
118 }
119