summaryrefslogtreecommitdiff
path: root/test/testfile.c
blob: efbe5fb9e0dbc459b76d879865092df94ce56712 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/* ====================================================================
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Apache" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "apr_file_io.h"
#include "apr_network_io.h"
#include "apr_errno.h"
#include "apr_general.h"
#include "apr_lib.h"
#ifdef BEOS
#include <unistd.h>
#endif

int test_filedel(apr_pool_t *);
int testdirs(apr_pool_t *);
static void test_read(apr_pool_t *);

int main()
{
    apr_pool_t *context;
    apr_pool_t *cont2;
    apr_file_t *thefile = NULL;
    apr_socket_t *testsock = NULL;
    apr_pollfd_t *sdset = NULL;
    apr_status_t status = 0;
    apr_int32_t flag = APR_READ | APR_WRITE | APR_CREATE;
    apr_ssize_t nbytes = 0;
    apr_int32_t rv;
    apr_off_t zer = 0;
    char *buf;
    char *str;
    char *filename = "test.fil";
    char *teststr;

    if (apr_initialize() != APR_SUCCESS) {
        fprintf(stderr, "Couldn't initialize.");
        exit(-1);
    }
    atexit(apr_terminate);
    if (apr_create_pool(&context, NULL) != APR_SUCCESS) {
        fprintf(stderr, "Couldn't allocate context.");
        exit(-1);
    }
    if (apr_create_pool(&cont2, context) != APR_SUCCESS) {
        fprintf(stderr, "Couldn't allocate context.");
        exit(-1);
    }

    fprintf(stdout, "Testing file functions.\n");

    fprintf(stdout, "\tOpening file.......");
    if (apr_open(&thefile, filename, flag, APR_UREAD | APR_UWRITE | APR_GREAD, context) != APR_SUCCESS) {
        perror("Didn't open file");
        exit(-1);
    }
    else {
        fprintf(stdout, "OK\n");
    }
    
    fprintf(stdout, "\tChecking file.......");
    if (thefile == NULL) {
        fprintf(stderr, "Bad file des\n");
        exit(-1);
    }
    apr_get_filename(&str, thefile);
    if (strcmp(str, filename) != 0) {
        fprintf(stderr, "wrong filename\n");
        exit(-1);
    }
    else {
        fprintf(stdout, "OK\n");
    }

    fprintf(stdout, "\tWriting to file.......");
    
    nbytes = (apr_ssize_t)strlen("this is a test");
    if (apr_write(thefile, "this is a test", &nbytes) != APR_SUCCESS) {
        perror("something's wrong");
        exit(-1);
    }
    if (nbytes != (apr_ssize_t)strlen("this is a test")) {
        fprintf(stderr, "didn't write properly.\n");
        exit(-1);
    }
    else {
        fprintf(stdout, "OK\n");
    }

    fprintf(stdout, "\tMoving to start of file.......");
    zer = 0;
    if (apr_seek(thefile, SEEK_SET, &zer) != 0) {
        perror("couldn't seek to beginning of file.");
        exit(-1);
    }
    else {
        fprintf(stdout, "OK\n");
    }

#if APR_FILES_AS_SOCKETS
    fprintf(stdout, "\tThis platform supports files_like_sockets\n");
    fprintf(stdout, "\t\tMaking file look like a socket.......");
    if (apr_socket_from_file(&testsock, thefile) != APR_SUCCESS) {
        perror("Something went wrong");
        exit(-1);
    }
    fprintf(stdout, "OK\n");

    fprintf(stdout, "\t\tChecking for incoming data.......");
    apr_setup_poll(&sdset, 1, context);
    apr_add_poll_socket(sdset, testsock, APR_POLLIN);
    rv = 1;
    if (apr_poll(sdset, &rv, -1) != APR_SUCCESS) {
        fprintf(stderr, "Select caused an error\n");
        exit(-1);
    }
    else if (rv == 0) {
        fprintf(stderr, "I should not return until rv == 1\n");
        exit(-1);
    }
    fprintf(stdout, "OK\n");
#endif


    fprintf(stdout, "\tReading from the file.......");
    nbytes = (apr_ssize_t)strlen("this is a test");
    buf = (char *)apr_palloc(context, nbytes + 1);
    if (apr_read(thefile, buf, &nbytes) != APR_SUCCESS) {
        perror("something's wrong");
        exit(-1);
    }
    if (nbytes != (apr_ssize_t)strlen("this is a test")) {
        fprintf(stderr, "didn't read properly.\n");
        exit(-1);
    }
    else {
        fprintf(stdout, "OK\n");
    }

    fprintf(stdout, "\tAdding user data to the file.......");
    status = apr_set_filedata(thefile, "This is a test", "test", apr_null_cleanup);
    if (status  != APR_SUCCESS) {
        fprintf(stderr, "Couldn't add the data\n");
        exit(-1); 
    }
    else {
        fprintf(stdout, "OK\n");
    }

    fprintf(stdout, "\tGetting user data from the file.......");
    status = apr_get_filedata((void **)&teststr, "test", thefile);
    if (status  != APR_SUCCESS || strcmp(teststr, "This is a test")) {
        fprintf(stderr, "Couldn't get the data\n");
        exit(-1); 
    }
    else {
        fprintf(stdout, "OK\n");
    }

    fprintf(stdout, "\tClosing File.......");
    status = apr_close(thefile);
    if (status  != APR_SUCCESS) {
        fprintf(stderr, "Couldn't close the file\n");
        exit(-1); 
    }
    else {
        fprintf(stdout, "OK\n");
    }
    
    fprintf(stdout, "\tDeleting file.......");
    status = apr_remove_file(filename, context);
    if (status  != APR_SUCCESS) {
        fprintf(stderr, "Couldn't delete the file\n");
        exit(-1); 
    }
    else {
        fprintf(stdout, "OK\n");
    }
    
    fprintf(stdout, "\tMaking sure it's gone.......");
    status = apr_open(&thefile, filename, APR_READ, APR_UREAD | APR_UWRITE | APR_GREAD, context);
    if (status == APR_SUCCESS) {
        fprintf(stderr, "I could open the file for some reason?\n");
        exit(-1);
    }
    else {
        fprintf(stdout, "OK\n");
    }

    testdirs(context); 
    test_filedel(context);
    test_read(context);

    return 1;
}

int test_filedel(apr_pool_t *context)
{
    apr_file_t *thefile = NULL;
    apr_int32_t flag = APR_READ | APR_WRITE | APR_CREATE;
    apr_status_t stat;
  
    stat = apr_open(&thefile, "testdel", flag, APR_UREAD | APR_UWRITE | APR_GREAD, context);
    if (stat != APR_SUCCESS) {
         return stat;
    }

    if ((stat = apr_close(thefile))  != APR_SUCCESS) {
        return stat;
    }

    if ((stat = apr_remove_file("testdel", context))  != APR_SUCCESS) {
        return stat;
    }

    stat = apr_open(&thefile, "testdel", APR_READ, APR_UREAD | APR_UWRITE | APR_GREAD, context);
    if (stat == APR_SUCCESS) {
        return stat;
    }
  
    return APR_SUCCESS;
}

int testdirs(apr_pool_t *context)
{
    apr_dir_t *temp;  
    apr_file_t *file = NULL;
    apr_ssize_t bytes;
    apr_filetype_e type;
    char *fname;

    fprintf(stdout, "Testing Directory functions.\n");

    fprintf(stdout, "\tMakeing Directory.......");
    if (apr_make_dir("testdir", APR_UREAD | APR_UWRITE | APR_UEXECUTE | APR_GREAD | APR_GWRITE | APR_GEXECUTE | APR_WREAD | APR_WWRITE | APR_WEXECUTE, context)  != APR_SUCCESS) {
        fprintf(stderr, "Could not create directory\n");
        return -1;
    }
    else {
        fprintf(stdout, "OK\n");
    }

    if (apr_open(&file, "testdir/testfile", APR_READ | APR_WRITE | APR_CREATE, APR_UREAD | APR_UWRITE | APR_UEXECUTE, context) != APR_SUCCESS) {;
        return -1;
    }

    bytes = strlen("Another test!!!");
    apr_write(file, "Another test!!", &bytes); 
	apr_close(file);

    fprintf(stdout, "\tOpening Directory.......");
    if (apr_opendir(&temp, "testdir", context) != APR_SUCCESS) {
        fprintf(stderr, "Could not open directory\n");
        return -1;
    }
    else {
        fprintf(stdout, "OK\n");
    }

    fprintf(stdout, "\tReading Directory.......");
    if ((apr_readdir(temp))  != APR_SUCCESS) {
        fprintf(stderr, "Could not read directory\n");
        return -1;
    }
    else {
        fprintf(stdout, "OK\n");
    }
   
    fprintf(stdout, "\tGetting Information about the file.......\n");
    fprintf(stdout, "\t\tFile name.......");
    do {
        /* Because I want the file I created, I am skipping the "." and ".."
         * files that are here. 
         */
        if (apr_readdir(temp) != APR_SUCCESS) {
            fprintf(stderr, "Error reading directory testdir"); 
            return -1;
        }
        apr_get_dir_filename(&fname, temp);
    } while (fname[0] == '.');
    if (strcmp(fname, "testfile")) {
        fprintf(stderr, "Got wrong file name %s\n", fname);
        return -1;
    }
    fprintf(stdout, "OK\n");

    fprintf(stdout, "\t\tFile type.......");
    apr_dir_entry_ftype(&type, temp);
    if (type != APR_REG) {
        fprintf(stderr, "Got wrong file type\n");
        return -1;
    }
    fprintf(stdout, "OK\n");

    fprintf(stdout, "\t\tFile size.......");
    apr_dir_entry_size(&bytes, temp);
    if (bytes != (apr_ssize_t)strlen("Another test!!!")) {
        fprintf(stderr, "Got wrong file size %d\n", bytes);
        return -1;
    }
    fprintf(stdout, "OK\n");
     
    fprintf(stdout, "\tRewinding directory.......");
    apr_rewinddir(temp); 
    fprintf(stdout, "OK\n");
    
    fprintf(stdout, "\tClosing Directory.......");
    if (apr_closedir(temp)  != APR_SUCCESS) {
        fprintf(stderr, "Could not close directory\n");
        return -1;
    }
    else {
        fprintf(stdout, "OK\n");
    }

    fprintf(stdout, "\tRemoving file from directory.......");
    if (apr_remove_file("testdir/testfile", context)  != APR_SUCCESS) {
        fprintf(stderr, "Could not remove file\n");
        return -1;
    }
    else {
        fprintf(stdout, "OK\n");
    }

    fprintf(stdout, "\tRemoving Directory.......");
    if (apr_remove_dir("testdir", context)  != APR_SUCCESS) {
        fprintf(stderr, "Could not remove directory\n");
        return -1;
    }
    else {
        fprintf(stdout, "OK\n");
    }
    
    return 1; 
}    

