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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2016-2025 Free Software Foundation, Inc.
GNU Mailutils 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, or (at your option)
any later version.
GNU Mailutils 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 GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#include <mailutils/mailutils.h>
union value
{
char *v_string;
signed short v_short;
unsigned short v_ushort;
int v_int;
unsigned v_uint;
long v_long;
unsigned long v_ulong;
size_t v_size;
time_t v_time;
struct mu_cidr v_cidr;
};
struct value_handler
{
void (*format) (union value *, FILE *);
int (*compare) (union value *, union value *);
};
static void
v_string_format (union value *val, FILE *fp)
{
char *p;
fputc ('"', fp);
for (p = val->v_string; *p; p++)
{
if (*p == '\\' || *p == '"')
fputc ('\\', fp);
fputc (*p, fp);
}
fputc ('"', fp);
}
static int
v_string_compare (union value *a, union value *b)
{
return strcmp (a->v_string, b->v_string);
}
static void
v_short_format (union value *val, FILE *fp)
{
fprintf (fp, "%hd", val->v_short);
}
static int
v_short_compare (union value *a, union value *b)
{
return a->v_short != b->v_short;
}
static void
v_ushort_format (union value *val, FILE *fp)
{
fprintf (fp, "%hu", val->v_short);
}
static int
v_ushort_compare (union value *a, union value *b)
{
return a->v_ushort != b->v_ushort;
}
static void
v_int_format (union value *val, FILE *fp)
{
fprintf (fp, "%d", val->v_int);
}
static int
v_int_compare (union value *a, union value *b)
{
return a->v_int != b->v_int;
}
static void
v_uint_format (union value *val, FILE *fp)
{
fprintf (fp, "%u", val->v_uint);
}
static int
v_uint_compare (union value *a, union value *b)
{
return a->v_uint != b->v_uint;
}
static void
v_long_format (union value *val, FILE *fp)
{
fprintf (fp, "%ld", val->v_long);
}
static int
v_long_compare (union value *a, union value *b)
{
return a->v_long != b->v_long;
}
static void
v_ulong_format (union value *val, FILE *fp)
{
fprintf (fp, "%lu", val->v_long);
}
static int
v_ulong_compare (union value *a, union value *b)
{
return a->v_ulong != b->v_ulong;
}
static void
v_size_format (union value *val, FILE *fp)
{
size_t v = val->v_size;
char buf[80];
size_t buflen = 80;
char *p = buf + buflen;
*--p = 0;
do
{
int n = v % 10;
*--p = n + '0';
v /= 10;
}
while (v && p > buf);
if (v)
fputs ("[overflow]", fp);
else
fputs (p, fp);
}
static int
v_size_compare (union value *a, union value *b)
{
return a->v_size != b->v_size;
}
static void
v_cidr_format (union value *val, FILE *fp)
{
char *buf;
int rc;
rc = mu_cidr_format (&val->v_cidr, 0, &buf);
if (rc)
{
fprintf (fp, "(can't convert value: %s)", mu_strerror (rc));
}
else
{
fprintf (fp, "%s", buf);
free (buf);
}
}
static int
v_cidr_compare (union value *a, union value *b)
{
if (a->v_cidr.family != b->v_cidr.family)
return 1;
if (a->v_cidr.len != b->v_cidr.len)
return 1;
if (memcmp (a->v_cidr.address, b->v_cidr.address, a->v_cidr.len))
return 1;
if (memcmp (a->v_cidr.netmask, b->v_cidr.netmask, a->v_cidr.len))
return 1;
return 0;
}
struct value_handler value_handler[] = {
[mu_c_string] = { v_string_format, v_string_compare },
[mu_c_short] = { v_short_format, v_short_compare },
[mu_c_ushort] = { v_ushort_format, v_ushort_compare },
[mu_c_int] = { v_int_format, v_int_compare },
[mu_c_uint] = { v_uint_format, v_uint_compare },
[mu_c_long] = { v_long_format, v_long_compare },
[mu_c_ulong] = { v_ulong_format, v_ulong_compare },
[mu_c_size] = { v_size_format, v_size_compare },
[mu_c_hsize] = { v_size_format, v_size_compare },
#if 0
mu_c_time,
#endif
[mu_c_bool] = { v_int_format, v_int_compare },
[mu_c_cidr] = { v_cidr_format, v_cidr_compare },
[mu_c_incr] = { v_int_format, v_int_compare },
};
int
valcmp (mu_c_type_t type, union value *a, union value *b)
{
if ((size_t)type < sizeof (value_handler) / sizeof (value_handler[0])
&& value_handler[type].compare)
return value_handler[type].compare (a, b);
else
{
fprintf (stderr, "unsupported value type: %d\n", type);
abort ();
}
}
void
valprint (FILE *fp, mu_c_type_t type, union value *val)
{
if ((size_t)type < sizeof (value_handler) / sizeof (value_handler[0])
&& value_handler[type].format)
value_handler[type].format (val, fp);
else
{
fprintf (stderr, "unsupported value type: %d\n", type);
abort ();
}
}
struct testdata
{
mu_c_type_t type;
char const *input;
int err;
union value val;
};
struct testdata tests[] = {
{ mu_c_string, "now is the time", 0, { .v_string = "now is the time" } },
{ mu_c_short, "115", 0, { .v_short = 115 } },
{ mu_c_short, "-400", 0, { .v_short = -400 } },
{ mu_c_short, "1a", EINVAL, },
{ mu_c_ushort, "110", 0, { .v_ushort = 110 } },
{ mu_c_ushort, "-110", ERANGE },
{ mu_c_int, "10568", 0, { .v_int = 10568 } },
{ mu_c_int, "-10568", 0, { .v_int = -10568 } },
{ mu_c_uint, "10568", 0, { .v_uint = 10568 } },
{ mu_c_long, "10568", 0, { .v_long = 10568 } },
{ mu_c_long, "-10568", 0, { .v_long = -10568 } },
{ mu_c_ulong, "10568", 0, { .v_ulong = 10568 } },
{ mu_c_size, "10568", 0, { .v_size = 10568 } },
{ mu_c_hsize, "0", 0, { .v_size = 0 } },
{ mu_c_hsize, "10", 0, { .v_size = 10 } },
{ mu_c_hsize, "10K", 0, { .v_size = 10240 } },
{ mu_c_hsize, " 10 M ", 0, { .v_size = 10485760 } },
{ mu_c_hsize, "10s", MU_ERR_PARSE },
{ mu_c_hsize, "-1", MU_ERR_PARSE },
{ mu_c_hsize, "", MU_ERR_PARSE },
{ mu_c_hsize, " ", MU_ERR_PARSE },
{ mu_c_hsize, " 1 M b ", MU_ERR_PARSE },
{ mu_c_bool, "true", 0, { .v_int = 1 } },
{ mu_c_bool, "false", 0, { .v_int = 0 } },
{ mu_c_cidr, "127.0.0.0/8", 0, { .v_cidr = {
.family = 2,
.len = 4,
.address = { 127, 0, 0, 0 },
.netmask = { 255 } } } },
{ mu_c_cidr, "127.10.4.15/8", 0, { .v_cidr = {
.family = 2,
.len = 4,
.address = { 127, 0, 0, 0 },
.netmask = { 255 } } } },
#ifdef MAILUTILS_IPV6
{ mu_c_cidr, "fe80::4a5b:39ff:fe09:97f0/64", 0, { .v_cidr = {
.family = 10,
.len = 16,
.address = { 0xfe, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
0x4a, 0x5b, 0x39, 0xff, 0xfe, 0x9, 0x97, 0xf0 },
.netmask = { 255, 255, 255, 255, 255, 255, 255, 255,
0, 0, 0, 0, 0, 0, 0, 0 } } } },
#endif
{ mu_c_incr, NULL, 0, { .v_int = 1 } },
{ mu_c_void }
};
void
print_test_id (int i, FILE *fp)
{
fprintf (fp, "%d: %s:%s: ", i, mu_c_type_str[tests[i].type],
tests[i].input ? tests[i].input : "NULL");
}
void
usage (int code, FILE *fp, char const *argv0)
{
fprintf (fp, "usage: %s [-v]\n", argv0);
exit (code);
}
int
main (int argc, char **argv)
{
union value val;
char *errmsg;
int i;
unsigned failures = 0;
int verbose = 0;
for (i = 1; i < argc; i++) {
if (strcmp (argv[i], "-v") == 0)
verbose = 1;
else if (strcmp (argv[i], "-h") == 0)
usage (0, stdout, argv[0]);
else
usage (1, stderr, argv[0]);
}
if (i != argc)
usage (1, stderr, argv[0]);
for (i = 0; tests[i].type != mu_c_void; i++)
{
int rc;
memset (&val, 0, sizeof (val));
rc = mu_str_to_c (tests[i].input, tests[i].type, &val, &errmsg);
if (rc)
{
if (tests[i].err == rc)
{
if (verbose)
{
print_test_id (i, stdout);
fprintf (stdout, "XFAIL\n");
}
}
else
{
print_test_id (i, stderr);
fprintf (stderr, "FAIL: error %s", mu_strerror (rc));
if (errmsg)
fprintf (stderr, ": %s", errmsg);
fputc ('\n', stderr);
free (errmsg);
++failures;
}
}
else if (tests[i].err)
{
print_test_id (i, stderr);
fprintf (stderr, "FAIL: unexpected success\n");
++failures;
}
else if (valcmp (tests[i].type, &tests[i].val, &val))
{
fprintf (stderr, "%d: FAIL: %s value differ: ",
i, mu_c_type_str[tests[i].type]);
fprintf (stderr, "expected: ");
valprint (stderr, tests[i].type, &tests[i].val);
fprintf (stderr, ", but got: ");
valprint (stderr, tests[i].type, &val);
fputc ('\n', stderr);
++failures;
}
else if (verbose)
{
print_test_id (i, stdout);
fprintf (stdout, "OK\n");
}
}
exit (!!failures);
}
|