Esempio n. 1
0
    def test_to_message_from_message_content_encoding_keys(self):
        for msg in self.mailbox:
            m = encoding.from_message(msg)
            out = encoding.to_message(m)
            assert repr(out)

            m2 = encoding.from_message(out)
            # salmon adds stuff to content headers that's missing from example messages
            for k in encoding.CONTENT_ENCODING_KEYS:
                if k == "Content-Transfer-Encoding" \
                        and m.content_encoding[k][0] is None \
                        and m2.content_encoding[k][0] == "7bit":
                    continue  # salmon fixing bad email
                elif k == "Mime-Version" \
                        and m.content_encoding[k][0] is None \
                        and m2.content_encoding[k][0] == "1.0":
                    continue  # again, salmon fixing bad email

                assert m.content_encoding[k][0] == m2.content_encoding[k][0], \
                    "%s: %r != %r" % (k, m.content_encoding[k], m2.content_encoding[k])

                for p in m.content_encoding[k][1]:
                    if p in m2.content_encoding[k][1]:
                        assert m.content_encoding[k][1][p].lower() == m2.content_encoding[k][1][p].lower(), \
                            "%s: %r != %r" % (p, m.content_encoding[k][1][p], m2.content_encoding[k][1][p])
Esempio n. 2
0
def test_to_message_from_message_with_spam():
    mb = mailbox.mbox("tests/spam")
    fails = 0
    total = 0

    for msg in mb:
        try:
            m = encoding.from_message(msg)
            out = encoding.to_message(m)
            assert repr(out)

            m2 = encoding.from_message(out)

            for k in m:
                if '@' in m[k]:
                    assert_equal(parseaddr(m[k]), parseaddr(m2[k]))
                else:
                    assert m[k].strip() == m2[k].strip(), "%s: %r != %r" % (k, m[k], m2[k])

                assert not m[k].startswith(u"=?")
                assert not m2[k].startswith(u"=?")
                assert m.body == m2.body, "Bodies don't match" 

                assert_equal(len(m.parts), len(m2.parts), "Not the same number of parts.")

                for i, part in enumerate(m.parts):
                    assert part.body == m2.parts[i].body, "Part %d isn't the same: %r \nvs\n. %r" % (i, part.body, m2.parts[i].body)
            total += 1
        except encoding.EncodingError, exc:
            fails += 1
Esempio n. 3
0
def test_to_message_from_message_with_spam():
    mb = mailbox.mbox("tests/spam")
    fails = 0
    total = 0

    for msg in mb:
        try:
            m = encoding.from_message(msg)
            out = encoding.to_message(m)
            assert repr(out)

            m2 = encoding.from_message(out)

            for k in m:
                if '@' in m[k]:
                    assert_equal(parseaddr(m[k]), parseaddr(m2[k]))
                else:
                    assert m[k].strip() == m2[k].strip(), "%s: %r != %r" % (
                        k, m[k], m2[k])

                assert not m[k].startswith(u"=?")
                assert not m2[k].startswith(u"=?")
                assert m.body == m2.body, "Bodies don't match"

                assert_equal(len(m.parts), len(m2.parts),
                             "Not the same number of parts.")

                for i, part in enumerate(m.parts):
                    assert part.body == m2.parts[
                        i].body, "Part %d isn't the same: %r \nvs\n. %r" % (
                            i, part.body, m2.parts[i].body)
            total += 1
        except encoding.EncodingError, exc:
            fails += 1
