Asterisk - The Open Source Telephony Project GIT-master-35be64c
Loading...
Searching...
No Matches
test_extension_state.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2026, Ben Kolodny
5 *
6 * Ben Kolodny <3232963@gmail.com>
7 *
8 * See https://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 Extension state legacy callback tests
22 *
23 * \ingroup tests
24 */
25
26/*** MODULEINFO
27 <depend>TEST_FRAMEWORK</depend>
28 <support_level>core</support_level>
29 ***/
30
31#include "asterisk.h"
32
33#include "asterisk/module.h"
34#include "asterisk/test.h"
35#include "asterisk/pbx.h"
37#include "asterisk/astobj2.h"
38#include "asterisk/lock.h"
39#include "asterisk/time.h"
40#include "asterisk/utils.h"
41
42#define TEST_CATEGORY "/main/extension_state/"
43#define TEST_CONTEXT "test_extension_state"
44#define TEST_REGISTRAR "test_extension_state"
45
46/*!
47 * \brief Prefix for the devices used by the test hints
48 *
49 * Device state is global, so the hints use device names reserved for this test
50 * to avoid affecting, or being affected by, any other hint.
51 */
52#define TEST_DEVICE_PREFIX "Custom:test_extension_state_"
53
54/*! \brief How long to wait for asynchronous events, in seconds */
55#define TEST_TIMEOUT 5
56
57/*!
58 * \brief Tracking for a single extension state callback
59 *
60 * While a callback is registered it holds its own reference to the watcher, which
61 * is released only by the destroy callback. This keeps the watcher valid for as long
62 * as the callback can use it, even if the test gives up waiting and returns.
63 */
65 /*! The ID returned when adding the callback */
66 int id;
67 /*! The last extension state received */
69 /*! Number of times the destroy callback was invoked */
71 /*! The spelling of the extension this watcher subscribed to */
73};
74
75/*! \brief Protects watcher state and test_run */
78
79/*! \brief Used to make the extensions, and so the devices, differ between test runs */
80static unsigned int test_run;
81
82static struct test_watcher *test_watcher_alloc(const char *exten)
83{
84 struct test_watcher *watcher;
85
86 watcher = ao2_alloc_options(sizeof(*watcher), NULL, AO2_ALLOC_OPT_LOCK_NOLOCK);
87 if (!watcher) {
88 return NULL;
89 }
90
91 watcher->id = -1;
93 ast_copy_string(watcher->exten, exten, sizeof(watcher->exten));
94
95 return watcher;
96}
97
98static int state_changed(const char *context, const char *exten, struct ast_state_cb_info *info, void *data)
99{
100 struct test_watcher *watcher = data;
101
103 watcher->last_state = info->exten_state;
106
107 return 0;
108}
109
110static void state_destroyed(int id, void *data)
111{
112 struct test_watcher *watcher = data;
113
115 watcher->destroyed++;
118
119 /* Release the reference held by the callback */
120 ao2_ref(watcher, -1);
121}
122
123/*!
124 * \brief Add an extension state callback for a watcher
125 *
126 * \retval 0 on success
127 * \retval -1 on failure, in which case the destroy callback must not have been
128 * invoked as the caller retains ownership of the callback data
129 */
130static int test_watcher_add(struct ast_test *test, const char *context, struct test_watcher *watcher)
131{
132 int destroyed;
133
134 /* The callback holds its own reference to the watcher */
135 ao2_ref(watcher, +1);
136
137 watcher->id = ast_extension_state_add_destroy_extended(context, watcher->exten,
139 if (watcher->id >= 0) {
140 return 0;
141 }
142
143 /*
144 * On failure the caller keeps ownership, so the reference given to the callback
145 * is ours to release. If the destroy callback was wrongly invoked it has already
146 * released it, synchronously, before the add returned.
147 */
149 destroyed = watcher->destroyed;
151
152 if (destroyed) {
153 ast_test_status_update(test, "Destroy callback for '%s' invoked %d times on failed add\n",
154 watcher->exten, destroyed);
155 } else {
156 ao2_ref(watcher, -1);
157 }
158
159 return -1;
160}
161
162/*!
163 * \brief Remove an extension state callback and wait for it to be destroyed
164 *
165 * \retval 0 if the destroy callback was invoked exactly once
166 * \retval -1 otherwise
167 *
168 * \note If destruction does not occur in time the callback keeps its reference,
169 * so the watcher remains valid if the callback is invoked later.
170 */
171static int test_watcher_remove(struct ast_test *test, struct test_watcher *watcher)
172{
173 struct timeval now = ast_tvnow();
174 struct timespec deadline = {
175 .tv_sec = now.tv_sec + TEST_TIMEOUT,
176 .tv_nsec = now.tv_usec * 1000,
177 };
178 int destroyed;
179
181
183 while (!watcher->destroyed) {
184 if (ast_cond_timedwait(&test_cond, &test_lock, &deadline) == ETIMEDOUT) {
185 break;
186 }
187 }
188 destroyed = watcher->destroyed;
190
191 if (destroyed != 1) {
192 ast_test_status_update(test, "Destroy callback for '%s' invoked %d times, expected 1\n",
193 watcher->exten, destroyed);
194 return -1;
195 }
196
197 return 0;
198}
199
200/*! \brief Wait until a watcher has received the given extension state */
202{
203 struct timeval now = ast_tvnow();
204 struct timespec deadline = {
205 .tv_sec = now.tv_sec + TEST_TIMEOUT,
206 .tv_nsec = now.tv_usec * 1000,
207 };
208 int res;
209
211 while (watcher->last_state != state) {
212 if (ast_cond_timedwait(&test_cond, &test_lock, &deadline) == ETIMEDOUT) {
213 break;
214 }
215 }
216 res = watcher->last_state == state ? 0 : -1;
218
219 return res;
220}
221
222static int dialplan_setup(void)
223{
225 return -1;
226 }
227
228 /*
229 * A pattern hint creates a concrete hint, and so a device, using the spelling
230 * of whichever lookup comes first.
231 */
234 return -1;
235 }
236
237 return 0;
238}
239
244
245/*!
246 * \brief Watch two spellings of an extension that differ only by dashes
247 *
248 * This mirrors two phones with BLF keys for the same extension, one configured
249 * with a dash and one without. The first spelling creates the hint from the
250 * pattern. Both watchers must be added, both must be notified when the device
251 * of the hint changes state, and both must be destroyed exactly once.
252 *
253 * \param test The test
254 * \param base The leading digits of the extension
255 * \param suffix The trailing part of the extension, joined by a dash when dashed
256 * \param dashed_first Whether the dashed spelling is watched first, naming the device
257 *
258 * The test run number is added to the extension, so that runs use different
259 * devices and a run is not affected by state left behind by an earlier one.
260 */
261static enum ast_test_result_state dash_spelling_check(struct ast_test *test,
262 const char *base, const char *suffix, int dashed_first)
263{
264 struct test_watcher *watchers[2] = { NULL, NULL };
266 char second[AST_MAX_EXTENSION];
267 char device[AST_MAX_EXTENSION + sizeof(TEST_DEVICE_PREFIX)];
269 unsigned int run;
270 int added[2] = { 0, 0 };
271 int i;
272
274 run = ++test_run;
276
277 snprintf(first, sizeof(first), "%s%u%s%s", base, run, dashed_first ? "-" : "", suffix);
278 snprintf(second, sizeof(second), "%s%u%s%s", base, run, dashed_first ? "" : "-", suffix);
279 /* The device is named after the spelling that created the hint */
280 snprintf(device, sizeof(device), TEST_DEVICE_PREFIX "%s", first);
281
282 watchers[0] = test_watcher_alloc(first);
283 watchers[1] = test_watcher_alloc(second);
284 if (!watchers[0] || !watchers[1]) {
285 ast_test_status_update(test, "Failed to allocate watchers\n");
286 ao2_cleanup(watchers[0]);
287 ao2_cleanup(watchers[1]);
288 return AST_TEST_FAIL;
289 }
290
291 if (dialplan_setup()) {
292 ast_test_status_update(test, "Failed to set up dialplan\n");
293 res = AST_TEST_FAIL;
294 goto cleanup;
295 }
296
297 for (i = 0; i < ARRAY_LEN(watchers); i++) {
298 if (test_watcher_add(test, TEST_CONTEXT, watchers[i])) {
299 ast_test_status_update(test, "Failed to watch '%s'\n", watchers[i]->exten);
300 res = AST_TEST_FAIL;
301 } else {
302 added[i] = 1;
303 }
304 }
305
306 if (res == AST_TEST_PASS) {
308 for (i = 0; i < ARRAY_LEN(watchers); i++) {
310 ast_test_status_update(test, "Watcher of '%s' was not notified of InUse\n",
311 watchers[i]->exten);
312 res = AST_TEST_FAIL;
313 }
314 }
315
317 for (i = 0; i < ARRAY_LEN(watchers); i++) {
319 ast_test_status_update(test, "Watcher of '%s' was not notified of Idle\n",
320 watchers[i]->exten);
321 res = AST_TEST_FAIL;
322 }
323 }
324 }
325
326 for (i = 0; i < ARRAY_LEN(watchers); i++) {
327 if (added[i] && test_watcher_remove(test, watchers[i])) {
328 res = AST_TEST_FAIL;
329 }
330 }
331
332cleanup:
335 ao2_ref(watchers[0], -1);
336 ao2_ref(watchers[1], -1);
337
338 return res;
339}
340
341AST_TEST_DEFINE(dash_spelling_dashless_first)
342{
343 switch (cmd) {
344 case TEST_INIT:
345 info->name = __func__;
346 info->category = TEST_CATEGORY;
347 info->summary = "Watch a dashed extension after the dashless spelling created the hint";
348 info->description =
349 "Dashes are ignored in extension matching, so '1000x' and '1000-x' are the\n"
350 "same extension. Watching the dashless spelling first creates the hint from a\n"
351 "pattern. Watching the dashed spelling must then also succeed, both watchers\n"
352 "must be notified of state changes of the hint's device, and both must be\n"
353 "destroyed exactly once.";
354 return AST_TEST_NOT_RUN;
355 case TEST_EXECUTE:
356 break;
357 }
358
359 return dash_spelling_check(test, "1000", "x", 0);
360}
361
362AST_TEST_DEFINE(dash_spelling_dashed_first)
363{
364 switch (cmd) {
365 case TEST_INIT:
366 info->name = __func__;
367 info->category = TEST_CATEGORY;
368 info->summary = "Watch a dashless extension after the dashed spelling created the hint";
369 info->description =
370 "Watching the dashed spelling first creates the hint from a pattern. Watching\n"
371 "the dashless spelling must then also succeed, both watchers must be notified\n"
372 "of state changes of the hint's device, and both must be destroyed exactly once.";
373 return AST_TEST_NOT_RUN;
374 case TEST_EXECUTE:
375 break;
376 }
377
378 return dash_spelling_check(test, "2000", "y", 1);
379}
380
381AST_TEST_DEFINE(add_failure_ownership)
382{
383 struct test_watcher *watcher;
385
386 switch (cmd) {
387 case TEST_INIT:
388 info->name = __func__;
389 info->category = TEST_CATEGORY;
390 info->summary = "Failed callback registration leaves data owned by the caller";
391 info->description =
392 "Adding a callback for an extension that does not exist must fail without\n"
393 "invoking the destroy callback, so the caller can release its own data.";
394 return AST_TEST_NOT_RUN;
395 case TEST_EXECUTE:
396 break;
397 }
398
399 watcher = test_watcher_alloc("3000");
400 if (!watcher) {
401 return AST_TEST_FAIL;
402 }
403
404 if (!test_watcher_add(test, "test_extension_state_nonexistent", watcher)) {
405 ast_test_status_update(test, "Callback for nonexistent extension unexpectedly added\n");
406 test_watcher_remove(test, watcher);
407 res = AST_TEST_FAIL;
408 } else {
409 /*
410 * Any destroy callback on the failure path happens synchronously while
411 * the add unwinds, so it has already been counted.
412 */
414 if (watcher->destroyed) {
415 res = AST_TEST_FAIL;
416 }
418 }
419
420 ao2_ref(watcher, -1);
421
422 return res;
423}
424
425static int unload_module(void)
426{
427 AST_TEST_UNREGISTER(dash_spelling_dashless_first);
428 AST_TEST_UNREGISTER(dash_spelling_dashed_first);
429 AST_TEST_UNREGISTER(add_failure_ownership);
431
432 return 0;
433}
434
435static int load_module(void)
436{
438
439 /* Start somewhere other than where an earlier load of this module likely started */
440 test_run = ast_random() % 100000;
441
442 AST_TEST_REGISTER(dash_spelling_dashless_first);
443 AST_TEST_REGISTER(dash_spelling_dashed_first);
444 AST_TEST_REGISTER(add_failure_ownership);
445
447}
448
struct sla_ringing_trunk * first
Definition app_sla.c:338
Asterisk main include file. File version handling, generic pbx functions.
@ AO2_ALLOC_OPT_LOCK_NOLOCK
Definition astobj2.h:367
#define ao2_cleanup(obj)
Definition astobj2.h:1934
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition astobj2.h:459
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition astobj2.h:404
#define AST_MAX_EXTENSION
Definition channel.h:134
Device state management.
@ AST_DEVSTATE_CACHABLE
Definition devicestate.h:70
int ast_device_state_clear_cache(const char *device)
Clear the device from the stasis cache.
int ast_devstate_changed_literal(enum ast_device_state state, enum ast_devstate_cache cachable, const char *device)
Tells Asterisk the State for Device is changed.
@ AST_DEVICE_INUSE
Definition devicestate.h:55
@ AST_DEVICE_NOT_INUSE
Definition devicestate.h:54
Asterisk locking-related definitions:
#define ast_cond_destroy(cond)
Definition lock.h:209
#define ast_cond_init(cond, attr)
Definition lock.h:208
#define ast_cond_timedwait(cond, mutex, time)
Definition lock.h:213
#define ast_mutex_unlock(a)
Definition lock.h:197
#define ast_cond_broadcast(cond)
Definition lock.h:211
pthread_cond_t ast_cond_t
Definition lock.h:185
#define ast_mutex_lock(a)
Definition lock.h:196
#define AST_MUTEX_DEFINE_STATIC(mutex)
Definition lock.h:527
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
Core PBX routines and definitions.
int ast_add_extension(const char *context, int replace, const char *extension, int priority, const char *label, const char *callerid, const char *application, void *data, void(*datad)(void *), const char *registrar)
Add and extension to an extension context.
Definition pbx.c:5206
int ast_context_destroy_by_name(const char *context, const char *registrar)
Destroy a context by name.
Definition pbx.c:6491
int ast_extension_state_add_destroy_extended(const char *context, const char *exten, ast_state_cb_type change_cb, ast_state_cb_destroy_type destroy_cb, void *data)
Add watcher for extended extension states with destructor.
struct ast_context * ast_context_find_or_create(struct ast_context **extcontexts, struct ast_hashtab *exttable, const char *name, const char *registrar)
Register a new context or find an existing one.
Definition pbx.c:4542
ast_extension_states
Extension states.
Definition pbx.h:61
@ AST_EXTENSION_REMOVED
Definition pbx.h:62
@ AST_EXTENSION_NOT_INUSE
Definition pbx.h:64
@ AST_EXTENSION_INUSE
Definition pbx.h:65
int ast_extension_state_del(int id, ast_state_cb_type change_cb)
Deletes a state change watcher by ID.
#define PRIORITY_HINT
Definition pbx.h:54
static void cleanup(void)
Clean up any old apps that we don't need any more.
Definition res_stasis.c:327
#define NULL
Definition resample.c:96
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition strings.h:425
Tracking for a single extension state callback.
enum ast_extension_states last_state
char exten[AST_MAX_EXTENSION]
Test Framework API.
@ TEST_INIT
Definition test.h:200
@ TEST_EXECUTE
Definition test.h:201
#define AST_TEST_REGISTER(cb)
Definition test.h:127
#define ast_test_status_update(a, b, c...)
Definition test.h:129
#define AST_TEST_UNREGISTER(cb)
Definition test.h:128
#define AST_TEST_DEFINE(hdr)
Definition test.h:126
ast_test_result_state
Definition test.h:193
@ AST_TEST_PASS
Definition test.h:195
@ AST_TEST_FAIL
Definition test.h:196
@ AST_TEST_NOT_RUN
Definition test.h:194
#define TEST_REGISTRAR
static int dialplan_setup(void)
#define TEST_DEVICE_PREFIX
Prefix for the devices used by the test hints.
static int test_watcher_remove(struct ast_test *test, struct test_watcher *watcher)
Remove an extension state callback and wait for it to be destroyed.
static unsigned int test_run
Used to make the extensions, and so the devices, differ between test runs.
static int state_changed(const char *context, const char *exten, struct ast_state_cb_info *info, void *data)
static int test_watcher_wait_for_state(struct test_watcher *watcher, enum ast_extension_states state)
Wait until a watcher has received the given extension state.
static void state_destroyed(int id, void *data)
#define TEST_TIMEOUT
How long to wait for asynchronous events, in seconds.
#define TEST_CONTEXT
static int load_module(void)
static void dialplan_cleanup(void)
static ast_mutex_t test_lock
Protects watcher state and test_run.
static int unload_module(void)
static int test_watcher_add(struct ast_test *test, const char *context, struct test_watcher *watcher)
Add an extension state callback for a watcher.
#define TEST_CATEGORY
static struct test_watcher * test_watcher_alloc(const char *exten)
static enum ast_test_result_state dash_spelling_check(struct ast_test *test, const char *base, const char *suffix, int dashed_first)
Watch two spellings of an extension that differ only by dashes.
static ast_cond_t test_cond
Time-related functions and macros.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition time.h:159
Utility functions.
long int ast_random(void)
Definition utils.c:2346
#define ARRAY_LEN(a)
Definition utils.h:706