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

Source Code for Module dkim.tests.test_canonicalization

  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 unittest 
 20   
 21  from dkim.canonicalization import ( 
 22      CanonicalizationPolicy, 
 23      InvalidCanonicalizationPolicyError, 
 24      Simple, 
 25      Relaxed, 
 26      ) 
 27   
 28   
29 -class BaseCanonicalizationTest(unittest.TestCase):
30
31 - def assertCanonicalForm(self, expected, input):
32 self.assertEqual(expected, self.func(expected)) 33 self.assertEqual(expected, self.func(input))
34 35
36 -class TestSimpleAlgorithmHeaders(BaseCanonicalizationTest):
37 38 func = staticmethod(Simple.canonicalize_headers) 39
40 - def test_untouched(self):
41 test_headers = [(b'Foo ', b'bar\r\n'), (b'Foo', b'baz\r\n')] 42 self.assertCanonicalForm( 43 test_headers, 44 test_headers)
45 46
47 -class TestSimpleAlgorithmBody(BaseCanonicalizationTest):
48 49 func = staticmethod(Simple.canonicalize_body) 50
52 self.assertCanonicalForm( 53 b'Foo \tbar \r\n', 54 b'Foo \tbar \r\n\r\n')
55
56 - def test_adds_crlf(self):
57 self.assertCanonicalForm( 58 b'Foo bar\r\n', 59 b'Foo bar')
60
61 - def test_empty_body(self):
62 self.assertCanonicalForm( 63 b'\r\n', 64 b'')
65
66 - def test_single_crlf_body(self):
67 self.assertCanonicalForm( 68 b'\r\n', 69 b'\r\n')
70
71 - def test_multiple_crlf_body(self):
72 self.assertCanonicalForm( 73 b'\r\n', 74 b'\r\n\r\n')
75 76
77 -class TestRelaxedAlgorithmHeaders(BaseCanonicalizationTest):
78 79 func = staticmethod(Relaxed.canonicalize_headers) 80
81 - def test_lowercases_names(self):
82 self.assertCanonicalForm( 83 [(b'foo', b'Bar\r\n'), (b'baz', b'Foo\r\n')], 84 [(b'Foo', b'Bar\r\n'), (b'BaZ', b'Foo\r\n')])
85
86 - def test_unfolds_values(self):
87 self.assertCanonicalForm( 88 [(b'foo', b'Bar baz\r\n')], 89 [(b'Foo', b'Bar\r\n baz\r\n')])
90
92 self.assertCanonicalForm( 93 [(b'foo', b'Bar baz\r\n')], 94 [(b'Foo', b'Bar \t baz\r\n')])
95
96 - def test_wsp_strips(self):
97 self.assertCanonicalForm( 98 [(b'foo', b'Bar baz\r\n')], 99 [(b'Foo ', b' Bar \t baz \r\n')])
100 101
102 -class TestRelaxedAlgorithmBody(BaseCanonicalizationTest):
103 104 func = staticmethod(Relaxed.canonicalize_body) 105
106 - def test_strips_trailing_wsp(self):
107 self.assertCanonicalForm( 108 b'Foo\r\nbar\r\n', 109 b'Foo \t\r\nbar\r\n')
110
111 - def test_wsp_compresses(self):
112 self.assertCanonicalForm( 113 b'Foo bar\r\n', 114 b'Foo \t bar\r\n')
115
117 self.assertCanonicalForm( 118 b'Foo\r\nbar\r\n', 119 b'Foo\r\nbar\r\n\r\n\r\n')
120
121 - def test_adds_crlf(self):
122 self.assertCanonicalForm( 123 b'Foo bar\r\n', 124 b'Foo bar')
125
126 - def test_empty_body(self):
127 self.assertCanonicalForm( 128 b'', 129 b'')
130
131 - def test_single_crlf_body(self):
132 self.assertCanonicalForm( 133 b'', 134 b'\r\n')
135
136 - def test_multiple_crlf_body(self):
137 self.assertCanonicalForm( 138 b'', 139 b'\r\n\r\n')
140 141
142 -class TestCanonicalizationPolicyFromCValue(unittest.TestCase):
143
144 - def assertAlgorithms(self, header_algo, body_algo, c_value):
145 p = CanonicalizationPolicy.from_c_value(c_value) 146 self.assertEqual( 147 (header_algo, body_algo), 148 (p.header_algorithm, p.body_algorithm))
149
150 - def assertValueDoesNotParse(self, c_value):
154
156 self.assertAlgorithms(Simple, Simple, None)
157
158 - def test_relaxed_headers(self):
159 self.assertAlgorithms(Relaxed, Simple, b'relaxed')
160
161 - def test_relaxed_body(self):
162 self.assertAlgorithms(Simple, Relaxed, b'simple/relaxed')
163
164 - def test_relaxed_both(self):
165 self.assertAlgorithms(Relaxed, Relaxed, b'relaxed/relaxed')
166
167 - def test_explict_simple_both(self):
168 self.assertAlgorithms(Simple, Simple, b'simple/simple')
169
171 self.assertValueDoesNotParse(b'') 172 self.assertValueDoesNotParse(b'simple/simple/simple') 173 self.assertValueDoesNotParse(b'relaxed/stressed') 174 self.assertValueDoesNotParse(b'worried')
175 176
177 -class TestCanonicalizationPolicyToCValue(unittest.TestCase):
178
179 - def assertCValue(self, c_value, header_algo, body_algo):
180 self.assertEqual( 181 c_value, 182 CanonicalizationPolicy(header_algo, body_algo).to_c_value())
183
184 - def test_both_simple(self):
185 self.assertCValue(b'simple/simple', Simple, Simple)
186
187 - def test_relaxed_body(self):
188 self.assertCValue(b'simple/relaxed', Simple, Relaxed)
189
190 - def test_both_relaxed(self):
191 self.assertCValue(b'relaxed/relaxed', Relaxed, Relaxed)
192
193 - def test_relaxed_headers(self):
194 self.assertCValue(b'relaxed/simple', Relaxed, Simple)
195 196
197 -def test_suite():
198 from unittest import TestLoader 199 return TestLoader().loadTestsFromName(__name__)
200