Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions | Variables
test_res_stasis.c File Reference

Test Stasis Application API.

Author
David M. Lee, II <[email protected]> 
.
More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/stasis_app.h"
Include dependency graph for test_res_stasis.c:

Go to the source code of this file.

Data Structures

struct  app_data
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static struct app_dataapp_data_create (void)
 
static void app_data_dtor (void *obj)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (app_invoke_dne)
 
 AST_TEST_DEFINE (app_invoke_one)
 
 AST_TEST_DEFINE (app_replaced)
 
static int load_module (void)
 
static void test_handler (void *data, const char *app_name, struct ast_json *message)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Stasis Core testing" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_stasis", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static const char * test_category = "/stasis/res/"
 

Detailed Description

Test Stasis Application API.

Author
David M. Lee, II <[email protected]> 
.

Definition in file test_res_stasis.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 201 of file test_res_stasis.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 201 of file test_res_stasis.c.

◆ app_data_create()

static struct app_data* app_data_create ( void  )
static

Definition at line 75 of file test_res_stasis.c.

References ao2_alloc, app_data_dtor(), ast_json_array_create(), app_data::messages, and NULL.

Referenced by AST_TEST_DEFINE().

76 {
77  struct app_data *res = ao2_alloc(sizeof(struct app_data), app_data_dtor);
78 
79  if (!res) {
80  return NULL;
81  }
82 
84  return res;
85 }
static void app_data_dtor(void *obj)
struct ast_json * messages
#define NULL
Definition: resample.c:96
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:352
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411

◆ app_data_dtor()

static void app_data_dtor ( void *  obj)
static

Definition at line 67 of file test_res_stasis.c.

References ast_json_unref(), app_data::messages, and NULL.

Referenced by app_data_create().

68 {
69  struct app_data *actual = obj;
70 
71  ast_json_unref(actual->messages);
72  actual->messages = NULL;
73 }
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
struct ast_json * messages
#define NULL
Definition: resample.c:96

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 201 of file test_res_stasis.c.

◆ AST_TEST_DEFINE() [1/3]

AST_TEST_DEFINE ( app_invoke_dne  )

Definition at line 41 of file test_res_stasis.c.

References ast_json_null(), AST_TEST_NOT_RUN, AST_TEST_PASS, sip_to_pjsip::info(), stasis_app_send(), test_category, TEST_EXECUTE, and TEST_INIT.

42 {
43  int res;
44 
45  switch (cmd) {
46  case TEST_INIT:
47  info->name = __func__;
48  info->category = test_category;
49  info->summary = "Test stasis app invocation.";
50  info->description = "Test stasis app invocation.";
51  return AST_TEST_NOT_RUN;
52  case TEST_EXECUTE:
53  break;
54  }
55 
56  res = stasis_app_send("i-am-not-an-app", ast_json_null());
57  ast_test_validate(test, -1 == res);
58 
59  return AST_TEST_PASS;
60 }
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
static const char * test_category
def info(msg)
int stasis_app_send(const char *app_name, struct ast_json *message)
Send a message to the given Stasis application.
Definition: res_stasis.c:1656

◆ AST_TEST_DEFINE() [2/3]

AST_TEST_DEFINE ( app_invoke_one  )

Definition at line 96 of file test_res_stasis.c.

References ao2_cleanup, app_data_create(), app_name(), ast_json_equal(), ast_json_null(), ast_json_pack(), ast_json_ref(), ast_json_unref(), AST_TEST_NOT_RUN, AST_TEST_PASS, sip_to_pjsip::info(), app_data::invocations, app_data::messages, NULL, RAII_VAR, stasis_app_register(), stasis_app_send(), stasis_app_unregister(), test_category, TEST_EXECUTE, test_handler(), and TEST_INIT.

97 {
100  RAII_VAR(struct ast_json *, expected_message, NULL, ast_json_unref);
102  int res;
103 
104  switch (cmd) {
105  case TEST_INIT:
106  info->name = __func__;
107  info->category = test_category;
108  info->summary = "Test stasis app invocation.";
109  info->description = "Test stasis app invocation.";
110  return AST_TEST_NOT_RUN;
111  case TEST_EXECUTE:
112  break;
113  }
114 
115  app_name = "test-handler";
116 
118 
120  message = ast_json_pack("{ s: o }", "test-message", ast_json_null());
121  expected_message = ast_json_pack("[o]", ast_json_ref(message));
122 
124  ast_test_validate(test, 0 == res);
125  ast_test_validate(test, 1 == app_data->invocations);
126  ast_test_validate(test, ast_json_equal(expected_message, app_data->messages));
127 
128  return AST_TEST_PASS;
129 }
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
struct ast_json * messages
#define NULL
Definition: resample.c:96
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
static const char * test_category
void stasis_app_unregister(const char *app_name)
Unregister a Stasis application.
Definition: res_stasis.c:1787
#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 void test_handler(void *data, const char *app_name, struct ast_json *message)
def info(msg)
const char * app_name(struct ast_app *app)
Definition: pbx_app.c:463
int stasis_app_register(const char *app_name, stasis_app_cb handler, void *data)
Register a new Stasis application.
Definition: res_stasis.c:1777
static struct app_data * app_data_create(void)
int stasis_app_send(const char *app_name, struct ast_json *message)
Send a message to the given Stasis application.
Definition: res_stasis.c:1656
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
Compare two JSON objects.
Definition: json.c:347
Abstract JSON element (object, array, string, int, ...).

◆ AST_TEST_DEFINE() [3/3]

AST_TEST_DEFINE ( app_replaced  )

Definition at line 131 of file test_res_stasis.c.