#define TESTREAD_BLKSIZE 1024
#define APR_BUFFERSIZE   4096 /* This should match APR's buffer size. */

static void create_testread(apr_pool_t *p, const char *fname)
{
    apr_file_t *f = NULL;
    apr_status_t rv;
    char buf[TESTREAD_BLKSIZE];
    apr_ssize_t nbytes;

    /* Create a test file with known content.
     */
    rv = apr_open(&f, fname, APR_CREATE | APR_WRITE | APR_TRUNCATE, APR_UREAD | APR_UWRITE, p);
    if (rv) {
        fprintf(stderr, "apr_open()->%d/%s\n",
                rv, apr_strerror(rv, buf, sizeof buf));
        exit(1);
    }
    nbytes = 4;
    rv = apr_write(f, "abc\n", &nbytes);
    assert(!rv && nbytes == 4);
    memset(buf, 'a', sizeof buf);
    nbytes = sizeof buf;
    rv = apr_write(f, buf, &nbytes);
    assert(!rv && nbytes == sizeof buf);
    nbytes = 2;
    rv = apr_write(f, "\n\n", &nbytes);
    assert(!rv && nbytes == 2);
    rv = apr_close(f);
    assert(!rv);
}

static char read_one(apr_file_t *f, int expected)
{
  char bytes[3];
  apr_status_t rv;
  static int counter = 0;
  apr_ssize_t nbytes;

  counter += 1;

  bytes[0] = bytes[2] = 0x01;
  if (counter % 2) {
      rv = apr_getc(bytes + 1, f);
  }
  else {
      nbytes = 1;
      rv = apr_read(f, bytes + 1, &nbytes);
      assert(nbytes == 1);
  }
  assert(!rv);
  assert(bytes[0] == 0x01 && bytes[2] == 0x01);
  if (expected != -1) {
      assert(bytes[1] == expected);
  }
  return bytes[1];
}

