Asterisk - The Open Source Telephony Project  18.5.0
test_optional_api.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * David M. Lee, II <[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 Test optional API.
22  *
23  * This tests exercise the underlying implementation functions. Acutal usage
24  * won't look anything like this; it would use the wrapper macros.
25  *
26  * \author\verbatim David M. Lee, II <[email protected]> \endverbatim
27  *
28  * \ingroup tests
29  */
30 
31 /*** MODULEINFO
32  <depend>TEST_FRAMEWORK</depend>
33  <depend>OPTIONAL_API</depend>
34  <support_level>core</support_level>
35  ***/
36 
37 #include "asterisk.h"
38 
39 #include "asterisk/module.h"
40 #include "asterisk/optional_api.h"
41 #include "asterisk/test.h"
42 
43 #define CATEGORY "/main/optional_api/"
44 
45 enum was_called {
49 };
50 
52 
53 ast_optional_fn test_optional_ref;
54 
55 static void test_optional_stub(void)
56 {
58 }
59 
60 static void test_optional_impl(void)
61 {
63 }
64 
65 static void test_optional(void)
66 {
68  if (test_optional_ref) {
70  }
71 }
72 
73 #define SYMNAME "test_option"
74 
75 AST_TEST_DEFINE(test_provide_first)
76 {
77  enum ast_test_result_state res;
78 
79  switch (cmd) {
80  case TEST_INIT:
81  info->name = __func__;
82  info->category = CATEGORY;
83  info->summary = "Test optional API publishing.";
84  info->description = "Test optional API publishing.";
85  return AST_TEST_NOT_RUN;
86  case TEST_EXECUTE:
87  break;
88  }
89 
90  res = AST_TEST_FAIL;
92 
94 
96  AST_MODULE);
97 
98  test_optional();
99 
100  if (was_called_result != IMPL) {
101  ast_test_status_update(test, "Expected %d, was %u",
103  goto done;
104  }
105 
106  res = AST_TEST_PASS;
107 
108  done:
111  return res;
112 }
113 
114 AST_TEST_DEFINE(test_provide_last)
115 {
116  enum ast_test_result_state res;
117 
118  switch (cmd) {
119  case TEST_INIT:
120  info->name = __func__;
121  info->category = CATEGORY;
122  info->summary = "Test optional API publishing.";
123  info->description = "Test optional API publishing.";
124  return AST_TEST_NOT_RUN;
125  case TEST_EXECUTE:
126  break;
127  }
128 
129  res = AST_TEST_FAIL;
130  test_optional_ref = 0;
131 
133  AST_MODULE);
134 
135  test_optional();
136  if (was_called_result != STUB) {
137  ast_test_status_update(test, "Expected %d, was %u",
139  goto done;
140  }
141 
143 
144  test_optional();
145  if (was_called_result != IMPL) {
146  ast_test_status_update(test, "Expected %d, was %u",
149  goto done;
150  }
151 
153 
154  test_optional();
155  if (was_called_result != STUB) {
156  ast_test_status_update(test, "Expected %d, was %u",
159  goto done;
160  }
161 
162  res = AST_TEST_PASS;
163 
164  done:
166  return res;
167 }
168 
169 static int unload_module(void)
170 {
171  AST_TEST_UNREGISTER(test_provide_first);
172  AST_TEST_UNREGISTER(test_provide_last);
173  return 0;
174 }
175 
176 static int load_module(void)
177 {
178  AST_TEST_REGISTER(test_provide_first);
179  AST_TEST_REGISTER(test_provide_last);
181 }
182 
184  .support_level = AST_MODULE_SUPPORT_CORE,
185  .load = load_module,
186  .unload = unload_module,
187 );
ast_optional_fn test_optional_ref
void ast_optional_api_provide(const char *symname, ast_optional_fn impl)
Definition: optional_api.c:212
Asterisk main include file. File version handling, generic pbx functions.
#define SYMNAME
Optional API function macros.
Test Framework API.
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
AST_TEST_DEFINE(test_provide_first)
enum was_called was_called_result
int done
Definition: test_amihooks.c:48
static int unload_module(void)
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
static int load_module(void)
static void test_optional(void)
void ast_optional_api_unprovide(const char *symname, ast_optional_fn impl)
Definition: optional_api.c:222
static void test_optional_stub(void)
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
def info(msg)
void ast_optional_api_use(const char *symname, ast_optional_fn *optional_ref, ast_optional_fn stub, const char *module)
Definition: optional_api.c:232
void ast_optional_api_unuse(const char *symname, ast_optional_fn *optional_ref, const char *module)
Definition: optional_api.c:257
#define CATEGORY
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
#define AST_MODULE
was_called
static void test_optional_impl(void)
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
ast_test_result_state
Definition: test.h:200