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