summaryrefslogtreecommitdiff
path: root/tests/tsexp.c
blob: 2615f42448664b8591a2052bb38334a6573a7b82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* tsexp.c  -  S-expression regression tests
 *	Copyright (C) 2001, 2002 Free Software Foundation, Inc.
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "../src/gcrypt.h"

#define PGMNAME "tsexp"

static int verbose;
static int error_count;

static void
info (const char *format, ...)
{
  va_list arg_ptr;

  if (verbose)
    {
      va_start( arg_ptr, format ) ;
      vfprintf (stderr, format, arg_ptr );
      va_end(arg_ptr);
    }
}

static void
fail ( const char *format, ... )
{
    va_list arg_ptr ;

    fputs (PGMNAME ": ", stderr);
    va_start( arg_ptr, format ) ;
    vfprintf (stderr, format, arg_ptr );
    va_end(arg_ptr);
    error_count++;
}


/* fixme: we need better tests */
static void
basic (void)
{
  GCRY_SEXP sexp;
  int idx;
  const char *string;
  static struct {
    const char *token;
    const char *parm;
  } values[] = {
    { "public-key", NULL },
    { "dsa", NULL },
    { "dsa", "p" },
    { "dsa", "y" },
    { "dsa", "q" },
    { "dsa", "g" },
    { NULL }
  };

  info ("doing some pretty pointless tests\n"); 
  string = ("(public-key (dsa (p #41424344#) (y this_is_y) "
            "(q #61626364656667#) (g %m)))");
     
  if ( gcry_sexp_build (&sexp, NULL, string, gcry_mpi_set_ui (NULL, 42)) )
    {
      fail (" scanning `%s' failed\n", string);
      return;
    }

  /* now find something */
  for (idx=0; values[idx].token; idx++)
    {
      const char *token = values[idx].token;
      const char *parm = values[idx].parm;
      GCRY_SEXP s1, s2;
      GCRY_MPI a;
      const char *p;
      size_t n;

      s1 = gcry_sexp_find_token (sexp, token, strlen(token) );
      if (!s1)
        {
          fail ("didn't found `%s'\n", token);
          continue;
        }

      p = gcry_sexp_nth_data (s1, 0, &n);
      if (!p)
        {
          fail ("no car for `%s'\n", token);
          continue;
        }
      info ("car=`%.*s'\n", (int)n, p);

      s2 = gcry_sexp_cdr (s1);
      if (!s2) 
        {
          fail ("no cdr for `%s'\n", token);
          continue;
        }

      p = gcry_sexp_nth_data (s2, 0, &n);
      if (p)
        {
          fail ("data at car of `%s'\n", token);
          continue;
        }

      if (parm)
        {
          s2 = gcry_sexp_find_token (s1, parm, strlen (parm));
          if (!s2)
	    {
              fail ("didn't found `%s'\n", parm);
              continue;
	    }
          p = gcry_sexp_nth_data (s2, 0, &n);
          if (!p) 
            {
              fail("no car for `%s'\n", parm );
              continue;
            }
          info ("car=`%.*s'\n", (int)n, p);
          p = gcry_sexp_nth_data (s2, 1, &n);
          if (!p) 
            {
              fail("no cdr for `%s'\n", parm );
              continue;
            }
          info ("cdr=`%.*s'\n", (int)n, p);
          
          a = gcry_sexp_nth_mpi (s2, 0, GCRYMPI_FMT_USG);
          if (!a)
	    {
              fail("failed to cdr the mpi for `%s'\n", parm);
              continue;
            }
        }
    }
}


static void
canon_len (void)
{
  static struct {
    size_t textlen; /* length of the buffer */
    size_t expected;/* expected length or 0 on error and then ... */
    size_t erroff;  /* ... and at this offset */
    int errcode;    /* ... with this error code */
    unsigned char *text; 
  } values[] = {
    { 14, 13, 0,  0, "(9:abcdefghi) " },
    { 16, 15, 0,  0, "(10:abcdefghix)" },
    { 14,  0,14, -2, "(10:abcdefghi)" },
    { 15,  0, 1, -7, "(010:abcdefghi)" },
    {  2,  0, 0, -4, "1:"},
    {  4,  0, 4, -2, "(1:)"},
    {  5,  5, 0,  0, "(1:x)"},
    {  2,  2, 0,  0, "()"},
    {  4,  2, 0,  0, "()()"},
    {  4,  4, 0,  0, "(())"},
    {  3,  0, 3, -2, "(()"},
    {  3,  0, 1, -5, "( )"},
    {  9,  9, 0,  0, "(3:abc())"},
    { 10,  0, 6, -5, "(3:abc ())"},
    /* fixme: we need much more cases */
    { 0 },
  };
  int idx;
  int errcode;
  size_t n, erroff;

  info ("checking canoncial length test function\n");
  for (idx=0; values[idx].text; idx++)
    {
      n = gcry_sexp_canon_len (values[idx].text, values[idx].textlen, 
                               &erroff, &errcode);
      
      if (n && n == values[idx].expected)
        ; /* success */
      else if (!n && !values[idx].expected)
        { /* we expected an error - check that this is the right one */
          if (values[idx].erroff != erroff)
            fail ("canonical length test %d - wrong error offset %u\n",
                  idx, (unsigned int)erroff);
          if (values[idx].errcode != errcode)
            fail ("canonical length test %d - wrong error code %d\n",
                  idx, errcode);
        }
      else
        fail ("canonical length test %d failed - n=%u, off=%u, err=%d\n",
              idx, (unsigned int)n, (unsigned int)erroff, errcode);
    }
}


int
main (int argc, char **argv)
{
  if (argc > 1 && !strcmp (argv[1], "-v"))
    verbose = 1;

  basic ();
  canon_len ();
  
  return error_count? 1:0;
}