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

Sorcery Unit Tests. More...

#include "asterisk.h"
#include "asterisk/test.h"
#include "asterisk/module.h"
#include "asterisk/sorcery.h"
#include "asterisk/astdb.h"
#include "asterisk/logger.h"
Include dependency graph for test_sorcery_astdb.c:

Go to the source code of this file.

Data Structures

struct  test_sorcery_object
 Dummy sorcery object. More...
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static struct ast_sorceryalloc_and_initialize_sorcery (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (object_create)
 
 AST_TEST_DEFINE (object_retrieve_id)
 
 AST_TEST_DEFINE (object_retrieve_field)
 
 AST_TEST_DEFINE (object_retrieve_multiple_all)
 
 AST_TEST_DEFINE (object_retrieve_multiple_field)
 
 AST_TEST_DEFINE (object_retrieve_regex)
 
 AST_TEST_DEFINE (object_update)
 
 AST_TEST_DEFINE (object_update_uncreated)
 
 AST_TEST_DEFINE (object_delete)
 
 AST_TEST_DEFINE (object_delete_uncreated)
 
static void deinitialize_sorcery (struct ast_sorcery *sorcery)
 
static int load_module (void)
 
static void * test_sorcery_object_alloc (const char *id)
 Internal function to allocate a test object. More...
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Sorcery astdb Wizard test module" , .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, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

Sorcery Unit Tests.

Author
Joshua Colp jcolp.nosp@m.@dig.nosp@m.ium.c.nosp@m.om

Definition in file test_sorcery_astdb.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 636 of file test_sorcery_astdb.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 636 of file test_sorcery_astdb.c.

◆ alloc_and_initialize_sorcery()

static struct ast_sorcery* alloc_and_initialize_sorcery ( void  )
static

Definition at line 53 of file test_sorcery_astdb.c.

References ast_sorcery_apply_default, AST_SORCERY_APPLY_SUCCESS, ast_sorcery_internal_object_register, ast_sorcery_object_field_register_nodoc, ast_sorcery_open, ast_sorcery_unref, test_sorcery_object::bob, FLDSET, test_sorcery_object::joe, NULL, OPT_UINT_T, sorcery, and test_sorcery_object_alloc().

Referenced by AST_TEST_DEFINE().

54 {
55  struct ast_sorcery *sorcery;
56 
57  if (!(sorcery = ast_sorcery_open())) {
58  return NULL;
59  }
60 
61  if ((ast_sorcery_apply_default(sorcery, "test", "astdb", "test") != AST_SORCERY_APPLY_SUCCESS) ||
63  ast_sorcery_unref(sorcery);
64  return NULL;
65  }
66 
67  ast_sorcery_object_field_register_nodoc(sorcery, "test", "bob", "5", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, bob));
68  ast_sorcery_object_field_register_nodoc(sorcery, "test", "joe", "10", OPT_UINT_T, 0, FLDSET(struct test_sorcery_object, joe));
69 
70  return sorcery;
71 }
#define ast_sorcery_object_field_register_nodoc(sorcery, type, name, default_val, opt_type, flags,...)
Register a field within an object without documentation.
Definition: sorcery.h:987
Dummy sorcery object.
static void * test_sorcery_object_alloc(const char *id)
Internal function to allocate a test object.
Full structure for sorcery.
Definition: sorcery.c:230
#define NULL
Definition: resample.c:96
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition: sorcery.h:1502
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
Type for default option handler for unsigned integers.
#define ast_sorcery_internal_object_register(sorcery, type, alloc, transform, apply)
Register an internal, hidden object type.
Definition: sorcery.h:868
#define ast_sorcery_apply_default(sorcery, type, name, data)
Definition: sorcery.h:477
static struct ast_sorcery * sorcery
#define ast_sorcery_open()
Definition: sorcery.h:408

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 636 of file test_sorcery_astdb.c.

◆ AST_TEST_DEFINE() [1/10]

AST_TEST_DEFINE ( object_create  )

Definition at line 79 of file test_sorcery_astdb.c.

