Asterisk - The Open Source Telephony Project GIT-master-b023714
Loading...
Searching...
No Matches
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 (gettree_deltree)
 
 AST_TEST_DEFINE (perftest)
 
 AST_TEST_DEFINE (put_get_del)
 
 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 = ASTERISK_GPL_KEY , .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"

◆ FAM1

#define FAM1   BASE "/" SUB1

◆ FAM2

#define FAM2   BASE "/" SUB2

◆ STR_FILL_32

#define STR_FILL_32   "abcdefghijklmnopqrstuvwxyz123456"

◆ SUB1

#define SUB1   "one"

◆ SUB2

#define SUB2   "two"

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};
@ KEY
Definition test_db.c:41
@ FAMILY
Definition test_db.c:40
@ VALUE
Definition test_db.c:42

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 320 of file test_db.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 320 of file test_db.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 320 of file test_db.c.

◆ AST_TEST_DEFINE() [1/4]

AST_TEST_DEFINE ( gettree_deltree  )

Definition at line 99 of file test_db.c.

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}
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition db.c:335
struct ast_db_entry * ast_db_gettree(const char *family, const char *keytree)
Get a list of values within the astdb tree.
Definition db.c:635
int ast_db_deltree(const char *family, const char *keytree)
Delete one or more entries in astdb.
Definition db.c:559
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
Definition db.c:695
char buf[BUFSIZE]
Definition eagi_proxy.c:66
#define NULL
Definition resample.c:96
Definition astdb.h:31
struct ast_db_entry * next
Definition astdb.h:32
char * key
Definition astdb.h:33
char data[0]
Definition astdb.h:34
@ TEST_INIT
Definition test.h:200
@ TEST_EXECUTE
Definition test.h:201
#define ast_test_status_update(a, b, c...)
Definition test.h:129
@ AST_TEST_PASS
Definition test.h:195
@ AST_TEST_FAIL
Definition test.h:196
@ AST_TEST_NOT_RUN
Definition test.h:194
#define FAM1
#define SUB2
#define BASE
#define SUB1
#define FAM2
#define ARRAY_LEN(a)
Definition utils.h:703

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, ast_db_entry::key, KEY, ast_db_entry::next, NULL, SUB1, SUB2, TEST_EXECUTE, TEST_INIT, and VALUE.

◆ AST_TEST_DEFINE() [2/4]

AST_TEST_DEFINE ( perftest  )

Definition at line 207 of file test_db.c.

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}

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

◆ AST_TEST_DEFINE() [3/4]

AST_TEST_DEFINE ( put_get_del  )

Definition at line 49 of file test_db.c.

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}
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition db.c:421
int ast_db_del(const char *family, const char *key)
Delete entry in astdb.
Definition db.c:472
static const char long_val[]
Definition test_db.c:47

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, KEY, long_val, TEST_EXECUTE, TEST_INIT, and VALUE.

◆ AST_TEST_DEFINE() [4/4]

AST_TEST_DEFINE ( put_get_long  )

Definition at line 234 of file test_db.c.

235{
236 int res = AST_TEST_PASS;
237 struct ast_str *s;
238 struct ast_str *key;
239 int i, j;
240
241#define STR_FILL_32 "abcdefghijklmnopqrstuvwxyz123456"
242
243 switch (cmd) {
244 case TEST_INIT:
245 info->name = "put_get_long";
246 info->category = "/main/astdb/";
247 info->summary = "ast_db_(put|get_allocated) unit test";
248 info->description =
249 "Ensures that the ast_db_put and ast_db_get_allocated functions work with log key and long data";
250 return AST_TEST_NOT_RUN;
251 case TEST_EXECUTE:
252 break;
253 }
254
255 if (!(s = ast_str_create(4096))) {
256 return AST_TEST_FAIL;
257 }
258
259 if (!(key = ast_str_create(512))) {
260 return AST_TEST_FAIL;
261 }
262
263 for (j = 0; j < 512; j += sizeof(STR_FILL_32) - 1) {
264 ast_str_append(&key, 0, "%s", STR_FILL_32);
265 }
266
267 for (i = 1024; i <= 1024 * 1024 * 8; i *= 2) {
268 char *out = NULL;
269
270 ast_str_reset(s);
271 ast_str_reset(key);
272
273 for (j = 0; j < i; j += sizeof(STR_FILL_32) - 1) {
274 ast_str_append(&s, 0, "%s", STR_FILL_32);
275 }
276
277 if (ast_db_put("astdbtest", ast_str_buffer(key), ast_str_buffer(s))) {
278 ast_test_status_update(test, "Failed to put value of %zu bytes\n", ast_str_strlen(s));
279 res = AST_TEST_FAIL;
280 } else if (ast_db_get_allocated("astdbtest", ast_str_buffer(key), &out)) {
281 ast_test_status_update(test, "Failed to get value of %zu bytes\n", ast_str_strlen(s));
282 res = AST_TEST_FAIL;
283 } else if (strcmp(ast_str_buffer(s), out)) {
284 ast_test_status_update(test, "Failed to match value of %zu bytes\n", ast_str_strlen(s));
285 res = AST_TEST_FAIL;
286 } else if (ast_db_del("astdbtest", ast_str_buffer(key))) {
287 ast_test_status_update(test, "Failed to delete astdbtest/long\n");
288 res = AST_TEST_FAIL;
289 }
290
291 if (out) {
292 ast_free(out);
293 }
294 }
295
296 ast_free(s);
297 ast_free(key);
298
299 return res;
300}
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 db.c:431
#define ast_free(a)
Definition astmm.h:180
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:1139
size_t attribute_pure ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition strings.h:730
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:693
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition strings.h:659
char *attribute_pure ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition strings.h:761
Support for dynamic strings.
Definition strings.h:623
#define STR_FILL_32
FILE * out
Definition utils/frame.c:33

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, NULL, out, STR_FILL_32, TEST_EXECUTE, and TEST_INIT.

◆ load_module()

static int load_module ( void  )
static

Definition at line 311 of file test_db.c.

312{
313 AST_TEST_REGISTER(put_get_del);
314 AST_TEST_REGISTER(gettree_deltree);
315 AST_TEST_REGISTER(perftest);
316 AST_TEST_REGISTER(put_get_long);
318}
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
#define AST_TEST_REGISTER(cb)
Definition test.h:127

References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 302 of file test_db.c.

303{
304 AST_TEST_UNREGISTER(put_get_del);
305 AST_TEST_UNREGISTER(gettree_deltree);
306 AST_TEST_UNREGISTER(perftest);
307 AST_TEST_UNREGISTER(put_get_long);
308 return 0;
309}
#define AST_TEST_UNREGISTER(cb)
Definition test.h:128

References AST_TEST_UNREGISTER.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "AstDB test module" , .key = ASTERISK_GPL_KEY , .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 320 of file test_db.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 320 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().