1 /* t-g13tuple.c - Module test for g13tuple.c
2 * Copyright (C) 2016 Werner Koch
4 * This file is part of GnuPG.
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.
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.
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/>.
30 #define PGM "t-g13tuple"
36 /* Test for the functions append_tuple_uint and find_tuple_unit. */
38 test_tuple_uint (void)
44 unsigned long long val;
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 }
69 unsigned long long value;
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++)
78 append_tuple (&mb, tv[tidx].tag, tv[tidx].data, tv[tidx].len);
80 append_tuple_uint (&mb2, tv[tidx].tag, tv[tidx].val);
83 p = get_membuf (&mb, &n);
86 err = gpg_error_from_syserror ();
87 fprintf (stderr, PGM ":%s: get_membuf failed: %s\n",
88 __func__, gpg_strerror (err));
91 err = create_tupledesc (&tuples, p, n);
94 fprintf (stderr, PGM ":%s: create_tupledesc failed: %s\n",
95 __func__, gpg_strerror (err));
98 p = get_membuf (&mb2, &n);
101 err = gpg_error_from_syserror ();
102 fprintf (stderr, PGM ":%s: get_membuf failed: %s\n",
103 __func__, gpg_strerror (err));
106 err = create_tupledesc (&tuples2, p, n);
109 fprintf (stderr, PGM ":%s: create_tupledesc failed: %s\n",
110 __func__, gpg_strerror (err));
114 for (tidx=0; tidx < DIM (tv); tidx++)
116 err = find_tuple_uint (tuples, tv[tidx].tag, &value);
117 if (tv[tidx].ec != gpg_err_code (err))
119 fprintf (stderr, PGM ":%s:tidx=%d: wrong error returned; "
120 "expected(%s) got(%s)\n",
122 gpg_strerror (tv[tidx].ec), gpg_strerror (err));
125 else if (!err && tv[tidx].val != value)
127 fprintf (stderr, PGM ":%s:tidx=%d: wrong value returned; "
128 "expected(%llx) got(%llx)\n",
129 __func__, tidx, tv[tidx].val, value);
133 err = find_tuple_uint (tuples2, tv[tidx].tag, &value);
134 if (gpg_err_code (err) == GPG_ERR_NOT_FOUND)
138 fprintf (stderr, PGM ":%s:tidx=%d: find_tuple failed: %s\n",
139 __func__, tidx, gpg_strerror (err));
143 else if (tv[tidx].ec != gpg_err_code (err))
145 fprintf (stderr, PGM ":%s:tidx=%d: wrong error returned (2); "
146 "expected(%s) got(%s)\n",
148 gpg_strerror (tv[tidx].ec), gpg_strerror (err));
151 else if (!err && tv[tidx].val != value)
153 fprintf (stderr, PGM ":%s:tidx=%d: wrong value returned (2); "
154 "expected(%llx) got(%llx)\n",
155 __func__, tidx, tv[tidx].val, value);
159 s = find_tuple (tuples2, tv[tidx].tag, &n);
162 else if (tv[tidx].len != n)
164 fprintf (stderr, PGM ":%s:tidx=%d: wrong string length returned; "
165 "expected(%d) got(%zu)\n",
166 __func__, tidx, tv[tidx].len, n);
169 else if (memcmp (tv[tidx].data, s, n))
171 fprintf (stderr, PGM ":%s:tidx=%d: wrong string returned:",
173 for (i=0; i < n; i++)
174 fprintf (stderr, " %02x", ((unsigned char*)s)[i]);
175 fputc ('\n', stderr);
180 destroy_tupledesc (tuples);
181 destroy_tupledesc (tuples2);
187 main (int argc, char **argv)
194 while (argc && last_argc != argc )
197 if (!strcmp (*argv, "--"))
202 else if (!strcmp (*argv, "--verbose"))
207 else if (!strcmp (*argv, "--debug"))
213 else if (!strncmp (*argv, "--", 2))
215 fprintf (stderr, PGM ": unknown option '%s'\n", *argv);