Esempio n. 4
0
def test_to_message_from_message_with_spam():
    mb = mailbox.mbox("tests/spam")

    for msg in mb:
        m = encoding.from_message(msg)
        out = encoding.to_message(m)
        assert repr(out)

        m2 = encoding.from_message(out)

        for k in m:
            if '@' in m[k]:
                assert_equal(parseaddr(m[k]), parseaddr(m2[k]))
            elif k.lower() in [
                    key.lower() for key in encoding.CONTENT_ENCODING_KEYS
            ]:
                pass  # skip!
            else:
                assert m[k].strip() == m2[k].strip(), "%s: %r != %r" % (
                    k, m[k], m2[k])

            assert not m[k].startswith(u"=?")
            assert not m2[k].startswith(u"=?")

        # salmon adds stuff to content headers that's missing from example messages
        for k in encoding.CONTENT_ENCODING_KEYS:
            if k == "Content-Transfer-Encoding" \
                    and m.content_encoding[k][0] is None \
                    and m2.content_encoding[k][0] == "7bit":
                continue  # salmon fixing bad email
            elif k == "Mime-Version" \
                    and m.content_encoding[k][0] is None \
                    and m2.content_encoding[k][0] == "1.0":
                continue  # again, salmon fixing bad email

            assert m.content_encoding[k][0] == m2.content_encoding[k][0], \
                "%s: %r != %r" % (k, m.content_encoding[k], m2.content_encoding[k])

            for p in m.content_encoding[k][1]:
                if p in m2.content_encoding[k][1]:
                    assert m.content_encoding[k][1][p].lower() == m2.content_encoding[k][1][p].lower(), \
                        "%s: %r != %r" % (p, m.content_encoding[k][1][p], m2.content_encoding[k][1][p])

        assert m.body == m2.body, "Bodies don't match"

        assert_equal(len(m.parts), len(m2.parts),
                     "Not the same number of parts.")

        for i, part in enumerate(m.parts):
            assert part.body == m2.parts[i].body, \
                "Part %d isn't the same: %r \nvs\n. %r" % (i, part.body, m2.parts[i].body)
Esempio n. 5
0
    def test_to_message_from_message_bodies(self):
        for msg in self.mailbox:
            m = encoding.from_message(msg)
            out = encoding.to_message(m)
            assert repr(out)

            m2 = encoding.from_message(out)
            assert m.body == m2.body, "Bodies don't match"

            self.assertEqual(len(m.parts), len(m2.parts),
                             "Not the same number of parts.")

            for i, part in enumerate(m.parts):
                assert part.body == m2.parts[i].body, \
                    "Part %d isn't the same: %r \nvs\n. %r" % (i, part.body, m2.parts[i].body)
Esempio n. 6
0
    def test_MIMEPart(self):
        text1 = encoding.MIMEPart("text/plain")
        text1.set_payload("The first payload.")
        text2 = encoding.MIMEPart("text/plain")
        text2.set_payload("The second payload.")

        with open("tests/data/salmon.png", "rb") as file_obj:
            image_data = file_obj.read()
        img1 = encoding.MIMEPart("image/png")
        img1.set_payload(image_data)
        img1.set_param('attachment', '', header='Content-Disposition')
        img1.set_param('filename', 'salmon.png', header='Content-Disposition')
        encoders.encode_base64(img1)

        multi = encoding.MIMEPart("multipart/mixed")
        for x in [text1, text2, img1]:
            multi.attach(x)

        mail = encoding.from_message(multi)

        self.assertEqual(mail.parts[0].body, "The first payload.")
        self.assertEqual(mail.parts[1].body, "The second payload.")
        self.assertEqual(mail.parts[2].body, image_data)

        encoding.to_message(mail)
Esempio n. 7
0
def test_MailBase():
    the_subject = u"p\xf6stal"
    m = encoding.MailBase()

    m["To"] = "testing@localhost"
    m["Subject"] = the_subject
    m["Content-Type"] = "text/plain; charset=iso-8859-1"
    m["MIME-Version"] = "1.0"

    assert m["To"] == "testing@localhost"
    assert m["TO"] == m["To"]
    assert m["to"] == m["To"]

    assert m["Subject"] == the_subject
    assert m["subject"] == m["Subject"]
    assert m["sUbjeCt"] == m["Subject"]

    msg = encoding.to_message(m)
    m2 = encoding.from_message(msg)

    assert_equal(len(m), len(m2))

    for k in m:
        assert m[k] == m2[k], "%s: %r != %r" % (k, m[k], m2[k])

    for k in m.keys():
        assert k in m
        del m[k]
        assert k not in m
