diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/tests/test_tcp.c | 16 | ||||
-rw-r--r-- | lib/utils_tcp.c | 19 | ||||
-rw-r--r-- | lib/utils_tcp.h | 6 |
3 files changed, 23 insertions, 18 deletions
diff --git a/lib/tests/test_tcp.c b/lib/tests/test_tcp.c index 6cf93946..8e9d43c8 100644 --- a/lib/tests/test_tcp.c +++ b/lib/tests/test_tcp.c | |||
@@ -33,21 +33,21 @@ main (int argc, char **argv) | |||
33 | server_expect[1] = strdup("bb"); | 33 | server_expect[1] = strdup("bb"); |
34 | server_expect[2] = strdup("CC"); | 34 | server_expect[2] = strdup("CC"); |
35 | 35 | ||
36 | ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == TRUE, | 36 | ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == TRUE, |
37 | "Test matching any string at the beginning (first expect string)"); | 37 | "Test matching any string at the beginning (first expect string)"); |
38 | ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == TRUE, | 38 | ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == TRUE, |
39 | "Test matching any string at the beginning (second expect string)"); | 39 | "Test matching any string at the beginning (second expect string)"); |
40 | ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == FALSE, | 40 | ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == FALSE, |
41 | "Test with strings not matching at the beginning"); | 41 | "Test with strings not matching at the beginning"); |
42 | ok(np_expect_match("XX CC XX", server_expect, server_expect_count, FALSE, TRUE, FALSE) == FALSE, | 42 | ok(np_expect_match("XX CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == FALSE, |
43 | "Test matching any string"); | 43 | "Test matching any string"); |
44 | ok(np_expect_match("XX", server_expect, server_expect_count, FALSE, FALSE, FALSE) == FALSE, | 44 | ok(np_expect_match("XX", server_expect, server_expect_count, 0) == FALSE, |
45 | "Test not matching any string"); | 45 | "Test not matching any string"); |
46 | ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == TRUE, | 46 | ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == TRUE, |
47 | "Test matching all strings"); | 47 | "Test matching all strings"); |
48 | ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == FALSE, | 48 | ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == FALSE, |
49 | "Test not matching all strings"); | 49 | "Test not matching all strings"); |
50 | ok(np_expect_match("XX XX", server_expect, server_expect_count, TRUE, FALSE, FALSE) == FALSE, | 50 | ok(np_expect_match("XX XX", server_expect, server_expect_count, NP_MATCH_ALL) == FALSE, |
51 | "Test not matching any string (testing all)"); | 51 | "Test not matching any string (testing all)"); |
52 | 52 | ||
53 | 53 | ||
diff --git a/lib/utils_tcp.c b/lib/utils_tcp.c index 8589ce67..cf67b116 100644 --- a/lib/utils_tcp.c +++ b/lib/utils_tcp.c | |||
@@ -30,26 +30,27 @@ | |||
30 | #include "utils_tcp.h" | 30 | #include "utils_tcp.h" |
31 | 31 | ||
32 | int | 32 | int |
33 | np_expect_match(char* status, char** server_expect, int expect_count, int all, int exact_match, int verbose) | 33 | np_expect_match(char* status, char** server_expect, int expect_count, int flags) |
34 | { | 34 | { |
35 | int match = 0; | 35 | int match = 0; |
36 | int i; | 36 | int i; |
37 | for (i = 0; i < expect_count; i++) { | 37 | for (i = 0; i < expect_count; i++) { |
38 | if (verbose) | 38 | if (flags & NP_MATCH_VERBOSE) |
39 | printf ("looking for [%s] %s [%s]\n", server_expect[i], | 39 | printf ("looking for [%s] %s [%s]\n", server_expect[i], |
40 | (exact_match) ? "in beginning of" : "anywhere in", | 40 | (flags & NP_MATCH_EXACT) ? "in beginning of" : "anywhere in", |
41 | status); | 41 | status); |
42 | 42 | ||
43 | if ((exact_match && !strncmp(status, server_expect[i], strlen(server_expect[i]))) || | 43 | if ((flags & NP_MATCH_EXACT && |
44 | (! exact_match && strstr(status, server_expect[i]))) | 44 | !strncmp(status, server_expect[i], strlen(server_expect[i]))) || |
45 | (!(flags & NP_MATCH_EXACT) && strstr(status, server_expect[i]))) | ||
45 | { | 46 | { |
46 | if(verbose) puts("found it"); | 47 | if(flags & NP_MATCH_VERBOSE) puts("found it"); |
47 | match += 1; | 48 | match += 1; |
48 | } else | 49 | } else |
49 | if(verbose) puts("couldn't find it"); | 50 | if(flags & NP_MATCH_VERBOSE) puts("couldn't find it"); |
50 | } | 51 | } |
51 | if ((all == TRUE && match == expect_count) || | 52 | if ((flags & NP_MATCH_ALL && match == expect_count) || |
52 | (! all && match >= 1)) { | 53 | (!(flags & NP_MATCH_ALL) && match >= 1)) { |
53 | return TRUE; | 54 | return TRUE; |
54 | } else | 55 | } else |
55 | return FALSE; | 56 | return FALSE; |
diff --git a/lib/utils_tcp.h b/lib/utils_tcp.h index b0eb8bee..34b771d6 100644 --- a/lib/utils_tcp.h +++ b/lib/utils_tcp.h | |||
@@ -1,4 +1,8 @@ | |||
1 | /* Header file for utils_tcp */ | 1 | /* Header file for utils_tcp */ |
2 | 2 | ||
3 | #define NP_MATCH_ALL 0x1 | ||
4 | #define NP_MATCH_EXACT 0x2 | ||
5 | #define NP_MATCH_VERBOSE 0x4 | ||
6 | |||
3 | int np_expect_match(char* status, char** server_expect, int server_expect_count, | 7 | int np_expect_match(char* status, char** server_expect, int server_expect_count, |
4 | int all, int exact_match, int verbose); | 8 | int flags); |