aboutsummaryrefslogtreecommitdiffstats
path: root/test/rspamd_cxx_unit_dkim.hxx
blob: 3f6b71a39ad42f0e88f9454cc36e616e33f899cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*-
 * 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 "fmt/core.h"
#include "libserver/dkim.h"

#include <vector>
#include <utility>
#include <string>
#include <tuple>

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<test_case> 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"},
			// Spaces within base64
			{"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, ""},
		};

		auto cur_test_idx = 0;
		for (auto &&c: cases) {
			SUBCASE(fmt::format("process DKIM record {}: {}", cur_test_idx++, c.input).c_str())
			{
				GError *err = nullptr;
				gsize klen = c.input.size();
				auto *key = rspamd_dkim_parse_key(c.input.c_str(), &klen, &err);
				if (c.is_valid) {
					REQUIRE_MESSAGE(key != nullptr, (err ? err->message : "unknown error"));
					char hexbuf[RSPAMD_DKIM_KEY_ID_LEN * 2 + 1];
					auto *id = rspamd_dkim_key_id(key);
					REQUIRE(id != nullptr);

					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