diff options
author | Stef Walter <stefw@gnome.org> | 2013-03-11 17:17:15 +0100 |
---|---|---|
committer | Stef Walter <stefw@gnome.org> | 2013-03-15 18:18:47 +0100 |
commit | 48004b92d4c65080ac71f6a48297abd4d83dfdcb (patch) | |
tree | 09c1f5051e5ca59d5465344e532e2e3b85b48648 /common/tests | |
parent | 06bf3da80eb780621e0f1eb0ab8d4716ed7b3478 (diff) |
url: Split out the URL encoding and decoding functions
We want to use these as the format for encoding binary data
in our PKCS#11 attribute persistence
https://bugs.freedesktop.org/show_bug.cgi?id=62156
Diffstat (limited to 'common/tests')
-rw-r--r-- | common/tests/Makefile.am | 1 | ||||
-rw-r--r-- | common/tests/test-url.c | 166 |
2 files changed, 167 insertions, 0 deletions
diff --git a/common/tests/Makefile.am b/common/tests/Makefile.am index f31cebb..e14fddd 100644 --- a/common/tests/Makefile.am +++ b/common/tests/Makefile.am @@ -21,6 +21,7 @@ CHECK_PROGS = \ test-attrs \ test-buffer \ test-lexer \ + test-url \ $(NULL) noinst_PROGRAMS = \ diff --git a/common/tests/test-url.c b/common/tests/test-url.c new file mode 100644 index 0000000..096563b --- /dev/null +++ b/common/tests/test-url.c @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2013 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Stef Walter <stefw@redhat.com> + */ + +#include "config.h" +#include "CuTest.h" + +#include "library.h" + +#include <assert.h> +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +#include "url.h" + +static void +check_decode_msg (CuTest *tc, + const char *file, + int line, + const char *input, + ssize_t input_len, + const char *expected, + size_t expected_len) +{ + unsigned char *decoded; + size_t length; + + if (input_len < 0) + input_len = strlen (input); + decoded = p11_url_decode (input, input + input_len, "", &length); + + if (expected == NULL) { + CuAssert_Line (tc, file, line, "decoding should have failed", decoded == NULL); + + } else { + CuAssert_Line (tc, file, line, "decoding failed", decoded != NULL); + CuAssertIntEquals_LineMsg (tc, file, line, "wrong length", expected_len, length); + CuAssert_Line (tc, file, line, "decoded wrong", memcmp (decoded, expected, length) == 0); + free (decoded); + } +} + +#define check_decode_success(tc, input, input_len, expected, expected_len) \ + check_decode_msg (tc, __FILE__, __LINE__, input, input_len, expected, expected_len) + +#define check_decode_failure(tc, input, input_len) \ + check_decode_msg (tc, __FILE__, __LINE__, input, input_len, NULL, 0) + +static void +test_decode_success (CuTest *tc) +{ + check_decode_success (tc, "%54%45%53%54%00", -1, "TEST", 5); + check_decode_success (tc, "%54%45%53%54%00", 6, "TE", 2); + check_decode_success (tc, "%54est%00", -1, "Test", 5); +} + +static void +test_decode_skip (CuTest *tc) +{ + const char *input = "%54 %45 %53 %54 %00"; + unsigned char *decoded; + size_t length; + + decoded = p11_url_decode (input, input + strlen (input), P11_URL_WHITESPACE, &length); + CuAssertStrEquals (tc, "TEST", (char *)decoded); + CuAssertIntEquals (tc, 5, length); + + free (decoded); +} + +static void +test_decode_failure (CuTest *tc) +{ + /* Early termination */ + check_decode_failure (tc, "%54%45%53%5", -1); + check_decode_failure (tc, "%54%45%53%", -1); + + /* Not hex characters */ + check_decode_failure (tc, "%54%XX%53%54%00", -1); +} + +static void +test_encode (CuTest *tc) +{ + const unsigned char *input = (unsigned char *)"TEST"; + char *encoded; + size_t length; + + encoded = p11_url_encode (input, input + 5, "", &length); + CuAssertStrEquals (tc, "%54%45%53%54%00", (char *)encoded); + CuAssertIntEquals (tc, 15, length); + + free (encoded); +} + +static void +test_encode_verbatim (CuTest *tc) +{ + const unsigned char *input = (unsigned char *)"TEST"; + char *encoded; + size_t length; + + encoded = p11_url_encode (input, input + 5, "ES", &length); + CuAssertStrEquals (tc, "%54ES%54%00", (char *)encoded); + CuAssertIntEquals (tc, 11, length); + + free (encoded); +} + +int +main (void) +{ + CuString *output = CuStringNew (); + CuSuite* suite = CuSuiteNew (); + int ret; + + putenv ("P11_KIT_STRICT=1"); + p11_library_init (); + + SUITE_ADD_TEST (suite, test_decode_success); + SUITE_ADD_TEST (suite, test_decode_skip); + SUITE_ADD_TEST (suite, test_decode_failure); + + SUITE_ADD_TEST (suite, test_encode); + SUITE_ADD_TEST (suite, test_encode_verbatim); + + CuSuiteRun (suite); + CuSuiteSummary (suite, output); + CuSuiteDetails (suite, output); + printf ("%s\n", output->buffer); + ret = suite->failCount; + CuSuiteDelete (suite); + CuStringDelete (output); + return ret; +} |