"Fossies" - the Fresh Open Source Software Archive

Member "gnupg-2.2.17/common/t-recsel.c" (28 Aug 2017, 10632 Bytes) of package /linux/misc/gnupg-2.2.17.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "t-recsel.c" see the Fossies "Dox" file reference documentation.

    1 /* t-recsel.c - Module test for recsel.c
    2  * Copyright (C) 2016 Werner Koch
    3  *
    4  * This file is part of GnuPG.
    5  *
    6  * GnuPG is free software; you can redistribute it and/or modify
    7  * it under the terms of the GNU General Public License as published by
    8  * the Free Software Foundation; either version 3 of the License, or
    9  * (at your option) any later version.
   10  *
   11  * GnuPG is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  * GNU General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU General Public License
   17  * along with this program; if not, see <https://www.gnu.org/licenses/>.
   18  */
   19 
   20 #include <config.h>
   21 #include <stdio.h>
   22 #include <stdlib.h>
   23 #include <string.h>
   24 
   25 #include "util.h"
   26 #include "init.h"
   27 #include "recsel.h"
   28 
   29 #define PGM  "t-recsel"
   30 
   31 #define pass()  do { ; } while(0)
   32 #define fail(a,e)  do { log_error ("line %d: test %d failed: %s\n",     \
   33                                    __LINE__, (a), gpg_strerror ((e)));  \
   34                        exit (1);                                        \
   35                     } while(0)
   36 
   37 static int verbose;
   38 static int debug;
   39 
   40 
   41 #define FREEEXPR() do { recsel_release (se); se = NULL; } while (0)
   42 #define ADDEXPR(a) do {                         \
   43     err = recsel_parse_expr (&se, (a));         \
   44     if (err)                                    \
   45       fail (0, err);                            \
   46   } while (0)
   47 
   48 
   49 static const char *
   50 test_1_getval (void *cookie, const char *name)
   51 {
   52   if (strcmp (name, "uid"))
   53     fail (0, 0);
   54   return cookie;
   55 }
   56 
   57 static void
   58 run_test_1 (void)
   59 {
   60   static const char *expr[] = {
   61     "uid =~ Alfa",
   62     "&& uid !~   Test  ",
   63     "|| uid =~  Alpha",
   64     " uid  !~ Test"
   65   };
   66   gpg_error_t err;
   67   recsel_expr_t se = NULL;
   68   int i;
   69 
   70   for (i=0; i < DIM (expr); i++)
   71     {
   72       err = recsel_parse_expr (&se, expr[i]);
   73       if (err)
   74         fail (i, err);
   75     }
   76 
   77   if (debug)
   78     recsel_dump (se);
   79 
   80   /* The example from recsel.c in several variants. */
   81   if (!recsel_select (se, test_1_getval, "Alfa"))
   82     fail (0, 0);
   83   if (!recsel_select (se, test_1_getval, "Alpha"))
   84     fail (0, 0);
   85   if (recsel_select (se, test_1_getval, "Alfa Test"))
   86     fail (0, 0);
   87   if (recsel_select (se, test_1_getval, "Alpha Test"))
   88     fail (0, 0);
   89 
   90   /* Some modified versions from above.  */
   91   if (!recsel_select (se, test_1_getval, " AlfA Tes"))
   92     fail (0, 0);
   93   if (!recsel_select (se, test_1_getval, " AlfA Tes "))
   94     fail (0, 0);
   95   if (!recsel_select (se, test_1_getval, " Tes  AlfA"))
   96     fail (0, 0);
   97   if (!recsel_select (se, test_1_getval, "TesAlfA"))
   98     fail (0, 0);
   99 
  100   /* Simple cases. */
  101   if (recsel_select (se, NULL, NULL))
  102     fail (0, 0);
  103   if (recsel_select (se, test_1_getval, NULL))
  104     fail (0, 0);
  105   if (recsel_select (se, test_1_getval, ""))
  106     fail (0, 0);
  107 
  108   FREEEXPR();
  109 }
  110 
  111 
  112 /* Same as test1 but using a combined expression.. */
  113 static void
  114 run_test_1b (void)
  115 {
  116   gpg_error_t err;
  117   recsel_expr_t se = NULL;
  118 
  119   err = recsel_parse_expr
  120     (&se, "uid =~ Alfa && uid !~   Test  || uid =~  Alpha && uid  !~ Test" );
  121   if (err)
  122     fail (0, err);
  123 
  124   if (debug)
  125     recsel_dump (se);
  126 
  127   /* The example from recsel.c in several variants. */
  128   if (!recsel_select (se, test_1_getval, "Alfa"))
  129     fail (0, 0);
  130   if (!recsel_select (se, test_1_getval, "Alpha"))
  131     fail (0, 0);
  132   if (recsel_select (se, test_1_getval, "Alfa Test"))
  133     fail (0, 0);
  134   if (recsel_select (se, test_1_getval, "Alpha Test"))
  135     fail (0, 0);
  136 
  137   /* Some modified versions from above.  */
  138   if (!recsel_select (se, test_1_getval, " AlfA Tes"))
  139     fail (0, 0);
  140   if (!recsel_select (se, test_1_getval, " AlfA Tes "))
  141     fail (0, 0);
  142   if (!recsel_select (se, test_1_getval, " Tes  AlfA"))
  143     fail (0, 0);
  144   if (!recsel_select (se, test_1_getval, "TesAlfA"))
  145     fail (0, 0);
  146 
  147   /* Simple cases. */
  148   if (recsel_select (se, NULL, NULL))
  149     fail (0, 0);
  150   if (recsel_select (se, test_1_getval, NULL))
  151     fail (0, 0);
  152   if (recsel_select (se, test_1_getval, ""))
  153     fail (0, 0);
  154 
  155   FREEEXPR();
  156 }
  157 
  158 
  159 static const char *
  160 test_2_getval (void *cookie, const char *name)
  161 {
  162   if (!strcmp (name, "uid"))
  163     return "foo@example.org";
  164   else if (!strcmp (name, "keyid"))
  165     return "0x12345678";
  166   else if (!strcmp (name, "zero"))
  167     return "0";
  168   else if (!strcmp (name, "one"))
  169     return "1";
  170   else if (!strcmp (name, "blanks"))
  171     return "    ";
  172   else if (!strcmp (name, "letters"))
  173     return "abcde";
  174   else if (!strcmp (name, "str1"))
  175     return "aaa";
  176   else
  177     return cookie;
  178 }
  179 
  180 static void
  181 run_test_2 (void)
  182 {
  183   gpg_error_t err;
  184   recsel_expr_t se = NULL;
  185 
  186   ADDEXPR ("uid = foo@example.org");
  187   if (!recsel_select (se, test_2_getval, NULL))
  188     fail (0, 0);
  189   FREEEXPR();
  190   ADDEXPR ("uid = Foo@example.org");
  191   if (!recsel_select (se, test_2_getval, NULL))
  192     fail (0, 0);
  193   FREEEXPR();
  194   ADDEXPR ("-c uid = Foo@example.org");
  195   if (recsel_select (se, test_2_getval, NULL))
  196     fail (0, 0);
  197 
  198   FREEEXPR();
  199   ADDEXPR ("uid =~ foo@example.org");
  200   if (!recsel_select (se, test_2_getval, NULL))
  201     fail (0, 0);
  202   FREEEXPR();
  203   ADDEXPR ("uid =~ Foo@example.org");
  204   if (!recsel_select (se, test_2_getval, NULL))
  205     fail (0, 0);
  206   FREEEXPR();
  207   ADDEXPR ("-c uid =~ Foo@example.org");
  208   if (recsel_select (se, test_2_getval, NULL))
  209     fail (0, 0);
  210 
  211   FREEEXPR();
  212   ADDEXPR ("uid !~ foo@example.org");
  213   if (recsel_select (se, test_2_getval, NULL))
  214     fail (0, 0);
  215   FREEEXPR();
  216   ADDEXPR ("uid !~ Foo@example.org");
  217   if (recsel_select (se, test_2_getval, NULL))
  218     fail (0, 0);
  219   FREEEXPR();
  220   ADDEXPR ("-c uid !~ Foo@example.org");
  221   if (!recsel_select (se, test_2_getval, NULL))
  222     fail (0, 0);
  223 
  224   FREEEXPR();
  225   ADDEXPR ("uid =~ @");
  226   if (!recsel_select (se, test_2_getval, NULL))
  227     fail (0, 0);
  228   FREEEXPR();
  229   ADDEXPR ("uid =~ @");
  230   if (!recsel_select (se, test_2_getval, NULL))
  231     fail (0, 0);
  232 
  233   FREEEXPR();
  234   ADDEXPR ("keyid == 0x12345678");
  235   if (!recsel_select (se, test_2_getval, NULL))
  236     fail (0, 0);
  237   FREEEXPR();
  238   ADDEXPR ("keyid != 0x12345678");
  239   if (recsel_select (se, test_2_getval, NULL))
  240     fail (0, 0);
  241   FREEEXPR();
  242   ADDEXPR ("keyid >= 0x12345678");
  243   if (!recsel_select (se, test_2_getval, NULL))
  244     fail (0, 0);
  245   FREEEXPR();
  246   ADDEXPR ("keyid <= 0x12345678");
  247   if (!recsel_select (se, test_2_getval, NULL))
  248     fail (0, 0);
  249   FREEEXPR();
  250   ADDEXPR ("keyid > 0x12345677");
  251   if (!recsel_select (se, test_2_getval, NULL))
  252     fail (0, 0);
  253   FREEEXPR();
  254   ADDEXPR ("keyid < 0x12345679");
  255   if (!recsel_select (se, test_2_getval, NULL))
  256     fail (0, 0);
  257 
  258   FREEEXPR();
  259   ADDEXPR ("keyid > 0x12345678");
  260   if (recsel_select (se, test_2_getval, NULL))
  261     fail (0, 0);
  262   FREEEXPR();
  263   ADDEXPR ("keyid < 0x12345678");
  264   if (recsel_select (se, test_2_getval, NULL))
  265     fail (0, 0);
  266 
  267 
  268   FREEEXPR();
  269   ADDEXPR ("str1 -gt aa");
  270   if (!recsel_select (se, test_2_getval, NULL))
  271     fail (0, 0);
  272   FREEEXPR();
  273   ADDEXPR ("str1 -gt aaa");
  274   if (recsel_select (se, test_2_getval, NULL))
  275     fail (0, 0);
  276   FREEEXPR();
  277   ADDEXPR ("str1 -ge aaa");
  278   if (!recsel_select (se, test_2_getval, NULL))
  279     fail (0, 0);
  280   FREEEXPR();
  281   ADDEXPR ("str1 -lt aab");
  282   if (!recsel_select (se, test_2_getval, NULL))
  283     fail (0, 0);
  284   FREEEXPR();
  285   ADDEXPR ("str1 -le aaa");
  286   if (!recsel_select (se, test_2_getval, NULL))
  287     fail (0, 0);
  288 
  289   FREEEXPR();
  290   ADDEXPR ("-c str1 -lt AAB");
  291   if (recsel_select (se, test_2_getval, NULL))
  292     fail (0, 0);
  293   FREEEXPR();
  294   ADDEXPR ("str1 -lt AAB");
  295   if (!recsel_select (se, test_2_getval, NULL))
  296     fail (0, 0);
  297 
  298 
  299   FREEEXPR();
  300   ADDEXPR ("uid -n");
  301   if (!recsel_select (se, test_2_getval, NULL))
  302     fail (0, 0);
  303   FREEEXPR();
  304   ADDEXPR ("uid -z");
  305   if (recsel_select (se, test_2_getval, NULL))
  306     fail (0, 0);
  307 
  308   FREEEXPR();
  309   ADDEXPR ("nothing -z");
  310   if (!recsel_select (se, test_2_getval, NULL))
  311     fail (0, 0);
  312   FREEEXPR();
  313   ADDEXPR ("nothing -n");
  314   if (recsel_select (se, test_2_getval, NULL))
  315     fail (0, 0);
  316 
  317   FREEEXPR();
  318   ADDEXPR ("blanks -n");
  319   if (!recsel_select (se, test_2_getval, NULL))
  320     fail (0, 0);
  321   FREEEXPR();
  322   ADDEXPR ("blanks -z");
  323   if (recsel_select (se, test_2_getval, NULL))
  324     fail (0, 0);
  325 
  326   FREEEXPR();
  327   ADDEXPR ("letters -n");
  328   if (!recsel_select (se, test_2_getval, NULL))
  329     fail (0, 0);
  330   FREEEXPR();
  331   ADDEXPR ("letters -z");
  332   if (recsel_select (se, test_2_getval, NULL))
  333     fail (0, 0);
  334 
  335 
  336   FREEEXPR();
  337   ADDEXPR ("nothing -f");
  338   if (!recsel_select (se, test_2_getval, NULL))
  339     fail (0, 0);
  340   FREEEXPR();
  341   ADDEXPR ("nothing -t");
  342   if (recsel_select (se, test_2_getval, NULL))
  343     fail (0, 0);
  344 
  345   FREEEXPR();
  346   ADDEXPR ("zero -f");
  347   if (!recsel_select (se, test_2_getval, NULL))
  348     fail (0, 0);
  349   FREEEXPR();
  350   ADDEXPR ("zero -t");
  351   if (recsel_select (se, test_2_getval, NULL))
  352     fail (0, 0);
  353 
  354   FREEEXPR();
  355   ADDEXPR ("one -t");
  356   if (!recsel_select (se, test_2_getval, NULL))
  357     fail (0, 0);
  358   FREEEXPR();
  359   ADDEXPR ("one -f");
  360   if (recsel_select (se, test_2_getval, NULL))
  361     fail (0, 0);
  362 
  363   FREEEXPR();
  364   ADDEXPR ("blanks -f");
  365   if (!recsel_select (se, test_2_getval, NULL))
  366     fail (0, 0);
  367   FREEEXPR();
  368   ADDEXPR ("blanks -t");
  369   if (recsel_select (se, test_2_getval, NULL))
  370     fail (0, 0);
  371 
  372   FREEEXPR();
  373   ADDEXPR ("letter -f");
  374   if (!recsel_select (se, test_2_getval, NULL))
  375     fail (0, 0);
  376   FREEEXPR();
  377   ADDEXPR ("letters -t");
  378   if (recsel_select (se, test_2_getval, NULL))
  379     fail (0, 0);
  380 
  381 
  382   FREEEXPR();
  383 }
  384 
  385 
  386 
  387 int
  388 main (int argc, char **argv)
  389 {
  390   int last_argc = -1;
  391 
  392   log_set_prefix (PGM, GPGRT_LOG_WITH_PREFIX);
  393   init_common_subsystems (&argc, &argv);
  394 
  395   if (argc)
  396     { argc--; argv++; }
  397   while (argc && last_argc != argc )
  398     {
  399       last_argc = argc;
  400       if (!strcmp (*argv, "--"))
  401         {
  402           argc--; argv++;
  403           break;
  404         }
  405       else if (!strcmp (*argv, "--help"))
  406         {
  407           fputs ("usage: " PGM " [options]\n"
  408                  "Options:\n"
  409                  "  --verbose       print timings etc.\n"
  410                  "  --debug         flyswatter\n",
  411                  stdout);
  412           exit (0);
  413         }
  414       else if (!strcmp (*argv, "--verbose"))
  415         {
  416           verbose++;
  417           argc--; argv++;
  418         }
  419       else if (!strcmp (*argv, "--debug"))
  420         {
  421           verbose += 2;
  422           debug++;
  423           argc--; argv++;
  424         }
  425       else if (!strncmp (*argv, "--", 2))
  426         {
  427           log_error ("unknown option '%s'\n", *argv);
  428           exit (2);
  429         }
  430     }
  431 
  432   run_test_1 ();
  433   run_test_1b ();
  434   run_test_2 ();
  435   /* Fixme: We should add test for complex conditions.  */
  436 
  437   return 0;
  438 }