commit 67857e6: [Minor] Css: Add a simple testing logic for css parsing

Vsevolod Stakhov vsevolod at highsecure.ru
Fri Jan 29 14:42:07 UTC 2021


Author: Vsevolod Stakhov
Date: 2021-01-29 11:38:56 +0000
URL: https://github.com/rspamd/rspamd/commit/67857e64c268fe2dc04e425c74e37c4859edfd58

[Minor] Css: Add a simple testing logic for css parsing

---
 src/libserver/css/css.cxx | 19 ++++++++++++++-----
 test/lua/unit/css.lua     | 19 +++++++++++++++++++
 2 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/src/libserver/css/css.cxx b/src/libserver/css/css.cxx
index bd148cecd..6633b68fd 100644
--- a/src/libserver/css/css.cxx
+++ b/src/libserver/css/css.cxx
@@ -17,14 +17,23 @@
 #include "css.h"
 #include "css.hxx"
 #include "css_style.hxx"
+#include "css_parser.hxx"
 
 rspamd_css
-rspamd_css_parse_style (const guchar *begin, gsize len, GError **err)
+rspamd_css_parse_style (rspamd_mempool_t *pool, const guchar *begin, gsize len,
+						GError **err)
 {
-	rspamd::css::css_style_sheet *style = nullptr;
-
-
-	return reinterpret_cast<rspamd_css>(style);
+	auto parse_res = rspamd::css::parse_css(pool, {(const char* )begin, len});
+
+	if (parse_res.has_value()) {
+		return reinterpret_cast<rspamd_css>(parse_res.value().release());
+	}
+	else {
+		g_set_error(err, g_quark_from_static_string("css"),
+				static_cast<int>(parse_res.error().type),
+				"parse error");
+		return nullptr;
+	}
 }
 
 namespace rspamd::css {
diff --git a/test/lua/unit/css.lua b/test/lua/unit/css.lua
index 8d4d4469b..ad7d2762c 100644
--- a/test/lua/unit/css.lua
+++ b/test/lua/unit/css.lua
@@ -7,11 +7,15 @@ const char *rspamd_css_unescape (void *pool,
             const char *begin,
             size_t len,
             size_t *olen);
+void* rspamd_css_parse_style (void *pool,
+            const char *begin,
+            size_t len, void *err);
 ]]
 
   local cases = {
     {'#\\31 a2b3c {', '#1a2b3c {'}
   }
+
   for _,t in ipairs(cases) do
     test("Unescape " .. t[1], function()
       local olen = ffi.new('size_t[1]')
@@ -20,4 +24,19 @@ const char *rspamd_css_unescape (void *pool,
       assert_equal(escaped, t[2], escaped .. " not equal " .. t[2])
     end)
   end
+
+  local cases = {[[
+p {
+  color: red;
+  text-align: center;
+}
+]]
+  }
+  local NULL = ffi.new 'void*'
+  for i,t in ipairs(cases) do
+    test("Parse css sample " .. i, function()
+      local escaped = ffi.C.rspamd_css_parse_style(pool:topointer(), t, #t, NULL)
+      assert_not_null(escaped)
+    end)
+  end
 end)
\ No newline at end of file


More information about the Commits mailing list