From: Richard Russon Date: Wed, 1 May 2019 00:07:20 +0000 (+0100) Subject: test: move rfc2047 tests X-Git-Tag: 2019-10-25~228^2~4 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=03f99046f4e34f0d279e9863294a7ac4e27f8828;p=neomutt test: move rfc2047 tests --- diff --git a/email/rfc2047.c b/email/rfc2047.c index b7ade7586..5857cb355 100644 --- a/email/rfc2047.c +++ b/email/rfc2047.c @@ -625,7 +625,7 @@ static int encode(const char *d, size_t dlen, int col, const char *fromcode, */ void rfc2047_encode(char **pd, const char *specials, int col, const char *charsets) { - if (!C_Charset || !*pd) + if (!C_Charset || !pd || !*pd) return; if (!charsets || !*charsets) diff --git a/test/Makefile.autosetup b/test/Makefile.autosetup index ff1cb1a62..886b8cdb4 100644 --- a/test/Makefile.autosetup +++ b/test/Makefile.autosetup @@ -1,7 +1,6 @@ TEST_OBJS = test/main.o \ test/buffer.o \ test/md5.o \ - test/rfc2047.o \ test/address.o \ test/url.o @@ -361,6 +360,7 @@ REGEX_OBJS = test/regex/main.o \ test/regex/mutt_replacelist_remove.o RFC2047_OBJS = test/rfc2047/main.o \ + test/rfc2047/common.o \ test/rfc2047/rfc2047_decode_addrlist.o \ test/rfc2047/rfc2047_decode.o \ test/rfc2047/rfc2047_decode_envelope.o \ diff --git a/test/main.c b/test/main.c index e251c900a..6bb1a988e 100644 --- a/test/main.c +++ b/test/main.c @@ -26,7 +26,6 @@ * Add your test cases to this list. *****************************************************************************/ #define NEOMUTT_TEST_LIST \ - NEOMUTT_TEST_ITEM(test_rfc2047) \ NEOMUTT_TEST_ITEM(test_md5) \ NEOMUTT_TEST_ITEM(test_md5_ctx) \ NEOMUTT_TEST_ITEM(test_md5_ctx_bytes) \ diff --git a/test/rfc2047.c b/test/rfc2047/common.c similarity index 57% rename from test/rfc2047.c rename to test/rfc2047/common.c index baad7c249..0bf793610 100644 --- a/test/rfc2047.c +++ b/test/rfc2047/common.c @@ -1,6 +1,6 @@ /** * @file - * Test code for RFC2047 Encoding + * Common code for RFC2047 tests * * @authors * Copyright (C) 2018 Pietro Cerutti @@ -22,21 +22,14 @@ #define TEST_NO_MAIN #include "acutest.h" - -#include "mutt/charset.h" -#include "mutt/memory.h" -#include "mutt/string2.h" -#include "email/rfc2047.h" - +#include "config.h" #include +#include "mutt/mutt.h" +#include "email/lib.h" +#include "common.h" -static const struct -{ - const char *original; /* the string as received in the original email */ - const char *decoded; /* the expected plain-text string */ - const char *encoded; /* the string as it's encoded by NeoMutt */ -} test_data[] = - /* clang-format off */ +// clang-format off +const struct Rfc2047TestData test_data[] = { { /* The string is split in the middle of a multi-byte sequence */ @@ -77,54 +70,8 @@ static const struct "=?UTF-8?Q?Sicherheitsl=C3=BCcke in praktisch allen IT-Systemen?=" , "Sicherheitslücke in praktisch allen IT-Systemen" , "=?utf-8?Q?Sicherheitsl=C3=BCcke?= in praktisch allen IT-Systemen" - } + }, + { NULL, NULL, NULL }, }; -/* clang-format on */ - -void test_rfc2047(void) -{ - if (!TEST_CHECK((setlocale(LC_ALL, "en_US.UTF-8") != NULL) || - (setlocale(LC_ALL, "C.UTF-8") != NULL))) - { - TEST_MSG("Cannot set locale to (en_US|C).UTF-8"); - return; - } - - C_Charset = "utf-8"; - - for (size_t i = 0; i < mutt_array_size(test_data); ++i) - { - /* decode the original string */ - char *s = mutt_str_strdup(test_data[i].original); - rfc2047_decode(&s); - if (!TEST_CHECK(strcmp(s, test_data[i].decoded) == 0)) - { - TEST_MSG("Iteration: %zu", i); - TEST_MSG("Expected : %s", test_data[i].decoded); - TEST_MSG("Actual : %s", s); - } - FREE(&s); - - /* encode the expected result */ - s = mutt_str_strdup(test_data[i].decoded); - rfc2047_encode(&s, NULL, 0, "utf-8"); - if (!TEST_CHECK(strcmp(s, test_data[i].encoded) == 0)) - { - TEST_MSG("Iteration: %zu", i); - TEST_MSG("Expected : %s", test_data[i].encoded); - TEST_MSG("Actual : %s", s); - } - FREE(&s); +// clang-format on - /* decode the encoded result */ - s = mutt_str_strdup(test_data[i].encoded); - rfc2047_decode(&s); - if (!TEST_CHECK(strcmp(s, test_data[i].decoded) == 0)) - { - TEST_MSG("Iteration: %zu", i); - TEST_MSG("Expected : %s", test_data[i].decoded); - TEST_MSG("Actual : %s", s); - } - FREE(&s); - } -} diff --git a/test/rfc2047/common.h b/test/rfc2047/common.h new file mode 100644 index 000000000..d7a4379af --- /dev/null +++ b/test/rfc2047/common.h @@ -0,0 +1,37 @@ +/** + * @file + * Common code for RFC2047 tests + * + * @authors + * Copyright (C) 2018 Pietro Cerutti + * + * @copyright + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 2 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#define TEST_NO_MAIN +#include "acutest.h" +#include +#include "mutt/mutt.h" +#include "email/lib.h" + +struct Rfc2047TestData +{ + const char *original; // the string as received in the original email + const char *decoded; // the expected plain-text string + const char *encoded; // the string as it's encoded by NeoMutt +}; + +extern const struct Rfc2047TestData test_data[]; + diff --git a/test/rfc2047/rfc2047_decode.c b/test/rfc2047/rfc2047_decode.c index f9e9ab215..6ce9edc46 100644 --- a/test/rfc2047/rfc2047_decode.c +++ b/test/rfc2047/rfc2047_decode.c @@ -26,11 +26,21 @@ #include "mutt/mutt.h" #include "email/lib.h" #include "address/lib.h" +#include "common.h" void test_rfc2047_decode(void) { // void rfc2047_decode(char **pd); + if (!TEST_CHECK((setlocale(LC_ALL, "en_US.UTF-8") != NULL) || + (setlocale(LC_ALL, "C.UTF-8") != NULL))) + { + TEST_MSG("Cannot set locale to (en_US|C).UTF-8"); + return; + } + + C_Charset = "utf-8"; + { rfc2047_decode(NULL); TEST_CHECK_(1, "rfc2047_decode(NULL)"); @@ -41,4 +51,31 @@ void test_rfc2047_decode(void) rfc2047_decode(&pd); TEST_CHECK_(1, "rfc2047_decode(&pd)"); } + + { + for (size_t i = 0; test_data[i].original; i++) + { + /* decode the original string */ + char *s = mutt_str_strdup(test_data[i].original); + rfc2047_decode(&s); + if (!TEST_CHECK(strcmp(s, test_data[i].decoded) == 0)) + { + TEST_MSG("Iteration: %zu", i); + TEST_MSG("Expected : %s", test_data[i].decoded); + TEST_MSG("Actual : %s", s); + } + FREE(&s); + + /* decode the encoded result */ + s = mutt_str_strdup(test_data[i].encoded); + rfc2047_decode(&s); + if (!TEST_CHECK(strcmp(s, test_data[i].decoded) == 0)) + { + TEST_MSG("Iteration: %zu", i); + TEST_MSG("Expected : %s", test_data[i].decoded); + TEST_MSG("Actual : %s", s); + } + FREE(&s); + } + } } diff --git a/test/rfc2047/rfc2047_encode.c b/test/rfc2047/rfc2047_encode.c index 31ba95869..fd632ef08 100644 --- a/test/rfc2047/rfc2047_encode.c +++ b/test/rfc2047/rfc2047_encode.c @@ -26,11 +26,21 @@ #include "mutt/mutt.h" #include "email/lib.h" #include "address/lib.h" +#include "common.h" void test_rfc2047_encode(void) { // void rfc2047_encode(char **pd, const char *specials, int col, const char *charsets); + if (!TEST_CHECK((setlocale(LC_ALL, "en_US.UTF-8") != NULL) || + (setlocale(LC_ALL, "C.UTF-8") != NULL))) + { + TEST_MSG("Cannot set locale to (en_US|C).UTF-8"); + return; + } + + C_Charset = "utf-8"; + { rfc2047_encode(NULL, AddressSpecials, 0, "apple"); TEST_CHECK_(1, "rfc2047_encode(NULL, AddressSpecials, 0, \"apple\")"); @@ -47,4 +57,20 @@ void test_rfc2047_encode(void) rfc2047_encode(&pd, AddressSpecials, 0, NULL); TEST_CHECK_(1, "rfc2047_encode(&pd, AddressSpecials, 0, NULL)"); } + + { + for (size_t i = 0; test_data[i].decoded; i++) + { + /* encode the expected result */ + char *s = mutt_str_strdup(test_data[i].decoded); + rfc2047_encode(&s, NULL, 0, "utf-8"); + if (!TEST_CHECK(strcmp(s, test_data[i].encoded) == 0)) + { + TEST_MSG("Iteration: %zu", i); + TEST_MSG("Expected : %s", test_data[i].encoded); + TEST_MSG("Actual : %s", s); + } + FREE(&s); + } + } }