Esempio n. 1
0
def test_payload():
    body = 'body\nmessage'
    ubody = u'bödy\nmessäge'
    enc8 = ('base64','quoted-printable','8bit')
    for uni in range(2):
        encodings = enc8 if uni else ['7bit']
        for enc in encodings:
            msg = create_mail(sender,receiver,'subject',
                ubody if uni else body,charset='UTF-8' if uni else 'us-ascii',
                encoding=enc)
            txt = mail_payload(msg)
            if uni: assert txt == ubody
            else: assert txt == body
            txt = mail_payload(msg.as_string())
            if uni: assert txt == ubody
            else: assert txt == body
            txt = mail_payload(mail_binary(msg))
            if uni: assert txt == ubody
            else: assert txt == body
    binary = b'\xc3\n\xf1'
    for encoding in enc8:
        att = create_mime(binary,'application','octet-stream',encoding=encoding)
        msg = create_mail(sender,receiver,'subject',body,attach=[att])
        cont = mail_payload(msg)
        assert len(cont)==2 and cont[0]==body and cont[1]==binary
        msg = mail_binary(msg)
        cont = mail_payload(msg)
        assert len(cont)==2 and cont[0]==body and cont[1]==binary
    with raises(UnicodeError):
        create_mime(binary,'application','octet-stream',encoding='7bit')
    body = b'body\nmessage'
    att = create_mime(body,'application','octet-stream',encoding='7bit')
    assert mail_payload(att)==body
Esempio n. 2
0
def test_unicode():
    from email.header import decode_header
    def get_header(msg,key):
        v = msg[key]
        v = decode_header(v)[0]
        v = v[0].decode(v[1])
        return v
    usender = sender.replace('Foo',u'Föo')
    ureceiver = receiver.replace('Bar',u'Bär')
    usubject = u'sübject'
    body = 'body\nmessage'
    ubody = u'bödy\nmessage'
    msg = create_mail(usender,ureceiver,usubject,body,headers={'X-Spam':'No'})
    assert get_header(msg,'From') == usender
    assert get_header(msg,'To') == ureceiver
    assert get_header(msg,'Subject') == usubject
    assert msg['X-Spam'] == 'No'
    assert msg.get_charset() == 'us-ascii'
    assert msg.get_payload(decode=False) == body

    msg = create_mail(sender,receiver,'subject',ubody)
    assert not msg['X-Spam']
    _mail_addreplace_header(msg,'X-Spam','No')
    assert msg['X-Spam'] == 'No'
    _mail_addreplace_header(msg,'X-Spam','Yes')
    assert msg['X-Spam'] == 'Yes'
    assert msg.get_charset() == 'UTF-8'
    assert mail_payload(msg) == ubody

    attachment = 'some\nattachment'
    uattachment = u'söme\nattachment'
    attach=create_mime(attachment)
    uattach=create_mime(uattachment,charset='utf-8')
    for variant in range(1,4):
        unibody,uniatt  = variant&1,variant&2
        msg = create_mail(sender,receiver,'subject',ubody if unibody else body,
            attach=[uattach if uniatt else attach])
        assert not msg.get_charset()
        submsg = msg.get_payload(0)
        if unibody:
            assert submsg.get_charset() == 'UTF-8'
            assert submsg.get_payload(decode=True).decode('UTF-8') == ubody
        else:
            assert submsg.get_charset() == 'us-ascii'
            assert submsg.get_payload(decode=False) == body
        submsg = msg.get_payload(1)
        if uniatt:
            assert submsg.get_charset() == 'UTF-8'
            assert submsg.get_payload(decode=True).decode('UTF-8') == uattachment
        else:
            assert submsg.get_charset() == 'us-ascii'
            assert submsg.get_payload(decode=False) == attachment
Esempio n. 3
0
def test_protect():
    #> From foo
    msg='''Content-Type: multipart/mixed;
 boundary="------------030608090900090202040409"

This is a multi-part message in MIME format.
--------------030608090900090202040409
Content-Type: text/plain; charset=ISO-8859-15
Content-Transfer-Encoding: quoted-printable

body
line2

--------------030608090900090202040409
Content-Type: text/plain; charset=UTF-8;
 name="attach.txt"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
 filename="attach.txt"

attachment
line2
--------------030608090900090202040409--
'''
    prot = protect_mail(msg,linesep='\n',sevenbit=True)
    assert prot.as_string() == msg
    prot = protect_mail(msg,linesep='\r\n',sevenbit=True)
    assert prot.as_string() == fix_lines(msg,'\r\n')
    msg = create_mail(sender,receiver,'subject','body\nmessage')
    prot = protect_mail(msg,linesep='\n')
    assert msg.get_payload() == prot.get_payload()
    prot = protect_mail(msg,linesep='\r\n')
    assert fix_lines(msg.get_payload(),'\r\n') == prot.get_payload()
Esempio n. 4
0
 def test_uni_encrypt_attach(self,bilateral):
     usender = sender.replace('Foo','Föo')
     ureceiver = receiver.replace('Bar','Bär')
     attachment = email.mime.text.MIMEText(u'söme\nattachment',_charset='utf-8')
     msgatt = create_mail(usender,ureceiver,u'sübject',u'bödy\nmessage',
         attach=[attachment],charset='utf-8')
     self.encrypt(bilateral, msgatt, sign=False, inline=False)
