Пример #1
0
/* Regression test for PR 34708, where a file bucket will keep
 * duplicating itself on being read() when EOF is reached
 * prematurely. */
static void test_truncfile(abts_case *tc, void *data)
{
    apr_bucket_alloc_t *ba = apr_bucket_alloc_create(p);
    apr_bucket_brigade *bb = apr_brigade_create(p, ba);
    apr_file_t *f = make_test_file(tc, "testfile.txt", "hello");
    apr_bucket *e;
    const char *buf;
    apr_size_t len;

    apr_brigade_insert_file(bb, f, 0, 5, p);

    apr_file_trunc(f, 0);

    e = APR_BRIGADE_FIRST(bb);

    ABTS_ASSERT(tc, "single bucket in brigade",
                APR_BUCKET_NEXT(e) == APR_BRIGADE_SENTINEL(bb));

    apr_bucket_file_enable_mmap(e, 0);

    ABTS_ASSERT(tc, "read gave APR_EOF",
                apr_bucket_read(e, &buf, &len, APR_BLOCK_READ) == APR_EOF);

    ABTS_ASSERT(tc, "read length 0", len == 0);
    
    ABTS_ASSERT(tc, "still a single bucket in brigade",
                APR_BUCKET_NEXT(e) == APR_BRIGADE_SENTINEL(bb));

    apr_file_close(f);
    apr_brigade_destroy(bb);
    apr_bucket_alloc_destroy(ba);
}
Пример #2
0
static void test_manyfile(abts_case *tc, void *data)
{
    apr_bucket_alloc_t *ba = apr_bucket_alloc_create(p);
    apr_bucket_brigade *bb = apr_brigade_create(p, ba);
    apr_file_t *f;

    f = make_test_file(tc, "manyfile.bin",
                       "world" "hello" "brave" " ,\n");

    apr_brigade_insert_file(bb, f, 5, 5, p);
    apr_brigade_insert_file(bb, f, 16, 1, p);
    apr_brigade_insert_file(bb, f, 15, 1, p);
    apr_brigade_insert_file(bb, f, 10, 5, p);
    apr_brigade_insert_file(bb, f, 15, 1, p);
    apr_brigade_insert_file(bb, f, 0, 5, p);
    apr_brigade_insert_file(bb, f, 17, 1, p);

    /* can you tell what it is yet? */
    flatten_match(tc, "file seek test", bb,
                  "hello, brave world\n");

    apr_file_close(f);
    apr_brigade_destroy(bb);
    apr_bucket_alloc_destroy(ba);
}
Пример #3
0
int test_getdelim()
{
    char buf[BUF_SIZE];
    FILE *f;
    int rc;
    int i;

    if (make_test_file(DATA_2, LINE_NUM_2) != 0) {
        printf("test_getdelim: failed to make test file\n");
        return 1;
    }

    f = fopen(TEST_FILE_NAME, "r");
    if (f == NULL) {
        rc = errno;
        printf("test_getdelim: failed to open temp file; error %i; %s\n", rc, strerror(rc));
        return 1;
    }

    for (i=0; i<LINE_NUM_2; ++i) {
        char *s = NULL;
        int num = 0;
        if (getdelim(&s, &num, ' ', f) == -1) {
            rc = errno;
            printf("test_getdelim: failed to read temp file; error %i; %s\n", rc, strerror(rc));
            return 1;
        }
        if (strcmp(s, DATA_2[i]) != 0) {
            printf("test_getdelim: expected: '%s'\n", DATA_2[i]);
            printf("test_getdelim: read:     '%s'\n", s);
            return 1;
        }
    }

    if (fclose(f) != 0) {
        rc = errno;
        printf("failed to close test file; error %i; %s\n", rc, strerror(rc));
        return 1;
    }

    if (unlink(TEST_FILE_NAME) != 0) {
        rc = errno;
        printf("failed to close test file; error %i; %s\n", rc, strerror(rc));
        return 1;
    }

    printf("getdelim - ok\n");
    return 0;
}
Пример #4
0
int main(int argc, char ** argv)
{
    if (argc < 3) {
        print_usage_and_exit();
    }

    if (!strcmp(argv[1], "-creat")) {
        make_test_file(argv[2]);
    } else if (!strcmp(argv[1], "-check")) {
        check_output(argv[2]);
    } else {

        /* Do the main thing*/

        /* Open fds...*/
        int fd_in = open(argv[1], O_RDONLY);
        fprintf(stderr, "Opening %s... ", argv[1]);
        if (fd_in == -1) {
            perror("open() failed");
            exit(EXIT_FAILURE);
        }
        fprintf(stderr, "success\n");
        int fd_out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
        fprintf(stderr, "Opening %s... ", argv[2]);
        if (fd_in == -1) {
            perror("open() failed");
            exit(EXIT_FAILURE);
        }
        fprintf(stderr, "success\n");

        /* Sorting procedure :
        *
        *       1. Sequentially read headers from input file and build index-tree
        *          that holds key, size and offset of records in input file.
        *       2. Then, walk the tree in ascending order
        *          and write records described by its nodes into output file
        */

        /*Current read position in file*/
        uint64_t in_position = 0;

        /*Tree top*/
        compact_header_t * tree = NULL;

        /*Total count of records in file (only for logging)*/
        int rec_cnt = 0;

        int fl_break = 0;
        while (!fl_break) {
            int r = do_process_record(fd_in, &in_position, &tree);
            switch (r) {
            case IO_EOF :
                /*file end is reached*/
                fl_break = 1;
                break;
            case IO_ERROR :
                fprintf(stderr, "File read error\n");
                exit(EXIT_FAILURE);
                break;
            case IO_SUCCESS :
                rec_cnt++;
                break;
            default :
                assert(0);
                break;
            }
        }

        fprintf(stderr, "Count of records: %i\n", rec_cnt);
        fprintf(stderr, "Writing sorted file...");
        walk_tree(fd_in, fd_out, tree);
        close(fd_in);
        close(fd_out);
        fprintf(stderr, "done\n");
    }
    return EXIT_SUCCESS;
}
Пример #5
0
int main()
{
    make_test_file("test3.txt");
    return 0;
}
Пример #6
0
void test_body()
{
    make_test_file("test6.txt");
}