References ao2_cleanup, app_data_create(), app_name(), ast_eid_default, ast_eid_to_str(), ast_json_array_get(), ast_json_equal(), ast_json_null(), ast_json_object_del(), ast_json_object_get(), ast_json_pack(), ast_json_ref(), ast_json_unref(), AST_TEST_NOT_RUN, AST_TEST_PASS, sip_to_pjsip::info(), NULL, RAII_VAR, stasis_app_register(), stasis_app_send(), stasis_app_unregister(), test_category, TEST_EXECUTE, test_handler(), and TEST_INIT.

132 {
133  RAII_VAR(struct app_data *, app_data1, NULL, ao2_cleanup);
134  RAII_VAR(struct app_data *, app_data2, NULL, ao2_cleanup);
136  RAII_VAR(struct ast_json *, expected_message1, NULL, ast_json_unref);
138  RAII_VAR(struct ast_json *, expected_message2, NULL, ast_json_unref);
139  char eid[20];
140  int res;
141 
142  switch (cmd) {
143  case TEST_INIT:
144  info->name = __func__;
145  info->category = test_category;
146  info->summary = "Test stasis app invocation.";
147  info->description = "Test stasis app invocation.";
148  return AST_TEST_NOT_RUN;
149  case TEST_EXECUTE:
150  break;
151  }
152 
153  app_name = "test-handler";
154 
155  app_data1 = app_data_create();
156  app_data2 = app_data_create();
157 
160  expected_message1 = ast_json_pack("[{s: s, s: s, s: s}]",
161  "type", "ApplicationReplaced",
162  "application", app_name,
163  "asterisk_id", ast_eid_to_str(eid, sizeof(eid), &ast_eid_default));
164  message = ast_json_pack("{ s: o }", "test-message", ast_json_null());
165  expected_message2 = ast_json_pack("[o]", ast_json_ref(message));
166 
168  ast_test_validate(test, 0 == res);
169  ast_test_validate(test, 1 == app_data1->invocations);
170  ast_test_validate(test, ast_json_object_get(ast_json_array_get(app_data1->messages, 0), "timestamp")? 1: 0);
171  ast_json_object_del(ast_json_array_get(app_data1->messages, 0), "timestamp");
172  ast_test_validate(test, ast_json_equal(expected_message1, app_data1->messages));
173 
174  ast_test_validate(test, 1 == app_data2->invocations);
175  ast_test_validate(test, ast_json_equal(expected_message2, app_data2->messages));
176 
177  return AST_TEST_PASS;
178 }
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
Definition: json.c:591
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
Definition: main/utils.c:2587
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
int ast_json_object_del(struct ast_json *object, const char *key)
Delete a field from a JSON object.
Definition: json.c:408
#define NULL
Definition: resample.c:96
struct ast_json * ast_json_null(void)
Get the JSON null value.
Definition: json.c:248
static const char * test_category
void stasis_app_unregister(const char *app_name)
Unregister a Stasis application.
Definition: res_stasis.c:1787
#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 void test_handler(void *data, const char *app_name, struct ast_json *message)
def info(msg)
const char * app_name(struct ast_app *app)
Definition: pbx_app.c:463
int stasis_app_register(const char *app_name, stasis_app_cb handler, void *data)
Register a new Stasis application.
Definition: res_stasis.c:1777
static struct app_data * app_data_create(void)
int stasis_app_send(const char *app_name, struct ast_json *message)
Send a message to the given Stasis application.
Definition: res_stasis.c:1656
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
Compare two JSON objects.
Definition: json.c:347
Abstract JSON element (object, array, string, int, ...).
struct ast_json * ast_json_array_get(const struct ast_json *array, size_t index)
Get an element from an array.
Definition: json.c:360

◆ load_module()

static int load_module ( void  )
static

Definition at line 188 of file test_res_stasis.c.

References AST_MODFLAG_DEFAULT, AST_MODULE_INFO(), AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_CORE, AST_TEST_REGISTER, ASTERISK_GPL_KEY, and unload_module().

189 {
190  AST_TEST_REGISTER(app_replaced);
191  AST_TEST_REGISTER(app_invoke_one);
192  AST_TEST_REGISTER(app_invoke_dne);
194 }
#define AST_TEST_REGISTER(cb)
Definition: test.h:127

◆ test_handler()

static void test_handler ( void *  data,
const char *  app_name,
struct ast_json message 
)
static

Definition at line 87 of file test_res_stasis.c.

References ast_assert, ast_json_array_append(), ast_json_copy(), app_data::invocations, and app_data::messages.

Referenced by AST_TEST_DEFINE().

88 {
89  struct app_data *actual = data;
90  int res;
91  ++(actual->invocations);
92  res = ast_json_array_append(actual->messages, ast_json_copy(message));
93  ast_assert(res == 0);
94 }
struct ast_json * messages
#define ast_assert(a)
Definition: utils.h:695
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:368
struct ast_json * ast_json_copy(const struct ast_json *value)
Copy a JSON value, but not its children.
Definition: json.c:616

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 180 of file test_res_stasis.c.

References AST_TEST_UNREGISTER.

Referenced by load_module().

181 {
182  AST_TEST_UNREGISTER(app_invoke_dne);
183  AST_TEST_UNREGISTER(app_invoke_one);
184  AST_TEST_UNREGISTER(app_replaced);
185  return 0;
186 }
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Stasis Core testing" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_stasis", }
static

Definition at line 201 of file test_res_stasis.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 201 of file test_res_stasis.c.

◆ test_category

const char* test_category = "/stasis/res/"
static

Definition at line 39 of file test_res_stasis.c.

Referenced by AST_TEST_DEFINE().