| 1 |
/* HEADER Testing snmp_enum */ |
| 2 |
|
| 3 |
#ifndef NETSNMP_FEATURE_REMOVE_SNMP_ENUM_STORE_LIST |
| 4 |
|
| 5 |
#define CONFIG_TYPE "snmp-enum-unit-test" |
| 6 |
#define STRING1 "life, and everything" |
| 7 |
#define STRING2 "restaurant at the end of the universe" |
| 8 |
#define STRING3 "label3" |
| 9 |
#define LONG_STRING "a-string-of-255-characters-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" |
| 10 |
|
| 11 |
#define STORE_AND_COMPARE(maj, min, s) \ |
| 12 |
{ \ |
| 13 |
FILE *fp; \ |
| 14 |
int read = 0; \ |
| 15 |
char *p, contents[4096]; \ |
| 16 |
\ |
| 17 |
se_store_list(maj, min, CONFIG_TYPE); \ |
| 18 |
fp = fopen(tmp_persist_file, "r"); \ |
| 19 |
if (fp) { \ |
| 20 |
read = fread(contents, 1, sizeof(contents) - 1, fp); \ |
| 21 |
fclose(fp); \ |
| 22 |
} \ |
| 23 |
contents[read > 0 ? read : 0] = '\0'; \ |
| 24 |
for (p = contents; *p; ++p) \ |
| 25 |
if (*p == '\n') \ |
| 26 |
*p = '|'; \ |
| 27 |
OKF(strcmp(contents, (s)) == 0, \ |
| 28 |
("stored list %s <> %s", (s), contents)); \ |
| 29 |
remove(tmp_persist_file); \ |
| 30 |
} |
| 31 |
|
| 32 |
char tmp_persist_file[256]; |
| 33 |
char *se_find_result; |
| 34 |
|
| 35 |
snprintf(tmp_persist_file, sizeof(tmp_persist_file), |
| 36 |
"/tmp/snmp-enum-unit-test-%ld", (long)getpid()); |
| 37 |
netsnmp_setenv("SNMP_PERSISTENT_FILE", tmp_persist_file, 1); |
| 38 |
|
| 39 |
init_snmp_enum("snmp"); |
| 40 |
|
| 41 |
STORE_AND_COMPARE(1, 1, "enum 1:1|"); |
| 42 |
|
| 43 |
OK(se_add_pair(1, 1, strdup("hi"), 1) == SE_OK, "add to list"); |
| 44 |
|
| 45 |
STORE_AND_COMPARE(1, 1, "enum 1:1 1:hi|"); |
| 46 |
|
| 47 |
OK(se_add_pair(1, 1, strdup("there"), 2) == SE_OK, "add to list"); |
| 48 |
|
| 49 |
STORE_AND_COMPARE(1, 1, "enum 1:1 1:hi 2:there|"); |
| 50 |
|
| 51 |
OK(se_add_pair(1, 1, strdup(LONG_STRING), 3) == SE_OK, "add to list"); |
| 52 |
OK(se_add_pair(1, 1, strdup(LONG_STRING), 4) == SE_OK, "add to list"); |
| 53 |
OK(se_add_pair(1, 1, strdup(LONG_STRING), 5) == SE_OK, "add to list"); |
| 54 |
OK(se_add_pair(1, 1, strdup(LONG_STRING), 6) == SE_OK, "add to list"); |
| 55 |
OK(se_add_pair(1, 1, strdup(LONG_STRING), 7) == SE_OK, "add to list"); |
| 56 |
OK(se_add_pair(1, 1, strdup(LONG_STRING), 8) == SE_OK, "add to list"); |
| 57 |
OK(se_add_pair(1, 1, strdup(LONG_STRING), 9) == SE_OK, "add to list"); |
| 58 |
|
| 59 |
STORE_AND_COMPARE(1, 1, "enum 1:1 1:hi 2:there 3:" LONG_STRING " 4:" LONG_STRING |
| 60 |
" 5:" LONG_STRING " 6:" LONG_STRING " 7:" LONG_STRING |
| 61 |
" 8:" LONG_STRING " 9:" LONG_STRING "|"); |
| 62 |
|
| 63 |
OK(se_add_pair(1, 1, strdup(LONG_STRING), 10) == SE_OK, "add to list"); |
| 64 |
|
| 65 |
STORE_AND_COMPARE(1, 1, "enum 1:1 1:hi 2:there 3:" LONG_STRING " 4:" LONG_STRING |
| 66 |
" 5:" LONG_STRING " 6:" LONG_STRING " 7:" LONG_STRING |
| 67 |
" 8:" LONG_STRING " 9:" LONG_STRING "|" |
| 68 |
"enum 1:1 10:" LONG_STRING "|"); |
| 69 |
|
| 70 |
OK(se_find_value(1, 1, "hi") == 1, |
| 71 |
"lookup by number #1 should be the proper string"); |
| 72 |
OK(strcmp(se_find_label(1, 1, 2), "there") == 0, |
| 73 |
"lookup by string #1 should be the proper number"); |
| 74 |
|
| 75 |
|
| 76 |
OK(se_add_pair_to_slist("testing", strdup(STRING1), 42) == SE_OK, |
| 77 |
"add to list"); |
| 78 |
OK(se_add_pair_to_slist("testing", strdup(STRING2), 2) == SE_OK, "add to list"); |
| 79 |
OK(se_add_pair_to_slist("testing", strdup(STRING3), 2) == SE_ALREADY_THERE, |
| 80 |
"add to list"); |
| 81 |
|
| 82 |
OK(se_find_value_in_slist("testing", STRING1) == 42, |
| 83 |
"lookup by number should be the proper string"); |
| 84 |
OK(strcmp(se_find_label_in_slist("testing", 2), STRING2) == 0, |
| 85 |
"lookup by string should be the proper number"); |
| 86 |
|
| 87 |
se_clear_slist("testing"); |
| 88 |
|
| 89 |
|
| 90 |
se_read_conf("enum", "2:3 1:apple 2:pear 3:kiwifruit"); |
| 91 |
OK(se_find_list(2, 3), "list (2, 3) should be present"); |
| 92 |
if (se_find_list(2, 3)) { |
| 93 |
OK(se_find_value(2, 3, "kiwifruit") == 3, |
| 94 |
"lookup by string should return the proper value"); |
| 95 |
se_find_result = se_find_label(2, 3, 2); |
| 96 |
OK(se_find_result && strcmp(se_find_result, "pear") == 0, |
| 97 |
"lookup by label should return the proper string"); |
| 98 |
} |
| 99 |
|
| 100 |
se_read_conf("enum", "fruit 1:apple 2:pear 3:kiwifruit"); |
| 101 |
OK(se_find_value_in_slist("fruit", "kiwifruit") == 3, |
| 102 |
"lookup by string should return the proper value"); |
| 103 |
se_find_result = se_find_label_in_slist("fruit", 2); |
| 104 |
OK(se_find_result && strcmp(se_find_result, "pear") == 0, |
| 105 |
"lookup by value should return the proper string"); |
| 106 |
|
| 107 |
clear_snmp_enum(); |
| 108 |
unregister_all_config_handlers(); |
| 109 |
|
| 110 |
#endif |