Asterisk - The Open Source Telephony Project  18.5.0
test_uri.c
Go to the documentation of this file.
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);
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 
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk main include file. File version handling, generic pbx functions.
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
static int load_module(void)
Definition: test_uri.c:136
const char * ast_uri_path(const struct ast_uri *uri)
Retrieve the uri path.
Definition: uri.c:135
Stores parsed uri information.
Definition: uri.c:30
Test Framework API.
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
struct ast_uri * ast_uri_parse(const char *uri)
Parse the given uri into a structure.
Definition: uri.c:152
AST_TEST_DEFINE(uri_parse)
Definition: test_uri.c:51
#define VALIDATE(value, expected_value)
#define NULL
Definition: resample.c:96
const char * ast_uri_query(const struct ast_uri *uri)
Retrieve the uri query parameters.
Definition: uri.c:140
const char * ast_uri_user_info(const struct ast_uri *uri)
Retrieve the uri user information.
Definition: uri.c:120
struct ast_uri * ast_uri_parse_http(const char *uri)
Parse the given http uri into a structure.
Definition: uri.c:290
int attribute_pure ast_uri_is_secure(const struct ast_uri *uri)
Retrieve if the uri is of a secure type.
Definition: uri.c:145
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
static int unload_module(void)
Definition: test_uri.c:144
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
def info(msg)
const char * ast_uri_port(const struct ast_uri *uri)
Retrieve the uri port.
Definition: uri.c:130
static const char * scenarios[][7]
Definition: test_uri.c:40
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define CATEGORY
Definition: test_uri.c:38
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
const char * ast_uri_scheme(const struct ast_uri *uri)
Retrieve the uri scheme.
Definition: uri.c:115
const char * ast_uri_host(const struct ast_uri *uri)
Retrieve the uri host.
Definition: uri.c:125