~cpick/mongrel2/release

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
#include "minunit.h"
#include <http11/httpclient_parser.h>
#include <glob.h>
#include <bstring.h>


httpclient_parser setup_parser()
{
    httpclient_parser p;
    httpclient_parser_init(&p);
    p.http_field = NULL;
    p.reason_phrase = NULL;
    p.status_code = NULL;
    p.chunk_size = NULL;
    p.http_version = NULL;
    p.header_done = NULL;
    p.last_chunk = NULL;

    return p;
}

char *test_httpclient_parser_basics() 
{
    httpclient_parser p = setup_parser();
    p.data = &p;
    int rc = 0;

    rc = httpclient_parser_finish(&p);
    mu_assert(rc == 0, "Client parser SHOULD be finished if nothing parsed.");

    rc = httpclient_parser_has_error(&p);
    mu_assert(rc == 0, "Should not have an error at the beginning.");

    rc = httpclient_parser_is_finished(&p);
    mu_assert(rc == 0, "Should not be finished since never handed anything.");
    return NULL;
}

bstring load_test_case(const char *path)
{
    FILE *infile = fopen(path, "r");
    check(infile != NULL, "Failed to open test file.");

    bstring data = bread((bNread)fread, infile);
    fclose(infile);
    check(data != NULL, "Failed to read test file.");

    return data;

error:
    if(infile) fclose(infile);
    return NULL;
}

char *test_parser_thrashing()
{
    glob_t test_files;
    unsigned int i = 0;
    int nparsed = 0;
    int tests_run = 0;
    int execs_run = 0;
    int unfinished = 0;
    int errors = 0;

    int rc = glob("tests/client_suite/*", 0, NULL, &test_files);
    mu_assert(rc == 0, "Failed to glob file sin tests/client_suite/*");

    for(i = 0; i < test_files.gl_pathc; i++) {
        debug("TESTING: %s", test_files.gl_pathv[i]);
        bstring data = load_test_case(test_files.gl_pathv[i]);
        mu_assert(data != NULL, "Failed to load test case.");

        tests_run++;

        httpclient_parser p = setup_parser();
        p.data = &p;

        nparsed = httpclient_parser_execute(&p, bdata(data), blength(data), 0);
        execs_run++;

        if(!httpclient_parser_finish(&p)) {
            unfinished++;
        }

        if(httpclient_parser_has_error(&p)) {
            errors++;
            debug("TEST %s results: delta %d, has_error: %d, is_finished: %d",
                    test_files.gl_pathv[i],
                    nparsed, httpclient_parser_has_error(&p), 
                    httpclient_parser_is_finished(&p));
        } else if(p.chunked) {
            unsigned int start = p.body_start;

            do {
                httpclient_parser_init(&p);
                
                nparsed = httpclient_parser_execute(&p, bdata(data), blength(data), start);
                mu_assert(p.body_start > start, "Didn't go past start.");
                mu_assert(p.chunked, "Should still be chunked.");
                start = p.body_start + p.content_len + 1;

                debug("CHUNK: length: %d, done: %d, start: %d",
                        p.content_len, p.chunks_done, start);
            } while(!p.chunks_done && p.content_len > 0 && !httpclient_parser_has_error(&p));

            mu_assert(p.chunks_done, "Should have chunks_done set.");
            mu_assert(p.content_len == 0, "Should have 0 content_len too.");
        }
    }

    debug("HTTP PARSING: tests_run: %d, execs_run: %d, unfinished: %d, errors: %d",
            tests_run, execs_run, unfinished, errors);

    return NULL;
}


char * all_tests() {
    mu_suite_start();

    mu_run_test(test_httpclient_parser_basics);
    mu_run_test(test_parser_thrashing);

    return NULL;
}

RUN_TESTS(all_tests);