apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r..@apache.org
Subject cvs commit: apr/test Makefile.in test_apr.h testall.c testfile.c
Date Sun, 10 Nov 2002 02:44:51 GMT
rbb         2002/11/09 18:44:51

  Modified:    test     Makefile.in test_apr.h testall.c testfile.c
  Log:
  Migrate testfile to the new test suite.
  
  Revision  Changes    Path
  1.101     +2 -2      apr/test/Makefile.in
  
  Index: Makefile.in
  ===================================================================
  RCS file: /home/cvs/apr/test/Makefile.in,v
  retrieving revision 1.100
  retrieving revision 1.101
  diff -u -r1.100 -r1.101
  --- Makefile.in	24 Oct 2002 00:20:33 -0000	1.100
  +++ Makefile.in	10 Nov 2002 02:44:50 -0000	1.101
  @@ -165,8 +165,8 @@
   testmutexscope@EXEEXT@: testmutexscope.lo $(LOCAL_LIBS)
   	$(LINK) testmutexscope.lo $(LOCAL_LIBS) $(ALL_LIBS)
   
  -testall: testall.lo testtime.lo teststr.lo testvsn.lo testipsub.lo testmmap.lo testud.lo
testtable.lo testsleep.lo testpools.lo testfmt.lo CuTest.lo $(LOCAL_LIBS)
  -	$(LINK) testall.lo testtime.lo teststr.lo testvsn.lo testipsub.lo testmmap.lo testud.lo
testtable.lo testsleep.lo testpools.lo testfmt.lo CuTest.lo $(LOCAL_LIBS) $(ALL_LIBS)
  +testall: testall.lo testtime.lo teststr.lo testvsn.lo testipsub.lo testmmap.lo testud.lo
testtable.lo testsleep.lo testpools.lo testfmt.lo testfile.lo CuTest.lo $(LOCAL_LIBS)
  +	$(LINK) testall.lo testtime.lo teststr.lo testvsn.lo testipsub.lo testmmap.lo testud.lo