References alloc_and_initialize_sorcery(), ao2_cleanup, ast_db_get(), ast_sorcery_alloc(), ast_sorcery_create(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, deinitialize_sorcery(), sip_to_pjsip::info(), NULL, RAII_VAR, sorcery, TEST_EXECUTE, TEST_INIT, and value.

80 {
83  char value[2];
84 
85  switch (cmd) {
86  case TEST_INIT:
87  info->name = "object_create";
88  info->category = "/res/sorcery_astdb/";
89  info->summary = "sorcery astdb object creation unit test";
90  info->description =
91  "Test object creation in sorcery using astdb wizard";
92  return AST_TEST_NOT_RUN;
93  case TEST_EXECUTE:
94  break;
95  }
96 
98  ast_test_status_update(test, "Failed to open sorcery structure\n");
99  return AST_TEST_FAIL;
100  }
101 
102  if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
103  ast_test_status_update(test, "Failed to allocate a known object type\n");
104  return AST_TEST_FAIL;
105  }
106 
107  if (ast_sorcery_create(sorcery, obj)) {
108  ast_test_status_update(test, "Failed to create object using astdb wizard\n");
109  return AST_TEST_FAIL;
110  } else if (ast_db_get("test/test", "blah", value, sizeof(value))) {
111  ast_test_status_update(test, "Object was apparently created but does not actually exist in astdb\n");
112  return AST_TEST_FAIL;
113  }
114 
115  return AST_TEST_PASS;
116 }
Dummy sorcery object.
Full structure for sorcery.
Definition: sorcery.c:230
static struct ast_sorcery * alloc_and_initialize_sorcery(void)
#define NULL
Definition: resample.c:96
int value
Definition: syslog.c:37
int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
Create and potentially persist an object using an available wizard.
Definition: sorcery.c:2057
#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
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
def info(msg)
static void deinitialize_sorcery(struct ast_sorcery *sorcery)
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: main/db.c:412
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ AST_TEST_DEFINE() [2/10]

AST_TEST_DEFINE ( object_retrieve_id  )

Definition at line 118 of file test_sorcery_astdb.c.

References alloc_and_initialize_sorcery(), ao2_cleanup, ast_sorcery_alloc(), ast_sorcery_create(), ast_sorcery_object_get_id(), ast_sorcery_retrieve_by_id(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, deinitialize_sorcery(), sip_to_pjsip::info(), NULL, RAII_VAR, sorcery, TEST_EXECUTE, and TEST_INIT.

119 {
121  RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
122 
123  switch (cmd) {
124  case TEST_INIT:
125  info->name = "object_retrieve_id";
126  info->category = "/res/sorcery_astdb/";
127  info->summary = "sorcery object retrieval using id unit test";
128  info->description =
129  "Test object retrieval using id in sorcery with astdb wizard";
130  return AST_TEST_NOT_RUN;
131  case TEST_EXECUTE:
132  break;
133  }
134 
136  ast_test_status_update(test, "Failed to open sorcery structure\n");
137  return AST_TEST_FAIL;
138  }
139 
140  if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
141  ast_test_status_update(test, "Failed to allocate a known object type\n");
142  return AST_TEST_FAIL;
143  }
144 
145  if (ast_sorcery_create(sorcery, obj)) {
146  ast_test_status_update(test, "Failed to create object using astdb wizard\n");
147  return AST_TEST_FAIL;
148  }
149 
150  ao2_cleanup(obj);
151 
152  if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah2"))) {
153  ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
154  return AST_TEST_FAIL;
155  }
156 
157  if (ast_sorcery_create(sorcery, obj)) {
158  ast_test_status_update(test, "Failed to create second object using astdb wizard\n");
159  return AST_TEST_FAIL;
160  }
161 
162  ao2_cleanup(obj);
163 
164  if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
165  ast_test_status_update(test, "Failed to retrieve properly created object using id of 'blah'\n");
166  return AST_TEST_FAIL;
167  } else if (strcmp(ast_sorcery_object_get_id(obj), "blah")) {
168  ast_test_status_update(test, "Retrieved object does not have correct id\n");
169  return AST_TEST_FAIL;
170  }
171 
172  return AST_TEST_PASS;
173 }
Dummy sorcery object.
Full structure for sorcery.
Definition: sorcery.c:230
static struct ast_sorcery * alloc_and_initialize_sorcery(void)
#define NULL
Definition: resample.c:96
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
Create and potentially persist an object using an available wizard.
Definition: sorcery.c:2057
#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
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
def info(msg)
static void deinitialize_sorcery(struct ast_sorcery *sorcery)
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ AST_TEST_DEFINE() [3/10]

AST_TEST_DEFINE ( object_retrieve_field  )

Definition at line 175 of file test_sorcery_astdb.c.

References alloc_and_initialize_sorcery(), ao2_cleanup, AST_RETRIEVE_FLAG_DEFAULT, ast_sorcery_alloc(), ast_sorcery_create(), ast_sorcery_retrieve_by_fields(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, ast_variable_new, ast_variables_destroy(), deinitialize_sorcery(), sip_to_pjsip::info(), NULL, RAII_VAR, sorcery, TEST_EXECUTE, and TEST_INIT.

176 {
178  RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
179  RAII_VAR(struct ast_variable *, fields, ast_variable_new("joe", "42", ""), ast_variables_destroy);
180 
181  switch (cmd) {
182  case TEST_INIT:
183  info->name = "object_retrieve_field";
184  info->category = "/res/sorcery_astdb/";
185  info->summary = "sorcery object retrieval using a specific field unit test";
186  info->description =
187  "Test object retrieval using a specific field in sorcery with astdb wizard";
188  return AST_TEST_NOT_RUN;
189  case TEST_EXECUTE:
190  break;
191  }
192 
193  if (!fields) {
194  ast_test_status_update(test, "Failed to create fields for object retrieval attempt\n");
195  return AST_TEST_FAIL;
196  }
197 
199  ast_test_status_update(test, "Failed to open sorcery structure\n");
200  return AST_TEST_FAIL;
201  }
202 
203  if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
204  ast_test_status_update(test, "Failed to allocate a known object type\n");
205  return AST_TEST_FAIL;
206  }
207 
208  obj->joe = 42;
209 
210  if (ast_sorcery_create(sorcery, obj)) {
211  ast_test_status_update(test, "Failed to create object using astdb wizard\n");
212  return AST_TEST_FAIL;
213  }
214 
215  ao2_cleanup(obj);
216 
217  if (!(obj = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_DEFAULT, fields))) {
218  ast_test_status_update(test, "Failed to retrieve properly created object using 'joe' field\n");
219  return AST_TEST_FAIL;
220  }
221 
222  ao2_cleanup(obj);
223  ast_variables_destroy(fields);
224 
225  if (!(fields = ast_variable_new("joe", "49", ""))) {
226  ast_test_status_update(test, "Failed to create fields for object retrieval attempt\n");
227  return AST_TEST_FAIL;
228  }
229 
230  if ((obj = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_DEFAULT, fields))) {
231  ast_test_status_update(test, "Retrieved an object using a field with an in-correct value... that should not happen\n");
232  return AST_TEST_FAIL;
233  }
234 
235  return AST_TEST_PASS;
236 }
Dummy sorcery object.
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1263
Structure for variables, used for configurations and for channel variables.
Full structure for sorcery.
Definition: sorcery.c:230
static struct ast_sorcery * alloc_and_initialize_sorcery(void)
#define NULL
Definition: resample.c:96
int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
Create and potentially persist an object using an available wizard.
Definition: sorcery.c:2057
#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
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
#define ast_variable_new(name, value, filename)
Default retrieval flags.
Definition: sorcery.h:117
def info(msg)
static void deinitialize_sorcery(struct ast_sorcery *sorcery)
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
void * ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
Retrieve an object or multiple objects using specific fields.
Definition: sorcery.c:1897
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ AST_TEST_DEFINE() [4/10]

AST_TEST_DEFINE ( object_retrieve_multiple_all  )

Definition at line 238 of file test_sorcery_astdb.c.

