]> git.cworth.org Git - sup/blob - test/test_message.rb
Add new :crypto_default configuration option.
[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     sup_message.load_from_source!
77
78     # see how well parsing the header went
79
80     to = sup_message.to
81     # "to" is an Array containing person items
82
83     # there should be only one item
84     assert_equal(1, to.length)
85
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)
89
90     from = sup_message.from
91     # "from" is just a simple person item 
92
93     assert_equal("fake_sender@example.invalid", from.email)
94     assert_equal("Fake Sender", from.name)
95     
96     subj = sup_message.subj
97     assert_equal("Re: Test message subject", subj)
98     
99     list_subscribe = sup_message.list_subscribe
100     assert_equal("<mailto:example-subscribe@example.invalid>", list_subscribe)
101
102     list_unsubscribe = sup_message.list_unsubscribe
103     assert_equal("<mailto:example-unsubscribe@example.invalid>", list_unsubscribe)
104     
105     list_address = sup_message.list_address
106     assert_equal("example@example.invalid", list_address.email)
107     assert_equal("example", list_address.name)
108     
109     date = sup_message.date
110     assert_equal(Time.parse("Sun, 9 Dec 2007 21:48:19 +0200"), date)
111     
112     id = sup_message.id
113     assert_equal("20071209194819.GA25972@example.invalid", id)
114
115     refs = sup_message.refs
116     assert_equal(1, refs.length)
117     assert_equal("E1J1Rvb-0006k2-CE@localhost.localdomain", refs[0])
118     
119     replytos = sup_message.replytos
120     assert_equal(1, replytos.length)
121     assert_equal("E1J1Rvb-0006k2-CE@localhost.localdomain", replytos[0])
122     
123     cc = sup_message.cc
124     # there are no ccs
125     assert_equal(0, cc.length)
126     
127     bcc = sup_message.bcc
128     # there are no bccs
129     assert_equal(0, bcc.length)
130     
131     recipient_email = sup_message.recipient_email
132     assert_equal("fake_receiver@localhost", recipient_email)
133
134     message_source = sup_message.source
135     assert_equal(message_source, source)
136     
137     message_source_info = sup_message.source_info
138     assert_equal(message_source_info, source_info)
139     
140     # read the message body chunks
141
142     chunks = sup_message.load_from_source!
143
144     # there should be only one chunk
145     assert_equal(1, chunks.length)
146
147     lines = chunks[0].lines
148
149     # there should be only one line
150     assert_equal(1, lines.length)
151
152     assert_equal("Test message!", lines[0])
153
154   end
155
156   def test_multipart_message
157
158     message = <<EOS
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>
167 User-Agent: Sup/0.3
168 Content-Type: multipart/mixed; boundary="=-1197232418-506707-26079-6122-2-="
169 MIME-Version: 1.0
170
171
172 --=-1197232418-506707-26079-6122-2-=
173 Content-Type: text/plain; charset=utf-8
174 Content-Disposition: inline
175
176 Excerpts from Fake Sender's message of Sun Dec 09 21:48:19 +0200 2007:
177 > Test message!
178
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
184
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
199
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
204
205 HACKING
206 History.txt
207 LICENSE
208 Manifest.txt
209 README.txt
210 Rakefile
211 bin/sup
212 bin/sup-add
213 bin/sup-config
214 bin/sup-dump
215 bin/sup-recover-sources
216 bin/sup-sync
217 bin/sup-sync-back
218
219 --=-1197232418-506707-26079-6122-2-=--
220 EOS
221     source = DummySource.new("sup-test://test_multipart_message")
222     source.messages = [ message ]
223     source_info = 0
224
225     sup_message = Message.new( {:source => source, :source_info => source_info } )
226     sup_message.load_from_source!
227     
228     # read the message body chunks
229
230     chunks = sup_message.load_from_source!
231
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)
235
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)
240
241     # further testing of chunks will happen in test_message_chunks.rb
242     # (possibly not yet implemented)
243
244   end
245   
246   def test_broken_message_1
247
248     # an example of a broken message, missing "to" and "from" fields
249
250     message = <<EOS
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>)
256       id 1J1S8R-0006lA-MJ
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>
262 MIME-Version: 1.0
263 Content-Type: text/plain; charset=us-ascii
264 Content-Disposition: inline
265 In-Reply-To: <E1J1Rvb-0006k2-CE@localhost.localdomain>
266 User-Agent: Sup/0.3
267
268 Test message!
269 EOS
270     
271     source = DummySource.new("sup-test://test_broken_message_1")
272     source.messages = [ message ]
273     source_info = 0
274
275     sup_message = Message.new( {:source => source, :source_info => source_info } )
276     sup_message.load_from_source!
277     
278     to = sup_message.to
279
280     # there should no items, since the message doesn't have any
281     # recipients -- still not nil
282     assert_equal(0, to.length)
283
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)
289
290   end
291   
292   def test_broken_message_2
293
294     # an example of a broken message, no body at all
295
296     message = <<EOS
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>)
304       id 1J1S8R-0006lA-MJ
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>
310 MIME-Version: 1.0
311 Content-Type: text/plain; charset=us-ascii
312 Content-Disposition: inline
313 In-Reply-To: <E1J1Rvb-0006k2-CE@localhost.localdomain>
314 User-Agent: Sup/0.3
315 EOS
316     
317     source = DummySource.new("sup-test://test_broken_message_1")
318     source.messages = [ message ]
319     source_info = 0
320
321     sup_message = Message.new( {:source => source, :source_info => source_info } )
322     sup_message.load_from_source!
323     
324     # read the message body chunks: no errors should reach this level
325
326     chunks = nil
327
328     assert_nothing_raised() do
329       chunks = sup_message.load_from_source!
330     end
331
332     # the chunks list should be empty
333
334     assert_equal(0, chunks.length)
335
336   end
337   
338   def test_multipart_message_2
339
340     message = <<EOS
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>)
347         id 1FqXk3-0006jM-48
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
365 Precedence: bulk
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
371 To: vim-mac@vim.org
372 Subject: Mail Delivery (failure vim-mac@vim.org)
373 Date: Mon, 27 Mar 2006 10:29:39 +1000
374 MIME-Version: 1.0
375 Content-Type: multipart/related;
376         type="multipart/alternative";
377         boundary="----=_NextPart_000_001B_01C0CA80.6B015D10"
378 X-Priority: 3
379 X-MSMail-Priority: Normal
380
381 ------=_NextPart_000_001B_01C0CA80.6B015D10
382 Content-Type: multipart/alternative;
383         boundary="----=_NextPart_001_001C_01C0CA80.6B015D10"
384
385 ------=_NextPart_001_001C_01C0CA80.6B015D10
386 Content-Type: text/plain;
387         charset="iso-8859-1"
388 Content-Transfer-Encoding: quoted-printable
389
390 ------=_NextPart_001_001C_01C0CA80.6B015D10
391 Content-Type: text/html;
392         charset="iso-8859-1"
393 Content-Transfer-Encoding: quoted-printable
394
395 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
396 <HTML><HEAD>
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>
400 <STYLE></STYLE>
401 </HEAD>
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>
406 <iframe
407 src=3Dcid:031401Mfdab4$3f3dL780$73387018@57W81fa70Re height=3D0 width=3D0></iframe> 
408 <DIV>&nbsp;</DIV></BODY></HTML>
409
410 ------=_NextPart_001_001C_01C0CA80.6B015D10--
411
412 ------=_NextPart_000_001B_01C0CA80.6B015D10--
413
414
415 EOS
416     source = DummySource.new("sup-test://test_multipart_message_2")
417     source.messages = [ message ]
418     source_info = 0
419
420     sup_message = Message.new( {:source => source, :source_info => source_info } )
421     sup_message.load_from_source!
422     
423     # read the message body chunks
424
425     assert_nothing_raised() do
426       chunks = sup_message.load_from_source!
427     end
428
429   end
430   
431   def test_blank_header_lines
432
433     message = <<EOS
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)
440 MIME-Version: 1.0
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
446  2009 13:42:30 -0700
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
449  13:42:30 -0700
450 Received: by mailman2.widget.com (Postfix)      id 47095AE30856; Thu, 19 Mar 2009
451  13:42:29 -0700 (PDT)
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
454  13:42:28 -0700 (PDT)
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
459  13:42:26 -0700 (PDT)
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==
472 Message-ID:
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>
477 List-Unsubscribe:
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
484 X-MS-Has-Attach:
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
498
499 Hi all,
500
501     Just wondering if anybody can lend me a mac to reproduce PR 384931 ?
502     Thanks.
503
504 Michael=
505 EOS
506
507     source = DummySource.new("sup-test://test_blank_header_lines")
508     source.messages = [ message ]
509     source_info = 0
510
511     sup_message = Message.new( {:source => source, :source_info => source_info } )
512     sup_message.load_from_source!
513
514     # See how well parsing the message ID went.
515     id = sup_message.id
516     assert_equal("D3C12B2AD838B44DA9D6B2CA334246D011E72A73A4@PA-EXMBX04.widget.com", id)
517
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>",
522                  list_unsubscribe)
523
524   end
525
526   # TODO: test different error cases, malformed messages etc.
527
528   # TODO: test different quoting styles, see that they are all divided
529   # to chunks properly
530
531 end
532
533 end
534
535 # vim:noai:ts=2:sw=2:
536