Debian Codegraph @

tests/test_uri.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Digium, Inc.
5 *
6 * Kevin Harwell <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*!
20 * \file
21 * \brief URI Unit Tests
22 *
23 * \author Kevin Harwell <[email protected]>
24 *
25 */
26
27 /*** MODULEINFO
28 <depend>TEST_FRAMEWORK</depend>
29 <support_level>core</support_level>
30 ***/
31
32 #include "asterisk.h"
33
34 #include "asterisk/test.h"
35 #include "asterisk/module.h"
36 #include "asterisk/uri.h"
37
38 #define CATEGORY "/main/uri/"
39
40 static const char *scenarios[][7] = {
41 {"http://name:pass@localhost", "http", "name:pass", "localhost", NULL, NULL, NULL},
42 {"http://localhost", "http", NULL, "localhost", NULL, NULL, NULL},
43 {"http://localhost:80", "http", NULL, "localhost", "80", NULL, NULL},
44 {"http://localhost/path/", "http", NULL, "localhost", NULL, "path/", NULL},
45 {"http://localhost/?query", "http", NULL, "localhost", NULL, "", "query"},
46 {"http://localhost:80/path", "http", NULL, "localhost", "80", "path", NULL},
47 {"http://localhost:80/?query", "http", NULL, "localhost", "80", "", "query"},
48 {"http://localhost:80/path?query", "http", NULL, "localhost", "80", "path", "query"},
49 };
50
51 AST_TEST_DEFINE(uri_parse)
52 {
53 #define VALIDATE(value, expected_value) \
54 do { ast_test_validate(test, \
55 (value == expected_value) || \
56 (value && expected_value && \
57 !strcmp(value, expected_value))); \
58 } while (0)
59
60 int i;
61
62 switch (cmd) {
63 case TEST_INIT:
64 info->name = __func__;
65 info->category = CATEGORY;
66 info->summary = "Uri parsing scenarios";
67 info->description = "For each scenario validate result(s)";
68 return AST_TEST_NOT_RUN;
69 case TEST_EXECUTE:
70 break;
71 }
72 for (i = 0; i < ARRAY_LEN(scenarios); ++i) {
73 RAII_VAR(struct ast_uri *, uri, NULL, ao2_cleanup);
74 const char **scenario = scenarios[i];
75
76 ast_test_validate(test, (uri = ast_uri_parse(scenario[0])));
77 VALIDATE(ast_uri_scheme(uri), scenario[1]);
78 VALIDATE(ast_uri_user_info(uri), scenario[2]);
79 VALIDATE(ast_uri_host(uri), scenario[3]);
80 VALIDATE(ast_uri_port(uri), scenario[4]);
81 VALIDATE(ast_uri_path(uri), scenario[5]);
82 VALIDATE(ast_uri_query(uri), scenario[6]);
83 }
84
85 return AST_TEST_PASS;
86 }
87
88 AST_TEST_DEFINE(uri_default_http)
89 {
90 RAII_VAR(struct ast_uri *, uri, NULL, ao2_cleanup);
91
92 switch (cmd) {
93 case TEST_INIT:
94 info->name = __func__;
95 info->category = CATEGORY;
96 info->summary = "parse an http uri with host only";
97 info->description = info->summary;
98 return AST_TEST_NOT_RUN;
99 case TEST_EXECUTE:
100 break;
101 }
102
103 ast_test_validate(test, (uri = ast_uri_parse_http("localhost")));
104 ast_test_validate(test, !strcmp(ast_uri_scheme(uri), "http"));
105 ast_test_validate(test, !strcmp(ast_uri_host(uri), "localhost"));
106 ast_test_validate(test, !strcmp(ast_uri_port(uri), "80"));
107 ast_test_validate(test, !ast_uri_is_secure(uri));
108
109 return AST_TEST_PASS;
110 }
111
112 AST_TEST_DEFINE(uri_default_http_secure)
113 {
114 RAII_VAR(struct ast_uri *, uri, NULL, ao2_cleanup);
115
116 switch (cmd) {
117 case TEST_INIT:
118 info->name = __func__;
119 info->category = CATEGORY;
120 info->summary = "parse an https uri with host only";
121 info->description = info->summary;
122 return AST_TEST_NOT_RUN;
123 case TEST_EXECUTE:
124 break;
125 }
126
127 ast_test_validate(test, (uri = ast_uri_parse_http("https://localhost")));
128 ast_test_validate(test, !strcmp(ast_uri_scheme(uri), "https"));
129 ast_test_validate(test, !strcmp(ast_uri_host(uri), "localhost"));
130 ast_test_validate(test, !strcmp(ast_uri_port(uri), "443"));
131 ast_test_validate(test, ast_uri_is_secure(uri));
132
133 return AST_TEST_PASS;
134 }
135
136 static int load_module(void)
137 {
138 AST_TEST_REGISTER(uri_parse);
139 AST_TEST_REGISTER(uri_default_http);
140 AST_TEST_REGISTER(uri_default_http_secure);
141 return AST_MODULE_LOAD_SUCCESS;
142 }
143
144 static int unload_module(void)
145 {
146 AST_TEST_UNREGISTER(uri_default_http_secure);
147 AST_TEST_UNREGISTER(uri_default_http);
148 AST_TEST_UNREGISTER(uri_parse);
149 return 0;
150 }
151
152 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "URI test module");