Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/file.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
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 #define THIS_FILE "file.c"
28
29 static char buffer[11] = {'H', 'e', 'l', 'l', 'o', ' ',
30 'W', 'o', 'r', 'l', 'd' };
31
32 static int file_test_internal(void)
33 {
34 enum { FILE_MAX_AGE = 1000 };
35 pj_oshandle_t fd = 0;
36 pj_status_t status;
37 char readbuf[sizeof(buffer)+16];
38 pj_file_stat stat;
39 pj_time_val start_time;
40 pj_ssize_t size;
41 pj_off_t pos;
42
43 PJ_LOG(3,("", "..file io test.."));
44
45 /* Get time. */
46 pj_gettimeofday(&start_time);
47
48 /* Delete original file if exists. */
49 if (pj_file_exists(FILENAME))
50 pj_file_delete(FILENAME);
51
52 /*
53 * Write data to the file.
54 */
55 PJ_TEST_SUCCESS(pj_file_open(NULL, FILENAME, PJ_O_WRONLY, &fd),
56 NULL, return -10);
57
58 size = sizeof(buffer);
59 PJ_TEST_SUCCESS(pj_file_write(fd, buffer, &size), NULL,
60 { pj_file_close(fd); return -20; });
61 PJ_TEST_EQ(size, sizeof(buffer), NULL, return -25 );
62 PJ_TEST_SUCCESS( pj_file_close(fd), NULL, return -30 );
63
64 /* Check the file existance and size. */
65 PJ_TEST_TRUE( pj_file_exists(FILENAME), NULL, return -40 );
66
67 PJ_TEST_EQ( pj_file_size(FILENAME), sizeof(buffer), NULL, return -50 );
68
69 /* Get file stat. */
70 PJ_TEST_SUCCESS(pj_file_getstat(FILENAME, &stat), NULL, return -60 );
71
72 /* Check stat size. */
73 PJ_TEST_EQ(stat.size, sizeof(buffer), NULL, return -70 );
74
75 #if INCLUDE_FILE_TIME_TEST
76 /* Check file creation time >= start_time. */
77 if (!PJ_TIME_VAL_GTE(stat.ctime, start_time))
78 return -80;
79 /* Check file creation time is not much later. */
80 PJ_TIME_VAL_SUB(stat.ctime, start_time);
81 if (stat.ctime.sec > FILE_MAX_AGE)
82 return -90;
83
84 /* Check file modification time >= start_time. */
85 if (!PJ_TIME_VAL_GTE(stat.mtime, start_time))
86 return -80;
87 /* Check file modification time is not much later. */
88 PJ_TIME_VAL_SUB(stat.mtime, start_time);
89 if (stat.mtime.sec > FILE_MAX_AGE)
90 return -90;
91
92 /* Check file access time >= start_time. */
93 if (!PJ_TIME_VAL_GTE(stat.atime, start_time))
94 return -80;
95 /* Check file access time is not much later. */
96 PJ_TIME_VAL_SUB(stat.atime, start_time);
97 if (stat.atime.sec > FILE_MAX_AGE)
98 return -90;
99 #endif
100
101 /*
102 * Re-open the file and read data.
103 */
104 PJ_TEST_SUCCESS(pj_file_open(NULL, FILENAME, PJ_O_RDONLY, &fd),
105 NULL, return -100);
106
107 size = 0;
108 while (size < (pj_ssize_t)sizeof(readbuf)) {
109 pj_ssize_t read;
110 read = 1;
111 status = pj_file_read(fd, &readbuf[size], &read);
112 if (status != PJ_SUCCESS) {
113 PJ_LOG(3,("", "...error reading file after %ld bytes "
114 "(error follows)", size));
115 app_perror("...error", status);
116 pj_file_close(fd);
117 return -110;
118 }
119 if (read == 0) {
120 // EOF
121 break;
122 }
123 size += read;
124 }
125
126 PJ_TEST_EQ(size, sizeof(buffer), NULL,
127 {pj_file_close(fd); return -120; });
128
129 /*
130 if (!pj_file_eof(fd, PJ_O_RDONLY))
131 return -130;
132 */
133
134 PJ_TEST_EQ(pj_memcmp(readbuf, buffer, size), 0, NULL,
135 {pj_file_close(fd); return -140; });
136
137 /* Seek test. */
138 PJ_TEST_SUCCESS(pj_file_setpos(fd, 4, PJ_SEEK_SET), NULL,
139 {pj_file_close(fd); return -141; });
140
141 /* getpos test. */
142 PJ_TEST_SUCCESS(pj_file_getpos(fd, &pos), NULL,
143 {pj_file_close(fd); return -142; });
144 PJ_TEST_EQ(pos, 4, NULL, {pj_file_close(fd); return -143; });
145
146 PJ_TEST_SUCCESS(pj_file_close(fd), NULL, return -150);
147
148 /*
149 * Rename test.
150 */
151 PJ_TEST_SUCCESS(pj_file_move(FILENAME, NEWNAME), NULL, return -160);
152 PJ_TEST_EQ(pj_file_exists(FILENAME), 0, NULL, return -170);
153 PJ_TEST_TRUE(pj_file_exists(NEWNAME), NULL, return -180);
154 PJ_TEST_EQ(pj_file_size(NEWNAME), sizeof(buffer), NULL, return -190);
155
156 /* Delete test. */
157 PJ_TEST_SUCCESS(pj_file_delete(NEWNAME), NULL, return -200);
158 PJ_TEST_EQ(pj_file_exists(NEWNAME), 0, NULL, return -210);
159
160 return 0;
161 }
162
163
164 int file_test(void)
165 {
166 int rc = file_test_internal();
167
168 /* Delete test file if exists. */
169 if (pj_file_exists(FILENAME))
170 pj_file_delete(FILENAME);
171
172 return rc;
173 }
174
175 #else
176 int dummy_file_test;
177 #endif
178