Esempio n. 8
0
def test_MailBase():
    the_subject = u'p\xf6stal'
    m = encoding.MailBase()

    m['To'] = "testing@localhost"
    m['Subject'] = the_subject
    m['Content-Type'] = 'text/plain; charset=iso-8859-1'
    m['MIME-Version'] = '1.0'

    assert_equal(m['To'], "testing@localhost")
    assert_equal(m['TO'], m['To'])
    assert_equal(m['to'], m['To'])

    assert_equal(m['Subject'], the_subject)
    assert_equal(m['subject'], m['Subject'])
    assert_equal(m['sUbjeCt'], m['Subject'])

    msg = encoding.to_message(m)
    m2 = encoding.from_message(msg)

    assert_equal(len(m), len(m2))

    assert_equal(m.items(), m2.items())

    for k in m:
        assert m[k] == m2[k], "%s: %r != %r" % (k, m[k], m2[k])

    assert ("To" in m)
    assert ("Bob" not in m)
    assert_equal(m["To"], "testing@localhost")
    m["To"] = "somebody@localhost"
    assert_equal(m["To"], "somebody@localhost")
    assert_equal(m.keys(), ['Subject', 'Mime-Version', 'Content-Type', 'To'])
    assert_equal(m.items(),
                 [('Subject', the_subject), ('Mime-Version', '1.0'),
                  ('Content-Type', 'text/plain; charset="iso-8859-1"'),
                  ('To', 'somebody@localhost')])
    assert_equal(m.get_all("Subject"), [the_subject])
    assert_equal(m.get_all("Bob"), [])

    # appending headers
    m.append_header("subject", "something else")
    assert_equal(m["Subject"], the_subject)
    assert_equal(m.keys(),
                 ['Subject', 'Mime-Version', 'Content-Type', 'To', "Subject"])
    assert_equal(m.items(),
                 [('Subject', the_subject), ('Mime-Version', '1.0'),
                  ('Content-Type', 'text/plain; charset="iso-8859-1"'),
                  ('To', 'somebody@localhost'), ('Subject', "something else")])
    assert_equal(m.get_all("Subject"), [the_subject, "something else"])

    # reset subject header
    m["Subject"] = the_subject

    for key, item in zip(m.keys(), m.items()):
        assert_equal(key, item[0])
        assert key in m
        del m[key]
        assert key not in m
        assert key not in [i[0] for i in m.items()]
Esempio n. 9
0
def test_MailBase():
    the_subject = u'p\xf6stal'
    m = encoding.MailBase()

    m['To'] = "testing@localhost"
    m['Subject'] = the_subject

    assert m['To'] == "testing@localhost"
    assert m['TO'] == m['To']
    assert m['to'] == m['To']

    assert m['Subject'] == the_subject
    assert m['subject'] == m['Subject']
    assert m['sUbjeCt'] == m['Subject']

    msg = encoding.to_message(m)
    m2 = encoding.from_message(msg)

    assert_equal(len(m), len(m2))

    for k in m:
        assert m[k] == m2[k], "%s: %r != %r" % (k, m[k], m2[k])

    for k in m.keys():
        assert k in m
        del m[k]
        assert not k in m
Esempio n. 10
0
def test_MailBase():
    the_subject = u'p\xf6stal'
    m = encoding.MailBase()

    m['To'] = "testing@localhost"
    m['Subject'] = the_subject
    m['Content-Type'] = 'text/plain; charset=iso-8859-1'
    m['MIME-Version'] = '1.0'

    assert m['To'] == "testing@localhost"
    assert m['TO'] == m['To']
    assert m['to'] == m['To']

    assert m['Subject'] == the_subject
    assert m['subject'] == m['Subject']
    assert m['sUbjeCt'] == m['Subject']

    msg = encoding.to_message(m)
    m2 = encoding.from_message(msg)

    assert_equal(len(m), len(m2))

    for k in m:
        assert m[k] == m2[k], "%s: %r != %r" % (k, m[k], m2[k])

    for k in m.keys():
        assert k in m
        del m[k]
        assert k not in m
Esempio n. 11
0
def test_MailBase():
    the_subject = u'p\xf6stal'
    m = encoding.MailBase()
    
    m['To'] = "testing@localhost"
    m['Subject'] = the_subject

    assert m['To'] == "testing@localhost"
    assert m['TO'] == m['To']
    assert m['to'] == m['To']

    assert m['Subject'] == the_subject
    assert m['subject'] == m['Subject']
    assert m['sUbjeCt'] == m['Subject']
    
    msg = encoding.to_message(m)
    m2 = encoding.from_message(msg)

    assert_equal(len(m), len(m2))

    for k in m:
        assert m[k] == m2[k], "%s: %r != %r" % (k, m[k], m2[k])
    
    for k in m.keys():
        assert k in m
        del m[k]
        assert not k in m