Esempio n. 5
0
 def test_uni_sign_attach(self,bilateral):
     usender = sender.replace('Foo','Föo')
     ureceiver = receiver.replace('Bar','Bär')
     attachment = create_mime(u'söme\nattachment',charset='utf-8')
     msgatt = create_mail(usender,ureceiver,u'sübject',u'bödy\nmessage',
         attach=[attachment],charset='utf-8')
     self.sign(bilateral, msgatt, inline=False)
Esempio n. 6
0
def test_attach():
    attachment = create_mime('some\nattachment')
    msg = create_mail(sender,receiver,'subject','body\nmessage',attach=[attachment])
    # boundary is generated randomly by as_string - hardcode here
    msg.set_boundary('===============1808028167789866750==')
    prot = _protected(msg)
    assert prot.as_string() == msg.as_string()
    prot = protect_mail(msg,linesep='\n',sevenbit=True)
    assert prot.as_string() == msg.as_string()
Esempio n. 7
0
def encode_mail(sender, receiver, data):
    "data must be validated by check_mail"
    from kryptomime.mail import create_mail, create_mime, check_charset, protect_mail
    from time import time as epochtime
    import email.utils
    from email.mime.text import MIMEText
    from six import iteritems
    from idapi.models import Message
    subject = data['subject']
    time = data.get('date')
    if not time: time = epochtime()
    parts = data.get('parts',None)
    if parts: # multi-part
        mail = None
        for i,part in enumerate(parts):
            ctype = part.get('content-type','text/plain').lower().split('/')
            encoding = part.get('content-encoding')
            content = part['content']
            content, charset = check_charset(content,part.get('content-charset'))
            if not i:
                msg = create_mail(sender,receiver,subject,content,time=time,subtype=ctype[1],
                    charset=charset,encoding=encoding,attach=[])
            else:
                msg = create_mime(content,*ctype,charset=charset,encoding=encoding)
                filename= part.get('filename')
                filename = dict(filename=filename) if filename else {}
                msg.add_header('Content-Disposition', 'attachment', **filename)
            params = part.get('content-params',{})
            if params:
                for k,v in iteritems(params):
                    msg.set_param(k,v)
            if i: mail.attach(msg)
            else: mail = msg
    else: # single part
        ctype = data.get('content-type','text/plain').lower().split('/')
        encoding = data.get('content-encoding')
        body, charset = check_charset(data['content'],data.get('content-charset'))
        mail = create_mail(sender,receiver,subject,body,time=time,subtype=ctype[1],
                charset=charset,encoding=encoding)
        params = data.get('content-params',{})
        if params:
            for k,v in iteritems(params):
                mail.set_param(k,v)
    return protect_mail(mail)
Esempio n. 8
0
 def test_uni_sign_attach(self, bilateral):
     usender = sender.replace('Foo', 'Föo')
     ureceiver = receiver.replace('Bar', 'Bär')
     attachment = create_mime(u'söme\nattachment', charset='utf-8')
     msgatt = create_mail(usender,
                          ureceiver,
                          u'sübject',
                          u'bödy\nmessage',
                          attach=[attachment],
                          charset='utf-8')
     self.sign(bilateral, msgatt, inline=False)
Esempio n. 9
0
 def test_uni_encrypt_attach(self, bilateral):
     usender = sender.replace('Foo', 'Föo')
     ureceiver = receiver.replace('Bar', 'Bär')
     attachment = email.mime.text.MIMEText(u'söme\nattachment',
                                           _charset='utf-8')
     msgatt = create_mail(usender,
                          ureceiver,
                          u'sübject',
                          u'bödy\nmessage',
                          attach=[attachment],
                          charset='utf-8')
     self.encrypt(bilateral, msgatt, sign=False, inline=False)
Esempio n. 10
0
# For more details see the file COPYING.

from pytest import fixture, mark, raises

from kryptomime import KeyMissingError
from kryptomime.mail import create_mail, protect_mail
from kryptomime.smime import OpenSMIME, Certificate, PrivateKey, MemoryKeyStore, OpenSSL, OpenSSL_CA

import email.mime.text

from conftest import sender, receiver
from test_openssl import x509keys, openssl

passphrase='mysecret'
attachment = email.mime.text.MIMEText('some\nattachment')
msg = create_mail(sender,receiver,'subject','body\nmessage')
msg.epilogue=''
msgatt = create_mail(sender,receiver,'subject','body\nmessage',attach=[attachment])
msgrev = create_mail(receiver,sender,'subject','body\nmessage')
msgself = create_mail(sender,sender,'subject','body\nmessage')
prot = protect_mail(msg,linesep='\r\n')
protatt = protect_mail(msgatt,linesep='\r\n')

def compare_mail(a,b):
    if type(a)==str: return a==b
    assert a.is_multipart() == b.is_multipart()
    #from kryptomime.mail import ProtectedMessage
    #assert isinstance(a,ProtectedMessage)==isinstance(b,ProtectedMessage)
    # todo headers
    if a.is_multipart():
        for i in range(len(a.get_payload())):
Esempio n. 11
0
def test_8bit(attachments):
    expect=b'''Content-Type: multipart/mixed; boundary="===============6661726347990728450=="
MIME-Version: 1.0
From: Foo <foo@localhost>
To: Bar <bar@localhost>
Subject: subject
Date: Mon, 02 Feb 2015 12:00:00 +0100

--===============6661726347990728450==
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64

w7xiZXIKMQoy
--===============6661726347990728450==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

uber
--===============6661726347990728450==
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64

w7xiZXIKMQoy
--===============6661726347990728450==
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64

w7xiZXIKMQoy
--===============6661726347990728450==
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

=C3=BCber
1
2
--===============6661726347990728450==
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

\xc3\xbcber
1
2
--===============6661726347990728450==
Content-Type: text/plain; charset="iso8859-1"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

/GJlcgoxCjI=
--===============6661726347990728450==
Content-Type: text/plain; charset="iso8859-1"
MIME-Version: 1.0
Content-Transfer-Encoding: base64

/GJlcgoxCjI=
--===============6661726347990728450==
Content-Type: text/plain; charset="iso8859-1"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable

=FCber
1
2
--===============6661726347990728450==
Content-Type: text/plain; charset="iso8859-1"
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit

\xfcber
1
2
--===============6661726347990728450==--
'''
    s = u'über\n1\n2'
    msg = create_mail(sender,receiver,'subject',s,attach=attachments,time='Mon, 02 Feb 2015 12:00:00 +0100')
    msg.set_boundary('===============6661726347990728450==')
    out = mail_binary(msg)
    assert out==expect
    prot = protect_mail(out,linesep='\n',sevenbit=False)
    att = prot.get_payload()
    out2 = mail_binary(prot)
    assert out==out2
    compare_mail(msg,prot)
Esempio n. 12
0
def encode_mail(msg, sender):
    from kryptomime.mail import create_mail, create_mime, check_charset
    from time import time as epochtime
    import email.utils
    from email.mime.text import MIMEText
    from six import iteritems
    from idapi.models import Message
    user, data = msg.user, msg.data
    receiver = user.email
    subject = data['subject']
    time = data.get('date', epochtime())
    parts = data.get('parts', None)
    if parts:  # multi-part
        mail = None
        for i, part in enumerate(parts):
            ctype = part.get('content-type', 'text/plain').lower().split('/')
            encoding = part.get('content-encoding')
            content = part['content']
            content, charset = check_charset(content,
                                             part.get('content-charset'))
            if i:
                msg = create_mime(content,
                                  *ctype,
                                  charset=charset,
                                  encoding=encoding)
                filename = part.get('filename')
                filename = dict(filename=filename) if filename else {}
                msg.add_header('Content-Disposition', 'attachment', **filename)
            else:
                assert ctype[0] == 'text'
                msg = create_mail(sender,
                                  receiver,
                                  subject,
                                  content,
                                  time=time,
                                  subtype=ctype[1],
                                  charset=charset,
                                  encoding=encoding)
            params = part.get('content-params', {})
            if params:
                assert type(params) == dict
                for k, v in iteritems(params):
                    msg.set_param(k, v)
            if i: mail.attach(msg)
            else: mail = msg
    else:  # single part
        ctype = data.get('content-type', 'text/plain').lower().split('/')
        assert ctype[0] == 'text'
        encoding = data.get('content-encoding')
        body, charset = check_charset(data['content'],
                                      data.get('content-charset'))
        mail = create_mail(sender,
                           receiver,
                           subject,
                           body,
                           time=time,
                           subtype=ctype[1],
                           charset=charset,
                           encoding=encoding)
        params = data.get('content-params', {})
        if params:
            assert type(params) == dict
            for k, v in iteritems(params):
                mail.set_param(k, v)
    #print mail
    return mail
Esempio n. 13
0
from itertools import product

from kryptomime import KeyMissingError
from kryptomime.mail import create_mail, create_mime, protect_mail
from kryptomime.pgp import GPGMIME, find_gnupg_key
from kryptomime.utils import tmpfname

import gnupg, email.mime.text
"""
TODO: 'multipart/encrypted' but single-part, text/plain but encrypted, 'multipart/mixed' but signed
"""
from conftest import sender, receiver, compare_mail

passphrase = 'mysecret'
attachment = email.mime.text.MIMEText('some\nattachment')
msg = create_mail(sender, receiver, 'subject', 'body\nmessage')
msg.epilogue = ''
msgatt = create_mail(sender,
                     receiver,
                     'subject',
                     'body\nmessage',
                     attach=[attachment])
msgrev = create_mail(receiver, sender, 'subject', 'body\nmessage')
msgself = create_mail(sender, sender, 'subject', 'body\nmessage')
prot = protect_mail(msg, linesep='\r\n')
protatt = protect_mail(msgatt, linesep='\r\n')
bools = [False, True]


@fixture(scope='module')
def keys(request):