varnish-cache/lib/libvarnish/vsb_test.c
0
/*-
1
 * Copyright (c) 2020-2021 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 * 1. Redistributions of source code must retain the above copyright
10
 *    notice, this list of conditions and the following disclaimer.
11
 * 2. Redistributions in binary form must reproduce the above copyright
12
 *    notice, this list of conditions and the following disclaimer in the
13
 *    documentation and/or other materials provided with the distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
 * SUCH DAMAGE.
26
 */
27
28
#ifdef VSB_TEST
29
30
#include <stdio.h>
31
#include <string.h>
32
33
#include "vdef.h"
34
#include "vas.h"
35
#include "vsb.h"
36
37
struct tc {
38
        int             how;
39
        int             inlen;
40
        const char      *in;
41
        const char      *out;
42
};
43
44
static struct tc tcs[] = {
45
        {
46
                VSB_QUOTE_HEX,
47
                5, "\x00\n\x7e\x7f\xff",
48
                "PFX0x000a7e7fff"
49
        },
50
        {
51
                VSB_QUOTE_HEX,
52
                5, "\0\0\0\0\0",
53
                "PFX0x0...0"
54
        },
55
        {
56
                VSB_QUOTE_HEX | VSB_QUOTE_NONL,
57
                5, "\x00\n\x7e\x7f\xff",
58
                "PFX0x000a7e7fff\n"
59
        },
60
        {
61
                VSB_QUOTE_ESCHEX,
62
                5, "\x00\n\x7e\x7f\xff",
63
                "PFX\\x00\\n~\\x7f\\xff",
64
        },
65
        {
66
                0,
67
                5, "\x00\n\x7e\x7f\xff",
68
                "PFX\\000\\n~\\177\\377",
69
        },
70
        {
71
                VSB_QUOTE_UNSAFE,
72
                5, "\x00\n\x7e\x7f\xff",
73
                "PFX\\000\nPFX~\\177\\377\n",
74
        },
75
        {
76
                VSB_QUOTE_UNSAFE,
77
                -1, "\n\"\\\t",
78
                "PFX\nPFX\"\\\\t\n"
79
        },
80
        {
81
                VSB_QUOTE_CSTR | VSB_QUOTE_ESCHEX,
82
                5, "\x00\n\x7e\x7f\xff",
83
                "PFX\"\\x00\\n\"\nPFX\"~\\x7f\\xff\"",
84
        },
85
        /*
86
         * VSB_QUOTE_JSON puts >0x7e literally
87
         */
88
        {
89
                VSB_QUOTE_JSON,
90
                4, "\xf0\x9f\x90\xb0",
91
                "PFX\xf0\x9f\x90\xb0",
92
        },
93
        /*
94
         * VSB_QUOTE_JSON encodes <0x20 as \u00XX, which looks like
95
         * unicode codepoints in the JSON standard, but are just
96
         * individual bytes
97
         */
98
        {
99
                VSB_QUOTE_JSON,
100
                5, "\"\x01\x02\x03\x04",
101
                "PFX\\\"\\u0001\\u0002\\u0003\\u0004",
102
        },
103
        {
104
                VSB_QUOTE_JSON,
105
                5, "\x00\n\x7e\x7f\xff",
106
                "PFX\\u0000\\n~\x7f\xff",
107
        },
108
        {
109
                VSB_QUOTE_JSON | VSB_QUOTE_NONL,
110
                5, "\x00\n\x7e\x7f\xff",
111
                "PFX\\u0000\\n~\x7f\xff\n",
112
        },
113
        {
114
                VSB_QUOTE_CSTR,
115
                -1, "",
116
                "PFX\"\""
117
        },
118
        {
119
                VSB_QUOTE_CSTR,
120
                -1, "?",
121
                "PFX\"\\?\""
122
        },
123
        {
124
                VSB_QUOTE_NONL,
125
                -1, "\n\t",
126
                "PFX\nPFX\\t\n"
127
        },
128
        {
129
                0, -1, NULL, NULL
130
        }
131
};
132
133
int
134 40
main(int argc, char *argv[])
135
{
136 40
        int err = 0;
137
        struct tc *tc;
138
        struct vsb *vsb;
139
        struct vsb *vsbo;
140
141 40
        (void)argc;
142 40
        (void)argv;
143 40
        vsb = VSB_new_auto();
144 40
        AN(vsb);
145 40
        vsbo = VSB_new_auto();
146 40
        AN(vsbo);
147
148 640
        for (tc = tcs; tc->in; tc++) {
149 600
                VSB_quote_pfx(vsb, "PFX", tc->in, tc->inlen, tc->how);
150 600
                assert(VSB_finish(vsb) == 0);
151
152 600
                VSB_clear(vsbo);
153 600
                VSB_printf(vsbo, "0x%02x: ", tc->how);
154 600
                VSB_quote(vsbo, tc->in, tc->inlen, VSB_QUOTE_HEX);
155 600
                VSB_cat(vsbo, " -> ");
156 600
                VSB_quote(vsbo, VSB_data(vsb), -1, VSB_QUOTE_HEX);
157 600
                VSB_cat(vsbo, " (");
158 600
                VSB_quote(vsbo, tc->out, -1, VSB_QUOTE_ESCHEX);
159 600
                VSB_cat(vsbo, ")");
160 600
                if (strcmp(VSB_data(vsb), tc->out)) {
161 0
                        VSB_cat(vsbo, "\nShould have been:\n\t");
162 0
                        VSB_quote(vsbo, tc->out, -1, VSB_QUOTE_HEX);
163 0
                        VSB_cat(vsbo, "\nThat's:\n\t");
164 0
                        VSB_quote(vsbo, VSB_data(vsb), -1, VSB_QUOTE_ESCHEX);
165 0
                        VSB_cat(vsbo, "\nvs:\n\t");
166 0
                        VSB_quote(vsbo, tc->out, -1, VSB_QUOTE_ESCHEX);
167 0
                        VSB_printf(vsbo, "\nFlags 0x%02x = ", tc->how);
168 0
                        if (!tc->how)
169 0
                                VSB_cat(vsbo, "\n\t0");
170 0
                        if (tc->how & VSB_QUOTE_NONL)
171 0
                                VSB_cat(vsbo, "\n\tVSB_QUOTE_NONL");
172 0
                        if (tc->how & VSB_QUOTE_JSON)
173 0
                                VSB_cat(vsbo, "\n\tVSB_QUOTE_JSON");
174 0
                        if (tc->how & VSB_QUOTE_HEX)
175 0
                                VSB_cat(vsbo, "\n\tVSB_QUOTE_HEX");
176 0
                        if (tc->how & VSB_QUOTE_CSTR)
177 0
                                VSB_cat(vsbo, "\n\tVSB_QUOTE_CSTR");
178 0
                        if (tc->how & VSB_QUOTE_UNSAFE)
179 0
                                VSB_cat(vsbo, "\n\tVSB_QUOTE_UNSAFE");
180 0
                        if (tc->how & VSB_QUOTE_ESCHEX)
181 0
                                VSB_cat(vsbo, "\n\tVSB_QUOTE_ESCHEX");
182 0
                        VSB_cat(vsbo, "\n\n");
183 0
                        err = 1;
184 0
                }
185 600
                AZ(VSB_finish(vsbo));
186 600
                printf("%s\n", VSB_data(vsbo));
187 600
                VSB_clear(vsb);
188 600
        }
189 40
        VSB_destroy(&vsb);
190 40
        VSB_destroy(&vsbo);
191 40
        printf("error is %i\n", err);
192 40
        return (err);
193
}
194
195
#endif /* VSB_TEST */