Esempio n. 12
0
def cleanse(inbox, outbox):
    """
    Uses Salmon mail cleansing and canonicalization system to take an
    input Maildir (or mbox) and replicate the email over into another
    Maildir.  It's used mostly for testing and cleaning.
    """
    error_count = 0

    try:
        inbox = mailbox.mbox(inbox)
    except IOError:
        inbox = mailbox.Maildir(inbox, factory=None)

    outbox = mailbox.Maildir(outbox)

    for msg in inbox:
        try:
            mail = encoding.from_message(msg)
            outbox.add(encoding.to_string(mail))
        except encoding.EncodingError as exc:
            click.echo("ERROR: %s" % exc)
            error_count += 1

    outbox.close()
    inbox.close()

    if error_count > 0:
        raise SalmonCommandError("TOTAL ERRORS: %s" % error_count)
    else:
        click.echo("Completed without errors")
Esempio n. 13
0
def test_to_message_from_message_with_spam():
    mb = mailbox.mbox("tests/spam")

    for msg in mb:
        m = encoding.from_message(msg)
        out = encoding.to_message(m)
        assert repr(out)

        m2 = encoding.from_message(out)

        for k in m:
            if '@' in m[k]:
                assert_equal(parseaddr(m[k]), parseaddr(m2[k]))
            elif k.lower() in [key.lower() for key in encoding.CONTENT_ENCODING_KEYS]:
                pass  # skip!
            else:
                assert m[k].strip() == m2[k].strip(), "%s: %r != %r" % (k, m[k], m2[k])

            assert not m[k].startswith(u"=?")
            assert not m2[k].startswith(u"=?")

        # salmon adds stuff to content headers that's missing from example messages
        for k in encoding.CONTENT_ENCODING_KEYS:
            if k == "Content-Transfer-Encoding" \
                    and m.content_encoding[k][0] is None \
                    and m2.content_encoding[k][0] == "7bit":
                continue  # salmon fixing bad email
            elif k == "Mime-Version" \
                    and m.content_encoding[k][0] is None \
                    and m2.content_encoding[k][0] == "1.0":
                continue  # again, salmon fixing bad email

            assert m.content_encoding[k][0] == m2.content_encoding[k][0], \
                "%s: %r != %r" % (k, m.content_encoding[k], m2.content_encoding[k])

            for p in m.content_encoding[k][1]:
                if p in m2.content_encoding[k][1]:
                    assert m.content_encoding[k][1][p].lower() == m2.content_encoding[k][1][p].lower(), \
                        "%s: %r != %r" % (p, m.content_encoding[k][1][p], m2.content_encoding[k][1][p])

        assert m.body == m2.body, "Bodies don't match"

        assert_equal(len(m.parts), len(m2.parts), "Not the same number of parts.")

        for i, part in enumerate(m.parts):
            assert part.body == m2.parts[i].body, \
                "Part %d isn't the same: %r \nvs\n. %r" % (i, part.body, m2.parts[i].body)
Esempio n. 14
0
    def test_to_message_from_message_headers(self):
        for msg in self.mailbox:
            m = encoding.from_message(msg)
            out = encoding.to_message(m)
            assert repr(out)

            m2 = encoding.from_message(out)

            for k in m:
                if '@' in m[k]:
                    self.assertEqual(parseaddr(m[k]), parseaddr(m2[k]))
                elif k in encoding.CONTENT_ENCODING_KEYS:
                    pass  # skip!
                else:
                    assert m[k].strip() == m2[k].strip(), "%s: %r != %r" % (
                        k, m[k], m2[k])

                assert not m[k].startswith("=?")
                assert not m2[k].startswith("=?")
Esempio n. 15
0
    def test_to_file_from_file(self):
        mb = mailbox.mbox("tests/data/spam")
        msg = encoding.from_message(mb[0])

        outfile = "run/encoding_test.msg"

        with open(outfile, 'w') as outfp:
            encoding.to_file(msg, outfp)

        with open(outfile) as outfp:
            msg2 = encoding.from_file(outfp)

        self.assertEqual(len(msg), len(msg2))
        os.unlink(outfile)
