varnish-cache/lib/libvarnishapi/vsl_glob_test.c
0
/*-
1
 * Copyright (c) 2013 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Martin Blix Grydeland <martin@varnish-software.com>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * Test what VSL_Name2Tag and VSL_Glob2Tags produces
30
 */
31
32
#ifndef __FLEXELINT__
33
34
#include <fnmatch.h>
35
#include <stdint.h>
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
40
#include "vapi/vsl.h"
41
#include "vdef.h"
42
#include "vas.h"
43
44
#ifndef FNM_CASEFOLD
45
#  define FNM_CASEFOLD FNM_IGNORECASE
46
#endif
47
48
static void
49 1575
cb(int tag, void *priv)
50
{
51 1575
        printf("\t%d (%s)\n", tag, VSL_tags[tag]);
52 1575
        if (priv != NULL)
53 1089
                assert(!fnmatch(priv, VSL_tags[tag], FNM_CASEFOLD));
54 1575
}
55
56
static int
57 90
tst_one_glob(const char *p)
58
{
59
        int i;
60
61 90
        printf("Test <%s>\n", p);
62 90
        i = VSL_Glob2Tags(p, -1, cb, TRUST_ME(p));
63 90
        printf("  -> %d\n", i);
64 90
        return (i);
65
}
66
67
int
68 36
main(int argc, char * const *argv)
69
{
70
        int i, j;
71
72 36
        if (argc == 1) {
73 9
                i = tst_one_glob("Req*");
74 9
                assert(i == 10);
75 9
                j = tst_one_glob("reQ*");
76 9
                assert(i == j);
77 9
                assert(tst_one_glob("*Header") > 0);
78 9
                assert(tst_one_glob("Req*eader") == 1);
79 9
                assert(tst_one_glob("xyz*y") == -1);
80 9
                assert(tst_one_glob("*") > 0);
81 9
                assert(tst_one_glob("a*b*c") == -3);
82 9
                assert(tst_one_glob("**") == -3);
83 9
                assert(tst_one_glob("_") == -1);
84 9
                assert(tst_one_glob("") == -1);
85 9
                assert(VSL_Glob2Tags("", 0, cb, NULL) == -1);
86
87 9
                assert(VSL_List2Tags("Req*,Resp*",-1,cb,NULL) > 0);
88 9
                assert(VSL_List2Tags(",,,",-1,cb,NULL) == -1);
89 9
                assert(VSL_List2Tags("xyzzy,,xyzzy",-1,cb,NULL) == -1);
90 9
                return (0);
91
        }
92 27
        if (argc != 2) {
93 9
                fprintf(stderr, "vsl_glob_test <tagname/glob>\n");
94 9
                exit(1);
95
        }
96
97 18
        i = VSL_Name2Tag(argv[1], -1);
98 18
        printf("VSL_Name2Tag returns %d", i);
99 18
        if (i >= 0)
100 0
                printf(" (%s)", VSL_tags[i]);
101 18
        printf("\n");
102
103 18
        printf("VSL_Glob2Tags:\n");
104 18
        i = VSL_Glob2Tags(argv[1], -1, cb, NULL);
105 18
        printf("VSL_Glob2Tags returns %d\n", i);
106
107 18
        printf("VSL_List2Tags:\n");
108 18
        i = VSL_List2Tags(argv[1], -1, cb, NULL);
109 18
        printf("VSL_List2Tags returns %d\n", i);
110
111 18
        return (0);
112 27
}
113
114
#endif // __FLEXELINT__