Package dkim :: Package tests :: Module test_dkim_rsavariants
[hide private]
[frames] | no frames]

Source Code for Module dkim.tests.test_dkim_rsavariants

  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 <me@williamgrant.id.au> 
 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
45 - def dnsfunc(self, domain, timeout=5):
46 _dns_responses = { 47 'test._domainkey.example.com.': read_test_data("1024_testkey_wo_markers.pub.txt"), 48 'test2._domainkey.example.com.': read_test_data("1024_testkey_wo_markers.pub.rsa.txt"), 49 'test3._domainkey.example.com.': read_test_data("2048_testkey_wo_markers.pub.txt"), 50 'test4._domainkey.example.com.': read_test_data("2048_testkey_wo_markers.pub.rsa.txt"), 51 } 52 try: 53 domain = domain.decode('ascii') 54 except UnicodeDecodeError: 55 return None 56 self.assertTrue(domain in _dns_responses,domain) 57 return _dns_responses[domain]
58 59
61 # A message verifies after being signed. 62 for header_algo in (b"simple", b"relaxed"): 63 for body_algo in (b"simple", b"relaxed"): 64 sig = dkim.sign( 65 self.message, b"test", b"example.com", self.key1024, 66 canonicalize=(header_algo, body_algo)) 67 res = dkim.verify(sig + self.message, dnsfunc=self.dnsfunc) 68 self.assertTrue(res)
69 70 71
73 # A message verifies after being signed. 74 for header_algo in (b"simple", b"relaxed"): 75 for body_algo in (b"simple", b"relaxed"): 76 sig = dkim.sign( 77 self.message, b"test2", b"example.com", self.key1024, 78 canonicalize=(header_algo, body_algo)) 79 res = dkim.verify(sig + self.message, dnsfunc=self.dnsfunc) 80 self.assertTrue(res)
81 82
84 # A message verifies after being signed. 85 for header_algo in (b"simple", b"relaxed"): 86 for body_algo in (b"simple", b"relaxed"): 87 sig = dkim.sign( 88 self.message, b"test3", b"example.com", self.key2048, 89 canonicalize=(header_algo, body_algo)) 90 res = dkim.verify(sig + self.message, dnsfunc=self.dnsfunc) 91 self.assertTrue(res)
92 93
95 # A message verifies after being signed. 96 for header_algo in (b"simple", b"relaxed"): 97 for body_algo in (b"simple", b"relaxed"): 98 sig = dkim.sign( 99 self.message, b"test4", b"example.com", self.key2048, 100 canonicalize=(header_algo, body_algo)) 101 res = dkim.verify(sig + self.message, dnsfunc=self.dnsfunc) 102 self.assertTrue(res)
103 104
105 -def test_suite():
106 from unittest import TestLoader 107 return TestLoader().loadTestsFromName(__name__)
108