From 28b6b571cf68b4fa6c40ae5ef44fcae0637b29a7 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 4 Nov 2021 20:14:30 +0000 Subject: [PATCH] [Test] Add tests for DKIM key record parsing Issue: #3955 --- test/rspamd_cxx_unit.cxx | 1 + test/rspamd_cxx_unit_dkim.hxx | 159 ++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 test/rspamd_cxx_unit_dkim.hxx diff --git a/test/rspamd_cxx_unit.cxx b/test/rspamd_cxx_unit.cxx index 37aa45b6f..acbbbabce 100644 --- a/test/rspamd_cxx_unit.cxx +++ b/test/rspamd_cxx_unit.cxx @@ -23,6 +23,7 @@ #include "rspamd_cxx_unit_utils.hxx" #include "rspamd_cxx_local_ptr.hxx" +#include "rspamd_cxx_unit_dkim.hxx" static gboolean verbose = false; static const GOptionEntry entries[] = diff --git a/test/rspamd_cxx_unit_dkim.hxx b/test/rspamd_cxx_unit_dkim.hxx new file mode 100644 index 000000000..87e244b3d --- /dev/null +++ b/test/rspamd_cxx_unit_dkim.hxx @@ -0,0 +1,159 @@ +/*- + * Copyright 2021 Vsevolod Stakhov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* Detached unit tests for the dkim utils */ + +#ifndef RSPAMD_RSPAMD_CXX_UNIT_DKIM_HXX +#define RSPAMD_RSPAMD_CXX_UNIT_DKIM_HXX + +#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL +#include "doctest/doctest.h" + +#include "libserver/dkim.h" + +#include +#include +#include +#include + +TEST_SUITE("rspamd_dkim") { + +TEST_CASE("rspamd_dkim_parse_key") +{ + struct test_case { + std::string input; + bool is_valid; + std::string expected_id; + }; + std::vector cases{ + {"p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" + "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" + "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" + "TG02HwWNy/kf1h+OlAGQKJLgakivZ3nMMnUIPHkUjwvhbkaMXCI046XoqsEQ7KW" + "VKRoF3cK1cFXLo+bgO3sEJgGtvwzPodG0CqVu+gjehrjwdLnPhqyEspfI1IbL" + "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" + "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB", + true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, + // Spaces before p + {" p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" + "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" + "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" + "TG02HwWNy/kf1h+OlAGQKJLgakivZ3nMMnUIPHkUjwvhbkaMXCI046XoqsEQ7KW" + "VKRoF3cK1cFXLo+bgO3sEJgGtvwzPodG0CqVu+gjehrjwdLnPhqyEspfI1IbL" + "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" + "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB", + true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, + // Spaces and bogus semicolon before p + {"; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" + "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" + "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" + "TG02HwWNy/kf1h+OlAGQKJLgakivZ3nMMnUIPHkUjwvhbkaMXCI046XoqsEQ7KW" + "VKRoF3cK1cFXLo+bgO3sEJgGtvwzPodG0CqVu+gjehrjwdLnPhqyEspfI1IbL" + "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" + "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB", + true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, + // Spaces after p + {"k=rsa; t=s; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" + "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" + "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" + "TG02HwWNy/kf1h+OlAGQKJLgakivZ3nMMnUIPHkUjwvhbkaMXCI046XoqsEQ7KW" + "VKRoF3cK1cFXLo+bgO3sEJgGtvwzPodG0CqVu+gjehrjwdLnPhqyEspfI1IbL" + "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" + "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB ", + true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, + // ; and spaces + {"k=rsa; t=s; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" + "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" + "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" + "TG02HwWNy/kf1h+OlAGQKJLgakivZ3nMMnUIPHkUjwvhbkaMXCI046XoqsEQ7KW" + "VKRoF3cK1cFXLo+bgO3sEJgGtvwzPodG0CqVu+gjehrjwdLnPhqyEspfI1IbL" + "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" + "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB ; ", + true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, + // ; and spaces around '=' sign + {"k=rsa; t=s; p = MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" + "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" + "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" + "TG02HwWNy/kf1h+OlAGQKJLgakivZ3nMMnUIPHkUjwvhbkaMXCI046XoqsEQ7KW" + "VKRoF3cK1cFXLo+bgO3sEJgGtvwzPodG0CqVu+gjehrjwdLnPhqyEspfI1IbL" + "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" + "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB;", + true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, + // ; and spaces around '=' sign + bad stuff + {"ololo k=rsa; t=s; p = MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" + "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" + "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" + "TG02HwWNy/kf1h+OlAGQKJLgakivZ3nMMnUIPHkUjwvhbkaMXCI046XoqsEQ7KW" + "VKRoF3cK1cFXLo+bgO3sEJgGtvwzPodG0CqVu+gjehrjwdLnPhqyEspfI1IbL" + "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" + "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB;", + true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, + // ; and spaces around '=' sign + bad stuff + {"ololo=trololo; k=rsa; t=s; " + "p = MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" + "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" + "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" + "TG02HwWNy/kf1h+OlAGQKJLgakivZ3nMMnUIPHkUjwvhbkaMXCI046XoqsEQ7KW" + "VKRoF3cK1cFXLo+bgO3sEJgGtvwzPodG0CqVu+gjehrjwdLnPhqyEspfI1IbL" + "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" + "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB ", + true, "e40cc5c40ee29cb4f21d95c7f0dc9989"}, + // Invalid RSA + {"ololo=trololo; k=rsa; t=s; " + "p = BADMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" + "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" + "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" + "TG02HwWNy/kf1h+OlAGQKJLgakivZ3nMMnUIPHkUjwvhbkaMXCI046XoqsEQ7KW" + "VKRoF3cK1cFXLo+bgO3sEJgGtvwzPodG0CqVu+gjehrjwdLnPhqyEspfI1IbL" + "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" + "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB ", + false, ""}, + // Invalid RSA for eddsa + {"ololo=trololo; k=ed25519; t=s; " + "p = MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCA" + "QEA5CeQZpoPbsS8lG41UI1rxTtOSqPrfgZzhrZsk0t9dIbFTvaoql/FLuYcbdUARc" + "5zuyXsDj1eSprOgcPT9PY9RoSUsY8i/jnD49DHXtMfXoBk0J6epNzbZqqWU+" + "TG02HwWNy/kf1h+OlAGQKJLgakivZ3nMMnUIPHkUjwvhbkaMXCI046XoqsEQ7KW" + "VKRoF3cK1cFXLo+bgO3sEJgGtvwzPodG0CqVu+gjehrjwdLnPhqyEspfI1IbL" + "lnNaq5pWei/B8pG6teV+y3t4yay5ZGktALJjlylKHVo2USkVYQTFQ9Ji25m2jupdCd" + "kn1FTuYNqh0Nzg3KPQHNVp7mlE7lfwIDAQAB ", + false, ""}, + }; + + for (auto &&c : cases) { + SUBCASE (("process DKIM record " + c.input).c_str()) { + gsize klen = c.input.size(); + auto *key = rspamd_dkim_parse_key(c.input.c_str(), &klen, nullptr); + if (c.is_valid) { + CHECK(key != nullptr); + char hexbuf[RSPAMD_DKIM_KEY_ID_LEN * 2 + 1]; + auto *id = rspamd_dkim_key_id(key); + auto hexlen = rspamd_encode_hex_buf(id, RSPAMD_DKIM_KEY_ID_LEN, hexbuf, + sizeof(hexbuf)); + CHECK(hexlen > 0); + CHECK(std::string{hexbuf, (std::size_t) hexlen} == c.expected_id); + rspamd_dkim_key_free(key); + } + else { + CHECK(key == nullptr); + } + } + } +} + +} + +#endif -- 2.39.5