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 4375
cb(int tag, void *priv)
50
{
51 4375
        printf("\t%d (%s)\n", tag, VSL_tags[tag]);
52 4375
        if (priv != NULL)
53 3025
                assert(!fnmatch(priv, VSL_tags[tag], FNM_CASEFOLD));
54 4375
}
55
56
static int
57 250
tst_one_glob(const char *p)
58
{
59
        int i;
60
61 250
        printf("Test <%s>\n", p);
62 250
        i = VSL_Glob2Tags(p, -1, cb, TRUST_ME(p));
63 250
        printf("  -> %d\n", i);
64 250
        return (i);
65
}
66
67
int
68 100
main(int argc, char * const *argv)
69
{
70
        int i, j;
71
72 100
        if (argc == 1) {
73 25
                i = tst_one_glob("Req*");
74 25
                assert(i == 10);
75 25
                j = tst_one_glob("reQ*");
76 25
                assert(i == j);
77 25
                assert(tst_one_glob("*Header") > 0);
78 25
                assert(tst_one_glob("Req*eader") == 1);
79 25
                assert(tst_one_glob("xyz*y") == -1);
80 25
                assert(tst_one_glob("*") > 0);
81 25
                assert(tst_one_glob("a*b*c") == -3);
82 25
                assert(tst_one_glob("**") == -3);
83 25
                assert(tst_one_glob("_") == -1);
84 25
                assert(tst_one_glob("") == -1);
85 25
                assert(VSL_Glob2Tags("", 0, cb, NULL) == -1);
86
87 25
                assert(VSL_List2Tags("Req*,Resp*",-1,cb,NULL) > 0);
88 25
                assert(VSL_List2Tags(",,,",-1,cb,NULL) == -1);
89 25
                assert(VSL_List2Tags("xyzzy,,xyzzy",-1,cb,NULL) == -1);
90 25
                return (0);
91
        }
92 75
        if (argc != 2) {
93 25
                fprintf(stderr, "vsl_glob_test <tagname/glob>\n");
94 25
                exit(1);
95
        }
96
97 50
        i = VSL_Name2Tag(argv[1], -1);
98 50
        printf("VSL_Name2Tag returns %d", i);
99 50
        if (i >= 0)
100 0
                printf(" (%s)", VSL_tags[i]);
101 50
        printf("\n");
102
103 50
        printf("VSL_Glob2Tags:\n");
104 50
        i = VSL_Glob2Tags(argv[1], -1, cb, NULL);
105 50
        printf("VSL_Glob2Tags returns %d\n", i);
106
107 50
        printf("VSL_List2Tags:\n");
108 50
        i = VSL_List2Tags(argv[1], -1, cb, NULL);
109 50
        printf("VSL_List2Tags returns %d\n", i);
110
111 50
        return (0);
112 75
}
113
114
#endif // __FLEXELINT__