From 9413628b875ef912027281f25d5ed340bb893ae0 Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Thu, 27 Nov 2008 09:21:52 +0000 Subject: [PATCH] token enumerator missed the last token if it contains only a single char --- .../unit_tester/tests/test_enumerator.c | 50 +++++++++++++++++-- src/libstrongswan/utils/enumerator.c | 2 +- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/charon/plugins/unit_tester/tests/test_enumerator.c b/src/charon/plugins/unit_tester/tests/test_enumerator.c index a7f3dd822..32d3ff8d2 100644 --- a/src/charon/plugins/unit_tester/tests/test_enumerator.c +++ b/src/charon/plugins/unit_tester/tests/test_enumerator.c @@ -226,18 +226,24 @@ bool test_enumerate_token() char *string; char *sep; char *trim; - } tests[] = { + } tests1[] = { {"abc, cde, efg", ",", " "}, {" abc 1:2 cde;3 4efg5. ", ":;.,", " 12345"}, {"abc.cde,efg", ",.", ""}, {" abc cde efg ", " ", " "}, + }, tests2[] = { + {"a, b, c", ",", " "}, + {"a,b,c", ",", " "}, + {" a 1:2 b;3 4c5. ", ":;.,", " 12345"}, + {"a.b,c", ",.", ""}, + {" a b c ", " ", " "}, }; - for (num = 0; num < countof(tests); num++) + for (num = 0; num < countof(tests1); num++) { i = 0; - enumerator = enumerator_create_token( - tests[num].string, tests[num].sep, tests[num].trim); + enumerator = enumerator_create_token(tests1[num].string, + tests1[num].sep, tests1[num].trim); while (enumerator->enumerate(enumerator, &token)) { switch (i) @@ -256,9 +262,43 @@ bool test_enumerate_token() } i++; } + if (i != 3) + { + return FALSE; + } enumerator->destroy(enumerator); } - + + for (num = 0; num < countof(tests2); num++) + { + i = 0; + enumerator = enumerator_create_token(tests2[num].string, + tests2[num].sep, tests2[num].trim); + while (enumerator->enumerate(enumerator, &token)) + { + switch (i) + { + case 0: + if (!streq(token, "a")) return FALSE; + break; + case 1: + if (!streq(token, "b")) return FALSE; + break; + case 2: + if (!streq(token, "c")) return FALSE; + break; + default: + return FALSE; + } + i++; + } + if (i != 3) + { + return FALSE; + } + enumerator->destroy(enumerator); + } + return TRUE; } diff --git a/src/libstrongswan/utils/enumerator.c b/src/libstrongswan/utils/enumerator.c index 1d1c5e9a2..ad8f7ad02 100644 --- a/src/libstrongswan/utils/enumerator.c +++ b/src/libstrongswan/utils/enumerator.c @@ -268,7 +268,7 @@ static bool enumerate_token_enum(token_enum_t *this, char **token) } } - if (!last || pos > *token) + if (!last || pos >= *token) { return TRUE; }