diff options
author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-09-09 02:07:32 +0200 |
---|---|---|
committer | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-09-09 02:07:32 +0200 |
commit | fbd60d6a9d9ba5b45879280dcfda92379dec65da (patch) | |
tree | a7c0e428be10bb27b6344c6531b804451c4d40cc /plugins/tests/test_check_snmp.c | |
parent | b54869391faab7ef91586c81de21f18a61bac5aa (diff) | |
parent | 615fec347cd575c0ee4343aa17ee99962f375f64 (diff) | |
download | monitoring-plugins-fbd60d6a9d9ba5b45879280dcfda92379dec65da.tar.gz |
Merge branch 'master' into refactor/check_curl
Diffstat (limited to 'plugins/tests/test_check_snmp.c')
-rw-r--r-- | plugins/tests/test_check_snmp.c | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/plugins/tests/test_check_snmp.c b/plugins/tests/test_check_snmp.c new file mode 100644 index 00000000..d71706d0 --- /dev/null +++ b/plugins/tests/test_check_snmp.c | |||
@@ -0,0 +1,175 @@ | |||
1 | /***************************************************************************** | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 3 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | * | ||
16 | * | ||
17 | *****************************************************************************/ | ||
18 | |||
19 | #include "tap.h" | ||
20 | #include "../../config.h" | ||
21 | |||
22 | #include <unistd.h> | ||
23 | #include <sys/types.h> | ||
24 | #include <sys/stat.h> | ||
25 | |||
26 | #include "utils_base.c" | ||
27 | #include "../check_snmp.d/check_snmp_helpers.h" | ||
28 | |||
29 | char *_np_state_generate_key(int argc, char **argv); | ||
30 | char *_np_state_calculate_location_prefix(void); | ||
31 | |||
32 | int main(int argc, char **argv) { | ||
33 | char *temp_string = (char *)_np_state_generate_key(argc, argv); | ||
34 | ok(!strcmp(temp_string, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), | ||
35 | "Got hash with exe and no parameters") || | ||
36 | diag("You are probably running in wrong directory. Must run as ./test_utils"); | ||
37 | |||
38 | int fake_argc = 4; | ||
39 | char *fake_argv[] = { | ||
40 | "./test_utils", | ||
41 | "here", | ||
42 | "--and", | ||
43 | "now", | ||
44 | }; | ||
45 | temp_string = (char *)_np_state_generate_key(fake_argc, fake_argv); | ||
46 | ok(!strcmp(temp_string, "bd72da9f78ff1419fad921ea5e43ce56508aef6c"), | ||
47 | "Got based on expected argv"); | ||
48 | |||
49 | unsetenv("MP_STATE_PATH"); | ||
50 | temp_string = (char *)_np_state_calculate_location_prefix(); | ||
51 | ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory"); | ||
52 | |||
53 | setenv("MP_STATE_PATH", "", 1); | ||
54 | temp_string = (char *)_np_state_calculate_location_prefix(); | ||
55 | ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string"); | ||
56 | |||
57 | setenv("MP_STATE_PATH", "/usr/local/nagios/var", 1); | ||
58 | temp_string = (char *)_np_state_calculate_location_prefix(); | ||
59 | ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory"); | ||
60 | |||
61 | fake_argc = 1; | ||
62 | fake_argv[0] = "./test_utils"; | ||
63 | state_key temp_state_key1 = np_enable_state(NULL, 51, "check_test", fake_argc, fake_argv); | ||
64 | ok(!strcmp(temp_state_key1.plugin_name, "check_test"), "Got plugin name"); | ||
65 | ok(!strcmp(temp_state_key1.name, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), | ||
66 | "Got generated filename"); | ||
67 | |||
68 | state_key temp_state_key2 = | ||
69 | np_enable_state("allowedchars_in_keyname", 77, "check_snmp", fake_argc, fake_argv); | ||
70 | |||
71 | char state_path[1024]; | ||
72 | sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/allowedchars_in_keyname", | ||
73 | (unsigned long)geteuid()); | ||
74 | ok(!strcmp(temp_state_key2.plugin_name, "check_test"), "Got plugin name"); | ||
75 | ok(!strcmp(temp_state_key2.name, "allowedchars_in_keyname"), "Got key name with valid chars"); | ||
76 | ok(!strcmp(temp_state_key2._filename, state_path), "Got internal filename"); | ||
77 | |||
78 | /* Don't do this test just yet. Will die */ | ||
79 | /* | ||
80 | np_enable_state("bad^chars$in@here", 77); | ||
81 | temp_state_key = this_monitoring_plugin->state; | ||
82 | ok( !strcmp(temp_state_key->name, "bad_chars_in_here"), "Got key name with bad chars replaced" | ||
83 | ); | ||
84 | */ | ||
85 | |||
86 | state_key temp_state_key3 = | ||
87 | np_enable_state("funnykeyname", 54, "check_snmp", fake_argc, fake_argv); | ||
88 | sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/funnykeyname", | ||
89 | (unsigned long)geteuid()); | ||
90 | ok(!strcmp(temp_state_key3.plugin_name, "check_test"), "Got plugin name"); | ||
91 | ok(!strcmp(temp_state_key3.name, "funnykeyname"), "Got key name"); | ||
92 | |||
93 | ok(!strcmp(temp_state_key3._filename, state_path), "Got internal filename"); | ||
94 | ok(temp_state_key3.data_version == 54, "Version set"); | ||
95 | |||
96 | state_data *temp_state_data = np_state_read(temp_state_key3); | ||
97 | ok(temp_state_data == NULL, "Got no state data as file does not exist"); | ||
98 | |||
99 | /* | ||
100 | temp_fp = fopen("var/statefile", "r"); | ||
101 | if (temp_fp==NULL) | ||
102 | printf("Error opening. errno=%d\n", errno); | ||
103 | printf("temp_fp=%s\n", temp_fp); | ||
104 | ok( _np_state_read_file(temp_fp) == true, "Can read state file" ); | ||
105 | fclose(temp_fp); | ||
106 | */ | ||
107 | |||
108 | temp_state_key3._filename = "var/statefile"; | ||
109 | temp_state_data = np_state_read(temp_state_key3); | ||
110 | ok(temp_state_data != NULL, "Got state data now") || | ||
111 | diag("Are you running in right directory? Will get coredump next if not"); | ||
112 | ok(temp_state_data->time == 1234567890, "Got time"); | ||
113 | ok(!strcmp((char *)temp_state_data->data, "String to read"), "Data as expected"); | ||
114 | |||
115 | temp_state_key3.data_version = 53; | ||
116 | temp_state_data = np_state_read(temp_state_key3); | ||
117 | ok(temp_state_data == NULL, "Older data version gives NULL"); | ||
118 | temp_state_key3.data_version = 54; | ||
119 | |||
120 | temp_state_key3._filename = "var/nonexistent"; | ||
121 | temp_state_data = np_state_read(temp_state_key3); | ||
122 | ok(temp_state_data == NULL, "Missing file gives NULL"); | ||
123 | |||
124 | temp_state_key3._filename = "var/oldformat"; | ||
125 | temp_state_data = np_state_read(temp_state_key3); | ||
126 | ok(temp_state_data == NULL, "Old file format gives NULL"); | ||
127 | |||
128 | temp_state_key3._filename = "var/baddate"; | ||
129 | temp_state_data = np_state_read(temp_state_key3); | ||
130 | ok(temp_state_data == NULL, "Bad date gives NULL"); | ||
131 | |||
132 | temp_state_key3._filename = "var/missingdataline"; | ||
133 | temp_state_data = np_state_read(temp_state_key3); | ||
134 | ok(temp_state_data == NULL, "Missing data line gives NULL"); | ||
135 | |||
136 | unlink("var/generated"); | ||
137 | temp_state_key3._filename = "var/generated"; | ||
138 | |||
139 | time_t current_time = 1234567890; | ||
140 | np_state_write_string(temp_state_key3, current_time, "String to read"); | ||
141 | ok(system("cmp var/generated var/statefile") == 0, "Generated file same as expected"); | ||
142 | |||
143 | unlink("var/generated_directory/statefile"); | ||
144 | unlink("var/generated_directory"); | ||
145 | temp_state_key3._filename = "var/generated_directory/statefile"; | ||
146 | current_time = 1234567890; | ||
147 | np_state_write_string(temp_state_key3, current_time, "String to read"); | ||
148 | ok(system("cmp var/generated_directory/statefile var/statefile") == 0, | ||
149 | "Have created directory"); | ||
150 | |||
151 | /* This test to check cannot write to dir - can't automate yet */ | ||
152 | /* | ||
153 | unlink("var/generated_bad_dir"); | ||
154 | mkdir("var/generated_bad_dir", S_IRUSR); | ||
155 | np_state_write_string(current_time, "String to read"); | ||
156 | */ | ||
157 | |||
158 | temp_state_key3._filename = "var/generated"; | ||
159 | time(¤t_time); | ||
160 | np_state_write_string(temp_state_key3, 0, "String to read"); | ||
161 | temp_state_data = np_state_read(temp_state_key3); | ||
162 | /* Check time is set to current_time */ | ||
163 | ok(system("cmp var/generated var/statefile > /dev/null") != 0, | ||
164 | "Generated file should be different this time"); | ||
165 | ok(temp_state_data->time - current_time <= 1, "Has time generated from current time"); | ||
166 | |||
167 | /* Don't know how to automatically test this. Need to be able to redefine die and catch the | ||
168 | * error */ | ||
169 | /* | ||
170 | temp_state_key->_filename="/dev/do/not/expect/to/be/able/to/write"; | ||
171 | np_state_write_string(0, "Bad file"); | ||
172 | */ | ||
173 | |||
174 | np_cleanup(); | ||
175 | } | ||