Asterisk - The Open Source Telephony Project  18.5.0
Macros | Enumerations | Functions | Variables
test_db.c File Reference

AstDB Unit Tests. More...

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

Go to the source code of this file.

Macros

#define BASE   "astdbtest"
 
#define FAM1   BASE "/" SUB1
 
#define FAM2   BASE "/" SUB2
 
#define STR_FILL_32   "abcdefghijklmnopqrstuvwxyz123456"
 
#define SUB1   "one"
 
#define SUB2   "two"
 

Enumerations

enum  { FAMILY = 0, KEY = 1, VALUE = 2 }
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (put_get_del)
 
 AST_TEST_DEFINE (gettree_deltree)
 
 AST_TEST_DEFINE (perftest)
 
 AST_TEST_DEFINE (put_get_long)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "AstDB 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
 
static const char long_val [] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
 

Detailed Description

AstDB Unit Tests.

Author
Terry Wilson twils.nosp@m.on@d.nosp@m.igium.nosp@m..com

Definition in file test_db.c.

Macro Definition Documentation

◆ BASE

#define BASE   "astdbtest"

Referenced by AST_TEST_DEFINE().

◆ FAM1

#define FAM1   BASE "/" SUB1

Referenced by AST_TEST_DEFINE().

◆ FAM2

#define FAM2   BASE "/" SUB2

Referenced by AST_TEST_DEFINE().

◆ STR_FILL_32

#define STR_FILL_32   "abcdefghijklmnopqrstuvwxyz123456"

Referenced by AST_TEST_DEFINE().

◆ SUB1

#define SUB1   "one"

Referenced by AST_TEST_DEFINE().

◆ SUB2

#define SUB2   "two"

Referenced by AST_TEST_DEFINE().

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
FAMILY 
KEY 
VALUE 

Definition at line 39 of file test_db.c.

39  {
40  FAMILY = 0,
41  KEY = 1,
42  VALUE = 2,
43 };
Definition: test_db.c:41
Definition: test_db.c:40
Definition: test_db.c:42

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 309 of file test_db.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 309 of file test_db.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 309 of file test_db.c.

◆ AST_TEST_DEFINE() [1/4]

AST_TEST_DEFINE ( put_get_del  )

Definition at line 49 of file test_db.c.

References ARRAY_LEN, ast_db_del(), ast_db_get(), ast_db_put(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, buf, FAMILY, sip_to_pjsip::info(), KEY, long_val, TEST_EXECUTE, TEST_INIT, and VALUE.

50 {
51  int res = AST_TEST_PASS;
52  const char *inputs[][3] = {
53  {"family", "key", "value"},
54  {"astdbtest", "a", "b"},
55  {"astdbtest", "a", "a"},
56  {"astdbtest", "b", "a"},
57  {"astdbtest", "b", "b"},
58  {"astdbtest", "b", "!@#$%^&*()|+-<>?"},
59  {"astdbtest", long_val, "b"},
60  {"astdbtest", "b", long_val},
61  {"astdbtest", "!@#$%^&*()|+-<>?", "b"},
62  };
63  size_t x;
64  char buf[sizeof(long_val)] = { 0, };
65 
66  switch (cmd) {
67  case TEST_INIT:
68  info->name = "put_get_del";
69  info->category = "/main/astdb/";
70  info->summary = "ast_db_(put|get|del) unit test";
71  info->description =
72  "Ensures that the ast_db put, get, and del functions work";
73  return AST_TEST_NOT_RUN;
74  case TEST_EXECUTE:
75  break;
76  }
77 
78  for (x = 0; x < ARRAY_LEN(inputs); x++) {
79  if (ast_db_put(inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE])) {
80  ast_test_status_update(test, "Failed to put %s : %s : %s\n", inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE]);
81  res = AST_TEST_FAIL;
82  }
83  if (ast_db_get(inputs[x][FAMILY], inputs[x][KEY], buf, sizeof(buf))) {
84  ast_test_status_update(test, "Failed to get %s : %s : %s\n", inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE]);
85  res = AST_TEST_FAIL;
86  } else if (strcmp(buf, inputs[x][VALUE])) {
87  ast_test_status_update(test, "Failed to match key '%s/%s' value '%s' to '%s'\n", inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE], buf);
88  res = AST_TEST_FAIL;
89  }
90  if (ast_db_del(inputs[x][FAMILY], inputs[x][KEY])) {
91  ast_test_status_update(test, "Failed to del %s : %s\n", inputs[x][FAMILY], inputs[x][KEY]);
92  res = AST_TEST_FAIL;
93  }
94  }
95 
96  return res;
97 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
Definition: test_db.c:41
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
Definition: test_db.c:40
def info(msg)
Definition: test_db.c:42
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
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: main/db.c:429
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:327
static const char long_val[]
Definition: test_db.c:47

