summaryrefslogtreecommitdiffstats
path: root/g13/t-g13tuple.c
blob: 2809d23f77d6608191b92d446b3eda6c820108a0 (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
/* t-g13tuple.c - Module test for g13tuple.c
 * Copyright (C) 2016 Werner Koch
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuPG 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>


#include "../common/util.h"
#include "keyblob.h"
#include "g13tuple.h"

#define PGM "t-g13tuple"

static int verbose;
static int debug;
static int errcount;

/* Test for the functions append_tuple_uint and find_tuple_unit.  */
static void
test_tuple_uint (void)
{
  static struct {
    int tag;
    int len;
    char *data;
    unsigned long long val;
    gpg_err_code_t ec;
  } tv[] = {
    { 1, 0, "",     0, GPG_ERR_ERANGE },
    { 2, 1, "\x00", 0, 0},
    { 3, 1, "\x7f", 127ull, 0},
    { 4, 1, "\x80", 0, GPG_ERR_ERANGE },
    { 5, 1, "\x81", 0, GPG_ERR_ERANGE },
    { 6, 2, "\x80\x01", 0, GPG_ERR_ERANGE },
    { 7, 2, "\x00\x80", 128ull, 0 },
    { 8, 1, "\x01", 1, 0 },
    { 9, 1, "\x40", 64, 0 },
    { 10, 2, "\x40\x00", 16384, 0 },
    { 11, 8, "\x7f\xff\xff\xff\xff\xff\xff\xff", 0x7fffffffffffffffull, 0 },
    { 12, 9, "\x00\xff\xff\xff\xff\xff\xff\xff\xff", 0xffffffffffffffffull, 0},
    { 13, 9, "\x01\xff\xff\xff\xff\xff\xff\xff\xff", 0, GPG_ERR_ERANGE }
  };
  int tidx;
  gpg_error_t err;
  membuf_t mb, mb2;
  void *p;
  const void *s;
  size_t n;
  tupledesc_t tuples;
  tupledesc_t tuples2;
  unsigned long long value;
  int i;

  init_membuf (&mb, 512);
  init_membuf (&mb2, 512);
  append_tuple (&mb, KEYBLOB_TAG_BLOBVERSION, "\x01", 1);
  append_tuple (&mb2, KEYBLOB_TAG_BLOBVERSION, "\x01", 1);
  for (tidx=0; tidx < DIM (tv); tidx++)
    {
      append_tuple (&mb, tv[tidx].tag, tv[tidx].data, tv[tidx].len);
      if (!tv[tidx].ec)
        append_tuple_uint (&mb2, tv[tidx].tag, tv[tidx].val);
    }

  p = get_membuf (&mb, &n);
  if (!p)
    {
      err = gpg_error_from_syserror ();
      fprintf (stderr, PGM ":%s: get_membuf failed: %s\n",
               __func__, gpg_strerror (err));
      exit (1);
    }
  err = create_tupledesc (&tuples, p, n);
  if (err)
    {
      fprintf (stderr, PGM ":%s: create_tupledesc failed: %s\n",
               __func__, gpg_strerror (err));
      exit (1);
    }
  p = get_membuf (&mb2, &n);
  if (!p)
    {
      err = gpg_error_from_syserror ();
      fprintf (stderr, PGM ":%s: get_membuf failed: %s\n",
               __func__, gpg_strerror (err));
      exit (1);
    }
  err = create_tupledesc (&tuples2, p, n);
  if (err)
    {
      fprintf (stderr, PGM ":%s: create_tupledesc failed: %s\n",
               __func__, gpg_strerror (err));
      exit (1);
    }

  for (tidx=0; tidx < DIM (tv); tidx++)
    {
      err = find_tuple_uint (tuples, tv[tidx].tag, &value);
      if (tv[tidx].ec != gpg_err_code (err))
        {
          fprintf (stderr, PGM ":%s:tidx=%d: wrong error returned; "
                   "expected(%s) got(%s)\n",
                   __func__, tidx,
                   gpg_strerror (tv[tidx].ec), gpg_strerror (err));
          errcount++;
        }
      else if (!err && tv[tidx].val != value)
        {
          fprintf (stderr, PGM ":%s:tidx=%d: wrong value returned; "
                   "expected(%llx) got(%llx)\n",
                   __func__, tidx, tv[tidx].val, value);
          errcount++;
        }

      err = find_tuple_uint (tuples2, tv[tidx].tag, &value);
      if (gpg_err_code (err) == GPG_ERR_NOT_FOUND)
        {
          if (!tv[tidx].ec)
            {
              fprintf (stderr, PGM ":%s:tidx=%d: find_tuple failed: %s\n",
                       __func__, tidx, gpg_strerror (err));
              errcount++;
            }
        }
      else if (tv[tidx].ec != gpg_err_code (err))
        {
          fprintf (stderr, PGM ":%s:tidx=%d: wrong error returned (2); "
                   "expected(%s) got(%s)\n",
                   __func__, tidx,
                   gpg_strerror (tv[tidx].ec), gpg_strerror (err));
          errcount++;
        }
      else if (!err && tv[tidx].val != value)
        {
          fprintf (stderr, PGM ":%s:tidx=%d: wrong value returned (2); "
                   "expected(%llx) got(%llx)\n",
                   __func__, tidx, tv[tidx].val, value);
          errcount++;
        }

      s = find_tuple (tuples2, tv[tidx].tag, &n);
      if (!s)
        ;
      else if (tv[tidx].len != n)
        {
          fprintf (stderr, PGM ":%s:tidx=%d: wrong string length returned; "
                   "expected(%d) got(%zu)\n",
                   __func__, tidx, tv[tidx].len, n);
          errcount++;
            }
      else if (memcmp (tv[tidx].data, s, n))
        {
          fprintf (stderr, PGM ":%s:tidx=%d: wrong string returned:",
                   __func__, tidx);
          for (i=0; i < n; i++)
            fprintf (stderr, " %02x", ((unsigned char*)s)[i]);
          fputc ('\n', stderr);
          errcount++;
        }
    }

  destroy_tupledesc (tuples);
  destroy_tupledesc (tuples2);
}



int
main (int argc, char **argv)
{
  int last_argc = -1;

  gpgrt_init ();
  if (argc)
    { argc--; argv++; }
  while (argc && last_argc != argc )
    {
      last_argc = argc;
      if (!strcmp (*argv, "--"))
        {
          argc--; argv++;
          break;
        }
      else if (!strcmp (*argv, "--verbose"))
        {
          verbose++;
          argc--; argv++;
        }
      else if (!strcmp (*argv, "--debug"))
        {
          verbose += 2;
          debug++;
          argc--; argv++;
        }
      else if (!strncmp (*argv, "--", 2))
        {
          fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
          exit (1);
        }
    }

  test_tuple_uint ();

  return !!errcount;
}