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

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.1.0/pjlib/src/pjlib-test/file.c

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: file.c 3553 2011-05-05 06:14:19Z nanang $ */
2
 
/* 
3
 
 * Copyright (C) 2008-2011 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
 
#include "test.h"
21
 
#include <pjlib.h>
22
 
 
23
 
#if INCLUDE_FILE_TEST
24
 
 
25
 
#define FILENAME                "testfil1.txt"
26
 
#define NEWNAME                 "testfil2.txt"
27
 
#define INCLUDE_FILE_TIME_TEST  0
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
 
    status = pj_file_open(NULL, FILENAME, PJ_O_WRONLY, &fd);
56
 
    if (status != PJ_SUCCESS) {
57
 
        app_perror("...file_open() error", status);
58
 
        return -10;
59
 
    }
60
 
 
61
 
    size = sizeof(buffer);
62
 
    status = pj_file_write(fd, buffer, &size);
63
 
    if (status != PJ_SUCCESS) {
64
 
        app_perror("...file_write() error", status);
65
 
        pj_file_close(fd);
66
 
        return -20;
67
 
    }
68
 
    if (size != sizeof(buffer))
69
 
        return -25;
70
 
 
71
 
    status = pj_file_close(fd);
72
 
    if (status != PJ_SUCCESS) {
73
 
        app_perror("...file_close() error", status);
74
 
        return -30;
75
 
    }
76
 
 
77
 
    /* Check the file existance and size. */
78
 
    if (!pj_file_exists(FILENAME))
79
 
        return -40;
80
 
 
81
 
    if (pj_file_size(FILENAME) != sizeof(buffer))
82
 
        return -50;
83
 
 
84
 
    /* Get file stat. */
85
 
    status = pj_file_getstat(FILENAME, &stat);
86
 
    if (status != PJ_SUCCESS)
87
 
        return -60;
88
 
 
89
 
    /* Check stat size. */
90
 
    if (stat.size != sizeof(buffer))
91
 
        return -70;
92
 
 
93
 
#if INCLUDE_FILE_TIME_TEST
94
 
    /* Check file creation time >= start_time. */
95
 
    if (!PJ_TIME_VAL_GTE(stat.ctime, start_time))
96
 
        return -80;
97
 
    /* Check file creation time is not much later. */
98
 
    PJ_TIME_VAL_SUB(stat.ctime, start_time);
99
 
    if (stat.ctime.sec > FILE_MAX_AGE)
100
 
        return -90;
101
 
 
102
 
    /* Check file modification time >= start_time. */
103
 
    if (!PJ_TIME_VAL_GTE(stat.mtime, start_time))
104
 
        return -80;
105
 
    /* Check file modification time is not much later. */
106
 
    PJ_TIME_VAL_SUB(stat.mtime, start_time);
107
 
    if (stat.mtime.sec > FILE_MAX_AGE)
108
 
        return -90;
109
 
 
110
 
    /* Check file access time >= start_time. */
111
 
    if (!PJ_TIME_VAL_GTE(stat.atime, start_time))
112
 
        return -80;
113
 
    /* Check file access time is not much later. */
114
 
    PJ_TIME_VAL_SUB(stat.atime, start_time);
115
 
    if (stat.atime.sec > FILE_MAX_AGE)
116
 
        return -90;
117
 
#endif
118
 
 
119
 
    /*
120
 
     * Re-open the file and read data.
121
 
     */
122
 
    status = pj_file_open(NULL, FILENAME, PJ_O_RDONLY, &fd);
123
 
    if (status != PJ_SUCCESS) {
124
 
        app_perror("...file_open() error", status);
125
 
        return -100;
126
 
    }
127
 
 
128
 
    size = 0;
129
 
    while (size < (pj_ssize_t)sizeof(readbuf)) {
130
 
        pj_ssize_t read;
131
 
        read = 1;
132
 
        status = pj_file_read(fd, &readbuf[size], &read);
133
 
        if (status != PJ_SUCCESS) {
134
 
            PJ_LOG(3,("", "...error reading file after %d bytes (error follows)", 
135
 
                      size));
136
 
            app_perror("...error", status);
137
 
            return -110;
138
 
        }
139
 
        if (read == 0) {
140
 
            // EOF
141
 
            break;
142
 
        }
143
 
        size += read;
144
 
    }
145
 
 
146
 
    if (size != sizeof(buffer))
147
 
        return -120;
148
 
 
149
 
    /*
150
 
    if (!pj_file_eof(fd, PJ_O_RDONLY))
151
 
        return -130;
152
 
     */
153
 
 
154
 
    if (pj_memcmp(readbuf, buffer, size) != 0)
155
 
        return -140;
156
 
 
157
 
    /* Seek test. */
158
 
    status = pj_file_setpos(fd, 4, PJ_SEEK_SET);
159
 
    if (status != PJ_SUCCESS) {
160
 
        app_perror("...file_setpos() error", status);
161
 
        return -141;
162
 
    }
163
 
 
164
 
    /* getpos test. */
165
 
    status = pj_file_getpos(fd, &pos);
166
 
    if (status != PJ_SUCCESS) {
167
 
        app_perror("...file_getpos() error", status);
168
 
        return -142;
169
 
    }
170
 
    if (pos != 4)
171
 
        return -143;
172
 
 
173
 
    status = pj_file_close(fd);
174
 
    if (status != PJ_SUCCESS) {
175
 
        app_perror("...file_close() error", status);
176
 
        return -150;
177
 
    }
178
 
 
179
 
    /*
180
 
     * Rename test.
181
 
     */
182
 
    status = pj_file_move(FILENAME, NEWNAME);
183
 
    if (status != PJ_SUCCESS) {
184
 
        app_perror("...file_move() error", status);
185
 
        return -160;
186
 
    }
187
 
 
188
 
    if (pj_file_exists(FILENAME))
189
 
        return -170;
190
 
    if (!pj_file_exists(NEWNAME))
191
 
        return -180;
192
 
 
193
 
    if (pj_file_size(NEWNAME) != sizeof(buffer))
194
 
        return -190;
195
 
 
196
 
    /* Delete test. */
197
 
    status = pj_file_delete(NEWNAME);
198
 
    if (status != PJ_SUCCESS) {
199
 
        app_perror("...file_delete() error", status);
200
 
        return -200;
201
 
    }
202
 
 
203
 
    if (pj_file_exists(NEWNAME))
204
 
        return -210;
205
 
 
206
 
    PJ_LOG(3,("", "...success"));
207
 
    return PJ_SUCCESS;
208
 
}
209
 
 
210
 
 
211
 
int file_test(void)
212
 
{
213
 
    int rc = file_test_internal();
214
 
 
215
 
    /* Delete test file if exists. */
216
 
    if (pj_file_exists(FILENAME))
217
 
        pj_file_delete(FILENAME);
218
 
 
219
 
    return rc;
220
 
}
221
 
 
222
 
#else
223
 
int dummy_file_test;
224
 
#endif
225