]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Css: Add a simple testing logic for css parsing
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 29 Jan 2021 11:38:56 +0000 (11:38 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 29 Jan 2021 11:38:56 +0000 (11:38 +0000)
src/libserver/css/css.cxx
test/lua/unit/css.lua

index bd148cecda6b083410199202af34fb0048605b63..6633b68fd8714058c373bb9560b3c30026fd7c4d 100644 (file)
 #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 {
index 8d4d4469bfed276f5d8f897095f3119cc6179649..ad7d2762c6c792db61c12c0af1bb655cac3b19a1 100644 (file)
@@ -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