Esempio n. 16
0
def test_to_file_from_file():
    mb = mailbox.mbox("tests/spam")
    msg = encoding.from_message(mb[0])

    outfile = "run/encoding_test.msg"

    with open(outfile, 'w') as outfp:
        encoding.to_file(msg, outfp)

    with open(outfile) as outfp:
        msg2 = encoding.from_file(outfp)

    assert_equal(len(msg), len(msg2))
    os.unlink(outfile)
Esempio n. 17
0
def test_to_message_from_message_with_spam():
    raise SkipTest  # skip this
    mb = mailbox.mbox("tests/spam")
    fails = 0
    total = 0

    for msg in mb:
        try:
            m = encoding.from_message(msg)
            out = encoding.to_message(m)
            assert repr(out)

            m2 = encoding.from_message(out)

            for k in m:
                if "@" in m[k]:
                    assert_equal(parseaddr(m[k]), parseaddr(m2[k]))
                else:
                    assert m[k].strip() == m2[k].strip(), "%s: %r != %r" % (k, m[k], m2[k])

                assert not m[k].startswith(u"=?")
                assert not m2[k].startswith(u"=?")
                assert m.body == m2.body, "Bodies don't match"

                assert_equal(len(m.parts), len(m2.parts), "Not the same number of parts.")

                for i, part in enumerate(m.parts):
                    assert part.body == m2.parts[i].body, "Part %d isn't the same: %r \nvs\n. %r" % (
                        i,
                        part.body,
                        m2.parts[i].body,
                    )
            total += 1
        except encoding.EncodingError:
            fails += 1

    assert fails / total < 0.01, "There were %d failures out of %d total." % (fails, total)
Esempio n. 18
0
    def command(input, output):
        error_count = 0

        try:
            inbox = mailbox.mbox(input)
        except IOError:
            inbox = mailbox.Maildir(input, factory=None)

        outbox = mailbox.Maildir(output)

        for msg in inbox:
            try:
                mail = encoding.from_message(msg)
                outbox.add(encoding.to_string(mail))
            except encoding.EncodingError, exc:
                print "ERROR: ", exc
                error_count += 1
Esempio n. 19
0
    def command(input, output):
        error_count = 0

        try:
            inbox = mailbox.mbox(input)
        except IOError:
            inbox = mailbox.Maildir(input, factory=None)

        outbox = mailbox.Maildir(output)

        for msg in inbox:
            try:
                mail = encoding.from_message(msg)
                outbox.add(encoding.to_string(mail))
            except encoding.EncodingError, exc:
                print "ERROR: ", exc
                error_count += 1
Esempio n. 20
0
def cleanse_command(input=None, output=None):
    """
    Uses Salmon mail cleansing and canonicalization system to take an
    input maildir (or mbox) and replicate the email over into another
    maildir.  It's used mostly for testing and cleaning.
    """
    error_count = 0

    try:
        inbox = mailbox.mbox(input)
    except IOError:
        inbox = mailbox.Maildir(input, factory=None)

    outbox = mailbox.Maildir(output)

    for msg in inbox:
        try:
            mail = encoding.from_message(msg)
            outbox.add(encoding.to_string(mail))
        except encoding.EncodingError, exc:
            print "ERROR: ", exc
            error_count += 1
Esempio n. 21
0
    def command(input, output):
        error_count = 0

        try:
            inbox = mailbox.mbox(input)
        except IOError:
            inbox = mailbox.Maildir(input, factory=None)

        outbox = mailbox.Maildir(output)

        for msg in inbox:
            try:
                mail = encoding.from_message(msg)
                outbox.add(encoding.to_string(mail))
            except encoding.EncodingError as exc:
                print("ERROR: %s" % exc)
                error_count += 1

        outbox.close()
        inbox.close()

        print("TOTAL ERRORS: %s" % error_count)
