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