blob: e32a27107d8a844023a6f4ed4efff9555a0a1313 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "test.h"
20#include <pjlib.h>
21
22#if INCLUDE_FILE_TEST
23
24#define FILENAME "testfil1.txt"
25#define NEWNAME "testfil2.txt"
26#define INCLUDE_FILE_TIME_TEST 0
27
28static char buffer[11] = {'H', 'e', 'l', 'l', 'o', ' ',
29 'W', 'o', 'r', 'l', 'd' };
30
Benny Prijonoc77fd572007-02-17 00:13:11 +000031static int file_test_internal(void)
Benny Prijono5dcb38d2005-11-21 01:55:47 +000032{
33 enum { FILE_MAX_AGE = 1000 };
34 pj_oshandle_t fd = 0;
35 pj_status_t status;
36 char readbuf[sizeof(buffer)+16];
37 pj_file_stat stat;
38 pj_time_val start_time;
39 pj_ssize_t size;
40 pj_off_t pos;
41
42 PJ_LOG(3,("", "..file io test.."));
43
44 /* Get time. */
45 pj_gettimeofday(&start_time);
46
47 /* Delete original file if exists. */
48 if (pj_file_exists(FILENAME))
49 pj_file_delete(FILENAME);
50
51 /*
52 * Write data to the file.
53 */
54 status = pj_file_open(NULL, FILENAME, PJ_O_WRONLY, &fd);
55 if (status != PJ_SUCCESS) {
56 app_perror("...file_open() error", status);
57 return -10;
58 }
59
60 size = sizeof(buffer);
61 status = pj_file_write(fd, buffer, &size);
62 if (status != PJ_SUCCESS) {
63 app_perror("...file_write() error", status);
64 pj_file_close(fd);
65 return -20;
66 }
67 if (size != sizeof(buffer))
68 return -25;
69
70 status = pj_file_close(fd);
71 if (status != PJ_SUCCESS) {
72 app_perror("...file_close() error", status);
73 return -30;
74 }
75
76 /* Check the file existance and size. */
77 if (!pj_file_exists(FILENAME))
78 return -40;
79
80 if (pj_file_size(FILENAME) != sizeof(buffer))
81 return -50;
82
83 /* Get file stat. */
84 status = pj_file_getstat(FILENAME, &stat);
85 if (status != PJ_SUCCESS)
86 return -60;
87
88 /* Check stat size. */
89 if (stat.size != sizeof(buffer))
90 return -70;
91
Benny Prijonoc77fd572007-02-17 00:13:11 +000092#if INCLUDE_FILE_TIME_TEST
Benny Prijono5dcb38d2005-11-21 01:55:47 +000093 /* Check file creation time >= start_time. */
94 if (!PJ_TIME_VAL_GTE(stat.ctime, start_time))
Benny Prijono5dcb38d2005-11-21 01:55:47 +000095 return -80;
96 /* Check file creation time is not much later. */
97 PJ_TIME_VAL_SUB(stat.ctime, start_time);
98 if (stat.ctime.sec > FILE_MAX_AGE)
99 return -90;
100
101 /* Check file modification time >= start_time. */
102 if (!PJ_TIME_VAL_GTE(stat.mtime, start_time))
103 return -80;
104 /* Check file modification time is not much later. */
105 PJ_TIME_VAL_SUB(stat.mtime, start_time);
106 if (stat.mtime.sec > FILE_MAX_AGE)
107 return -90;
108
109 /* Check file access time >= start_time. */
110 if (!PJ_TIME_VAL_GTE(stat.atime, start_time))
111 return -80;
112 /* Check file access time is not much later. */
113 PJ_TIME_VAL_SUB(stat.atime, start_time);
114 if (stat.atime.sec > FILE_MAX_AGE)
115 return -90;
116#endif
117
118 /*
119 * Re-open the file and read data.
120 */
121 status = pj_file_open(NULL, FILENAME, PJ_O_RDONLY, &fd);
122 if (status != PJ_SUCCESS) {
123 app_perror("...file_open() error", status);
124 return -100;
125 }
126
127 size = 0;
Benny Prijonoa1e69682007-05-11 15:14:34 +0000128 while (size < (pj_ssize_t)sizeof(readbuf)) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000129 pj_ssize_t read;
130 read = 1;
131 status = pj_file_read(fd, &readbuf[size], &read);
132 if (status != PJ_SUCCESS) {
133 PJ_LOG(3,("", "...error reading file after %d bytes (error follows)",
134 size));
135 app_perror("...error", status);
136 return -110;
137 }
138 if (read == 0) {
139 // EOF
140 break;
141 }
142 size += read;
143 }
144
145 if (size != sizeof(buffer))
146 return -120;
147
148 /*
149 if (!pj_file_eof(fd, PJ_O_RDONLY))
150 return -130;
151 */
152
153 if (pj_memcmp(readbuf, buffer, size) != 0)
154 return -140;
155
156 /* Seek test. */
157 status = pj_file_setpos(fd, 4, PJ_SEEK_SET);
158 if (status != PJ_SUCCESS) {
159 app_perror("...file_setpos() error", status);
160 return -141;
161 }
162
163 /* getpos test. */
164 status = pj_file_getpos(fd, &pos);
165 if (status != PJ_SUCCESS) {
166 app_perror("...file_getpos() error", status);
167 return -142;
168 }
169 if (pos != 4)
170 return -143;
171
172 status = pj_file_close(fd);
173 if (status != PJ_SUCCESS) {
174 app_perror("...file_close() error", status);
175 return -150;
176 }
177
178 /*
179 * Rename test.
180 */
181 status = pj_file_move(FILENAME, NEWNAME);
182 if (status != PJ_SUCCESS) {
183 app_perror("...file_move() error", status);
184 return -160;
185 }
186
187 if (pj_file_exists(FILENAME))
188 return -170;
189 if (!pj_file_exists(NEWNAME))
190 return -180;
191
192 if (pj_file_size(NEWNAME) != sizeof(buffer))
193 return -190;
194
195 /* Delete test. */
196 status = pj_file_delete(NEWNAME);
197 if (status != PJ_SUCCESS) {
198 app_perror("...file_delete() error", status);
199 return -200;
200 }
201
202 if (pj_file_exists(NEWNAME))
203 return -210;
204
205 PJ_LOG(3,("", "...success"));
206 return PJ_SUCCESS;
207}
208
Benny Prijonoc77fd572007-02-17 00:13:11 +0000209
210int file_test(void)
211{
212 int rc = file_test_internal();
213
214 /* Delete test file if exists. */
215 if (pj_file_exists(FILENAME))
216 pj_file_delete(FILENAME);
217
218 return rc;
219}
220
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000221#else
222int dummy_file_test;
223#endif
224