[da4e695] | 1 | /*
|
---|
| 2 | * Copyright (c) 2014 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <pcut/pcut.h>
|
---|
| 30 | #include <str.h>
|
---|
| 31 | #include "../uri.h"
|
---|
| 32 |
|
---|
| 33 | typedef struct {
|
---|
| 34 | const char *scheme;
|
---|
| 35 | const char *user_info;
|
---|
| 36 | const char *user_credential;
|
---|
| 37 | const char *host;
|
---|
| 38 | const char *port;
|
---|
| 39 | const char *path;
|
---|
| 40 | const char *query;
|
---|
| 41 | const char *fragment;
|
---|
| 42 | } const_uri_t;
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | static const_uri_t expected_uri;
|
---|
| 46 | static uri_t *parsed_uri;
|
---|
| 47 |
|
---|
| 48 | /*
|
---|
| 49 | * In the following macros we assume that the expected and the parsed
|
---|
| 50 | * URI are in the global variables declared above.
|
---|
| 51 | */
|
---|
| 52 |
|
---|
| 53 | #define CHECK(item) \
|
---|
| 54 | PCUT_ASSERT_STR_EQUALS_OR_NULL(expected_uri.item, parsed_uri->item)
|
---|
| 55 |
|
---|
| 56 | #define PARSE_AND_CHECK(the_uri) \
|
---|
| 57 | do { \
|
---|
| 58 | parsed_uri = uri_parse(the_uri); \
|
---|
| 59 | PCUT_ASSERT_NOT_NULL(parsed_uri); \
|
---|
| 60 | PCUT_ASSERT_INT_EQUALS(0, !uri_validate(parsed_uri)); \
|
---|
| 61 | CHECK(scheme); \
|
---|
| 62 | CHECK(user_info); \
|
---|
| 63 | CHECK(user_credential); \
|
---|
| 64 | CHECK(host); \
|
---|
| 65 | CHECK(port); \
|
---|
| 66 | CHECK(path); \
|
---|
| 67 | CHECK(query); \
|
---|
| 68 | CHECK(fragment); \
|
---|
| 69 | } while (0)
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | PCUT_INIT
|
---|
| 73 |
|
---|
| 74 | PCUT_TEST_SUITE(uri_parse);
|
---|
| 75 |
|
---|
| 76 | PCUT_TEST_BEFORE {
|
---|
| 77 | parsed_uri = NULL;
|
---|
| 78 | expected_uri.scheme = NULL;
|
---|
| 79 | expected_uri.user_info = NULL;
|
---|
| 80 | expected_uri.user_credential = NULL;
|
---|
| 81 | expected_uri.host = NULL;
|
---|
| 82 | expected_uri.port = NULL;
|
---|
| 83 | expected_uri.path = "";
|
---|
| 84 | expected_uri.query = NULL;
|
---|
| 85 | expected_uri.fragment = NULL;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | PCUT_TEST_AFTER {
|
---|
| 89 | if (parsed_uri != NULL) {
|
---|
| 90 | uri_destroy(parsed_uri);
|
---|
| 91 | parsed_uri = NULL;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | PCUT_TEST(only_hostname) {
|
---|
| 96 | expected_uri.scheme = "http";
|
---|
| 97 | expected_uri.host = "localhost";
|
---|
| 98 |
|
---|
| 99 | PARSE_AND_CHECK("http://localhost");
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | PCUT_TEST(hostname_with_user) {
|
---|
| 103 | expected_uri.scheme = "http";
|
---|
| 104 | expected_uri.host = "localhost";
|
---|
| 105 | expected_uri.user_info = "user";
|
---|
| 106 |
|
---|
| 107 | PARSE_AND_CHECK("http://user@localhost");
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | PCUT_TEST(hostname_with_user_and_password) {
|
---|
| 111 | expected_uri.scheme = "https";
|
---|
| 112 | expected_uri.host = "localhost";
|
---|
| 113 | expected_uri.user_info = "user";
|
---|
| 114 | expected_uri.user_credential = "password";
|
---|
| 115 |
|
---|
| 116 | PARSE_AND_CHECK("https://user:password@localhost");
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | PCUT_TEST(path_specification) {
|
---|
| 120 | expected_uri.scheme = "http";
|
---|
| 121 | expected_uri.host = "localhost";
|
---|
| 122 | expected_uri.path = "/alpha";
|
---|
| 123 |
|
---|
| 124 | PARSE_AND_CHECK("http://localhost/alpha");
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | PCUT_TEST(with_fragment) {
|
---|
| 128 | expected_uri.scheme = "http";
|
---|
| 129 | expected_uri.host = "localhost";
|
---|
| 130 | expected_uri.path = "/alpha";
|
---|
| 131 | expected_uri.fragment = "fragment-name";
|
---|
| 132 |
|
---|
| 133 | PARSE_AND_CHECK("http://localhost/alpha#fragment-name");
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | PCUT_EXPORT(uri_parse);
|
---|