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,
87
                5, "\x00\n\x7e\x7f\xff",
88
                "PFX\\u0000\\n~\x7f\xff",
89
        },
90
        {
91
                VSB_QUOTE_JSON | VSB_QUOTE_NONL,
92
                5, "\x00\n\x7e\x7f\xff",
93
                "PFX\\u0000\\n~\x7f\xff\n",
94
        },
95
        {
96
                VSB_QUOTE_CSTR,
97
                -1, "",
98
                "PFX\"\""
99
        },
100
        {
101
                VSB_QUOTE_CSTR,
102
                -1, "?",
103
                "PFX\"\\?\""
104
        },
105
        {
106
                VSB_QUOTE_NONL,
107
                -1, "\n\t",
108
                "PFX\nPFX\\t\n"
109
        },
110
        {
111
                0, -1, NULL, NULL
112
        }
113
};
114
115
int
116 28
main(int argc, char *argv[])
117
{
118 28
        int err = 0;
119
        struct tc *tc;
120
        struct vsb *vsb;
121
        struct vsb *vsbo;
122
123 28
        (void)argc;
124 28
        (void)argv;
125 28
        vsb = VSB_new_auto();
126 28
        AN(vsb);
127 28
        vsbo = VSB_new_auto();
128 28
        AN(vsbo);
129
130 392
        for (tc = tcs; tc->in; tc++) {
131 364
                VSB_quote_pfx(vsb, "PFX", tc->in, tc->inlen, tc->how);
132 364
                assert(VSB_finish(vsb) == 0);
133
134 364
                VSB_clear(vsbo);
135 364
                VSB_printf(vsbo, "0x%02x: ", tc->how);
136 364
                VSB_quote(vsbo, tc->in, tc->inlen, VSB_QUOTE_HEX);
137 364
                VSB_cat(vsbo, " -> ");
138 364
                VSB_quote(vsbo, VSB_data(vsb), -1, VSB_QUOTE_HEX);
139 364
                VSB_cat(vsbo, " (");
140 364
                VSB_quote(vsbo, tc->out, -1, VSB_QUOTE_ESCHEX);
141 364
                VSB_cat(vsbo, ")");
142 364
                if (strcmp(VSB_data(vsb), tc->out)) {
143 0
                        VSB_cat(vsbo, "\nShould have been:\n\t");
144 0
                        VSB_quote(vsbo, tc->out, -1, VSB_QUOTE_HEX);
145 0
                        VSB_cat(vsbo, "\nThat's:\n\t");
146 0
                        VSB_quote(vsbo, VSB_data(vsb), -1, VSB_QUOTE_ESCHEX);
147 0
                        VSB_cat(vsbo, "\nvs:\n\t");
148 0
                        VSB_quote(vsbo, tc->out, -1, VSB_QUOTE_ESCHEX);
149 0
                        VSB_printf(vsbo, "\nFlags 0x%02x = ", tc->how);
150 0
                        if (!tc->how)
151 0
                                VSB_cat(vsbo, "\n\t0");
152 0
                        if (tc->how & VSB_QUOTE_NONL)
153 0
                                VSB_cat(vsbo, "\n\tVSB_QUOTE_NONL");
154 0
                        if (tc->how & VSB_QUOTE_JSON)
155 0
                                VSB_cat(vsbo, "\n\tVSB_QUOTE_JSON");
156 0
                        if (tc->how & VSB_QUOTE_HEX)
157 0
                                VSB_cat(vsbo, "\n\tVSB_QUOTE_HEX");
158 0
                        if (tc->how & VSB_QUOTE_CSTR)
159 0
                                VSB_cat(vsbo, "\n\tVSB_QUOTE_CSTR");
160 0
                        if (tc->how & VSB_QUOTE_UNSAFE)
161 0
                                VSB_cat(vsbo, "\n\tVSB_QUOTE_UNSAFE");
162 0
                        if (tc->how & VSB_QUOTE_ESCHEX)
163 0
                                VSB_cat(vsbo, "\n\tVSB_QUOTE_ESCHEX");
164 0
                        VSB_cat(vsbo, "\n\n");
165 0
                        err = 1;
166 0
                }
167 364
                AZ(VSB_finish(vsbo));
168 364
                printf("%s\n", VSB_data(vsbo));
169 364
                VSB_clear(vsb);
170 364
        }
171 28
        VSB_destroy(&vsb);
172 28
        VSB_destroy(&vsbo);
173 28
        printf("error is %i\n", err);
174 28
        return (err);
175
}
176
177
#endif /* VSB_TEST */