github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/third_party/stdlib/test/test_rfc822.py (about) 1 import unittest 2 from test import test_support 3 4 #rfc822 = test_support.import_module("rfc822", deprecated=True) 5 import rfc822 6 7 try: 8 from cStringIO import StringIO 9 except ImportError: 10 from StringIO import StringIO 11 12 13 class MessageTestCase(unittest.TestCase): 14 def create_message(self, msg): 15 return rfc822.Message(StringIO(msg)) 16 17 def test_get(self): 18 msg = self.create_message( 19 'To: "last, first" <userid@foo.net>\n\ntest\n') 20 self.assertTrue(msg.get("to") == '"last, first" <userid@foo.net>') 21 self.assertTrue(msg.get("TO") == '"last, first" <userid@foo.net>') 22 self.assertTrue(msg.get("No-Such-Header") is None) 23 self.assertTrue(msg.get("No-Such-Header", "No-Such-Value") 24 == "No-Such-Value") 25 26 def test_setdefault(self): 27 msg = self.create_message( 28 'To: "last, first" <userid@foo.net>\n\ntest\n') 29 self.assertTrue(not msg.has_key("New-Header")) 30 self.assertTrue(msg.setdefault("New-Header", "New-Value") == "New-Value") 31 self.assertTrue(msg.setdefault("New-Header", "Different-Value") 32 == "New-Value") 33 self.assertTrue(msg["new-header"] == "New-Value") 34 35 self.assertTrue(msg.setdefault("Another-Header") == "") 36 self.assertTrue(msg["another-header"] == "") 37 38 def check(self, msg, results): 39 """Check addresses and the date.""" 40 m = self.create_message(msg) 41 i = 0 42 for n, a in m.getaddrlist('to') + m.getaddrlist('cc'): 43 try: 44 mn, ma = results[i][0], results[i][1] 45 except IndexError: 46 print 'extra parsed address:', repr(n), repr(a) 47 continue 48 i = i + 1 49 self.assertEqual(mn, n, 50 "Un-expected name: %r != %r" % (mn, n)) 51 self.assertEqual(ma, a, 52 "Un-expected address: %r != %r" % (ma, a)) 53 if mn == n and ma == a: 54 pass 55 else: 56 print 'not found:', repr(n), repr(a) 57 58 out = m.getdate('date') 59 if out: 60 self.assertEqual(out, 61 (1999, 1, 13, 23, 57, 35, 0, 1, 0), 62 "date conversion failed") 63 64 65 # Note: all test cases must have the same date (in various formats), 66 # or no date! 67 68 def test_basic(self): 69 self.check( 70 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' 71 'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n' 72 'To: "Guido van\n' 73 '\t : Rossum" <guido@python.org>\n' 74 'Subject: test2\n' 75 '\n' 76 'test2\n', 77 [('Guido van\n\t : Rossum', 'guido@python.org')]) 78 79 self.check( 80 'From: Barry <bwarsaw@python.org\n' 81 'To: guido@python.org (Guido: the Barbarian)\n' 82 'Subject: nonsense\n' 83 'Date: Wednesday, January 13 1999 23:57:35 -0500\n' 84 '\n' 85 'test', 86 [('Guido: the Barbarian', 'guido@python.org')]) 87 88 self.check( 89 'From: Barry <bwarsaw@python.org\n' 90 'To: guido@python.org (Guido: the Barbarian)\n' 91 'Cc: "Guido: the Madman" <guido@python.org>\n' 92 'Date: 13-Jan-1999 23:57:35 EST\n' 93 '\n' 94 'test', 95 [('Guido: the Barbarian', 'guido@python.org'), 96 ('Guido: the Madman', 'guido@python.org') 97 ]) 98 99 self.check( 100 'To: "The monster with\n' 101 ' the very long name: Guido" <guido@python.org>\n' 102 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' 103 '\n' 104 'test', 105 [('The monster with\n the very long name: Guido', 106 'guido@python.org')]) 107 108 self.check( 109 'To: "Amit J. Patel" <amitp@Theory.Stanford.EDU>\n' 110 'CC: Mike Fletcher <mfletch@vrtelecom.com>,\n' 111 ' "\'string-sig@python.org\'" <string-sig@python.org>\n' 112 'Cc: fooz@bat.com, bart@toof.com\n' 113 'Cc: goit@lip.com\n' 114 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' 115 '\n' 116 'test', 117 [('Amit J. Patel', 'amitp@Theory.Stanford.EDU'), 118 ('Mike Fletcher', 'mfletch@vrtelecom.com'), 119 ("'string-sig@python.org'", 'string-sig@python.org'), 120 ('', 'fooz@bat.com'), 121 ('', 'bart@toof.com'), 122 ('', 'goit@lip.com'), 123 ]) 124 125 self.check( 126 'To: Some One <someone@dom.ain>\n' 127 'From: Anudder Persin <subuddy.else@dom.ain>\n' 128 'Date:\n' 129 '\n' 130 'test', 131 [('Some One', 'someone@dom.ain')]) 132 133 self.check( 134 'To: person@dom.ain (User J. Person)\n\n', 135 [('User J. Person', 'person@dom.ain')]) 136 137 def test_doublecomment(self): 138 # The RFC allows comments within comments in an email addr 139 self.check( 140 'To: person@dom.ain ((User J. Person)), John Doe <foo@bar.com>\n\n', 141 [('User J. Person', 'person@dom.ain'), ('John Doe', 'foo@bar.com')]) 142 143 def test_twisted(self): 144 # This one is just twisted. I don't know what the proper 145 # result should be, but it shouldn't be to infloop, which is 146 # what used to happen! 147 self.check( 148 'To: <[smtp:dd47@mail.xxx.edu]_at_hmhq@hdq-mdm1-imgout.companay.com>\n' 149 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' 150 '\n' 151 'test', 152 [('', ''), 153 ('', 'dd47@mail.xxx.edu'), 154 ('', '_at_hmhq@hdq-mdm1-imgout.companay.com'), 155 ]) 156 157 def test_commas_in_full_name(self): 158 # This exercises the old commas-in-a-full-name bug, which 159 # should be doing the right thing in recent versions of the 160 # module. 161 self.check( 162 'To: "last, first" <userid@foo.net>\n' 163 '\n' 164 'test', 165 [('last, first', 'userid@foo.net')]) 166 167 def test_quoted_name(self): 168 self.check( 169 'To: (Comment stuff) "Quoted name"@somewhere.com\n' 170 '\n' 171 'test', 172 [('Comment stuff', '"Quoted name"@somewhere.com')]) 173 174 def test_bogus_to_header(self): 175 self.check( 176 'To: :\n' 177 'Cc: goit@lip.com\n' 178 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' 179 '\n' 180 'test', 181 [('', 'goit@lip.com')]) 182 183 def test_addr_ipquad(self): 184 self.check( 185 'To: guido@[132.151.1.21]\n' 186 '\n' 187 'foo', 188 [('', 'guido@[132.151.1.21]')]) 189 190 def test_iter(self): 191 m = rfc822.Message(StringIO( 192 'Date: Wed, 13 Jan 1999 23:57:35 -0500\n' 193 'From: Guido van Rossum <guido@CNRI.Reston.VA.US>\n' 194 'To: "Guido van\n' 195 '\t : Rossum" <guido@python.org>\n' 196 'Subject: test2\n' 197 '\n' 198 'test2\n' )) 199 self.assertEqual(sorted(m), ['date', 'from', 'subject', 'to']) 200 201 def test_rfc2822_phrases(self): 202 # RFC 2822 (the update to RFC 822) specifies that dots in phrases are 203 # obsolete syntax, which conforming programs MUST recognize but NEVER 204 # generate (see $4.1 Miscellaneous obsolete tokens). This is a 205 # departure from RFC 822 which did not allow dots in non-quoted 206 # phrases. 207 self.check('To: User J. Person <person@dom.ain>\n\n', 208 [('User J. Person', 'person@dom.ain')]) 209 210 # This takes too long to add to the test suite 211 ## def test_an_excrutiatingly_long_address_field(self): 212 ## OBSCENELY_LONG_HEADER_MULTIPLIER = 10000 213 ## oneaddr = ('Person' * 10) + '@' + ('.'.join(['dom']*10)) + '.com' 214 ## addr = ', '.join([oneaddr] * OBSCENELY_LONG_HEADER_MULTIPLIER) 215 ## lst = rfc822.AddrlistClass(addr).getaddrlist() 216 ## self.assertEqual(len(lst), OBSCENELY_LONG_HEADER_MULTIPLIER) 217 218 def test_2getaddrlist(self): 219 eq = self.assertEqual 220 msg = self.create_message("""\ 221 To: aperson@dom.ain 222 Cc: bperson@dom.ain 223 Cc: cperson@dom.ain 224 Cc: dperson@dom.ain 225 226 A test message. 227 """) 228 ccs = [('', a) for a in 229 ['bperson@dom.ain', 'cperson@dom.ain', 'dperson@dom.ain']] 230 addrs = msg.getaddrlist('cc') 231 addrs.sort() 232 eq(addrs, ccs) 233 # Try again, this one used to fail 234 addrs = msg.getaddrlist('cc') 235 addrs.sort() 236 eq(addrs, ccs) 237 238 def test_parseaddr(self): 239 eq = self.assertEqual 240 eq(rfc822.parseaddr('<>'), ('', '')) 241 eq(rfc822.parseaddr('aperson@dom.ain'), ('', 'aperson@dom.ain')) 242 eq(rfc822.parseaddr('bperson@dom.ain (Bea A. Person)'), 243 ('Bea A. Person', 'bperson@dom.ain')) 244 eq(rfc822.parseaddr('Cynthia Person <cperson@dom.ain>'), 245 ('Cynthia Person', 'cperson@dom.ain')) 246 247 def test_quote_unquote(self): 248 eq = self.assertEqual 249 eq(rfc822.quote('foo\\wacky"name'), 'foo\\\\wacky\\"name') 250 eq(rfc822.unquote('"foo\\\\wacky\\"name"'), 'foo\\wacky"name') 251 252 def test_invalid_headers(self): 253 eq = self.assertEqual 254 msg = self.create_message("First: val\n: otherval\nSecond: val2\n") 255 eq(msg.getheader('First'), 'val') 256 eq(msg.getheader('Second'), 'val2') 257 258 259 def test_main(): 260 test_support.run_unittest(MessageTestCase) 261 262 263 if __name__ == "__main__": 264 test_main()