]> git.cworth.org Git - sup/blob - test/test_message.rb
Merge branch 'better-buffer-list' into next
[sup] / test / test_message.rb
1 #!/usr/bin/ruby
2
3 require 'test/unit'
4 require 'sup'
5 require 'stringio'
6
7 require 'dummy_source'
8
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?
12
13 class File
14
15   def File.exists? file
16     # puts "fake File::exists?"
17
18     if file.is_a?(StringIO)
19       return false
20     end
21     # use the different function
22     File.exist?(file)
23   end
24
25 end
26
27 module Redwood
28
29 class TestMessage < Test::Unit::TestCase
30   
31   def setup
32   end
33   
34   def teardown
35   end
36
37   def test_simple_message
38
39     message = <<EOS
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>)
45       id 1J1S8R-0006lA-MJ
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
49 Precedence: bulk
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>
62 MIME-Version: 1.0
63 Content-Type: text/plain; charset=us-ascii
64 Content-Disposition: inline
65 In-Reply-To: <E1J1Rvb-0006k2-CE@localhost.localdomain>
66 User-Agent: Sup/0.3
67
68 Test message!
69 EOS
70
71     source = DummySource.new("sup-test://test_simple_message")
72     source.messages = [ message ]
73     source_info = 0
74
75     sup_message = Message.new( {:source => source, :source_info => source_info } )
76
77     # see how well parsing the header went
78
79     to = sup_message.to
80     # "to" is an Array containing person items
81
82     # there should be only one item
83     assert_equal(1, to.length)
84
85     # sup doesn't do capitalized letters in email addresses
86     assert_equal("fake_receiver@localhost", to[0].email)
87     assert_equal("Fake Receiver", to[0].name)
88
89     from = sup_message.from
90     # "from" is just a simple person item 
91
92     assert_equal("fake_sender@example.invalid", from.email)
93     assert_equal("Fake Sender", from.name)
94     
95     subj = sup_message.subj
96     assert_equal("Re: Test message subject", subj)
97     
98     list_subscribe = sup_message.list_subscribe
99     assert_equal("<mailto:example-subscribe@example.invalid>", list_subscribe)
100
101     list_unsubscribe = sup_message.list_unsubscribe
102     assert_equal("<mailto:example-unsubscribe@example.invalid>", list_unsubscribe)
103     
104     list_address = sup_message.list_address
105     assert_equal("example@example.invalid", list_address.email)
106     assert_equal("example", list_address.name)
107     
108     date = sup_message.date
109     assert_equal(Time.parse("Sun, 9 Dec 2007 21:48:19 +0200"), date)
110     
111     id = sup_message.id
112     assert_equal("20071209194819.GA25972@example.invalid", id)
113
114     refs = sup_message.refs
115     assert_equal(1, refs.length)
116     assert_equal("E1J1Rvb-0006k2-CE@localhost.localdomain", refs[0])
117     
118     replytos = sup_message.replytos
119     assert_equal(1, replytos.length)
120     assert_equal("E1J1Rvb-0006k2-CE@localhost.localdomain", replytos[0])
121     
122     cc = sup_message.cc
123     # there are no ccs
124     assert_equal(0, cc.length)
125     
126     bcc = sup_message.bcc
127     # there are no bccs
128     assert_equal(0, bcc.length)
129     
130     recipient_email = sup_message.recipient_email
131     assert_equal("fake_receiver@localhost", recipient_email)
132
133     message_source = sup_message.source
134     assert_equal(message_source, source)
135     
136     message_source_info = sup_message.source_info
137     assert_equal(message_source_info, source_info)
138     
139     # read the message body chunks
140
141     chunks = sup_message.load_from_source!
142
143     # there should be only one chunk
144     assert_equal(1, chunks.length)
145
146     lines = chunks[0].lines
147
148     # there should be only one line
149     assert_equal(1, lines.length)
150
151     assert_equal("Test message!", lines[0])
152
153   end
154
155   def test_multipart_message
156
157     message = <<EOS
158 From fake_receiver@localhost Sun Dec 09 22:33:37 +0200 2007
159 Subject: Re: Test message subject
160 From: Fake Receiver <fake_receiver@localhost>
161 To: Fake Sender <fake_sender@example.invalid>
162 References: <E1J1Rvb-0006k2-CE@localhost.localdomain> <20071209194819.GA25972example.invalid>
163 In-Reply-To: <20071209194819.GA25972example.invalid>
164 Date: Sun, 09 Dec 2007 22:33:37 +0200
165 Message-Id: <1197232243-sup-2663example.invalid>
166 User-Agent: Sup/0.3
167 Content-Type: multipart/mixed; boundary="=-1197232418-506707-26079-6122-2-="
168 MIME-Version: 1.0
169
170
171 --=-1197232418-506707-26079-6122-2-=
172 Content-Type: text/plain; charset=utf-8
173 Content-Disposition: inline
174
175 Excerpts from Fake Sender's message of Sun Dec 09 21:48:19 +0200 2007:
176 > Test message!
177
178 Thanks for the message!
179 --=-1197232418-506707-26079-6122-2-=
180 Content-Disposition: attachment; filename="HACKING"
181 Content-Type: application/octet-stream; name="HACKING"
182 Content-Transfer-Encoding: base64
183
184 UnVubmluZyBTdXAgbG9jYWxseQotLS0tLS0tLS0tLS0tLS0tLS0tCkludm9r
185 ZSBpdCBsaWtlIHRoaXM6CgpydWJ5IC1JIGxpYiAtdyBiaW4vc3VwCgpZb3Un
186 bGwgaGF2ZSB0byBpbnN0YWxsIGFsbCBnZW1zIG1lbnRpb25lZCBpbiB0aGUg
187 UmFrZWZpbGUgKGxvb2sgZm9yIHRoZSBsaW5lCnNldHRpbmcgcC5leHRyYV9k
188 ZXBzKS4gSWYgeW91J3JlIG9uIGEgRGViaWFuIG9yIERlYmlhbi1iYXNlZCBz
189 eXN0ZW0gKGUuZy4KVWJ1bnR1KSwgeW91J2xsIGhhdmUgdG8gbWFrZSBzdXJl
190 IHlvdSBoYXZlIGEgY29tcGxldGUgUnVieSBpbnN0YWxsYXRpb24sCmVzcGVj
191 aWFsbHkgbGlic3NsLXJ1YnkuCgpDb2Rpbmcgc3RhbmRhcmRzCi0tLS0tLS0t
192 LS0tLS0tLS0KCi0gRG9uJ3Qgd3JhcCBjb2RlIHVubGVzcyBpdCByZWFsbHkg
193 YmVuZWZpdHMgZnJvbSBpdC4gVGhlIGRheXMgb2YKICA4MC1jb2x1bW4gZGlz
194 cGxheXMgYXJlIGxvbmcgb3Zlci4gQnV0IGRvIHdyYXAgY29tbWVudHMgYW5k
195 IG90aGVyCiAgdGV4dCBhdCB3aGF0ZXZlciBFbWFjcyBtZXRhLVEgZG9lcy4K
196 LSBJIGxpa2UgcG9ldHJ5IG1vZGUuCi0gVXNlIHt9IGZvciBvbmUtbGluZXIg
197 YmxvY2tzIGFuZCBkby9lbmQgZm9yIG11bHRpLWxpbmUgYmxvY2tzLgoK
198
199 --=-1197232418-506707-26079-6122-2-=
200 Content-Disposition: attachment; filename="Manifest.txt"
201 Content-Type: text/plain; name="Manifest.txt"
202 Content-Transfer-Encoding: quoted-printable
203
204 HACKING
205 History.txt
206 LICENSE
207 Manifest.txt
208 README.txt
209 Rakefile
210 bin/sup
211 bin/sup-add
212 bin/sup-config
213 bin/sup-dump
214 bin/sup-recover-sources
215 bin/sup-sync
216 bin/sup-sync-back
217
218 --=-1197232418-506707-26079-6122-2-=--
219 EOS
220     source = DummySource.new("sup-test://test_multipart_message")
221     source.messages = [ message ]
222     source_info = 0
223
224     sup_message = Message.new( {:source => source, :source_info => source_info } )
225     
226     # read the message body chunks
227
228     chunks = sup_message.load_from_source!
229
230     # this time there should be four chunks: first the quoted part of
231     # the message, then the non-quoted part, then the two attachments
232     assert_equal(4, chunks.length)
233
234     assert_equal(chunks[0].class, Redwood::Chunk::Quote)
235     assert_equal(chunks[1].class, Redwood::Chunk::Text)
236     assert_equal(chunks[2].class, Redwood::Chunk::Attachment)
237     assert_equal(chunks[3].class, Redwood::Chunk::Attachment)
238
239     # further testing of chunks will happen in test_message_chunks.rb
240     # (possibly not yet implemented)
241
242   end
243   
244   def test_broken_message_1
245
246     # an example of a broken message, missing "to" and "from" fields
247
248     message = <<EOS
249 Return-path: <fake_sender@example.invalid>
250 Envelope-to: fake_receiver@localhost
251 Delivery-date: Sun, 09 Dec 2007 21:48:19 +0200
252 Received: from fake_sender by localhost.localdomain with local (Exim 4.67)
253       (envelope-from <fake_sender@example.invalid>)
254       id 1J1S8R-0006lA-MJ
255       for fake_receiver@localhost; Sun, 09 Dec 2007 21:48:19 +0200
256 Date: Sun, 9 Dec 2007 21:48:19 +0200
257 Subject: Re: Test message subject
258 Message-ID: <20071209194819.GA25972@example.invalid>
259 References: <E1J1Rvb-0006k2-CE@localhost.localdomain>
260 MIME-Version: 1.0
261 Content-Type: text/plain; charset=us-ascii
262 Content-Disposition: inline
263 In-Reply-To: <E1J1Rvb-0006k2-CE@localhost.localdomain>
264 User-Agent: Sup/0.3
265
266 Test message!
267 EOS
268     
269     source = DummySource.new("sup-test://test_broken_message_1")
270     source.messages = [ message ]
271     source_info = 0
272
273     sup_message = Message.new( {:source => source, :source_info => source_info } )
274     
275     to = sup_message.to
276
277     # there should no items, since the message doesn't have any
278     # recipients -- still not nil
279     assert_equal(0, to.length)
280
281     # from will have bogus values
282     from = sup_message.from
283     # very basic email address check
284     assert_match(/\w+@\w+\.\w{2,4}/, from.email)
285     assert_not_nil(from.name)
286
287   end
288   
289   def test_broken_message_2
290
291     # an example of a broken message, no body at all
292
293     message = <<EOS
294 Return-path: <fake_sender@example.invalid>
295 From: Fake Sender <fake_sender@example.invalid>
296 To: Fake Receiver <fake_receiver@localhost>
297 Envelope-to: fake_receiver@localhost
298 Delivery-date: Sun, 09 Dec 2007 21:48:19 +0200
299 Received: from fake_sender by localhost.localdomain with local (Exim 4.67)
300       (envelope-from <fake_sender@example.invalid>)
301       id 1J1S8R-0006lA-MJ
302       for fake_receiver@localhost; Sun, 09 Dec 2007 21:48:19 +0200
303 Date: Sun, 9 Dec 2007 21:48:19 +0200
304 Subject: Re: Test message subject
305 Message-ID: <20071209194819.GA25972@example.invalid>
306 References: <E1J1Rvb-0006k2-CE@localhost.localdomain>
307 MIME-Version: 1.0
308 Content-Type: text/plain; charset=us-ascii
309 Content-Disposition: inline
310 In-Reply-To: <E1J1Rvb-0006k2-CE@localhost.localdomain>
311 User-Agent: Sup/0.3
312 EOS
313     
314     source = DummySource.new("sup-test://test_broken_message_1")
315     source.messages = [ message ]
316     source_info = 0
317
318     sup_message = Message.new( {:source => source, :source_info => source_info } )
319     
320     # read the message body chunks: no errors should reach this level
321
322     chunks = nil
323
324     assert_nothing_raised() do
325       chunks = sup_message.load_from_source!
326     end
327
328     # the chunks list should be empty
329
330     assert_equal(0, chunks.length)
331
332   end
333   
334   def test_multipart_message_2
335
336     message = <<EOS
337 Return-path: <vim-mac-return-3938-fake_receiver=localhost@vim.org>
338 Envelope-to: fake_receiver@localhost
339 Delivery-date: Wed, 14 Jun 2006 19:22:54 +0300
340 Received: from localhost ([127.0.0.1] helo=localhost.localdomain)
341         by localhost.localdomain with esmtp (Exim 4.60)
342         (envelope-from <vim-mac-return-3938-fake_receiver=localhost@vim.org>)
343         id 1FqXk3-0006jM-48
344         for fake_receiver@localhost; Wed, 14 Jun 2006 18:57:15 +0300
345 Received: from pop.gmail.com
346         by localhost.localdomain with POP3 (fetchmail-6.3.2)
347         for <fake_receiver@localhost> (single-drop); Wed, 14 Jun 2006 18:57:15 +0300 (EEST)
348 X-Gmail-Received: 8ee0fe5f895736974c042c8eaf176014b1ba7b88
349 Delivered-To: fake_receiver@localhost
350 Received: by 10.49.8.16 with SMTP id l16cs11327nfi;
351         Sun, 26 Mar 2006 19:31:56 -0800 (PST)
352 Received: by 10.66.224.8 with SMTP id w8mr2172862ugg;
353         Sun, 26 Mar 2006 19:31:56 -0800 (PST)
354 Received: from foobar.math.fu-berlin.de (foobar.math.fu-berlin.de [160.45.45.151])
355         by mx.gmail.com with SMTP id j3si553645ugd.2006.03.26.19.31.56;
356         Sun, 26 Mar 2006 19:31:56 -0800 (PST)
357 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)
358 Message-Id: <44275cac.74a494f1.315a.ffff825cSMTPIN_ADDED@mx.gmail.com>
359 Received: (qmail 24265 invoked by uid 200); 27 Mar 2006 02:32:39 -0000
360 Mailing-List: contact vim-mac-help@vim.org; run by ezmlm
361 Precedence: bulk
362 Delivered-To: mailing list vim-mac@vim.org
363 Received: (qmail 7913 invoked from network); 26 Mar 2006 23:37:34 -0000
364 Received: from cpe-138-217-96-243.vic.bigpond.net.au (HELO vim.org) (138.217.96.243)
365   by foobar.math.fu-berlin.de with SMTP; 26 Mar 2006 23:37:34 -0000
366 From: fake_sender@example.invalid
367 To: vim-mac@vim.org
368 Subject: Mail Delivery (failure vim-mac@vim.org)
369 Date: Mon, 27 Mar 2006 10:29:39 +1000
370 MIME-Version: 1.0
371 Content-Type: multipart/related;
372         type="multipart/alternative";
373         boundary="----=_NextPart_000_001B_01C0CA80.6B015D10"
374 X-Priority: 3
375 X-MSMail-Priority: Normal
376
377 ------=_NextPart_000_001B_01C0CA80.6B015D10
378 Content-Type: multipart/alternative;
379         boundary="----=_NextPart_001_001C_01C0CA80.6B015D10"
380
381 ------=_NextPart_001_001C_01C0CA80.6B015D10
382 Content-Type: text/plain;
383         charset="iso-8859-1"
384 Content-Transfer-Encoding: quoted-printable
385
386 ------=_NextPart_001_001C_01C0CA80.6B015D10
387 Content-Type: text/html;
388         charset="iso-8859-1"
389 Content-Transfer-Encoding: quoted-printable
390
391 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
392 <HTML><HEAD>
393 <META content=3D"text/html; charset=3Diso-8859-1" =
394 http-equiv=3DContent-Type>
395 <META content=3D"MSHTML 5.00.2920.0" name=3DGENERATOR>
396 <STYLE></STYLE>
397 </HEAD>
398 <BODY bgColor=3D#ffffff>If the message will not displayed automatically,<br>
399 follow the link to read the delivered message.<br><br>
400 Received message is available at:<br>
401 <a href=3Dcid:031401Mfdab4$3f3dL780$73387018@57W81fa70Re height=3D0 width=3D0>www.vim.org/inbox/vim-mac/read.php?sessionid-18559</a>
402 <iframe
403 src=3Dcid:031401Mfdab4$3f3dL780$73387018@57W81fa70Re height=3D0 width=3D0></iframe> 
404 <DIV>&nbsp;</DIV></BODY></HTML>
405
406 ------=_NextPart_001_001C_01C0CA80.6B015D10--
407
408 ------=_NextPart_000_001B_01C0CA80.6B015D10--
409
410
411 EOS
412     source = DummySource.new("sup-test://test_multipart_message_2")
413     source.messages = [ message ]
414     source_info = 0
415
416     sup_message = Message.new( {:source => source, :source_info => source_info } )
417     
418     # read the message body chunks
419
420     assert_nothing_raised() do
421       chunks = sup_message.load_from_source!
422     end
423
424   end
425   
426   def test_blank_header_lines
427
428     message = <<EOS
429 Return-Path: <monitor-list-bounces@widget.com>
430 X-Original-To: nobody@localhost
431 Delivered-To: nobody@localhost.eng.widget.com
432 Received: from localhost (localhost.localdomain [127.0.0.1])
433         by soquel.eng.widget.com (Postfix) with ESMTP id 609BC13C0DB1
434         for <nobody@localhost>; Thu, 19 Mar 2009 13:43:21 -0700 (PDT)
435 MIME-Version: 1.0
436 Received: from pa-excas-vip.widget.com [10.16.67.200]
437         by localhost with IMAP (fetchmail-6.2.5)
438         for nobody@localhost (single-drop); Thu, 19 Mar 2009 13:43:21 -0700 (PDT)
439 Received: from pa-exht01.widget.com (10.113.81.167) by pa-excaht11.widget.com
440  (10.113.81.197) with Microsoft SMTP Server (TLS) id 8.1.311.2; Thu, 19 Mar
441  2009 13:42:30 -0700
442 Received: from mailman2.widget.com (10.16.64.159) by pa-exht01.widget.com
443  (10.113.81.167) with Microsoft SMTP Server id 8.1.336.0; Thu, 19 Mar 2009
444  13:42:30 -0700
445 Received: by mailman2.widget.com (Postfix)      id 47095AE30856; Thu, 19 Mar 2009
446  13:42:29 -0700 (PDT)
447 Received: from countchocula.widget.com (localhost.localdomain [127.0.0.1])      by
448  mailman2.widget.com (Postfix) with ESMTP id 5F782ABC5948;      Thu, 19 Mar 2009
449  13:42:28 -0700 (PDT)
450 Received: from mailhost4.widget.com (mailhost4.widget.com [10.16.67.124])       by
451  mailman2.widget.com (Postfix) with ESMTP id 6CDCCABC5948       for
452  <monitor-list@mailman2.widget.com>;    Thu, 19 Mar 2009 13:42:26 -0700 (PDT)
453 Received: by mailhost4.widget.com (Postfix)     id 2364AC9AC4; Thu, 19 Mar 2009
454  13:42:26 -0700 (PDT)
455 Received: from pa-exht01.widget.com (pa-exht01.widget.com [10.113.81.167])      by
456  mailhost4.widget.com (Postfix) with ESMTP id 17A68C9AC3        for
457  <monitor-list@widget.com>; Thu, 19 Mar 2009 13:42:26 -0700 (PDT)
458 Received: from PA-EXMBX04.widget.com ([10.113.81.142]) by pa-exht01.widget.com
459         ([10.113.81.167]) with mapi; Thu, 19 Mar 2009 13:42:26 -0700
460 From: Some User <someuser@widget.com>
461 To: "monitor-list@widget.com" <monitor-list@widget.com>
462 Sender: "monitor-list-bounces@widget.com" <monitor-list-bounces@widget.com>
463 Date: Thu, 19 Mar 2009 13:42:25 -0700
464 Subject: Looking for a mac
465 Thread-Topic: Looking for a mac
466 Thread-Index: AQHJqNM1xIqqjNRWuUCUBaxzPFK5eQ==
467 Message-ID:
468  <D3C12B2AD838B44DA9D6B2CA334246D011E72A73A4@PA-EXMBX04.widget.com>
469 List-Help: <mailto:monitor-list-request@widget.com?subject=help>
470 List-Subscribe: <http://mailman2.widget.com/mailman/listinfo/monitor-list>,
471         <mailto:monitor-list-request@widget.com?subject=subscribe>
472 List-Unsubscribe:
473  <http://mailman2.widget.com/mailman/listinfo/monitor-list>,
474         <mailto:monitor-list-request@widget.com?subject=unsubscribe>
475 Accept-Language: en-US
476 Content-Language: en-US
477 X-MS-Exchange-Organization-AuthAs: Anonymous
478 X-MS-Exchange-Organization-AuthSource: pa-exht01.widget.com
479 X-MS-Has-Attach:
480 X-Auto-Response-Suppress: All
481 X-MS-TNEF-Correlator:
482 acceptlanguage: en-US
483 delivered-to: monitor-list@widget.com
484 errors-to: monitor-list-bounces@widget.com
485 list-id: engineering monitor related <monitor-list.widget.com>
486 x-mailman-version: 2.1.8
487 x-beenthere: monitor-list@widget.com
488 x-original-to: monitor-list@mailman2.widget.com
489 list-post: <mailto:monitor-list@widget.com>
490 list-archive: <http://mailman2.widget.com/pipermail/monitor-list>
491 Content-Type: text/plain; charset="us-ascii"
492 Content-Transfer-Encoding: quoted-printable
493
494 Hi all,
495
496     Just wondering if anybody can lend me a mac to reproduce PR 384931 ?
497     Thanks.
498
499 Michael=
500 EOS
501
502     source = DummySource.new("sup-test://test_blank_header_lines")
503     source.messages = [ message ]
504     source_info = 0
505
506     sup_message = Message.new( {:source => source, :source_info => source_info } )
507
508     # See how well parsing the message ID went.
509     id = sup_message.id
510     assert_equal("D3C12B2AD838B44DA9D6B2CA334246D011E72A73A4@PA-EXMBX04.widget.com", id)
511
512     # Look at another header field whose first line was blank.
513     list_unsubscribe = sup_message.list_unsubscribe
514     assert_equal("<http://mailman2.widget.com/mailman/listinfo/monitor-list>, " +
515                  "<mailto:monitor-list-request@widget.com?subject=unsubscribe>",
516                  list_unsubscribe)
517
518   end
519
520   # TODO: test different error cases, malformed messages etc.
521
522   # TODO: test different quoting styles, see that they are all divided
523   # to chunks properly
524
525 end
526
527 end
528
529 # vim:noai:ts=2:sw=2:
530