X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Ftests%2Ftest_silcregex.c;h=41c51ad3fc969a19d036f053b76b48b9946071a3;hb=8174e79f59a93437b0fe02378889e27df7bfc299;hp=6e79cb0a83346fbd7062a9b589960bf3a48719b1;hpb=96ae4adfa8757be98b0587941e26d0733e1fb22e;p=crypto.git diff --git a/lib/silcutil/tests/test_silcregex.c b/lib/silcutil/tests/test_silcregex.c index 6e79cb0a..41c51ad3 100644 --- a/lib/silcutil/tests/test_silcregex.c +++ b/lib/silcutil/tests/test_silcregex.c @@ -9,6 +9,7 @@ int main(int argc, char **argv) SilcRegexMatchStruct match[10]; int i, num_match = 10; char *regex, *string, *sub; + SilcBufferStruct bmatch; if (argc > 1 && !strcmp(argv[1], "-d")) { silc_log_debug(TRUE); @@ -17,6 +18,210 @@ int main(int argc, char **argv) silc_log_set_debug_string("*regex*,*errno*"); } + regex = ".{5}"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "abcdefghijklmn"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "....."; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "abcdefghijklmn"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "^a{0}$"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = ""; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "^a{0,}$"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "bbbb"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "^a{0,}$"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "aaaaaaaaa"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "^a{0,0}$"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "a"; + SILC_LOG_DEBUG(("DO NOT Match %s", string)); + if (silc_regex(string, regex, &bmatch, NULL)) + goto err; + + regex = "^a{3}$"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "aaa"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "^a{3}$"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "aaaa"; + SILC_LOG_DEBUG(("DO NOT Match %s", string)); + if (silc_regex(string, regex, &bmatch, NULL)) + goto err; + + regex = "^a{3,5}$"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "aaa"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "^a{3,5}$"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "aaaa"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "^a{3,5}$"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "aaaaaa"; + SILC_LOG_DEBUG(("DO NOT Match %s", string)); + if (silc_regex(string, regex, &bmatch, NULL)) + goto err; + + regex = "^a{3,}$"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "aaa"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "^a{3,}$"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "aaaaaaaaaaaaa"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "^a{3,}$"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "aa"; + SILC_LOG_DEBUG(("DO NOT Match %s", string)); + if (silc_regex(string, regex, &bmatch, NULL)) + goto err; + + + regex = "a*b"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "b"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "a*b"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "ab"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "a*b"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "aaaab"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + + regex = "a+b"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "ab"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "a+b"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "aaaab"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "a+b"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "b"; + SILC_LOG_DEBUG(("DO NOT Match %s", string)); + if (silc_regex(string, regex, &bmatch, NULL)) + goto err; + + + regex = "ca?b"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "cb"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "ca?b"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "cab"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex(string, regex, &bmatch, NULL)) + goto err; + silc_buffer_printf(&bmatch, TRUE); + + regex = "ca?b"; + SILC_LOG_DEBUG(("Regex %s", regex)); + string = "caab"; + SILC_LOG_DEBUG(("DO NOT Match %s", string)); + if (silc_regex(string, regex, &bmatch, NULL)) + goto err; + + + regex = "(H..).(o..)"; + SILC_LOG_DEBUG(("Regex %s", regex)); + if (!silc_regex_compile(®, regex, 0)) + goto err; + + string = "Hello World"; + SILC_LOG_DEBUG(("Match %s", string)); + if (!silc_regex_match(®, string, strlen(string), num_match, match, 0)) + goto err; + for (i = 0; i < num_match; i++) { + if (match[i].start != -1) { + SILC_LOG_DEBUG(("Match start %d, end %d", match[i].start, + match[i].end)); + sub = silc_memdup(string + match[i].start, match[i].end - + match[i].start); + SILC_LOG_DEBUG(("Match substring '%s'", sub)); + silc_free(sub); + } + } + + silc_regex_free(®); + regex = "foo[0-9]*"; SILC_LOG_DEBUG(("Regex %s", regex)); if (!silc_regex_compile(®, regex, 0)) @@ -24,17 +229,17 @@ int main(int argc, char **argv) string = "foo"; SILC_LOG_DEBUG(("Match %s", string)); - if (!silc_regex_match(®, string, 0, NULL, 0)) + if (!silc_regex_match(®, string, strlen(string), 0, NULL, 0)) goto err; string = "foo20"; SILC_LOG_DEBUG(("Match %s", string)); - if (!silc_regex_match(®, string, 0, NULL, 0)) + if (!silc_regex_match(®, string, strlen(string), 0, NULL, 0)) goto err; string = "foo20, bar, foo100, foo"; SILC_LOG_DEBUG(("Match all substrings in %s", string)); - while (silc_regex_match(®, string, 1, match, 0)) { + while (silc_regex_match(®, string, strlen(string), 1, match, 0)) { SILC_LOG_DEBUG(("Match start %d", match[0].start)); sub = silc_memdup(string + match[0].start, match[0].end - match[0].start); SILC_LOG_DEBUG(("Match substring '%s'", sub)); @@ -44,7 +249,7 @@ int main(int argc, char **argv) string = "foo20, bar, foo100, Foo, foo0"; SILC_LOG_DEBUG(("Match all substrings at once in %s", string)); - if (!silc_regex_match(®, string, num_match, match, 0)) + if (!silc_regex_match(®, string, strlen(string), num_match, match, 0)) goto err; for (i = 0; i < num_match; i++) { @@ -66,7 +271,7 @@ int main(int argc, char **argv) string = "http://silcnet.org:443/foobar/pelle.html"; SILC_LOG_DEBUG(("Parse URI")); - if (!silc_regex_match(®, string, num_match, match, 0)) + if (!silc_regex_match(®, string, strlen(string), num_match, match, 0)) goto err; for (i = 0; i < num_match; i++) { @@ -81,7 +286,7 @@ int main(int argc, char **argv) string = "http://silcnet.org/"; SILC_LOG_DEBUG(("Parse URI")); - if (!silc_regex_match(®, string, num_match, match, 0)) + if (!silc_regex_match(®, string, strlen(string), num_match, match, 0)) goto err; for (i = 0; i < num_match; i++) { @@ -103,7 +308,7 @@ int main(int argc, char **argv) string = "ab"; SILC_LOG_DEBUG(("Match all substrings at once in %s", string)); - if (!silc_regex_match(®, string, num_match, match, 0)) + if (!silc_regex_match(®, string, strlen(string), num_match, match, 0)) goto err; for (i = 0; i < num_match; i++) { @@ -118,6 +323,38 @@ int main(int argc, char **argv) silc_regex_free(®); + regex = "^a"; + SILC_LOG_DEBUG(("Regex %s", regex)); + if (!silc_regex_compile(®, regex, 0)) + goto err; + + string = "a"; + SILC_LOG_DEBUG(("Test NOTBOL flag", string)); + if (silc_regex_match(®, string, strlen(string), 0, NULL, + SILC_REGEX_NOTBOL)) + goto err; + if (silc_errno != SILC_ERR_NOT_FOUND) + goto err; + SILC_LOG_DEBUG(("Did not match (OK)")); + + silc_regex_free(®); + + regex = "a$"; + SILC_LOG_DEBUG(("Regex %s", regex)); + if (!silc_regex_compile(®, regex, 0)) + goto err; + + string = "a"; + SILC_LOG_DEBUG(("Test NOTEOL flag", string)); + if (silc_regex_match(®, string, strlen(string), 0, NULL, + SILC_REGEX_NOTEOL)) + goto err; + if (silc_errno != SILC_ERR_NOT_FOUND) + goto err; + SILC_LOG_DEBUG(("Did not match (OK)")); + + silc_regex_free(®); + success = TRUE; err: