| 1 |
/* t-name-value.c - Check name-value functions |
| 2 |
* Copyright (C) 2015, 2025 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of Libgpg-error. |
| 5 |
* |
| 6 |
* Libgpg-error is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2.1 of |
| 9 |
* the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* Libgpg-error is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public License |
| 17 |
* along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 18 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifdef HAVE_CONFIG_H |
| 22 |
#include <config.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#define PGM "t-name-value" |
| 30 |
#include "t-common.h" |
| 31 |
|
| 32 |
|
| 33 |
static void test_getting_values (gpgrt_nvc_t cont); |
| 34 |
static void test_getting_section_values (gpgrt_nvc_t pk); |
| 35 |
static void test_key_extraction (gpgrt_nvc_t pk); |
| 36 |
static void test_iteration (gpgrt_nvc_t pk); |
| 37 |
static void test_whitespace (gpgrt_nvc_t pk); |
| 38 |
|
| 39 |
static int private_key_mode; |
| 40 |
static int section_mode; |
| 41 |
|
| 42 |
static struct |
| 43 |
{ |
| 44 |
char *value; |
| 45 |
void (*test_func) (gpgrt_nvc_t); |
| 46 |
int only_section_mode; |
| 47 |
} tests[] = |
| 48 |
{ |
| 49 |
{ |
| 50 |
"# This is a comment followed by an empty line\n" |
| 51 |
"\n", |
| 52 |
NULL |
| 53 |
}, |
| 54 |
{ |
| 55 |
"# This is a comment followed by two empty lines, Windows style\r\n" |
| 56 |
"\r\n" |
| 57 |
"\r\n", |
| 58 |
NULL |
| 59 |
}, |
| 60 |
{ |
| 61 |
"# Some name,value pairs\n" |
| 62 |
"Comment: Some comment.\n" |
| 63 |
"SomeOtherName: Some value.\n", |
| 64 |
test_getting_values |
| 65 |
}, |
| 66 |
{ |
| 67 |
" # Whitespace is preserved as much as possible\r\n" |
| 68 |
"Comment:Some comment.\n" |
| 69 |
"SomeOtherName: Some value. \n", |
| 70 |
test_getting_values |
| 71 |
}, |
| 72 |
{ |
| 73 |
"# Values may be continued in the next line as indicated by leading\n" |
| 74 |
"# space\n" |
| 75 |
"Comment: Some rather long\n" |
| 76 |
" comment that is continued in the next line.\n" |
| 77 |
"\n" |
| 78 |
" Blank lines with or without whitespace are allowed within\n" |
| 79 |
" continuations to allow paragraphs.\n" |
| 80 |
"SomeOtherName: Some value.\n", |
| 81 |
test_getting_values |
| 82 |
}, |
| 83 |
{ |
| 84 |
"# Names may be given multiple times forming an array of values\n" |
| 85 |
"Comment: Some comment, element 0.\n" |
| 86 |
"Comment: Some comment, element 1.\n" |
| 87 |
"Comment: Some comment, element 2.\n" |
| 88 |
"SomeOtherName: Some value.\n", |
| 89 |
test_iteration |
| 90 |
}, |
| 91 |
{ |
| 92 |
"# One whitespace at the beginning of a continuation is swallowed.\n" |
| 93 |
"One: Without\n" |
| 94 |
" Whitespace\n" |
| 95 |
"Two: With\n" |
| 96 |
" Whitespace\n" |
| 97 |
"Three: Blank lines in continuations encode newlines.\n" |
| 98 |
"\n" |
| 99 |
" Next paragraph.\n", |
| 100 |
test_whitespace |
| 101 |
}, |
| 102 |
{ |
| 103 |
"Description: Key to sign all GnuPG released tarballs.\n" |
| 104 |
" The key is actually stored on a smart card.\n" |
| 105 |
"Use-for-ssh: yes\n" |
| 106 |
"OpenSSH-cert: long base64 encoded string wrapped so that this\n" |
| 107 |
" key file can be easily edited with a standard editor.\n" |
| 108 |
"Key: (shadowed-private-key\n" |
| 109 |
" (rsa\n" |
| 110 |
" (n #00AA1AD2A55FD8C8FDE9E1941772D9CC903FA43B268CB1B5A1BAFDC900\n" |
| 111 |
" 2961D8AEA153424DC851EF13B83AC64FBE365C59DC1BD3E83017C90D4365B4\n" |
| 112 |
" 83E02859FC13DB5842A00E969480DB96CE6F7D1C03600392B8E08EF0C01FC7\n" |
| 113 |
" 19F9F9086B25AD39B4F1C2A2DF3E2BE317110CFFF21D4A11455508FE407997\n" |
| 114 |
" 601260816C8422297C0637BB291C3A079B9CB38A92CE9E551F80AA0EBF4F0E\n" |
| 115 |
" 72C3F250461E4D31F23A7087857FC8438324A013634563D34EFDDCBF2EA80D\n" |
| 116 |
" F9662C9CCD4BEF2522D8BDFED24CEF78DC6B309317407EAC576D889F88ADA0\n" |
| 117 |
" 8C4FFB480981FB68C5C6CA27503381D41018E6CDC52AAAE46B166BDC10637A\n" |
| 118 |
" E186A02BA2497FDC5D1221#)\n" |
| 119 |
" (e #00010001#)\n" |
| 120 |
" (shadowed t1-v1\n" |
| 121 |
" (#D2760001240102000005000011730000# OPENPGP.1)\n" |
| 122 |
" )))\n", |
| 123 |
test_key_extraction |
| 124 |
}, |
| 125 |
{ |
| 126 |
"# This is a test for section mode\n" |
| 127 |
"[ HKLM\\Software\\Bla\\Foo ]# the section name\n" |
| 128 |
"Myname: Erika\n" |
| 129 |
"Surname: Musterfrau\n" |
| 130 |
"\n" |
| 131 |
"[\\Software\\blub]\n" |
| 132 |
"Myname: Otto Blub\n" |
| 133 |
"Surname: Ottoman\n" |
| 134 |
"[HKLM\\Software/Bla\\Foo ] # Add stuff to the first section\n" |
| 135 |
"Url: https://example.org\n" |
| 136 |
"Url: http://example.de\n" |
| 137 |
"Surname: Hacker\n" |
| 138 |
"# last comment\n", |
| 139 |
test_getting_section_values, 1 |
| 140 |
} |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
static void |
| 146 |
test_getting_values (gpgrt_nvc_t pk) |
| 147 |
{ |
| 148 |
gpgrt_nve_t e; |
| 149 |
|
| 150 |
e = gpgrt_nvc_lookup (pk, "Comment:"); |
| 151 |
gpgrt_assert (e); |
| 152 |
|
| 153 |
/* Names are case-insensitive. */ |
| 154 |
e = gpgrt_nvc_lookup (pk, "comment:"); |
| 155 |
gpgrt_assert (e); |
| 156 |
e = gpgrt_nvc_lookup (pk, "COMMENT:"); |
| 157 |
gpgrt_assert (e); |
| 158 |
|
| 159 |
e = gpgrt_nvc_lookup (pk, "SomeOtherName:"); |
| 160 |
gpgrt_assert (e); |
| 161 |
|
| 162 |
/* The colon is optional. */ |
| 163 |
e = gpgrt_nvc_lookup (pk, "comment"); |
| 164 |
gpgrt_assert (e); |
| 165 |
e = gpgrt_nvc_lookup (pk, "COMMENT"); |
| 166 |
gpgrt_assert (e); |
| 167 |
|
| 168 |
e = gpgrt_nvc_lookup (pk, "SomeOtherName"); |
| 169 |
gpgrt_assert (e); |
| 170 |
|
| 171 |
/* And check one which does not exist. */ |
| 172 |
e = gpgrt_nvc_lookup (pk, "SomeOtherNam"); |
| 173 |
gpgrt_assert (!e); |
| 174 |
e = gpgrt_nvc_lookup (pk, "SomeOtherNam:"); |
| 175 |
gpgrt_assert (!e); |
| 176 |
e = gpgrt_nvc_lookup (pk, "SomeOtherNam :"); |
| 177 |
gpgrt_assert (!e); |
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
static void |
| 183 |
test_getting_section_values (gpgrt_nvc_t pk) |
| 184 |
{ |
| 185 |
gpgrt_nve_t e; |
| 186 |
const char *value; |
| 187 |
|
| 188 |
enter_test_function (); |
| 189 |
|
| 190 |
e = gpgrt_nvc_lookup (pk, "MyName"); |
| 191 |
if (e) |
| 192 |
fail ("nvc_lookup unexpected succeeded at line %d\n", __LINE__); |
| 193 |
|
| 194 |
e = gpgrt_nvc_lookup (pk, "HKLM/Software/Bla/Foo:MyName"); |
| 195 |
if (!e) |
| 196 |
fail ("nvc_lookup failed at line %d\n", __LINE__); |
| 197 |
else |
| 198 |
{ |
| 199 |
value = gpgrt_nve_value (e); |
| 200 |
show ("value for %s is ->%s<-\n", gpgrt_nve_name (e), value); |
| 201 |
} |
| 202 |
|
| 203 |
e = gpgrt_nvc_lookup (pk, "HKLM/Software/Bla/Foo:SurName"); |
| 204 |
if (!e) |
| 205 |
fail ("nvc_lookup failed at line %d\n", __LINE__); |
| 206 |
else |
| 207 |
{ |
| 208 |
value = gpgrt_nve_value (e); |
| 209 |
show ("value for %s is ->%s<-\n", gpgrt_nve_name (e), value); |
| 210 |
} |
| 211 |
e = gpgrt_nve_next (e, "HKLM/Software/Bla/Foo:SurName"); |
| 212 |
if (!e) |
| 213 |
fail ("nvc_next failed at line %d\n", __LINE__); |
| 214 |
else |
| 215 |
{ |
| 216 |
value = gpgrt_nve_value (e); |
| 217 |
show ("value for %s is ->%s<-\n", gpgrt_nve_name (e), value); |
| 218 |
} |
| 219 |
e = gpgrt_nve_next (e, "HKLM/Software/Bla/Foo:surname"); |
| 220 |
if (e) |
| 221 |
fail ("nve_next unexpected succeeded at line %d\n", __LINE__); |
| 222 |
|
| 223 |
e = gpgrt_nvc_lookup (pk, "HKLM/Software/Bla/Foo:Url"); |
| 224 |
if (!e) |
| 225 |
fail ("nvc_lookup failed at line %d\n", __LINE__); |
| 226 |
else |
| 227 |
{ |
| 228 |
value = gpgrt_nve_value (e); |
| 229 |
show ("value for %s is ->%s<-\n", gpgrt_nve_name (e), value); |
| 230 |
} |
| 231 |
e = gpgrt_nve_next (e, "HKLM/Software/Bla/Foo:Url"); |
| 232 |
if (!e) |
| 233 |
fail ("nve_next failed at line %d\n", __LINE__); |
| 234 |
else |
| 235 |
{ |
| 236 |
value = gpgrt_nve_value (e); |
| 237 |
show ("value for %s is ->%s<-\n", gpgrt_nve_name (e), value); |
| 238 |
} |
| 239 |
e = gpgrt_nve_next (e, "HKLM/Software/Bla/Foo:Url"); |
| 240 |
if (e) |
| 241 |
fail ("nve_next unexpected succeeded at line %d\n", __LINE__); |
| 242 |
|
| 243 |
leave_test_function (); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
static void |
| 248 |
test_key_extraction (gpgrt_nvc_t pk) |
| 249 |
{ |
| 250 |
const char *key; |
| 251 |
|
| 252 |
/* Note that the original test in gnupg used the gnupg specific |
| 253 |
* private key extraction function which works only in |
| 254 |
* private_key_mode. We can't do this here so we do a mere |
| 255 |
* extraction test. */ |
| 256 |
key = gpgrt_nve_value (gpgrt_nvc_lookup (pk, "Key:")); |
| 257 |
gpgrt_assert (key); |
| 258 |
|
| 259 |
if (verbose) |
| 260 |
fprintf (stdout, "->%s<-\n", key); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
static void |
| 265 |
test_iteration (gpgrt_nvc_t pk) |
| 266 |
{ |
| 267 |
int i; |
| 268 |
gpgrt_nve_t e; |
| 269 |
|
| 270 |
i = 0; |
| 271 |
for (e = gpgrt_nvc_lookup (pk, NULL); e; e = gpgrt_nve_next (e, NULL)) |
| 272 |
i++; |
| 273 |
gpgrt_assert (i == 4); |
| 274 |
|
| 275 |
i = 0; |
| 276 |
for (e = gpgrt_nvc_lookup (pk, "Comment:"); |
| 277 |
e; |
| 278 |
e = gpgrt_nve_next (e, "Comment:")) |
| 279 |
i++; |
| 280 |
gpgrt_assert (i == 3); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
static void |
| 285 |
test_whitespace (gpgrt_nvc_t pk) |
| 286 |
{ |
| 287 |
gpgrt_nve_t e; |
| 288 |
|
| 289 |
e = gpgrt_nvc_lookup (pk, "One:"); |
| 290 |
gpgrt_assert (e); |
| 291 |
gpgrt_assert (strcmp (gpgrt_nve_value (e), "WithoutWhitespace") == 0); |
| 292 |
|
| 293 |
e = gpgrt_nvc_lookup (pk, "Two:"); |
| 294 |
gpgrt_assert (e); |
| 295 |
gpgrt_assert (strcmp (gpgrt_nve_value (e), "With Whitespace") == 0); |
| 296 |
|
| 297 |
e = gpgrt_nvc_lookup (pk, "Three:"); |
| 298 |
gpgrt_assert (e); |
| 299 |
gpgrt_assert (strcmp (gpgrt_nve_value (e), |
| 300 |
"Blank lines in continuations encode newlines.\n" |
| 301 |
"Next paragraph.") == 0); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
/* Convert container PK to a string. Terminates on error. */ |
| 306 |
static char * |
| 307 |
nvc_to_string (gpgrt_nvc_t pk) |
| 308 |
{ |
| 309 |
gpg_error_t err; |
| 310 |
char *buf; |
| 311 |
size_t len; |
| 312 |
estream_t sink; |
| 313 |
|
| 314 |
sink = es_fopenmem (0, "rw"); |
| 315 |
gpgrt_assert (sink); |
| 316 |
|
| 317 |
err = gpgrt_nvc_write (pk, sink); |
| 318 |
gpgrt_assert (err == 0); |
| 319 |
|
| 320 |
len = es_ftell (sink); |
| 321 |
buf = xmalloc (len+1); |
| 322 |
gpgrt_assert (buf); |
| 323 |
|
| 324 |
es_fseek (sink, 0, SEEK_SET); |
| 325 |
es_read (sink, buf, len, NULL); |
| 326 |
buf[len] = 0; |
| 327 |
|
| 328 |
es_fclose (sink); |
| 329 |
return buf; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
static void dummy_free (void *p) { (void) p; } |
| 334 |
static void *dummy_realloc (void *p, size_t s) { (void) s; return p; } |
| 335 |
|
| 336 |
/* |
| 337 |
* Run the standard test cases. |
| 338 |
*/ |
| 339 |
static void |
| 340 |
run_tests (void) |
| 341 |
{ |
| 342 |
gpg_error_t err; |
| 343 |
gpgrt_nvc_t pk; |
| 344 |
int i; |
| 345 |
int errlno; |
| 346 |
|
| 347 |
for (i = 0; i < DIM (tests); i++) |
| 348 |
{ |
| 349 |
estream_t source; |
| 350 |
char *buf; |
| 351 |
size_t len; |
| 352 |
|
| 353 |
if (tests[i].only_section_mode && !section_mode) |
| 354 |
continue; |
| 355 |
|
| 356 |
len = strlen (tests[i].value); |
| 357 |
source = es_mopen (tests[i].value, len, len, |
| 358 |
0, dummy_realloc, dummy_free, "r"); |
| 359 |
gpgrt_assert (source); |
| 360 |
|
| 361 |
if (private_key_mode) |
| 362 |
err = gpgrt_nvc_parse (&pk, &errlno, source, GPGRT_NVC_PRIVKEY); |
| 363 |
else if (section_mode) |
| 364 |
err = gpgrt_nvc_parse (&pk, &errlno, source, GPGRT_NVC_SECTION); |
| 365 |
else |
| 366 |
err = gpgrt_nvc_parse (&pk, &errlno, source, 0); |
| 367 |
if (err) |
| 368 |
show ("parser failed at input line %d: %s\n", |
| 369 |
errlno, gpg_strerror (err)); |
| 370 |
gpgrt_assert (err == 0); |
| 371 |
gpgrt_assert (pk); |
| 372 |
|
| 373 |
if (section_mode) |
| 374 |
buf = NULL; /* nvc_to_string does not yet work. */ |
| 375 |
else |
| 376 |
{ |
| 377 |
if (verbose) |
| 378 |
{ |
| 379 |
err = gpgrt_nvc_write (pk, es_stderr); |
| 380 |
gpgrt_assert (err == 0); |
| 381 |
} |
| 382 |
buf = nvc_to_string (pk); |
| 383 |
gpgrt_assert (memcmp (tests[i].value, buf, len) == 0); |
| 384 |
} |
| 385 |
|
| 386 |
es_fclose (source); |
| 387 |
xfree (buf); |
| 388 |
|
| 389 |
if (tests[i].test_func) |
| 390 |
tests[i].test_func (pk); |
| 391 |
|
| 392 |
gpgrt_nvc_release (pk); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
/* |
| 398 |
* Run tests checking the modification functions. |
| 399 |
*/ |
| 400 |
static void |
| 401 |
run_modification_tests (void) |
| 402 |
{ |
| 403 |
gpg_error_t err; |
| 404 |
gpgrt_nvc_t pk; |
| 405 |
gpgrt_nve_t e; |
| 406 |
char *buf; |
| 407 |
|
| 408 |
enter_test_function (); |
| 409 |
|
| 410 |
pk = gpgrt_nvc_new (private_key_mode? GPGRT_NVC_PRIVKEY : 0); |
| 411 |
gpgrt_assert (pk); |
| 412 |
|
| 413 |
err = gpgrt_nvc_set (pk, "Foo:", "Bar"); |
| 414 |
gpgrt_assert (!err); |
| 415 |
buf = nvc_to_string (pk); |
| 416 |
gpgrt_assert (strcmp (buf, "Foo: Bar\n") == 0); |
| 417 |
xfree (buf); |
| 418 |
|
| 419 |
err = gpgrt_nvc_set (pk, "Foo:", "Baz"); |
| 420 |
if (err) |
| 421 |
fail ("gpgrt_nvc_set failed at line %d: %s\n",__LINE__,gpg_strerror (err)); |
| 422 |
buf = nvc_to_string (pk); |
| 423 |
gpgrt_assert (strcmp (buf, "Foo: Baz\n") == 0); |
| 424 |
xfree (buf); |
| 425 |
|
| 426 |
err = gpgrt_nvc_set (pk, "Bar:", "Bazzel"); |
| 427 |
if (err) |
| 428 |
fail ("gpgrt_nvc_set failed at line %d: %s\n",__LINE__,gpg_strerror (err)); |
| 429 |
buf = nvc_to_string (pk); |
| 430 |
gpgrt_assert (strcmp (buf, "Foo: Baz\nBar: Bazzel\n") == 0); |
| 431 |
xfree (buf); |
| 432 |
|
| 433 |
err = gpgrt_nvc_add (pk, "Foo:", "Bar"); |
| 434 |
if (err) |
| 435 |
fail ("gpgrt_nvc_add failed at line %d: %s\n",__LINE__,gpg_strerror (err)); |
| 436 |
buf = nvc_to_string (pk); |
| 437 |
gpgrt_assert (strcmp (buf, "Foo: Baz\nFoo: Bar\nBar: Bazzel\n") == 0); |
| 438 |
xfree (buf); |
| 439 |
|
| 440 |
err = gpgrt_nvc_add (pk, "DontExistYet:", "Bar"); |
| 441 |
if (err) |
| 442 |
fail ("gpgrt_nvc_set failed at line %d: %s\n",__LINE__,gpg_strerror (err)); |
| 443 |
buf = nvc_to_string (pk); |
| 444 |
gpgrt_assert (strcmp (buf, ("Foo: Baz\nFoo: Bar\n" |
| 445 |
"Bar: Bazzel\nDontExistYet: Bar\n")) == 0); |
| 446 |
xfree (buf); |
| 447 |
|
| 448 |
gpgrt_nvc_delete (pk, gpgrt_nvc_lookup (pk, "DontExistYet:"), NULL); |
| 449 |
buf = nvc_to_string (pk); |
| 450 |
gpgrt_assert (strcmp (buf, "Foo: Baz\nFoo: Bar\nBar: Bazzel\n") == 0); |
| 451 |
xfree (buf); |
| 452 |
|
| 453 |
gpgrt_nvc_delete (pk, gpgrt_nve_next (gpgrt_nvc_lookup (pk, "Foo:"), |
| 454 |
"Foo:"), NULL); |
| 455 |
buf = nvc_to_string (pk); |
| 456 |
gpgrt_assert (strcmp (buf, "Foo: Baz\nBar: Bazzel\n") == 0); |
| 457 |
xfree (buf); |
| 458 |
|
| 459 |
gpgrt_nvc_delete (pk, gpgrt_nvc_lookup (pk, "Foo:"), NULL); |
| 460 |
buf = nvc_to_string (pk); |
| 461 |
gpgrt_assert (strcmp (buf, "Bar: Bazzel\n") == 0); |
| 462 |
xfree (buf); |
| 463 |
|
| 464 |
gpgrt_nvc_delete (pk, gpgrt_nvc_lookup (pk, NULL), NULL); |
| 465 |
buf = nvc_to_string (pk); |
| 466 |
gpgrt_assert (strcmp (buf, "") == 0); |
| 467 |
xfree (buf); |
| 468 |
|
| 469 |
/* Test whether we can delete an entry by name. */ |
| 470 |
err = gpgrt_nvc_add (pk, "Key:", "(3:foo)"); |
| 471 |
if (err) |
| 472 |
fail ("gpgrt_nvc_add failed at line %d: %s\n",__LINE__,gpg_strerror (err)); |
| 473 |
e = gpgrt_nvc_lookup (pk, "Key:"); |
| 474 |
gpgrt_assert (e); |
| 475 |
gpgrt_nvc_delete (pk, NULL, "Kez:"); /* Delete an nonexistent name. */ |
| 476 |
e = gpgrt_nvc_lookup (pk, "Key:"); |
| 477 |
gpgrt_assert (e); |
| 478 |
gpgrt_nvc_delete (pk, NULL, "Key:"); |
| 479 |
e = gpgrt_nvc_lookup (pk, "Key:"); |
| 480 |
gpgrt_assert (!e); |
| 481 |
|
| 482 |
/* Ditto but now whether it deletes all entries with that name. We |
| 483 |
* don't use "Key" because that name is special in private key mode. */ |
| 484 |
err = gpgrt_nvc_add (pk, "AKey:", "A-value"); |
| 485 |
if (err) |
| 486 |
fail ("gpgrt_nvc_add failed at line %d: %s\n",__LINE__,gpg_strerror (err)); |
| 487 |
|
| 488 |
err = gpgrt_nvc_add (pk, "AKey:", "B-value"); |
| 489 |
if (err) |
| 490 |
fail ("gpgrt_nvc_add failed at line %d: %s\n",__LINE__,gpg_strerror (err)); |
| 491 |
|
| 492 |
e = gpgrt_nvc_lookup (pk, "AKey:"); |
| 493 |
gpgrt_assert (e); |
| 494 |
|
| 495 |
gpgrt_nvc_delete (pk, NULL, "AKey:"); |
| 496 |
e = gpgrt_nvc_lookup (pk, "AKey:"); |
| 497 |
gpgrt_assert (!e); |
| 498 |
|
| 499 |
err = gpgrt_nvc_set (pk, "Foo:", |
| 500 |
"A really long value spanning across multiple lines" |
| 501 |
" that has to be wrapped at a convenient space."); |
| 502 |
if (err) |
| 503 |
fail ("gpgrt_nvc_set failed at line %d: %s\n",__LINE__,gpg_strerror (err)); |
| 504 |
buf = nvc_to_string (pk); |
| 505 |
gpgrt_assert (strcmp (buf, "Foo: A really long value spanning across multiple" |
| 506 |
" lines that has to be\n wrapped at a convenient space.\n") |
| 507 |
== 0); |
| 508 |
xfree (buf); |
| 509 |
|
| 510 |
err = gpgrt_nvc_set (pk, "Foo:", |
| 511 |
"XA really long value spanning across multiple lines" |
| 512 |
" that has to be wrapped at a convenient space."); |
| 513 |
if (err) |
| 514 |
fail ("gpgrt_nvc_set failed at line %d: %s\n",__LINE__,gpg_strerror (err)); |
| 515 |
buf = nvc_to_string (pk); |
| 516 |
gpgrt_assert (strcmp (buf, |
| 517 |
"Foo: XA really long value spanning across multiple" |
| 518 |
" lines that has to\n be wrapped at a convenient space.\n") |
| 519 |
== 0); |
| 520 |
xfree (buf); |
| 521 |
|
| 522 |
err = gpgrt_nvc_set (pk, "Foo:", |
| 523 |
"XXXXA really long value spanning across multiple lines" |
| 524 |
" that has to be wrapped at a convenient space."); |
| 525 |
if (err) |
| 526 |
fail ("gpgrt_nvc_set failed at line %d: %s\n",__LINE__,gpg_strerror (err)); |
| 527 |
buf = nvc_to_string (pk); |
| 528 |
gpgrt_assert (strcmp (buf, |
| 529 |
"Foo: XXXXA really long value spanning across multiple" |
| 530 |
" lines that has\n to be wrapped at a convenient space.\n") |
| 531 |
== 0); |
| 532 |
xfree (buf); |
| 533 |
|
| 534 |
err = gpgrt_nvc_set (pk, "Foo:", |
| 535 |
"Areallylongvaluespanningacrossmultiplelines" |
| 536 |
"thathastobewrappedataconvenientspacethatisnotthere."); |
| 537 |
if (err) |
| 538 |
fail ("gpgrt_nvc_set failed at line %d: %s\n",__LINE__,gpg_strerror (err)); |
| 539 |
buf = nvc_to_string (pk); |
| 540 |
gpgrt_assert (strcmp (buf, |
| 541 |
"Foo: Areallylongvaluespanningacrossmultiplelinesthat" |
| 542 |
"hastobewrappedataco\n nvenientspacethatisnotthere.\n") |
| 543 |
== 0); |
| 544 |
xfree (buf); |
| 545 |
gpgrt_nvc_release (pk); |
| 546 |
|
| 547 |
pk = gpgrt_nvc_new (private_key_mode? GPGRT_NVC_PRIVKEY : 0); |
| 548 |
gpgrt_assert (pk); |
| 549 |
|
| 550 |
err = gpgrt_nvc_set (pk, "Key:", "(hello world)"); |
| 551 |
gpgrt_assert (err == 0); |
| 552 |
buf = nvc_to_string (pk); |
| 553 |
gpgrt_assert (strcmp (buf, "Key: (hello world)\n") == 0); |
| 554 |
xfree (buf); |
| 555 |
gpgrt_nvc_release (pk); |
| 556 |
|
| 557 |
leave_test_function (); |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
static void |
| 563 |
parse (const char *fname) |
| 564 |
{ |
| 565 |
gpg_error_t err; |
| 566 |
estream_t source; |
| 567 |
char *buf; |
| 568 |
gpgrt_nvc_t pk_a, pk_b; |
| 569 |
gpgrt_nve_t e; |
| 570 |
int line; |
| 571 |
|
| 572 |
source = es_fopen (fname, "rb"); |
| 573 |
if (source == NULL) |
| 574 |
{ |
| 575 |
perror (fname); |
| 576 |
exit (1); |
| 577 |
} |
| 578 |
|
| 579 |
if (private_key_mode) |
| 580 |
err = gpgrt_nvc_parse (&pk_a, &line, source, GPGRT_NVC_PRIVKEY); |
| 581 |
else |
| 582 |
err = gpgrt_nvc_parse (&pk_a, &line, source, 0); |
| 583 |
if (err) |
| 584 |
{ |
| 585 |
fail ("failed to parse %s line %d: %s\n", |
| 586 |
fname, line, gpg_strerror (err)); |
| 587 |
exit (1); |
| 588 |
} |
| 589 |
|
| 590 |
buf = nvc_to_string (pk_a); |
| 591 |
xfree (buf); |
| 592 |
|
| 593 |
pk_b = gpgrt_nvc_new (private_key_mode? GPGRT_NVC_PRIVKEY : 0); |
| 594 |
gpgrt_assert (pk_b); |
| 595 |
|
| 596 |
for (e = gpgrt_nvc_lookup (pk_a, NULL); e; e = gpgrt_nve_next (e, NULL)) |
| 597 |
{ |
| 598 |
const char *key = NULL; |
| 599 |
|
| 600 |
if (private_key_mode && !strcasecmp (gpgrt_nve_name (e), "Key:")) |
| 601 |
{ |
| 602 |
key = gpgrt_nve_value (e); |
| 603 |
} |
| 604 |
|
| 605 |
if (key) |
| 606 |
{ |
| 607 |
err = gpgrt_nve_set (pk_a, e, key); |
| 608 |
gpgrt_assert (err == 0); |
| 609 |
} |
| 610 |
else |
| 611 |
{ |
| 612 |
err = gpgrt_nvc_add (pk_b, gpgrt_nve_name (e), gpgrt_nve_value (e)); |
| 613 |
gpgrt_assert (err == 0); |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
buf = nvc_to_string (pk_b); |
| 618 |
if (verbose) |
| 619 |
fprintf (stdout, "%s", buf); |
| 620 |
xfree (buf); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
static const char * |
| 626 |
my_strusage (int level) |
| 627 |
{ |
| 628 |
const char *p; |
| 629 |
|
| 630 |
switch (level) |
| 631 |
{ |
| 632 |
case 9: p = "LGPL-2.1-or-later"; break; |
| 633 |
case 11: p = PGM; break; |
| 634 |
case 12: p = PACKAGE_NAME; break; |
| 635 |
case 13: p = PACKAGE_VERSION; break; |
| 636 |
case 14: p = GPGRT_STD_COPYRIGHT_LINE; break; |
| 637 |
case 19: p = "Please report bugs to <https://bugs.gnupg.org>.\n"; break; |
| 638 |
default: p = NULL; |
| 639 |
} |
| 640 |
return p; |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
int |
| 645 |
main (int argc, char **argv) |
| 646 |
{ |
| 647 |
enum { CMD_TEST = 500, CMD_PARSE } command = CMD_TEST; |
| 648 |
gpgrt_opt_t opts[] = { |
| 649 |
ARGPARSE_x ('v', "verbose", NONE, 0, "Print more diagnostics"), |
| 650 |
ARGPARSE_s_n('d', "debug", "Flyswatter"), |
| 651 |
ARGPARSE_c (CMD_TEST, "test", "Test mode (default)"), |
| 652 |
ARGPARSE_c (CMD_PARSE, "parse", "Parse a file"), |
| 653 |
ARGPARSE_end() |
| 654 |
}; |
| 655 |
gpgrt_argparse_t pargs = { &argc, &argv, 0 }; |
| 656 |
|
| 657 |
gpgrt_set_strusage (my_strusage); |
| 658 |
gpgrt_log_set_prefix (gpgrt_strusage (11), GPGRT_LOG_WITH_PREFIX); |
| 659 |
|
| 660 |
while (gpgrt_argparse (NULL, &pargs, opts)) |
| 661 |
{ |
| 662 |
switch (pargs.r_opt) |
| 663 |
{ |
| 664 |
case 'v': verbose++; break; |
| 665 |
case 'd': debug++; break; |
| 666 |
|
| 667 |
case CMD_TEST: |
| 668 |
case CMD_PARSE: |
| 669 |
command = pargs.r_opt; |
| 670 |
break; |
| 671 |
|
| 672 |
default : pargs.err = ARGPARSE_PRINT_ERROR; break; |
| 673 |
} |
| 674 |
} |
| 675 |
gpgrt_argparse (NULL, &pargs, NULL); |
| 676 |
|
| 677 |
if (command == CMD_TEST) |
| 678 |
{ |
| 679 |
show ("testing name-value functions\n"); |
| 680 |
|
| 681 |
run_tests (); |
| 682 |
run_modification_tests (); |
| 683 |
|
| 684 |
show ("again in private key mode\n"); |
| 685 |
/* Now again in rivate key mode */ |
| 686 |
private_key_mode = 1; |
| 687 |
run_tests (); |
| 688 |
run_modification_tests (); |
| 689 |
|
| 690 |
show ("again in section mode\n"); |
| 691 |
private_key_mode = 0; |
| 692 |
section_mode = 1; |
| 693 |
run_tests (); |
| 694 |
|
| 695 |
show ("testing name-value functions finished\n"); |
| 696 |
} |
| 697 |
else if (command == CMD_PARSE) |
| 698 |
{ |
| 699 |
if (argc != 1) |
| 700 |
gpgrt_usage (1); |
| 701 |
parse (*argv); |
| 702 |
} |
| 703 |
|
| 704 |
return !!errorcount; |
| 705 |
} |