Debian Codegraph @

dkim/tests/test_dkim_rsavariants.py

raw symbols SCIP
1 # This software is provided 'as-is', without any express or implied
2 # warranty. In no event will the author be held liable for any damages
3 # arising from the use of this software.
4 #
5 # Permission is granted to anyone to use this software for any purpose,
6 # including commercial applications, and to alter it and redistribute it
7 # freely, subject to the following restrictions:
8 #
9 # 1. The origin of this software must not be misrepresented; you must not
10 # claim that you wrote the original software. If you use this software
11 # in a product, an acknowledgment in the product documentation would be
12 # appreciated but is not required.
13 # 2. Altered source versions must be plainly marked as such, and must not be
14 # misrepresented as being the original software.
15 # 3. This notice may not be removed or altered from any source distribution.
16 #
17 # Copyright (c) 2011 William Grant <[email protected]>
18
19 import email
20 import os.path
21 import unittest
22 import time
23
24 import dkim
25
26
27 def read_test_data(filename):
28 """Get the content of the given test data file.
29
30 The files live in dkim/tests/data.
31 """
32 path = os.path.join(os.path.dirname(__file__), 'data', filename)
33 with open(path, 'rb') as f:
34 return f.read()
35
36
37 class TestSignAndVerify(unittest.TestCase):
38 """End-to-end signature and verification tests."""
39
40 def setUp(self):
41 self.message = read_test_data("test.message")
42 self.key1024 = read_test_data("1024_testkey.key")
43 self.key2048 = read_test_data("2048_testkey.key")
44 self.key2048PKCS8 = read_test_data("2048_testkey_PKCS8.key")
45
46 def dnsfunc(self, domain, timeout=5):
47 _dns_responses = {
48 'test._domainkey.example.com.': read_test_data("1024_testkey_wo_markers.pub.txt"),
49 'test2._domainkey.example.com.': read_test_data("1024_testkey_wo_markers.pub.rsa.txt"),
50 'test3._domainkey.example.com.': read_test_data("2048_testkey_wo_markers.pub.txt"),
51 'test4._domainkey.example.com.': read_test_data("2048_testkey_wo_markers.pub.rsa.txt"),
52 'test5._domainkey.example.com.': read_test_data("2048_testkey_PKCS8.key.pub.txt")
53 }
54 try:
55 domain = domain.decode('ascii')
56 except UnicodeDecodeError:
57 return None
58 self.assertTrue(domain in _dns_responses,domain)
59 return _dns_responses[domain]
60
61 def test_verifies_SubjectPublicKeyInfo1024(self):
62 # A message verifies after being signed.
63 for header_algo in (b"simple", b"relaxed"):
64 for body_algo in (b"simple", b"relaxed"):
65 sig = dkim.sign(
66 self.message, b"test", b"example.com", self.key1024,
67 canonicalize=(header_algo, body_algo))
68 res = dkim.verify(sig + self.message, dnsfunc=self.dnsfunc)
69 self.assertTrue(res)
70
71 def test_verifies_RSAPublicKey1024(self):
72 # A message verifies after being signed.
73 for header_algo in (b"simple", b"relaxed"):
74 for body_algo in (b"simple", b"relaxed"):
75 sig = dkim.sign(
76 self.message, b"test2", b"example.com", self.key1024,
77 canonicalize=(header_algo, body_algo))
78 res = dkim.verify(sig + self.message, dnsfunc=self.dnsfunc)
79 self.assertTrue(res)
80
81
82 def test_verifiesSubjectPublicKeyInfo2048(self):
83 # A message verifies after being signed.
84 for header_algo in (b"simple", b"relaxed"):
85 for body_algo in (b"simple", b"relaxed"):
86 sig = dkim.sign(
87 self.message, b"test3", b"example.com", self.key2048,
88 canonicalize=(header_algo, body_algo))
89 res = dkim.verify(sig + self.message, dnsfunc=self.dnsfunc)
90 self.assertTrue(res)
91
92
93 def test_verifiesRSAPublicKey2048(self):
94 # A message verifies after being signed.
95 for header_algo in (b"simple", b"relaxed"):
96 for body_algo in (b"simple", b"relaxed"):
97 sig = dkim.sign(
98 self.message, b"test4", b"example.com", self.key2048,
99 canonicalize=(header_algo, body_algo))
100 res = dkim.verify(sig + self.message, dnsfunc=self.dnsfunc)
101 self.assertTrue(res)
102
103
104 def test_verifies_RSAPublicKey2048PKCS8(self):
105 #A message verifies after being signed (with PKCS8 private key)
106 for header_algo in (b"simple", b"relaxed"):
107 for body_algo in (b"simple", b"relaxed"):
108 sig = dkim.sign(
109 self.message, b"test5", b"example.com", self.key2048PKCS8,
110 canonicalize=(header_algo, body_algo))
111 res = dkim.verify(sig + self.message, dnsfunc=self.dnsfunc)
112 self.assertTrue(res)
113
114 def test_suite():
115 from unittest import TestLoader
116 return TestLoader().loadTestsFromName(__name__)