Merge "doc/lang/language-criteria.txt: Link to wiki."
[asterisk/asterisk.git] / tests / test_optional_api.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@digium.com>
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 <dlee@digium.com> \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 {
46         NONE,
47         STUB,
48         IMPL
49 };
50
51 enum was_called was_called_result;
52
53 ast_optional_fn test_optional_ref;
54
55 static void test_optional_stub(void)
56 {
57         was_called_result = STUB;
58 }
59
60 static void test_optional_impl(void)
61 {
62         was_called_result = IMPL;
63 }
64
65 static void test_optional(void)
66 {
67         was_called_result = NONE;
68         if (test_optional_ref) {
69                 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;
91         test_optional_ref = 0;
92
93         ast_optional_api_provide(SYMNAME, test_optional_impl);
94
95         ast_optional_api_use(SYMNAME, &test_optional_ref, test_optional_stub,
96                 AST_MODULE);
97
98         test_optional();
99
100         if (was_called_result != IMPL) {
101                 ast_test_status_update(test, "Expected %d, was %u",
102                         IMPL, was_called_result);
103                 goto done;
104         }
105
106         res = AST_TEST_PASS;
107
108  done:
109         ast_optional_api_unuse(SYMNAME, &test_optional_ref, AST_MODULE);
110         ast_optional_api_unprovide(SYMNAME, test_optional_impl);
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
132         ast_optional_api_use(SYMNAME, &test_optional_ref, test_optional_stub,
133                 AST_MODULE);
134
135         test_optional();
136         if (was_called_result != STUB) {
137                 ast_test_status_update(test, "Expected %d, was %u",
138                         STUB, was_called_result);
139                 goto done;
140         }
141
142         ast_optional_api_provide(SYMNAME, test_optional_impl);
143
144         test_optional();
145         if (was_called_result != IMPL) {
146                 ast_test_status_update(test, "Expected %d, was %u",
147                         IMPL, was_called_result);
148                 ast_optional_api_unprovide(SYMNAME, test_optional_impl);
149                 goto done;
150         }
151
152         ast_optional_api_unprovide(SYMNAME, test_optional_impl);
153
154         test_optional();
155         if (was_called_result != STUB) {
156                 ast_test_status_update(test, "Expected %d, was %u",
157                         STUB, was_called_result);
158                 ast_optional_api_unprovide(SYMNAME, test_optional_impl);
159                 goto done;
160         }
161
162         res = AST_TEST_PASS;
163
164  done:
165         ast_optional_api_unuse(SYMNAME, &test_optional_ref, AST_MODULE);
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);
180         return AST_MODULE_LOAD_SUCCESS;
181 }
182
183 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ARI testing",
184         .load = load_module,
185         .unload = unload_module,
186 );