]> git.cworth.org Git - sup/blob - test/test_message.rb
Merge commit 'origin/find-in-buffer-fix'
[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     person_file = StringIO.new("")
33     # this is a singleton
34     if not PersonManager.instantiated?
35       @person_manager = PersonManager.new(person_file)
36     end
37   end
38   
39   def teardown
40   end
41
42   def test_simple_message
43
44     message = <<EOS
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>)
50       id 1J1S8R-0006lA-MJ
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
54 Precedence: bulk
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>
67 MIME-Version: 1.0
68 Content-Type: text/plain; charset=us-ascii
69 Content-Disposition: inline
70 In-Reply-To: <E1J1Rvb-0006k2-CE@localhost.localdomain>
71 User-Agent: Sup/0.3
72
73 Test message!
74 EOS
75
76     source = DummySource.new("sup-test://test_simple_message")
77     source.messages = [ message ]
78     source_info = 0
79
80     sup_message = Message.new( {:source => source, :source_info => source_info } )
81
82     # see how well parsing the header went
83
84     to = sup_message.to
85     # "to" is an Array containing person items
86
87     # there should be only one item
88     assert_equal(1, to.length)
89
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)
93
94     from = sup_message.from
95     # "from" is just a simple person item 
96
97     assert_equal("fake_sender@example.invalid", from.email)
98     assert_equal("Fake Sender", from.name)
99     
100     subj = sup_message.subj
101     assert_equal("Re: Test message subject", subj)
102     
103     list_subscribe = sup_message.list_subscribe
104     assert_equal("<mailto:example-subscribe@example.invalid>", list_subscribe)
105
106     list_unsubscribe = sup_message.list_unsubscribe
107     assert_equal("<mailto:example-unsubscribe@example.invalid>", list_unsubscribe)
108     
109     list_address = sup_message.list_address
110     assert_equal("example@example.invalid", list_address.email)
111     assert_equal("example", list_address.name)
112     
113     date = sup_message.date
114     assert_equal(Time.parse("Sun, 9 Dec 2007 21:48:19 +0200"), date)
115     
116     id = sup_message.id
117     assert_equal("20071209194819.GA25972@example.invalid", id)
118
119     refs = sup_message.refs
120     assert_equal(1, refs.length)
121     assert_equal("E1J1Rvb-0006k2-CE@localhost.localdomain", refs[0])
122     
123     replytos = sup_message.replytos
124     assert_equal(1, replytos.length)
125     assert_equal("E1J1Rvb-0006k2-CE@localhost.localdomain", replytos[0])
126     
127     cc = sup_message.cc
128     # there are no ccs
129     assert_equal(0, cc.length)
130     
131     bcc = sup_message.bcc
132     # there are no bccs
133     assert_equal(0, bcc.length)
134     
135     recipient_email = sup_message.recipient_email
136     assert_equal("fake_receiver@localhost", recipient_email)
137
138     message_source = sup_message.source
139     assert_equal(message_source, source)
140     
141     message_source_info = sup_message.source_info
142     assert_equal(message_source_info, source_info)
143     
144     # read the message body chunks
145
146     chunks = sup_message.load_from_source!
147
148     # there should be only one chunk
149     assert_equal(1, chunks.length)
150
151     lines = chunks[0].lines
152
153     # there should be only one line
154     assert_equal(1, lines.length)
155
156     assert_equal("Test message!", lines[0])
157
158   end
159
160   def test_multipart_message
161
162     message = <<EOS
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>
171 User-Agent: Sup/0.3
172 Content-Type: multipart/mixed; boundary="=-1197232418-506707-26079-6122-2-="
173 MIME-Version: 1.0
174
175
176 --=-1197232418-506707-26079-6122-2-=
177 Content-Type: text/plain; charset=utf-8
178 Content-Disposition: inline
179
180 Excerpts from Fake Sender's message of Sun Dec 09 21:48:19 +0200 2007:
181 > Test message!
182
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
188
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
203
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
208
209 HACKING
210 History.txt
211 LICENSE
212 Manifest.txt
213 README.txt
214 Rakefile
215 bin/sup
216 bin/sup-add
217 bin/sup-config
218 bin/sup-dump
219 bin/sup-recover-sources
220 bin/sup-sync
221 bin/sup-sync-back
222
223 --=-1197232418-506707-26079-6122-2-=--
224 EOS
225     source = DummySource.new("sup-test://test_multipart_message")
226     source.messages = [ message ]
227     source_info = 0
228
229     sup_message = Message.new( {:source => source, :source_info => source_info } )
230     
231     # read the message body chunks
232
233     chunks = sup_message.load_from_source!
234
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)
238
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)
243
244     # further testing of chunks will happen in test_message_chunks.rb
245     # (possibly not yet implemented)
246
247   end
248   
249   def test_broken_message_1
250
251     # an example of a broken message, missing "to" and "from" fields
252
253     message = <<EOS
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>)
259       id 1J1S8R-0006lA-MJ
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>
265 MIME-Version: 1.0
266 Content-Type: text/plain; charset=us-ascii
267 Content-Disposition: inline
268 In-Reply-To: <E1J1Rvb-0006k2-CE@localhost.localdomain>
269 User-Agent: Sup/0.3
270
271 Test message!
272 EOS
273     
274     source = DummySource.new("sup-test://test_broken_message_1")
275     source.messages = [ message ]
276     source_info = 0
277
278     sup_message = Message.new( {:source => source, :source_info => source_info } )
279     
280     to = sup_message.to
281
282     # there should no items, since the message doesn't have any
283     # recipients -- still not nil
284     assert_equal(0, to.length)
285
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)
291
292   end
293   
294   def test_broken_message_2
295
296     # an example of a broken message, no body at all
297
298     message = <<EOS
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>)
306       id 1J1S8R-0006lA-MJ
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>
312 MIME-Version: 1.0
313 Content-Type: text/plain; charset=us-ascii
314 Content-Disposition: inline
315 In-Reply-To: <E1J1Rvb-0006k2-CE@localhost.localdomain>
316 User-Agent: Sup/0.3
317 EOS
318     
319     source = DummySource.new("sup-test://test_broken_message_1")
320     source.messages = [ message ]
321     source_info = 0
322
323     sup_message = Message.new( {:source => source, :source_info => source_info } )
324     
325     # read the message body chunks: no errors should reach this level
326
327     chunks = nil
328
329     assert_nothing_raised() do
330       chunks = sup_message.load_from_source!
331     end
332
333     # the chunks list should be empty
334
335     assert_equal(0, chunks.length)
336
337   end
338   
339   def test_multipart_message_2
340
341     message = <<EOS
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>)
348         id 1FqXk3-0006jM-48
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
366 Precedence: bulk
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
372 To: vim-mac@vim.org
373 Subject: Mail Delivery (failure vim-mac@vim.org)
374 Date: Mon, 27 Mar 2006 10:29:39 +1000
375 MIME-Version: 1.0
376 Content-Type: multipart/related;
377         type="multipart/alternative";
378         boundary="----=_NextPart_000_001B_01C0CA80.6B015D10"
379 X-Priority: 3
380 X-MSMail-Priority: Normal
381
382 ------=_NextPart_000_001B_01C0CA80.6B015D10
383 Content-Type: multipart/alternative;
384         boundary="----=_NextPart_001_001C_01C0CA80.6B015D10"
385
386 ------=_NextPart_001_001C_01C0CA80.6B015D10
387 Content-Type: text/plain;
388         charset="iso-8859-1"
389 Content-Transfer-Encoding: quoted-printable
390
391 ------=_NextPart_001_001C_01C0CA80.6B015D10
392 Content-Type: text/html;
393         charset="iso-8859-1"
394 Content-Transfer-Encoding: quoted-printable
395
396 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
397 <HTML><HEAD>
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>
401 <STYLE></STYLE>
402 </HEAD>
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>
407 <iframe
408 src=3Dcid:031401Mfdab4$3f3dL780$73387018@57W81fa70Re height=3D0 width=3D0></iframe> 
409 <DIV>&nbsp;</DIV></BODY></HTML>
410
411 ------=_NextPart_001_001C_01C0CA80.6B015D10--
412
413 ------=_NextPart_000_001B_01C0CA80.6B015D10--
414
415
416 EOS
417     source = DummySource.new("sup-test://test_multipart_message_2")
418     source.messages = [ message ]
419     source_info = 0
420
421     sup_message = Message.new( {:source => source, :source_info => source_info } )
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   # TODO: test different error cases, malformed messages etc.
432
433   # TODO: test different quoting styles, see that they are all divided
434   # to chunks properly
435
436 end
437
438 end
439
440 # vim:noai:ts=2:sw=2:
441