◆ AST_TEST_DEFINE() [2/4]

AST_TEST_DEFINE ( gettree_deltree  )

Definition at line 99 of file test_db.c.

References ARRAY_LEN, ast_db_deltree(), ast_db_freetree(), ast_db_gettree(), ast_db_put(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, BASE, buf, ast_db_entry::data, FAM1, FAM2, FAMILY, sip_to_pjsip::info(), ast_db_entry::key, KEY, ast_db_entry::next, NULL, SUB1, SUB2, TEST_EXECUTE, TEST_INIT, and VALUE.

100 {
101  int res = AST_TEST_PASS;
102  const char *inputs[][3] = {
103 #define BASE "astdbtest"
104 #define SUB1 "one"
105 #define SUB2 "two"
106 #define FAM1 BASE "/" SUB1
107 #define FAM2 BASE "/" SUB2
108  {FAM1, "one", "blah"},
109  {FAM1, "two", "bling"},
110  {FAM1, "three", "blast"},
111  {FAM2, "one", "blah"},
112  {FAM2, "two", "bling"},
113  {FAM2, "three", "blast"},
114  };
115  size_t x;
116  struct ast_db_entry *dbes, *cur;
117  int num_deleted;
118 
119  switch (cmd) {
120  case TEST_INIT:
121  info->name = "gettree_deltree";
122  info->category = "/main/astdb/";
123  info->summary = "ast_db_(gettree|deltree) unit test";
124  info->description =
125  "Ensures that the ast_db gettree and deltree functions work";
126  return AST_TEST_NOT_RUN;
127  case TEST_EXECUTE:
128  break;
129  }
130 
131  for (x = 0; x < ARRAY_LEN(inputs); x++) {
132  if (ast_db_put(inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE])) {
133  ast_test_status_update(test, "Failed to put %s : %s : %s\n", inputs[x][FAMILY], inputs[x][KEY], inputs[x][VALUE]);
134  res = AST_TEST_FAIL;
135  }
136  }
137 
138  if (!(dbes = ast_db_gettree(BASE, NULL))) {
139  ast_test_status_update(test, "Failed to ast_db_gettree family %s\n", BASE);
140  res = AST_TEST_FAIL;
141  }
142 
143  for (cur = dbes, x = 0; cur; cur = cur->next, x++) {
144  int found = 0;
145  size_t z;
146  for (z = 0; z < ARRAY_LEN(inputs); z++) {
147  char buf[256];
148  snprintf(buf, sizeof(buf), "/%s/%s", inputs[z][FAMILY], inputs[z][KEY]);
149  if (!strcmp(buf, cur->key) && !strcmp(inputs[z][VALUE], cur->data)) {
150  found = 1;
151  }
152  }
153  if (!found) {
154  ast_test_status_update(test, "inputs array has no entry for %s == %s\n", cur->key, cur->data);
155  res = AST_TEST_FAIL;
156  }
157  }
158 
159  if (x != ARRAY_LEN(inputs)) {
160  ast_test_status_update(test, "ast_db_gettree returned %zu entries when we expected %zu\n", x, ARRAY_LEN(inputs));
161  res = AST_TEST_FAIL;
162  }
163 
164  ast_db_freetree(dbes);
165 
166  if (!(dbes = ast_db_gettree(BASE, SUB1))) {
167  ast_test_status_update(test, "Failed to ast_db_gettree for %s/%s\n", BASE, SUB1);
168  res = AST_TEST_FAIL;
169  }
170 
171  for (cur = dbes, x = 0; cur; cur = cur->next, x++) {
172  int found = 0;
173  size_t z;
174  for (z = 0; z < ARRAY_LEN(inputs); z++) {
175  char buf[256];
176  snprintf(buf, sizeof(buf), "/%s/%s", inputs[z][FAMILY], inputs[z][KEY]);
177  if (!strcmp(buf, cur->key) && !strcmp(inputs[z][VALUE], cur->data)) {
178  found = 1;
179  }
180  }
181  if (!found) {
182  ast_test_status_update(test, "inputs array has no entry for %s == %s\n", cur->key, cur->data);
183  res = AST_TEST_FAIL;
184  }
185  }
186 
187  if (x != (ARRAY_LEN(inputs) / 2)) {
188  ast_test_status_update(test, "ast_db_gettree returned %zu entries when we expected %zu\n", x, ARRAY_LEN(inputs) / 2);
189  res = AST_TEST_FAIL;
190  }
191 
192  ast_db_freetree(dbes);
193 
194  if ((num_deleted = ast_db_deltree(BASE, SUB2)) != ARRAY_LEN(inputs) / 2) {
195  ast_test_status_update(test, "Failed to deltree %s/%s, expected %zu deletions and got %d\n", BASE, SUB2, ARRAY_LEN(inputs) / 2, num_deleted);
196  res = AST_TEST_FAIL;
197  }
198 
199  if ((num_deleted = ast_db_deltree(BASE, NULL)) != ARRAY_LEN(inputs) / 2) {
200  ast_test_status_update(test, "Failed to deltree %s, expected %zu deletions and got %d\n", BASE, ARRAY_LEN(inputs) / 2, num_deleted);
201  res = AST_TEST_FAIL;
202  }
203 
204  return res;
205 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
Definition: test_db.c:41
#define BASE
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
Definition: main/db.c:598
#define FAM1
#define NULL
Definition: resample.c:96
#define SUB2
struct ast_db_entry * next
Definition: astdb.h:32
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
Definition: test_db.c:40
struct ast_db_entry * ast_db_gettree(const char *family, const char *keytree)
Get a list of values within the astdb tree.
Definition: main/db.c:531
def info(msg)
#define SUB1
#define FAM2
Definition: astdb.h:31
char data[0]
Definition: astdb.h:34
Definition: test_db.c:42
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:327
int ast_db_deltree(const char *family, const char *keytree)
Delete one or more entries in astdb.
Definition: main/db.c:457
char * key
Definition: astdb.h:33

◆ AST_TEST_DEFINE() [3/4]

AST_TEST_DEFINE ( perftest  )

Definition at line 207 of file test_db.c.

References ast_db_deltree(), ast_db_put(), AST_TEST_NOT_RUN, AST_TEST_PASS, buf, sip_to_pjsip::info(), NULL, TEST_EXECUTE, and TEST_INIT.

208 {
209  int res = AST_TEST_PASS;
210  size_t x;
211  char buf[10];
212 
213  switch (cmd) {
214  case TEST_INIT:
215  info->name = "perftest";
216  info->category = "/main/astdb/";
217  info->summary = "astdb performance unit test";
218  info->description =
219  "Measure astdb performance";
220  return AST_TEST_NOT_RUN;
221  case TEST_EXECUTE:
222  break;
223  }
224 
225  for (x = 0; x < 100000; x++) {
226  sprintf(buf, "%zu", x);
227  ast_db_put("astdbtest", buf, buf);
228  }
229  ast_db_deltree("astdbtest", NULL);
230 
231  return res;
232 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define NULL
Definition: resample.c:96
def info(msg)
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:327
int ast_db_deltree(const char *family, const char *keytree)
Delete one or more entries in astdb.
Definition: main/db.c:457

◆ AST_TEST_DEFINE() [4/4]

AST_TEST_DEFINE ( put_get_long  )

Definition at line 234 of file test_db.c.

References ast_db_del(), ast_db_get_allocated(), ast_db_put(), ast_free, ast_str_append(), ast_str_buffer(), ast_str_create, ast_str_reset(), ast_str_strlen(), AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, sip_to_pjsip::info(), NULL, out, STR_FILL_32, TEST_EXECUTE, and TEST_INIT.

235 {
236  int res = AST_TEST_PASS;
237  struct ast_str *s;
238  int i, j;
239 
240 #define STR_FILL_32 "abcdefghijklmnopqrstuvwxyz123456"
241 
242  switch (cmd) {
243  case TEST_INIT:
244  info->name = "put_get_long";
245  info->category = "/main/astdb/";
246  info->summary = "ast_db_(put|get_allocated) unit test";
247  info->description =
248  "Ensures that the ast_db_put and ast_db_get_allocated functions work";
249  return AST_TEST_NOT_RUN;
250  case TEST_EXECUTE:
251  break;
252  }
253 
254  if (!(s = ast_str_create(4096))) {
255  return AST_TEST_FAIL;
256  }
257 
258  for (i = 1024; i <= 1024 * 1024 * 8; i *= 2) {
259  char *out = NULL;
260 
261  ast_str_reset(s);
262 
263  for (j = 0; j < i; j += sizeof(STR_FILL_32) - 1) {
264  ast_str_append(&s, 0, "%s", STR_FILL_32);
265  }
266 
267  if (ast_db_put("astdbtest", "long", ast_str_buffer(s))) {
268  ast_test_status_update(test, "Failed to put value of %zu bytes\n", ast_str_strlen(s));
269  res = AST_TEST_FAIL;
270  } else if (ast_db_get_allocated("astdbtest", "long", &out)) {
271  ast_test_status_update(test, "Failed to get value of %zu bytes\n", ast_str_strlen(s));
272  res = AST_TEST_FAIL;
273  } else if (strcmp(ast_str_buffer(s), out)) {
274  ast_test_status_update(test, "Failed to match value of %zu bytes\n", ast_str_strlen(s));
275  res = AST_TEST_FAIL;
276  } else if (ast_db_del("astdbtest", "long")) {
277  ast_test_status_update(test, "Failed to delete astdbtest/long\n");
278  res = AST_TEST_FAIL;
279  }
280 
281  if (out) {
282  ast_free(out);
283  }
284  }
285 
286  ast_free(s);
287 
288  return res;
289 }
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
int ast_db_get_allocated(const char *family, const char *key, char **out)
Get key value specified by family/key as a heap allocated string.
Definition: main/db.c:422
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1091
#define NULL
Definition: resample.c:96
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
def info(msg)
#define ast_free(a)
Definition: astmm.h:182
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition: strings.h:653
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:688
FILE * out
Definition: utils/frame.c:33
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition: main/db.c:429
#define STR_FILL_32
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:327
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

◆ load_module()

static int load_module ( void  )
static

Definition at line 300 of file test_db.c.

References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.

301 {
302  AST_TEST_REGISTER(put_get_del);
303  AST_TEST_REGISTER(gettree_deltree);
304  AST_TEST_REGISTER(perftest);
305  AST_TEST_REGISTER(put_get_long);
307 }
#define AST_TEST_REGISTER(cb)
Definition: test.h:127

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 291 of file test_db.c.

References AST_TEST_UNREGISTER.

292 {
293  AST_TEST_UNREGISTER(put_get_del);
294  AST_TEST_UNREGISTER(gettree_deltree);
295  AST_TEST_UNREGISTER(perftest);
296  AST_TEST_UNREGISTER(put_get_long);
297  return 0;
298 }
#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 = "AstDB 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 309 of file test_db.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 309 of file test_db.c.

◆ long_val

const char long_val[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
static

Definition at line 47 of file test_db.c.

Referenced by AST_TEST_DEFINE().