References alloc_and_initialize_sorcery(), ao2_cleanup, ao2_container_count(), AST_RETRIEVE_FLAG_ALL, AST_RETRIEVE_FLAG_MULTIPLE, ast_sorcery_alloc(), ast_sorcery_create(), ast_sorcery_retrieve_by_fields(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, deinitialize_sorcery(), sip_to_pjsip::info(), NULL, RAII_VAR, sorcery, TEST_EXECUTE, and TEST_INIT.

239 {
241  RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
242  RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
243 
244  switch (cmd) {
245  case TEST_INIT:
246  info->name = "object_retrieve_multiple_all";
247  info->category = "/res/sorcery_astdb/";
248  info->summary = "sorcery multiple object retrieval unit test";
249  info->description =
250  "Test multiple object retrieval in sorcery using astdb wizard";
251  return AST_TEST_NOT_RUN;
252  case TEST_EXECUTE:
253  break;
254  }
255 
257  ast_test_status_update(test, "Failed to open sorcery structure\n");
258  return AST_TEST_FAIL;
259  }
260 
261  if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
262  ast_test_status_update(test, "Failed to allocate a known object type\n");
263  return AST_TEST_FAIL;
264  }
265 
266  if (ast_sorcery_create(sorcery, obj)) {
267  ast_test_status_update(test, "Failed to create object using astdb wizard\n");
268  return AST_TEST_FAIL;
269  }
270 
271  ao2_cleanup(obj);
272 
273  if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah2"))) {
274  ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
275  return AST_TEST_FAIL;
276  }
277 
278  if (ast_sorcery_create(sorcery, obj)) {
279  ast_test_status_update(test, "Failed to create second object using astdb wizard\n");
280  return AST_TEST_FAIL;
281  }
282 
284  ast_test_status_update(test, "Failed to retrieve a container of all objects\n");
285  return AST_TEST_FAIL;
286  } else if (ao2_container_count(objects) != 2) {
287  ast_test_status_update(test, "Received a container with no objects in it when there should be some\n");
288  return AST_TEST_FAIL;
289  }
290 
291  return AST_TEST_PASS;
292 }
Dummy sorcery object.
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
Perform no matching, return all objects.
Definition: sorcery.h:123
Full structure for sorcery.
Definition: sorcery.c:230
static struct ast_sorcery * alloc_and_initialize_sorcery(void)
Return all matching objects.
Definition: sorcery.h:120
#define NULL
Definition: resample.c:96
int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
Create and potentially persist an object using an available wizard.
Definition: sorcery.c:2057
#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
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
def info(msg)
static void deinitialize_sorcery(struct ast_sorcery *sorcery)
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
void * ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
Retrieve an object or multiple objects using specific fields.
Definition: sorcery.c:1897
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
Generic container type.

◆ AST_TEST_DEFINE() [5/10]

AST_TEST_DEFINE ( object_retrieve_multiple_field  )

Definition at line 294 of file test_sorcery_astdb.c.

References alloc_and_initialize_sorcery(), ao2_cleanup, ao2_container_count(), AST_RETRIEVE_FLAG_MULTIPLE, ast_sorcery_alloc(), ast_sorcery_create(), ast_sorcery_retrieve_by_fields(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, ast_variable_new, ast_variables_destroy(), deinitialize_sorcery(), sip_to_pjsip::info(), NULL, RAII_VAR, sorcery, TEST_EXECUTE, and TEST_INIT.

295 {
297  RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
298  RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
299  RAII_VAR(struct ast_variable *, fields, ast_variable_new("joe >=", "6", ""), ast_variables_destroy);
300 
301  switch (cmd) {
302  case TEST_INIT:
303  info->name = "object_retrieve_multiple_field";
304  info->category = "/res/sorcery_astdb/";
305  info->summary = "sorcery multiple object retrieval unit test";
306  info->description =
307  "Test multiple object retrieval in sorcery using fields using astdb wizard";
308  return AST_TEST_NOT_RUN;
309  case TEST_EXECUTE:
310  break;
311  }
312 
313  if (!fields) {
314  ast_test_status_update(test, "Failed to create fields for multiple retrieve\n");
315  return AST_TEST_FAIL;
316  }
317 
319  ast_test_status_update(test, "Failed to open sorcery structure\n");
320  return AST_TEST_FAIL;
321  }
322 
323  if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
324  ast_test_status_update(test, "Failed to allocate a known object type\n");
325  return AST_TEST_FAIL;
326  }
327 
328  obj->joe = 6;
329 
330  if (ast_sorcery_create(sorcery, obj)) {
331  ast_test_status_update(test, "Failed to create object using astdb wizard\n");
332  return AST_TEST_FAIL;
333  }
334 
335  if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, fields))) {
336  ast_test_status_update(test, "Failed to retrieve a container of all objects\n");
337  return AST_TEST_FAIL;
338  } else if (ao2_container_count(objects) != 1) {
339  ast_test_status_update(test, "Received a container with no objects in it when there should be some\n");
340  return AST_TEST_FAIL;
341  }
342 
343  ao2_cleanup(objects);
344  ast_variables_destroy(fields);
345 
346  if (!(fields = ast_variable_new("joe <", "6", ""))) {
347  ast_test_status_update(test, "Failed to create fields for multiple retrieval\n");
348  return AST_TEST_FAIL;
349  } else if (!(objects = ast_sorcery_retrieve_by_fields(sorcery, "test", AST_RETRIEVE_FLAG_MULTIPLE, fields))) {
350  ast_test_status_update(test, "Failed to retrieve an empty container when retrieving multiple\n");
351  return AST_TEST_FAIL;
352  } else if (ao2_container_count(objects)) {
353  ast_test_status_update(test, "Received a container with objects when there should be none in it\n");
354  return AST_TEST_FAIL;
355  }
356 
357  return AST_TEST_PASS;
358 }
Dummy sorcery object.
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1263
Structure for variables, used for configurations and for channel variables.
Full structure for sorcery.
Definition: sorcery.c:230
static struct ast_sorcery * alloc_and_initialize_sorcery(void)
Return all matching objects.
Definition: sorcery.h:120
#define NULL
Definition: resample.c:96
int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
Create and potentially persist an object using an available wizard.
Definition: sorcery.c:2057
#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
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
#define ast_variable_new(name, value, filename)
def info(msg)
static void deinitialize_sorcery(struct ast_sorcery *sorcery)
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
void * ast_sorcery_retrieve_by_fields(const struct ast_sorcery *sorcery, const char *type, unsigned int flags, struct ast_variable *fields)
Retrieve an object or multiple objects using specific fields.
Definition: sorcery.c:1897
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
Generic container type.

