test incomplete slug

This commit is contained in:
c9s 2014-06-02 00:27:30 +08:00
parent 258128f30d
commit e1e5c3a4ae
2 changed files with 24 additions and 2 deletions

View file

@ -48,6 +48,11 @@ int slug_count(const char * needle, int len, char **errstr) {
char * p = (char*) needle; char * p = (char*) needle;
while( (p-needle) < len) { while( (p-needle) < len) {
if (*p == '\\' ) {
p++; p++;
}
if (state == 1 && *p == '}') { if (state == 1 && *p == '}') {
cnt++; cnt++;
} }
@ -58,10 +63,10 @@ int slug_count(const char * needle, int len, char **errstr) {
} }
p++; p++;
}; };
info("FOUND PATTERN: %s (%d), CHAR: '%s', STATE: %d\n", needle, len, p, state); info("FOUND PATTERN: '%s' (%d), STATE: %d\n", needle, len, state);
if (state != 0) { if (state != 0) {
if (errstr) { if (errstr) {
asprintf(errstr, "incomplete slug pattern. PATTERN: %s (%d), CHAR: '%s', STATE: %d\n", needle, len, p, state); asprintf(errstr, "incomplete slug pattern. PATTERN (%d): '%s' (%d), OFFSET: %d, STATE: %d", len, needle, p - needle, state);
} }
return 0; return 0;
} }

View file

@ -82,6 +82,18 @@ START_TEST (test_inside_slug)
} }
END_TEST END_TEST
START_TEST (test_incomplete_slug)
{
int cnt = 0;
char * errstr = NULL;
char * pattern = "/user/{name:\\d{3}}/to/{id";
cnt = slug_count(pattern, strlen(pattern), &errstr);
ck_assert_int_eq(cnt, 0);
ck_assert(errstr);
printf("%s\n",errstr);
}
END_TEST
START_TEST (test_slug_count) START_TEST (test_slug_count)
{ {
int cnt = 0; int cnt = 0;
@ -93,6 +105,10 @@ START_TEST (test_slug_count)
char * pattern2 = "/user/{name:\\d{3}}/to/{id}"; char * pattern2 = "/user/{name:\\d{3}}/to/{id}";
cnt = slug_count(pattern2, strlen(pattern2), &errstr); cnt = slug_count(pattern2, strlen(pattern2), &errstr);
ck_assert_int_eq(cnt, 2); ck_assert_int_eq(cnt, 2);
char * pattern3 = "/user/{name:\\d{3}}/to/{id}";
cnt = slug_count(pattern3, strlen(pattern3), &errstr);
ck_assert_int_eq(cnt, 2);
} }
END_TEST END_TEST
@ -117,6 +133,7 @@ Suite* r3_suite (void) {
tcase_add_test(tcase, test_slug_count); tcase_add_test(tcase, test_slug_count);
tcase_add_test(tcase, test_slug_compile); tcase_add_test(tcase, test_slug_compile);
tcase_add_test(tcase, test_pattern_to_opcode); tcase_add_test(tcase, test_pattern_to_opcode);
tcase_add_test(tcase, test_incomplete_slug);
suite_add_tcase(suite, tcase); suite_add_tcase(suite, tcase);
return suite; return suite;