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

Source Code for Module dkim.tests.test_util

  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.util import ( 
 22      DuplicateTag, 
 23      InvalidTagSpec, 
 24      parse_tag_value, 
 25      get_linesep, 
 26      ) 
 27   
 28   
29 -class TestParseTagValue(unittest.TestCase):
30 """Tag=Value parsing tests.""" 31
32 - def test_single(self):
33 self.assertEqual( 34 {b'foo': b'bar'}, 35 parse_tag_value(b'foo=bar'))
36
38 self.assertEqual( 39 {b'foo': b'bar'}, 40 parse_tag_value(b'foo=bar;'))
41
42 - def test_multiple(self):
43 self.assertEqual( 44 {b'foo': b'bar', b'baz': b'foo'}, 45 parse_tag_value(b'foo=bar;baz=foo'))
46
47 - def test_value_with_equals(self):
48 self.assertEqual( 49 {b'foo': b'bar', b'baz': b'foo=bar'}, 50 parse_tag_value(b'foo=bar;baz=foo=bar'))
51
53 self.assertEqual( 54 {b'foo': b'bar', b'baz': b'f oo=bar'}, 55 parse_tag_value(b' foo \t= bar;\tbaz= f oo=bar '))
56
58 self.assertRaises( 59 InvalidTagSpec, parse_tag_value, b'foo=bar;baz')
60
62 self.assertRaises( 63 DuplicateTag, parse_tag_value, b'foo=bar;foo=baz')
64
65 - def test_trailing_whitespace(self):
66 hval = b'''v=1; a=rsa-sha256; d=facebookmail.com; s=s1024-2011-q2; c=relaxed/simple; 67 q=dns/txt; i=@facebookmail.com; t=1308078492; 68 h=From:Subject:Date:To:MIME-Version:Content-Type; 69 bh=+qPyCOiDQkusTPstCoGjimgDgeZbUaJWIr1mdE6RFxk=; 70 b=EUmDmdnAsNtjSEHGHNTa8PXgGaEUtOVezagmninX5Bs/Q26R9r3AMgawyUSKkbHp 71 /bQZU6QPZfdvmLMPdIWCQPo8SP+gsz4dpox2efO61DlvgYaxBRhwFedAW9LjYhQc 72 3KzW0yB9JHwiDCw1EioVkv+OMHhAYzoIypA0bQyi2bc=; 73 ''' 74 sig = parse_tag_value(hval) 75 self.assertEqual(sig[b't'],b'1308078492') 76 self.assertEqual(len(sig),11)
77 78
79 -class TestGetLineSep(unittest.TestCase):
80 """Line seperator probing tests.""" 81
82 - def test_default(self):
83 self.assertEqual( 84 b'\r\n', 85 get_linesep(b'abc'))
86
87 - def test_withcrlf(self):
88 self.assertEqual( 89 b'\r\n', 90 get_linesep(b'abc\r\n'))
91
92 - def test_withlf(self):
93 self.assertEqual( 94 b'\n', 95 get_linesep(b'abc\n'))
96
97 - def test_toosmall(self):
98 self.assertEqual( 99 b'\r\n', 100 get_linesep(b'a'))
101 102
103 -def test_suite():
104 from unittest import TestLoader 105 return TestLoader().loadTestsFromName(__name__)
106