From 07993b26c96d7ab0c20bfacd5352a0f131c23029 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Mon, 9 Sep 2019 16:43:41 +0200 Subject: [PATCH] Add unit tests for convertLF() and convertCRLF() --- tests/unit/CMakeLists.txt | 3 + tests/unit/convertlf.cxx | 124 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 tests/unit/convertlf.cxx diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 656c8a75..c847238d 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -3,5 +3,8 @@ include_directories(${CMAKE_SOURCE_DIR}/common) add_executable(conv conv.cxx) target_link_libraries(conv rfb) +add_executable(convertlf convertlf.cxx) +target_link_libraries(convertlf rfb) + add_executable(hostport hostport.cxx) target_link_libraries(hostport rfb) diff --git a/tests/unit/convertlf.cxx b/tests/unit/convertlf.cxx new file mode 100644 index 00000000..6bd98fe2 --- /dev/null +++ b/tests/unit/convertlf.cxx @@ -0,0 +1,124 @@ +/* Copyright 2019 Pierre Ossman for Cendio AB + * + * This 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 software 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 software; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +#include + +#include + +static const char* escape(const char* input) +{ + static char output[4096]; + + const char* in; + char* out; + + in = input; + out = output; + do { + if (*in == '\r') { + *out++ = '\\'; + *out++ = 'r'; + } else if (*in == '\n') { + *out++ = '\\'; + *out++ = 'n'; + } else { + *out++ = *in; + } + } while (*in++ != '\0'); + + return output; +} + +static void testLF(const char* input, const char* expected) +{ + char* output; + + printf("convertLF(\"%s\"): ", escape(input)); + + output = rfb::convertLF(input); + + if (strcmp(output, expected) != 0) + printf("FAILED: got \"%s\"", escape(output)); + else + printf("OK"); + printf("\n"); + fflush(stdout); + + rfb::strFree(output); +} + +static void testCRLF(const char* input, const char* expected) +{ + char* output; + + printf("convertCRLF(\"%s\"): ", escape(input)); + + output = rfb::convertCRLF(input); + + if (strcmp(output, expected) != 0) + printf("FAILED: got \"%s\"", escape(output)); + else + printf("OK"); + printf("\n"); + fflush(stdout); + + rfb::strFree(output); +} + +int main(int argc, char** argv) +{ + testLF("", ""); + testLF("no EOL", "no EOL"); + + testLF("\n", "\n"); + testLF("already correct\n", "already correct\n"); + testLF("multiple\nlines\n", "multiple\nlines\n"); + testLF("empty lines\n\n", "empty lines\n\n"); + testLF("\ninitial line", "\ninitial line"); + + testLF("\r\n", "\n"); + testLF("one line\r\n", "one line\n"); + testLF("multiple\r\nlines\r\n", "multiple\nlines\n"); + testLF("empty lines\r\n\r\n", "empty lines\n\n"); + testLF("\r\ninitial line", "\ninitial line"); + testLF("mixed\r\nlines\n", "mixed\nlines\n"); + + testLF("cropped\r", "cropped\n"); + testLF("old\rmac\rformat", "old\nmac\nformat"); + + testCRLF("", ""); + testCRLF("no EOL", "no EOL"); + + testCRLF("\r\n", "\r\n"); + testCRLF("already correct\r\n", "already correct\r\n"); + testCRLF("multiple\r\nlines\r\n", "multiple\r\nlines\r\n"); + testCRLF("empty lines\r\n\r\n", "empty lines\r\n\r\n"); + testCRLF("\r\ninitial line", "\r\ninitial line"); + + testCRLF("\n", "\r\n"); + testCRLF("one line\n", "one line\r\n"); + testCRLF("multiple\nlines\n", "multiple\r\nlines\r\n"); + testCRLF("empty lines\n\n", "empty lines\r\n\r\n"); + testCRLF("\ninitial line", "\r\ninitial line"); + testCRLF("mixed\r\nlines\n", "mixed\r\nlines\r\n"); + + testCRLF("cropped\r", "cropped\r\n"); + testCRLF("old\rmac\rformat", "old\r\nmac\r\nformat"); + + return 0; +} -- 2.39.5