| 1 |
/* |
| 2 |
* etimetest.c |
| 3 |
* |
| 4 |
* HEADER Testing read_config.c shutdown callback |
| 5 |
* |
| 6 |
* Expected SUCCESSes for all tests: 8 |
| 7 |
* |
| 8 |
* Test of snmpd_unregister_config_handler SUCCESSes: 4 |
| 9 |
* Test of unregister_all_config_handlers SUCCESSes: 4 |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <net-snmp/net-snmp-config.h> |
| 14 |
#include <net-snmp/net-snmp-includes.h> |
| 15 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 16 |
#include <net-snmp/library/testing.h> |
| 17 |
|
| 18 |
#ifndef NETSNMP_FEATURE_REMOVE_UNREGISTER_APP_CONFIG_HANDLER |
| 19 |
|
| 20 |
/* #undef TEST_INTERNAL_API */ |
| 21 |
#define TEST_INTERNAL_API 1 |
| 22 |
|
| 23 |
/* |
| 24 |
* Global variables. |
| 25 |
*/ |
| 26 |
int callback_called = 0; |
| 27 |
|
| 28 |
static void test_callback(void); |
| 29 |
static void test_callback(void) { |
| 30 |
++callback_called; |
| 31 |
}; |
| 32 |
|
| 33 |
static void parse_config(const char *token, char *cptr); |
| 34 |
static void parse_config(const char *token, char *cptr) { |
| 35 |
/* do nothing */ |
| 36 |
}; |
| 37 |
|
| 38 |
/* each test returns 0 on success, and >0 on failure */ |
| 39 |
int test1(void); |
| 40 |
int test2(void); |
| 41 |
int test3(void); |
| 42 |
|
| 43 |
int (*tests[])(void) = { test1, test2, test3 }; |
| 44 |
|
| 45 |
int main(int argc, char *argv[]) |
| 46 |
{ |
| 47 |
int i,ret=0; |
| 48 |
for(i=0;i<sizeof(tests)/sizeof(*tests);++i) |
| 49 |
ret+=tests[i](); |
| 50 |
if (__did_plan == 0) { |
| 51 |
PLAN(__test_counter); |
| 52 |
} |
| 53 |
return ret; |
| 54 |
} |
| 55 |
|
| 56 |
int test1(void) { |
| 57 |
int sum = 0; |
| 58 |
callback_called = 0; |
| 59 |
fprintf(stdout, "# snmpd_unregister_config_handler tests\n"); |
| 60 |
init_snmp("testing"); |
| 61 |
OKF(callback_called==0, ("Callback shouldn't be called before registering it - it was called %d times\n",callback_called)); |
| 62 |
sum += callback_called; |
| 63 |
|
| 64 |
callback_called = 0; |
| 65 |
register_app_config_handler("testing", parse_config, test_callback, "module-name module-path"); |
| 66 |
OKF(callback_called==0, ("Callback shouldn't be called after registering it - it was called %d times\n",callback_called)); |
| 67 |
sum += callback_called; |
| 68 |
|
| 69 |
callback_called = 0; |
| 70 |
unregister_app_config_handler("testing"); |
| 71 |
OKF(callback_called==1, ("Callback should have been called once and only once after unregistering handler - it was called %d times\n",callback_called)); |
| 72 |
sum += callback_called; |
| 73 |
|
| 74 |
callback_called = 0; |
| 75 |
snmp_shutdown("testing"); |
| 76 |
OKF(callback_called==0, ("Callback should have been called once and only once after shutdown - it was called %d times\n",callback_called)); |
| 77 |
sum += callback_called; |
| 78 |
|
| 79 |
return (sum==1)?0:1; |
| 80 |
} |
| 81 |
|
| 82 |
int test2(void) { |
| 83 |
int sum = 0; |
| 84 |
callback_called = 0; |
| 85 |
fprintf(stdout, "# unregister_all_config_handlers tests\n"); |
| 86 |
init_snmp("testing"); |
| 87 |
OKF(callback_called==0, ("Callback shouldn't be called before registering it - it was called %d times\n",callback_called)); |
| 88 |
sum += callback_called; |
| 89 |
|
| 90 |
callback_called = 0; |
| 91 |
register_app_config_handler("testing", parse_config, test_callback, "module-name module-path"); |
| 92 |
OKF(callback_called==0, ("Callback shouldn't be called after registering it - it was called %d times\n",callback_called)); |
| 93 |
sum += callback_called; |
| 94 |
|
| 95 |
callback_called = 0; |
| 96 |
snmp_shutdown("testing"); |
| 97 |
OKF(callback_called==1, ("Callback should have been called once and only once during shutdown - it was called %d times\n",callback_called)); |
| 98 |
sum += callback_called; |
| 99 |
|
| 100 |
return (sum==1)?0:1; |
| 101 |
} |
| 102 |
|
| 103 |
int test3(void) { |
| 104 |
#ifdef TEST_INTERNAL_API |
| 105 |
int sum = 0; |
| 106 |
callback_called = 0; |
| 107 |
fprintf(stdout, "# unregister_all_config_handlers internal api tests\n"); |
| 108 |
init_snmp("testing"); |
| 109 |
OKF(callback_called==0, ("Callback shouldn't be called before registering it - it was called %d times\n",callback_called)); |
| 110 |
sum += callback_called; |
| 111 |
|
| 112 |
callback_called = 0; |
| 113 |
register_app_config_handler("testing", parse_config, test_callback, "module-name module-path"); |
| 114 |
OKF(callback_called==0, ("Callback shouldn't be called after registering it - it was called %d times\n",callback_called)); |
| 115 |
sum += callback_called; |
| 116 |
|
| 117 |
callback_called = 0; |
| 118 |
unregister_all_config_handlers(); |
| 119 |
OKF(callback_called==1, ("Callback should have been called once and only once after unregistering handler - it was called %d times\n",callback_called)); |
| 120 |
sum += callback_called; |
| 121 |
|
| 122 |
callback_called = 0; |
| 123 |
snmp_shutdown("testing"); |
| 124 |
OKF(callback_called==0, ("Callback should not have been called during shutdown after unregistering handler - it was called %d times\n",callback_called)); |
| 125 |
sum += callback_called; |
| 126 |
|
| 127 |
return (sum==1)?0:1; |
| 128 |
#else |
| 129 |
return 0; |
| 130 |
#endif |
| 131 |
} |
| 132 |
|
| 133 |
#else |
| 134 |
int main(int argc, char *argv[]) |
| 135 |
{ |
| 136 |
OK(1, "skipped test"); |
| 137 |
if (__did_plan == 0) |
| 138 |
PLAN(__test_counter); |
| 139 |
return 0; |
| 140 |
} |
| 141 |
#endif |