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
32 person_file = StringIO.new("")
34 if not PersonManager.instantiated?
35 @person_manager = PersonManager.new(person_file)
42 def test_simple_message
45 Return-path: <fake_sender@example.invalid>
46 Envelope-to: fake_receiver@localhost
47 Delivery-date: Sun, 09 Dec 2007 21:48:19 +0200
48 Received: from fake_sender by localhost.localdomain with local (Exim 4.67)
49 (envelope-from <fake_sender@example.invalid>)
51 for fake_receiver@localhost; Sun, 09 Dec 2007 21:48:19 +0200
52 Date: Sun, 9 Dec 2007 21:48:19 +0200
53 Mailing-List: contact example-help@example.invalid; run by ezmlm
55 List-Id: <example.list-id.example.invalid>
56 List-Post: <mailto:example@example.invalid>
57 List-Help: <mailto:example-help@example.invalid>
58 List-Unsubscribe: <mailto:example-unsubscribe@example.invalid>
59 List-Subscribe: <mailto:example-subscribe@example.invalid>
60 Delivered-To: mailing list example@example.invalid
61 Delivered-To: moderator for example@example.invalid
62 From: Fake Sender <fake_sender@example.invalid>
63 To: Fake Receiver <fake_receiver@localhost>
64 Subject: Re: Test message subject
65 Message-ID: <20071209194819.GA25972@example.invalid>
66 References: <E1J1Rvb-0006k2-CE@localhost.localdomain>
68 Content-Type: text/plain; charset=us-ascii
69 Content-Disposition: inline
70 In-Reply-To: <E1J1Rvb-0006k2-CE@localhost.localdomain>
76 source = DummySource.new("sup-test://test_simple_message")
77 source.messages = [ message ]
80 sup_message = Message.new( {:source => source, :source_info => source_info } )
82 # see how well parsing the header went
85 # "to" is an Array containing person items
87 # there should be only one item
88 assert_equal(1, to.length)
90 # sup doesn't do capitalized letters in email addresses
91 assert_equal("fake_receiver@localhost", to[0].email)
92 assert_equal("Fake Receiver", to[0].name)
94 from = sup_message.from
95 # "from" is just a simple person item
97 assert_equal("fake_sender@example.invalid", from.email)
98 assert_equal("Fake Sender", from.name)
100 subj = sup_message.subj
101 assert_equal("Re: Test message subject", subj)
103 list_subscribe = sup_message.list_subscribe
104 assert_equal("<mailto:example-subscribe@example.invalid>", list_subscribe)
106 list_unsubscribe = sup_message.list_unsubscribe
107 assert_equal("<mailto:example-unsubscribe@example.invalid>", list_unsubscribe)
109 list_address = sup_message.list_address
110 assert_equal("example@example.invalid", list_address.email)
111 assert_equal("example", list_address.name)
113 date = sup_message.date
114 assert_equal(Time.parse("Sun, 9 Dec 2007 21:48:19 +0200"), date)
117 assert_equal("20071209194819.GA25972@example.invalid", id)
119 refs = sup_message.refs
120 assert_equal(1, refs.length)
121 assert_equal("E1J1Rvb-0006k2-CE@localhost.localdomain", refs[0])
123 replytos = sup_message.replytos
124 assert_equal(1, replytos.length)
125 assert_equal("E1J1Rvb-0006k2-CE@localhost.localdomain", replytos[0])
129 assert_equal(0, cc.length)
131 bcc = sup_message.bcc
133 assert_equal(0, bcc.length)
135 recipient_email = sup_message.recipient_email
136 assert_equal("fake_receiver@localhost", recipient_email)
138 message_source = sup_message.source
139 assert_equal(message_source, source)
141 message_source_info = sup_message.source_info
142 assert_equal(message_source_info, source_info)
144 # read the message body chunks
146 chunks = sup_message.load_from_source!
148 # there should be only one chunk
149 assert_equal(1, chunks.length)
151 lines = chunks[0].lines
153 # there should be only one line
154 assert_equal(1, lines.length)
156 assert_equal("Test message!", lines[0])
160 def test_multipart_message
163 From fake_receiver@localhost Sun Dec 09 22:33:37 +0200 2007
164 Subject: Re: Test message subject
165 From: Fake Receiver <fake_receiver@localhost>
166 To: Fake Sender <fake_sender@example.invalid>
167 References: <E1J1Rvb-0006k2-CE@localhost.localdomain> <20071209194819.GA25972example.invalid>
168 In-Reply-To: <20071209194819.GA25972example.invalid>
169 Date: Sun, 09 Dec 2007 22:33:37 +0200
170 Message-Id: <1197232243-sup-2663example.invalid>
172 Content-Type: multipart/mixed; boundary="=-1197232418-506707-26079-6122-2-="
176 --=-1197232418-506707-26079-6122-2-=
177 Content-Type: text/plain; charset=utf-8
178 Content-Disposition: inline
180 Excerpts from Fake Sender's message of Sun Dec 09 21:48:19 +0200 2007:
183 Thanks for the message!
184 --=-1197232418-506707-26079-6122-2-=
185 Content-Disposition: attachment; filename="HACKING"
186 Content-Type: application/octet-stream; name="HACKING"
187 Content-Transfer-Encoding: base64
189 UnVubmluZyBTdXAgbG9jYWxseQotLS0tLS0tLS0tLS0tLS0tLS0tCkludm9r
190 ZSBpdCBsaWtlIHRoaXM6CgpydWJ5IC1JIGxpYiAtdyBiaW4vc3VwCgpZb3Un
191 bGwgaGF2ZSB0byBpbnN0YWxsIGFsbCBnZW1zIG1lbnRpb25lZCBpbiB0aGUg
192 UmFrZWZpbGUgKGxvb2sgZm9yIHRoZSBsaW5lCnNldHRpbmcgcC5leHRyYV9k
193 ZXBzKS4gSWYgeW91J3JlIG9uIGEgRGViaWFuIG9yIERlYmlhbi1iYXNlZCBz
194 eXN0ZW0gKGUuZy4KVWJ1bnR1KSwgeW91J2xsIGhhdmUgdG8gbWFrZSBzdXJl
195 IHlvdSBoYXZlIGEgY29tcGxldGUgUnVieSBpbnN0YWxsYXRpb24sCmVzcGVj
196 aWFsbHkgbGlic3NsLXJ1YnkuCgpDb2Rpbmcgc3RhbmRhcmRzCi0tLS0tLS0t
197 LS0tLS0tLS0KCi0gRG9uJ3Qgd3JhcCBjb2RlIHVubGVzcyBpdCByZWFsbHkg
198 YmVuZWZpdHMgZnJvbSBpdC4gVGhlIGRheXMgb2YKICA4MC1jb2x1bW4gZGlz
199 cGxheXMgYXJlIGxvbmcgb3Zlci4gQnV0IGRvIHdyYXAgY29tbWVudHMgYW5k
200 IG90aGVyCiAgdGV4dCBhdCB3aGF0ZXZlciBFbWFjcyBtZXRhLVEgZG9lcy4K
201 LSBJIGxpa2UgcG9ldHJ5IG1vZGUuCi0gVXNlIHt9IGZvciBvbmUtbGluZXIg
202 YmxvY2tzIGFuZCBkby9lbmQgZm9yIG11bHRpLWxpbmUgYmxvY2tzLgoK
204 --=-1197232418-506707-26079-6122-2-=
205 Content-Disposition: attachment; filename="Manifest.txt"
206 Content-Type: text/plain; name="Manifest.txt"
207 Content-Transfer-Encoding: quoted-printable
219 bin/sup-recover-sources
223 --=-1197232418-506707-26079-6122-2-=--
225 source = DummySource.new("sup-test://test_multipart_message")
226 source.messages = [ message ]
229 sup_message = Message.new( {:source => source, :source_info => source_info } )
231 # read the message body chunks
233 chunks = sup_message.load_from_source!
235 # this time there should be four chunks: first the quoted part of
236 # the message, then the non-quoted part, then the two attachments
237 assert_equal(4, chunks.length)
239 assert_equal(chunks[0].class, Redwood::Chunk::Quote)
240 assert_equal(chunks[1].class, Redwood::Chunk::Text)
241 assert_equal(chunks[2].class, Redwood::Chunk::Attachment)
242 assert_equal(chunks[3].class, Redwood::Chunk::Attachment)
244 # further testing of chunks will happen in test_message_chunks.rb
245 # (possibly not yet implemented)
249 def test_broken_message_1
251 # an example of a broken message, missing "to" and "from" fields
254 Return-path: <fake_sender@example.invalid>
255 Envelope-to: fake_receiver@localhost
256 Delivery-date: Sun, 09 Dec 2007 21:48:19 +0200
257 Received: from fake_sender by localhost.localdomain with local (Exim 4.67)
258 (envelope-from <fake_sender@example.invalid>)
260 for fake_receiver@localhost; Sun, 09 Dec 2007 21:48:19 +0200
261 Date: Sun, 9 Dec 2007 21:48:19 +0200
262 Subject: Re: Test message subject
263 Message-ID: <20071209194819.GA25972@example.invalid>
264 References: <E1J1Rvb-0006k2-CE@localhost.localdomain>
266 Content-Type: text/plain; charset=us-ascii
267 Content-Disposition: inline
268 In-Reply-To: <E1J1Rvb-0006k2-CE@localhost.localdomain>
274 source = DummySource.new("sup-test://test_broken_message_1")
275 source.messages = [ message ]
278 sup_message = Message.new( {:source => source, :source_info => source_info } )
282 # there should no items, since the message doesn't have any
283 # recipients -- still not nil
284 assert_equal(0, to.length)
286 # from will have bogus values
287 from = sup_message.from
288 # very basic email address check
289 assert_match(/\w+@\w+\.\w{2,4}/, from.email)
290 assert_not_nil(from.name)
294 def test_broken_message_2
296 # an example of a broken message, no body at all
299 Return-path: <fake_sender@example.invalid>
300 From: Fake Sender <fake_sender@example.invalid>
301 To: Fake Receiver <fake_receiver@localhost>
302 Envelope-to: fake_receiver@localhost
303 Delivery-date: Sun, 09 Dec 2007 21:48:19 +0200
304 Received: from fake_sender by localhost.localdomain with local (Exim 4.67)
305 (envelope-from <fake_sender@example.invalid>)
307 for fake_receiver@localhost; Sun, 09 Dec 2007 21:48:19 +0200
308 Date: Sun, 9 Dec 2007 21:48:19 +0200
309 Subject: Re: Test message subject
310 Message-ID: <20071209194819.GA25972@example.invalid>
311 References: <E1J1Rvb-0006k2-CE@localhost.localdomain>
313 Content-Type: text/plain; charset=us-ascii
314 Content-Disposition: inline
315 In-Reply-To: <E1J1Rvb-0006k2-CE@localhost.localdomain>
319 source = DummySource.new("sup-test://test_broken_message_1")
320 source.messages = [ message ]
323 sup_message = Message.new( {:source => source, :source_info => source_info } )
325 # read the message body chunks: no errors should reach this level
329 assert_nothing_raised() do
330 chunks = sup_message.load_from_source!
333 # the chunks list should be empty
335 assert_equal(0, chunks.length)
339 def test_multipart_message_2
342 Return-path: <vim-mac-return-3938-fake_receiver=localhost@vim.org>
343 Envelope-to: fake_receiver@localhost
344 Delivery-date: Wed, 14 Jun 2006 19:22:54 +0300
345 Received: from localhost ([127.0.0.1] helo=localhost.localdomain)
346 by localhost.localdomain with esmtp (Exim 4.60)
347 (envelope-from <vim-mac-return-3938-fake_receiver=localhost@vim.org>)
349 for fake_receiver@localhost; Wed, 14 Jun 2006 18:57:15 +0300
350 Received: from pop.gmail.com
351 by localhost.localdomain with POP3 (fetchmail-6.3.2)
352 for <fake_receiver@localhost> (single-drop); Wed, 14 Jun 2006 18:57:15 +0300 (EEST)
353 X-Gmail-Received: 8ee0fe5f895736974c042c8eaf176014b1ba7b88
354 Delivered-To: fake_receiver@localhost
355 Received: by 10.49.8.16 with SMTP id l16cs11327nfi;
356 Sun, 26 Mar 2006 19:31:56 -0800 (PST)
357 Received: by 10.66.224.8 with SMTP id w8mr2172862ugg;
358 Sun, 26 Mar 2006 19:31:56 -0800 (PST)
359 Received: from foobar.math.fu-berlin.de (foobar.math.fu-berlin.de [160.45.45.151])
360 by mx.gmail.com with SMTP id j3si553645ugd.2006.03.26.19.31.56;
361 Sun, 26 Mar 2006 19:31:56 -0800 (PST)
362 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)
363 Message-Id: <44275cac.74a494f1.315a.ffff825cSMTPIN_ADDED@mx.gmail.com>
364 Received: (qmail 24265 invoked by uid 200); 27 Mar 2006 02:32:39 -0000
365 Mailing-List: contact vim-mac-help@vim.org; run by ezmlm
367 Delivered-To: mailing list vim-mac@vim.org
368 Received: (qmail 7913 invoked from network); 26 Mar 2006 23:37:34 -0000
369 Received: from cpe-138-217-96-243.vic.bigpond.net.au (HELO vim.org) (138.217.96.243)
370 by foobar.math.fu-berlin.de with SMTP; 26 Mar 2006 23:37:34 -0000
371 From: fake_sender@example.invalid
373 Subject: Mail Delivery (failure vim-mac@vim.org)
374 Date: Mon, 27 Mar 2006 10:29:39 +1000
376 Content-Type: multipart/related;
377 type="multipart/alternative";
378 boundary="----=_NextPart_000_001B_01C0CA80.6B015D10"
380 X-MSMail-Priority: Normal
382 ------=_NextPart_000_001B_01C0CA80.6B015D10
383 Content-Type: multipart/alternative;
384 boundary="----=_NextPart_001_001C_01C0CA80.6B015D10"
386 ------=_NextPart_001_001C_01C0CA80.6B015D10
387 Content-Type: text/plain;
389 Content-Transfer-Encoding: quoted-printable
391 ------=_NextPart_001_001C_01C0CA80.6B015D10
392 Content-Type: text/html;
394 Content-Transfer-Encoding: quoted-printable
396 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
398 <META content=3D"text/html; charset=3Diso-8859-1" =
399 http-equiv=3DContent-Type>
400 <META content=3D"MSHTML 5.00.2920.0" name=3DGENERATOR>
403 <BODY bgColor=3D#ffffff>If the message will not displayed automatically,<br>
404 follow the link to read the delivered message.<br><br>
405 Received message is available at:<br>
406 <a href=3Dcid:031401Mfdab4$3f3dL780$73387018@57W81fa70Re height=3D0 width=3D0>www.vim.org/inbox/vim-mac/read.php?sessionid-18559</a>
408 src=3Dcid:031401Mfdab4$3f3dL780$73387018@57W81fa70Re height=3D0 width=3D0></iframe>
409 <DIV> </DIV></BODY></HTML>
411 ------=_NextPart_001_001C_01C0CA80.6B015D10--
413 ------=_NextPart_000_001B_01C0CA80.6B015D10--
417 source = DummySource.new("sup-test://test_multipart_message_2")
418 source.messages = [ message ]
421 sup_message = Message.new( {:source => source, :source_info => source_info } )
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 } )
513 # See how well parsing the message ID went.
515 assert_equal("D3C12B2AD838B44DA9D6B2CA334246D011E72A73A4@PA-EXMBX04.widget.com", id)
517 # Look at another header field whose first line was blank.
518 list_unsubscribe = sup_message.list_unsubscribe
519 assert_equal(" <http://mailman2.widget.com/mailman/listinfo/monitor-list>, " +
520 "<mailto:monitor-list-request@widget.com?subject=unsubscribe>",
525 # TODO: test different error cases, malformed messages etc.
527 # TODO: test different quoting styles, see that they are all divided
534 # vim:noai:ts=2:sw=2: