aboutsummaryrefslogtreecommitdiffstats
path: root/src/libserver/css
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2021-03-15 21:04:36 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2021-03-15 21:04:36 +0000
commit10fb4d1a18a0a0573ad84a5ec4e5dd2ba55fdaa3 (patch)
tree6183213b35be006d9cc12adac81c0b9e01d4d3a3 /src/libserver/css
parent92f20993f94c91ad03213c5fcc2984ee3b487a67 (diff)
downloadrspamd-10fb4d1a18a0a0573ad84a5ec4e5dd2ba55fdaa3.tar.gz
rspamd-10fb4d1a18a0a0573ad84a5ec4e5dd2ba55fdaa3.zip
[Project] Css: Move some of the tests to the doctest
Diffstat (limited to 'src/libserver/css')
-rw-r--r--src/libserver/css/css.cxx5
-rw-r--r--src/libserver/css/css_parser.cxx61
-rw-r--r--src/libserver/css/css_parser.hxx2
-rw-r--r--src/libserver/css/css_value.cxx15
4 files changed, 73 insertions, 10 deletions
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;
+ }
+ }
}
}
};