]> source.dussan.org Git - rspamd.git/commitdiff
[Test] Add tests for urls extraction
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 13 Jul 2021 12:57:13 +0000 (13:57 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 13 Jul 2021 13:13:24 +0000 (14:13 +0100)
src/libserver/html/html.cxx
src/libserver/html/html_tests.cxx

index d061f77260d7d42eee7df6892caef1f6c4c1f3f0..bd323b43f054bd6627de084bd1a07cdeaa392d26 100644 (file)
@@ -1331,6 +1331,9 @@ html_process_input(rspamd_mempool_t *pool,
                                                url->count++;
                                        }
                                }
+                               if (part_urls) {
+                                       g_ptr_array_add(part_urls, url);
+                               }
 
                                href_offset = hc->parsed.size();
                        }
index 323858d71c30a2cf4454779d59e7812b9b5c96cc..07618b2737dafefecccfe86e6583d336021f997e 100644 (file)
@@ -217,6 +217,38 @@ TEST_CASE("html text extraction")
        rspamd_mempool_delete(pool);
 }
 
+TEST_CASE("html urls extraction")
+{
+       using namespace std::string_literals;
+       const std::vector<std::pair<std::string, std::vector<std::string>>> cases{
+                       {"<a href=\"https://example.com\">test</a>", {"https://example.com"}}
+       };
+
+       rspamd_url_init(NULL);
+       auto *pool = rspamd_mempool_new(rspamd_mempool_suggest_size(),
+                       "html", 0);
+       auto i = 1;
+       for (const auto &c : cases) {
+               SUBCASE((fmt::format("html url extraction case {}", i)).c_str()) {
+                       GPtrArray *purls = g_ptr_array_new();
+                       GByteArray *tmp = g_byte_array_sized_new(c.first.size());
+                       g_byte_array_append(tmp, (const guint8 *) c.first.data(), c.first.size());
+                       auto *hc = html_process_input(pool, tmp, nullptr, nullptr, purls, true);
+                       CHECK(hc != nullptr);
+                       auto expected = c.second;
+                       CHECK(expected.size() == purls->len);
+                       for (auto j = 0; j < expected.size(); ++j) {
+                               auto *url = (rspamd_url *)g_ptr_array_index(purls, j);
+                               CHECK(expected[j] == std::string{url->string, url->urllen});
+                       }
+                       g_byte_array_free(tmp, TRUE);
+                       g_ptr_array_free(purls, TRUE);
+               }
+       }
+
+       rspamd_mempool_delete(pool);
+}
+
 }
 
 } /* namespace rspamd::html */