testtable.lo testsleep.lo testpools.lo testfmt.lo testfile.lo CuTest.lo $(LOCAL_LIBS) $(ALL_LIBS)
   
   
   # DO NOT REMOVE
  
  
  
  1.19      +1 -0      apr/test/test_apr.h
  
  Index: test_apr.h
  ===================================================================
  RCS file: /home/cvs/apr/test/test_apr.h,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- test_apr.h	24 Oct 2002 00:20:33 -0000	1.18
  +++ test_apr.h	10 Nov 2002 02:44:50 -0000	1.19
  @@ -76,6 +76,7 @@
   CuSuite *testsleep(void);
   CuSuite *testpool(void);
   CuSuite *testfmt(void);
  +CuSuite *testfile(void);
   
   
   
  
  
  
  1.12      +1 -0      apr/test/testall.c
  
  Index: testall.c
  ===================================================================
  RCS file: /home/cvs/apr/test/testall.c,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- testall.c	24 Oct 2002 00:20:33 -0000	1.11
  +++ testall.c	10 Nov 2002 02:44:50 -0000	1.12
  @@ -74,6 +74,7 @@
       testsleep,
       testpool,
       testfmt,
  +    testfile,
       NULL
   };
   
  
  
  
  1.53      +289 -453  apr/test/testfile.c
  
  Index: testfile.c
  ===================================================================
  RCS file: /home/cvs/apr/test/testfile.c,v
  retrieving revision 1.52
  retrieving revision 1.53
  diff -u -r1.52 -r1.53
  --- testfile.c	10 Nov 2002 02:08:16 -0000	1.52
  +++ testfile.c	10 Nov 2002 02:44:50 -0000	1.53
  @@ -52,10 +52,6 @@
    * <http://www.apache.org/>.
    */
   
  -#include <assert.h>
  -#include <stdio.h>
  -#include <stdlib.h>
  -#include <string.h>
   #include "apr_file_io.h"
   #include "apr_file_info.h"
   #include "apr_network_io.h"
  @@ -65,496 +61,302 @@
   #include "apr_lib.h"
   #include "test_apr.h"
   
  -struct view_fileinfo
  +#define FILENAME "data/file_datafile.txt"
  +#define TESTSTR  "This is the file data file."
  +
  +#define TESTREAD_BLKSIZE 1024
  +#define APR_BUFFERSIZE   4096 /* This should match APR's buffer size. */
  +
  +
  +static apr_file_t *filetest = NULL;
  +
  +static void test_open_noreadwrite(CuTest *tc)
   {
  -    apr_int32_t bits;
  -    char *description;
  -} vfi[] = {
  -    {APR_FINFO_MTIME,  "MTIME"},
  -    {APR_FINFO_CTIME,  "CTIME"},
  -    {APR_FINFO_ATIME,  "ATIME"},
  -    {APR_FINFO_SIZE,   "SIZE"},
  -    {APR_FINFO_DEV,    "DEV"},
  -    {APR_FINFO_INODE,  "INODE"},
  -    {APR_FINFO_NLINK,  "NLINK"},
  -    {APR_FINFO_TYPE,   "TYPE"},
  -    {APR_FINFO_USER,   "USER"}, 
  -    {APR_FINFO_GROUP,  "GROUP"}, 
  -    {APR_FINFO_UPROT,  "UPROT"}, 
  -    {APR_FINFO_GPROT,  "GPROT"},
  -    {APR_FINFO_WPROT,  "WPROT"},
  -    {0,                NULL}
  -}; 
  -
  -void test_filedel(apr_pool_t *);
  -static void test_read(apr_pool_t *);
  -static void test_read_seek(apr_int32_t, apr_pool_t *);
  -static void test_mod_neg(apr_pool_t *, apr_int32_t);
  +    apr_status_t rv;
  +    apr_file_t *thefile = NULL;
   
  -int main(void)
  +    rv = apr_file_open(&thefile, FILENAME,
  +                       APR_CREATE | APR_EXCL, 
  +                       APR_UREAD | APR_UWRITE | APR_GREAD, p);
  +    CuAssertTrue(tc, rv != APR_SUCCESS);
  +    CuAssertIntEquals(tc, APR_EACCES, rv);
  +#if 0
  +    /* I consider this a bug, if we are going to return an error, we shouldn't
  +     * allocate the file pointer.  But, this would make us fail the text, so
  +     * I am commenting it out for now.
  +     */
  +    CuAssertPtrEquals(tc, NULL, thefile); 
  +#endif
  +}
  +
  +static void test_open_excl(CuTest *tc)
   {
  -    apr_pool_t *pool;
  +    apr_status_t rv;
       apr_file_t *thefile = NULL;
  -    apr_finfo_t finfo;
  -    apr_socket_t *testsock = NULL;
  -    apr_pollfd_t *sdset = NULL;
  -    apr_status_t status;
  -    apr_int32_t flag = APR_READ | APR_WRITE | APR_CREATE;
  -    apr_size_t nbytes = 0;
  -    apr_off_t zer = 0;
  -    char *buf;
  -    const char *str;
  -    char *filename = "test.fil";
  -    char *teststr;
  -    apr_uid_t uid;
  -    apr_gid_t gid;
  -#if APR_FILES_AS_SOCKETS
  -    apr_int32_t num;
  +
  +    rv = apr_file_open(&thefile, FILENAME,
  +                       APR_CREATE | APR_EXCL | APR_WRITE, 
  +                       APR_UREAD | APR_UWRITE | APR_GREAD, p);
  +    CuAssertTrue(tc, rv != APR_SUCCESS);
  +    CuAssertIntEquals(tc, 1, APR_STATUS_IS_EEXIST(rv));
  +#if 0
  +    /* I consider this a bug, if we are going to return an error, we shouldn't
  +     * allocate the file pointer.  But, this would make us fail the text, so
  +     * I am commenting it out for now.
  +     */
  +    CuAssertPtrEquals(tc, NULL, thefile); 
   #endif
  +}
   
  -    printf("APR File Functions Test\n=======================\n\n");
  -    
  -    STD_TEST_NEQ("Initializing APR", apr_initialize())
  -    atexit(apr_terminate);
  -    STD_TEST_NEQ("Creating the main pool we'll use", 
  -                 apr_pool_create(&pool, NULL))
  -    STD_TEST_NEQ("Creating the second pool we'll use", 
  -                 apr_pool_create(&pool, NULL))
  -    
  +static void test_open_read(CuTest *tc)
  +{
  +    apr_status_t rv;
   
  -    fprintf(stdout, "Testing file functions.\n");
  +    rv = apr_file_open(&filetest, FILENAME, 
  +                       APR_READ, 
  +                       APR_UREAD | APR_UWRITE | APR_GREAD, p);
  +    CuAssertIntEquals(tc, rv, APR_SUCCESS);
  +    CuAssertPtrNotNull(tc, filetest);
  +}
   
  -    STD_TEST_NEQ("    Opening file", 
  -                 apr_file_open(&thefile, filename, flag, 
  -                               APR_UREAD | APR_UWRITE | APR_GREAD, pool))
  +static void test_read(CuTest *tc)
  +{
  +    apr_status_t rv;
  +    apr_size_t nbytes = 256;
  +    char *str = apr_palloc(p, nbytes + 1);
       
  -    printf("%-60s", "    Checking filename");
  -    if (thefile == NULL){
  -        MSG_AND_EXIT("\nBad file descriptor")
  -    }
  -    apr_file_name_get(&str, thefile);
  -    printf("%s\n", str);
  -    if (strcmp(str, filename) != 0){
  -        MSG_AND_EXIT("Wrong filename\n")
  -    }
  +    rv = apr_file_read(filetest, str, &nbytes);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertIntEquals(tc, strlen(TESTSTR), nbytes);
  +    CuAssertStrEquals(tc, TESTSTR, str);
  +}
  +
  +static void test_filename(CuTest *tc)
  +{
  +    const char *str;
  +    apr_status_t rv;
  +
  +    rv = apr_file_name_get(&str, filetest);
  +    CuAssertIntEquals(tc, rv, APR_SUCCESS);
  +    CuAssertStrEquals(tc, FILENAME, str);
  +}
       
  -    nbytes = strlen("this is a test");
  -    STD_TEST_NEQ("    Writing to the file", 
  -                 apr_file_write(thefile, "this is a test", &nbytes))
  -    TEST_NEQ("    Checking we wrote everything", nbytes,
  -             strlen("this is a test"), "OK", "Failed to write everything")
  -
  -    zer = 0;
  -    STD_TEST_NEQ("    Moving to the start of file", 
  -                 apr_file_seek(thefile, SEEK_SET, &zer))
  -                 
  -#if APR_FILES_AS_SOCKETS
  -    printf("    This platform supports files_like_sockets, testing...\n");
  -    STD_TEST_NEQ("        Making file look like a socket",
  -                 apr_socket_from_file(&testsock, thefile))
  -
  -    apr_poll_setup(&sdset, 1, pool);
  -    apr_poll_socket_add(sdset, testsock, APR_POLLIN);
  -    num = 1;
  -    STD_TEST_NEQ_NONFATAL("        Checking for incoming data",
  -      apr_poll(sdset, 1, &num, apr_time_from_sec(1)));
  -    if (num == 0) {
  -        printf("** This platform doesn't return readability on a regular file.**\n");
  -    }
  -    printf("    End of files as sockets test.\n");
  -#endif
  +static void test_fileclose(CuTest *tc)
  +{
  +    char *str;
  +    apr_status_t rv;
  +    apr_size_t one = 1;
   
  -    nbytes = strlen("this is a test");
  -    buf = (char *)apr_palloc(pool, nbytes + 1);
  -    STD_TEST_NEQ("    Reading from the file",
  -                 apr_file_read(thefile, buf, &nbytes))
  -    TEST_NEQ("    Checking what we read", nbytes, strlen("this is a test"),
  -             "OK", "We didn't read properly.\n")
  -    STD_TEST_NEQ("    Adding user data to the file",
  -                 apr_file_data_set(thefile, "This is a test",
  -                                   "test", apr_pool_cleanup_null))
  -    STD_TEST_NEQ("    Getting user data from the file",
  -                 apr_file_data_get((void **)&teststr, "test", thefile))
  -    TEST_NEQ("    Checking the data we got", strcmp(teststr, "This is a test"), 0,
  -             "OK", "Got the data, but it was wrong")
  -
  -    printf("%-60s", "    Getting fileinfo");
  -    status = apr_file_info_get(&finfo, APR_FINFO_NORM, thefile);
  -    if (status  == APR_INCOMPLETE) {
  -	int i;
  -        printf("INCOMPLETE\n");
  -        for (i = 0; vfi[i].bits; ++i)
  -            if (vfi[i].bits & ~finfo.valid)
  -                fprintf(stderr, "\t    Missing %s\n", vfi[i].description);
  -    }
  -    else if (status != APR_SUCCESS) {
  -        printf("OK\n");
  -        MSG_AND_EXIT("Couldn't get the fileinfo")
  -    }
  -    else {
  -        printf("OK\n");
  -    }
  -    gid = finfo.group;
  -    uid = finfo.user;
  +    rv = apr_file_close(filetest);
  +    CuAssertIntEquals(tc, rv, APR_SUCCESS);
  +    /* We just closed the file, so this should fail */
  +    rv = apr_file_read(filetest, str, &one);
  +    CuAssertIntEquals(tc, APR_EBADF, rv);
  +}
   
  -    STD_TEST_NEQ("    Closing the file", apr_file_close(thefile))
  +static void test_file_remove(CuTest *tc)
  +{
  +    apr_status_t rv;
   
  -    printf("%-60s", "    Stat'ing file");
  -    status = apr_stat(&finfo, filename, APR_FINFO_NORM, pool);
  -    if (status  == APR_INCOMPLETE) {
  -	int i;
  -        printf("INCOMPLETE\n");
  -        for (i = 0; vfi[i].bits; ++i)
  -            if (vfi[i].bits & ~finfo.valid)
  -                fprintf(stderr, "\t    Missing %s\n", vfi[i].description);
  -    }
  -    else if (status  != APR_SUCCESS) {
  -        printf("Failed\n");
  -        MSG_AND_EXIT("Couldn't stat the file")
  -    }
  -    else {
  -        printf("OK\n");
  -    }    
  -
  -    if (finfo.valid & APR_FINFO_GROUP) {
  -        STD_TEST_NEQ("    Getting groupname", 
  -                     apr_group_name_get(&buf, finfo.group, pool))
  -        STD_TEST_NEQ("    Comparing group ID's",
  -                     apr_compare_groups(finfo.group, gid))
  -        printf("     (gid's for %s match)\n", buf);
  -    }
  +    rv = apr_file_remove(FILENAME, p);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   
  -    if (finfo.valid & APR_FINFO_USER) {
  -        STD_TEST_NEQ("    Getting username", 
  -                     apr_get_username(&buf, finfo.user, pool))
  -        STD_TEST_NEQ("    Comparing users",
  -                     apr_compare_users(finfo.user, uid))
  -        printf("     (uid's for %s match)\n", buf);
  -    }
  +    rv = apr_file_open(&filetest, FILENAME, APR_READ, 
  +                       APR_UREAD | APR_UWRITE | APR_GREAD, p);
  +    CuAssertIntEquals(tc, 1, APR_STATUS_IS_ENOENT(rv));
  +}
   
  -    STD_TEST_NEQ("    Deleting the file", apr_file_remove(filename, pool))
  -    TEST_EQ("    Making sure it's gone",
  -           apr_file_open(&thefile, filename, APR_READ, 
  -                         APR_UREAD | APR_UWRITE | APR_GREAD, pool),
  -           APR_SUCCESS, "OK", "Failed")
  +static void test_open_write(CuTest *tc)
  +{
  +    apr_status_t rv;
   
  -    test_filedel(pool);
  -    test_read(pool);
  -    test_mod_neg(pool, 0); /* unbuffered */
  -    test_mod_neg(pool, APR_BUFFERED);
  +    filetest = NULL;
  +    rv = apr_file_open(&filetest, FILENAME, 
  +                       APR_WRITE, 
  +                       APR_UREAD | APR_UWRITE | APR_GREAD, p);
  +    CuAssertIntEquals(tc, APR_ENOENT, rv);
  +}
   
  -    apr_pool_destroy(pool);
  +static void test_open_writecreate(CuTest *tc)
  +{
  +    apr_status_t rv;
   
  -    printf("\nAll tests passed OK\n");
  -    return 0;
  +    filetest = NULL;
  +    rv = apr_file_open(&filetest, FILENAME, 
  +                       APR_WRITE | APR_CREATE, 
  +                       APR_UREAD | APR_UWRITE | APR_GREAD, p);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   }
   
  -void test_filedel(apr_pool_t *pool)
  +static void test_write(CuTest *tc)
   {
  -    apr_file_t *thefile = NULL;
  -    apr_int32_t flag = APR_READ | APR_WRITE | APR_CREATE;
  -  
  -    STD_TEST_NEQ("    Creating the file",
  -                 apr_file_open(&thefile, "testdel", flag, 
  -                               APR_UREAD | APR_UWRITE | APR_GREAD, pool))
  -    STD_TEST_NEQ("    Closing the file",
  -                 apr_file_close(thefile))
  -    STD_TEST_NEQ("    Removing the file", apr_file_remove("testdel", pool))
  -    TEST_EQ("    Checking it's gone",
  -            apr_file_open(&thefile, "testdel", APR_READ, 
  -                          APR_UREAD | APR_UWRITE | APR_GREAD, pool),
  -            APR_SUCCESS,
  -            "OK", "Failed")
  +    apr_status_t rv;
  +    apr_size_t bytes = strlen(TESTSTR);
  +
  +    rv = apr_file_write(filetest, TESTSTR, &bytes);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   }
   
  -#define TESTREAD_BLKSIZE 1024
  -#define APR_BUFFERSIZE   4096 /* This should match APR's buffer size. */
  +static void test_open_readwrite(CuTest *tc)
  +{
  +    apr_status_t rv;
  +
  +    apr_file_close(filetest);
  +    filetest = NULL;
  +    rv = apr_file_open(&filetest, FILENAME, 
  +                       APR_READ | APR_WRITE, 
  +                       APR_UREAD | APR_UWRITE | APR_GREAD, p);
  +    CuAssertIntEquals(tc, rv, APR_SUCCESS);
  +    CuAssertPtrNotNull(tc, filetest);
  +}
   
  -static void create_testread(apr_pool_t *p, const char *fname)
  +static void test_seek(CuTest *tc)
   {
  -    apr_file_t *f = NULL;
       apr_status_t rv;
  -    char buf[TESTREAD_BLKSIZE];
  -    apr_size_t nbytes;
  +    apr_off_t offset = 5;
  +    apr_size_t nbytes = 256;
  +    char *str = apr_palloc(p, nbytes + 1);
   
  -    /* Create a test file with known content.
  -     */
  +    rv = apr_file_seek(filetest, SEEK_SET, &offset);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
       
  -    rv = apr_file_open(&f, fname, APR_CREATE | APR_WRITE | APR_TRUNCATE, APR_UREAD
| APR_UWRITE, p);
  -    if (rv) {
  -        fprintf(stderr, "apr_file_open()->%d/%s\n",
  -                rv, apr_strerror(rv, buf, sizeof buf));
  -        exit(1);
  -    }
  -    nbytes = 4;
  -    rv = apr_file_write(f, "abc\n", &nbytes);
  -    assert(!rv && nbytes == 4);
  -    memset(buf, 'a', sizeof buf);
  -    nbytes = sizeof buf;
  -    rv = apr_file_write(f, buf, &nbytes);
  -    assert(!rv && nbytes == sizeof buf);
  -    nbytes = 2;
  -    rv = apr_file_write(f, "\n\n", &nbytes);
  -    assert(!rv && nbytes == 2);
  -    rv = apr_file_close(f);
  -    assert(!rv);
  -}
  +    rv = apr_file_read(filetest, str, &nbytes);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertIntEquals(tc, strlen(TESTSTR) - 5, nbytes);
  +    CuAssertStrEquals(tc, TESTSTR + 5, str);
  +}                
   
  -static char read_one(apr_file_t *f, int expected)
  +
  +static void test_userdata_set(CuTest *tc)
   {
  -  char bytes[3];
  -  apr_status_t rv;
  -  static int counter = 0;
  -  apr_size_t nbytes;
  -
  -  counter += 1;
  -
  -  bytes[0] = bytes[2] = 0x01;
  -  if (counter % 2) {
  -      rv = apr_file_getc(bytes + 1, f);
  -  }
  -  else {
  -      nbytes = 1;
  -      rv = apr_file_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];
  +    apr_status_t rv;
  +
  +    rv = apr_file_data_set(filetest, "This is a test",
  +                           "test", apr_pool_cleanup_null);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   }
   
  -static int test_read_guts(apr_pool_t *p, const char *fname, apr_int32_t extra_flags)
  +static void test_userdata_get(CuTest *tc)
   {
  -    apr_file_t *f = NULL;
       apr_status_t rv;
  -    apr_size_t nbytes;
  -    char buf[1024];
  -    int i;
  +    char *teststr;
   
  -    rv = apr_file_open(&f, fname, APR_READ | extra_flags, 0, p);
  -    assert(!rv);
  -    read_one(f, 'a');
  -    read_one(f, 'b');
  -    rv = apr_file_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_file_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_file_getc(buf, f);
  -    assert(rv == APR_EOF);
  -    rv = apr_file_close(f);
  -    assert(!rv);
  +    rv = apr_file_data_get((void **)&teststr, "test", filetest);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertStrEquals(tc, "This is a test", teststr);
  +}
   
  -    f = NULL;
  -    rv = apr_file_open(&f, fname, APR_READ | extra_flags, 0, p);
  -    assert(!rv);
  -    rv = apr_file_gets(buf, 10, f);
  -    assert(!rv);
  -    assert(!strcmp(buf, "abc\n"));
  -    /* read first 800 of TESTREAD_BLKSIZE 'a's 
  -     */
  -    rv = apr_file_gets(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_file_gets(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_file_gets(buf, sizeof buf, f);
  -    assert(!rv);
  -    assert(!strcmp(buf, "\n"));
  -    /* get APR_EOF
  -     */
  -    rv = apr_file_gets(buf, sizeof buf, f);
  -    assert(rv == APR_EOF);
  -    /* get APR_EOF with apr_file_getc
  -     */
  -    rv = apr_file_getc(buf, f);
  -    assert(rv == APR_EOF);
  -    /* get APR_EOF with apr_file_read
  -     */
  -    nbytes = sizeof buf;
  -    rv = apr_file_read(f, buf, &nbytes);
  -    assert(rv == APR_EOF);
  -    rv = apr_file_close(f);
  -    assert(!rv);
  -    return (1);
  +static void test_userdata_getnokey(CuTest *tc)
  +{
  +    apr_status_t rv;
  +    char *teststr;
  +
  +    rv = apr_file_data_get((void **)&teststr, "nokey", filetest);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertPtrEquals(tc, NULL, teststr);
   }
   
  -static void test_bigread(apr_pool_t *p, const char *fname, apr_int32_t extra_flags)
  +static void test_getc(CuTest *tc)
   {
       apr_file_t *f = NULL;
       apr_status_t rv;
  -    char buf[APR_BUFFERSIZE * 2];
  -    apr_size_t nbytes;
  +    char ch;
   
  -    /* Create a test file with known content.
  -     */
  -    rv = apr_file_open(&f, fname, APR_CREATE | APR_WRITE | APR_TRUNCATE, APR_UREAD
| APR_UWRITE, p);
  -    if (rv) {
  -        fprintf(stderr, "apr_file_open()->%d/%s\n",
  -                rv, apr_strerror(rv, buf, sizeof buf));
  -        exit(1);
  -    }
  -    nbytes = APR_BUFFERSIZE;
  -    memset(buf, 0xFE, nbytes);
  -    rv = apr_file_write(f, buf, &nbytes);
  -    assert(!rv && nbytes == APR_BUFFERSIZE);
  -    rv = apr_file_close(f);
  -    assert(!rv);
  +    rv = apr_file_open(&f, FILENAME, APR_READ, 0, p);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   
  -    f = NULL;
  -    rv = apr_file_open(&f, fname, APR_READ | extra_flags, 0, p);
  -    assert(!rv);
  -    nbytes = sizeof buf;
  -    rv = apr_file_read(f, buf, &nbytes);
  -    assert(!rv);
  -    assert(nbytes == APR_BUFFERSIZE);
  -    rv = apr_file_close(f);
  -    assert(!rv);
  +    apr_file_getc(&ch, f);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertIntEquals(tc, (int)TESTSTR[0], (int)ch);
  +    apr_file_close(f);
   }
   
  -static void test_read(apr_pool_t *p)
  +static void test_ungetc(CuTest *tc)
   {
  -    const char *fname = "testread.dat";
  +    apr_file_t *f = NULL;
       apr_status_t rv;
  +    char ch;
   
  -    printf("Testing file read functions.\n");
  -
  -    create_testread(p, fname);
  -    printf("%-60s", "    Buffered file tests");
  -    if (test_read_guts(p, fname, APR_BUFFERED))
  -        printf("OK\n");
  -    printf("%-60s", "    Unbuffered file tests");
  -    test_read_guts(p, fname, 0);
  -    printf("OK\n");
  -    printf("%-60s", "    More buffered file tests");
  -    test_bigread(p, fname, APR_BUFFERED);
  -    printf("OK\n");
  -    printf("%-60s", "    More unbuffered file tests");
  -    test_bigread(p, fname, 0);
  -    printf("OK\n");
  -    printf("%-60s", "    Even more buffered file tests");
  -    test_read_seek(APR_BUFFERED, p);
  -    printf("OK\n");
  -    printf("%-60s", "    Even more unbuffered file tests");
  -    test_read_seek(0, p);
  -    printf("OK\n");
  -    rv = apr_file_remove(fname, p);
  -    assert(!rv);
  -    printf("%-60s", "    All read tests");
  -    printf("OK\n");
  +    rv = apr_file_open(&f, FILENAME, APR_READ, 0, p);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +
  +    apr_file_getc(&ch, f);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertIntEquals(tc, (int)TESTSTR[0], (int)ch);
  +
  +    apr_file_ungetc('X', f);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +
  +    apr_file_getc(&ch, f);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertIntEquals(tc, 'X', (int)ch);
  +
  +    apr_file_close(f);
   }
   
  -static void test_read_seek(apr_int32_t moreflags, apr_pool_t *p)
  +static void test_gets(CuTest *tc)
   {
  -    const char *fname = "readseek.dat";
  +    apr_file_t *f = NULL;
       apr_status_t rv;
  -    apr_file_t *f;
  -    apr_size_t nbytes;
  -    const char *str1, *str2, *str3;
  -    char buf[8192];
  -    apr_off_t seek_amt;
  -
  -    /* create the content */
  -
  -    rv = apr_file_open(&f, fname, APR_CREATE | APR_WRITE, APR_UREAD | APR_UWRITE, p);
  -    assert(!rv);
  +    char *str = apr_palloc(p, 256);
   
  -    str1 = "abcdefghijklmnopqrstuvwxyz\n";
  -    str2 = "1234567890\n";
  -    str3 = "1234567890-=+_)(*&^%$#@!\n";
  +    rv = apr_file_open(&f, FILENAME, APR_READ, 0, p);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   
  -    nbytes = strlen(str1);
  -    rv = apr_file_write(f, str1, &nbytes);
  -    assert(!rv && nbytes == strlen(str1));
  +    rv = apr_file_gets(str, 256, f);
  +    /* Only one line in the test file, so we should get the EOF on the first
  +     * call to gets.
  +     */
  +    CuAssertIntEquals(tc, APR_EOF, rv);
  +    CuAssertStrEquals(tc, TESTSTR, str);
   
  -    nbytes = strlen(str2);
  -    rv = apr_file_write(f, str2, &nbytes);
  -    assert(!rv && nbytes == strlen(str2));
  +    apr_file_close(f);
  +}
   
  -    nbytes = strlen(str3);
  -    rv = apr_file_write(f, str3, &nbytes);
  -    assert(!rv && nbytes == strlen(str3));
  +static void test_bigread(CuTest *tc)
  +{
  +    apr_file_t *f = NULL;
  +    apr_status_t rv;
  +    char buf[APR_BUFFERSIZE * 2];
  +    apr_size_t nbytes;
   
  -    nbytes = strlen(str1);
  -    rv = apr_file_write(f, str1, &nbytes);
  -    assert(!rv && nbytes == strlen(str1));
  +    /* Create a test file with known content.
  +     */
  +    rv = apr_file_open(&f, "data/created_file", 
  +                       APR_CREATE | APR_WRITE | APR_TRUNCATE, 
  +                       APR_UREAD | APR_UWRITE, p);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   
  -    nbytes = strlen(str2);
  -    rv = apr_file_write(f, str2, &nbytes);
  -    assert(!rv && nbytes == strlen(str2));
  +    nbytes = APR_BUFFERSIZE;
  +    memset(buf, 0xFE, nbytes);
   
  -    nbytes = strlen(str3);
  -    rv = apr_file_write(f, str3, &nbytes);
  -    assert(!rv && nbytes == strlen(str3));
  +    rv = apr_file_write(f, buf, &nbytes);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertIntEquals(tc, APR_BUFFERSIZE, nbytes);
   
       rv = apr_file_close(f);
  -    assert(!rv);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   
  -    rv = apr_file_open(&f, fname, APR_READ | moreflags, 0, p);
  -    assert(!rv);
  -
  -    rv = apr_file_gets(buf, sizeof buf, f);
  -    assert(!rv);
  -    assert(!strcmp(buf, str1));
  -
  -    rv = apr_file_gets(buf, sizeof buf, f);
  -    assert(!rv);
  -    assert(!strcmp(buf, str2));
  +    f = NULL;
  +    rv = apr_file_open(&f, "data/created_file", APR_READ, 0, p);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   
       nbytes = sizeof buf;
       rv = apr_file_read(f, buf, &nbytes);
  -    assert(!rv);
  -    assert(nbytes == strlen(str3) + strlen(str1) + strlen(str2) + strlen(str3));
  -    assert(!memcmp(buf, str3, strlen(str3)));
  -
  -    /* seek back a couple of strings */
  -    seek_amt = -(apr_off_t)(nbytes - strlen(str3) - strlen(str1));
  -    rv = apr_file_seek(f, APR_CUR, &seek_amt);
  -    assert(!rv);
  -    /* seek_amt should be updated with the current offset into the file */
  -    assert(seek_amt == strlen(str1) + strlen(str2) + strlen(str3) + strlen(str1));
  -
  -    rv = apr_file_gets(buf, sizeof buf, f);
  -    assert(!rv);
  -    assert(!strcmp(buf, str2));
  -
  -    rv = apr_file_gets(buf, sizeof buf, f);
  -    assert(!rv);
  -    assert(!strcmp(buf, str3));
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertIntEquals(tc, APR_BUFFERSIZE, nbytes);
   
       rv = apr_file_close(f);
  -    assert(!rv);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   
  -    rv = apr_file_remove(fname, p);
  -    assert(!rv);
  +    rv = apr_file_remove("data/created_file", p);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   }
   
  -static void test_mod_neg(apr_pool_t *p, apr_int32_t flags)
  +/* This is a horrible name for this function.  We are testing APR, not how
  + * Apache uses APR.  And, this function tests _way_ too much stuff.
  + */
  +static void test_mod_neg(CuTest *tc)
   {
       apr_status_t rv;
       apr_file_t *f;
  @@ -563,74 +365,108 @@
       apr_size_t nbytes;
       char buf[8192];
       apr_off_t cur;
  -    const char *fname = "modneg.dat";
  +    const char *fname = "data/modneg.dat";
   
  -    printf("    Testing mod_negotiation-style file access (%sbuffered)...\n",
  -           !flags ? "un" : "");
  -    
  -    rv = apr_file_open(&f, fname, APR_CREATE | APR_WRITE, APR_UREAD | APR_UWRITE, p);
  -    assert(!rv);
  +    rv = apr_file_open(&f, fname, 
  +                       APR_CREATE | APR_WRITE, APR_UREAD | APR_UWRITE, p);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   
       s = "body56789\n";
       nbytes = strlen(s);
       rv = apr_file_write(f, s, &nbytes);
  -    assert(!rv);
  -    assert(nbytes == strlen(s));
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertIntEquals(tc, strlen(s), nbytes);
       
       for (i = 0; i < 7980; i++) {
           s = "0";
           nbytes = strlen(s);
           rv = apr_file_write(f, s, &nbytes);
  -        assert(!rv);
  -        assert(nbytes == strlen(s));
  +        CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +        CuAssertIntEquals(tc, strlen(s), nbytes);
       }
       
       s = "end456789\n";
       nbytes = strlen(s);
       rv = apr_file_write(f, s, &nbytes);
  -    assert(!rv);
  -    assert(nbytes == strlen(s));
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertIntEquals(tc, strlen(s), nbytes);
   
       for (i = 0; i < 10000; i++) {
           s = "1";
           nbytes = strlen(s);
           rv = apr_file_write(f, s, &nbytes);
  -        assert(!rv);
  -        assert(nbytes == strlen(s));
  +        CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +        CuAssertIntEquals(tc, strlen(s), nbytes);
       }
       
       rv = apr_file_close(f);
  -    assert(!rv);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   
  -    rv = apr_file_open(&f, fname, APR_READ | flags, 0, p);
  -    assert(!rv);
  +    rv = apr_file_open(&f, fname, APR_READ, 0, p);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   
       rv = apr_file_gets(buf, 11, f);
  -    assert(!rv);
  -    assert(!strcmp(buf, "body56789\n"));
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertStrEquals(tc, "body56789\n", buf);
   
       cur = 0;
       rv = apr_file_seek(f, APR_CUR, &cur);
  -    assert(!rv);
  -    assert(cur == 10);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertIntEquals(tc, 10, cur);
   
       nbytes = sizeof(buf);
       rv = apr_file_read(f, buf, &nbytes);
  -    assert(!rv);
  -    assert(nbytes == sizeof(buf));
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertIntEquals(tc, nbytes, sizeof(buf));
   
       cur = -((apr_off_t)nbytes - 7980);
       rv = apr_file_seek(f, APR_CUR, &cur);
  -    assert(!rv);
  -    assert(cur == 7990);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertIntEquals(tc, 7990, cur);
   
       rv = apr_file_gets(buf, 11, f);
  -    assert(!rv);
  -    assert(!strcmp(buf, "end456789\n"));
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +    CuAssertStrEquals(tc, "end456789\n", buf);
   
       rv = apr_file_close(f);
  -    assert(!rv);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
   
       rv = apr_file_remove(fname, p);
  -    assert(!rv);
  +    CuAssertIntEquals(tc, APR_SUCCESS, rv);
  +}
  +
  +CuSuite *testfile(void)
  +{
  +    CuSuite *suite = CuSuiteNew("Test File I/O");
  +
  +    SUITE_ADD_TEST(suite, test_open_noreadwrite);
  +    SUITE_ADD_TEST(suite, test_open_excl);
  +    SUITE_ADD_TEST(suite, test_open_read);
  +    SUITE_ADD_TEST(suite, test_read); 
  +    SUITE_ADD_TEST(suite, test_seek);
  +    SUITE_ADD_TEST(suite, test_filename);
  +    SUITE_ADD_TEST(suite, test_fileclose);
  +    SUITE_ADD_TEST(suite, test_file_remove);
  +    SUITE_ADD_TEST(suite, test_open_write);
  +    SUITE_ADD_TEST(suite, test_open_writecreate);
  +    SUITE_ADD_TEST(suite, test_write);
  +    SUITE_ADD_TEST(suite, test_userdata_set);
  +    SUITE_ADD_TEST(suite, test_userdata_get);
  +    SUITE_ADD_TEST(suite, test_userdata_getnokey);
  +    SUITE_ADD_TEST(suite, test_getc);
  +    SUITE_ADD_TEST(suite, test_ungetc);
  +    SUITE_ADD_TEST(suite, test_gets);
  +    SUITE_ADD_TEST(suite, test_bigread);
  +    SUITE_ADD_TEST(suite, test_mod_neg);
  +
  +    return suite;
   }
  +
  +#ifdef SINGLE_PROG
  +CuSuite *getsuite(void)
  +{
  +    return testfile();
  +}
  +#endif
  +
  +
  
  
  

Mime
View raw message