~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to sflphone-common/libs/pjproject/pjlib/src/pjlib-test/file.c

  • Committer: Package Import Robot
  • Author(s): Francois Marier
  • Date: 2011-11-25 13:24:12 UTC
  • mfrom: (4.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20111125132412-dc4qvhyosk74cd42
Tags: 1.0.1-4
Don't assume that arch:all packages will get built (closes: #649726)

Show diffs side-by-side

added added

removed removed

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