1 /* t-g13tuple.c - Module test for g13tuple.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 <assert.h>
24
25
26 #include "../common/util.h"
27 #include "keyblob.h"
28 #include "g13tuple.h"
29
30 #define PGM "t-g13tuple"
31
32 static int verbose;
33 static int debug;
34 static int errcount;
35
36 /* Test for the functions append_tuple_uint and find_tuple_unit. */
37 static void
test_tuple_uint(void)38 test_tuple_uint (void)
39 {
40 static struct {
41 int tag;
42 int len;
43 char *data;
44 unsigned long long val;
45 gpg_err_code_t ec;
46 } tv[] = {
47 { 1, 0, "", 0, GPG_ERR_ERANGE },
48 { 2, 1, "\x00", 0, 0},
49 { 3, 1, "\x7f", 127ull, 0},
50 { 4, 1, "\x80", 0, GPG_ERR_ERANGE },
51 { 5, 1, "\x81", 0, GPG_ERR_ERANGE },
52 { 6, 2, "\x80\x01", 0, GPG_ERR_ERANGE },
53 { 7, 2, "\x00\x80", 128ull, 0 },
54 { 8, 1, "\x01", 1, 0 },
55 { 9, 1, "\x40", 64, 0 },
56 { 10, 2, "\x40\x00", 16384, 0 },
57 { 11, 8, "\x7f\xff\xff\xff\xff\xff\xff\xff", 0x7fffffffffffffffull, 0 },
58 { 12, 9, "\x00\xff\xff\xff\xff\xff\xff\xff\xff", 0xffffffffffffffffull, 0},
59 { 13, 9, "\x01\xff\xff\xff\xff\xff\xff\xff\xff", 0, GPG_ERR_ERANGE }
60 };
61 int tidx;
62 gpg_error_t err;
63 membuf_t mb, mb2;
64 void *p;
65 const void *s;
66 size_t n;
67 tupledesc_t tuples;
68 tupledesc_t tuples2;
69 unsigned long long value;
70 int i;
71
72 init_membuf (&mb, 512);
73 init_membuf (&mb2, 512);
74 append_tuple (&mb, KEYBLOB_TAG_BLOBVERSION, "\x01", 1);
75 append_tuple (&mb2, KEYBLOB_TAG_BLOBVERSION, "\x01", 1);
76 for (tidx=0; tidx < DIM (tv); tidx++)
77 {
78 append_tuple (&mb, tv[tidx].tag, tv[tidx].data, tv[tidx].len);
79 if (!tv[tidx].ec)
80 append_tuple_uint (&mb2, tv[tidx].tag, tv[tidx].val);
81 }
82
83 p = get_membuf (&mb, &n);
84 if (!p)
85 {
86 err = gpg_error_from_syserror ();
87 fprintf (stderr, PGM ":%s: get_membuf failed: %s\n",
88 __func__, gpg_strerror (err));
89 exit (1);
90 }
91 err = create_tupledesc (&tuples, p, n);
92 if (err)
93 {
94 fprintf (stderr, PGM ":%s: create_tupledesc failed: %s\n",
95 __func__, gpg_strerror (err));
96 exit (1);
97 }
98 p = get_membuf (&mb2, &n);
99 if (!p)
100 {
101 err = gpg_error_from_syserror ();
102 fprintf (stderr, PGM ":%s: get_membuf failed: %s\n",
103 __func__, gpg_strerror (err));
104 exit (1);
105 }
106 err = create_tupledesc (&tuples2, p, n);
107 if (err)
108 {
109 fprintf (stderr, PGM ":%s: create_tupledesc failed: %s\n",
110 __func__, gpg_strerror (err));
111 exit (1);
112 }
113
114 for (tidx=0; tidx < DIM (tv); tidx++)
115 {
116 err = find_tuple_uint (tuples, tv[tidx].tag, &value);
117 if (tv[tidx].ec != gpg_err_code (err))
118 {
119 fprintf (stderr, PGM ":%s:tidx=%d: wrong error returned; "
120 "expected(%s) got(%s)\n",
121 __func__, tidx,
122 gpg_strerror (tv[tidx].ec), gpg_strerror (err));
123 errcount++;
124 }
125 else if (!err && tv[tidx].val != value)
126 {
127 fprintf (stderr, PGM ":%s:tidx=%d: wrong value returned; "
128 "expected(%llx) got(%llx)\n",
129 __func__, tidx, tv[tidx].val, value);
130 errcount++;
131 }
132
133 err = find_tuple_uint (tuples2, tv[tidx].tag, &value);
134 if (gpg_err_code (err) == GPG_ERR_NOT_FOUND)
135 {
136 if (!tv[tidx].ec)
137 {
138 fprintf (stderr, PGM ":%s:tidx=%d: find_tuple failed: %s\n",
139 __func__, tidx, gpg_strerror (err));
140 errcount++;
141 }
142 }
143 else if (tv[tidx].ec != gpg_err_code (err))
144 {
145 fprintf (stderr, PGM ":%s:tidx=%d: wrong error returned (2); "
146 "expected(%s) got(%s)\n",
147 __func__, tidx,
148 gpg_strerror (tv[tidx].ec), gpg_strerror (err));
149 errcount++;
150 }
151 else if (!err && tv[tidx].val != value)
152 {
153 fprintf (stderr, PGM ":%s:tidx=%d: wrong value returned (2); "
154 "expected(%llx) got(%llx)\n",
155 __func__, tidx, tv[tidx].val, value);
156 errcount++;
157 }
158
159 s = find_tuple (tuples2, tv[tidx].tag, &n);
160 if (!s)
161 ;
162 else if (tv[tidx].len != n)
163 {
164 fprintf (stderr, PGM ":%s:tidx=%d: wrong string length returned; "
165 "expected(%d) got(%zu)\n",
166 __func__, tidx, tv[tidx].len, n);
167 errcount++;
168 }
169 else if (memcmp (tv[tidx].data, s, n))
170 {
171 fprintf (stderr, PGM ":%s:tidx=%d: wrong string returned:",
172 __func__, tidx);
173 for (i=0; i < n; i++)
174 fprintf (stderr, " %02x", ((unsigned char*)s)[i]);
175 fputc ('\n', stderr);
176 errcount++;
177 }
178 }
179
180 destroy_tupledesc (tuples);
181 destroy_tupledesc (tuples2);
182 }
183
184
185
186 int
main(int argc,char ** argv)187 main (int argc, char **argv)
188 {
189 int last_argc = -1;
190
191 gpgrt_init ();
192 if (argc)
193 { argc--; argv++; }
194 while (argc && last_argc != argc )
195 {
196 last_argc = argc;
197 if (!strcmp (*argv, "--"))
198 {
199 argc--; argv++;
200 break;
201 }
202 else if (!strcmp (*argv, "--verbose"))
203 {
204 verbose++;
205 argc--; argv++;
206 }
207 else if (!strcmp (*argv, "--debug"))
208 {
209 verbose += 2;
210 debug++;
211 argc--; argv++;
212 }
213 else if (!strncmp (*argv, "--", 2))
214 {
215 fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
216 exit (1);
217 }
218 }
219
220 test_tuple_uint ();
221
222 return !!errcount;
223 }
224