Add basic end-to-end signing and verification tests.
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
Received: from localhost
|
||||
Message-ID: <example@example.com>
|
||||
Date: Mon, 01 Jan 2011 01:02:03 +0400
|
||||
From: Test User <test@example.com>
|
||||
To: somebody@example.com
|
||||
Subject: Testing
|
||||
|
||||
This is a test message.
|
||||
@@ -0,0 +1,15 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIICXQIBAAKBgQDkHlOQoBTzWRiGs5V6NpP3idY6Wk08a5qhdR6wy5bdOKb2jLQi
|
||||
Y/J16JYi0Qvx/byYzCNb3W91y3FutACDfzwQ/BC/e/8uBsCR+yz1Lxj+PL6lHvqM
|
||||
KrM3rG4hstT5QjvHO9PzoxZyVYLzBfO2EeC3Ip3G+2kryOTIKT+l/K4w3QIDAQAB
|
||||
AoGAH0cxOhFZDgzXWhDhnAJDw5s4roOXN4OhjiXa8W7Y3rhX3FJqmJSPuC8N9vQm
|
||||
6SVbaLAE4SG5mLMueHlh4KXffEpuLEiNp9Ss3O4YfLiQpbRqE7Tm5SxKjvvQoZZe
|
||||
zHorimOaChRL2it47iuWxzxSiRMv4c+j70GiWdxXnxe4UoECQQDzJB/0U58W7RZy
|
||||
6enGVj2kWF732CoWFZWzi1FicudrBFoy63QwcowpoCazKtvZGMNlPWnC7x/6o8Gc
|
||||
uSe0ga2xAkEA8C7PipPm1/1fTRQvj1o/dDmZp243044ZNyxjg+/OPN0oWCbXIGxy
|
||||
WvmZbXriOWoSALJTjExEgraHEgnXssuk7QJBALl5ICsYMu6hMxO73gnfNayNgPxd
|
||||
WFV6Z7ULnKyV7HSVYF0hgYOHjeYe9gaMtiJYoo0zGN+L3AAtNP9huqkWlzECQE1a
|
||||
licIeVlo1e+qJ6Mgqr0Q7Aa7falZ448ccbSFYEPD6oFxiOl9Y9se9iYHZKKfIcst
|
||||
o7DUw1/hz2Ck4N5JrgUCQQCyKveNvjzkkd8HjYs0SwM0fPjK16//5qDZ2UiDGnOe
|
||||
uEzxBDAr518Z8VFbR41in3W4Y3yCDgQlLlcETrS+zYcL
|
||||
-----END RSA PRIVATE KEY-----
|
||||
@@ -0,0 +1 @@
|
||||
v=DKIM1; g=*; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDkHlOQoBTzWRiGs5V6NpP3idY6Wk08a5qhdR6wy5bdOKb2jLQiY/J16JYi0Qvx/byYzCNb3W91y3FutACDfzwQ/BC/e/8uBsCR+yz1Lxj+PL6lHvqMKrM3rG4hstT5QjvHO9PzoxZyVYLzBfO2EeC3Ip3G+2kryOTIKT+l/K4w3QIDAQAB
|
||||
@@ -1,8 +1,18 @@
|
||||
import os.path
|
||||
import unittest
|
||||
|
||||
import dkim
|
||||
|
||||
|
||||
def read_test_data(filename):
|
||||
"""Get the content of the given test data file.
|
||||
|
||||
The files live in dkim/tests/data.
|
||||
"""
|
||||
path = os.path.join(os.path.dirname(__file__), 'data', filename)
|
||||
return open(path).read()
|
||||
|
||||
|
||||
class TestFold(unittest.TestCase):
|
||||
|
||||
def test_short_line(self):
|
||||
@@ -16,5 +26,29 @@ class TestFold(unittest.TestCase):
|
||||
"foo"*24 + "\r\n foo", dkim.fold("foo" * 25))
|
||||
|
||||
|
||||
class TestSignAndVerify(unittest.TestCase):
|
||||
"""End-to-end signature and verification tests."""
|
||||
|
||||
def setUp(self):
|
||||
self.message = read_test_data("test.message")
|
||||
self.key = read_test_data("test.private")
|
||||
|
||||
def dnsfunc(self, domain):
|
||||
self.assertEqual('test._domainkey.example.com.', domain)
|
||||
return read_test_data("test.txt")
|
||||
|
||||
def test_verifies(self):
|
||||
# A message verifies after being signed.
|
||||
sig = dkim.sign(self.message, "test", "example.com", self.key)
|
||||
res = dkim.verify(sig + self.message, dnsfunc=self.dnsfunc)
|
||||
self.assertTrue(res)
|
||||
|
||||
def test_altered_body_fails(self):
|
||||
# An altered body fails verification.
|
||||
sig = dkim.sign(self.message, "test", "example.com", self.key)
|
||||
res = dkim.verify(sig + self.message + "foo", dnsfunc=self.dnsfunc)
|
||||
self.assertFalse(res)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user