source: mainline/uspace/lib/uri/test/parser.c@ 4e2d387

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4e2d387 was 3bacee1, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Make ccheck-fix again and commit more good files.

  • Property mode set to 100644
File size: 3.7 KB
Line 
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
33typedef 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
45static const_uri_t expected_uri;
46static 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
72PCUT_INIT;
73
74PCUT_TEST_SUITE(uri_parse);
75
76PCUT_TEST_BEFORE
77{
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
89PCUT_TEST_AFTER
90{
91 if (parsed_uri != NULL) {
92 uri_destroy(parsed_uri);
93 parsed_uri = NULL;
94 }
95}
96
97PCUT_TEST(only_hostname)
98{
99 expected_uri.scheme = "http";
100 expected_uri.host = "localhost";
101
102 PARSE_AND_CHECK("http://localhost");
103}
104
105PCUT_TEST(hostname_with_user)
106{
107 expected_uri.scheme = "http";
108 expected_uri.host = "localhost";
109 expected_uri.user_info = "user";
110
111 PARSE_AND_CHECK("http://user@localhost");
112}
113
114PCUT_TEST(hostname_with_user_and_password)
115{
116 expected_uri.scheme = "https";
117 expected_uri.host = "localhost";
118 expected_uri.user_info = "user";
119 expected_uri.user_credential = "password";
120
121 PARSE_AND_CHECK("https://user:password@localhost");
122}
123
124PCUT_TEST(path_specification)
125{
126 expected_uri.scheme = "http";
127 expected_uri.host = "localhost";
128 expected_uri.path = "/alpha";
129
130 PARSE_AND_CHECK("http://localhost/alpha");
131}
132
133PCUT_TEST(with_fragment)
134{
135 expected_uri.scheme = "http";
136 expected_uri.host = "localhost";
137 expected_uri.path = "/alpha";
138 expected_uri.fragment = "fragment-name";
139
140 PARSE_AND_CHECK("http://localhost/alpha#fragment-name");
141}
142
143
144PCUT_EXPORT(uri_parse);
Note: See TracBrowser for help on using the repository browser.