From 77c7c2305f4ff5bd60258e53f2a9aec0c486d1af Mon Sep 17 00:00:00 2001 From: Ali Fardan Date: Sun, 25 Oct 2020 14:18:23 +0300 Subject: deocde: handle whitespace in link lines properly --- decode.c | 14 ++++----- tests/test.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test23.gmi | 1 + tests/test24.gmi | 1 + tests/test25.gmi | 1 + 5 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 tests/test23.gmi create mode 100644 tests/test24.gmi create mode 100644 tests/test25.gmi diff --git a/decode.c b/decode.c index 756a7b8..6498fc9 100644 --- a/decode.c +++ b/decode.c @@ -64,17 +64,17 @@ _case_link(char **text) memset(ret, 0, sizeof(*ret)); ret->type = GEMTEXT_LINK; - if (*(ptr+2) == ' ') - ptr += 3; - else - ptr += 2; + ptr += 2; /* skip "=>" */ + while (*ptr == ' ' || *ptr == '\t') + ptr++; cpy = ptr; - while (*ptr != '\r' && *ptr != '\n' && *ptr != ' ' && *ptr != '\0') + while (*ptr != '\r' && *ptr != '\n' && *ptr != ' ' && *ptr != '\t' && *ptr != '\0') ptr++; ret->link = strndup(cpy, ptr-cpy); - if (*ptr == ' ') { - ptr++; + if (*ptr == ' ' || *ptr == '\t') { + while(*ptr == ' ' || *ptr == '\t') + ptr++; cpy = ptr; while (*ptr != '\r' && *ptr != '\n' && *ptr != '\0') ptr++; diff --git a/tests/test.c b/tests/test.c index a1e6e21..0854add 100644 --- a/tests/test.c +++ b/tests/test.c @@ -84,6 +84,14 @@ const char *decodetest21 = "no one said"; const char *decodetest22 = "*I should be treated as plaintext line"; +const char *decodetest23_1 = +"link_but"; +const char *decodetest23_2 = +"separated with tabs and spaces"; +const char *decodetest24 = +"nospace"; +const char *decodetest25 = +"yesspace"; const char *encodetest1 = "### 2cu3nfTLaEpx1Ztbbvhy4Jg5Kj-D-bwXafbK8Ma73QGBTJap7u0Uvco_YO0X-H_d_tSbJh_N"; @@ -165,6 +173,12 @@ const char *encodetest21 = ">no one said"; const char *encodetest22 = "*I should be treated as plaintext line"; +const char *encodetest23 = +"=> link_but separated with tabs and spaces"; +const char *encodetest24 = +"=> nospace"; +const char *encodetest25 = +"=> yesspace"; static void @@ -736,6 +750,83 @@ test_22(void) PASS("Test22"); } +static void +test_23(void) +{ + struct gemtext_link *t; + char *text; + + t = gemtext_decode_file("test23.gmi"); + if (t == NULL) + FAIL("Test23"); + + if (t->type != GEMTEXT_LINK) + FAIL("Test23"); + if (strcmp(t->link, decodetest23_1) != 0) + FAIL("Test23"); + if (strcmp(t->name, decodetest23_2) != 0) + FAIL("Test23"); + + if (gemtext_encode((struct gemtext *)t, &text, NULL) == -1) + FAIL("Test23"); + if (strcmp(text, encodetest23) != 0) + FAIL("Test23"); + + free(text); + gemtext_free(t); + PASS("Test23"); +} + +static void +test_24(void) +{ + struct gemtext_link *t; + char *text; + + t = gemtext_decode_file("test24.gmi"); + if (t == NULL) + FAIL("Test24"); + + if (t->type != GEMTEXT_LINK) + FAIL("Test24"); + if (strcmp(t->link, decodetest24) != 0) + FAIL("Test24"); + + if (gemtext_encode((struct gemtext *)t, &text, NULL) == -1) + FAIL("Test24"); + if (strcmp(text, encodetest24) != 0) + FAIL("Test24"); + + free(text); + gemtext_free(t); + PASS("Test24"); +} + +static void +test_25(void) +{ + struct gemtext_link *t; + char *text; + + t = gemtext_decode_file("test25.gmi"); + if (t == NULL) + FAIL("Test25"); + + if (t->type != GEMTEXT_LINK) + FAIL("Test25"); + if (strcmp(t->link, decodetest25) != 0) + FAIL("Test25"); + + if (gemtext_encode((struct gemtext *)t, &text, NULL) == -1) + FAIL("Test25"); + if (strcmp(text, encodetest25) != 0) + FAIL("Test25"); + + free(text); + gemtext_free(t); + PASS("Test25"); +} + int main(void) { @@ -761,6 +852,9 @@ main(void) test_20(); test_21(); test_22(); + test_23(); + test_24(); + test_25(); return 0; } diff --git a/tests/test23.gmi b/tests/test23.gmi new file mode 100644 index 0000000..d852962 --- /dev/null +++ b/tests/test23.gmi @@ -0,0 +1 @@ +=> link_but separated with tabs and spaces diff --git a/tests/test24.gmi b/tests/test24.gmi new file mode 100644 index 0000000..069859a --- /dev/null +++ b/tests/test24.gmi @@ -0,0 +1 @@ +=>nospace diff --git a/tests/test25.gmi b/tests/test25.gmi new file mode 100644 index 0000000..da59c6d --- /dev/null +++ b/tests/test25.gmi @@ -0,0 +1 @@ +=> yesspace -- cgit 1.4.1-2-gfad0