◆ AST_TEST_DEFINE() [6/10]

AST_TEST_DEFINE ( object_retrieve_regex  )

Definition at line 360 of file test_sorcery_astdb.c.

References alloc_and_initialize_sorcery(), ao2_cleanup, ao2_container_count(), ast_sorcery_alloc(), ast_sorcery_create(), ast_sorcery_retrieve_by_regex(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, deinitialize_sorcery(), sip_to_pjsip::info(), NULL, RAII_VAR, sorcery, TEST_EXECUTE, and TEST_INIT.

361 {
363  RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
364  RAII_VAR(struct ao2_container *, objects, NULL, ao2_cleanup);
365 
366  switch (cmd) {
367  case TEST_INIT:
368  info->name = "object_retrieve_regex";
369  info->category = "/res/sorcery_astdb/";
370  info->summary = "sorcery multiple object retrieval using regex unit test";
371  info->description =
372  "Test multiple object retrieval in sorcery using regular expression for matching using astdb wizard";
373  return AST_TEST_NOT_RUN;
374  case TEST_EXECUTE:
375  break;
376  }
377 
379  ast_test_status_update(test, "Failed to open sorcery structure\n");
380  return AST_TEST_FAIL;
381  }
382 
383  if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah-98joe"))) {
384  ast_test_status_update(test, "Failed to allocate a known object type\n");
385  return AST_TEST_FAIL;
386  }
387 
388  if (ast_sorcery_create(sorcery, obj)) {
389  ast_test_status_update(test, "Failed to create object using astdb wizard\n");
390  return AST_TEST_FAIL;
391  }
392 
393  ao2_cleanup(obj);
394 
395  if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah-93joe"))) {
396  ast_test_status_update(test, "Failed to allocate second instance of a known object type\n");
397  return AST_TEST_FAIL;
398  }
399 
400  if (ast_sorcery_create(sorcery, obj)) {
401  ast_test_status_update(test, "Failed to create second object using astdb wizard\n");
402  return AST_TEST_FAIL;
403  }
404 
405  ao2_cleanup(obj);
406 
407  if (!(obj = ast_sorcery_alloc(sorcery, "test", "neener-93joe"))) {
408  ast_test_status_update(test, "Failed to allocate third instance of a known object type\n");
409  return AST_TEST_FAIL;
410  }
411 
412  if (ast_sorcery_create(sorcery, obj)) {
413  ast_test_status_update(test, "Failed to create third object using astdb wizard\n");
414  return AST_TEST_FAIL;
415  }
416 
417  if (!(objects = ast_sorcery_retrieve_by_regex(sorcery, "test", "^blah-"))) {
418  ast_test_status_update(test, "Failed to retrieve a container of objects\n");
419  return AST_TEST_FAIL;
420  } else if (ao2_container_count(objects) != 2) {
421  ast_test_status_update(test, "Received a container with incorrect number of objects in it\n");
422  return AST_TEST_FAIL;
423  }
424 
425  return AST_TEST_PASS;
426 }
Dummy sorcery object.
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
Full structure for sorcery.
Definition: sorcery.c:230
static struct ast_sorcery * alloc_and_initialize_sorcery(void)
#define NULL
Definition: resample.c:96
struct ao2_container * ast_sorcery_retrieve_by_regex(const struct ast_sorcery *sorcery, const char *type, const char *regex)
Retrieve multiple objects using a regular expression on their id.
Definition: sorcery.c:1949
int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
Create and potentially persist an object using an available wizard.
Definition: sorcery.c:2057
#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
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
def info(msg)
static void deinitialize_sorcery(struct ast_sorcery *sorcery)
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
Generic container type.

