Line data Source code
1 : /* t-b64dec.c - b64dec test.
2 : Copyright (C) 2017 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
17 : License along with libgpgme-error; if not, write to the Free
18 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 : 02110-1301, USA. */
20 :
21 : #if HAVE_CONFIG_H
22 : #include <config.h>
23 : #endif
24 :
25 : #include <stdio.h>
26 : #include <string.h>
27 : #if HAVE_STDLIB_H
28 : #include <stdlib.h>
29 : #endif
30 :
31 : #include <gpg-error.h>
32 :
33 : static const char *test_b64_string = "bGliZ3BnLWVycm9yIGlzIGZyZWUgc29"
34 : "mdHdhcmU7IHlvdSBjYW4gcmVkaXN0cmlidXRlIGl0IGFuZC9vciBtb2RpZnkgaXQgd"
35 : "W5kZXIgdGhlIHRlcm1zIG9mIHRoZSBHTlUgTGVzc2VyIEdlbmVyYWwgUHVibGljIEx"
36 : "pY2Vuc2UgYXMgcHVibGlzaGVkIGJ5IHRoZSBGcmVlIFNvZnR3YXJlIEZvdW5kYXRpb"
37 : "247IGVpdGhlciB2ZXJzaW9uIDIuMSBvZiB0aGUgTGljZW5zZSwgb3IgKGF0IHlvdXI"
38 : "gb3B0aW9uKSBhbnkgbGF0ZXIgdmVyc2lvbi4=";
39 :
40 : static const char *test_string = "libgpg-error is free software; "
41 : "you can redistribute it and/or modify it under the terms of "
42 : "the GNU Lesser General Public License as published by the Free "
43 : "Software Foundation; either version 2.1 of the License, or "
44 : "(at your option) any later version.";
45 :
46 : #define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
47 : __FILE__,__LINE__, (a)); \
48 : errcount++; \
49 : } while(0)
50 :
51 : static int errcount;
52 :
53 : static gpg_error_t
54 1 : test_b64dec_string (const char *string, const char *expected)
55 : {
56 : gpg_error_t err;
57 : gpgrt_b64state_t state;
58 : char *buffer;
59 : size_t len;
60 :
61 1 : len = strlen (string);
62 1 : buffer = malloc (strlen (string) + 1);
63 1 : if (!buffer)
64 : {
65 : err = gpg_error_from_syserror ();
66 : return err;
67 : }
68 :
69 1 : state = gpgrt_b64dec_start ("");
70 1 : if (!state)
71 : {
72 : err = gpg_error_from_syserror ();
73 0 : free (buffer);
74 0 : return err;
75 : }
76 :
77 1 : err = gpgrt_b64dec_proc (state, buffer, len, &len);
78 1 : if (err)
79 : {
80 0 : if (gpg_err_code (err) != GPG_ERR_EOF)
81 : {
82 0 : free (buffer);
83 0 : free (state);
84 0 : return err;
85 : }
86 : }
87 :
88 1 : err = gpgrt_b64dec_finish (state);
89 1 : if (err)
90 : {
91 0 : free (buffer);
92 0 : return err;
93 : }
94 :
95 1 : if (strncmp (buffer, expected, len) == 0)
96 : err = 0;
97 : else
98 : err = GPG_ERR_INTERNAL;
99 :
100 1 : free (buffer);
101 1 : return err;
102 : }
103 :
104 :
105 :
106 : int
107 1 : main (int argc, char **argv)
108 : {
109 : gpg_error_t err;
110 :
111 : (void)argc;
112 : (void)argv;
113 :
114 1 : err = test_b64dec_string (test_b64_string, test_string);
115 :
116 1 : if (err)
117 : {
118 0 : fail (1);
119 0 : return 1;
120 : }
121 : else
122 : return 0;
123 : }
|