static void test_read_guts(apr_pool_t *p, const char *fname, apr_int32_t extra_flags)
{
    apr_file_t *f = NULL;
    apr_status_t rv;
    apr_ssize_t nbytes;
    char buf[1024];
    int i;

    rv = apr_open(&f, fname, APR_READ | extra_flags, 0, p);
    assert(!rv);
    read_one(f, 'a');
    read_one(f, 'b');
    if (extra_flags & APR_BUFFERED) {
        fprintf(stdout, 
                "\n\t\tskipping apr_ungetc() for APR_BUFFERED... "
                "doesn't work yet...\n\t\t\t\t ");
    }
    else {
        rv = apr_ungetc('b', f);
        assert(!rv);
        /* Note: some implementations move the file ptr back;
         *       others just save up to one char; it isn't 
         *       portable to unget more than once.
         */
        /* Don't do this: rv = apr_ungetc('a', f); */
        read_one(f, 'b');
    }
    read_one(f, 'c');
    read_one(f, '\n');
    for (i = 0; i < TESTREAD_BLKSIZE; i++) {
        read_one(f, 'a');
    }
    read_one(f, '\n');
    read_one(f, '\n');
    rv = apr_getc(buf, f);
    assert(rv == APR_EOF);
    rv = apr_close(f);
    assert(!rv);

    f = NULL;
    rv = apr_open(&f, fname, APR_READ | extra_flags, 0, p);
    assert(!rv);
    rv = apr_fgets(buf, 10, f);
    assert(!rv);
    assert(!strcmp(buf, "abc\n"));
    /* read first 800 of TESTREAD_BLKSIZE 'a's 
     */
    rv = apr_fgets(buf, 801, f);
    assert(!rv);
    assert(strlen(buf) == 800);
    for (i = 0; i < 800; i++) {
        assert(buf[i] == 'a');
    }
    /* read rest of the 'a's and the first newline
     */
    rv = apr_fgets(buf, sizeof buf, f);
    assert(!rv);
    assert(strlen(buf) == TESTREAD_BLKSIZE - 800 + 1);
    for (i = 0; i < TESTREAD_BLKSIZE - 800; i++) {
        assert(buf[i] == 'a');
    }
    assert(buf[TESTREAD_BLKSIZE - 800] == '\n');
    /* read the last newline
     */
    rv = apr_fgets(buf, sizeof buf, f);
    assert(!rv);
    assert(!strcmp(buf, "\n"));
    /* get APR_EOF
     */
    rv = apr_fgets(buf, sizeof buf, f);
    assert(rv == APR_EOF);
    /* get APR_EOF with apr_getc
     */
    rv = apr_getc(buf, f);
    assert(rv == APR_EOF);
    /* get APR_EOF with apr_read
     */
    nbytes = sizeof buf;
    rv = apr_read(f, buf, &nbytes);
    assert(rv == APR_EOF);
    rv = apr_close(f);
    assert(!rv);
}

