varnish-cache/lib/libvarnish/vsb_test.c
1
2
#ifdef VSB_TEST
3
4
#include <stdio.h>
5
#include <string.h>
6
7
#include "vdef.h"
8
#include "vas.h"
9
#include "vsb.h"
10
11
struct tc {
12
        int             how;
13
        int             inlen;
14
        const char      *in;
15
        const char      *out;
16
};
17
18
static struct tc tcs[] = {
19
        {
20
                VSB_QUOTE_HEX,
21
                5, "\x00\n\x7e\x7f\xff",
22
                "PFX0x000a7e7fff"
23
        },
24
        {
25
                VSB_QUOTE_HEX,
26
                5, "\0\0\0\0\0",
27
                "PFX0x0...0"
28
        },
29
        {
30
                VSB_QUOTE_HEX | VSB_QUOTE_NONL,
31
                5, "\x00\n\x7e\x7f\xff",
32
                "PFX0x000a7e7fff\n"
33
        },
34
        {
35
                VSB_QUOTE_ESCHEX,
36
                5, "\x00\n\x7e\x7f\xff",
37
                "PFX\\x00\\n~\\x7f\\xff",
38
        },
39
        {
40
                0,
41
                5, "\x00\n\x7e\x7f\xff",
42
                "PFX\\000\\n~\\177\\377",
43
        },
44
        {
45
                VSB_QUOTE_UNSAFE,
46
                5, "\x00\n\x7e\x7f\xff",
47
                "PFX\\000\nPFX~\\177\\377\n",
48
        },
49
        {
50
                VSB_QUOTE_UNSAFE,
51
                -1, "\n\"\\\t",
52
                "PFX\nPFX\"\\\\t\n"
53
        },
54
        {
55
                VSB_QUOTE_CSTR | VSB_QUOTE_ESCHEX,
56
                5, "\x00\n\x7e\x7f\xff",
57
                "PFX\"\\x00\\n\"\nPFX\"~\\x7f\\xff\"",
58
        },
59
        {
60
                VSB_QUOTE_JSON,
61
                5, "\x00\n\x7e\x7f\xff",
62
                "PFX\\u0000\\n~\x7f\xff",
63
        },
64
        {
65
                VSB_QUOTE_JSON | VSB_QUOTE_NONL,
66
                5, "\x00\n\x7e\x7f\xff",
67
                "PFX\\u0000\\n~\x7f\xff\n",
68
        },
69
        {
70
                VSB_QUOTE_CSTR,
71
                -1, "",
72
                "PFX\"\""
73
        },
74
        {
75
                VSB_QUOTE_CSTR,
76
                -1, "?",
77
                "PFX\"\\?\""
78
        },
79
        {
80
                VSB_QUOTE_NONL,
81
                -1, "\n\t",
82
                "PFX\nPFX\\t\n"
83
        },
84
        {
85
                0, -1, NULL, NULL
86
        }
87
};
88
89
int
90 40
main(int argc, char *argv[])
91
{
92 40
        int err = 0;
93
        struct tc *tc;
94
        struct vsb *vsb;
95
        struct vsb *vsbo;
96
97 40
        (void)argc;
98 40
        (void)argv;
99 40
        vsb = VSB_new_auto();
100 40
        AN(vsb);
101 40
        vsbo = VSB_new_auto();
102 40
        AN(vsbo);
103
104 560
        for (tc = tcs; tc->in; tc++) {
105 520
                VSB_quote_pfx(vsb, "PFX", tc->in, tc->inlen, tc->how);
106 520
                assert(VSB_finish(vsb) == 0);
107
108 520
                VSB_clear(vsbo);
109 520
                VSB_printf(vsbo, "0x%02x: ", tc->how);
110 520
                VSB_quote(vsbo, tc->in, tc->inlen, VSB_QUOTE_HEX);
111 520
                VSB_printf(vsbo, " -> ");
112 520
                VSB_quote(vsbo, VSB_data(vsb), -1, VSB_QUOTE_HEX);
113 520
                VSB_printf(vsbo, " (");
114 520
                VSB_quote(vsbo, tc->out, -1, VSB_QUOTE_ESCHEX);
115 520
                VSB_printf(vsbo, ")");
116 520
                if (strcmp(VSB_data(vsb), tc->out)) {
117 0
                        VSB_printf(vsbo, "\nShould have been:\n\t");
118 0
                        VSB_quote(vsbo, tc->out, -1, VSB_QUOTE_HEX);
119 0
                        VSB_printf(vsbo, "\nThat's:\n\t");
120 0
                        VSB_quote(vsbo, VSB_data(vsb), -1, VSB_QUOTE_ESCHEX);
121 0
                        VSB_printf(vsbo, "\nvs:\n\t");
122 0
                        VSB_quote(vsbo, tc->out, -1, VSB_QUOTE_ESCHEX);
123 0
                        VSB_printf(vsbo, "\nFlags 0x%02x = ", tc->how);
124 0
                        if (!tc->how)
125 0
                                VSB_printf(vsbo, "\n\t0");
126 0
                        if (tc->how & VSB_QUOTE_NONL)
127 0
                                VSB_printf(vsbo, "\n\tVSB_QUOTE_NONL");
128 0
                        if (tc->how & VSB_QUOTE_JSON)
129 0
                                VSB_printf(vsbo, "\n\tVSB_QUOTE_JSON");
130 0
                        if (tc->how & VSB_QUOTE_HEX)
131 0
                                VSB_printf(vsbo, "\n\tVSB_QUOTE_HEX");
132 0
                        if (tc->how & VSB_QUOTE_CSTR)
133 0
                                VSB_printf(vsbo, "\n\tVSB_QUOTE_CSTR");
134 0
                        if (tc->how & VSB_QUOTE_UNSAFE)
135 0
                                VSB_printf(vsbo, "\n\tVSB_QUOTE_UNSAFE");
136 0
                        if (tc->how & VSB_QUOTE_ESCHEX)
137 0
                                VSB_printf(vsbo, "\n\tVSB_QUOTE_ESCHEX");
138 0
                        VSB_printf(vsbo, "\n\n");
139 0
                        err = 1;
140 0
                }
141 520
                AZ(VSB_finish(vsbo));
142 520
                printf("%s\n", VSB_data(vsbo));
143 520
                VSB_clear(vsb);
144 520
        }
145 40
        VSB_destroy(&vsb);
146 40
        VSB_destroy(&vsbo);
147 40
        printf("error is %i\n", err);
148 40
        return (err);
149
}
150
151
#endif /* VSB_TEST */