◆ AST_TEST_DEFINE() [7/10]

AST_TEST_DEFINE ( object_update  )

Definition at line 428 of file test_sorcery_astdb.c.

References alloc_and_initialize_sorcery(), ao2_cleanup, ast_sorcery_alloc(), ast_sorcery_copy(), ast_sorcery_create(), ast_sorcery_retrieve_by_id(), ast_sorcery_update(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, deinitialize_sorcery(), sip_to_pjsip::info(), NULL, RAII_VAR, sorcery, TEST_EXECUTE, and TEST_INIT.

429 {
431  RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
432  RAII_VAR(struct test_sorcery_object *, obj2, NULL, ao2_cleanup);
433 
434  switch (cmd) {
435  case TEST_INIT:
436  info->name = "object_update";
437  info->category = "/res/sorcery_astdb/";
438  info->summary = "sorcery object update unit test";
439  info->description =
440  "Test object updating in sorcery using astdb wizard";
441  return AST_TEST_NOT_RUN;
442  case TEST_EXECUTE:
443  break;
444  }
445 
447  ast_test_status_update(test, "Failed to open sorcery structure\n");
448  return AST_TEST_FAIL;
449  }
450 
451  if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
452  ast_test_status_update(test, "Failed to allocate a known object type\n");
453  return AST_TEST_FAIL;
454  }
455 
456  if (ast_sorcery_create(sorcery, obj)) {
457  ast_test_status_update(test, "Failed to create object using astdb wizard\n");
458  return AST_TEST_FAIL;
459  }
460 
461  if (!(obj2 = ast_sorcery_copy(sorcery, obj))) {
462  ast_test_status_update(test, "Failed to allocate a known object type for updating\n");
463  return AST_TEST_FAIL;
464  }
465 
466  ao2_cleanup(obj);
467 
468  obj2->bob = 1000;
469  obj2->joe = 2000;
470 
471  if (ast_sorcery_update(sorcery, obj2)) {
472  ast_test_status_update(test, "Failed to update sorcery with new object\n");
473  return AST_TEST_FAIL;
474  }
475 
476  if (!(obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
477  ast_test_status_update(test, "Failed to retrieve properly updated object\n");
478  return AST_TEST_FAIL;
479  } else if ((obj->bob != obj2->bob) || (obj->joe != obj2->joe)) {
480  ast_test_status_update(test, "Object retrieved is not the updated object\n");
481  return AST_TEST_FAIL;
482  }
483 
484  return AST_TEST_PASS;
485 }
Dummy sorcery object.
Full structure for sorcery.
Definition: sorcery.c:230
static struct ast_sorcery * alloc_and_initialize_sorcery(void)
#define NULL
Definition: resample.c:96
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
Create and potentially persist an object using an available wizard.
Definition: sorcery.c:2057
#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
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
def info(msg)
static void deinitialize_sorcery(struct ast_sorcery *sorcery)
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
void * ast_sorcery_copy(const struct ast_sorcery *sorcery, const void *object)
Create a copy of an object.
Definition: sorcery.c:1778
int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
Update an object.
Definition: sorcery.c:2145

◆ AST_TEST_DEFINE() [8/10]

AST_TEST_DEFINE ( object_update_uncreated  )

Definition at line 487 of file test_sorcery_astdb.c.

