<s> import glob <EOL> import io <EOL> import os <EOL> from cryptography . hazmat . backends import default_backend <EOL> from cryptography . hazmat . primitives import serialization <EOL> from nose . tools import assert_equal <EOL> from flanker import dkim <EOL> DUMMY_EMAIL = b"""<STR_LIT>""" . strip ( ) <EOL> DUMMY_RSA_KEY = serialization . load_pem_private_key ( b"""<STR_LIT>""" , password = None , backend = default_backend ( ) ) <EOL> def test_simple_domain_key_signature ( ) : <EOL> signer = dkim . DomainKeySigner ( DUMMY_RSA_KEY , "<STR_LIT>" , "<STR_LIT>" ) <EOL> sig = signer . sign ( DUMMY_EMAIL ) <EOL> assert_equal ( <EOL> sig , <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> ) <EOL> def test_simple_dkim_signature ( ) : <EOL> signer = dkim . DKIMSigner ( DUMMY_RSA_KEY , "<STR_LIT>" , "<STR_LIT>" ) <EOL> sig = signer . sign ( DUMMY_EMAIL , current_time = <NUM_LIT> ) <EOL> assert_equal ( <EOL> sig , <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> b"<STR_LIT>" <EOL> ) <EOL> def test_canonicalization ( ) : <EOL> path = os . path . join ( <EOL> os . path . dirname ( __file__ ) , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" <EOL> ) <EOL> for path in glob . glob ( path ) : <EOL> with open ( path ) as f : <EOL> contents = f . read ( ) <EOL> with open ( path . replace ( "<STR_LIT:email>" , "<STR_LIT>" ) ) as f : <EOL> nofws_contents = f . read ( ) <EOL> with open ( path . replace ( "<STR_LIT:email>" , "<STR_LIT>" ) ) as f : <EOL> simple_contents = f . read ( ) <EOL> with open ( path . replace ( "<STR_LIT:email>" , "<STR_LIT>" ) ) as f : <EOL> relaxed_contents = f . read ( ) <EOL> assert_equal ( <EOL> canonicalize_contents ( dkim . NoFWSCanonicalization ( ) , contents ) , <EOL> nofws_contents <EOL> ) <EOL> assert_equal ( <EOL> canonicalize_contents ( dkim . SimpleCanonicalization ( ) , contents ) , <EOL> simple_contents <EOL> ) <EOL> assert_equal ( <EOL> canonicalize_contents ( dkim . RelaxedCanonicalization ( ) , contents ) , <EOL> relaxed_contents <EOL> ) <EOL> def canonicalize_contents ( canonicalization_rule , contents ) : <EOL> headers , body = dkim . _rfc822_parse ( contents ) <EOL> output = io . BytesIO ( ) <EOL> for header , value in headers : <EOL> header , value = canonicalization_rule . canonicalize_header ( <EOL> header , value ) <EOL> output . write ( b"<STR_LIT>" . format ( h = header , v = value ) ) <EOL> body = canonicalization_rule . canonicalize_body ( body ) <EOL> output . write ( b"<STR_LIT:\r\n>" ) <EOL> output . write ( body ) <EOL> return output . getvalue ( ) </s>
