Imported Upstream version 1.4.16+git20130912
This commit is contained in:
parent
ea90af2ba1
commit
01ca3b324f
14 changed files with 514 additions and 442 deletions
|
@ -21,11 +21,12 @@
|
|||
#include "tap.h"
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
main(void)
|
||||
{
|
||||
char** server_expect;
|
||||
char **server_expect;
|
||||
int server_expect_count = 3;
|
||||
plan_tests(8);
|
||||
|
||||
plan_tests(9);
|
||||
|
||||
server_expect = malloc(sizeof(char*) * server_expect_count);
|
||||
|
||||
|
@ -33,24 +34,25 @@ main (int argc, char **argv)
|
|||
server_expect[1] = strdup("bb");
|
||||
server_expect[2] = strdup("CC");
|
||||
|
||||
ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == TRUE,
|
||||
ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_SUCCESS,
|
||||
"Test matching any string at the beginning (first expect string)");
|
||||
ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == TRUE,
|
||||
ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_SUCCESS,
|
||||
"Test matching any string at the beginning (second expect string)");
|
||||
ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == FALSE,
|
||||
ok(np_expect_match("b", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_RETRY,
|
||||
"Test matching any string at the beginning (substring match)");
|
||||
ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_FAILURE,
|
||||
"Test with strings not matching at the beginning");
|
||||
ok(np_expect_match("XX CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == FALSE,
|
||||
ok(np_expect_match("XX CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_FAILURE,
|
||||
"Test matching any string");
|
||||
ok(np_expect_match("XX", server_expect, server_expect_count, FALSE, FALSE, FALSE) == FALSE,
|
||||
ok(np_expect_match("XX", server_expect, server_expect_count, 0) == NP_MATCH_RETRY,
|
||||
"Test not matching any string");
|
||||
ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == TRUE,
|
||||
ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == NP_MATCH_SUCCESS,
|
||||
"Test matching all strings");
|
||||
ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == FALSE,
|
||||
ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == NP_MATCH_RETRY,
|
||||
"Test not matching all strings");
|
||||
ok(np_expect_match("XX XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == FALSE,
|
||||
ok(np_expect_match("XX XX", server_expect, server_expect_count, NP_MATCH_ALL) == NP_MATCH_RETRY,
|
||||
"Test not matching any string (testing all)");
|
||||
|
||||
|
||||
return exit_status();
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
* Library for check_tcp
|
||||
*
|
||||
* License: GPL
|
||||
* Copyright (c) 1999-2007 Nagios Plugins Development Team
|
||||
* Copyright (c) 1999-2013 Nagios Plugins Development Team
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
|
@ -29,28 +29,47 @@
|
|||
#include "common.h"
|
||||
#include "utils_tcp.h"
|
||||
|
||||
int
|
||||
np_expect_match(char* status, char** server_expect, int expect_count, int all, int exact_match, int verbose)
|
||||
{
|
||||
int match = 0;
|
||||
int i;
|
||||
for (i = 0; i < expect_count; i++) {
|
||||
if (verbose)
|
||||
printf ("looking for [%s] %s [%s]\n", server_expect[i],
|
||||
(exact_match) ? "in beginning of" : "anywhere in",
|
||||
status);
|
||||
#define VERBOSE(message) \
|
||||
do { \
|
||||
if (flags & NP_MATCH_VERBOSE) \
|
||||
puts(message); \
|
||||
} while (0)
|
||||
|
||||
if ((exact_match && !strncmp(status, server_expect[i], strlen(server_expect[i]))) ||
|
||||
(! exact_match && strstr(status, server_expect[i])))
|
||||
{
|
||||
if(verbose) puts("found it");
|
||||
match += 1;
|
||||
} else
|
||||
if(verbose) puts("couldn't find it");
|
||||
enum np_match_result
|
||||
np_expect_match(char *status, char **server_expect, int expect_count, int flags)
|
||||
{
|
||||
int i, match = 0, partial = 0;
|
||||
|
||||
for (i = 0; i < expect_count; i++) {
|
||||
if (flags & NP_MATCH_VERBOSE)
|
||||
printf("looking for [%s] %s [%s]\n", server_expect[i],
|
||||
(flags & NP_MATCH_EXACT) ?
|
||||
"in beginning of" : "anywhere in",
|
||||
status);
|
||||
|
||||
if (flags & NP_MATCH_EXACT) {
|
||||
if (strncmp(status, server_expect[i], strlen(server_expect[i])) == 0) {
|
||||
VERBOSE("found it");
|
||||
match++;
|
||||
continue;
|
||||
} else if (strncmp(status, server_expect[i], strlen(status)) == 0) {
|
||||
VERBOSE("found a substring");
|
||||
partial++;
|
||||
continue;
|
||||
}
|
||||
} else if (strstr(status, server_expect[i]) != NULL) {
|
||||
VERBOSE("found it");
|
||||
match++;
|
||||
continue;
|
||||
}
|
||||
VERBOSE("couldn't find it");
|
||||
}
|
||||
if ((all == TRUE && match == expect_count) ||
|
||||
(! all && match >= 1)) {
|
||||
return TRUE;
|
||||
} else
|
||||
return FALSE;
|
||||
|
||||
if ((flags & NP_MATCH_ALL && match == expect_count) ||
|
||||
(!(flags & NP_MATCH_ALL) && match >= 1))
|
||||
return NP_MATCH_SUCCESS;
|
||||
else if (partial > 0 || !(flags & NP_MATCH_EXACT))
|
||||
return NP_MATCH_RETRY;
|
||||
else
|
||||
return NP_MATCH_FAILURE;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,22 @@
|
|||
/* Header file for utils_tcp */
|
||||
|
||||
int np_expect_match(char* status, char** server_expect, int server_expect_count,
|
||||
int all, int exact_match, int verbose);
|
||||
#define NP_MATCH_ALL 0x1
|
||||
#define NP_MATCH_EXACT 0x2
|
||||
#define NP_MATCH_VERBOSE 0x4
|
||||
|
||||
/*
|
||||
* The NP_MATCH_RETRY state indicates that matching might succeed if
|
||||
* np_expect_match() is called with a longer input string. This allows the
|
||||
* caller to decide whether it makes sense to wait for additional data from the
|
||||
* server.
|
||||
*/
|
||||
enum np_match_result {
|
||||
NP_MATCH_FAILURE,
|
||||
NP_MATCH_SUCCESS,
|
||||
NP_MATCH_RETRY
|
||||
};
|
||||
|
||||
enum np_match_result np_expect_match(char *status,
|
||||
char **server_expect,
|
||||
int server_expect_count,
|
||||
int flags);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue