aboutsummaryrefslogtreecommitdiffstats
path: root/tests/blogc-runserver/check_httpd_utils.c
blob: 69c2a5b4645e0c4f638f10f9b7d8735786eec326 (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
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2019 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>
#include <squareball.h>

#include "../../src/blogc-runserver/httpd-utils.h"


ssize_t
__wrap_read(int fd, void *buf, size_t count)
{
    assert_int_equal(fd, mock_type(int));
    const char *mock_buf = mock_type(const char*);
    strcpy(buf, mock_buf);
    assert_int_equal(count, READLINE_BUFFER_SIZE);
    return strlen(mock_buf) > 0 ? strlen(mock_buf) : -1;
}


static void
test_readline(void **state)
{
    char *t;
    will_return(__wrap_read, 1234);
    will_return(__wrap_read, "bola");
    t = br_readline(1234);
    assert_string_equal(t, "bola");
    free(t);
    will_return(__wrap_read, 1234);
    will_return(__wrap_read, "bola1\nguda\nxd");
    t = br_readline(1234);
    assert_string_equal(t, "bola1");
    free(t);
    will_return(__wrap_read, 1234);
    will_return(__wrap_read, "bola2\rguda\rxd");
    t = br_readline(1234);
    assert_string_equal(t, "bola2");
    free(t);
    will_return(__wrap_read, 1234);
    will_return(__wrap_read, "bola3\r\nguda\r\nxd");
    t = br_readline(1234);
    assert_string_equal(t, "bola3");
    free(t);
}


static void
test_hextoi(void **state)
{
    assert_int_equal(br_hextoi('0'), 0);
    assert_int_equal(br_hextoi('1'), 1);
    assert_int_equal(br_hextoi('2'), 2);
    assert_int_equal(br_hextoi('3'), 3);
    assert_int_equal(br_hextoi('4'), 4);
    assert_int_equal(br_hextoi('5'), 5);
    assert_int_equal(br_hextoi('6'), 6);
    assert_int_equal(br_hextoi('7'), 7);
    assert_int_equal(br_hextoi('8'), 8);
    assert_int_equal(br_hextoi('9'), 9);
    assert_int_equal(br_hextoi('a'), 10);
    assert_int_equal(br_hextoi('b'), 11);
    assert_int_equal(br_hextoi('c'), 12);
    assert_int_equal(br_hextoi('d'), 13);
    assert_int_equal(br_hextoi('e'), 14);
    assert_int_equal(br_hextoi('f'), 15);
    assert_int_equal(br_hextoi('A'), 10);
    assert_int_equal(br_hextoi('B'), 11);
    assert_int_equal(br_hextoi('C'), 12);
    assert_int_equal(br_hextoi('D'), 13);
    assert_int_equal(br_hextoi('E'), 14);
    assert_int_equal(br_hextoi('F'), 15);
    assert_int_equal(br_hextoi('g'), -1);
    assert_int_equal(br_hextoi('G'), -1);
    assert_int_equal(br_hextoi('-'), -1);
}


static void
test_urldecode(void **state)
{
    for (size_t i = 0; i < 128; i++) {
        char *t = sb_strdup_printf("%%%02x", i);
        char *r = br_urldecode(t);
        assert_int_equal(r[0], i);
        assert_int_equal(r[1], 0);
        free(r);
        free(t);
    }
    char *r = br_urldecode("%Ab");
    assert_string_equal(r, "\xab");
    free(r);
    r = br_urldecode("%xb");
    assert_string_equal(r, "%xb");
    free(r);
    r = br_urldecode("%C3%BC");
    assert_string_equal(r, "\xc3\xbc");
    free(r);
}


static void
test_get_extension(void **state)
{
    assert_null(br_get_extension("bola"));
    assert_string_equal(br_get_extension("bola.txt"), "txt");
    assert_string_equal(br_get_extension("bola.txt.jpg"), "jpg");
    assert_null(br_get_extension("bola.txt/foo"));
    assert_string_equal(br_get_extension("bola.txt/foo.jpg"), "jpg");
}


int
main(void)
{
    const UnitTest tests[] = {
        unit_test(test_readline),
        unit_test(test_hextoi),
        unit_test(test_urldecode),
        unit_test(test_get_extension),
    };
    return run_tests(tests);
}