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