2 * collectd - src/snmp_agent_test.c
4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Marcin Mozejko <marcinx.mozejko@intel.com>
28 #include "snmp_agent.c"
31 #define TEST_HOSTNAME "test_hostname"
32 #define TEST_PLUGIN "test_plugin"
33 #define TEST_PLUGIN_INST "test_plugin_inst"
34 #define TEST_TYPE "test_type"
35 #define TEST_TYPE_INST "test_type_inst"
37 DEF_TEST(oid_to_string) {
38 oid_t o = {.oid = {1, 2, 3, 4, 5, 6, 7, 8, 9}, .oid_len = 9};
39 char oid_str[DATA_MAX_NAME_LEN];
41 int ret = snmp_agent_oid_to_string(oid_str, DATA_MAX_NAME_LEN, &o);
42 EXPECT_EQ_INT(o.oid_len * 2 - 1, ret);
43 EXPECT_EQ_STR("1.2.3.4.5.6.7.8.9", oid_str);
48 /* Testing formatting metric name for simple scalar */
49 DEF_TEST(format_name_scalar) {
50 data_definition_t *dd = calloc(1, sizeof(*dd));
52 dd->plugin = TEST_PLUGIN;
53 dd->plugin_instance = TEST_PLUGIN_INST;
55 dd->type_instance = TEST_TYPE_INST;
57 char name[DATA_MAX_NAME_LEN];
58 int ret = snmp_agent_format_name(name, sizeof(name), dd, NULL);
60 EXPECT_EQ_INT(0, ret);
62 "example.com/test_plugin-test_plugin_inst/test_type-test_type_inst",
70 DEF_TEST(format_name_simple_index) {
71 netsnmp_variable_list *index_list_tmp = NULL;
73 data_definition_t *dd = calloc(1, sizeof(*dd));
74 table_definition_t *td = calloc(1, sizeof(*td));
76 td->index_list_cont = NULL;
77 td->index_keys[0].source = INDEX_PLUGIN_INSTANCE;
78 td->index_keys[0].type = ASN_OCTET_STR;
79 td->index_keys[1].source = INDEX_TYPE_INSTANCE;
80 td->index_keys[1].type = ASN_OCTET_STR;
82 dd->plugin = TEST_PLUGIN;
85 const char plugin_inst[] = TEST_PLUGIN_INST;
86 const char type_inst[] = TEST_TYPE_INST;
88 snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_OCTET_STR,
89 (const u_char *)plugin_inst, strlen(plugin_inst));
90 snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_OCTET_STR,
91 (const u_char *)type_inst, strlen(type_inst));
93 build_oid_noalloc(index_oid.oid, sizeof(index_oid.oid), &index_oid.oid_len,
94 NULL, 0, index_list_tmp);
96 snmp_varlist_add_variable(&td->index_list_cont, NULL, 0, ASN_OCTET_STR, NULL,
98 snmp_varlist_add_variable(&td->index_list_cont, NULL, 0, ASN_OCTET_STR, NULL,
101 char name[DATA_MAX_NAME_LEN];
103 int ret = snmp_agent_format_name(name, DATA_MAX_NAME_LEN, dd, &index_oid);
105 EXPECT_EQ_INT(0, ret);
107 "example.com/test_plugin-test_plugin_inst/test_type-test_type_inst",
110 snmp_free_varbind(index_list_tmp);
111 snmp_free_varbind(td->index_list_cont);
118 DEF_TEST(format_name_regex_index) {
119 netsnmp_variable_list *index_list_tmp = NULL;
121 data_definition_t *dd = calloc(1, sizeof(*dd));
122 table_definition_t *td = calloc(1, sizeof(*td));
124 td->index_keys_len = 3;
125 td->index_list_cont = NULL;
126 td->index_keys[0].source = INDEX_PLUGIN_INSTANCE;
127 td->index_keys[0].type = ASN_OCTET_STR;
128 td->index_keys[1].source = INDEX_TYPE_INSTANCE;
129 td->index_keys[1].type = ASN_INTEGER;
130 td->index_keys[1].regex = "^vcpu_([0-9]{1,3})-cpu_[0-9]{1,3}$";
131 td->index_keys[1].group = 1;
132 td->index_keys[2].source = INDEX_TYPE_INSTANCE;
133 td->index_keys[2].type = ASN_INTEGER;
134 td->index_keys[2].regex = "^vcpu_[0-9]{1,3}-cpu_([0-9]{1,3})$";
135 td->index_keys[2].group = 1;
138 dd->plugin = TEST_PLUGIN;
139 dd->type = TEST_TYPE;
141 const char plugin_inst[] = TEST_PLUGIN_INST;
145 snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_OCTET_STR,
146 (const u_char *)plugin_inst, strlen(plugin_inst));
147 snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_INTEGER,
148 (const u_char *)&vcpu, 1);
149 snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_INTEGER,
150 (const u_char *)&cpu, 1);
152 build_oid_noalloc(index_oid.oid, sizeof(index_oid.oid), &index_oid.oid_len,
153 NULL, 0, index_list_tmp);
158 td->tokens[INDEX_TYPE_INSTANCE] =
159 c_avl_create((int (*)(const void *, const void *))num_compare);
160 snmp_varlist_add_variable(&td->index_list_cont, NULL, 0, ASN_OCTET_STR, NULL,
163 token = malloc(sizeof(*token));
164 offset = malloc(sizeof(*offset));
165 token->key = snmp_varlist_add_variable(&td->index_list_cont, NULL, 0,
166 ASN_INTEGER, NULL, 0);
167 token->str = strdup("vcpu_");
169 int ret = c_avl_insert(td->tokens[INDEX_TYPE_INSTANCE], (void *)offset,
172 token = malloc(sizeof(*token));
173 offset = malloc(sizeof(*offset));
174 token->key = snmp_varlist_add_variable(&td->index_list_cont, NULL, 0,
175 ASN_INTEGER, NULL, 0);
176 token->str = strdup("-cpu_");
178 ret += c_avl_insert(td->tokens[INDEX_TYPE_INSTANCE], (void *)offset,
180 char name[DATA_MAX_NAME_LEN];
182 ret += snmp_agent_format_name(name, DATA_MAX_NAME_LEN, dd, &index_oid);
184 EXPECT_EQ_INT(0, ret);
186 "example.com/test_plugin-test_plugin_inst/test_type-vcpu_1-cpu_10", name);
187 while (c_avl_pick(td->tokens[INDEX_TYPE_INSTANCE], (void **)&offset,
188 (void **)&token) == 0) {
193 c_avl_destroy(td->tokens[INDEX_TYPE_INSTANCE]);
194 snmp_free_varbind(index_list_tmp);
195 snmp_free_varbind(td->index_list_cont);
202 DEF_TEST(prep_index_list) {
203 table_definition_t *td = calloc(1, sizeof(*td));
206 td->index_keys_len = 5;
207 td->index_keys[0].source = INDEX_HOST;
208 td->index_keys[0].type = ASN_OCTET_STR;
209 td->index_keys[1].source = INDEX_PLUGIN;
210 td->index_keys[1].type = ASN_OCTET_STR;
211 td->index_keys[2].source = INDEX_PLUGIN_INSTANCE;
212 td->index_keys[2].type = ASN_INTEGER;
213 td->index_keys[3].source = INDEX_TYPE;
214 td->index_keys[3].type = ASN_INTEGER;
215 td->index_keys[4].source = INDEX_TYPE_INSTANCE;
216 td->index_keys[4].type = ASN_OCTET_STR;
217 td->index_list_cont = NULL;
219 int ret = snmp_agent_prep_index_list(td, &td->index_list_cont);
220 EXPECT_EQ_INT(0, ret);
222 netsnmp_variable_list *key = td->index_list_cont;
225 EXPECT_EQ_INT(ASN_OCTET_STR, key->type);
226 key = key->next_variable;
228 EXPECT_EQ_INT(ASN_OCTET_STR, key->type);
229 key = key->next_variable;
231 EXPECT_EQ_INT(ASN_INTEGER, key->type);
232 key = key->next_variable;
234 EXPECT_EQ_INT(ASN_INTEGER, key->type);
235 key = key->next_variable;
237 EXPECT_EQ_INT(ASN_OCTET_STR, key->type);
238 key = key->next_variable;
241 snmp_free_varbind(td->index_list_cont);
247 DEF_TEST(fill_index_list_simple) {
248 table_definition_t *td = calloc(1, sizeof(*td));
251 /* Preparing value list */
252 value_list_t *vl = calloc(1, sizeof(*vl));
254 strncpy(vl->host, TEST_HOSTNAME, DATA_MAX_NAME_LEN);
255 strncpy(vl->plugin, TEST_PLUGIN, DATA_MAX_NAME_LEN);
256 strncpy(vl->plugin_instance, TEST_PLUGIN_INST, DATA_MAX_NAME_LEN);
257 strncpy(vl->type, TEST_TYPE, DATA_MAX_NAME_LEN);
258 strncpy(vl->type_instance, TEST_TYPE_INST, DATA_MAX_NAME_LEN);
260 td->index_keys_len = 5;
261 td->index_keys[0].source = INDEX_HOST;
262 td->index_keys[0].type = ASN_OCTET_STR;
263 td->index_keys[1].source = INDEX_PLUGIN;
264 td->index_keys[1].type = ASN_OCTET_STR;
265 td->index_keys[2].source = INDEX_PLUGIN_INSTANCE;
266 td->index_keys[2].type = ASN_OCTET_STR;
267 td->index_keys[3].source = INDEX_TYPE;
268 td->index_keys[3].type = ASN_OCTET_STR;
269 td->index_keys[4].source = INDEX_TYPE_INSTANCE;
270 td->index_keys[4].type = ASN_OCTET_STR;
272 td->index_list_cont = NULL;
273 for (int i = 0; i < td->index_keys_len; i++)
274 snmp_varlist_add_variable(&td->index_list_cont, NULL, 0, ASN_OCTET_STR,
277 int ret = snmp_agent_fill_index_list(td, vl);
278 EXPECT_EQ_INT(0, ret);
280 netsnmp_variable_list *key = td->index_list_cont;
285 EXPECT_EQ_STR(vl->host, (char *)key->val.string);
286 key = key->next_variable;
288 EXPECT_EQ_STR(vl->plugin, (char *)key->val.string);
289 key = key->next_variable;
291 EXPECT_EQ_STR(vl->plugin_instance, (char *)key->val.string);
292 key = key->next_variable;
294 EXPECT_EQ_STR(vl->type, (char *)key->val.string);
295 key = key->next_variable;
297 EXPECT_EQ_STR(vl->type_instance, (char *)key->val.string);
298 key = key->next_variable;
301 snmp_free_varbind(td->index_list_cont);
308 DEF_TEST(fill_index_list_regex) {
309 table_definition_t *td = calloc(1, sizeof(*td));
314 /* Preparing value list */
315 value_list_t *vl = calloc(1, sizeof(*vl));
316 strncpy(vl->plugin_instance, TEST_PLUGIN_INST, DATA_MAX_NAME_LEN);
317 strncpy(vl->type_instance, "1test2test3", DATA_MAX_NAME_LEN);
319 td->index_keys_len = 4;
320 td->index_keys[0].source = INDEX_PLUGIN_INSTANCE;
321 td->index_keys[0].type = ASN_OCTET_STR;
322 td->index_keys[1].source = INDEX_TYPE_INSTANCE;
323 td->index_keys[1].type = ASN_INTEGER;
324 td->index_keys[1].regex = "^([0-9])test[0-9]test[0-9]$";
325 td->index_keys[1].group = 1;
326 td->index_keys[2].source = INDEX_TYPE_INSTANCE;
327 td->index_keys[2].type = ASN_INTEGER;
328 td->index_keys[2].regex = "^[0-9]test([0-9])test[0-9]$";
329 td->index_keys[2].group = 1;
330 td->index_keys[3].source = INDEX_TYPE_INSTANCE;
331 td->index_keys[3].type = ASN_INTEGER;
332 td->index_keys[3].regex = "^[0-9]test[0-9]test([0-9])$";
333 td->index_keys[3].group = 1;
335 td->index_list_cont = NULL;
336 snmp_varlist_add_variable(&td->index_list_cont, NULL, 0, ASN_OCTET_STR, NULL,
338 for (int i = 1; i < td->index_keys_len; i++) {
339 snmp_varlist_add_variable(&td->index_list_cont, NULL, 0, ASN_INTEGER, NULL,
341 ret = regcomp(&td->index_keys[i].regex_info, td->index_keys[i].regex,
343 EXPECT_EQ_INT(0, ret);
345 td->tokens[INDEX_TYPE_INSTANCE] =
346 c_avl_create((int (*)(const void *, const void *))num_compare);
347 assert(td->tokens[INDEX_TYPE_INSTANCE] != NULL);
349 ret = snmp_agent_fill_index_list(td, vl);
350 EXPECT_EQ_INT(0, ret);
351 EXPECT_EQ_INT(1, td->tokens_done);
353 netsnmp_variable_list *key = td->index_list_cont;
356 EXPECT_EQ_STR(vl->plugin_instance, (char *)key->val.string);
357 key = key->next_variable;
359 EXPECT_EQ_INT(1, *key->val.integer);
360 key = key->next_variable;
362 EXPECT_EQ_INT(2, *key->val.integer);
363 key = key->next_variable;
365 EXPECT_EQ_INT(3, *key->val.integer);
366 key = key->next_variable;
372 while (c_avl_pick(td->tokens[INDEX_TYPE_INSTANCE], (void **)&offset,
373 (void **)&token) == 0) {
379 c_avl_destroy(td->tokens[INDEX_TYPE_INSTANCE]);
380 snmp_free_varbind(td->index_list_cont);
383 for (int i = 0; i < td->index_keys_len; i++) {
384 regfree(&td->index_keys[i].regex_info);
391 DEF_TEST(config_index_key_source) {
392 oconfig_item_t *ci = calloc(1, sizeof(*ci));
393 table_definition_t *td = calloc(1, sizeof(*td));
394 data_definition_t *dd = calloc(1, sizeof(*dd));
400 ci->values = calloc(1, sizeof(*ci->values));
401 assert(ci->values != NULL);
403 ci->values->value.string = "PluginInstance";
404 ci->values->type = OCONFIG_TYPE_STRING;
406 int ret = snmp_agent_config_index_key_source(td, dd, ci);
408 EXPECT_EQ_INT(0, ret);
409 EXPECT_EQ_INT(1, td->index_keys_len);
410 EXPECT_EQ_INT(0, dd->index_key_pos);
411 EXPECT_EQ_INT(INDEX_PLUGIN_INSTANCE, td->index_keys[0].source);
412 EXPECT_EQ_INT(GROUP_UNUSED, td->index_keys[0].group);
413 OK(td->index_keys[0].regex == NULL);
423 DEF_TEST(config_index_key_regex) {
424 oconfig_item_t *ci = calloc(1, sizeof(*ci));
425 table_definition_t *td = calloc(1, sizeof(*td));
426 data_definition_t *dd = calloc(1, sizeof(*dd));
432 dd->index_key_pos = 0;
433 td->index_keys_len = 1;
434 td->index_keys[0].source = INDEX_PLUGIN_INSTANCE;
435 td->index_keys[0].group = 1;
436 ci->values = calloc(1, sizeof(*ci->values));
437 assert(ci->values != NULL);
439 ci->values->value.string = "^([0-9])test[0-9]test[0-9]$";
440 ci->values->type = OCONFIG_TYPE_STRING;
442 int ret = snmp_agent_config_index_key_regex(td, dd, ci);
444 EXPECT_EQ_INT(0, ret);
445 EXPECT_EQ_STR(td->index_keys[0].regex, "^([0-9])test[0-9]test[0-9]$");
446 OK(td->tokens[INDEX_PLUGIN_INSTANCE] != NULL);
448 c_avl_destroy(td->tokens[INDEX_PLUGIN_INSTANCE]);
451 sfree(td->index_keys[0].regex);
452 regfree(&td->index_keys[0].regex_info);
459 DEF_TEST(config_index_key) {
460 oconfig_item_t *ci = calloc(1, sizeof(*ci));
461 table_definition_t *td = calloc(1, sizeof(*td));
462 data_definition_t *dd = calloc(1, sizeof(*dd));
468 ci->children_num = 3;
469 ci->children = calloc(1, sizeof(*ci->children) * ci->children_num);
471 ci->children[0].key = "Source";
472 ci->children[0].parent = ci;
473 ci->children[0].values_num = 1;
474 ci->children[0].values = calloc(1, sizeof(*ci->children[0].values));
475 assert(ci->children[0].values != NULL);
476 ci->children[0].values->value.string = "PluginInstance";
477 ci->children[0].values->type = OCONFIG_TYPE_STRING;
479 ci->children[1].key = "Regex";
480 ci->children[1].parent = ci;
481 ci->children[1].values_num = 1;
482 ci->children[1].values = calloc(1, sizeof(*ci->children[0].values));
483 assert(ci->children[1].values != NULL);
484 ci->children[1].values->value.string = "^([0-9])test[0-9]test[0-9]$";
485 ci->children[1].values->type = OCONFIG_TYPE_STRING;
487 ci->children[2].key = "Group";
488 ci->children[2].parent = ci;
489 ci->children[2].values_num = 1;
490 ci->children[2].values = calloc(1, sizeof(*ci->children[0].values));
491 assert(ci->children[2].values != NULL);
492 ci->children[2].values->value.number = 1;
493 ci->children[2].values->type = OCONFIG_TYPE_NUMBER;
495 int ret = snmp_agent_config_index_key(td, dd, ci);
497 EXPECT_EQ_INT(0, ret);
498 EXPECT_EQ_INT(1, td->index_keys_len);
499 EXPECT_EQ_INT(0, dd->index_key_pos);
500 EXPECT_EQ_INT(INDEX_PLUGIN_INSTANCE, td->index_keys[0].source);
501 EXPECT_EQ_INT(1, td->index_keys[0].group);
502 EXPECT_EQ_STR("^([0-9])test[0-9]test[0-9]$", td->index_keys[0].regex);
503 OK(td->tokens[INDEX_PLUGIN_INSTANCE] != NULL);
505 sfree(ci->children[0].values);
506 sfree(ci->children[1].values);
507 sfree(ci->children[2].values);
512 c_avl_destroy(td->tokens[INDEX_PLUGIN_INSTANCE]);
514 sfree(td->index_keys[0].regex);
515 regfree(&td->index_keys[0].regex_info);
521 DEF_TEST(parse_index_key) {
522 const char regex[] = "test-([0-9])-([0-9])";
523 const char input[] = "snmp-test-5-6";
527 int ret = regcomp(®ex_info, regex, REG_EXTENDED);
528 EXPECT_EQ_INT(0, ret);
530 ret = snmp_agent_parse_index_key(input, ®ex_info, 0, &match);
531 EXPECT_EQ_INT(0, ret);
532 EXPECT_EQ_INT(5, match.rm_so);
533 EXPECT_EQ_INT(13, match.rm_eo);
535 ret = snmp_agent_parse_index_key(input, ®ex_info, 1, &match);
536 EXPECT_EQ_INT(0, ret);
537 EXPECT_EQ_INT(10, match.rm_so);
538 EXPECT_EQ_INT(11, match.rm_eo);
540 ret = snmp_agent_parse_index_key(input, ®ex_info, 2, &match);
541 EXPECT_EQ_INT(0, ret);
542 EXPECT_EQ_INT(12, match.rm_so);
543 EXPECT_EQ_INT(13, match.rm_eo);
545 regfree(®ex_info);
550 DEF_TEST(create_token) {
551 c_avl_tree_t *tokens =
552 c_avl_create((int (*)(const void *, const void *))num_compare);
553 const char input[] = "testA1-testB2";
555 assert(tokens != NULL);
557 int ret = snmp_agent_create_token(input, 0, 5, tokens, NULL);
558 EXPECT_EQ_INT(0, ret);
559 ret = snmp_agent_create_token(input, 6, 6, tokens, NULL);
560 EXPECT_EQ_INT(0, ret);
561 EXPECT_EQ_INT(2, c_avl_size(tokens));
566 ret = c_avl_pick(tokens, (void **)&offset, (void **)&token);
567 EXPECT_EQ_INT(0, ret);
568 EXPECT_EQ_INT(6, *offset);
569 EXPECT_EQ_STR("-testB", token->str);
574 ret = c_avl_pick(tokens, (void **)&offset, (void **)&token);
575 EXPECT_EQ_INT(0, ret);
576 EXPECT_EQ_INT(0, *offset);
577 EXPECT_EQ_STR("testA", token->str);
582 ret = c_avl_pick(tokens, (void **)&offset, (void **)&token);
585 c_avl_destroy(tokens);
590 DEF_TEST(delete_token) {
591 c_avl_tree_t *tokens =
592 c_avl_create((int (*)(const void *, const void *))num_compare);
593 const char input[] = "testA1-testB2-testC3";
595 assert(tokens != NULL);
597 int ret = snmp_agent_create_token(input, 0, 5, tokens, NULL);
598 EXPECT_EQ_INT(0, ret);
599 ret = snmp_agent_create_token(input, 6, 6, tokens, NULL);
600 EXPECT_EQ_INT(0, ret);
601 ret = snmp_agent_create_token(input, 13, 6, tokens, NULL);
602 EXPECT_EQ_INT(0, ret);
603 EXPECT_EQ_INT(3, c_avl_size(tokens));
604 ret = snmp_agent_delete_token(6, tokens);
605 EXPECT_EQ_INT(0, ret);
610 ret = c_avl_pick(tokens, (void **)&offset, (void **)&token);
611 EXPECT_EQ_INT(0, ret);
612 EXPECT_EQ_INT(0, *offset);
613 EXPECT_EQ_STR("testA", token->str);
618 ret = c_avl_pick(tokens, (void **)&offset, (void **)&token);
619 EXPECT_EQ_INT(0, ret);
620 EXPECT_EQ_INT(13, *offset);
621 EXPECT_EQ_STR("-testC", token->str);
626 ret = c_avl_pick(tokens, (void **)&offset, (void **)&token);
629 c_avl_destroy(tokens);
634 DEF_TEST(get_token) {
635 c_avl_tree_t *tokens =
636 c_avl_create((int (*)(const void *, const void *))num_compare);
637 const char input[] = "testA1-testB2-testC3";
639 assert(tokens != NULL);
641 int ret = snmp_agent_create_token(input, 0, 5, tokens, NULL);
642 EXPECT_EQ_INT(0, ret);
643 ret = snmp_agent_create_token(input, 6, 6, tokens, NULL);
644 EXPECT_EQ_INT(0, ret);
645 ret = snmp_agent_create_token(input, 13, 6, tokens, NULL);
646 EXPECT_EQ_INT(0, ret);
647 EXPECT_EQ_INT(3, c_avl_size(tokens));
648 ret = snmp_agent_get_token(tokens, 12);
649 EXPECT_EQ_INT(6, ret);
654 while (c_avl_pick(tokens, (void **)&offset, (void **)&token) == 0) {
660 c_avl_destroy(tokens);
666 regmatch_t m[3] = {{5, 6}, /* "1" */
669 c_avl_tree_t *tokens =
670 c_avl_create((int (*)(const void *, const void *))num_compare);
671 const char input[] = "testA1-testB2-testC3";
674 c_avl_iterator_t *it;
677 assert(tokens != NULL);
680 ret = snmp_agent_tokenize(input, tokens, &m[0], NULL);
681 EXPECT_EQ_INT(0, ret);
682 it = c_avl_get_iterator(tokens);
683 ret = c_avl_iterator_next(it, (void **)&offset, (void **)&token);
684 EXPECT_EQ_INT(0, ret);
685 EXPECT_EQ_STR("testA", token->str);
686 ret = c_avl_iterator_next(it, (void **)&offset, (void **)&token);
687 EXPECT_EQ_INT(0, ret);
688 EXPECT_EQ_STR("-testB2-testC3", token->str);
689 ret = c_avl_iterator_next(it, (void **)&offset, (void **)&token);
691 c_avl_iterator_destroy(it);
694 ret = snmp_agent_tokenize(input, tokens, &m[1], NULL);
695 EXPECT_EQ_INT(0, ret);
696 it = c_avl_get_iterator(tokens);
697 ret = c_avl_iterator_next(it, (void **)&offset, (void **)&token);
698 EXPECT_EQ_INT(0, ret);
699 EXPECT_EQ_STR("testA", token->str);
700 ret = c_avl_iterator_next(it, (void **)&offset, (void **)&token);
701 EXPECT_EQ_INT(0, ret);
702 EXPECT_EQ_STR("-testB", token->str);
703 ret = c_avl_iterator_next(it, (void **)&offset, (void **)&token);
704 EXPECT_EQ_INT(0, ret);
705 EXPECT_EQ_STR("-testC3", token->str);
706 ret = c_avl_iterator_next(it, (void **)&offset, (void **)&token);
708 c_avl_iterator_destroy(it);
711 ret = snmp_agent_tokenize(input, tokens, &m[2], NULL);
712 EXPECT_EQ_INT(0, ret);
713 it = c_avl_get_iterator(tokens);
714 ret = c_avl_iterator_next(it, (void **)&offset, (void **)&token);
715 EXPECT_EQ_INT(0, ret);
716 EXPECT_EQ_STR("testA", token->str);
717 ret = c_avl_iterator_next(it, (void **)&offset, (void **)&token);
718 EXPECT_EQ_INT(0, ret);
719 EXPECT_EQ_STR("-testB", token->str);
720 ret = c_avl_iterator_next(it, (void **)&offset, (void **)&token);
721 EXPECT_EQ_INT(0, ret);
722 EXPECT_EQ_STR("-testC", token->str);
723 ret = c_avl_iterator_next(it, (void **)&offset, (void **)&token);
725 c_avl_iterator_destroy(it);
727 while (c_avl_pick(tokens, (void **)&offset, (void **)&token) == 0) {
733 c_avl_destroy(tokens);
738 DEF_TEST(build_name) {
739 table_definition_t *td = calloc(1, sizeof(*td));
740 c_avl_tree_t *tokens =
741 c_avl_create((int (*)(const void *, const void *))num_compare);
743 assert(tokens != NULL);
746 int n[3] = {1, 2, 3};
747 char *t[3] = {"testA", "-testB", "-testC"};
748 int off[3] = {0, 6, 13};
754 td->index_list_cont = NULL;
755 for (int i = 0; i < 3; i++) {
756 token = malloc(sizeof(*token));
759 snmp_varlist_add_variable(&td->index_list_cont, NULL, 0, ASN_INTEGER,
760 (const u_char *)&n[i], sizeof(n[i]));
761 assert(token->key != NULL);
763 ret = c_avl_insert(tokens, (void *)offset, (void *)token);
767 ret = snmp_agent_build_name(&name, tokens);
768 EXPECT_EQ_INT(0, ret);
769 EXPECT_EQ_STR("testA1-testB2-testC3", name);
771 while (c_avl_pick(tokens, (void **)&offset, (void **)&token) == 0)
774 c_avl_destroy(tokens);
775 snmp_free_varbind(td->index_list_cont);
782 /* snmp_agent_oid_to_string */
783 RUN_TEST(oid_to_string);
785 /* snmp_agent_prep_index_list */
786 RUN_TEST(prep_index_list);
788 /* snmp_agent_fill_index_list */
789 RUN_TEST(fill_index_list_simple);
790 RUN_TEST(fill_index_list_regex);
792 /* snmp_agent_format_name */
793 RUN_TEST(format_name_scalar);
794 RUN_TEST(format_name_simple_index);
795 RUN_TEST(format_name_regex_index);
797 /* snmp_agent_config_index_key_source */
798 RUN_TEST(config_index_key_source);
800 /* snmp_agent_config_index_key_regex */
801 RUN_TEST(config_index_key_regex);
803 /* snmp_agent_config_index_key */
804 RUN_TEST(config_index_key);
806 /*snmp_agent_parse_index_key */
807 RUN_TEST(parse_index_key);
809 /* snmp_agent_create_token */
810 RUN_TEST(create_token);
812 /* snmp_agent_delete_token */
813 RUN_TEST(delete_token);
815 /* snmp_agent_get_token */
818 /* snmp_agent_tokenize */
821 /* snmp_agent_build_name */
822 RUN_TEST(build_name);