1/* ac-data.c - Public key encryption/decryption tests
2 *	Copyright (C) 2005 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt 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 Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24#include <stdlib.h>
25#include <stdio.h>
26#include <assert.h>
27
28#define assert_err(err) \
29  do \
30    if (err) \
31      { \
32        fprintf (stderr, "Error occurred at line %i: %s\n", \
33                 __LINE__, gcry_strerror (err)); \
34        exit (1); \
35      } \
36  while (0)
37
38#include "../src/gcrypt.h"
39
40static int verbose;
41
42static void
43die (const char *format, ...)
44{
45  va_list arg_ptr ;
46
47  va_start( arg_ptr, format ) ;
48  vfprintf (stderr, format, arg_ptr );
49  va_end(arg_ptr);
50  exit (1);
51}
52
53static void
54check_sexp_conversion (gcry_ac_data_t data, const char **identifiers)
55{
56  gcry_ac_data_t data2;
57  gcry_error_t err;
58  gcry_sexp_t sexp;
59  unsigned int i;
60  const char *label1, *label2;
61  gcry_mpi_t mpi1, mpi2;
62  size_t length1, length2;
63
64  err = gcry_ac_data_to_sexp (data, &sexp, identifiers);
65  assert_err (err);
66  if (verbose)
67    gcry_sexp_dump (sexp);
68  err = gcry_ac_data_from_sexp (&data2, sexp, identifiers);
69  assert_err (err);
70
71  length1 = gcry_ac_data_length (data);
72  length2 = gcry_ac_data_length (data2);
73  assert (length1 == length2);
74
75  for (i = 0; i < length1; i++)
76    {
77      err = gcry_ac_data_get_index (data, 0, i, &label1, &mpi1);
78      assert_err (err);
79      err = gcry_ac_data_get_index (data2, 0, i, &label2, &mpi2);
80      assert_err (err);
81      if (verbose)
82        {
83          fprintf (stderr, "Label1=`%s'\n", label1);
84          fprintf (stderr, "Label2=`%s'\n", label2);
85        }
86      assert (! strcmp (label1, label2));
87      assert (! gcry_mpi_cmp (mpi1, mpi2));
88    }
89
90  gcry_ac_data_destroy (data2);
91  gcry_sexp_release (sexp);
92}
93
94void
95check_run (void)
96{
97  const char *identifiers[] = { "foo",
98				"bar",
99				"baz",
100				"hello",
101				"somemoretexthere",
102				"blahblahblah",
103				NULL };
104  const char *identifiers_null[] = { NULL };
105  gcry_ac_data_t data;
106  gcry_error_t err;
107  const char *label0;
108  const char *label1;
109  gcry_mpi_t mpi0;
110  gcry_mpi_t mpi1;
111  gcry_mpi_t mpi2;
112
113  /* Initialize values.  */
114
115  label0 = "thisisreallylonglabelbutsincethereisnolimitationonthelengthoflabelsitshouldworkjustfine";
116  mpi0 = gcry_mpi_new (0);
117  assert (mpi0);
118  gcry_mpi_set_ui (mpi0, 123456);
119
120  err = gcry_ac_data_new (&data);
121  assert_err (err);
122
123  check_sexp_conversion (data, identifiers);
124  check_sexp_conversion (data, identifiers_null);
125  check_sexp_conversion (data, NULL);
126
127  err = gcry_ac_data_set (data, 0, label0, mpi0);
128  assert_err (err);
129  err = gcry_ac_data_get_index (data, 0, 0, &label1, &mpi1);
130  assert_err (err);
131  assert (label0 == label1);
132  assert (mpi0 == mpi1);
133  check_sexp_conversion (data, identifiers);
134  check_sexp_conversion (data, identifiers_null);
135  check_sexp_conversion (data, NULL);
136
137  if (verbose)
138    printf ("data-set-test-0 succeeded\n");
139
140  gcry_ac_data_clear (data);
141
142  err = gcry_ac_data_set (data, GCRY_AC_FLAG_COPY, label0, mpi0);
143  assert_err (err);
144
145  err = gcry_ac_data_set (data, GCRY_AC_FLAG_COPY, "foo", mpi0);
146  assert_err (err);
147  err = gcry_ac_data_set (data, GCRY_AC_FLAG_COPY, "foo", mpi0);
148  assert_err (err);
149  err = gcry_ac_data_set (data, GCRY_AC_FLAG_COPY, "bar", mpi0);
150  assert_err (err);
151  err = gcry_ac_data_set (data, GCRY_AC_FLAG_COPY, "blah1", mpi0);
152  assert_err (err);
153  check_sexp_conversion (data, identifiers);
154  check_sexp_conversion (data, identifiers_null);
155  check_sexp_conversion (data, NULL);
156
157  err = gcry_ac_data_get_name (data, 0, label0, &mpi1);
158  assert_err (err);
159  assert (mpi0 != mpi1);
160  err = gcry_ac_data_get_name (data, GCRY_AC_FLAG_COPY, label0, &mpi2);
161  assert_err (err);
162  assert (mpi0 != mpi1);
163  assert (mpi1 != mpi2);
164  err = gcry_ac_data_get_index (data, 0, 0, &label1, &mpi1);
165  assert_err (err);
166  gcry_mpi_release (mpi0);
167  gcry_mpi_release (mpi2);
168
169  if (verbose)
170    printf ("data-set-test-1 succeeded\n");
171
172  gcry_ac_data_clear (data);
173  assert (! gcry_ac_data_length (data));
174  check_sexp_conversion (data, identifiers);
175  check_sexp_conversion (data, identifiers_null);
176  check_sexp_conversion (data, NULL);
177
178  if (verbose)
179    printf ("data-set-test-2 succeeded\n");
180
181  gcry_ac_data_destroy (data);
182
183
184}
185
186int
187main (int argc, char **argv)
188{
189  int debug = 0;
190  int i = 1;
191
192  if (argc > 1 && !strcmp (argv[1], "--verbose"))
193    verbose = 1;
194  else if (argc > 1 && !strcmp (argv[1], "--debug"))
195    verbose = debug = 1;
196
197  gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
198  if (!gcry_check_version (GCRYPT_VERSION))
199    die ("version mismatch\n");
200  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
201  if (debug)
202    gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
203
204  for (; i > 0; i--)
205    check_run ();
206
207  return 0;
208}
209