diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-03-15 21:04:36 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-03-15 21:04:36 +0000 |
commit | 10fb4d1a18a0a0573ad84a5ec4e5dd2ba55fdaa3 (patch) | |
tree | 6183213b35be006d9cc12adac81c0b9e01d4d3a3 | |
parent | 92f20993f94c91ad03213c5fcc2984ee3b487a67 (diff) | |
download | rspamd-10fb4d1a18a0a0573ad84a5ec4e5dd2ba55fdaa3.tar.gz rspamd-10fb4d1a18a0a0573ad84a5ec4e5dd2ba55fdaa3.zip |
[Project] Css: Move some of the tests to the doctest
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/libserver/css/css.cxx | 5 | ||||
-rw-r--r-- | src/libserver/css/css_parser.cxx | 61 | ||||
-rw-r--r-- | src/libserver/css/css_parser.hxx | 2 | ||||
-rw-r--r-- | src/libserver/css/css_value.cxx | 15 | ||||
-rw-r--r-- | test/rspamd_cxx_unit.cxx | 15 |
6 files changed, 89 insertions, 11 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ce315822..a50bba36f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -251,7 +251,7 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_OPT_FLAGS} ${CMAKE_C_FLAGS}") SET(CMAKE_CXX_FLAGS "${CMAKE_C_OPT_FLAGS} ${CMAKE_CXX_FLAGS}") ADD_DEFINITIONS(-DHAVE_CONFIG_H) -ADD_DEFINITIONS(-DDOCTEST_CONFIG_SUPER_FAST_ASSERTS) +ADD_DEFINITIONS(-DDOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS) # Check platform specific includes CHECK_INCLUDE_FILES(sys/types.h HAVE_SYS_TYPES_H) diff --git a/src/libserver/css/css.cxx b/src/libserver/css/css.cxx index 49bab734d..4587085a8 100644 --- a/src/libserver/css/css.cxx +++ b/src/libserver/css/css.cxx @@ -28,6 +28,8 @@ rspamd_css_parse_style (rspamd_mempool_t *pool, const guchar *begin, gsize len, { auto parse_res = rspamd::css::parse_css(pool, {(const char* )begin, len}); +#if 0 + /* Return once semantical parsing is done */ if (parse_res.has_value()) { return reinterpret_cast<rspamd_css>(parse_res.value().release()); } @@ -37,6 +39,9 @@ rspamd_css_parse_style (rspamd_mempool_t *pool, const guchar *begin, gsize len, "parse error"); return nullptr; } +#else + return nullptr; +#endif } namespace rspamd::css { diff --git a/src/libserver/css/css_parser.cxx b/src/libserver/css/css_parser.cxx index e6213d30d..019849122 100644 --- a/src/libserver/css/css_parser.cxx +++ b/src/libserver/css/css_parser.cxx @@ -19,9 +19,12 @@ #include "css_selector.hxx" #include "css_rule.hxx" #include "fmt/core.h" + #include <vector> #include <unicode/utf8.h> +#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL +#include "doctest/doctest.h" namespace rspamd::css { @@ -117,9 +120,9 @@ auto css_consumed_block::debug_str(void) -> std::string { ret += R"("empty")"; } else if constexpr (std::is_same_v<T, css_function_block>) { - /* Empty block */ - ret += fmt::format(R"({ "content": {"token": "{}", "arguments": [)", - arg.function.debug_token_str()); + ret += R"({ "content": {"token": )"; + ret += "\"" + arg.function.debug_token_str() + "\", "; + ret += R"("arguments": [)"; for (const auto &block : arg.args) { ret += "{"; @@ -619,13 +622,59 @@ bool css_parser::consume_input(const std::string_view &sv) * Wrapper for the parser */ auto parse_css(rspamd_mempool_t *pool, const std::string_view &st) -> - tl::expected<std::unique_ptr<css_style_sheet>,css_parse_error> + bool { css_parser parser(pool); - parser.consume_input(st); + if (parser.consume_input(st)) { + return true; + } + + return false; +} - return parser.get_object_maybe(); +TEST_SUITE("css parser") { + TEST_CASE("parse colors") { + const std::vector<const char *> cases{ + "p { color: rgb(100%, 50%, 0%); opacity: -1; width: 1em; display: none; } /* very transparent solid orange */", + "p { color: rgb(100%, 50%, 0%); opacity: 2; display: inline; } /* very transparent solid orange */", + "p { color: rgb(100%, 50%, 0%); opacity: 0.5; } /* very transparent solid orange */\n", + "p { color: rgb(100%, 50%, 0%); opacity: 1; width: 99%; } /* very transparent solid orange */\n", + "p { color: rgb(100%, 50%, 0%); opacity: 10%; width: 99%; } /* very transparent solid orange */\n", + "p { color: rgb(100%, 50%, 0%); opacity: 10%; width: 100px; } /* very transparent solid orange */\n", + "p { color: rgb(100%, 50%, 0%); opacity: 10% } /* very transparent solid orange */\n", + "* { color: hsl(0, 100%, 50%) !important } /* red */\n", + "* { color: hsl(120, 100%, 50%) important } /* lime */\n", + "* { color: hsl(120, 100%, 25%) } /* dark green */\n", + "* { color: hsl(120, 100%, 75%) } /* light green */\n", + "* { color: hsl(120, 75%, 75%) } /* pastel green, and so on */\n", + "em { color: #f00 } /* #rgb */\n", + "em { color: #ff0000 } /* #rrggbb */\n", + "em { color: rgb(255,0,0) }\n", + "em { color: rgb(100%, 0%, 0%) }\n", + "body {color: black; background: white }\n", + "h1 { color: maroon }\n", + "h2 { color: olive }\n", + "em { color: rgb(255,0,0) } /* integer range 0 - 255 */\n", + "em { color: rgb(300,0,0) } /* clipped to rgb(255,0,0) */\n", + "em { color: rgb(255,-10,0) } /* clipped to rgb(255,0,0) */\n", + "em { color: rgb(110%, 0%, 0%) } /* clipped to rgb(100%,0%,0%) */\n", + "em { color: rgb(255,0,0) } /* integer range 0 - 255 */\n", + "em { color: rgba(255,0,0,1) /* the same, with explicit opacity of 1 */\n", + "em { color: rgb(100%,0%,0%) } /* float range 0.0% - 100.0% */\n", + "em { color: rgba(100%,0%,0%,1) } /* the same, with explicit opacity of 1 */\n", + "p { color: rgba(0,0,255,0.5) } /* semi-transparent solid blue */\n", + "p { color: rgba(100%, 50%, 0%, 0.1) } /* very transparent solid orange */", + }; + + rspamd_mempool_t *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(), + "css", 0); + for (const auto &c : cases) { + CHECK_UNARY(parse_css(pool, c)); + } + + rspamd_mempool_delete(pool); + } } } diff --git a/src/libserver/css/css_parser.hxx b/src/libserver/css/css_parser.hxx index d0c378b0f..034c135c6 100644 --- a/src/libserver/css/css_parser.hxx +++ b/src/libserver/css/css_parser.hxx @@ -186,7 +186,7 @@ extern const css_consumed_block css_parser_eof_block; using blocks_gen_functor = std::function<const css_consumed_block &(void)>; auto parse_css (rspamd_mempool_t *pool, const std::string_view &st) -> - tl::expected<std::unique_ptr<css_style_sheet>,css_parse_error>; + bool; } diff --git a/src/libserver/css/css_value.cxx b/src/libserver/css/css_value.cxx index 5e482b58f..9f1f4dd7f 100644 --- a/src/libserver/css/css_value.cxx +++ b/src/libserver/css/css_value.cxx @@ -386,10 +386,19 @@ TEST_SUITE("css values") { } } TEST_CASE("css colors strings") { + auto passed = 0; for (const auto &p : css_colors_map) { - auto col_parsed = css_value::maybe_color_from_string(p.first); - auto final_col = col_parsed.value().to_color().value(); - CHECK(final_col == p.second); + /* Match some of the colors selected randomly */ + if (rspamd_random_double_fast() > 0.9) { + auto col_parsed = css_value::maybe_color_from_string(p.first); + auto final_col = col_parsed.value().to_color().value(); + CHECK_MESSAGE(final_col == p.second, p.first.data()); + passed ++; + + if (passed > 20) { + break; + } + } } } }; diff --git a/test/rspamd_cxx_unit.cxx b/test/rspamd_cxx_unit.cxx index eb456d2fd..fedf453c3 100644 --- a/test/rspamd_cxx_unit.cxx +++ b/test/rspamd_cxx_unit.cxx @@ -15,12 +15,27 @@ */ #include "config.h" +#include "rspamd.h" +#include <memory> #define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL #include "doctest/doctest.h" int main(int argc, char** argv) { + std::unique_ptr<struct rspamd_main> rspamd_main{new struct rspamd_main}; + struct rspamd_config *cfg; + + rspamd_main->server_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL, 0); + cfg = rspamd_config_new (RSPAMD_CONFIG_INIT_DEFAULT); + cfg->libs_ctx = rspamd_init_libs (); + rspamd_main->cfg = cfg; + cfg->cfg_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), NULL, 0); + + rspamd_main->logger = rspamd_log_open_emergency (rspamd_main->server_pool, + RSPAMD_LOG_FLAG_RSPAMADM); + rspamd_log_set_log_level (rspamd_main->logger, G_LOG_LEVEL_MESSAGE); + doctest::Context context(argc, argv); int res = context.run(); |