|
Packit Service |
3749ba |
/*
|
|
Packit Service |
3749ba |
* Copyright (c) 2013 Red Hat Inc.
|
|
Packit Service |
3749ba |
*
|
|
Packit Service |
3749ba |
* Redistribution and use in source and binary forms, with or without
|
|
Packit Service |
3749ba |
* modification, are permitted provided that the following conditions
|
|
Packit Service |
3749ba |
* are met:
|
|
Packit Service |
3749ba |
*
|
|
Packit Service |
3749ba |
* * Redistributions of source code must retain the above
|
|
Packit Service |
3749ba |
* copyright notice, this list of conditions and the
|
|
Packit Service |
3749ba |
* following disclaimer.
|
|
Packit Service |
3749ba |
* * Redistributions in binary form must reproduce the
|
|
Packit Service |
3749ba |
* above copyright notice, this list of conditions and
|
|
Packit Service |
3749ba |
* the following disclaimer in the documentation and/or
|
|
Packit Service |
3749ba |
* other materials provided with the distribution.
|
|
Packit Service |
3749ba |
* * The names of contributors to this software may not be
|
|
Packit Service |
3749ba |
* used to endorse or promote products derived from this
|
|
Packit Service |
3749ba |
* software without specific prior written permission.
|
|
Packit Service |
3749ba |
*
|
|
Packit Service |
3749ba |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit Service |
3749ba |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit Service |
3749ba |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
Packit Service |
3749ba |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
Packit Service |
3749ba |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
Packit Service |
3749ba |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
Packit Service |
3749ba |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
Packit Service |
3749ba |
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
Packit Service |
3749ba |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
Packit Service |
3749ba |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
Packit Service |
3749ba |
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
|
Packit Service |
3749ba |
* DAMAGE.
|
|
Packit Service |
3749ba |
*
|
|
Packit Service |
3749ba |
* Author: Stef Walter <stefw@redhat.com>
|
|
Packit Service |
3749ba |
*/
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
#include "config.h"
|
|
Packit Service |
3749ba |
#include "test.h"
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
#include "debug.h"
|
|
Packit Service |
3749ba |
#include "message.h"
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
#include <assert.h>
|
|
Packit Service |
3749ba |
#include <string.h>
|
|
Packit Service |
3749ba |
#include <stdio.h>
|
|
Packit Service |
3749ba |
#include <stdlib.h>
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
#include "url.h"
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
check_decode_msg (const char *file,
|
|
Packit Service |
3749ba |
int line,
|
|
Packit Service |
3749ba |
const char *function,
|
|
Packit Service |
3749ba |
const char *input,
|
|
Packit Service |
3749ba |
ssize_t input_len,
|
|
Packit Service |
3749ba |
const char *expected,
|
|
Packit Service |
3749ba |
size_t expected_len)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
unsigned char *decoded;
|
|
Packit Service |
3749ba |
size_t length;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
if (input_len < 0)
|
|
Packit Service |
3749ba |
input_len = strlen (input);
|
|
Packit Service |
3749ba |
decoded = p11_url_decode (input, input + input_len, "", &length);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
if (expected == NULL) {
|
|
Packit Service |
3749ba |
if (decoded != NULL)
|
|
Packit Service |
3749ba |
p11_test_fail (file, line, function, "decoding should have failed");
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
} else {
|
|
Packit Service |
3749ba |
if (decoded == NULL)
|
|
Packit Service |
3749ba |
p11_test_fail (file, line, function, "decoding failed");
|
|
Packit Service |
3749ba |
if (expected_len != length)
|
|
Packit Service |
3749ba |
p11_test_fail (file, line, function, "wrong length: (%lu != %lu)",
|
|
Packit Service |
3749ba |
(unsigned long)expected_len, (unsigned long)length);
|
|
Packit Service |
3749ba |
if (memcmp (decoded, expected, length) != 0)
|
|
Packit Service |
3749ba |
p11_test_fail (file, line, function, "decoding wrong");
|
|
Packit Service |
3749ba |
free (decoded);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
#define check_decode_success(input, input_len, expected, expected_len) \
|
|
Packit Service |
3749ba |
check_decode_msg (__FILE__, __LINE__, __FUNCTION__, input, input_len, expected, expected_len)
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
#define check_decode_failure(input, input_len) \
|
|
Packit Service |
3749ba |
check_decode_msg (__FILE__, __LINE__, __FUNCTION__, input, input_len, NULL, 0)
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_decode_success (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
check_decode_success ("%54%45%53%54%00", -1, "TEST", 5);
|
|
Packit Service |
3749ba |
check_decode_success ("%54%45%53%54%00", 6, "TE", 2);
|
|
Packit Service |
3749ba |
check_decode_success ("%54est%00", -1, "Test", 5);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_decode_skip (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
const char *input = "%54 %45 %53 %54 %00";
|
|
Packit Service |
3749ba |
unsigned char *decoded;
|
|
Packit Service |
3749ba |
size_t length;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
decoded = p11_url_decode (input, input + strlen (input), P11_URL_WHITESPACE, &length);
|
|
Packit Service |
3749ba |
assert_str_eq ("TEST", (char *)decoded);
|
|
Packit Service |
3749ba |
assert_num_eq (5, length);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
free (decoded);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_decode_failure (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
/* Early termination */
|
|
Packit Service |
3749ba |
check_decode_failure ("%54%45%53%5", -1);
|
|
Packit Service |
3749ba |
check_decode_failure ("%54%45%53%", -1);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
/* Not hex characters */
|
|
Packit Service |
3749ba |
check_decode_failure ("%54%XX%53%54%00", -1);
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_encode (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
const unsigned char *input = (unsigned char *)"TEST";
|
|
Packit Service |
3749ba |
p11_buffer buf;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
if (!p11_buffer_init_null (&buf, 5))
|
|
Packit Service |
3749ba |
assert_not_reached ();
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_url_encode (input, input + 5, "", &buf;;
|
|
Packit Service |
3749ba |
assert (p11_buffer_ok (&buf));
|
|
Packit Service |
3749ba |
assert_str_eq ("%54%45%53%54%00", (char *)buf.data);
|
|
Packit Service |
3749ba |
assert_num_eq (15, buf.len);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_uninit (&buf;;
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_encode_verbatim (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
const unsigned char *input = (unsigned char *)"TEST";
|
|
Packit Service |
3749ba |
p11_buffer buf;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
if (!p11_buffer_init_null (&buf, 5))
|
|
Packit Service |
3749ba |
assert_not_reached ();
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_url_encode (input, input + 5, "ES", &buf;;
|
|
Packit Service |
3749ba |
assert (p11_buffer_ok (&buf));
|
|
Packit Service |
3749ba |
assert_str_eq ("%54ES%54%00", (char *)buf.data);
|
|
Packit Service |
3749ba |
assert_num_eq (11, buf.len);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_uninit (&buf;;
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
static void
|
|
Packit Service |
3749ba |
test_encode_lower (void)
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
const unsigned char *input = (unsigned char *)",.:;";
|
|
Packit Service |
3749ba |
p11_buffer buf;
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
if (!p11_buffer_init_null (&buf, 5))
|
|
Packit Service |
3749ba |
assert_not_reached ();
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_url_encode (input, input + 5, "", &buf;;
|
|
Packit Service |
3749ba |
assert (p11_buffer_ok (&buf));
|
|
Packit Service |
3749ba |
assert_str_eq ("%2C%2E%3A%3B%00", (char *)buf.data);
|
|
Packit Service |
3749ba |
assert_num_eq (15, buf.len);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
if (!p11_buffer_reset (&buf, 5))
|
|
Packit Service |
3749ba |
assert_not_reached ();
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
setenv ("P11_KIT_URI_LOWERCASE", "1", 1);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_url_encode (input, input + 5, "", &buf;;
|
|
Packit Service |
3749ba |
assert (p11_buffer_ok (&buf));
|
|
Packit Service |
3749ba |
assert_str_eq ("%2c%2e%3a%3b%00", (char *)buf.data);
|
|
Packit Service |
3749ba |
assert_num_eq (15, buf.len);
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_buffer_uninit (&buf;;
|
|
Packit Service |
3749ba |
}
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
int
|
|
Packit Service |
3749ba |
main (int argc,
|
|
Packit Service |
3749ba |
char *argv[])
|
|
Packit Service |
3749ba |
{
|
|
Packit Service |
3749ba |
p11_test (test_decode_success, "/url/decode-success");
|
|
Packit Service |
3749ba |
p11_test (test_decode_skip, "/url/decode-skip");
|
|
Packit Service |
3749ba |
p11_test (test_decode_failure, "/url/decode-failure");
|
|
Packit Service |
3749ba |
|
|
Packit Service |
3749ba |
p11_test (test_encode, "/url/encode");
|
|
Packit Service |
3749ba |
p11_test (test_encode_verbatim, "/url/encode-verbatim");
|
|
Packit Service |
3749ba |
p11_test (test_encode_lower, "/url/encode-lower");
|
|
Packit Service |
3749ba |
return p11_test_run (argc, argv);
|
|
Packit Service |
3749ba |
}
|