References alloc_and_initialize_sorcery(), ao2_cleanup, ast_sorcery_alloc(), ast_sorcery_update(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, deinitialize_sorcery(), sip_to_pjsip::info(), NULL, RAII_VAR, sorcery, TEST_EXECUTE, and TEST_INIT.

488 {
490  RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
491 
492  switch (cmd) {
493  case TEST_INIT:
494  info->name = "object_update_uncreated";
495  info->category = "/res/sorcery_astdb/";
496  info->summary = "sorcery object update unit test";
497  info->description =
498  "Test updating of an uncreated object in sorcery using astdb wizard";
499  return AST_TEST_NOT_RUN;
500  case TEST_EXECUTE:
501  break;
502  }
503 
505  ast_test_status_update(test, "Failed to open sorcery structure\n");
506  return AST_TEST_FAIL;
507  }
508 
509  if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
510  ast_test_status_update(test, "Failed to allocate a known object type\n");
511  return AST_TEST_FAIL;
512  }
513 
514  if (!ast_sorcery_update(sorcery, obj)) {
515  ast_test_status_update(test, "Successfully updated an object which has not been created yet\n");
516  return AST_TEST_FAIL;
517  }
518 
519  return AST_TEST_PASS;
520 }
Dummy sorcery object.
Full structure for sorcery.
Definition: sorcery.c:230
static struct ast_sorcery * alloc_and_initialize_sorcery(void)
#define NULL
Definition: resample.c:96
#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
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
def info(msg)
static void deinitialize_sorcery(struct ast_sorcery *sorcery)
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
int ast_sorcery_update(const struct ast_sorcery *sorcery, void *object)
Update an object.
Definition: sorcery.c:2145

◆ AST_TEST_DEFINE() [9/10]

AST_TEST_DEFINE ( object_delete  )

Definition at line 522 of file test_sorcery_astdb.c.

References alloc_and_initialize_sorcery(), ao2_cleanup, ast_sorcery_alloc(), ast_sorcery_create(), ast_sorcery_delete(), ast_sorcery_retrieve_by_id(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, deinitialize_sorcery(), sip_to_pjsip::info(), NULL, RAII_VAR, sorcery, TEST_EXECUTE, and TEST_INIT.

523 {
525  RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
526 
527  switch (cmd) {
528  case TEST_INIT:
529  info->name = "object_delete";
530  info->category = "/res/sorcery_astdb/";
531  info->summary = "sorcery object deletion unit test";
532  info->description =
533  "Test object deletion in sorcery using astdb wizard";
534  return AST_TEST_NOT_RUN;
535  case TEST_EXECUTE:
536  break;
537  }
538 
540  ast_test_status_update(test, "Failed to open sorcery structure\n");
541  return AST_TEST_FAIL;
542  }
543 
544  if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
545  ast_test_status_update(test, "Failed to allocate a known object type\n");
546  return AST_TEST_FAIL;
547  }
548 
549  if (ast_sorcery_create(sorcery, obj)) {
550  ast_test_status_update(test, "Failed to create object using astdb wizard\n");
551  return AST_TEST_FAIL;
552  }
553 
554  if (ast_sorcery_delete(sorcery, obj)) {
555  ast_test_status_update(test, "Failed to delete object using astdb wizard\n");
556  return AST_TEST_FAIL;
557  }
558 
559  ao2_cleanup(obj);
560 
561  if ((obj = ast_sorcery_retrieve_by_id(sorcery, "test", "blah"))) {
562  ast_test_status_update(test, "Retrieved deleted object that should not be there\n");
563  return AST_TEST_FAIL;
564  }
565 
566  return AST_TEST_PASS;
567 }
Dummy sorcery object.
Full structure for sorcery.
Definition: sorcery.c:230
static struct ast_sorcery * alloc_and_initialize_sorcery(void)
#define NULL
Definition: resample.c:96
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1853
int ast_sorcery_create(const struct ast_sorcery *sorcery, void *object)
Create and potentially persist an object using an available wizard.
Definition: sorcery.c:2057
#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
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
Delete an object.
Definition: sorcery.c:2233
def info(msg)
static void deinitialize_sorcery(struct ast_sorcery *sorcery)
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ AST_TEST_DEFINE() [10/10]

AST_TEST_DEFINE ( object_delete_uncreated  )

Definition at line 569 of file test_sorcery_astdb.c.

