gs2/test-parser.c

Go to the documentation of this file.
00001 /* test-parser.c --- Self tests of GS2 parser & printer.
00002  * Copyright (C) 2006, 2007, 2008  Simon Josefsson
00003  *
00004  * This file is part of GNU SASL Library.
00005  *
00006  * GNU SASL Library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation; either version 2.1 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * GNU SASL Library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with GNU SASL Library; if not, write to the Free
00018  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 
00027 #include "gs2parser.h"
00028 
00029 #define ZERO "\x00\x00\x00\x00"
00030 #define ONE "\x00\x00\x00\x01"
00031 #define TWO "\x00\x00\x00\x02"
00032 #define DATA "\x41"
00033 #define DATA2 "\x42"
00034 
00035 struct
00036 {
00037   char *name;
00038   char *token;
00039   size_t length;
00040   int expected_rc;
00041   char *expected_context;
00042   size_t expected_context_length;
00043   char *expected_wrap;
00044   size_t expected_wrap_length;
00045 } tv[] =
00046 {
00047   /* *INDENT-OFF* */
00048   { "string0", "foobarbaz", 0, -1 },
00049   { "string1", "foobarbaz", 1, -1 },
00050   { "string2", "foobarbaz", 2, -1 },
00051   { "string3", "foobarbaz", 3, -1 },
00052   { "string4", "foobarbaz", 4, -1 },
00053   { "string5", "foobarbaz", 5, -1 },
00054   { "string6", "foobarbaz", 6, -1 },
00055   { "string7", "foobarbaz", 7, -1 },
00056   { "string8", "foobarbaz", 8, -1 },
00057   { "string9", "foobarbaz", 9, -1 },
00058   { "allzero", ZERO ZERO, 8, -1 },
00059   { "allzero-overlong", ZERO ZERO DATA, 9, -1 },
00060   { "one-empty", ONE ZERO, 8, -1 },
00061   { "one-empty2", ZERO ONE, 8, -1 },
00062   { "size-one", ONE ZERO DATA, 9, 0, DATA, 1, NULL, 0 },
00063   { "size-one2", ZERO ONE DATA, 9, 0, NULL, 0, DATA, 1 },
00064   { "size-one3", ONE ONE DATA DATA, 10, 0, DATA, 1, DATA, 1 },
00065   { "size-one-overlong", ZERO ONE DATA DATA, 10, -1 },
00066   { "size-one-overlong2", ONE ZERO DATA DATA, 10, -1 },
00067   { "size-one-overlong3", ONE ONE DATA DATA DATA, 11, -1 },
00068   { "size-two", TWO TWO DATA DATA2 DATA DATA2, 12, 0,
00069     DATA DATA2, 2, DATA2 DATA, 2 },
00070   /* *INDENT-ON* */
00071 };
00072 
00073 int
00074 main (int argc, char *argv[])
00075 {
00076   struct gs2_token tok;
00077   int rc;
00078   size_t i;
00079 
00080   for (i = 0; i < sizeof (tv) / sizeof (tv[0]); i++)
00081     {
00082       rc = gs2_parser (tv[i].token, tv[i].length, &tok);
00083       if (rc != tv[i].expected_rc)
00084         {
00085           printf ("gs2 tv[%d] '%s': %.*s expected %d got %d\n",
00086                   i, tv[i].name, tv[i].length,
00087                   tv[i].token, tv[i].expected_rc, rc);
00088         }
00089       if (rc >= 0 &&
00090           (tv[i].expected_context_length != tok.context_length ||
00091            memcmp (tv[i].expected_context, tok.context_token,
00092                    tok.context_length) != 0))
00093         {
00094           printf ("gs2 tv[%d] '%s': "
00095                   "expected context %.*s (size %d) got %.*s (size %d)\n",
00096                   i, tv[i].name,
00097                   tv[i].expected_context_length,
00098                   tv[i].expected_context,
00099                   tv[i].expected_context_length,
00100                   tok.context_length, tok.context_token, tok.context_length);
00101           abort ();
00102         }
00103     }
00104 
00105   return 0;
00106 }

Generated on Tue Oct 21 18:28:19 2008 for gsasl by  doxygen 1.5.6