forked from SpamScope/mail-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mail_parser.py
More file actions
955 lines (768 loc) · 32.8 KB
/
test_mail_parser.py
File metadata and controls
955 lines (768 loc) · 32.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
#!/usr/bin/env python
"""
Copyright 2016 Fedele Mantuano (https://twitter.com/fedelemantuano)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import datetime
import hashlib
import os
import shutil
import sys
import tempfile
import unittest
from unittest.mock import patch
import mailparser
from mailparser.utils import (
convert_mail_date,
fingerprints,
get_header,
get_mail_keys,
get_to_domains,
parse_received,
ported_open,
ported_string,
random_string,
receiveds_parsing,
)
# base paths
base_path = os.path.realpath(os.path.dirname(__file__))
root = os.path.join(base_path, "..")
# raw mails to test
mail_test_1 = os.path.join(base_path, "mails", "mail_test_1")
mail_test_2 = os.path.join(base_path, "mails", "mail_test_2")
mail_test_3 = os.path.join(base_path, "mails", "mail_test_3")
mail_test_4 = os.path.join(base_path, "mails", "mail_test_4")
mail_test_5 = os.path.join(base_path, "mails", "mail_test_5")
mail_test_6 = os.path.join(base_path, "mails", "mail_test_6")
mail_test_7 = os.path.join(base_path, "mails", "mail_test_7")
mail_test_8 = os.path.join(base_path, "mails", "mail_test_8")
mail_test_9 = os.path.join(base_path, "mails", "mail_test_9")
mail_test_10 = os.path.join(base_path, "mails", "mail_test_10")
mail_test_11 = os.path.join(base_path, "mails", "mail_test_11")
mail_test_12 = os.path.join(base_path, "mails", "mail_test_12")
mail_test_13 = os.path.join(base_path, "mails", "mail_test_13")
mail_test_14 = os.path.join(base_path, "mails", "mail_test_14")
mail_test_15 = os.path.join(base_path, "mails", "mail_test_15")
mail_test_16 = os.path.join(base_path, "mails", "mail_test_16")
mail_test_17 = os.path.join(base_path, "mails", "mail_test_17")
mail_test_18 = os.path.join(base_path, "mails", "mail_test_18")
mail_malformed_1 = os.path.join(base_path, "mails", "mail_malformed_1")
mail_malformed_2 = os.path.join(base_path, "mails", "mail_malformed_2")
mail_malformed_3 = os.path.join(base_path, "mails", "mail_malformed_3")
mail_outlook_1 = os.path.join(base_path, "mails", "mail_outlook_1")
class TestMailParser(unittest.TestCase):
def setUp(self):
self.all_mails = (
mail_test_1,
mail_test_2,
mail_test_3,
mail_test_4,
mail_test_5,
mail_test_6,
mail_test_7,
mail_test_8,
mail_test_9,
mail_test_10,
mail_test_11,
mail_test_12,
mail_test_13,
mail_malformed_1,
mail_malformed_2,
mail_malformed_3,
)
def test_write_attachments(self):
attachments = [
"<_1_0B4E44A80B15F6FC005C1243C12580DD>",
"<_1_0B4E420C0B4E3DD0005C1243C12580DD>",
"<_1_0B4E24640B4E1564005C1243C12580DD>",
"Move To Eight ZWEP6227F.pdf",
]
random_path = os.path.join(root, "tests", random_string())
mail = mailparser.parse_from_file(mail_test_10)
os.makedirs(random_path)
mail.write_attachments(random_path)
for i in attachments:
self.assertTrue(os.path.exists(os.path.join(random_path, i)))
shutil.rmtree(random_path)
def test_issue62(self):
mail = mailparser.parse_from_file(mail_test_14)
received_spf = mail.Received_SPF
self.assertIsInstance(received_spf, list)
self.assertIn("custom_header1", received_spf)
self.assertIn("custom_header2", received_spf)
def test_html_field(self):
mail = mailparser.parse_from_file(mail_malformed_1)
self.assertIsInstance(mail.text_html, list)
self.assertIsInstance(mail.text_html_json, str)
self.assertEqual(len(mail.text_html), 1)
def test_text_not_managed(self):
mail = mailparser.parse_from_file(mail_test_14)
self.assertIsInstance(mail.text_not_managed, list)
self.assertIsInstance(mail.text_not_managed_json, str)
self.assertEqual(len(mail.text_not_managed), 1)
self.assertEqual("PNG here", mail.text_not_managed[0])
def test_get_mail_keys(self):
mail = mailparser.parse_from_file(mail_test_11)
all_parts = get_mail_keys(mail.message)
mains_parts = get_mail_keys(mail.message, False)
self.assertNotEqual(all_parts, mains_parts)
self.assertIn("message-id", mains_parts)
self.assertIn("x-filterd-recvd-size", all_parts)
self.assertNotIn("x-filterd-recvd-size", mains_parts)
def test_mail_partial(self):
mail = mailparser.parse_from_file(mail_test_10)
self.assertNotEqual(mail.mail, mail.mail_partial)
self.assertIn("message-id", mail.mail_partial)
self.assertIn("x-ibm-av-version", mail.mail)
self.assertNotIn("x-ibm-av-version", mail.mail_partial)
result = mail.mail_partial_json
self.assertIsInstance(result, str)
nr_attachments = len(mail._attachments)
self.assertEqual(nr_attachments, 4)
def test_not_parsed_received(self):
mail = mailparser.parse_from_file(mail_test_9)
for i in mail.received:
self.assertNotIn("raw", i)
self.assertIn("hop", i)
def test_issue_received(self):
mail = mailparser.parse_from_file(mail_test_8)
for i in mail.received:
self.assertIn("date_utc", i)
self.assertIsNotNone(i["date_utc"])
def test_get_header(self):
mail = mailparser.parse_from_file(mail_test_1)
h1 = get_header(mail.message, "from")
self.assertIsInstance(h1, str)
def test_receiveds_parsing(self):
for i in self.all_mails:
mail = mailparser.parse_from_file(i)
receiveds = mail.received_raw
result = receiveds_parsing(receiveds)
self.assertIsInstance(result, list)
for j in result:
self.assertIsInstance(j, dict)
self.assertIn("hop", j)
self.assertIn("delay", j)
def test_ipaddress(self):
mail = mailparser.parse_from_file(mail_test_2)
trust = "smtp.customers.net"
ip = "217.76.210.112"
result = mail.get_server_ipaddress(trust)
self.assertEqual(result, ip)
trust = ""
result = mail.get_server_ipaddress(trust)
self.assertIsNone(result)
trust = " "
result = mail.get_server_ipaddress(trust)
self.assertIsNone(result)
def test_ipaddress_unicodeerror(self):
mail = mailparser.parse_from_file(mail_test_12)
trust = "localhost"
result = mail.get_server_ipaddress(trust)
self.assertEqual(result, "96.202.181.20")
def test_fingerprints_body(self):
mail = mailparser.parse_from_file(mail_test_1)
md5, sha1, sha256, sha512 = fingerprints(mail.body.encode("utf-8"))
self.assertEqual(md5, "55852a2efe95e7249887c92cc02123f8")
self.assertEqual(sha1, "62fef1e38327ed09363624c3aff8ea11723ee05f")
self.assertEqual(
sha256,
("cd4af1017f2e623f6d38f691048b6a28d8b1f44a0478137b4337eac6de78f71a"),
)
self.assertEqual(
sha512,
(
"4a573c7929b078f2a2c1c0f869d418b0c020d4"
"d37196bd6dcc209f9ccb29ca67355aa5e47b97"
"c8bf90377204f59efde7ba1fc071b6f250a665"
"72f63b997e92e8"
),
)
def test_fingerprints_unicodeencodeerror(self):
mail = mailparser.parse_from_file(mail_test_7)
for i in mail.attachments:
fingerprints(i["payload"])
def test_malformed_mail(self):
mail = mailparser.parse_from_file(mail_malformed_3)
defects_categories = mail.defects_categories
self.assertIn("StartBoundaryNotFoundDefect", defects_categories)
self.assertIn("MultipartInvariantViolationDefect", defects_categories)
self.assertIn("reply-to", mail.mail)
self.assertNotIn("reply_to", mail.mail)
reply_to = [("VICTORIA Souvenirs", "smgesi4@gmail.com")]
self.assertEqual(mail.reply_to, reply_to)
self.assertEqual(mail.fake_header, str())
# This email has header X-MSMail-Priority
msmail_priority = mail.X_MSMail_Priority
self.assertEqual(msmail_priority, "High")
def test_type_error(self):
mail = mailparser.parse_from_file(mail_test_5)
self.assertEqual(len(mail.attachments), 5)
for i in mail.attachments:
self.assertIsInstance(i["filename"], str)
def test_filename_decode(self):
mail = mailparser.parse_from_file(mail_test_11)
for i in mail.attachments:
self.assertIsInstance(i["filename"], str)
def test_valid_mail(self):
m = mailparser.parse_from_string("fake mail")
self.assertFalse(m.message)
def test_receiveds(self):
mail = mailparser.parse_from_file(mail_test_1)
self.assertEqual(len(mail.received), 6)
self.assertIsInstance(mail.received, list)
for i in mail.received:
self.assertIsInstance(i, dict)
self.assertIsInstance(mail.received_raw, list)
for i in mail.received_raw:
self.assertIsInstance(i, str)
self.assertIsInstance(mail.received_json, str)
def test_parsing_know_values(self):
mail = mailparser.parse_from_file(mail_test_2)
trust = "smtp.customers.net"
self.assertFalse(mail.has_defects)
raw = "217.76.210.112"
result = mail.get_server_ipaddress(trust)
self.assertEqual(raw, result)
raw = "<4516257BC5774408ADC1263EEBBBB73F@ad.regione.vda.it>"
result = mail.message_id
self.assertEqual(raw, result)
raw = "echo@tu-berlin.de"
result = mail.to
self.assertEqual(len(result), 2)
self.assertIsInstance(result, list)
self.assertIsInstance(result[0], tuple)
self.assertIsInstance(mail.to_json, str)
self.assertIsInstance(mail.to_raw, str)
self.assertEqual(raw, result[0][1])
raw = "meteo@regione.vda.it"
result = mail.from_
self.assertEqual(raw, result[0][1])
raw = "Bollettino Meteorologico del 29/11/2015"
result = mail.subject
self.assertEqual(raw, result)
result = mail.has_defects
self.assertFalse(result)
result = len(mail.attachments)
self.assertEqual(3, result)
self.assertIsInstance(mail.date_raw, str)
self.assertIsInstance(mail.date_json, str)
raw_utc = "2015-11-29T08:45:18+00:00"
result = mail.date.isoformat()
self.assertEqual(raw_utc, result)
def test_types(self):
mail = mailparser.parse_from_file(mail_test_2)
trust = "smtp.customers.net"
self.assertFalse(mail.has_defects)
result = mail.mail
self.assertIsInstance(result, dict)
self.assertNotIn("defects", result)
self.assertIn("has_defects", result)
result = mail.get_server_ipaddress(trust)
self.assertIsInstance(result, str)
result = mail.mail_json
self.assertIsInstance(result, str)
result = mail.headers_json
self.assertIsInstance(result, str)
result = mail.headers
self.assertIsInstance(result, dict)
result = mail.body
self.assertIsInstance(result, str)
result = mail.date
self.assertIsInstance(result, datetime.datetime)
result = mail.from_
self.assertIsInstance(result, list)
result = mail.to
self.assertIsInstance(result, list)
self.assertEqual(len(result), 2)
self.assertIsInstance(result[0], tuple)
self.assertEqual(len(result[0]), 2)
result = mail.subject
self.assertIsInstance(result, str)
result = mail.message_id
self.assertIsInstance(result, str)
result = mail.attachments
self.assertIsInstance(result, list)
result = mail.date
self.assertIsInstance(result, datetime.datetime)
result = mail.defects
self.assertIsInstance(result, list)
def test_defects(self):
mail = mailparser.parse_from_file(mail_malformed_1)
self.assertTrue(mail.has_defects)
self.assertEqual(1, len(mail.defects))
self.assertEqual(1, len(mail.defects_categories))
self.assertIn("defects", mail.mail)
self.assertIn("StartBoundaryNotFoundDefect", mail.defects_categories)
self.assertIsInstance(mail.mail_json, str)
result = len(mail.attachments)
self.assertEqual(1, result)
mail = mailparser.parse_from_file(mail_test_1)
self.assertTrue(mail.has_defects)
self.assertEqual(1, len(mail.defects))
self.assertEqual(1, len(mail.defects_categories))
self.assertIn("defects", mail.mail)
self.assertIn("CloseBoundaryNotFoundDefect", mail.defects_categories)
def test_defects_bug(self):
mail = mailparser.parse_from_file(mail_malformed_2)
self.assertTrue(mail.has_defects)
self.assertEqual(1, len(mail.defects))
self.assertEqual(1, len(mail.defects_categories))
self.assertIn("defects", mail.mail)
self.assertIn("StartBoundaryNotFoundDefect", mail.defects_categories)
self.assertIsInstance(mail.parsed_mail_json, str)
result = len(mail.attachments)
self.assertEqual(1, result)
def test_add_content_type(self):
mail = mailparser.parse_from_file(mail_test_3)
self.assertFalse(mail.has_defects)
result = mail.mail
self.assertEqual(len(result["attachments"]), 1)
self.assertIsInstance(result["attachments"][0]["mail_content_type"], str)
self.assertFalse(result["attachments"][0]["binary"])
self.assertIsInstance(result["attachments"][0]["payload"], str)
self.assertEqual(
result["attachments"][0]["content_transfer_encoding"], "quoted-printable"
)
self.assertEqual(result["attachments"][0]["charset"], "iso-8859-1")
self.assertEqual(result["attachments"][0]["content-disposition"], "inline")
mail = mailparser.parse_from_file(mail_malformed_1)
attachments = mail.mail["attachments"]
self.assertEqual(attachments[0]["content-disposition"], "")
def test_classmethods(self):
# MailParser.from_file
m = mailparser.MailParser.from_file(mail_test_3)
m.parse()
result = m.mail
self.assertEqual(len(result["attachments"]), 1)
# MailParser.from_string
m = mailparser.MailParser.from_string(m.message_as_string)
m.parse()
result = m.mail
self.assertEqual(len(result["attachments"]), 1)
def test_bug_UnicodeDecodeError(self):
m = mailparser.parse_from_file(mail_test_6)
self.assertIsInstance(m.mail, dict)
self.assertIsInstance(m.mail_json, str)
@patch("mailparser.core.os.remove")
@patch("mailparser.core.msgconvert")
def test_parse_from_file_msg(self, mock_msgconvert, mock_remove):
"""
Tested mail from VirusTotal: md5 b89bf096c9e3717f2d218b3307c69bd0
The email used for unittest were found randomly on VirusTotal and
then already publicly available so can not be considered
as privacy violation
"""
mock_msgconvert.return_value = (mail_test_2, None)
m = mailparser.parse_from_file_msg(mail_outlook_1)
mock_remove.assert_called_once_with(mail_test_2)
email = m.mail
self.assertIn("attachments", email)
self.assertEqual(len(email["attachments"]), 3)
self.assertIn("from", email)
self.assertEqual(email["from"][0][1], "meteo@regione.vda.it")
self.assertIn("subject", email)
def test_from_file_obj(self):
with ported_open(mail_test_2) as fp:
mail = mailparser.parse_from_file_obj(fp)
trust = "smtp.customers.net"
self.assertFalse(mail.has_defects)
result = mail.mail
self.assertIsInstance(result, dict)
self.assertNotIn("defects", result)
self.assertNotIn("anomalies", result)
self.assertIn("has_defects", result)
result = mail.get_server_ipaddress(trust)
self.assertIsInstance(result, str)
result = mail.mail_json
self.assertIsInstance(result, str)
result = mail.headers
self.assertIsInstance(result, dict)
result = mail.headers_json
self.assertIsInstance(result, str)
result = mail.body
self.assertIsInstance(result, str)
result = mail.date
self.assertIsInstance(result, datetime.datetime)
result = mail.from_
self.assertIsInstance(result, list)
result = mail.to
self.assertIsInstance(result, list)
self.assertEqual(len(result), 2)
self.assertIsInstance(result[0], tuple)
self.assertEqual(len(result[0]), 2)
result = mail.subject
self.assertIsInstance(result, str)
result = mail.message_id
self.assertIsInstance(result, str)
result = mail.attachments
self.assertIsInstance(result, list)
result = mail.date
self.assertIsInstance(result, datetime.datetime)
result = mail.defects
self.assertIsInstance(result, list)
result = mail.timezone
self.assertEqual(result, "+1.0")
def test_get_to_domains(self):
m = mailparser.parse_from_file(mail_test_6)
domains_1 = get_to_domains(m.to, m.reply_to)
self.assertIsInstance(domains_1, list)
self.assertIn("test.it", domains_1)
domains_2 = m.to_domains
self.assertIsInstance(domains_2, list)
self.assertIn("test.it", domains_2)
self.assertEqual(domains_1, domains_2)
self.assertIsInstance(m.to_domains_json, str)
def test_convert_mail_date(self):
s = "Mon, 20 Mar 2017 05:12:54 +0600"
d, t = convert_mail_date(s)
self.assertEqual(t, "+6.0")
self.assertEqual(str(d), "2017-03-19 23:12:54+00:00")
s = "Mon, 20 Mar 2017 05:12:54 -0600"
d, t = convert_mail_date(s)
self.assertEqual(t, "-6.0")
s = "Mon, 11 Dec 2017 15:27:44 +0530"
d, t = convert_mail_date(s)
self.assertEqual(t, "+5.5")
def test_ported_string(self):
raw_data = ""
s = ported_string(raw_data)
self.assertEqual(s, str())
raw_data = "test"
s = ported_string(raw_data)
self.assertEqual(s, "test")
def test_parse_domain_with_tld_dot_id(self):
"""Support for .id tld (Indonesia)"""
received = """
from web.myhost.id
by smtp.domain.id (Proxmox) with ESMTPS id SOMEIDHERE
for <email@example.id>; Wed, 19 Feb 2025 15:00:00 +0700 (WIB)
""".strip()
expected = {
"from": "web.myhost.id",
"by": "smtp.domain.id (Proxmox)",
"with": "ESMTPS",
"id": "SOMEIDHERE",
"for": "<email@example.id>",
"date": "Wed, 19 Feb 2025 15:00:00 +0700 (WIB)",
}
values_by_clause = parse_received(received)
self.assertEqual(expected, values_by_clause)
def test_parse_domain_with_tld_dot_by(self):
"""Support for .by tld (Belarus)"""
received = """
from web.myhost.by
by smtp.domain.by (Proxmox) with ESMTPS id SOMEIDHERE
for <email@example.by>; Wed, 19 Feb 2025 15:00:00 +0700 (WIB)
""".strip()
expected = {
"from": "web.myhost.by",
"by": "smtp.domain.by (Proxmox)",
"with": "ESMTPS",
"id": "SOMEIDHERE",
"for": "<email@example.by>",
"date": "Wed, 19 Feb 2025 15:00:00 +0700 (WIB)",
}
values_by_clause = parse_received(received)
self.assertEqual(expected, values_by_clause)
def test_standard_outlook(self):
"""Verify a basic outlook received header works."""
received = """
from DM3NAM03FT035
by CY4PR0601CA0051.outlook.office365.com
with Microsoft SMTP Server version=TLS1_2, cipher=TLS
id 15.20.1185.23
via Frontend Transport; Mon, 1 Oct 2018 09:49:21 +0000
""".strip()
expected = {
"from": "DM3NAM03FT035",
"by": "CY4PR0601CA0051.outlook.office365.com",
"with": "Microsoft SMTP Server version=TLS1_2, cipher=TLS",
"id": "15.20.1185.23",
"via": "Frontend Transport",
"date": "Mon, 1 Oct 2018 09:49:21 +0000",
}
values_by_clause = parse_received(received)
self.assertEqual(expected, values_by_clause)
def test_standard_google__with_cipher(self):
"""Verify that we don't match 'with cipher' a la google."""
received = """
from mail_yw1_f65.google.com
by subdomain.domain.com Postfix with ESMTPS
id abc123 for <user@domain.com>;
Tue, 25 Sep 2018 13:09:36 +0000 (UTC)"""
expected = {
"from": "mail_yw1_f65.google.com",
"by": "subdomain.domain.com Postfix",
"with": "ESMTPS",
"id": "abc123",
"for": "<user@domain.com>",
"date": "Tue, 25 Sep 2018 13:09:36 +0000 (UTC)",
}
values_by_clause = parse_received(received)
self.assertEqual(expected, values_by_clause)
@unittest.skipIf(sys.version_info[0] < 3, "Must be using Python 3")
def test_parse_from_bytes(self):
with open(mail_test_2, "rb") as f:
mail_bytes = f.read()
mail = mailparser.parse_from_bytes(mail_bytes)
trust = "smtp.customers.net"
self.assertFalse(mail.has_defects)
raw = "217.76.210.112"
result = mail.get_server_ipaddress(trust)
self.assertEqual(raw, result)
raw = "<4516257BC5774408ADC1263EEBBBB73F@ad.regione.vda.it>"
result = mail.message_id
self.assertEqual(raw, result)
raw = "echo@tu-berlin.de"
result = mail.to
self.assertEqual(len(result), 2)
self.assertIsInstance(result, list)
self.assertIsInstance(result[0], tuple)
self.assertIsInstance(mail.to_json, str)
self.assertIsInstance(mail.to_raw, str)
self.assertEqual(raw, result[0][1])
raw = "meteo@regione.vda.it"
result = mail.from_
self.assertEqual(raw, result[0][1])
raw = "Bollettino Meteorologico del 29/11/2015"
result = mail.subject
self.assertEqual(raw, result)
result = mail.has_defects
self.assertFalse(result)
result = len(mail.attachments)
self.assertEqual(3, result)
self.assertIsInstance(mail.date_raw, str)
self.assertIsInstance(mail.date_json, str)
raw_utc = "2015-11-29T08:45:18+00:00"
result = mail.date.isoformat()
self.assertEqual(raw_utc, result)
def test_write_uuencode_attachment(self):
mail = mailparser.parse_from_file(mail_test_15)
temp_dir = tempfile.mkdtemp()
mail.write_attachments(temp_dir)
md5 = hashlib.md5()
with open(os.path.join(temp_dir, "REQUEST FOR QUOTE.zip"), "rb") as f:
md5.update(f.read())
shutil.rmtree(temp_dir)
self.assertEqual(md5.hexdigest(), "4f2cf891e7cfb349fca812091f184ecc")
def test_issue_139(self):
mail = mailparser.parse_from_file(mail_test_16)
assert mail.headers == {
"MIME-Version": "1.0",
"Precedence": "junk",
"Content-Type": "text/plain; charset=us-ascii",
"From": [("Sender", "sender@example.net")],
"Date": "Wed, 23 Jul 2003 23:30:00 +0200",
"Content-Transfer-Encoding": "7bit",
"Message-ID": "<GTUBE1.1010101@example.net>",
"Subject": "Test spam mail (GTUBE)",
"To": [("Recipient", "recipient@example.net")],
}
def test_issue_136(self):
mail = mailparser.parse_from_file(mail_test_17)
assert mail.from_ == [
("", "notificaccion-clientes@bbva.mx"),
]
def test_str_method_with_message(self):
"""Test __str__ method returns subject when message exists"""
mail = mailparser.parse_from_file(mail_test_1)
str_result = str(mail)
self.assertEqual(str_result, mail.subject)
def test_str_method_without_message(self):
"""Test __str__ method returns empty string when no message"""
# Create a MailParser with None message
parser = mailparser.MailParser.__new__(mailparser.MailParser)
parser._message = None
str_result = str(parser)
self.assertEqual(str_result, "")
def test_from_file_obj_seekable(self):
"""Test from_file_obj with seekable file object"""
import os
import tempfile
content = "From: test@example.com\nSubject: Test Seekable\n\nBody"
# Create a real file to test seekable behavior
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".eml") as f:
f.write(content)
fname = f.name
try:
with ported_open(fname) as fp:
mail = mailparser.parse_from_file_obj(fp)
self.assertEqual(mail.subject, "Test Seekable")
finally:
os.unlink(fname)
def test_from_file_obj_non_seekable(self):
"""Test from_file_obj with non-seekable file object (like stdin/TTY)"""
import io
content = "From: test@example.com\nSubject: Test Non-Seekable\n\nBody"
# Create a mock non-seekable file object that acts like text
class NonSeekableIO(io.StringIO):
def seek(self, *args):
raise OSError("File is not seekable")
fp = NonSeekableIO(content)
mail = mailparser.parse_from_file_obj(fp)
self.assertEqual(mail.subject, "Test Non-Seekable")
def test_get_server_ipaddress_invalid_ip(self):
"""Test get_server_ipaddress with invalid IP that raises ValueError"""
# Create mail with received header containing invalid IP
raw_mail = """Received: from invalid.example.com (999.999.999.999)
by mail.example.com
Subject: Test
From: test@example.com
Body"""
mail = mailparser.parse_from_string(raw_mail)
# Should return None for invalid IP
result = mail.get_server_ipaddress("trust")
# The IP validation should fail and return None
self.assertIsNone(result)
def test_get_server_ipaddress_private_ip(self):
"""Test get_server_ipaddress with private IP address"""
raw_mail = """Received: from internal.example.com (192.168.1.100)
by mail.example.com
Subject: Test
From: test@example.com
Body"""
mail = mailparser.parse_from_string(raw_mail)
# Private IP should return None
result = mail.get_server_ipaddress("trust")
self.assertIsNone(result)
def test_epilogue_parsing_typeerror(self):
"""Test epilogue parsing with TypeError"""
# Create mail with problematic epilogue that causes TypeError
# This is edge case where epilogue exists but can't be parsed
raw_mail = """Content-Type: multipart/mixed; boundary=boundary
--boundary
Content-Type: text/plain
Test
--boundary--
InvalidEpilogueData"""
mail = mailparser.parse_from_string(raw_mail)
# Should handle TypeError gracefully
self.assertIsNotNone(mail)
def test_epilogue_parsing_typeerror_coverage(self):
"""Test epilogue parsing TypeError exception handler coverage"""
import email
from unittest.mock import patch
# Create a mail with StartBoundaryNotFoundDefect to trigger epilogue parsing
raw_mail = """Content-Type: multipart/mixed; boundary="boundary123"
--boundary123
Content-Type: text/plain
Test content
--boundary123--
Extra epilogue content here"""
# Parse to get the message
msg = email.message_from_string(raw_mail)
# Mock email.message_from_string to raise TypeError
with patch("email.message_from_string") as mock_parse:
# First call is for initial parsing (let it pass)
# Second call is for epilogue parsing (raise TypeError)
mock_parse.side_effect = [msg, TypeError("Test TypeError")]
# This won't trigger the epilogue path without defects
# So we need to mock find_between to return something
with patch("mailparser.core.find_between") as mock_find:
mock_find.return_value = "epilogue content"
# Mock the message to have epilogue defects
with patch.object(
mailparser.MailParser,
"defects_categories",
{"StartBoundaryNotFoundDefect"},
):
mail = mailparser.parse_from_string(raw_mail)
# Should handle TypeError and continue
self.assertIsNotNone(mail)
def test_epilogue_parsing_general_exception_coverage(self):
"""Test epilogue parsing general Exception handler coverage"""
import email
from unittest.mock import patch
# Create a mail with boundary
raw_mail = """Content-Type: multipart/mixed; boundary="boundary123"
--boundary123
Content-Type: text/plain
Test content
--boundary123--
Extra epilogue content"""
# Parse to get the message
msg = email.message_from_string(raw_mail)
# Mock email.message_from_string to raise a general Exception
with patch("email.message_from_string") as mock_parse:
mock_parse.side_effect = [msg, Exception("General error")]
with patch("mailparser.core.find_between") as mock_find:
mock_find.return_value = "epilogue content"
# Mock defects_categories to trigger epilogue parsing
with patch.object(
mailparser.MailParser,
"defects_categories",
{"StartBoundaryNotFoundDefect"},
):
mail = mailparser.parse_from_string(raw_mail)
# Should handle Exception and log error
self.assertIsNotNone(mail)
def test_attachment_with_content_id_no_subtype(self):
"""Test attachment handling with content-id but no html/plain subtype"""
raw_mail = """Content-Type: multipart/mixed; boundary=boundary
--boundary
Content-Type: image/png
Content-ID: <image001>
ImageData
--boundary--"""
mail = mailparser.parse_from_string(raw_mail)
self.assertGreater(len(mail.attachments), 0)
def test_attachment_rtf_type(self):
"""Test attachment handling for RTF content subtype"""
raw_mail = """Content-Type: multipart/mixed; boundary=boundary
--boundary
Content-Type: application/rtf
RTFData
--boundary--"""
mail = mailparser.parse_from_string(raw_mail)
attachments = mail.attachments
self.assertGreater(len(attachments), 0)
# Should have generated RTF filename
self.assertTrue(any(".rtf" in att.get("filename", "") for att in attachments))
def test_attachment_disposition_without_filename(self):
"""Test attachment with content-disposition but no filename"""
raw_mail = """Content-Type: multipart/mixed; boundary=boundary
--boundary
Content-Type: text/plain
Content-Disposition: attachment
PlainTextData
--boundary--"""
mail = mailparser.parse_from_string(raw_mail)
attachments = mail.attachments
self.assertGreater(len(attachments), 0)
# Should have generated .txt filename
self.assertTrue(any(".txt" in att.get("filename", "") for att in attachments))
def test_text_plain_7bit_encoding(self):
"""Test text/plain body part with 7bit encoding"""
raw_mail = """Content-Type: text/plain
Content-Transfer-Encoding: 7bit
This is plain text with 7bit encoding."""
mail = mailparser.parse_from_string(raw_mail)
self.assertIn("This is plain text", mail.body)
def test_text_plain_8bit_encoding(self):
"""Test text/plain body part with 8bit encoding"""
raw_mail = """Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
This is plain text with 8bit encoding."""
mail = mailparser.parse_from_string(raw_mail)
self.assertIn("This is plain text", mail.body)
def test_comma_in_name(self):
"""
Tests the fixes for both the 'comma-in-encoded-name' issue and the
'encoded-name-equals-email' issue (from test_issue_136).
"""
mail = mailparser.parse_from_file(mail_test_18)
assert mail.from_ == [("LastßlName, FirstName", "comma.name@example.com")]
assert mail.to == [("", "tony.stark@example.com")]
assert mail.cc == [
("", "simple@example.net"),
('John "Johnny" Doe', "john.doe@example.com"),
]