[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 |
|
---|
[3f932a7e] | 72 | PCUT_INIT;
|
---|
[da4e695] | 73 |
|
---|
| 74 | PCUT_TEST_SUITE(uri_parse);
|
---|
| 75 |
|
---|
[3bacee1] | 76 | PCUT_TEST_BEFORE
|
---|
| 77 | {
|
---|
[da4e695] | 78 | parsed_uri = NULL;
|
---|
| 79 | expected_uri.scheme = NULL;
|
---|
| 80 | expected_uri.user_info = NULL;
|
---|
| 81 | expected_uri.user_credential = NULL;
|
---|
| 82 | expected_uri.host = NULL;
|
---|
| 83 | expected_uri.port = NULL;
|
---|
| 84 | expected_uri.path = "";
|
---|
| 85 | expected_uri.query = NULL;
|
---|
| 86 | expected_uri.fragment = NULL;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[3bacee1] | 89 | PCUT_TEST_AFTER
|
---|
| 90 | {
|
---|
[da4e695] | 91 | if (parsed_uri != NULL) {
|
---|
| 92 | uri_destroy(parsed_uri);
|
---|
| 93 | parsed_uri = NULL;
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[3bacee1] | 97 | PCUT_TEST(only_hostname)
|
---|
| 98 | {
|
---|
[da4e695] | 99 | expected_uri.scheme = "http";
|
---|
| 100 | expected_uri.host = "localhost";
|
---|
[a35b458] | 101 |
|
---|
[da4e695] | 102 | PARSE_AND_CHECK("http://localhost");
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[3bacee1] | 105 | PCUT_TEST(hostname_with_user)
|
---|
| 106 | {
|
---|
[da4e695] | 107 | expected_uri.scheme = "http";
|
---|
| 108 | expected_uri.host = "localhost";
|
---|
| 109 | expected_uri.user_info = "user";
|
---|
[a35b458] | 110 |
|
---|
[da4e695] | 111 | PARSE_AND_CHECK("http://user@localhost");
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[3bacee1] | 114 | PCUT_TEST(hostname_with_user_and_password)
|
---|
| 115 | {
|
---|
[da4e695] | 116 | expected_uri.scheme = "https";
|
---|
| 117 | expected_uri.host = "localhost";
|
---|
| 118 | expected_uri.user_info = "user";
|
---|
| 119 | expected_uri.user_credential = "password";
|
---|
[a35b458] | 120 |
|
---|
[da4e695] | 121 | PARSE_AND_CHECK("https://user:password@localhost");
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[3bacee1] | 124 | PCUT_TEST(path_specification)
|
---|
| 125 | {
|
---|
[da4e695] | 126 | expected_uri.scheme = "http";
|
---|
| 127 | expected_uri.host = "localhost";
|
---|
| 128 | expected_uri.path = "/alpha";
|
---|
[a35b458] | 129 |
|
---|
[da4e695] | 130 | PARSE_AND_CHECK("http://localhost/alpha");
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[3bacee1] | 133 | PCUT_TEST(with_fragment)
|
---|
| 134 | {
|
---|
[da4e695] | 135 | expected_uri.scheme = "http";
|
---|
| 136 | expected_uri.host = "localhost";
|
---|
| 137 | expected_uri.path = "/alpha";
|
---|
| 138 | expected_uri.fragment = "fragment-name";
|
---|
[a35b458] | 139 |
|
---|
[da4e695] | 140 | PARSE_AND_CHECK("http://localhost/alpha#fragment-name");
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 | PCUT_EXPORT(uri_parse);
|
---|