summaryrefslogtreecommitdiffstats
path: root/lib/tests/test_utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/tests/test_utils.c')
-rw-r--r--lib/tests/test_utils.c490
1 files changed, 232 insertions, 258 deletions
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c
index 01afacd..901e0cc 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -1,20 +1,20 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* This program is free software: you can redistribute it and/or modify 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 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 5 * the Free Software Foundation, either version 3 of the License, or
6* (at your option) any later version. 6 * (at your option) any later version.
7* 7 *
8* This program is distributed in the hope that it will be useful, 8 * This program is distributed in the hope that it will be useful,
9* but WITHOUT ANY WARRANTY; without even the implied warranty of 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details. 11 * GNU General Public License for more details.
12* 12 *
13* You should have received a copy of the GNU General Public License 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/>. 14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15* 15 *
16* 16 *
17*****************************************************************************/ 17 *****************************************************************************/
18 18
19#include "common.h" 19#include "common.h"
20#include "utils_base.h" 20#include "utils_base.h"
@@ -27,331 +27,323 @@
27 27
28#include "utils_base.c" 28#include "utils_base.c"
29 29
30int 30int main(int argc, char **argv) {
31main (int argc, char **argv)
32{
33 char state_path[1024]; 31 char state_path[1024];
34 range *range; 32 range *range;
35 double temp; 33 double temp;
36 thresholds *thresholds = NULL; 34 thresholds *thresholds = NULL;
37 int i, rc; 35 int i, rc;
38 char *temp_string; 36 char *temp_string;
39 state_key *temp_state_key = NULL; 37 state_key *temp_state_key = NULL;
40 state_data *temp_state_data; 38 state_data *temp_state_data;
41 time_t current_time; 39 time_t current_time;
42 40
43 plan_tests(185); 41 plan_tests(185);
44 42
45 ok( this_monitoring_plugin==NULL, "monitoring_plugin not initialised"); 43 ok(this_monitoring_plugin == NULL, "monitoring_plugin not initialised");
46 44
47 np_init( "check_test", argc, argv ); 45 np_init("check_test", argc, argv);
48 46
49 ok( this_monitoring_plugin!=NULL, "monitoring_plugin now initialised"); 47 ok(this_monitoring_plugin != NULL, "monitoring_plugin now initialised");
50 ok( !strcmp(this_monitoring_plugin->plugin_name, "check_test"), "plugin name initialised" ); 48 ok(!strcmp(this_monitoring_plugin->plugin_name, "check_test"), "plugin name initialised");
51 49
52 ok( this_monitoring_plugin->argc==argc, "Argc set" ); 50 ok(this_monitoring_plugin->argc == argc, "Argc set");
53 ok( this_monitoring_plugin->argv==argv, "Argv set" ); 51 ok(this_monitoring_plugin->argv == argv, "Argv set");
54 52
55 np_set_args(0,0); 53 np_set_args(0, 0);
56 54
57 ok( this_monitoring_plugin->argc==0, "argc changed" ); 55 ok(this_monitoring_plugin->argc == 0, "argc changed");
58 ok( this_monitoring_plugin->argv==0, "argv changed" ); 56 ok(this_monitoring_plugin->argv == 0, "argv changed");
59 57
60 np_set_args(argc, argv); 58 np_set_args(argc, argv);
61 59
62 range = parse_range_string("6"); 60 range = parse_range_string("6");
63 ok( range != NULL, "'6' is valid range"); 61 ok(range != NULL, "'6' is valid range");
64 ok( range->start == 0, "Start correct"); 62 ok(range->start == 0, "Start correct");
65 ok( range->start_infinity == false, "Not using negative infinity"); 63 ok(range->start_infinity == false, "Not using negative infinity");
66 ok( range->end == 6, "End correct"); 64 ok(range->end == 6, "End correct");
67 ok( range->end_infinity == false, "Not using infinity"); 65 ok(range->end_infinity == false, "Not using infinity");
68 free(range); 66 free(range);
69 67
70 range = parse_range_string("1:12%%"); 68 range = parse_range_string("1:12%%");
71 ok( range != NULL, "'1:12%%' is valid - percentages are ignored"); 69 ok(range != NULL, "'1:12%%' is valid - percentages are ignored");
72 ok( range->start == 1, "Start correct"); 70 ok(range->start == 1, "Start correct");
73 ok( range->start_infinity == false, "Not using negative infinity"); 71 ok(range->start_infinity == false, "Not using negative infinity");
74 ok( range->end == 12, "End correct"); 72 ok(range->end == 12, "End correct");
75 ok( range->end_infinity == false, "Not using infinity"); 73 ok(range->end_infinity == false, "Not using infinity");
76 free(range); 74 free(range);
77 75
78 range = parse_range_string("-7:23"); 76 range = parse_range_string("-7:23");
79 ok( range != NULL, "'-7:23' is valid range"); 77 ok(range != NULL, "'-7:23' is valid range");
80 ok( range->start == -7, "Start correct"); 78 ok(range->start == -7, "Start correct");
81 ok( range->start_infinity == false, "Not using negative infinity"); 79 ok(range->start_infinity == false, "Not using negative infinity");
82 ok( range->end == 23, "End correct"); 80 ok(range->end == 23, "End correct");
83 ok( range->end_infinity == false, "Not using infinity"); 81 ok(range->end_infinity == false, "Not using infinity");
84 free(range); 82 free(range);
85 83
86 range = parse_range_string(":5.75"); 84 range = parse_range_string(":5.75");
87 ok( range != NULL, "':5.75' is valid range"); 85 ok(range != NULL, "':5.75' is valid range");
88 ok( range->start == 0, "Start correct"); 86 ok(range->start == 0, "Start correct");
89 ok( range->start_infinity == false, "Not using negative infinity"); 87 ok(range->start_infinity == false, "Not using negative infinity");
90 ok( range->end == 5.75, "End correct"); 88 ok(range->end == 5.75, "End correct");
91 ok( range->end_infinity == false, "Not using infinity"); 89 ok(range->end_infinity == false, "Not using infinity");
92 free(range); 90 free(range);
93 91
94 range = parse_range_string("~:-95.99"); 92 range = parse_range_string("~:-95.99");
95 ok( range != NULL, "~:-95.99' is valid range"); 93 ok(range != NULL, "~:-95.99' is valid range");
96 ok( range->start_infinity == true, "Using negative infinity"); 94 ok(range->start_infinity == true, "Using negative infinity");
97 ok( range->end == -95.99, "End correct (with rounding errors)"); 95 ok(range->end == -95.99, "End correct (with rounding errors)");
98 ok( range->end_infinity == false, "Not using infinity"); 96 ok(range->end_infinity == false, "Not using infinity");
99 free(range); 97 free(range);
100 98
101 range = parse_range_string("12345678901234567890:"); 99 range = parse_range_string("12345678901234567890:");
102 temp = atof("12345678901234567890"); /* Can't just use this because number too large */ 100 temp = atof("12345678901234567890"); /* Can't just use this because number too large */
103 ok( range != NULL, "'12345678901234567890:' is valid range"); 101 ok(range != NULL, "'12345678901234567890:' is valid range");
104 ok( range->start == temp, "Start correct"); 102 ok(range->start == temp, "Start correct");
105 ok( range->start_infinity == false, "Not using negative infinity"); 103 ok(range->start_infinity == false, "Not using negative infinity");
106 ok( range->end_infinity == true, "Using infinity"); 104 ok(range->end_infinity == true, "Using infinity");
107 /* Cannot do a "-1" on temp, as it appears to be same value */ 105 /* Cannot do a "-1" on temp, as it appears to be same value */
108 ok( check_range(temp/1.1, range) == true, "12345678901234567890/1.1 - alert"); 106 ok(check_range(temp / 1.1, range) == true, "12345678901234567890/1.1 - alert");
109 ok( check_range(temp, range) == false, "12345678901234567890 - no alert"); 107 ok(check_range(temp, range) == false, "12345678901234567890 - no alert");
110 ok( check_range(temp*2, range) == false, "12345678901234567890*2 - no alert"); 108 ok(check_range(temp * 2, range) == false, "12345678901234567890*2 - no alert");
111 free(range); 109 free(range);
112 110
113 range = parse_range_string("~:0"); 111 range = parse_range_string("~:0");
114 ok( range != NULL, "'~:0' is valid range"); 112 ok(range != NULL, "'~:0' is valid range");
115 ok( range->start_infinity == true, "Using negative infinity"); 113 ok(range->start_infinity == true, "Using negative infinity");
116 ok( range->end == 0, "End correct"); 114 ok(range->end == 0, "End correct");
117 ok( range->end_infinity == false, "Not using infinity"); 115 ok(range->end_infinity == false, "Not using infinity");
118 ok( range->alert_on == OUTSIDE, "Will alert on outside of this range"); 116 ok(range->alert_on == OUTSIDE, "Will alert on outside of this range");
119 ok( check_range(0.5, range) == true, "0.5 - alert"); 117 ok(check_range(0.5, range) == true, "0.5 - alert");
120 ok( check_range(-10, range) == false, "-10 - no alert"); 118 ok(check_range(-10, range) == false, "-10 - no alert");
121 ok( check_range(0, range) == false, "0 - no alert"); 119 ok(check_range(0, range) == false, "0 - no alert");
122 free(range); 120 free(range);
123 121
124 range = parse_range_string("@0:657.8210567"); 122 range = parse_range_string("@0:657.8210567");
125 ok( range != 0, "@0:657.8210567' is a valid range"); 123 ok(range != 0, "@0:657.8210567' is a valid range");
126 ok( range->start == 0, "Start correct"); 124 ok(range->start == 0, "Start correct");
127 ok( range->start_infinity == false, "Not using negative infinity"); 125 ok(range->start_infinity == false, "Not using negative infinity");
128 ok( range->end == 657.8210567, "End correct"); 126 ok(range->end == 657.8210567, "End correct");
129 ok( range->end_infinity == false, "Not using infinity"); 127 ok(range->end_infinity == false, "Not using infinity");
130 ok( range->alert_on == INSIDE, "Will alert on inside of this range" ); 128 ok(range->alert_on == INSIDE, "Will alert on inside of this range");
131 ok( check_range(32.88, range) == true, "32.88 - alert"); 129 ok(check_range(32.88, range) == true, "32.88 - alert");
132 ok( check_range(-2, range) == false, "-2 - no alert"); 130 ok(check_range(-2, range) == false, "-2 - no alert");
133 ok( check_range(657.8210567, range) == true, "657.8210567 - alert"); 131 ok(check_range(657.8210567, range) == true, "657.8210567 - alert");
134 ok( check_range(0, range) == true, "0 - alert"); 132 ok(check_range(0, range) == true, "0 - alert");
135 free(range); 133 free(range);
136 134
137 range = parse_range_string("@1:1"); 135 range = parse_range_string("@1:1");
138 ok( range != NULL, "'@1:1' is a valid range"); 136 ok(range != NULL, "'@1:1' is a valid range");
139 ok( range->start == 1, "Start correct"); 137 ok(range->start == 1, "Start correct");
140 ok( range->start_infinity == false, "Not using negative infinity"); 138 ok(range->start_infinity == false, "Not using negative infinity");
141 ok( range->end == 1, "End correct"); 139 ok(range->end == 1, "End correct");
142 ok( range->end_infinity == false, "Not using infinity"); 140 ok(range->end_infinity == false, "Not using infinity");
143 ok( range->alert_on == INSIDE, "Will alert on inside of this range" ); 141 ok(range->alert_on == INSIDE, "Will alert on inside of this range");
144 ok( check_range(0.5, range) == false, "0.5 - no alert"); 142 ok(check_range(0.5, range) == false, "0.5 - no alert");
145 ok( check_range(1, range) == true, "1 - alert"); 143 ok(check_range(1, range) == true, "1 - alert");
146 ok( check_range(5.2, range) == false, "5.2 - no alert"); 144 ok(check_range(5.2, range) == false, "5.2 - no alert");
147 free(range); 145 free(range);
148 146
149 range = parse_range_string("1:1"); 147 range = parse_range_string("1:1");
150 ok( range != NULL, "'1:1' is a valid range"); 148 ok(range != NULL, "'1:1' is a valid range");
151 ok( range->start == 1, "Start correct"); 149 ok(range->start == 1, "Start correct");
152 ok( range->start_infinity == false, "Not using negative infinity"); 150 ok(range->start_infinity == false, "Not using negative infinity");
153 ok( range->end == 1, "End correct"); 151 ok(range->end == 1, "End correct");
154 ok( range->end_infinity == false, "Not using infinity"); 152 ok(range->end_infinity == false, "Not using infinity");
155 ok( check_range(0.5, range) == true, "0.5 - alert"); 153 ok(check_range(0.5, range) == true, "0.5 - alert");
156 ok( check_range(1, range) == false, "1 - no alert"); 154 ok(check_range(1, range) == false, "1 - no alert");
157 ok( check_range(5.2, range) == true, "5.2 - alert"); 155 ok(check_range(5.2, range) == true, "5.2 - alert");
158 free(range); 156 free(range);
159 157
160 range = parse_range_string("2:1"); 158 range = parse_range_string("2:1");
161 ok( range == NULL, "'2:1' rejected"); 159 ok(range == NULL, "'2:1' rejected");
162 160
163 rc = _set_thresholds(&thresholds, NULL, NULL); 161 rc = _set_thresholds(&thresholds, NULL, NULL);
164 ok( rc == 0, "Thresholds (NULL, NULL) set"); 162 ok(rc == 0, "Thresholds (NULL, NULL) set");
165 ok( thresholds->warning == NULL, "Warning not set"); 163 ok(thresholds->warning == NULL, "Warning not set");
166 ok( thresholds->critical == NULL, "Critical not set"); 164 ok(thresholds->critical == NULL, "Critical not set");
167 165
168 rc = _set_thresholds(&thresholds, NULL, "80"); 166 rc = _set_thresholds(&thresholds, NULL, "80");
169 ok( rc == 0, "Thresholds (NULL, '80') set"); 167 ok(rc == 0, "Thresholds (NULL, '80') set");
170 ok( thresholds->warning == NULL, "Warning not set"); 168 ok(thresholds->warning == NULL, "Warning not set");
171 ok( thresholds->critical->end == 80, "Critical set correctly"); 169 ok(thresholds->critical->end == 80, "Critical set correctly");
172 170
173 rc = _set_thresholds(&thresholds, "5:33", NULL); 171 rc = _set_thresholds(&thresholds, "5:33", NULL);
174 ok( rc == 0, "Thresholds ('5:33', NULL) set"); 172 ok(rc == 0, "Thresholds ('5:33', NULL) set");
175 ok( thresholds->warning->start == 5, "Warning start set"); 173 ok(thresholds->warning->start == 5, "Warning start set");
176 ok( thresholds->warning->end == 33, "Warning end set"); 174 ok(thresholds->warning->end == 33, "Warning end set");
177 ok( thresholds->critical == NULL, "Critical not set"); 175 ok(thresholds->critical == NULL, "Critical not set");
178 176
179 rc = _set_thresholds(&thresholds, "30", "60"); 177 rc = _set_thresholds(&thresholds, "30", "60");
180 ok( rc == 0, "Thresholds ('30', '60') set"); 178 ok(rc == 0, "Thresholds ('30', '60') set");
181 ok( thresholds->warning->end == 30, "Warning set correctly"); 179 ok(thresholds->warning->end == 30, "Warning set correctly");
182 ok( thresholds->critical->end == 60, "Critical set correctly"); 180 ok(thresholds->critical->end == 60, "Critical set correctly");
183 ok( get_status(15.3, thresholds) == STATE_OK, "15.3 - ok"); 181 ok(get_status(15.3, thresholds) == STATE_OK, "15.3 - ok");
184 ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning"); 182 ok(get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
185 ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical"); 183 ok(get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
186 184
187 rc = _set_thresholds(&thresholds, "-10:-2", "-30:20"); 185 rc = _set_thresholds(&thresholds, "-10:-2", "-30:20");
188 ok( rc == 0, "Thresholds ('-30:20', '-10:-2') set"); 186 ok(rc == 0, "Thresholds ('-30:20', '-10:-2') set");
189 ok( thresholds->warning->start == -10, "Warning start set correctly"); 187 ok(thresholds->warning->start == -10, "Warning start set correctly");
190 ok( thresholds->warning->end == -2, "Warning end set correctly"); 188 ok(thresholds->warning->end == -2, "Warning end set correctly");
191 ok( thresholds->critical->start == -30, "Critical start set correctly"); 189 ok(thresholds->critical->start == -30, "Critical start set correctly");
192 ok( thresholds->critical->end == 20, "Critical end set correctly"); 190 ok(thresholds->critical->end == 20, "Critical end set correctly");
193 ok( get_status(-31, thresholds) == STATE_CRITICAL, "-31 - critical"); 191 ok(get_status(-31, thresholds) == STATE_CRITICAL, "-31 - critical");
194 ok( get_status(-29, thresholds) == STATE_WARNING, "-29 - warning"); 192 ok(get_status(-29, thresholds) == STATE_WARNING, "-29 - warning");
195 ok( get_status(-11, thresholds) == STATE_WARNING, "-11 - warning"); 193 ok(get_status(-11, thresholds) == STATE_WARNING, "-11 - warning");
196 ok( get_status(-10, thresholds) == STATE_OK, "-10 - ok"); 194 ok(get_status(-10, thresholds) == STATE_OK, "-10 - ok");
197 ok( get_status(-2, thresholds) == STATE_OK, "-2 - ok"); 195 ok(get_status(-2, thresholds) == STATE_OK, "-2 - ok");
198 ok( get_status(-1, thresholds) == STATE_WARNING, "-1 - warning"); 196 ok(get_status(-1, thresholds) == STATE_WARNING, "-1 - warning");
199 ok( get_status(19, thresholds) == STATE_WARNING, "19 - warning"); 197 ok(get_status(19, thresholds) == STATE_WARNING, "19 - warning");
200 ok( get_status(21, thresholds) == STATE_CRITICAL, "21 - critical"); 198 ok(get_status(21, thresholds) == STATE_CRITICAL, "21 - critical");
201 199
202 char *test; 200 char *test;
203 test = np_escaped_string("bob\\n"); 201 test = np_escaped_string("bob\\n");
204 ok( strcmp(test, "bob\n") == 0, "bob\\n ok"); 202 ok(strcmp(test, "bob\n") == 0, "bob\\n ok");
205 free(test); 203 free(test);
206 204
207 test = np_escaped_string("rhuba\\rb"); 205 test = np_escaped_string("rhuba\\rb");
208 ok( strcmp(test, "rhuba\rb") == 0, "rhuba\\rb okay"); 206 ok(strcmp(test, "rhuba\rb") == 0, "rhuba\\rb okay");
209 free(test); 207 free(test);
210 208
211 test = np_escaped_string("ba\\nge\\r"); 209 test = np_escaped_string("ba\\nge\\r");
212 ok( strcmp(test, "ba\nge\r") == 0, "ba\\nge\\r okay"); 210 ok(strcmp(test, "ba\nge\r") == 0, "ba\\nge\\r okay");
213 free(test); 211 free(test);
214 212
215 test = np_escaped_string("\\rabbi\\t"); 213 test = np_escaped_string("\\rabbi\\t");
216 ok( strcmp(test, "\rabbi\t") == 0, "\\rabbi\\t okay"); 214 ok(strcmp(test, "\rabbi\t") == 0, "\\rabbi\\t okay");
217 free(test); 215 free(test);
218 216
219 test = np_escaped_string("and\\\\or"); 217 test = np_escaped_string("and\\\\or");
220 ok( strcmp(test, "and\\or") == 0, "and\\\\or okay"); 218 ok(strcmp(test, "and\\or") == 0, "and\\\\or okay");
221 free(test); 219 free(test);
222 220
223 test = np_escaped_string("bo\\gus"); 221 test = np_escaped_string("bo\\gus");
224 ok( strcmp(test, "bogus") == 0, "bo\\gus okay"); 222 ok(strcmp(test, "bogus") == 0, "bo\\gus okay");
225 free(test); 223 free(test);
226 224
227 test = np_escaped_string("everything"); 225 test = np_escaped_string("everything");
228 ok( strcmp(test, "everything") == 0, "everything okay"); 226 ok(strcmp(test, "everything") == 0, "everything okay");
229 227
230 /* np_extract_ntpvar tests (23) */ 228 /* np_extract_ntpvar tests (23) */
231 test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foo"); 229 test = np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foo");
232 ok(test && !strcmp(test, "bar"), "1st test as expected"); 230 ok(test && !strcmp(test, "bar"), "1st test as expected");
233 free(test); 231 free(test);
234 232
235 test=np_extract_ntpvar("foo=bar,bar=foo,foobar=barfoo\n", "bar"); 233 test = np_extract_ntpvar("foo=bar,bar=foo,foobar=barfoo\n", "bar");
236 ok(test && !strcmp(test, "foo"), "2nd test as expected"); 234 ok(test && !strcmp(test, "foo"), "2nd test as expected");
237 free(test); 235 free(test);
238 236
239 test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foobar"); 237 test = np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foobar");
240 ok(test && !strcmp(test, "barfoo"), "3rd test as expected"); 238 ok(test && !strcmp(test, "barfoo"), "3rd test as expected");
241 free(test); 239 free(test);
242 240
243 test=np_extract_ntpvar("foo=bar\n", "foo"); 241 test = np_extract_ntpvar("foo=bar\n", "foo");
244 ok(test && !strcmp(test, "bar"), "Single test as expected"); 242 ok(test && !strcmp(test, "bar"), "Single test as expected");
245 free(test); 243 free(test);
246 244
247 test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfooi\n", "abcd"); 245 test = np_extract_ntpvar("foo=bar, bar=foo, foobar=barfooi\n", "abcd");
248 ok(!test, "Key not found 1"); 246 ok(!test, "Key not found 1");
249 247
250 test=np_extract_ntpvar("foo=bar\n", "abcd"); 248 test = np_extract_ntpvar("foo=bar\n", "abcd");
251 ok(!test, "Key not found 2"); 249 ok(!test, "Key not found 2");
252 250
253 test=np_extract_ntpvar("foo=bar=foobar", "foo"); 251 test = np_extract_ntpvar("foo=bar=foobar", "foo");
254 ok(test && !strcmp(test, "bar=foobar"), "Strange string 1"); 252 ok(test && !strcmp(test, "bar=foobar"), "Strange string 1");
255 free(test); 253 free(test);
256 254
257 test=np_extract_ntpvar("foo", "foo"); 255 test = np_extract_ntpvar("foo", "foo");
258 ok(!test, "Malformed string 1"); 256 ok(!test, "Malformed string 1");
259 257
260 test=np_extract_ntpvar("foo,", "foo"); 258 test = np_extract_ntpvar("foo,", "foo");
261 ok(!test, "Malformed string 2"); 259 ok(!test, "Malformed string 2");
262 260
263 test=np_extract_ntpvar("foo=", "foo"); 261 test = np_extract_ntpvar("foo=", "foo");
264 ok(!test, "Malformed string 3"); 262 ok(!test, "Malformed string 3");
265 263
266 test=np_extract_ntpvar("foo=,bar=foo", "foo"); 264 test = np_extract_ntpvar("foo=,bar=foo", "foo");
267 ok(!test, "Malformed string 4"); 265 ok(!test, "Malformed string 4");
268 266
269 test=np_extract_ntpvar(",foo", "foo"); 267 test = np_extract_ntpvar(",foo", "foo");
270 ok(!test, "Malformed string 5"); 268 ok(!test, "Malformed string 5");
271 269
272 test=np_extract_ntpvar("=foo", "foo"); 270 test = np_extract_ntpvar("=foo", "foo");
273 ok(!test, "Malformed string 6"); 271 ok(!test, "Malformed string 6");
274 272
275 test=np_extract_ntpvar("=foo,", "foo"); 273 test = np_extract_ntpvar("=foo,", "foo");
276 ok(!test, "Malformed string 7"); 274 ok(!test, "Malformed string 7");
277 275
278 test=np_extract_ntpvar(",,,", "foo"); 276 test = np_extract_ntpvar(",,,", "foo");
279 ok(!test, "Malformed string 8"); 277 ok(!test, "Malformed string 8");
280 278
281 test=np_extract_ntpvar("===", "foo"); 279 test = np_extract_ntpvar("===", "foo");
282 ok(!test, "Malformed string 9"); 280 ok(!test, "Malformed string 9");
283 281
284 test=np_extract_ntpvar(",=,=,", "foo"); 282 test = np_extract_ntpvar(",=,=,", "foo");
285 ok(!test, "Malformed string 10"); 283 ok(!test, "Malformed string 10");
286 284
287 test=np_extract_ntpvar("=,=,=", "foo"); 285 test = np_extract_ntpvar("=,=,=", "foo");
288 ok(!test, "Malformed string 11"); 286 ok(!test, "Malformed string 11");
289 287
290 test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foo"); 288 test = np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foo");
291 ok(test && !strcmp(test, "bar"), "Random spaces and newlines 1"); 289 ok(test && !strcmp(test, "bar"), "Random spaces and newlines 1");
292 free(test); 290 free(test);
293 291
294 test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "bar"); 292 test = np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "bar");
295 ok(test && !strcmp(test, "foo"), "Random spaces and newlines 2"); 293 ok(test && !strcmp(test, "foo"), "Random spaces and newlines 2");
296 free(test); 294 free(test);
297 295
298 test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foobar"); 296 test = np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foobar");
299 ok(test && !strcmp(test, "barfoo"), "Random spaces and newlines 3"); 297 ok(test && !strcmp(test, "barfoo"), "Random spaces and newlines 3");
300 free(test); 298 free(test);
301 299
302 test=np_extract_ntpvar(" foo=bar ,\n bar\n \n= \n foo\n , foobar=barfoo \n ", "bar"); 300 test = np_extract_ntpvar(" foo=bar ,\n bar\n \n= \n foo\n , foobar=barfoo \n ", "bar");
303 ok(test && !strcmp(test, "foo"), "Random spaces and newlines 4"); 301 ok(test && !strcmp(test, "foo"), "Random spaces and newlines 4");
304 free(test); 302 free(test);
305 303
306 test=np_extract_ntpvar("", "foo"); 304 test = np_extract_ntpvar("", "foo");
307 ok(!test, "Empty string return NULL"); 305 ok(!test, "Empty string return NULL");
308 306
309
310 /* This is the result of running ./test_utils */ 307 /* This is the result of running ./test_utils */
311 temp_string = (char *) _np_state_generate_key(); 308 temp_string = (char *)_np_state_generate_key();
312 ok(!strcmp(temp_string, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), "Got hash with exe and no parameters" ) || 309 ok(!strcmp(temp_string, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), "Got hash with exe and no parameters") ||
313 diag( "You are probably running in wrong directory. Must run as ./test_utils" ); 310 diag("You are probably running in wrong directory. Must run as ./test_utils");
314
315 311
316 this_monitoring_plugin->argc=4; 312 this_monitoring_plugin->argc = 4;
317 this_monitoring_plugin->argv[0] = "./test_utils"; 313 this_monitoring_plugin->argv[0] = "./test_utils";
318 this_monitoring_plugin->argv[1] = "here"; 314 this_monitoring_plugin->argv[1] = "here";
319 this_monitoring_plugin->argv[2] = "--and"; 315 this_monitoring_plugin->argv[2] = "--and";
320 this_monitoring_plugin->argv[3] = "now"; 316 this_monitoring_plugin->argv[3] = "now";
321 temp_string = (char *) _np_state_generate_key(); 317 temp_string = (char *)_np_state_generate_key();
322 ok(!strcmp(temp_string, "bd72da9f78ff1419fad921ea5e43ce56508aef6c"), "Got based on expected argv" ); 318 ok(!strcmp(temp_string, "bd72da9f78ff1419fad921ea5e43ce56508aef6c"), "Got based on expected argv");
323 319
324 unsetenv("MP_STATE_PATH"); 320 unsetenv("MP_STATE_PATH");
325 temp_string = (char *) _np_state_calculate_location_prefix(); 321 temp_string = (char *)_np_state_calculate_location_prefix();
326 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory" ); 322 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory");
327 323
328 setenv("MP_STATE_PATH", "", 1); 324 setenv("MP_STATE_PATH", "", 1);
329 temp_string = (char *) _np_state_calculate_location_prefix(); 325 temp_string = (char *)_np_state_calculate_location_prefix();
330 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string" ); 326 ok(!strcmp(temp_string, NP_STATE_DIR_PREFIX), "Got default directory even with empty string");
331 327
332 setenv("MP_STATE_PATH", "/usr/local/nagios/var", 1); 328 setenv("MP_STATE_PATH", "/usr/local/nagios/var", 1);
333 temp_string = (char *) _np_state_calculate_location_prefix(); 329 temp_string = (char *)_np_state_calculate_location_prefix();
334 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory" ); 330 ok(!strcmp(temp_string, "/usr/local/nagios/var"), "Got default directory");
335
336 331
332 ok(temp_state_key == NULL, "temp_state_key initially empty");
337 333
338 ok(temp_state_key==NULL, "temp_state_key initially empty"); 334 this_monitoring_plugin->argc = 1;
339
340 this_monitoring_plugin->argc=1;
341 this_monitoring_plugin->argv[0] = "./test_utils"; 335 this_monitoring_plugin->argv[0] = "./test_utils";
342 np_enable_state(NULL, 51); 336 np_enable_state(NULL, 51);
343 temp_state_key = this_monitoring_plugin->state; 337 temp_state_key = this_monitoring_plugin->state;
344 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 338 ok(!strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name");
345 ok( !strcmp(temp_state_key->name, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), "Got generated filename" ); 339 ok(!strcmp(temp_state_key->name, "e2d17f995fd4c020411b85e3e3d0ff7306d4147e"), "Got generated filename");
346
347 340
348 np_enable_state("allowedchars_in_keyname", 77); 341 np_enable_state("allowedchars_in_keyname", 77);
349 temp_state_key = this_monitoring_plugin->state; 342 temp_state_key = this_monitoring_plugin->state;
350 sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/allowedchars_in_keyname", (unsigned long)geteuid()); 343 sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/allowedchars_in_keyname", (unsigned long)geteuid());
351 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 344 ok(!strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name");
352 ok( !strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars" ); 345 ok(!strcmp(temp_state_key->name, "allowedchars_in_keyname"), "Got key name with valid chars");
353 ok( !strcmp(temp_state_key->_filename, state_path), "Got internal filename" ); 346 ok(!strcmp(temp_state_key->_filename, state_path), "Got internal filename");
354
355 347
356 /* Don't do this test just yet. Will die */ 348 /* Don't do this test just yet. Will die */
357 /* 349 /*
@@ -363,73 +355,64 @@ main (int argc, char **argv)
363 np_enable_state("funnykeyname", 54); 355 np_enable_state("funnykeyname", 54);
364 temp_state_key = this_monitoring_plugin->state; 356 temp_state_key = this_monitoring_plugin->state;
365 sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/funnykeyname", (unsigned long)geteuid()); 357 sprintf(state_path, "/usr/local/nagios/var/%lu/check_test/funnykeyname", (unsigned long)geteuid());
366 ok( !strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name" ); 358 ok(!strcmp(temp_state_key->plugin_name, "check_test"), "Got plugin name");
367 ok( !strcmp(temp_state_key->name, "funnykeyname"), "Got key name" ); 359 ok(!strcmp(temp_state_key->name, "funnykeyname"), "Got key name");
368 360
369 361 ok(!strcmp(temp_state_key->_filename, state_path), "Got internal filename");
370 362 ok(temp_state_key->data_version == 54, "Version set");
371 ok( !strcmp(temp_state_key->_filename, state_path), "Got internal filename" );
372 ok( temp_state_key->data_version==54, "Version set" );
373 363
374 temp_state_data = np_state_read(); 364 temp_state_data = np_state_read();
375 ok( temp_state_data==NULL, "Got no state data as file does not exist" ); 365 ok(temp_state_data == NULL, "Got no state data as file does not exist");
376
377 366
378/* 367 /*
379 temp_fp = fopen("var/statefile", "r"); 368 temp_fp = fopen("var/statefile", "r");
380 if (temp_fp==NULL) 369 if (temp_fp==NULL)
381 printf("Error opening. errno=%d\n", errno); 370 printf("Error opening. errno=%d\n", errno);
382 printf("temp_fp=%s\n", temp_fp); 371 printf("temp_fp=%s\n", temp_fp);
383 ok( _np_state_read_file(temp_fp) == true, "Can read state file" ); 372 ok( _np_state_read_file(temp_fp) == true, "Can read state file" );
384 fclose(temp_fp); 373 fclose(temp_fp);
385*/ 374 */
386 375
387 temp_state_key->_filename="var/statefile"; 376 temp_state_key->_filename = "var/statefile";
388 temp_state_data = np_state_read(); 377 temp_state_data = np_state_read();
389 ok( this_monitoring_plugin->state->state_data!=NULL, "Got state data now" ) || diag("Are you running in right directory? Will get coredump next if not"); 378 ok(this_monitoring_plugin->state->state_data != NULL, "Got state data now") || diag("Are you running in right directory? Will get coredump next if not");
390 ok( this_monitoring_plugin->state->state_data->time==1234567890, "Got time" ); 379 ok(this_monitoring_plugin->state->state_data->time == 1234567890, "Got time");
391 ok( !strcmp((char *)this_monitoring_plugin->state->state_data->data, "String to read"), "Data as expected" ); 380 ok(!strcmp((char *)this_monitoring_plugin->state->state_data->data, "String to read"), "Data as expected");
392 381
393 temp_state_key->data_version=53; 382 temp_state_key->data_version = 53;
394 temp_state_data = np_state_read(); 383 temp_state_data = np_state_read();
395 ok( temp_state_data==NULL, "Older data version gives NULL" ); 384 ok(temp_state_data == NULL, "Older data version gives NULL");
396 temp_state_key->data_version=54; 385 temp_state_key->data_version = 54;
397 386
398 temp_state_key->_filename="var/nonexistent"; 387 temp_state_key->_filename = "var/nonexistent";
399 temp_state_data = np_state_read(); 388 temp_state_data = np_state_read();
400 ok( temp_state_data==NULL, "Missing file gives NULL" ); 389 ok(temp_state_data == NULL, "Missing file gives NULL");
401 ok( this_monitoring_plugin->state->state_data==NULL, "No state information" ); 390 ok(this_monitoring_plugin->state->state_data == NULL, "No state information");
402 391
403 temp_state_key->_filename="var/oldformat"; 392 temp_state_key->_filename = "var/oldformat";
404 temp_state_data = np_state_read(); 393 temp_state_data = np_state_read();
405 ok( temp_state_data==NULL, "Old file format gives NULL" ); 394 ok(temp_state_data == NULL, "Old file format gives NULL");
406 395
407 temp_state_key->_filename="var/baddate"; 396 temp_state_key->_filename = "var/baddate";
408 temp_state_data = np_state_read(); 397 temp_state_data = np_state_read();
409 ok( temp_state_data==NULL, "Bad date gives NULL" ); 398 ok(temp_state_data == NULL, "Bad date gives NULL");
410 399
411 temp_state_key->_filename="var/missingdataline"; 400 temp_state_key->_filename = "var/missingdataline";
412 temp_state_data = np_state_read(); 401 temp_state_data = np_state_read();
413 ok( temp_state_data==NULL, "Missing data line gives NULL" ); 402 ok(temp_state_data == NULL, "Missing data line gives NULL");
414
415
416
417 403
418 unlink("var/generated"); 404 unlink("var/generated");
419 temp_state_key->_filename="var/generated"; 405 temp_state_key->_filename = "var/generated";
420 current_time=1234567890; 406 current_time = 1234567890;
421 np_state_write_string(current_time, "String to read"); 407 np_state_write_string(current_time, "String to read");
422 ok(system("cmp var/generated var/statefile")==0, "Generated file same as expected"); 408 ok(system("cmp var/generated var/statefile") == 0, "Generated file same as expected");
423
424
425
426 409
427 unlink("var/generated_directory/statefile"); 410 unlink("var/generated_directory/statefile");
428 unlink("var/generated_directory"); 411 unlink("var/generated_directory");
429 temp_state_key->_filename="var/generated_directory/statefile"; 412 temp_state_key->_filename = "var/generated_directory/statefile";
430 current_time=1234567890; 413 current_time = 1234567890;
431 np_state_write_string(current_time, "String to read"); 414 np_state_write_string(current_time, "String to read");
432 ok(system("cmp var/generated_directory/statefile var/statefile")==0, "Have created directory"); 415 ok(system("cmp var/generated_directory/statefile var/statefile") == 0, "Have created directory");
433 416
434 /* This test to check cannot write to dir - can't automate yet */ 417 /* This test to check cannot write to dir - can't automate yet */
435 /* 418 /*
@@ -438,15 +421,13 @@ main (int argc, char **argv)
438 np_state_write_string(current_time, "String to read"); 421 np_state_write_string(current_time, "String to read");
439 */ 422 */
440 423
441 424 temp_state_key->_filename = "var/generated";
442 temp_state_key->_filename="var/generated";
443 time(&current_time); 425 time(&current_time);
444 np_state_write_string(0, "String to read"); 426 np_state_write_string(0, "String to read");
445 temp_state_data = np_state_read(); 427 temp_state_data = np_state_read();
446 /* Check time is set to current_time */ 428 /* Check time is set to current_time */
447 ok(system("cmp var/generated var/statefile > /dev/null")!=0, "Generated file should be different this time"); 429 ok(system("cmp var/generated var/statefile > /dev/null") != 0, "Generated file should be different this time");
448 ok(this_monitoring_plugin->state->state_data->time-current_time<=1, "Has time generated from current time"); 430 ok(this_monitoring_plugin->state->state_data->time - current_time <= 1, "Has time generated from current time");
449
450 431
451 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */ 432 /* Don't know how to automatically test this. Need to be able to redefine die and catch the error */
452 /* 433 /*
@@ -454,23 +435,16 @@ main (int argc, char **argv)
454 np_state_write_string(0, "Bad file"); 435 np_state_write_string(0, "Bad file");
455 */ 436 */
456 437
457
458 np_cleanup(); 438 np_cleanup();
459 439
460 ok(this_monitoring_plugin==NULL, "Free'd this_monitoring_plugin"); 440 ok(this_monitoring_plugin == NULL, "Free'd this_monitoring_plugin");
461 441
462 ok(mp_suid() == false, "Test aren't suid"); 442 ok(mp_suid() == false, "Test aren't suid");
463 443
464 /* base states with random case */ 444 /* base states with random case */
465 char *states[] = { 445 char *states[] = {"Ok", "wArnINg", "cRiTIcaL", "UnKNoWN", NULL};
466 "Ok", 446
467 "wArnINg", 447 for (i = 0; states[i] != NULL; i++) {
468 "cRiTIcaL",
469 "UnKNoWN",
470 NULL
471 };
472
473 for (i=0; states[i]!=NULL; i++) {
474 /* out of the random case states, create the lower and upper versions + numeric string one */ 448 /* out of the random case states, create the lower and upper versions + numeric string one */
475 char *statelower = strdup(states[i]); 449 char *statelower = strdup(states[i]);
476 char *stateupper = strdup(states[i]); 450 char *stateupper = strdup(states[i]);
@@ -488,23 +462,23 @@ main (int argc, char **argv)
488 char testname[64] = "Translate state string: "; 462 char testname[64] = "Translate state string: ";
489 int tlen = strlen(testname); 463 int tlen = strlen(testname);
490 464
491 strcpy(testname+tlen, states[i]); 465 strcpy(testname + tlen, states[i]);
492 ok(i==mp_translate_state(states[i]), testname); 466 ok(i == mp_translate_state(states[i]), testname);
493 467
494 strcpy(testname+tlen, statelower); 468 strcpy(testname + tlen, statelower);
495 ok(i==mp_translate_state(statelower), testname); 469 ok(i == mp_translate_state(statelower), testname);
496 470
497 strcpy(testname+tlen, stateupper); 471 strcpy(testname + tlen, stateupper);
498 ok(i==mp_translate_state(stateupper), testname); 472 ok(i == mp_translate_state(stateupper), testname);
499 473
500 strcpy(testname+tlen, statenum); 474 strcpy(testname + tlen, statenum);
501 ok(i==mp_translate_state(statenum), testname); 475 ok(i == mp_translate_state(statenum), testname);
502 } 476 }
503 ok(ERROR==mp_translate_state("warningfewgw"), "Translate state string with garbage"); 477 ok(ERROR == mp_translate_state("warningfewgw"), "Translate state string with garbage");
504 ok(ERROR==mp_translate_state("00"), "Translate state string: bad numeric string 1"); 478 ok(ERROR == mp_translate_state("00"), "Translate state string: bad numeric string 1");
505 ok(ERROR==mp_translate_state("01"), "Translate state string: bad numeric string 2"); 479 ok(ERROR == mp_translate_state("01"), "Translate state string: bad numeric string 2");
506 ok(ERROR==mp_translate_state("10"), "Translate state string: bad numeric string 3"); 480 ok(ERROR == mp_translate_state("10"), "Translate state string: bad numeric string 3");
507 ok(ERROR==mp_translate_state(""), "Translate state string: empty string"); 481 ok(ERROR == mp_translate_state(""), "Translate state string: empty string");
508 482
509 return exit_status(); 483 return exit_status();
510} 484}