static void test_bigread(apr_pool_t *p, const char *fname, apr_int32_t extra_flags)
{
    apr_file_t *f = NULL;
    apr_status_t rv;
    char buf[APR_BUFFERSIZE * 2];
    apr_ssize_t nbytes;

    /* Create a test file with known content.
     */
    rv = apr_open(&f, fname, APR_CREATE | APR_WRITE | APR_TRUNCATE, APR_UREAD | APR_UWRITE, p);
    if (rv) {
        fprintf(stderr, "apr_open()->%d/%s\n",
                rv, apr_strerror(rv, buf, sizeof buf));
        exit(1);
    }
    nbytes = APR_BUFFERSIZE;
    memset(buf, 0xFE, nbytes);
    rv = apr_write(f, buf, &nbytes);
    assert(!rv && nbytes == APR_BUFFERSIZE);
    rv = apr_close(f);
    assert(!rv);

    f = NULL;
    rv = apr_open(&f, fname, APR_READ | extra_flags, 0, p);
    assert(!rv);
    nbytes = sizeof buf;
    rv = apr_read(f, buf, &nbytes);
    assert(!rv);
    assert(nbytes == APR_BUFFERSIZE);
    rv = apr_close(f);
    assert(!rv);
}

static void test_read(apr_pool_t *p)
{
    const char *fname = "testread.dat";
    apr_status_t rv;

    fprintf(stdout, "Testing file read functions.\n");

    create_testread(p, fname);
    fprintf(stdout, "\tBuffered file tests......");
    test_read_guts(p, fname, APR_BUFFERED);
    fprintf(stdout, "OK\n");
    fprintf(stdout, "\tUnbuffered file tests....");
    test_read_guts(p, fname, 0);
    fprintf(stdout, "OK\n");
    fprintf(stdout, "\tMore buffered file tests......");
    test_bigread(p, fname, APR_BUFFERED);
    fprintf(stdout, "OK\n");
    fprintf(stdout, "\tMore unbuffered file tests......");
    test_bigread(p, fname, 0);
    fprintf(stdout, "OK\n");
    rv = apr_remove_file(fname, p);
    assert(!rv);
    fprintf(stdout, "\tAll read tests...........OK\n");
}