References alloc_and_initialize_sorcery(), ao2_cleanup, ast_sorcery_alloc(), ast_sorcery_delete(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, deinitialize_sorcery(), sip_to_pjsip::info(), NULL, RAII_VAR, sorcery, TEST_EXECUTE, and TEST_INIT.

570 {
572  RAII_VAR(struct test_sorcery_object *, obj, NULL, ao2_cleanup);
573 
574  switch (cmd) {
575  case TEST_INIT:
576  info->name = "object_delete_uncreated";
577  info->category = "/res/sorcery_astdb/";
578  info->summary = "sorcery object deletion unit test";
579  info->description =
580  "Test object deletion of an uncreated object in sorcery using astdb wizard";
581  return AST_TEST_NOT_RUN;
582  case TEST_EXECUTE:
583  break;
584  }
585 
587  ast_test_status_update(test, "Failed to open sorcery structure\n");
588  return AST_TEST_FAIL;
589  }
590 
591  if (!(obj = ast_sorcery_alloc(sorcery, "test", "blah"))) {
592  ast_test_status_update(test, "Failed to allocate a known object type\n");
593  return AST_TEST_FAIL;
594  }
595 
596  if (!ast_sorcery_delete(sorcery, obj)) {
597  ast_test_status_update(test, "Successfully deleted an object which was never created\n");
598  return AST_TEST_FAIL;
599  }
600 
601  return AST_TEST_PASS;
602 }
Dummy sorcery object.
Full structure for sorcery.
Definition: sorcery.c:230
static struct ast_sorcery * alloc_and_initialize_sorcery(void)
#define NULL
Definition: resample.c:96
#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
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
int ast_sorcery_delete(const struct ast_sorcery *sorcery, void *object)
Delete an object.
Definition: sorcery.c:2233
def info(msg)
static void deinitialize_sorcery(struct ast_sorcery *sorcery)
void * ast_sorcery_alloc(const struct ast_sorcery *sorcery, const char *type, const char *id)
Allocate an object.
Definition: sorcery.c:1744
static struct ast_sorcery * sorcery
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ deinitialize_sorcery()

static void deinitialize_sorcery ( struct ast_sorcery sorcery)
static

Definition at line 73 of file test_sorcery_astdb.c.

References ast_db_deltree(), ast_sorcery_unref, and NULL.

Referenced by AST_TEST_DEFINE().

74 {
75  ast_db_deltree("test/test", NULL);
76  ast_sorcery_unref(sorcery);
77 }
#define NULL
Definition: resample.c:96
#define ast_sorcery_unref(sorcery)
Decrease the reference count of a sorcery structure.
Definition: sorcery.h:1502
int ast_db_deltree(const char *family, const char *keytree)
Delete one or more entries in astdb.
Definition: main/db.c:457

◆ load_module()

static int load_module ( void  )
static

Definition at line 620 of file test_sorcery_astdb.c.

References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.

621 {
622  AST_TEST_REGISTER(object_create);
623  AST_TEST_REGISTER(object_retrieve_id);
624  AST_TEST_REGISTER(object_retrieve_field);
625  AST_TEST_REGISTER(object_retrieve_multiple_all);
626  AST_TEST_REGISTER(object_retrieve_multiple_field);
627  AST_TEST_REGISTER(object_retrieve_regex);
628  AST_TEST_REGISTER(object_update);
629  AST_TEST_REGISTER(object_update_uncreated);
630  AST_TEST_REGISTER(object_delete);
631  AST_TEST_REGISTER(object_delete_uncreated);
632 
634 }
#define AST_TEST_REGISTER(cb)
Definition: test.h:127

◆ test_sorcery_object_alloc()

static void* test_sorcery_object_alloc ( const char *  id)
static

Internal function to allocate a test object.

Definition at line 48 of file test_sorcery_astdb.c.

References ast_sorcery_generic_alloc(), and NULL.

Referenced by alloc_and_initialize_sorcery().

49 {
50  return ast_sorcery_generic_alloc(sizeof(struct test_sorcery_object), NULL);
51 }
Dummy sorcery object.
#define NULL
Definition: resample.c:96
void * ast_sorcery_generic_alloc(size_t size, ao2_destructor_fn destructor)
Allocate a generic sorcery capable object.
Definition: sorcery.c:1728

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 604 of file test_sorcery_astdb.c.

References AST_TEST_UNREGISTER.

605 {
606  AST_TEST_UNREGISTER(object_create);
607  AST_TEST_UNREGISTER(object_retrieve_id);
608  AST_TEST_UNREGISTER(object_retrieve_field);
609  AST_TEST_UNREGISTER(object_retrieve_multiple_all);
610  AST_TEST_UNREGISTER(object_retrieve_multiple_field);
611  AST_TEST_UNREGISTER(object_retrieve_regex);
612  AST_TEST_UNREGISTER(object_update);
613  AST_TEST_UNREGISTER(object_update_uncreated);
614  AST_TEST_UNREGISTER(object_delete);
615  AST_TEST_UNREGISTER(object_delete_uncreated);
616 
617  return 0;
618 }
#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_LOAD_ORDER , .description = "Sorcery astdb Wizard test module" , .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, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 636 of file test_sorcery_astdb.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 636 of file test_sorcery_astdb.c.