summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNIIBE Yutaka <gniibe@fsij.org>2017-02-01 19:45:39 +0900
committerNIIBE Yutaka <gniibe@fsij.org>2017-02-01 19:45:39 +0900
commit4bfc2117b70415a5c5d3f0a0ac9086e168350d83 (patch)
treef2c59bddd20f3bb89ffc298c2167fc2d966852e9 /tests
parent55c497904dd0794ca5cfcafe369943736b0d4e62 (diff)
downloadlibgpg-error-4bfc2117b70415a5c5d3f0a0ac9086e168350d83.tar.gz
Add Base64 decoder.
* NEWS: Add interface changes. * src/Makefile.am (libgpg_error_la_SOURCES): Add b64dec.c. * src/b64dec.c: New. Taken from gpgme. Prefix function names with _gpgrt_. Change API a bit, not exposing the structure. * src/gpg-error.def.in: Export Base64 functions. * src/gpg-error.vers: Likewise. * src/visibility.c, src/visibility.h: Likewise. * src/gpg-error.h.in: Add Base64 struct and functions. * src/gpgrt-int.h: Add Base64 internal functions. * tests/Makefile.am (TESTS): Add t-b64dec. * tests/t-b64dec.c: New. Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/t-b64dec.c123
2 files changed, 124 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 92b97f2..a3c6cbd 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -27,7 +27,7 @@ endif
gpg_error_lib = ../src/libgpg-error.la
-TESTS = t-version t-strerror t-syserror t-lock t-printf t-poll
+TESTS = t-version t-strerror t-syserror t-lock t-printf t-poll t-b64dec
AM_CPPFLAGS = -I$(top_builddir)/src $(extra_includes)
diff --git a/tests/t-b64dec.c b/tests/t-b64dec.c
new file mode 100644
index 0000000..aae208b
--- /dev/null
+++ b/tests/t-b64dec.c
@@ -0,0 +1,123 @@
+/* t-b64dec.c - b64dec test.
+ Copyright (C) 2017 g10 Code GmbH
+
+ This file is part of libgpg-error.
+
+ libgpg-error is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public License
+ as published by the Free Software Foundation; either version 2.1 of
+ the License, or (at your option) any later version.
+
+ libgpg-error 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with libgpgme-error; if not, write to the Free
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA. */
+
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#if HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#include <gpg-error.h>
+
+static const char *test_b64_string = "bGliZ3BnLWVycm9yIGlzIGZyZWUgc29"
+ "mdHdhcmU7IHlvdSBjYW4gcmVkaXN0cmlidXRlIGl0IGFuZC9vciBtb2RpZnkgaXQgd"
+ "W5kZXIgdGhlIHRlcm1zIG9mIHRoZSBHTlUgTGVzc2VyIEdlbmVyYWwgUHVibGljIEx"
+ "pY2Vuc2UgYXMgcHVibGlzaGVkIGJ5IHRoZSBGcmVlIFNvZnR3YXJlIEZvdW5kYXRpb"
+ "247IGVpdGhlciB2ZXJzaW9uIDIuMSBvZiB0aGUgTGljZW5zZSwgb3IgKGF0IHlvdXI"
+ "gb3B0aW9uKSBhbnkgbGF0ZXIgdmVyc2lvbi4=";
+
+static const char *test_string = "libgpg-error is free software; "
+ "you can redistribute it and/or modify it under the terms of "
+ "the GNU Lesser General Public License as published by the Free "
+ "Software Foundation; either version 2.1 of the License, or "
+ "(at your option) any later version.";
+
+#define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
+ __FILE__,__LINE__, (a)); \
+ errcount++; \
+ } while(0)
+
+static int errcount;
+
+static gpg_error_t
+test_b64dec_string (const char *string, const char *expected)
+{
+ gpg_error_t err;
+ gpgrt_b64state_t state;
+ char *buffer;
+ size_t len;
+
+ len = strlen (string);
+ buffer = malloc (strlen (string) + 1);
+ if (!buffer)
+ {
+ err = gpg_error_from_syserror ();
+ return err;
+ }
+
+ state = gpgrt_b64dec_start ("");
+ if (!state)
+ {
+ err = gpg_error_from_syserror ();
+ free (buffer);
+ return err;
+ }
+
+ err = gpgrt_b64dec_proc (state, buffer, len, &len);
+ if (err)
+ {
+ if (gpg_err_code (err) != GPG_ERR_EOF)
+ {
+ free (buffer);
+ free (state);
+ return err;
+ }
+ }
+
+ err = gpgrt_b64dec_finish (state);
+ if (err)
+ {
+ free (buffer);
+ return err;
+ }
+
+ if (strncmp (buffer, expected, len) == 0)
+ err = 0;
+ else
+ err = GPG_ERR_INTERNAL;
+
+ free (buffer);
+ return err;
+}
+
+
+
+int
+main (int argc, char **argv)
+{
+ gpg_error_t err;
+
+ (void)argc;
+ (void)argv;
+
+ err = test_b64dec_string (test_b64_string, test_string);
+
+ if (err)
+ {
+ fail (1);
+ return 1;
+ }
+ else
+ return 0;
+}