Improve interface of np_expect_match() function

Replace the three boolean parameters of lib/utils_tcp.c's
np_expect_match() function with a single "flags" parameter.
This commit is contained in:
Holger Weiss 2013-09-12 17:42:10 +02:00
parent ca9ce71576
commit 662997251d
4 changed files with 32 additions and 32 deletions

View file

@ -33,21 +33,21 @@ 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) == TRUE,
"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) == TRUE,
"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("XX bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == FALSE,
"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) == FALSE,
"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) == FALSE,
"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) == TRUE,
"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) == FALSE,
"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) == FALSE,
"Test not matching any string (testing all)");

View file

@ -30,26 +30,27 @@
#include "utils_tcp.h"
int
np_expect_match(char* status, char** server_expect, int expect_count, int all, int exact_match, int verbose)
np_expect_match(char* status, char** server_expect, int expect_count, int flags)
{
int match = 0;
int i;
for (i = 0; i < expect_count; i++) {
if (verbose)
if (flags & NP_MATCH_VERBOSE)
printf ("looking for [%s] %s [%s]\n", server_expect[i],
(exact_match) ? "in beginning of" : "anywhere in",
(flags & NP_MATCH_EXACT) ? "in beginning of" : "anywhere in",
status);
if ((exact_match && !strncmp(status, server_expect[i], strlen(server_expect[i]))) ||
(! exact_match && strstr(status, server_expect[i])))
if ((flags & NP_MATCH_EXACT &&
!strncmp(status, server_expect[i], strlen(server_expect[i]))) ||
(!(flags & NP_MATCH_EXACT) && strstr(status, server_expect[i])))
{
if(verbose) puts("found it");
if(flags & NP_MATCH_VERBOSE) puts("found it");
match += 1;
} else
if(verbose) puts("couldn't find it");
if(flags & NP_MATCH_VERBOSE) puts("couldn't find it");
}
if ((all == TRUE && match == expect_count) ||
(! all && match >= 1)) {
if ((flags & NP_MATCH_ALL && match == expect_count) ||
(!(flags & NP_MATCH_ALL) && match >= 1)) {
return TRUE;
} else
return FALSE;

View file

@ -1,4 +1,8 @@
/* Header file for utils_tcp */
#define NP_MATCH_ALL 0x1
#define NP_MATCH_EXACT 0x2
#define NP_MATCH_VERBOSE 0x4
int np_expect_match(char* status, char** server_expect, int server_expect_count,
int all, int exact_match, int verbose);
int flags);

View file

@ -82,15 +82,14 @@ static int sd = 0;
#define MAXBUF 1024
static char buffer[MAXBUF];
static int expect_mismatch_state = STATE_WARNING;
static int match_flags = NP_MATCH_EXACT;
#define FLAG_SSL 0x01
#define FLAG_VERBOSE 0x02
#define FLAG_EXACT_MATCH 0x04
#define FLAG_TIME_WARN 0x08
#define FLAG_TIME_CRIT 0x10
#define FLAG_HIDE_OUTPUT 0x20
#define FLAG_MATCH_ALL 0x40
static size_t flags = FLAG_EXACT_MATCH;
#define FLAG_TIME_WARN 0x04
#define FLAG_TIME_CRIT 0x08
#define FLAG_HIDE_OUTPUT 0x10
static size_t flags;
int
main (int argc, char **argv)
@ -296,12 +295,7 @@ main (int argc, char **argv)
(int)len + 1, status);
while(isspace(status[len])) status[len--] = '\0';
match = np_expect_match(status,
server_expect,
server_expect_count,
(flags & FLAG_MATCH_ALL ? TRUE : FALSE),
(flags & FLAG_EXACT_MATCH ? TRUE : FALSE),
(flags & FLAG_VERBOSE ? TRUE : FALSE));
match = np_expect_match(status, server_expect, server_expect_count, match_flags);
}
if (server_quit != NULL) {
@ -450,6 +444,7 @@ process_arguments (int argc, char **argv)
exit (STATE_OK);
case 'v': /* verbose mode */
flags |= FLAG_VERBOSE;
match_flags |= NP_MATCH_VERBOSE;
break;
case '4':
address_family = AF_INET;
@ -506,7 +501,7 @@ process_arguments (int argc, char **argv)
xasprintf(&server_send, "%s", optarg);
break;
case 'e': /* expect string (may be repeated) */
flags &= ~FLAG_EXACT_MATCH;
match_flags &= ~NP_MATCH_EXACT;
if (server_expect_count == 0)
server_expect = malloc (sizeof (char *) * (++server_expect_count));
else
@ -584,7 +579,7 @@ process_arguments (int argc, char **argv)
#endif
break;
case 'A':
flags |= FLAG_MATCH_ALL;
match_flags |= NP_MATCH_ALL;
break;
}
}