Esempio n. 22
0
def cleanse_command(input=None, output=None):
    """
    Uses Salmon mail cleansing and canonicalization system to take an
    input maildir (or mbox) and replicate the email over into another
    maildir.  It's used mostly for testing and cleaning.
    """
    error_count = 0

    try:
        inbox = mailbox.mbox(input)
    except IOError:
        inbox = mailbox.Maildir(input, factory=None)

    outbox = mailbox.Maildir(output)

    for msg in inbox:
        try:
            mail = encoding.from_message(msg)
            outbox.add(encoding.to_string(mail))
        except encoding.EncodingError, exc:
            print "ERROR: ", exc
            error_count += 1
Esempio n. 23
0
    def command(input, output):
        error_count = 0

        try:
            inbox = mailbox.mbox(input)
        except IOError:
            inbox = mailbox.Maildir(input, factory=None)

        outbox = mailbox.Maildir(output)

        for msg in inbox:
            try:
                mail = encoding.from_message(msg)
                outbox.add(encoding.to_string(mail))
            except encoding.EncodingError as exc:
                print("ERROR: %s" % exc)
                error_count += 1

        outbox.close()
        inbox.close()

        print("TOTAL ERRORS: %s" % error_count)
Esempio n. 24
0
def test_MIMEPart():
    text1 = encoding.MIMEPart("text/plain")
    text1.set_payload("The first payload.")
    text2 = encoding.MIMEPart("text/plain")
    text2.set_payload("The second payload.")

    image_data = open("tests/salmon.png").read()
    img1 = encoding.MIMEPart("image/png")
    img1.set_payload(image_data)
    img1.set_param('attachment','', header='Content-Disposition')
    img1.set_param('filename','salmon.png', header='Content-Disposition')
    encoders.encode_base64(img1)
    
    multi = encoding.MIMEPart("multipart/mixed")
    for x in [text1, text2, img1]:
        multi.attach(x)

    mail = encoding.from_message(multi)

    assert mail.parts[0].body == "The first payload."
    assert mail.parts[1].body == "The second payload."
    assert mail.parts[2].body == image_data

    encoding.to_message(mail)
Esempio n. 25
0
def test_MailBase():
    the_subject = u'p\xf6stal'
    m = encoding.MailBase()

    m['To'] = "testing@localhost"
    m['Subject'] = the_subject
    m['Content-Type'] = 'text/plain; charset=iso-8859-1'
    m['MIME-Version'] = '1.0'

    assert_equal(m['To'], "testing@localhost")
    assert_equal(m['TO'], m['To'])
    assert_equal(m['to'], m['To'])

    assert_equal(m['Subject'], the_subject)
    assert_equal(m['subject'], m['Subject'])
    assert_equal(m['sUbjeCt'], m['Subject'])

    msg = encoding.to_message(m)
    m2 = encoding.from_message(msg)

    assert_equal(len(m), len(m2))

    assert_equal(m.items(), m2.items())

    for k in m:
        assert m[k] == m2[k], "%s: %r != %r" % (k, m[k], m2[k])

    assert("To" in m)
    assert("Bob" not in m)
    assert_equal(m["To"], "testing@localhost")
    m["To"] = "somebody@localhost"
    assert_equal(m["To"], "somebody@localhost")
    assert_equal(m.keys(), ['Subject', 'Mime-Version', 'Content-Type', 'To'])
    assert_equal(m.items(), [
        ('Subject', the_subject),
        ('Mime-Version', '1.0'),
        ('Content-Type', 'text/plain; charset="iso-8859-1"'),
        ('To', 'somebody@localhost')])
    assert_equal(m.get_all("Subject"), [the_subject])
    assert_equal(m.get_all("Bob"), [])

    # appending headers
    m.append_header("subject", "something else")
    assert_equal(m["Subject"], the_subject)
    assert_equal(m.keys(), ['Subject', 'Mime-Version', 'Content-Type', 'To', "Subject"])
    assert_equal(m.items(), [
        ('Subject', the_subject),
        ('Mime-Version', '1.0'),
        ('Content-Type', 'text/plain; charset="iso-8859-1"'),
        ('To', 'somebody@localhost'),
        ('Subject', "something else")])
    assert_equal(m.get_all("Subject"), [the_subject, "something else"])

    # reset subject header
    m["Subject"] = the_subject

    for key, item in zip(m.keys(), m.items()):
        assert_equal(key, item[0])
        assert key in m
        del m[key]
        assert key not in m
        assert key not in [i[0] for i in m.items()]