9 # override File.exists? to make it work with StringIO for testing.
10 # FIXME: do aliasing to avoid breaking this when sup moves from
11 # File.exists? to File.exist?
16 # puts "fake File::exists?"
18 if file.is_a?(StringIO)
21 # use the different function
29 class TestMessage < Test::Unit::TestCase
37 def test_simple_message
40 Return-path: <fake_sender@example.invalid>
41 Envelope-to: fake_receiver@localhost
42 Delivery-date: Sun, 09 Dec 2007 21:48:19 +0200
43 Received: from fake_sender by localhost.localdomain with local (Exim 4.67)
44 (envelope-from <fake_sender@example.invalid>)
46 for fake_receiver@localhost; Sun, 09 Dec 2007 21:48:19 +0200
47 Date: Sun, 9 Dec 2007 21:48:19 +0200
48 Mailing-List: contact example-help@example.invalid; run by ezmlm
50 List-Id: <example.list-id.example.invalid>
51 List-Post: <mailto:example@example.invalid>
52 List-Help: <mailto:example-help@example.invalid>
53 List-Unsubscribe: <mailto:example-unsubscribe@example.invalid>
54 List-Subscribe: <mailto:example-subscribe@example.invalid>
55 Delivered-To: mailing list example@example.invalid
56 Delivered-To: moderator for example@example.invalid
57 From: Fake Sender <fake_sender@example.invalid>
58 To: Fake Receiver <fake_receiver@localhost>
59 Subject: Re: Test message subject
60 Message-ID: <20071209194819.GA25972@example.invalid>
61 References: <E1J1Rvb-0006k2-CE@localhost.localdomain>
63 Content-Type: text/plain; charset=us-ascii
64 Content-Disposition: inline
65 In-Reply-To: <E1J1Rvb-0006k2-CE@localhost.localdomain>
71 source = DummySource.new("sup-test://test_simple_message")
72 source.messages = [ message ]
75 sup_message = Message.new( {:source => source, :source_info => source_info } )
76 sup_message.load_from_source!
78 # see how well parsing the header went
81 # "to" is an Array containing person items
83 # there should be only one item
84 assert_equal(1, to.length)
86 # sup doesn't do capitalized letters in email addresses
87 assert_equal("fake_receiver@localhost", to[0].email)
88 assert_equal("Fake Receiver", to[0].name)
90 from = sup_message.from
91 # "from" is just a simple person item
93 assert_equal("fake_sender@example.invalid", from.email)
94 assert_equal("Fake Sender", from.name)
96 subj = sup_message.subj
97 assert_equal("Re: Test message subject", subj)
99 list_subscribe = sup_message.list_subscribe
100 assert_equal("<mailto:example-subscribe@example.invalid>", list_subscribe)
102 list_unsubscribe = sup_message.list_unsubscribe
103 assert_equal("<mailto:example-unsubscribe@example.invalid>", list_unsubscribe)
105 list_address = sup_message.list_address
106 assert_equal("example@example.invalid", list_address.email)
107 assert_equal("example", list_address.name)
109 date = sup_message.date
110 assert_equal(Time.parse("Sun, 9 Dec 2007 21:48:19 +0200"), date)
113 assert_equal("20071209194819.GA25972@example.invalid", id)
115 refs = sup_message.refs
116 assert_equal(1, refs.length)
117 assert_equal("E1J1Rvb-0006k2-CE@localhost.localdomain", refs[0])
119 replytos = sup_message.replytos
120 assert_equal(1, replytos.length)
121 assert_equal("E1J1Rvb-0006k2-CE@localhost.localdomain", replytos[0])
125 assert_equal(0, cc.length)
127 bcc = sup_message.bcc
129 assert_equal(0, bcc.length)
131 recipient_email = sup_message.recipient_email
132 assert_equal("fake_receiver@localhost", recipient_email)
134 message_source = sup_message.source
135 assert_equal(message_source, source)
137 message_source_info = sup_message.source_info
138 assert_equal(message_source_info, source_info)
140 # read the message body chunks
142 chunks = sup_message.load_from_source!
144 # there should be only one chunk
145 assert_equal(1, chunks.length)
147 lines = chunks[0].lines
149 # there should be only one line
150 assert_equal(1, lines.length)
152 assert_equal("Test message!", lines[0])
156 def test_multipart_message
159 From fake_receiver@localhost Sun Dec 09 22:33:37 +0200 2007
160 Subject: Re: Test message subject
161 From: Fake Receiver <fake_receiver@localhost>
162 To: Fake Sender <fake_sender@example.invalid>
163 References: <E1J1Rvb-0006k2-CE@localhost.localdomain> <20071209194819.GA25972example.invalid>
164 In-Reply-To: <20071209194819.GA25972example.invalid>
165 Date: Sun, 09 Dec 2007 22:33:37 +0200
166 Message-Id: <1197232243-sup-2663example.invalid>
168 Content-Type: multipart/mixed; boundary="=-1197232418-506707-26079-6122-2-="
172 --=-1197232418-506707-26079-6122-2-=
173 Content-Type: text/plain; charset=utf-8
174 Content-Disposition: inline
176 Excerpts from Fake Sender's message of Sun Dec 09 21:48:19 +0200 2007:
179 Thanks for the message!
180 --=-1197232418-506707-26079-6122-2-=
181 Content-Disposition: attachment; filename="HACKING"
182 Content-Type: application/octet-stream; name="HACKING"
183 Content-Transfer-Encoding: base64
185 UnVubmluZyBTdXAgbG9jYWxseQotLS0tLS0tLS0tLS0tLS0tLS0tCkludm9r
186 ZSBpdCBsaWtlIHRoaXM6CgpydWJ5IC1JIGxpYiAtdyBiaW4vc3VwCgpZb3Un
187 bGwgaGF2ZSB0byBpbnN0YWxsIGFsbCBnZW1zIG1lbnRpb25lZCBpbiB0aGUg
188 UmFrZWZpbGUgKGxvb2sgZm9yIHRoZSBsaW5lCnNldHRpbmcgcC5leHRyYV9k
189 ZXBzKS4gSWYgeW91J3JlIG9uIGEgRGViaWFuIG9yIERlYmlhbi1iYXNlZCBz
190 eXN0ZW0gKGUuZy4KVWJ1bnR1KSwgeW91J2xsIGhhdmUgdG8gbWFrZSBzdXJl
191 IHlvdSBoYXZlIGEgY29tcGxldGUgUnVieSBpbnN0YWxsYXRpb24sCmVzcGVj
192 aWFsbHkgbGlic3NsLXJ1YnkuCgpDb2Rpbmcgc3RhbmRhcmRzCi0tLS0tLS0t
193 LS0tLS0tLS0KCi0gRG9uJ3Qgd3JhcCBjb2RlIHVubGVzcyBpdCByZWFsbHkg
194 YmVuZWZpdHMgZnJvbSBpdC4gVGhlIGRheXMgb2YKICA4MC1jb2x1bW4gZGlz
195 cGxheXMgYXJlIGxvbmcgb3Zlci4gQnV0IGRvIHdyYXAgY29tbWVudHMgYW5k
196 IG90aGVyCiAgdGV4dCBhdCB3aGF0ZXZlciBFbWFjcyBtZXRhLVEgZG9lcy4K
197 LSBJIGxpa2UgcG9ldHJ5IG1vZGUuCi0gVXNlIHt9IGZvciBvbmUtbGluZXIg
198 YmxvY2tzIGFuZCBkby9lbmQgZm9yIG11bHRpLWxpbmUgYmxvY2tzLgoK
200 --=-1197232418-506707-26079-6122-2-=
201 Content-Disposition: attachment; filename="Manifest.txt"
202 Content-Type: text/plain; name="Manifest.txt"
203 Content-Transfer-Encoding: quoted-printable
215 bin/sup-recover-sources
219 --=-1197232418-506707-26079-6122-2-=--
221 source = DummySource.new("sup-test://test_multipart_message")
222 source.messages = [ message ]
225 sup_message = Message.new( {:source => source, :source_info => source_info } )
226 sup_message.load_from_source!
228 # read the message body chunks
230 chunks = sup_message.load_from_source!
232 # this time there should be four chunks: first the quoted part of
233 # the message, then the non-quoted part, then the two attachments
234 assert_equal(4, chunks.length)
236 assert_equal(chunks[0].class, Redwood::Chunk::Quote)
237 assert_equal(chunks[1].class, Redwood::Chunk::Text)
238 assert_equal(chunks[2].class, Redwood::Chunk::Attachment)
239 assert_equal(chunks[3].class, Redwood::Chunk::Attachment)
241 # further testing of chunks will happen in test_message_chunks.rb
242 # (possibly not yet implemented)
246 def test_broken_message_1
248 # an example of a broken message, missing "to" and "from" fields
251 Return-path: <fake_sender@example.invalid>
252 Envelope-to: fake_receiver@localhost
253 Delivery-date: Sun, 09 Dec 2007 21:48:19 +0200
254 Received: from fake_sender by localhost.localdomain with local (Exim 4.67)
255 (envelope-from <fake_sender@example.invalid>)
257 for fake_receiver@localhost; Sun, 09 Dec 2007 21:48:19 +0200
258 Date: Sun, 9 Dec 2007 21:48:19 +0200
259 Subject: Re: Test message subject
260 Message-ID: <20071209194819.GA25972@example.invalid>
261 References: <E1J1Rvb-0006k2-CE@localhost.localdomain>
263 Content-Type: text/plain; charset=us-ascii
264 Content-Disposition: inline
265 In-Reply-To: <E1J1Rvb-0006k2-CE@localhost.localdomain>
271 source = DummySource.new("sup-test://test_broken_message_1")
272 source.messages = [ message ]
275 sup_message = Message.new( {:source => source, :source_info => source_info } )
276 sup_message.load_from_source!
280 # there should no items, since the message doesn't have any
281 # recipients -- still not nil
282 assert_equal(0, to.length)
284 # from will have bogus values
285 from = sup_message.from
286 # very basic email address check
287 assert_match(/\w+@\w+\.\w{2,4}/, from.email)
288 assert_not_nil(from.name)
292 def test_broken_message_2
294 # an example of a broken message, no body at all
297 Return-path: <fake_sender@example.invalid>
298 From: Fake Sender <fake_sender@example.invalid>
299 To: Fake Receiver <fake_receiver@localhost>
300 Envelope-to: fake_receiver@localhost
301 Delivery-date: Sun, 09 Dec 2007 21:48:19 +0200
302 Received: from fake_sender by localhost.localdomain with local (Exim 4.67)
303 (envelope-from <fake_sender@example.invalid>)
305 for fake_receiver@localhost; Sun, 09 Dec 2007 21:48:19 +0200
306 Date: Sun, 9 Dec 2007 21:48:19 +0200
307 Subject: Re: Test message subject
308 Message-ID: <20071209194819.GA25972@example.invalid>
309 References: <E1J1Rvb-0006k2-CE@localhost.localdomain>
311 Content-Type: text/plain; charset=us-ascii
312 Content-Disposition: inline
313 In-Reply-To: <E1J1Rvb-0006k2-CE@localhost.localdomain>
317 source = DummySource.new("sup-test://test_broken_message_1")
318 source.messages = [ message ]
321 sup_message = Message.new( {:source => source, :source_info => source_info } )
322 sup_message.load_from_source!
324 # read the message body chunks: no errors should reach this level
328 assert_nothing_raised() do
329 chunks = sup_message.load_from_source!
332 # the chunks list should be empty
334 assert_equal(0, chunks.length)
338 def test_multipart_message_2
341 Return-path: <vim-mac-return-3938-fake_receiver=localhost@vim.org>
342 Envelope-to: fake_receiver@localhost
343 Delivery-date: Wed, 14 Jun 2006 19:22:54 +0300
344 Received: from localhost ([127.0.0.1] helo=localhost.localdomain)
345 by localhost.localdomain with esmtp (Exim 4.60)
346 (envelope-from <vim-mac-return-3938-fake_receiver=localhost@vim.org>)
348 for fake_receiver@localhost; Wed, 14 Jun 2006 18:57:15 +0300
349 Received: from pop.gmail.com
350 by localhost.localdomain with POP3 (fetchmail-6.3.2)
351 for <fake_receiver@localhost> (single-drop); Wed, 14 Jun 2006 18:57:15 +0300 (EEST)
352 X-Gmail-Received: 8ee0fe5f895736974c042c8eaf176014b1ba7b88
353 Delivered-To: fake_receiver@localhost
354 Received: by 10.49.8.16 with SMTP id l16cs11327nfi;
355 Sun, 26 Mar 2006 19:31:56 -0800 (PST)
356 Received: by 10.66.224.8 with SMTP id w8mr2172862ugg;
357 Sun, 26 Mar 2006 19:31:56 -0800 (PST)
358 Received: from foobar.math.fu-berlin.de (foobar.math.fu-berlin.de [160.45.45.151])
359 by mx.gmail.com with SMTP id j3si553645ugd.2006.03.26.19.31.56;
360 Sun, 26 Mar 2006 19:31:56 -0800 (PST)
361 Received-SPF: neutral (gmail.com: 160.45.45.151 is neither permitted nor denied by best guess record for domain of vim-mac-return-3938-fake_receiver=localhost@vim.org)
362 Message-Id: <44275cac.74a494f1.315a.ffff825cSMTPIN_ADDED@mx.gmail.com>
363 Received: (qmail 24265 invoked by uid 200); 27 Mar 2006 02:32:39 -0000
364 Mailing-List: contact vim-mac-help@vim.org; run by ezmlm
366 Delivered-To: mailing list vim-mac@vim.org
367 Received: (qmail 7913 invoked from network); 26 Mar 2006 23:37:34 -0000
368 Received: from cpe-138-217-96-243.vic.bigpond.net.au (HELO vim.org) (138.217.96.243)
369 by foobar.math.fu-berlin.de with SMTP; 26 Mar 2006 23:37:34 -0000
370 From: fake_sender@example.invalid
372 Subject: Mail Delivery (failure vim-mac@vim.org)
373 Date: Mon, 27 Mar 2006 10:29:39 +1000
375 Content-Type: multipart/related;
376 type="multipart/alternative";
377 boundary="----=_NextPart_000_001B_01C0CA80.6B015D10"
379 X-MSMail-Priority: Normal
381 ------=_NextPart_000_001B_01C0CA80.6B015D10
382 Content-Type: multipart/alternative;
383 boundary="----=_NextPart_001_001C_01C0CA80.6B015D10"
385 ------=_NextPart_001_001C_01C0CA80.6B015D10
386 Content-Type: text/plain;
388 Content-Transfer-Encoding: quoted-printable
390 ------=_NextPart_001_001C_01C0CA80.6B015D10
391 Content-Type: text/html;
393 Content-Transfer-Encoding: quoted-printable
395 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
397 <META content=3D"text/html; charset=3Diso-8859-1" =
398 http-equiv=3DContent-Type>
399 <META content=3D"MSHTML 5.00.2920.0" name=3DGENERATOR>
402 <BODY bgColor=3D#ffffff>If the message will not displayed automatically,<br>
403 follow the link to read the delivered message.<br><br>
404 Received message is available at:<br>
405 <a href=3Dcid:031401Mfdab4$3f3dL780$73387018@57W81fa70Re height=3D0 width=3D0>www.vim.org/inbox/vim-mac/read.php?sessionid-18559</a>
407 src=3Dcid:031401Mfdab4$3f3dL780$73387018@57W81fa70Re height=3D0 width=3D0></iframe>
408 <DIV> </DIV></BODY></HTML>
410 ------=_NextPart_001_001C_01C0CA80.6B015D10--
412 ------=_NextPart_000_001B_01C0CA80.6B015D10--
416 source = DummySource.new("sup-test://test_multipart_message_2")
417 source.messages = [ message ]
420 sup_message = Message.new( {:source => source, :source_info => source_info } )
421 sup_message.load_from_source!
423 # read the message body chunks
425 assert_nothing_raised() do
426 chunks = sup_message.load_from_source!
431 def test_blank_header_lines
434 Return-Path: <monitor-list-bounces@widget.com>
435 X-Original-To: nobody@localhost
436 Delivered-To: nobody@localhost.eng.widget.com
437 Received: from localhost (localhost.localdomain [127.0.0.1])
438 by soquel.eng.widget.com (Postfix) with ESMTP id 609BC13C0DB1
439 for <nobody@localhost>; Thu, 19 Mar 2009 13:43:21 -0700 (PDT)
441 Received: from pa-excas-vip.widget.com [10.16.67.200]
442 by localhost with IMAP (fetchmail-6.2.5)
443 for nobody@localhost (single-drop); Thu, 19 Mar 2009 13:43:21 -0700 (PDT)
444 Received: from pa-exht01.widget.com (10.113.81.167) by pa-excaht11.widget.com
445 (10.113.81.197) with Microsoft SMTP Server (TLS) id 8.1.311.2; Thu, 19 Mar
447 Received: from mailman2.widget.com (10.16.64.159) by pa-exht01.widget.com
448 (10.113.81.167) with Microsoft SMTP Server id 8.1.336.0; Thu, 19 Mar 2009
450 Received: by mailman2.widget.com (Postfix) id 47095AE30856; Thu, 19 Mar 2009
452 Received: from countchocula.widget.com (localhost.localdomain [127.0.0.1]) by
453 mailman2.widget.com (Postfix) with ESMTP id 5F782ABC5948; Thu, 19 Mar 2009
455 Received: from mailhost4.widget.com (mailhost4.widget.com [10.16.67.124]) by
456 mailman2.widget.com (Postfix) with ESMTP id 6CDCCABC5948 for
457 <monitor-list@mailman2.widget.com>; Thu, 19 Mar 2009 13:42:26 -0700 (PDT)
458 Received: by mailhost4.widget.com (Postfix) id 2364AC9AC4; Thu, 19 Mar 2009
460 Received: from pa-exht01.widget.com (pa-exht01.widget.com [10.113.81.167]) by
461 mailhost4.widget.com (Postfix) with ESMTP id 17A68C9AC3 for
462 <monitor-list@widget.com>; Thu, 19 Mar 2009 13:42:26 -0700 (PDT)
463 Received: from PA-EXMBX04.widget.com ([10.113.81.142]) by pa-exht01.widget.com
464 ([10.113.81.167]) with mapi; Thu, 19 Mar 2009 13:42:26 -0700
465 From: Some User <someuser@widget.com>
466 To: "monitor-list@widget.com" <monitor-list@widget.com>
467 Sender: "monitor-list-bounces@widget.com" <monitor-list-bounces@widget.com>
468 Date: Thu, 19 Mar 2009 13:42:25 -0700
469 Subject: Looking for a mac
470 Thread-Topic: Looking for a mac
471 Thread-Index: AQHJqNM1xIqqjNRWuUCUBaxzPFK5eQ==
473 <D3C12B2AD838B44DA9D6B2CA334246D011E72A73A4@PA-EXMBX04.widget.com>
474 List-Help: <mailto:monitor-list-request@widget.com?subject=help>
475 List-Subscribe: <http://mailman2.widget.com/mailman/listinfo/monitor-list>,
476 <mailto:monitor-list-request@widget.com?subject=subscribe>
478 <http://mailman2.widget.com/mailman/listinfo/monitor-list>,
479 <mailto:monitor-list-request@widget.com?subject=unsubscribe>
480 Accept-Language: en-US
481 Content-Language: en-US
482 X-MS-Exchange-Organization-AuthAs: Anonymous
483 X-MS-Exchange-Organization-AuthSource: pa-exht01.widget.com
485 X-Auto-Response-Suppress: All
486 X-MS-TNEF-Correlator:
487 acceptlanguage: en-US
488 delivered-to: monitor-list@widget.com
489 errors-to: monitor-list-bounces@widget.com
490 list-id: engineering monitor related <monitor-list.widget.com>
491 x-mailman-version: 2.1.8
492 x-beenthere: monitor-list@widget.com
493 x-original-to: monitor-list@mailman2.widget.com
494 list-post: <mailto:monitor-list@widget.com>
495 list-archive: <http://mailman2.widget.com/pipermail/monitor-list>
496 Content-Type: text/plain; charset="us-ascii"
497 Content-Transfer-Encoding: quoted-printable
501 Just wondering if anybody can lend me a mac to reproduce PR 384931 ?
507 source = DummySource.new("sup-test://test_blank_header_lines")
508 source.messages = [ message ]
511 sup_message = Message.new( {:source => source, :source_info => source_info } )
512 sup_message.load_from_source!
514 # See how well parsing the message ID went.
516 assert_equal("D3C12B2AD838B44DA9D6B2CA334246D011E72A73A4@PA-EXMBX04.widget.com", id)
518 # Look at another header field whose first line was blank.
519 list_unsubscribe = sup_message.list_unsubscribe
520 assert_equal("<http://mailman2.widget.com/mailman/listinfo/monitor-list>, " +
521 "<mailto:monitor-list-request@widget.com?subject=unsubscribe>",
526 # TODO: test different error cases, malformed messages etc.
528 # TODO: test different quoting styles, see that they are all divided
535 # vim:noai:ts=2:sw=2: