Fred Drake | 0aaed27 | 2000-10-23 13:39:15 +0000 | [diff] [blame] | 1 | import os |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2 | import sys |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 3 | import time |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4 | import stat |
| 5 | import socket |
| 6 | import email |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 7 | import email.message |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 8 | import re |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 9 | import io |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 10 | import tempfile |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 11 | from test import support |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 12 | import unittest |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 13 | import textwrap |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14 | import mailbox |
| 15 | import glob |
Guido van Rossum | 75ebb29 | 2001-04-10 15:01:20 +0000 | [diff] [blame] | 16 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 17 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 18 | class TestBase: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 19 | |
R David Murray | c988e24 | 2012-04-08 21:00:27 -0400 | [diff] [blame] | 20 | all_mailbox_types = (mailbox.Message, mailbox.MaildirMessage, |
| 21 | mailbox.mboxMessage, mailbox.MHMessage, |
| 22 | mailbox.BabylMessage, mailbox.MMDFMessage) |
| 23 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 24 | def _check_sample(self, msg): |
| 25 | # Inspect a mailbox.Message representation of the sample message |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 26 | self.assertIsInstance(msg, email.message.Message) |
| 27 | self.assertIsInstance(msg, mailbox.Message) |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 28 | for key, value in _sample_headers.items(): |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 29 | self.assertIn(value, msg.get_all(key)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 30 | self.assertTrue(msg.is_multipart()) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 31 | self.assertEqual(len(msg.get_payload()), len(_sample_payloads)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 32 | for i, payload in enumerate(_sample_payloads): |
| 33 | part = msg.get_payload(i) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 34 | self.assertIsInstance(part, email.message.Message) |
| 35 | self.assertNotIsInstance(part, mailbox.Message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 36 | self.assertEqual(part.get_payload(), payload) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 37 | |
| 38 | def _delete_recursively(self, target): |
| 39 | # Delete a file or delete a directory recursively |
| 40 | if os.path.isdir(target): |
R David Murray | b3e8384 | 2013-02-11 10:04:26 -0500 | [diff] [blame] | 41 | support.rmtree(target) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 42 | elif os.path.exists(target): |
R David Murray | b3e8384 | 2013-02-11 10:04:26 -0500 | [diff] [blame] | 43 | support.unlink(target) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | class TestMailbox(TestBase): |
| 47 | |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 48 | maxDiff = None |
| 49 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 50 | _factory = None # Overridden by subclasses to reuse tests |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 51 | _template = 'From: foo\n\n%s\n' |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 52 | |
| 53 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 54 | self._path = support.TESTFN |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 55 | self._delete_recursively(self._path) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 56 | self._box = self._factory(self._path) |
| 57 | |
| 58 | def tearDown(self): |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 59 | self._box.close() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 60 | self._delete_recursively(self._path) |
| 61 | |
| 62 | def test_add(self): |
| 63 | # Add copies of a sample message |
| 64 | keys = [] |
| 65 | keys.append(self._box.add(self._template % 0)) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 66 | self.assertEqual(len(self._box), 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 67 | keys.append(self._box.add(mailbox.Message(_sample_message))) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 68 | self.assertEqual(len(self._box), 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 69 | keys.append(self._box.add(email.message_from_string(_sample_message))) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 70 | self.assertEqual(len(self._box), 3) |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 71 | keys.append(self._box.add(io.BytesIO(_bytes_sample_message))) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 72 | self.assertEqual(len(self._box), 4) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 73 | keys.append(self._box.add(_sample_message)) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 74 | self.assertEqual(len(self._box), 5) |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 75 | keys.append(self._box.add(_bytes_sample_message)) |
| 76 | self.assertEqual(len(self._box), 6) |
| 77 | with self.assertWarns(DeprecationWarning): |
| 78 | keys.append(self._box.add( |
| 79 | io.TextIOWrapper(io.BytesIO(_bytes_sample_message)))) |
| 80 | self.assertEqual(len(self._box), 7) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 81 | self.assertEqual(self._box.get_string(keys[0]), self._template % 0) |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 82 | for i in (1, 2, 3, 4, 5, 6): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 83 | self._check_sample(self._box[keys[i]]) |
| 84 | |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 85 | _nonascii_msg = textwrap.dedent("""\ |
| 86 | From: foo |
| 87 | Subject: Falinaptár házhozszállítással. Már rendeltél? |
| 88 | |
| 89 | 0 |
| 90 | """) |
| 91 | |
| 92 | def test_add_invalid_8bit_bytes_header(self): |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 93 | key = self._box.add(self._nonascii_msg.encode('latin-1')) |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 94 | self.assertEqual(len(self._box), 1) |
| 95 | self.assertEqual(self._box.get_bytes(key), |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 96 | self._nonascii_msg.encode('latin-1')) |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 97 | |
| 98 | def test_invalid_nonascii_header_as_string(self): |
| 99 | subj = self._nonascii_msg.splitlines()[1] |
Marc-André Lemburg | 8f36af7 | 2011-02-25 15:42:01 +0000 | [diff] [blame] | 100 | key = self._box.add(subj.encode('latin-1')) |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 101 | self.assertEqual(self._box.get_string(key), |
| 102 | 'Subject: =?unknown-8bit?b?RmFsaW5hcHThciBo4Xpob3pzeuFsbO104XNz' |
| 103 | 'YWwuIE3hciByZW5kZWx06Ww/?=\n\n') |
| 104 | |
R. David Murray | fc14114 | 2011-02-11 22:47:17 +0000 | [diff] [blame] | 105 | def test_add_nonascii_string_header_raises(self): |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 106 | with self.assertRaisesRegex(ValueError, "ASCII-only"): |
| 107 | self._box.add(self._nonascii_msg) |
R. David Murray | fc14114 | 2011-02-11 22:47:17 +0000 | [diff] [blame] | 108 | self._box.flush() |
| 109 | self.assertEqual(len(self._box), 0) |
| 110 | self.assertMailboxEmpty() |
| 111 | |
| 112 | def test_add_that_raises_leaves_mailbox_empty(self): |
R David Murray | 7441a7a | 2012-03-14 02:59:51 -0400 | [diff] [blame] | 113 | def raiser(*args, **kw): |
| 114 | raise Exception("a fake error") |
| 115 | support.patch(self, email.generator.BytesGenerator, 'flatten', raiser) |
| 116 | with self.assertRaises(Exception): |
R. David Murray | fc14114 | 2011-02-11 22:47:17 +0000 | [diff] [blame] | 117 | self._box.add(email.message_from_string("From: Alphöso")) |
| 118 | self.assertEqual(len(self._box), 0) |
| 119 | self._box.close() |
| 120 | self.assertMailboxEmpty() |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 121 | |
| 122 | _non_latin_bin_msg = textwrap.dedent("""\ |
| 123 | From: foo@bar.com |
| 124 | To: báz |
| 125 | Subject: Maintenant je vous présente mon collègue, le pouf célèbre |
| 126 | \tJean de Baddie |
| 127 | Mime-Version: 1.0 |
| 128 | Content-Type: text/plain; charset="utf-8" |
| 129 | Content-Transfer-Encoding: 8bit |
| 130 | |
| 131 | Да, они летят. |
| 132 | """).encode('utf-8') |
| 133 | |
| 134 | def test_add_8bit_body(self): |
| 135 | key = self._box.add(self._non_latin_bin_msg) |
| 136 | self.assertEqual(self._box.get_bytes(key), |
| 137 | self._non_latin_bin_msg) |
| 138 | with self._box.get_file(key) as f: |
| 139 | self.assertEqual(f.read(), |
| 140 | self._non_latin_bin_msg.replace(b'\n', |
| 141 | os.linesep.encode())) |
| 142 | self.assertEqual(self._box[key].get_payload(), |
| 143 | "Да, они летят.\n") |
| 144 | |
| 145 | def test_add_binary_file(self): |
| 146 | with tempfile.TemporaryFile('wb+') as f: |
| 147 | f.write(_bytes_sample_message) |
| 148 | f.seek(0) |
| 149 | key = self._box.add(f) |
Petri Lehtinen | 3d12c43 | 2012-08-15 14:00:40 +0300 | [diff] [blame] | 150 | self.assertEqual(self._box.get_bytes(key).split(b'\n'), |
| 151 | _bytes_sample_message.split(b'\n')) |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 152 | |
| 153 | def test_add_binary_nonascii_file(self): |
| 154 | with tempfile.TemporaryFile('wb+') as f: |
| 155 | f.write(self._non_latin_bin_msg) |
| 156 | f.seek(0) |
| 157 | key = self._box.add(f) |
Petri Lehtinen | 3d12c43 | 2012-08-15 14:00:40 +0300 | [diff] [blame] | 158 | self.assertEqual(self._box.get_bytes(key).split(b'\n'), |
| 159 | self._non_latin_bin_msg.split(b'\n')) |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 160 | |
| 161 | def test_add_text_file_warns(self): |
| 162 | with tempfile.TemporaryFile('w+') as f: |
| 163 | f.write(_sample_message) |
| 164 | f.seek(0) |
| 165 | with self.assertWarns(DeprecationWarning): |
| 166 | key = self._box.add(f) |
Petri Lehtinen | 3d12c43 | 2012-08-15 14:00:40 +0300 | [diff] [blame] | 167 | self.assertEqual(self._box.get_bytes(key).split(b'\n'), |
| 168 | _bytes_sample_message.split(b'\n')) |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 169 | |
| 170 | def test_add_StringIO_warns(self): |
| 171 | with self.assertWarns(DeprecationWarning): |
| 172 | key = self._box.add(io.StringIO(self._template % "0")) |
| 173 | self.assertEqual(self._box.get_string(key), self._template % "0") |
| 174 | |
| 175 | def test_add_nonascii_StringIO_raises(self): |
| 176 | with self.assertWarns(DeprecationWarning): |
| 177 | with self.assertRaisesRegex(ValueError, "ASCII-only"): |
| 178 | self._box.add(io.StringIO(self._nonascii_msg)) |
R. David Murray | fc14114 | 2011-02-11 22:47:17 +0000 | [diff] [blame] | 179 | self.assertEqual(len(self._box), 0) |
| 180 | self._box.close() |
| 181 | self.assertMailboxEmpty() |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 182 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 183 | def test_remove(self): |
| 184 | # Remove messages using remove() |
| 185 | self._test_remove_or_delitem(self._box.remove) |
| 186 | |
| 187 | def test_delitem(self): |
| 188 | # Remove messages using __delitem__() |
| 189 | self._test_remove_or_delitem(self._box.__delitem__) |
| 190 | |
| 191 | def _test_remove_or_delitem(self, method): |
| 192 | # (Used by test_remove() and test_delitem().) |
| 193 | key0 = self._box.add(self._template % 0) |
| 194 | key1 = self._box.add(self._template % 1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 195 | self.assertEqual(len(self._box), 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 196 | method(key0) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 197 | self.assertEqual(len(self._box), 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 198 | self.assertRaises(KeyError, lambda: self._box[key0]) |
| 199 | self.assertRaises(KeyError, lambda: method(key0)) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 200 | self.assertEqual(self._box.get_string(key1), self._template % 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 201 | key2 = self._box.add(self._template % 2) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 202 | self.assertEqual(len(self._box), 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 203 | method(key2) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 204 | self.assertEqual(len(self._box), 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 205 | self.assertRaises(KeyError, lambda: self._box[key2]) |
| 206 | self.assertRaises(KeyError, lambda: method(key2)) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 207 | self.assertEqual(self._box.get_string(key1), self._template % 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 208 | method(key1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 209 | self.assertEqual(len(self._box), 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 210 | self.assertRaises(KeyError, lambda: self._box[key1]) |
| 211 | self.assertRaises(KeyError, lambda: method(key1)) |
| 212 | |
| 213 | def test_discard(self, repetitions=10): |
| 214 | # Discard messages |
| 215 | key0 = self._box.add(self._template % 0) |
| 216 | key1 = self._box.add(self._template % 1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 217 | self.assertEqual(len(self._box), 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 218 | self._box.discard(key0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 219 | self.assertEqual(len(self._box), 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 220 | self.assertRaises(KeyError, lambda: self._box[key0]) |
| 221 | self._box.discard(key0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 222 | self.assertEqual(len(self._box), 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 223 | self.assertRaises(KeyError, lambda: self._box[key0]) |
| 224 | |
| 225 | def test_get(self): |
| 226 | # Retrieve messages using get() |
| 227 | key0 = self._box.add(self._template % 0) |
| 228 | msg = self._box.get(key0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 229 | self.assertEqual(msg['from'], 'foo') |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 230 | self.assertEqual(msg.get_payload(), '0\n') |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 231 | self.assertIsNone(self._box.get('foo')) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 232 | self.assertIs(self._box.get('foo', False), False) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 233 | self._box.close() |
Georg Brandl | fb16cf1 | 2008-05-11 21:31:44 +0000 | [diff] [blame] | 234 | self._box = self._factory(self._path) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 235 | key1 = self._box.add(self._template % 1) |
| 236 | msg = self._box.get(key1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 237 | self.assertEqual(msg['from'], 'foo') |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 238 | self.assertEqual(msg.get_payload(), '1\n') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 239 | |
| 240 | def test_getitem(self): |
| 241 | # Retrieve message using __getitem__() |
| 242 | key0 = self._box.add(self._template % 0) |
| 243 | msg = self._box[key0] |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 244 | self.assertEqual(msg['from'], 'foo') |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 245 | self.assertEqual(msg.get_payload(), '0\n') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 246 | self.assertRaises(KeyError, lambda: self._box['foo']) |
| 247 | self._box.discard(key0) |
| 248 | self.assertRaises(KeyError, lambda: self._box[key0]) |
| 249 | |
| 250 | def test_get_message(self): |
| 251 | # Get Message representations of messages |
| 252 | key0 = self._box.add(self._template % 0) |
| 253 | key1 = self._box.add(_sample_message) |
| 254 | msg0 = self._box.get_message(key0) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 255 | self.assertIsInstance(msg0, mailbox.Message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 256 | self.assertEqual(msg0['from'], 'foo') |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 257 | self.assertEqual(msg0.get_payload(), '0\n') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 258 | self._check_sample(self._box.get_message(key1)) |
| 259 | |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 260 | def test_get_bytes(self): |
| 261 | # Get bytes representations of messages |
| 262 | key0 = self._box.add(self._template % 0) |
| 263 | key1 = self._box.add(_sample_message) |
| 264 | self.assertEqual(self._box.get_bytes(key0), |
| 265 | (self._template % 0).encode('ascii')) |
| 266 | self.assertEqual(self._box.get_bytes(key1), _bytes_sample_message) |
| 267 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 268 | def test_get_string(self): |
| 269 | # Get string representations of messages |
| 270 | key0 = self._box.add(self._template % 0) |
| 271 | key1 = self._box.add(_sample_message) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 272 | self.assertEqual(self._box.get_string(key0), self._template % 0) |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 273 | self.assertEqual(self._box.get_string(key1).split('\n'), |
| 274 | _sample_message.split('\n')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 275 | |
| 276 | def test_get_file(self): |
| 277 | # Get file representations of messages |
| 278 | key0 = self._box.add(self._template % 0) |
| 279 | key1 = self._box.add(_sample_message) |
Brett Cannon | 653238a | 2010-10-30 00:13:00 +0000 | [diff] [blame] | 280 | with self._box.get_file(key0) as file: |
| 281 | data0 = file.read() |
| 282 | with self._box.get_file(key1) as file: |
| 283 | data1 = file.read() |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 284 | self.assertEqual(data0.decode('ascii').replace(os.linesep, '\n'), |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 285 | self._template % 0) |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 286 | self.assertEqual(data1.decode('ascii').replace(os.linesep, '\n'), |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 287 | _sample_message) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 288 | |
R David Murray | c88bce1 | 2011-06-17 22:24:05 -0400 | [diff] [blame] | 289 | def test_get_file_can_be_closed_twice(self): |
| 290 | # Issue 11700 |
| 291 | key = self._box.add(_sample_message) |
| 292 | f = self._box.get_file(key) |
| 293 | f.close() |
| 294 | f.close() |
| 295 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 296 | def test_iterkeys(self): |
| 297 | # Get keys using iterkeys() |
Serhiy Storchaka | cd3aacf5 | 2014-08-13 09:35:21 +0300 | [diff] [blame] | 298 | self._check_iteration(self._box.iterkeys, do_keys=True, do_values=False) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 299 | |
| 300 | def test_keys(self): |
| 301 | # Get keys using keys() |
| 302 | self._check_iteration(self._box.keys, do_keys=True, do_values=False) |
| 303 | |
| 304 | def test_itervalues(self): |
| 305 | # Get values using itervalues() |
Serhiy Storchaka | cd3aacf5 | 2014-08-13 09:35:21 +0300 | [diff] [blame] | 306 | self._check_iteration(self._box.itervalues, do_keys=False, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 307 | do_values=True) |
| 308 | |
| 309 | def test_iter(self): |
| 310 | # Get values using __iter__() |
| 311 | self._check_iteration(self._box.__iter__, do_keys=False, |
| 312 | do_values=True) |
| 313 | |
| 314 | def test_values(self): |
| 315 | # Get values using values() |
| 316 | self._check_iteration(self._box.values, do_keys=False, do_values=True) |
| 317 | |
| 318 | def test_iteritems(self): |
| 319 | # Get keys and values using iteritems() |
Serhiy Storchaka | cd3aacf5 | 2014-08-13 09:35:21 +0300 | [diff] [blame] | 320 | self._check_iteration(self._box.iteritems, do_keys=True, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 321 | do_values=True) |
| 322 | |
| 323 | def test_items(self): |
| 324 | # Get keys and values using items() |
| 325 | self._check_iteration(self._box.items, do_keys=True, do_values=True) |
| 326 | |
| 327 | def _check_iteration(self, method, do_keys, do_values, repetitions=10): |
| 328 | for value in method(): |
| 329 | self.fail("Not empty") |
| 330 | keys, values = [], [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 331 | for i in range(repetitions): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 332 | keys.append(self._box.add(self._template % i)) |
| 333 | values.append(self._template % i) |
| 334 | if do_keys and not do_values: |
| 335 | returned_keys = list(method()) |
| 336 | elif do_values and not do_keys: |
| 337 | returned_values = list(method()) |
| 338 | else: |
| 339 | returned_keys, returned_values = [], [] |
| 340 | for key, value in method(): |
| 341 | returned_keys.append(key) |
| 342 | returned_values.append(value) |
| 343 | if do_keys: |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 344 | self.assertEqual(len(keys), len(returned_keys)) |
| 345 | self.assertEqual(set(keys), set(returned_keys)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 346 | if do_values: |
| 347 | count = 0 |
| 348 | for value in returned_values: |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 349 | self.assertEqual(value['from'], 'foo') |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 350 | self.assertLess(int(value.get_payload()), repetitions) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 351 | count += 1 |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 352 | self.assertEqual(len(values), count) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 353 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 354 | def test_contains(self): |
| 355 | # Check existence of keys using __contains__() |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 356 | self.assertNotIn('foo', self._box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 357 | key0 = self._box.add(self._template % 0) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 358 | self.assertIn(key0, self._box) |
| 359 | self.assertNotIn('foo', self._box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 360 | key1 = self._box.add(self._template % 1) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 361 | self.assertIn(key1, self._box) |
| 362 | self.assertIn(key0, self._box) |
| 363 | self.assertNotIn('foo', self._box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 364 | self._box.remove(key0) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 365 | self.assertNotIn(key0, self._box) |
| 366 | self.assertIn(key1, self._box) |
| 367 | self.assertNotIn('foo', self._box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 368 | self._box.remove(key1) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 369 | self.assertNotIn(key1, self._box) |
| 370 | self.assertNotIn(key0, self._box) |
| 371 | self.assertNotIn('foo', self._box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 372 | |
| 373 | def test_len(self, repetitions=10): |
| 374 | # Get message count |
| 375 | keys = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 376 | for i in range(repetitions): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 377 | self.assertEqual(len(self._box), i) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 378 | keys.append(self._box.add(self._template % i)) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 379 | self.assertEqual(len(self._box), i + 1) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 380 | for i in range(repetitions): |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 381 | self.assertEqual(len(self._box), repetitions - i) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 382 | self._box.remove(keys[i]) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 383 | self.assertEqual(len(self._box), repetitions - i - 1) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 384 | |
| 385 | def test_set_item(self): |
| 386 | # Modify messages using __setitem__() |
| 387 | key0 = self._box.add(self._template % 'original 0') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 388 | self.assertEqual(self._box.get_string(key0), |
| 389 | self._template % 'original 0') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 390 | key1 = self._box.add(self._template % 'original 1') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 391 | self.assertEqual(self._box.get_string(key1), |
| 392 | self._template % 'original 1') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 393 | self._box[key0] = self._template % 'changed 0' |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 394 | self.assertEqual(self._box.get_string(key0), |
| 395 | self._template % 'changed 0') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 396 | self._box[key1] = self._template % 'changed 1' |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 397 | self.assertEqual(self._box.get_string(key1), |
| 398 | self._template % 'changed 1') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 399 | self._box[key0] = _sample_message |
| 400 | self._check_sample(self._box[key0]) |
| 401 | self._box[key1] = self._box[key0] |
| 402 | self._check_sample(self._box[key1]) |
| 403 | self._box[key0] = self._template % 'original 0' |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 404 | self.assertEqual(self._box.get_string(key0), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 405 | self._template % 'original 0') |
| 406 | self._check_sample(self._box[key1]) |
| 407 | self.assertRaises(KeyError, |
| 408 | lambda: self._box.__setitem__('foo', 'bar')) |
| 409 | self.assertRaises(KeyError, lambda: self._box['foo']) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 410 | self.assertEqual(len(self._box), 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 411 | |
| 412 | def test_clear(self, iterations=10): |
| 413 | # Remove all messages using clear() |
| 414 | keys = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 415 | for i in range(iterations): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 416 | self._box.add(self._template % i) |
| 417 | for i, key in enumerate(keys): |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 418 | self.assertEqual(self._box.get_string(key), self._template % i) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 419 | self._box.clear() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 420 | self.assertEqual(len(self._box), 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 421 | for i, key in enumerate(keys): |
| 422 | self.assertRaises(KeyError, lambda: self._box.get_string(key)) |
| 423 | |
| 424 | def test_pop(self): |
| 425 | # Get and remove a message using pop() |
| 426 | key0 = self._box.add(self._template % 0) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 427 | self.assertIn(key0, self._box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 428 | key1 = self._box.add(self._template % 1) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 429 | self.assertIn(key1, self._box) |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 430 | self.assertEqual(self._box.pop(key0).get_payload(), '0\n') |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 431 | self.assertNotIn(key0, self._box) |
| 432 | self.assertIn(key1, self._box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 433 | key2 = self._box.add(self._template % 2) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 434 | self.assertIn(key2, self._box) |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 435 | self.assertEqual(self._box.pop(key2).get_payload(), '2\n') |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 436 | self.assertNotIn(key2, self._box) |
| 437 | self.assertIn(key1, self._box) |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 438 | self.assertEqual(self._box.pop(key1).get_payload(), '1\n') |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 439 | self.assertNotIn(key1, self._box) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 440 | self.assertEqual(len(self._box), 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 441 | |
| 442 | def test_popitem(self, iterations=10): |
| 443 | # Get and remove an arbitrary (key, message) using popitem() |
| 444 | keys = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 445 | for i in range(10): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 446 | keys.append(self._box.add(self._template % i)) |
| 447 | seen = [] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 448 | for i in range(10): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 449 | key, msg = self._box.popitem() |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 450 | self.assertIn(key, keys) |
| 451 | self.assertNotIn(key, seen) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 452 | seen.append(key) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 453 | self.assertEqual(int(msg.get_payload()), keys.index(key)) |
| 454 | self.assertEqual(len(self._box), 0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 455 | for key in keys: |
| 456 | self.assertRaises(KeyError, lambda: self._box[key]) |
| 457 | |
| 458 | def test_update(self): |
| 459 | # Modify multiple messages using update() |
| 460 | key0 = self._box.add(self._template % 'original 0') |
| 461 | key1 = self._box.add(self._template % 'original 1') |
| 462 | key2 = self._box.add(self._template % 'original 2') |
| 463 | self._box.update({key0: self._template % 'changed 0', |
| 464 | key2: _sample_message}) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 465 | self.assertEqual(len(self._box), 3) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 466 | self.assertEqual(self._box.get_string(key0), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 467 | self._template % 'changed 0') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 468 | self.assertEqual(self._box.get_string(key1), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 469 | self._template % 'original 1') |
| 470 | self._check_sample(self._box[key2]) |
| 471 | self._box.update([(key2, self._template % 'changed 2'), |
| 472 | (key1, self._template % 'changed 1'), |
| 473 | (key0, self._template % 'original 0')]) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 474 | self.assertEqual(len(self._box), 3) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 475 | self.assertEqual(self._box.get_string(key0), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 476 | self._template % 'original 0') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 477 | self.assertEqual(self._box.get_string(key1), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 478 | self._template % 'changed 1') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 479 | self.assertEqual(self._box.get_string(key2), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 480 | self._template % 'changed 2') |
| 481 | self.assertRaises(KeyError, |
| 482 | lambda: self._box.update({'foo': 'bar', |
| 483 | key0: self._template % "changed 0"})) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 484 | self.assertEqual(len(self._box), 3) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 485 | self.assertEqual(self._box.get_string(key0), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 486 | self._template % "changed 0") |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 487 | self.assertEqual(self._box.get_string(key1), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 488 | self._template % "changed 1") |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 489 | self.assertEqual(self._box.get_string(key2), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 490 | self._template % "changed 2") |
| 491 | |
| 492 | def test_flush(self): |
| 493 | # Write changes to disk |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 494 | self._test_flush_or_close(self._box.flush, True) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 495 | |
Petri Lehtinen | 02653f1 | 2012-06-15 20:50:51 +0300 | [diff] [blame] | 496 | def test_popitem_and_flush_twice(self): |
| 497 | # See #15036. |
| 498 | self._box.add(self._template % 0) |
| 499 | self._box.add(self._template % 1) |
| 500 | self._box.flush() |
| 501 | |
| 502 | self._box.popitem() |
| 503 | self._box.flush() |
| 504 | self._box.popitem() |
| 505 | self._box.flush() |
| 506 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 507 | def test_lock_unlock(self): |
| 508 | # Lock and unlock the mailbox |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 509 | self.assertFalse(os.path.exists(self._get_lock_path())) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 510 | self._box.lock() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 511 | self.assertTrue(os.path.exists(self._get_lock_path())) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 512 | self._box.unlock() |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 513 | self.assertFalse(os.path.exists(self._get_lock_path())) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 514 | |
| 515 | def test_close(self): |
| 516 | # Close mailbox and flush changes to disk |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 517 | self._test_flush_or_close(self._box.close, False) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 518 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 519 | def _test_flush_or_close(self, method, should_call_close): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 520 | contents = [self._template % i for i in range(3)] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 521 | self._box.add(contents[0]) |
| 522 | self._box.add(contents[1]) |
| 523 | self._box.add(contents[2]) |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 524 | oldbox = self._box |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 525 | method() |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 526 | if should_call_close: |
| 527 | self._box.close() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 528 | self._box = self._factory(self._path) |
| 529 | keys = self._box.keys() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 530 | self.assertEqual(len(keys), 3) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 531 | for key in keys: |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 532 | self.assertIn(self._box.get_string(key), contents) |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 533 | oldbox.close() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 534 | |
| 535 | def test_dump_message(self): |
| 536 | # Write message representations to disk |
| 537 | for input in (email.message_from_string(_sample_message), |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 538 | _sample_message, io.BytesIO(_bytes_sample_message)): |
| 539 | output = io.BytesIO() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 540 | self._box._dump_message(input, output) |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 541 | self.assertEqual(output.getvalue(), |
| 542 | _bytes_sample_message.replace(b'\n', os.linesep.encode())) |
| 543 | output = io.BytesIO() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 544 | self.assertRaises(TypeError, |
| 545 | lambda: self._box._dump_message(None, output)) |
| 546 | |
| 547 | def _get_lock_path(self): |
| 548 | # Return the path of the dot lock file. May be overridden. |
| 549 | return self._path + '.lock' |
| 550 | |
| 551 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 552 | class TestMailboxSuperclass(TestBase, unittest.TestCase): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 553 | |
| 554 | def test_notimplemented(self): |
| 555 | # Test that all Mailbox methods raise NotImplementedException. |
| 556 | box = mailbox.Mailbox('path') |
| 557 | self.assertRaises(NotImplementedError, lambda: box.add('')) |
| 558 | self.assertRaises(NotImplementedError, lambda: box.remove('')) |
| 559 | self.assertRaises(NotImplementedError, lambda: box.__delitem__('')) |
| 560 | self.assertRaises(NotImplementedError, lambda: box.discard('')) |
| 561 | self.assertRaises(NotImplementedError, lambda: box.__setitem__('', '')) |
Serhiy Storchaka | cd3aacf5 | 2014-08-13 09:35:21 +0300 | [diff] [blame] | 562 | self.assertRaises(NotImplementedError, lambda: box.iterkeys()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 563 | self.assertRaises(NotImplementedError, lambda: box.keys()) |
Serhiy Storchaka | cd3aacf5 | 2014-08-13 09:35:21 +0300 | [diff] [blame] | 564 | self.assertRaises(NotImplementedError, lambda: box.itervalues().__next__()) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 565 | self.assertRaises(NotImplementedError, lambda: box.__iter__().__next__()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 566 | self.assertRaises(NotImplementedError, lambda: box.values()) |
Serhiy Storchaka | cd3aacf5 | 2014-08-13 09:35:21 +0300 | [diff] [blame] | 567 | self.assertRaises(NotImplementedError, lambda: box.iteritems().__next__()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 568 | self.assertRaises(NotImplementedError, lambda: box.items()) |
| 569 | self.assertRaises(NotImplementedError, lambda: box.get('')) |
| 570 | self.assertRaises(NotImplementedError, lambda: box.__getitem__('')) |
| 571 | self.assertRaises(NotImplementedError, lambda: box.get_message('')) |
| 572 | self.assertRaises(NotImplementedError, lambda: box.get_string('')) |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 573 | self.assertRaises(NotImplementedError, lambda: box.get_bytes('')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 574 | self.assertRaises(NotImplementedError, lambda: box.get_file('')) |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 575 | self.assertRaises(NotImplementedError, lambda: '' in box) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 576 | self.assertRaises(NotImplementedError, lambda: box.__contains__('')) |
| 577 | self.assertRaises(NotImplementedError, lambda: box.__len__()) |
| 578 | self.assertRaises(NotImplementedError, lambda: box.clear()) |
| 579 | self.assertRaises(NotImplementedError, lambda: box.pop('')) |
| 580 | self.assertRaises(NotImplementedError, lambda: box.popitem()) |
| 581 | self.assertRaises(NotImplementedError, lambda: box.update((('', ''),))) |
| 582 | self.assertRaises(NotImplementedError, lambda: box.flush()) |
| 583 | self.assertRaises(NotImplementedError, lambda: box.lock()) |
| 584 | self.assertRaises(NotImplementedError, lambda: box.unlock()) |
| 585 | self.assertRaises(NotImplementedError, lambda: box.close()) |
| 586 | |
| 587 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 588 | class TestMaildir(TestMailbox, unittest.TestCase): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 589 | |
| 590 | _factory = lambda self, path, factory=None: mailbox.Maildir(path, factory) |
| 591 | |
| 592 | def setUp(self): |
| 593 | TestMailbox.setUp(self) |
Jesus Cea | f01d695 | 2012-10-05 03:36:11 +0200 | [diff] [blame] | 594 | if (os.name == 'nt') or (sys.platform == 'cygwin'): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 595 | self._box.colon = '!' |
| 596 | |
R. David Murray | fc14114 | 2011-02-11 22:47:17 +0000 | [diff] [blame] | 597 | def assertMailboxEmpty(self): |
| 598 | self.assertEqual(os.listdir(os.path.join(self._path, 'tmp')), []) |
| 599 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 600 | def test_add_MM(self): |
| 601 | # Add a MaildirMessage instance |
| 602 | msg = mailbox.MaildirMessage(self._template % 0) |
| 603 | msg.set_subdir('cur') |
| 604 | msg.set_info('foo') |
| 605 | key = self._box.add(msg) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 606 | self.assertTrue(os.path.exists(os.path.join(self._path, 'cur', '%s%sfoo' % |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 607 | (key, self._box.colon)))) |
| 608 | |
| 609 | def test_get_MM(self): |
| 610 | # Get a MaildirMessage instance |
| 611 | msg = mailbox.MaildirMessage(self._template % 0) |
| 612 | msg.set_subdir('cur') |
| 613 | msg.set_flags('RF') |
| 614 | key = self._box.add(msg) |
| 615 | msg_returned = self._box.get_message(key) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 616 | self.assertIsInstance(msg_returned, mailbox.MaildirMessage) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 617 | self.assertEqual(msg_returned.get_subdir(), 'cur') |
| 618 | self.assertEqual(msg_returned.get_flags(), 'FR') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 619 | |
| 620 | def test_set_MM(self): |
| 621 | # Set with a MaildirMessage instance |
| 622 | msg0 = mailbox.MaildirMessage(self._template % 0) |
| 623 | msg0.set_flags('TP') |
| 624 | key = self._box.add(msg0) |
| 625 | msg_returned = self._box.get_message(key) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 626 | self.assertEqual(msg_returned.get_subdir(), 'new') |
| 627 | self.assertEqual(msg_returned.get_flags(), 'PT') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 628 | msg1 = mailbox.MaildirMessage(self._template % 1) |
| 629 | self._box[key] = msg1 |
| 630 | msg_returned = self._box.get_message(key) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 631 | self.assertEqual(msg_returned.get_subdir(), 'new') |
| 632 | self.assertEqual(msg_returned.get_flags(), '') |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 633 | self.assertEqual(msg_returned.get_payload(), '1\n') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 634 | msg2 = mailbox.MaildirMessage(self._template % 2) |
| 635 | msg2.set_info('2,S') |
| 636 | self._box[key] = msg2 |
| 637 | self._box[key] = self._template % 3 |
| 638 | msg_returned = self._box.get_message(key) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 639 | self.assertEqual(msg_returned.get_subdir(), 'new') |
| 640 | self.assertEqual(msg_returned.get_flags(), 'S') |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 641 | self.assertEqual(msg_returned.get_payload(), '3\n') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 642 | |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 643 | def test_consistent_factory(self): |
| 644 | # Add a message. |
| 645 | msg = mailbox.MaildirMessage(self._template % 0) |
| 646 | msg.set_subdir('cur') |
| 647 | msg.set_flags('RF') |
| 648 | key = self._box.add(msg) |
| 649 | |
| 650 | # Create new mailbox with |
| 651 | class FakeMessage(mailbox.MaildirMessage): |
| 652 | pass |
| 653 | box = mailbox.Maildir(self._path, factory=FakeMessage) |
Christian Heimes | 15ebc88 | 2008-02-04 18:48:49 +0000 | [diff] [blame] | 654 | box.colon = self._box.colon |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 655 | msg2 = box.get_message(key) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 656 | self.assertIsInstance(msg2, FakeMessage) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 657 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 658 | def test_initialize_new(self): |
| 659 | # Initialize a non-existent mailbox |
| 660 | self.tearDown() |
| 661 | self._box = mailbox.Maildir(self._path) |
Georg Brandl | fb16cf1 | 2008-05-11 21:31:44 +0000 | [diff] [blame] | 662 | self._check_basics() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 663 | self._delete_recursively(self._path) |
| 664 | self._box = self._factory(self._path, factory=None) |
| 665 | self._check_basics() |
| 666 | |
| 667 | def test_initialize_existing(self): |
| 668 | # Initialize an existing mailbox |
| 669 | self.tearDown() |
| 670 | for subdir in '', 'tmp', 'new', 'cur': |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 671 | os.mkdir(os.path.normpath(os.path.join(self._path, subdir))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 672 | self._box = mailbox.Maildir(self._path) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 673 | self._check_basics() |
| 674 | |
| 675 | def _check_basics(self, factory=None): |
| 676 | # (Used by test_open_new() and test_open_existing().) |
| 677 | self.assertEqual(self._box._path, os.path.abspath(self._path)) |
| 678 | self.assertEqual(self._box._factory, factory) |
| 679 | for subdir in '', 'tmp', 'new', 'cur': |
| 680 | path = os.path.join(self._path, subdir) |
| 681 | mode = os.stat(path)[stat.ST_MODE] |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 682 | self.assertTrue(stat.S_ISDIR(mode), "Not a directory: '%s'" % path) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 683 | |
| 684 | def test_list_folders(self): |
| 685 | # List folders |
| 686 | self._box.add_folder('one') |
| 687 | self._box.add_folder('two') |
| 688 | self._box.add_folder('three') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 689 | self.assertEqual(len(self._box.list_folders()), 3) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 690 | self.assertEqual(set(self._box.list_folders()), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 691 | set(('one', 'two', 'three'))) |
| 692 | |
| 693 | def test_get_folder(self): |
| 694 | # Open folders |
| 695 | self._box.add_folder('foo.bar') |
| 696 | folder0 = self._box.get_folder('foo.bar') |
| 697 | folder0.add(self._template % 'bar') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 698 | self.assertTrue(os.path.isdir(os.path.join(self._path, '.foo.bar'))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 699 | folder1 = self._box.get_folder('foo.bar') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 700 | self.assertEqual(folder1.get_string(folder1.keys()[0]), |
| 701 | self._template % 'bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 702 | |
| 703 | def test_add_and_remove_folders(self): |
| 704 | # Delete folders |
| 705 | self._box.add_folder('one') |
| 706 | self._box.add_folder('two') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 707 | self.assertEqual(len(self._box.list_folders()), 2) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 708 | self.assertEqual(set(self._box.list_folders()), set(('one', 'two'))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 709 | self._box.remove_folder('one') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 710 | self.assertEqual(len(self._box.list_folders()), 1) |
| 711 | self.assertEqual(set(self._box.list_folders()), set(('two',))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 712 | self._box.add_folder('three') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 713 | self.assertEqual(len(self._box.list_folders()), 2) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 714 | self.assertEqual(set(self._box.list_folders()), set(('two', 'three'))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 715 | self._box.remove_folder('three') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 716 | self.assertEqual(len(self._box.list_folders()), 1) |
| 717 | self.assertEqual(set(self._box.list_folders()), set(('two',))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 718 | self._box.remove_folder('two') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 719 | self.assertEqual(len(self._box.list_folders()), 0) |
| 720 | self.assertEqual(self._box.list_folders(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 721 | |
| 722 | def test_clean(self): |
| 723 | # Remove old files from 'tmp' |
| 724 | foo_path = os.path.join(self._path, 'tmp', 'foo') |
| 725 | bar_path = os.path.join(self._path, 'tmp', 'bar') |
Brian Curtin | 5acec04 | 2010-10-13 02:29:46 +0000 | [diff] [blame] | 726 | with open(foo_path, 'w') as f: |
| 727 | f.write("@") |
| 728 | with open(bar_path, 'w') as f: |
| 729 | f.write("@") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 730 | self._box.clean() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 731 | self.assertTrue(os.path.exists(foo_path)) |
| 732 | self.assertTrue(os.path.exists(bar_path)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 733 | foo_stat = os.stat(foo_path) |
| 734 | os.utime(foo_path, (time.time() - 129600 - 2, |
| 735 | foo_stat.st_mtime)) |
| 736 | self._box.clean() |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 737 | self.assertFalse(os.path.exists(foo_path)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 738 | self.assertTrue(os.path.exists(bar_path)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 739 | |
| 740 | def test_create_tmp(self, repetitions=10): |
| 741 | # Create files in tmp directory |
| 742 | hostname = socket.gethostname() |
| 743 | if '/' in hostname: |
| 744 | hostname = hostname.replace('/', r'\057') |
| 745 | if ':' in hostname: |
| 746 | hostname = hostname.replace(':', r'\072') |
| 747 | pid = os.getpid() |
| 748 | pattern = re.compile(r"(?P<time>\d+)\.M(?P<M>\d{1,6})P(?P<P>\d+)" |
Serhiy Storchaka | f4ea642 | 2017-09-30 17:10:57 +0300 | [diff] [blame] | 749 | r"Q(?P<Q>\d+)\.(?P<host>[^:/]*)") |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 750 | previous_groups = None |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 751 | for x in range(repetitions): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 752 | tmp_file = self._box._create_tmp() |
| 753 | head, tail = os.path.split(tmp_file.name) |
| 754 | self.assertEqual(head, os.path.abspath(os.path.join(self._path, |
| 755 | "tmp")), |
| 756 | "File in wrong location: '%s'" % head) |
| 757 | match = pattern.match(tail) |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 758 | self.assertIsNotNone(match, "Invalid file name: '%s'" % tail) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 759 | groups = match.groups() |
Benjamin Peterson | 2a691a8 | 2008-03-31 01:51:45 +0000 | [diff] [blame] | 760 | if previous_groups is not None: |
Petri Lehtinen | 1c371b2 | 2012-09-01 14:22:36 +0300 | [diff] [blame] | 761 | self.assertGreaterEqual(int(groups[0]), int(previous_groups[0]), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 762 | "Non-monotonic seconds: '%s' before '%s'" % |
| 763 | (previous_groups[0], groups[0])) |
Petri Lehtinen | 1c371b2 | 2012-09-01 14:22:36 +0300 | [diff] [blame] | 764 | if int(groups[0]) == int(previous_groups[0]): |
| 765 | self.assertGreaterEqual(int(groups[1]), int(previous_groups[1]), |
| 766 | "Non-monotonic milliseconds: '%s' before '%s'" % |
| 767 | (previous_groups[1], groups[1])) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 768 | self.assertEqual(int(groups[2]), pid, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 769 | "Process ID mismatch: '%s' should be '%s'" % |
| 770 | (groups[2], pid)) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 771 | self.assertEqual(int(groups[3]), int(previous_groups[3]) + 1, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 772 | "Non-sequential counter: '%s' before '%s'" % |
| 773 | (previous_groups[3], groups[3])) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 774 | self.assertEqual(groups[4], hostname, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 775 | "Host name mismatch: '%s' should be '%s'" % |
| 776 | (groups[4], hostname)) |
| 777 | previous_groups = groups |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 778 | tmp_file.write(_bytes_sample_message) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 779 | tmp_file.seek(0) |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 780 | self.assertEqual(tmp_file.read(), _bytes_sample_message) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 781 | tmp_file.close() |
| 782 | file_count = len(os.listdir(os.path.join(self._path, "tmp"))) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 783 | self.assertEqual(file_count, repetitions, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 784 | "Wrong file count: '%s' should be '%s'" % |
| 785 | (file_count, repetitions)) |
| 786 | |
| 787 | def test_refresh(self): |
| 788 | # Update the table of contents |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 789 | self.assertEqual(self._box._toc, {}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 790 | key0 = self._box.add(self._template % 0) |
| 791 | key1 = self._box.add(self._template % 1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 792 | self.assertEqual(self._box._toc, {}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 793 | self._box._refresh() |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 794 | self.assertEqual(self._box._toc, {key0: os.path.join('new', key0), |
| 795 | key1: os.path.join('new', key1)}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 796 | key2 = self._box.add(self._template % 2) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 797 | self.assertEqual(self._box._toc, {key0: os.path.join('new', key0), |
| 798 | key1: os.path.join('new', key1)}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 799 | self._box._refresh() |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 800 | self.assertEqual(self._box._toc, {key0: os.path.join('new', key0), |
| 801 | key1: os.path.join('new', key1), |
| 802 | key2: os.path.join('new', key2)}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 803 | |
Petri Lehtinen | 8c482ee | 2011-11-05 09:44:59 +0200 | [diff] [blame] | 804 | def test_refresh_after_safety_period(self): |
| 805 | # Issue #13254: Call _refresh after the "file system safety |
| 806 | # period" of 2 seconds has passed; _toc should still be |
| 807 | # updated because this is the first call to _refresh. |
| 808 | key0 = self._box.add(self._template % 0) |
| 809 | key1 = self._box.add(self._template % 1) |
| 810 | |
| 811 | self._box = self._factory(self._path) |
| 812 | self.assertEqual(self._box._toc, {}) |
| 813 | |
| 814 | # Emulate sleeping. Instead of sleeping for 2 seconds, use the |
| 815 | # skew factor to make _refresh think that the filesystem |
| 816 | # safety period has passed and re-reading the _toc is only |
| 817 | # required if mtimes differ. |
Petri Lehtinen | c153cd3 | 2011-11-12 21:02:42 +0200 | [diff] [blame] | 818 | self._box._skewfactor = -3 |
Petri Lehtinen | 8c482ee | 2011-11-05 09:44:59 +0200 | [diff] [blame] | 819 | |
| 820 | self._box._refresh() |
| 821 | self.assertEqual(sorted(self._box._toc.keys()), sorted([key0, key1])) |
| 822 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 823 | def test_lookup(self): |
| 824 | # Look up message subpaths in the TOC |
| 825 | self.assertRaises(KeyError, lambda: self._box._lookup('foo')) |
| 826 | key0 = self._box.add(self._template % 0) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 827 | self.assertEqual(self._box._lookup(key0), os.path.join('new', key0)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 828 | os.remove(os.path.join(self._path, 'new', key0)) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 829 | self.assertEqual(self._box._toc, {key0: os.path.join('new', key0)}) |
Antoine Pitrou | 03f1344 | 2009-11-02 11:36:51 +0000 | [diff] [blame] | 830 | # Be sure that the TOC is read back from disk (see issue #6896 |
| 831 | # about bad mtime behaviour on some systems). |
| 832 | self._box.flush() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 833 | self.assertRaises(KeyError, lambda: self._box._lookup(key0)) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 834 | self.assertEqual(self._box._toc, {}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 835 | |
| 836 | def test_lock_unlock(self): |
| 837 | # Lock and unlock the mailbox. For Maildir, this does nothing. |
| 838 | self._box.lock() |
| 839 | self._box.unlock() |
| 840 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 841 | def test_folder (self): |
| 842 | # Test for bug #1569790: verify that folders returned by .get_folder() |
| 843 | # use the same factory function. |
| 844 | def dummy_factory (s): |
| 845 | return None |
| 846 | box = self._factory(self._path, factory=dummy_factory) |
| 847 | folder = box.add_folder('folder1') |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 848 | self.assertIs(folder._factory, dummy_factory) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 849 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 850 | folder1_alias = box.get_folder('folder1') |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 851 | self.assertIs(folder1_alias._factory, dummy_factory) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 852 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 853 | def test_directory_in_folder (self): |
| 854 | # Test that mailboxes still work if there's a stray extra directory |
| 855 | # in a folder. |
| 856 | for i in range(10): |
| 857 | self._box.add(mailbox.Message(_sample_message)) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 858 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 859 | # Create a stray directory |
| 860 | os.mkdir(os.path.join(self._path, 'cur', 'stray-dir')) |
| 861 | |
| 862 | # Check that looping still works with the directory present. |
| 863 | for msg in self._box: |
| 864 | pass |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 865 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 866 | @unittest.skipUnless(hasattr(os, 'umask'), 'test needs os.umask()') |
| 867 | @unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()') |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 868 | def test_file_permissions(self): |
| 869 | # Verify that message files are created without execute permissions |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 870 | msg = mailbox.MaildirMessage(self._template % 0) |
| 871 | orig_umask = os.umask(0) |
| 872 | try: |
| 873 | key = self._box.add(msg) |
| 874 | finally: |
| 875 | os.umask(orig_umask) |
| 876 | path = os.path.join(self._path, self._box._lookup(key)) |
| 877 | mode = os.stat(path).st_mode |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 878 | self.assertFalse(mode & 0o111) |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 879 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 880 | @unittest.skipUnless(hasattr(os, 'umask'), 'test needs os.umask()') |
| 881 | @unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()') |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 882 | def test_folder_file_perms(self): |
| 883 | # From bug #3228, we want to verify that the file created inside a Maildir |
| 884 | # subfolder isn't marked as executable. |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 885 | orig_umask = os.umask(0) |
| 886 | try: |
| 887 | subfolder = self._box.add_folder('subfolder') |
| 888 | finally: |
| 889 | os.umask(orig_umask) |
| 890 | |
| 891 | path = os.path.join(subfolder._path, 'maildirfolder') |
| 892 | st = os.stat(path) |
| 893 | perms = st.st_mode |
| 894 | self.assertFalse((perms & 0o111)) # Execute bits should all be off. |
| 895 | |
Andrew M. Kuchling | fa81534 | 2009-05-03 02:52:20 +0000 | [diff] [blame] | 896 | def test_reread(self): |
Petri Lehtinen | 8c482ee | 2011-11-05 09:44:59 +0200 | [diff] [blame] | 897 | # Do an initial unconditional refresh |
| 898 | self._box._refresh() |
Andrew M. Kuchling | fa81534 | 2009-05-03 02:52:20 +0000 | [diff] [blame] | 899 | |
R David Murray | caed7fe | 2011-05-06 22:07:19 -0400 | [diff] [blame] | 900 | # Put the last modified times more than two seconds into the past |
| 901 | # (because mtime may have a two second granularity) |
R David Murray | b9a428d | 2011-03-25 16:03:47 -0400 | [diff] [blame] | 902 | for subdir in ('cur', 'new'): |
| 903 | os.utime(os.path.join(self._box._path, subdir), |
| 904 | (time.time()-5,)*2) |
| 905 | |
R David Murray | caed7fe | 2011-05-06 22:07:19 -0400 | [diff] [blame] | 906 | # Because mtime has a two second granularity in worst case (FAT), a |
| 907 | # refresh is done unconditionally if called for within |
| 908 | # two-second-plus-a-bit of the last one, just in case the mbox has |
| 909 | # changed; so now we have to wait for that interval to expire. |
Petri Lehtinen | c153cd3 | 2011-11-12 21:02:42 +0200 | [diff] [blame] | 910 | # |
| 911 | # Because this is a test, emulate sleeping. Instead of |
| 912 | # sleeping for 2 seconds, use the skew factor to make _refresh |
| 913 | # think that 2 seconds have passed and re-reading the _toc is |
| 914 | # only required if mtimes differ. |
| 915 | self._box._skewfactor = -3 |
R David Murray | caed7fe | 2011-05-06 22:07:19 -0400 | [diff] [blame] | 916 | |
R David Murray | b9a428d | 2011-03-25 16:03:47 -0400 | [diff] [blame] | 917 | # Re-reading causes the ._toc attribute to be assigned a new dictionary |
| 918 | # object, so we'll check that the ._toc attribute isn't a different |
| 919 | # object. |
Andrew M. Kuchling | fa81534 | 2009-05-03 02:52:20 +0000 | [diff] [blame] | 920 | orig_toc = self._box._toc |
| 921 | def refreshed(): |
| 922 | return self._box._toc is not orig_toc |
| 923 | |
Andrew M. Kuchling | fa81534 | 2009-05-03 02:52:20 +0000 | [diff] [blame] | 924 | self._box._refresh() |
R David Murray | caed7fe | 2011-05-06 22:07:19 -0400 | [diff] [blame] | 925 | self.assertFalse(refreshed()) |
Andrew M. Kuchling | fa81534 | 2009-05-03 02:52:20 +0000 | [diff] [blame] | 926 | |
| 927 | # Now, write something into cur and remove it. This changes |
Petri Lehtinen | c153cd3 | 2011-11-12 21:02:42 +0200 | [diff] [blame] | 928 | # the mtime and should cause a re-read. Note that "sleep |
| 929 | # emulation" is still in effect, as skewfactor is -3. |
Andrew M. Kuchling | fa81534 | 2009-05-03 02:52:20 +0000 | [diff] [blame] | 930 | filename = os.path.join(self._path, 'cur', 'stray-file') |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 931 | support.create_empty_file(filename) |
Andrew M. Kuchling | fa81534 | 2009-05-03 02:52:20 +0000 | [diff] [blame] | 932 | os.unlink(filename) |
| 933 | self._box._refresh() |
R David Murray | caed7fe | 2011-05-06 22:07:19 -0400 | [diff] [blame] | 934 | self.assertTrue(refreshed()) |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 935 | |
Petri Lehtinen | f29435f | 2012-06-28 13:48:17 +0300 | [diff] [blame] | 936 | |
| 937 | class _TestSingleFile(TestMailbox): |
| 938 | '''Common tests for single-file mailboxes''' |
| 939 | |
| 940 | def test_add_doesnt_rewrite(self): |
| 941 | # When only adding messages, flush() should not rewrite the |
| 942 | # mailbox file. See issue #9559. |
| 943 | |
| 944 | # Inode number changes if the contents are written to another |
| 945 | # file which is then renamed over the original file. So we |
| 946 | # must check that the inode number doesn't change. |
| 947 | inode_before = os.stat(self._path).st_ino |
| 948 | |
| 949 | self._box.add(self._template % 0) |
| 950 | self._box.flush() |
| 951 | |
| 952 | inode_after = os.stat(self._path).st_ino |
| 953 | self.assertEqual(inode_before, inode_after) |
| 954 | |
| 955 | # Make sure the message was really added |
| 956 | self._box.close() |
| 957 | self._box = self._factory(self._path) |
| 958 | self.assertEqual(len(self._box), 1) |
| 959 | |
Petri Lehtinen | 5b5619f | 2012-06-29 15:09:12 +0300 | [diff] [blame] | 960 | def test_permissions_after_flush(self): |
| 961 | # See issue #5346 |
| 962 | |
| 963 | # Make the mailbox world writable. It's unlikely that the new |
| 964 | # mailbox file would have these permissions after flush(), |
| 965 | # because umask usually prevents it. |
| 966 | mode = os.stat(self._path).st_mode | 0o666 |
| 967 | os.chmod(self._path, mode) |
| 968 | |
| 969 | self._box.add(self._template % 0) |
| 970 | i = self._box.add(self._template % 1) |
| 971 | # Need to remove one message to make flush() create a new file |
| 972 | self._box.remove(i) |
| 973 | self._box.flush() |
| 974 | |
| 975 | self.assertEqual(os.stat(self._path).st_mode, mode) |
| 976 | |
Petri Lehtinen | f29435f | 2012-06-28 13:48:17 +0300 | [diff] [blame] | 977 | |
| 978 | class _TestMboxMMDF(_TestSingleFile): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 979 | |
| 980 | def tearDown(self): |
Brett Cannon | 653238a | 2010-10-30 00:13:00 +0000 | [diff] [blame] | 981 | super().tearDown() |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 982 | self._box.close() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 983 | self._delete_recursively(self._path) |
| 984 | for lock_remnant in glob.glob(self._path + '.*'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 985 | support.unlink(lock_remnant) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 986 | |
R. David Murray | fc14114 | 2011-02-11 22:47:17 +0000 | [diff] [blame] | 987 | def assertMailboxEmpty(self): |
| 988 | with open(self._path) as f: |
| 989 | self.assertEqual(f.readlines(), []) |
| 990 | |
Cheryl Sabella | d16f012 | 2018-10-18 20:21:47 -0400 | [diff] [blame^] | 991 | def test_get_bytes_from(self): |
| 992 | # Get bytes representations of messages with _unixfrom. |
| 993 | unixfrom = 'From foo@bar blah\n' |
| 994 | key0 = self._box.add(unixfrom + self._template % 0) |
| 995 | key1 = self._box.add(unixfrom + _sample_message) |
| 996 | self.assertEqual(self._box.get_bytes(key0, from_=False), |
| 997 | (self._template % 0).encode('ascii')) |
| 998 | self.assertEqual(self._box.get_bytes(key1, from_=False), |
| 999 | _bytes_sample_message) |
| 1000 | self.assertEqual(self._box.get_bytes(key0, from_=True), |
| 1001 | (unixfrom + self._template % 0).encode('ascii')) |
| 1002 | self.assertEqual(self._box.get_bytes(key1, from_=True), |
| 1003 | unixfrom.encode('ascii') + _bytes_sample_message) |
| 1004 | |
| 1005 | def test_get_string_from(self): |
| 1006 | # Get string representations of messages with _unixfrom. |
| 1007 | unixfrom = 'From foo@bar blah\n' |
| 1008 | key0 = self._box.add(unixfrom + self._template % 0) |
| 1009 | key1 = self._box.add(unixfrom + _sample_message) |
| 1010 | self.assertEqual(self._box.get_string(key0, from_=False), |
| 1011 | self._template % 0) |
| 1012 | self.assertEqual(self._box.get_string(key1, from_=False).split('\n'), |
| 1013 | _sample_message.split('\n')) |
| 1014 | self.assertEqual(self._box.get_string(key0, from_=True), |
| 1015 | unixfrom + self._template % 0) |
| 1016 | self.assertEqual(self._box.get_string(key1, from_=True).split('\n'), |
| 1017 | (unixfrom + _sample_message).split('\n')) |
| 1018 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1019 | def test_add_from_string(self): |
| 1020 | # Add a string starting with 'From ' to the mailbox |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 1021 | key = self._box.add('From foo@bar blah\nFrom: foo\n\n0\n') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1022 | self.assertEqual(self._box[key].get_from(), 'foo@bar blah') |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 1023 | self.assertEqual(self._box[key].get_payload(), '0\n') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1024 | |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 1025 | def test_add_from_bytes(self): |
| 1026 | # Add a byte string starting with 'From ' to the mailbox |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 1027 | key = self._box.add(b'From foo@bar blah\nFrom: foo\n\n0\n') |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 1028 | self.assertEqual(self._box[key].get_from(), 'foo@bar blah') |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 1029 | self.assertEqual(self._box[key].get_payload(), '0\n') |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 1030 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1031 | def test_add_mbox_or_mmdf_message(self): |
| 1032 | # Add an mboxMessage or MMDFMessage |
| 1033 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
Petri Lehtinen | 4680919 | 2012-09-25 21:58:51 +0300 | [diff] [blame] | 1034 | msg = class_('From foo@bar blah\nFrom: foo\n\n0\n') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1035 | key = self._box.add(msg) |
| 1036 | |
| 1037 | def test_open_close_open(self): |
| 1038 | # Open and inspect previously-created mailbox |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1039 | values = [self._template % i for i in range(3)] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1040 | for value in values: |
| 1041 | self._box.add(value) |
| 1042 | self._box.close() |
| 1043 | mtime = os.path.getmtime(self._path) |
| 1044 | self._box = self._factory(self._path) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1045 | self.assertEqual(len(self._box), 3) |
Serhiy Storchaka | cd3aacf5 | 2014-08-13 09:35:21 +0300 | [diff] [blame] | 1046 | for key in self._box.iterkeys(): |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1047 | self.assertIn(self._box.get_string(key), values) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1048 | self._box.close() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1049 | self.assertEqual(mtime, os.path.getmtime(self._path)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1050 | |
| 1051 | def test_add_and_close(self): |
| 1052 | # Verifying that closing a mailbox doesn't change added items |
| 1053 | self._box.add(_sample_message) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1054 | for i in range(3): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1055 | self._box.add(self._template % i) |
| 1056 | self._box.add(_sample_message) |
| 1057 | self._box._file.flush() |
| 1058 | self._box._file.seek(0) |
| 1059 | contents = self._box._file.read() |
| 1060 | self._box.close() |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 1061 | with open(self._path, 'rb') as f: |
Brian Curtin | 5acec04 | 2010-10-13 02:29:46 +0000 | [diff] [blame] | 1062 | self.assertEqual(contents, f.read()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1063 | self._box = self._factory(self._path) |
| 1064 | |
Charles-François Natali | 78ed83d | 2011-12-19 12:18:55 +0100 | [diff] [blame] | 1065 | @unittest.skipUnless(hasattr(os, 'fork'), "Test needs fork().") |
Charles-François Natali | bf38315 | 2011-12-20 11:48:22 +0100 | [diff] [blame] | 1066 | @unittest.skipUnless(hasattr(socket, 'socketpair'), "Test needs socketpair().") |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1067 | def test_lock_conflict(self): |
Charles-François Natali | 78ed83d | 2011-12-19 12:18:55 +0100 | [diff] [blame] | 1068 | # Fork off a child process that will lock the mailbox temporarily, |
| 1069 | # unlock it and exit. |
Charles-François Natali | bf38315 | 2011-12-20 11:48:22 +0100 | [diff] [blame] | 1070 | c, p = socket.socketpair() |
| 1071 | self.addCleanup(c.close) |
| 1072 | self.addCleanup(p.close) |
Charles-François Natali | 78ed83d | 2011-12-19 12:18:55 +0100 | [diff] [blame] | 1073 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1074 | pid = os.fork() |
| 1075 | if pid == 0: |
Charles-François Natali | 78ed83d | 2011-12-19 12:18:55 +0100 | [diff] [blame] | 1076 | # child |
Guido van Rossum | 62cbc8a | 2007-04-09 00:41:25 +0000 | [diff] [blame] | 1077 | try: |
Charles-François Natali | 78ed83d | 2011-12-19 12:18:55 +0100 | [diff] [blame] | 1078 | # lock the mailbox, and signal the parent it can proceed |
Guido van Rossum | 62cbc8a | 2007-04-09 00:41:25 +0000 | [diff] [blame] | 1079 | self._box.lock() |
Charles-François Natali | bf38315 | 2011-12-20 11:48:22 +0100 | [diff] [blame] | 1080 | c.send(b'c') |
Charles-François Natali | 78ed83d | 2011-12-19 12:18:55 +0100 | [diff] [blame] | 1081 | |
| 1082 | # wait until the parent is done, and unlock the mailbox |
Charles-François Natali | bf38315 | 2011-12-20 11:48:22 +0100 | [diff] [blame] | 1083 | c.recv(1) |
Guido van Rossum | 62cbc8a | 2007-04-09 00:41:25 +0000 | [diff] [blame] | 1084 | self._box.unlock() |
| 1085 | finally: |
| 1086 | os._exit(0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1087 | |
Charles-François Natali | 78ed83d | 2011-12-19 12:18:55 +0100 | [diff] [blame] | 1088 | # In the parent, wait until the child signals it locked the mailbox. |
Charles-François Natali | bf38315 | 2011-12-20 11:48:22 +0100 | [diff] [blame] | 1089 | p.recv(1) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1090 | try: |
| 1091 | self.assertRaises(mailbox.ExternalClashError, |
| 1092 | self._box.lock) |
| 1093 | finally: |
Charles-François Natali | 78ed83d | 2011-12-19 12:18:55 +0100 | [diff] [blame] | 1094 | # Signal the child it can now release the lock and exit. |
Charles-François Natali | bf38315 | 2011-12-20 11:48:22 +0100 | [diff] [blame] | 1095 | p.send(b'p') |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1096 | # Wait for child to exit. Locking should now succeed. |
| 1097 | exited_pid, status = os.waitpid(pid, 0) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1098 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1099 | self._box.lock() |
| 1100 | self._box.unlock() |
| 1101 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1102 | def test_relock(self): |
| 1103 | # Test case for bug #1575506: the mailbox class was locking the |
| 1104 | # wrong file object in its flush() method. |
| 1105 | msg = "Subject: sub\n\nbody\n" |
| 1106 | key1 = self._box.add(msg) |
| 1107 | self._box.flush() |
| 1108 | self._box.close() |
| 1109 | |
| 1110 | self._box = self._factory(self._path) |
| 1111 | self._box.lock() |
| 1112 | key2 = self._box.add(msg) |
| 1113 | self._box.flush() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1114 | self.assertTrue(self._box._locked) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1115 | self._box.close() |
| 1116 | |
| 1117 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 1118 | class TestMbox(_TestMboxMMDF, unittest.TestCase): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1119 | |
| 1120 | _factory = lambda self, path, factory=None: mailbox.mbox(path, factory) |
| 1121 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 1122 | @unittest.skipUnless(hasattr(os, 'umask'), 'test needs os.umask()') |
| 1123 | @unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()') |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 1124 | def test_file_perms(self): |
| 1125 | # From bug #3228, we want to verify that the mailbox file isn't executable, |
| 1126 | # even if the umask is set to something that would leave executable bits set. |
| 1127 | # We only run this test on platforms that support umask. |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 1128 | try: |
| 1129 | old_umask = os.umask(0o077) |
| 1130 | self._box.close() |
| 1131 | os.unlink(self._path) |
| 1132 | self._box = mailbox.mbox(self._path, create=True) |
| 1133 | self._box.add('') |
| 1134 | self._box.close() |
| 1135 | finally: |
| 1136 | os.umask(old_umask) |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 1137 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 1138 | st = os.stat(self._path) |
| 1139 | perms = st.st_mode |
| 1140 | self.assertFalse((perms & 0o111)) # Execute bits should all be off. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1141 | |
Petri Lehtinen | f39884b | 2012-09-25 22:00:32 +0300 | [diff] [blame] | 1142 | def test_terminating_newline(self): |
| 1143 | message = email.message.Message() |
| 1144 | message['From'] = 'john@example.com' |
| 1145 | message.set_payload('No newline at the end') |
| 1146 | i = self._box.add(message) |
| 1147 | |
| 1148 | # A newline should have been appended to the payload |
| 1149 | message = self._box.get(i) |
| 1150 | self.assertEqual(message.get_payload(), 'No newline at the end\n') |
| 1151 | |
| 1152 | def test_message_separator(self): |
| 1153 | # Check there's always a single blank line after each message |
| 1154 | self._box.add('From: foo\n\n0') # No newline at the end |
| 1155 | with open(self._path) as f: |
| 1156 | data = f.read() |
| 1157 | self.assertEqual(data[-3:], '0\n\n') |
| 1158 | |
| 1159 | self._box.add('From: foo\n\n0\n') # Newline at the end |
| 1160 | with open(self._path) as f: |
| 1161 | data = f.read() |
| 1162 | self.assertEqual(data[-3:], '0\n\n') |
| 1163 | |
| 1164 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 1165 | class TestMMDF(_TestMboxMMDF, unittest.TestCase): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1166 | |
| 1167 | _factory = lambda self, path, factory=None: mailbox.MMDF(path, factory) |
| 1168 | |
| 1169 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 1170 | class TestMH(TestMailbox, unittest.TestCase): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1171 | |
| 1172 | _factory = lambda self, path, factory=None: mailbox.MH(path, factory) |
| 1173 | |
R. David Murray | fc14114 | 2011-02-11 22:47:17 +0000 | [diff] [blame] | 1174 | def assertMailboxEmpty(self): |
| 1175 | self.assertEqual(os.listdir(self._path), ['.mh_sequences']) |
| 1176 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1177 | def test_list_folders(self): |
| 1178 | # List folders |
| 1179 | self._box.add_folder('one') |
| 1180 | self._box.add_folder('two') |
| 1181 | self._box.add_folder('three') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1182 | self.assertEqual(len(self._box.list_folders()), 3) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1183 | self.assertEqual(set(self._box.list_folders()), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1184 | set(('one', 'two', 'three'))) |
| 1185 | |
| 1186 | def test_get_folder(self): |
| 1187 | # Open folders |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1188 | def dummy_factory (s): |
| 1189 | return None |
| 1190 | self._box = self._factory(self._path, dummy_factory) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1191 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1192 | new_folder = self._box.add_folder('foo.bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1193 | folder0 = self._box.get_folder('foo.bar') |
| 1194 | folder0.add(self._template % 'bar') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 1195 | self.assertTrue(os.path.isdir(os.path.join(self._path, 'foo.bar'))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1196 | folder1 = self._box.get_folder('foo.bar') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1197 | self.assertEqual(folder1.get_string(folder1.keys()[0]), |
| 1198 | self._template % 'bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1199 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1200 | # Test for bug #1569790: verify that folders returned by .get_folder() |
| 1201 | # use the same factory function. |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1202 | self.assertIs(new_folder._factory, self._box._factory) |
| 1203 | self.assertIs(folder0._factory, self._box._factory) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1204 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1205 | def test_add_and_remove_folders(self): |
| 1206 | # Delete folders |
| 1207 | self._box.add_folder('one') |
| 1208 | self._box.add_folder('two') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1209 | self.assertEqual(len(self._box.list_folders()), 2) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1210 | self.assertEqual(set(self._box.list_folders()), set(('one', 'two'))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1211 | self._box.remove_folder('one') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1212 | self.assertEqual(len(self._box.list_folders()), 1) |
| 1213 | self.assertEqual(set(self._box.list_folders()), set(('two',))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1214 | self._box.add_folder('three') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1215 | self.assertEqual(len(self._box.list_folders()), 2) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1216 | self.assertEqual(set(self._box.list_folders()), set(('two', 'three'))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1217 | self._box.remove_folder('three') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1218 | self.assertEqual(len(self._box.list_folders()), 1) |
| 1219 | self.assertEqual(set(self._box.list_folders()), set(('two',))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1220 | self._box.remove_folder('two') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1221 | self.assertEqual(len(self._box.list_folders()), 0) |
| 1222 | self.assertEqual(self._box.list_folders(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1223 | |
| 1224 | def test_sequences(self): |
| 1225 | # Get and set sequences |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1226 | self.assertEqual(self._box.get_sequences(), {}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1227 | msg0 = mailbox.MHMessage(self._template % 0) |
| 1228 | msg0.add_sequence('foo') |
| 1229 | key0 = self._box.add(msg0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1230 | self.assertEqual(self._box.get_sequences(), {'foo':[key0]}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1231 | msg1 = mailbox.MHMessage(self._template % 1) |
| 1232 | msg1.set_sequences(['bar', 'replied', 'foo']) |
| 1233 | key1 = self._box.add(msg1) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1234 | self.assertEqual(self._box.get_sequences(), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1235 | {'foo':[key0, key1], 'bar':[key1], 'replied':[key1]}) |
| 1236 | msg0.set_sequences(['flagged']) |
| 1237 | self._box[key0] = msg0 |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1238 | self.assertEqual(self._box.get_sequences(), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1239 | {'foo':[key1], 'bar':[key1], 'replied':[key1], |
| 1240 | 'flagged':[key0]}) |
| 1241 | self._box.remove(key1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1242 | self.assertEqual(self._box.get_sequences(), {'flagged':[key0]}) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1243 | |
R. David Murray | 548ac41 | 2009-04-02 19:44:43 +0000 | [diff] [blame] | 1244 | def test_issue2625(self): |
| 1245 | msg0 = mailbox.MHMessage(self._template % 0) |
| 1246 | msg0.add_sequence('foo') |
| 1247 | key0 = self._box.add(msg0) |
| 1248 | refmsg0 = self._box.get_message(key0) |
| 1249 | |
Benjamin Peterson | 21896a3 | 2010-03-21 22:03:03 +0000 | [diff] [blame] | 1250 | def test_issue7627(self): |
| 1251 | msg0 = mailbox.MHMessage(self._template % 0) |
| 1252 | key0 = self._box.add(msg0) |
| 1253 | self._box.lock() |
| 1254 | self._box.remove(key0) |
| 1255 | self._box.unlock() |
| 1256 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1257 | def test_pack(self): |
| 1258 | # Pack the contents of the mailbox |
| 1259 | msg0 = mailbox.MHMessage(self._template % 0) |
| 1260 | msg1 = mailbox.MHMessage(self._template % 1) |
| 1261 | msg2 = mailbox.MHMessage(self._template % 2) |
| 1262 | msg3 = mailbox.MHMessage(self._template % 3) |
| 1263 | msg0.set_sequences(['foo', 'unseen']) |
| 1264 | msg1.set_sequences(['foo']) |
| 1265 | msg2.set_sequences(['foo', 'flagged']) |
| 1266 | msg3.set_sequences(['foo', 'bar', 'replied']) |
| 1267 | key0 = self._box.add(msg0) |
| 1268 | key1 = self._box.add(msg1) |
| 1269 | key2 = self._box.add(msg2) |
| 1270 | key3 = self._box.add(msg3) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1271 | self.assertEqual(self._box.get_sequences(), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1272 | {'foo':[key0,key1,key2,key3], 'unseen':[key0], |
| 1273 | 'flagged':[key2], 'bar':[key3], 'replied':[key3]}) |
| 1274 | self._box.remove(key2) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1275 | self.assertEqual(self._box.get_sequences(), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1276 | {'foo':[key0,key1,key3], 'unseen':[key0], 'bar':[key3], |
| 1277 | 'replied':[key3]}) |
| 1278 | self._box.pack() |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1279 | self.assertEqual(self._box.keys(), [1, 2, 3]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1280 | key0 = key0 |
| 1281 | key1 = key0 + 1 |
| 1282 | key2 = key1 + 1 |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1283 | self.assertEqual(self._box.get_sequences(), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1284 | {'foo':[1, 2, 3], 'unseen':[1], 'bar':[3], 'replied':[3]}) |
| 1285 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1286 | # Test case for packing while holding the mailbox locked. |
| 1287 | key0 = self._box.add(msg1) |
| 1288 | key1 = self._box.add(msg1) |
| 1289 | key2 = self._box.add(msg1) |
| 1290 | key3 = self._box.add(msg1) |
| 1291 | |
| 1292 | self._box.remove(key0) |
| 1293 | self._box.remove(key2) |
| 1294 | self._box.lock() |
| 1295 | self._box.pack() |
| 1296 | self._box.unlock() |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1297 | self.assertEqual(self._box.get_sequences(), |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1298 | {'foo':[1, 2, 3, 4, 5], |
| 1299 | 'unseen':[1], 'bar':[3], 'replied':[3]}) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 1300 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1301 | def _get_lock_path(self): |
| 1302 | return os.path.join(self._path, '.mh_sequences.lock') |
| 1303 | |
| 1304 | |
Petri Lehtinen | f29435f | 2012-06-28 13:48:17 +0300 | [diff] [blame] | 1305 | class TestBabyl(_TestSingleFile, unittest.TestCase): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1306 | |
| 1307 | _factory = lambda self, path, factory=None: mailbox.Babyl(path, factory) |
| 1308 | |
R. David Murray | fc14114 | 2011-02-11 22:47:17 +0000 | [diff] [blame] | 1309 | def assertMailboxEmpty(self): |
| 1310 | with open(self._path) as f: |
| 1311 | self.assertEqual(f.readlines(), []) |
| 1312 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1313 | def tearDown(self): |
Brett Cannon | 653238a | 2010-10-30 00:13:00 +0000 | [diff] [blame] | 1314 | super().tearDown() |
Guido van Rossum | c12a813 | 2007-10-26 04:29:23 +0000 | [diff] [blame] | 1315 | self._box.close() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1316 | self._delete_recursively(self._path) |
| 1317 | for lock_remnant in glob.glob(self._path + '.*'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1318 | support.unlink(lock_remnant) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1319 | |
| 1320 | def test_labels(self): |
| 1321 | # Get labels from the mailbox |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1322 | self.assertEqual(self._box.get_labels(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1323 | msg0 = mailbox.BabylMessage(self._template % 0) |
| 1324 | msg0.add_label('foo') |
| 1325 | key0 = self._box.add(msg0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1326 | self.assertEqual(self._box.get_labels(), ['foo']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1327 | msg1 = mailbox.BabylMessage(self._template % 1) |
| 1328 | msg1.set_labels(['bar', 'answered', 'foo']) |
| 1329 | key1 = self._box.add(msg1) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1330 | self.assertEqual(set(self._box.get_labels()), set(['foo', 'bar'])) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1331 | msg0.set_labels(['blah', 'filed']) |
| 1332 | self._box[key0] = msg0 |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1333 | self.assertEqual(set(self._box.get_labels()), |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1334 | set(['foo', 'bar', 'blah'])) |
| 1335 | self._box.remove(key1) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1336 | self.assertEqual(set(self._box.get_labels()), set(['blah'])) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1337 | |
| 1338 | |
R David Murray | 05ff990 | 2011-06-17 12:54:56 -0400 | [diff] [blame] | 1339 | class FakeFileLikeObject: |
| 1340 | |
| 1341 | def __init__(self): |
| 1342 | self.closed = False |
| 1343 | |
| 1344 | def close(self): |
| 1345 | self.closed = True |
| 1346 | |
| 1347 | |
| 1348 | class FakeMailBox(mailbox.Mailbox): |
| 1349 | |
| 1350 | def __init__(self): |
| 1351 | mailbox.Mailbox.__init__(self, '', lambda file: None) |
| 1352 | self.files = [FakeFileLikeObject() for i in range(10)] |
| 1353 | |
| 1354 | def get_file(self, key): |
| 1355 | return self.files[key] |
| 1356 | |
| 1357 | |
| 1358 | class TestFakeMailBox(unittest.TestCase): |
| 1359 | |
| 1360 | def test_closing_fd(self): |
| 1361 | box = FakeMailBox() |
| 1362 | for i in range(10): |
| 1363 | self.assertFalse(box.files[i].closed) |
| 1364 | for i in range(10): |
| 1365 | box[i] |
| 1366 | for i in range(10): |
| 1367 | self.assertTrue(box.files[i].closed) |
| 1368 | |
| 1369 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 1370 | class TestMessage(TestBase, unittest.TestCase): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1371 | |
| 1372 | _factory = mailbox.Message # Overridden by subclasses to reuse tests |
| 1373 | |
| 1374 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1375 | self._path = support.TESTFN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1376 | |
| 1377 | def tearDown(self): |
| 1378 | self._delete_recursively(self._path) |
| 1379 | |
| 1380 | def test_initialize_with_eMM(self): |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1381 | # Initialize based on email.message.Message instance |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1382 | eMM = email.message_from_string(_sample_message) |
| 1383 | msg = self._factory(eMM) |
| 1384 | self._post_initialize_hook(msg) |
| 1385 | self._check_sample(msg) |
| 1386 | |
| 1387 | def test_initialize_with_string(self): |
| 1388 | # Initialize based on string |
| 1389 | msg = self._factory(_sample_message) |
| 1390 | self._post_initialize_hook(msg) |
| 1391 | self._check_sample(msg) |
| 1392 | |
| 1393 | def test_initialize_with_file(self): |
| 1394 | # Initialize based on contents of file |
Brian Curtin | 5acec04 | 2010-10-13 02:29:46 +0000 | [diff] [blame] | 1395 | with open(self._path, 'w+') as f: |
| 1396 | f.write(_sample_message) |
| 1397 | f.seek(0) |
| 1398 | msg = self._factory(f) |
| 1399 | self._post_initialize_hook(msg) |
| 1400 | self._check_sample(msg) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1401 | |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 1402 | def test_initialize_with_binary_file(self): |
| 1403 | # Initialize based on contents of binary file |
| 1404 | with open(self._path, 'wb+') as f: |
| 1405 | f.write(_bytes_sample_message) |
| 1406 | f.seek(0) |
| 1407 | msg = self._factory(f) |
| 1408 | self._post_initialize_hook(msg) |
| 1409 | self._check_sample(msg) |
| 1410 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1411 | def test_initialize_with_nothing(self): |
| 1412 | # Initialize without arguments |
| 1413 | msg = self._factory() |
| 1414 | self._post_initialize_hook(msg) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 1415 | self.assertIsInstance(msg, email.message.Message) |
| 1416 | self.assertIsInstance(msg, mailbox.Message) |
| 1417 | self.assertIsInstance(msg, self._factory) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1418 | self.assertEqual(msg.keys(), []) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1419 | self.assertFalse(msg.is_multipart()) |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 1420 | self.assertIsNone(msg.get_payload()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1421 | |
| 1422 | def test_initialize_incorrectly(self): |
| 1423 | # Initialize with invalid argument |
| 1424 | self.assertRaises(TypeError, lambda: self._factory(object())) |
| 1425 | |
R David Murray | b019ee7 | 2012-04-08 22:36:07 -0400 | [diff] [blame] | 1426 | def test_all_eMM_attribues_exist(self): |
| 1427 | # Issue 12537 |
| 1428 | eMM = email.message_from_string(_sample_message) |
| 1429 | msg = self._factory(_sample_message) |
| 1430 | for attr in eMM.__dict__: |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 1431 | self.assertIn(attr, msg.__dict__, |
R David Murray | b019ee7 | 2012-04-08 22:36:07 -0400 | [diff] [blame] | 1432 | '{} attribute does not exist'.format(attr)) |
| 1433 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1434 | def test_become_message(self): |
| 1435 | # Take on the state of another message |
| 1436 | eMM = email.message_from_string(_sample_message) |
| 1437 | msg = self._factory() |
| 1438 | msg._become_message(eMM) |
| 1439 | self._check_sample(msg) |
| 1440 | |
| 1441 | def test_explain_to(self): |
| 1442 | # Copy self's format-specific data to other message formats. |
| 1443 | # This test is superficial; better ones are in TestMessageConversion. |
| 1444 | msg = self._factory() |
R David Murray | c988e24 | 2012-04-08 21:00:27 -0400 | [diff] [blame] | 1445 | for class_ in self.all_mailbox_types: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1446 | other_msg = class_() |
| 1447 | msg._explain_to(other_msg) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 1448 | other_msg = email.message.Message() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1449 | self.assertRaises(TypeError, lambda: msg._explain_to(other_msg)) |
| 1450 | |
| 1451 | def _post_initialize_hook(self, msg): |
| 1452 | # Overridden by subclasses to check extra things after initialization |
| 1453 | pass |
| 1454 | |
| 1455 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 1456 | class TestMaildirMessage(TestMessage, unittest.TestCase): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1457 | |
| 1458 | _factory = mailbox.MaildirMessage |
| 1459 | |
| 1460 | def _post_initialize_hook(self, msg): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1461 | self.assertEqual(msg._subdir, 'new') |
| 1462 | self.assertEqual(msg._info, '') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1463 | |
| 1464 | def test_subdir(self): |
| 1465 | # Use get_subdir() and set_subdir() |
| 1466 | msg = mailbox.MaildirMessage(_sample_message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1467 | self.assertEqual(msg.get_subdir(), 'new') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1468 | msg.set_subdir('cur') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1469 | self.assertEqual(msg.get_subdir(), 'cur') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1470 | msg.set_subdir('new') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1471 | self.assertEqual(msg.get_subdir(), 'new') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1472 | self.assertRaises(ValueError, lambda: msg.set_subdir('tmp')) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1473 | self.assertEqual(msg.get_subdir(), 'new') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1474 | msg.set_subdir('new') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1475 | self.assertEqual(msg.get_subdir(), 'new') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1476 | self._check_sample(msg) |
| 1477 | |
| 1478 | def test_flags(self): |
| 1479 | # Use get_flags(), set_flags(), add_flag(), remove_flag() |
| 1480 | msg = mailbox.MaildirMessage(_sample_message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1481 | self.assertEqual(msg.get_flags(), '') |
| 1482 | self.assertEqual(msg.get_subdir(), 'new') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1483 | msg.set_flags('F') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1484 | self.assertEqual(msg.get_subdir(), 'new') |
| 1485 | self.assertEqual(msg.get_flags(), 'F') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1486 | msg.set_flags('SDTP') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1487 | self.assertEqual(msg.get_flags(), 'DPST') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1488 | msg.add_flag('FT') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1489 | self.assertEqual(msg.get_flags(), 'DFPST') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1490 | msg.remove_flag('TDRP') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1491 | self.assertEqual(msg.get_flags(), 'FS') |
| 1492 | self.assertEqual(msg.get_subdir(), 'new') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1493 | self._check_sample(msg) |
| 1494 | |
| 1495 | def test_date(self): |
| 1496 | # Use get_date() and set_date() |
| 1497 | msg = mailbox.MaildirMessage(_sample_message) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1498 | self.assertLess(abs(msg.get_date() - time.time()), 60) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1499 | msg.set_date(0.0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1500 | self.assertEqual(msg.get_date(), 0.0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1501 | |
| 1502 | def test_info(self): |
| 1503 | # Use get_info() and set_info() |
| 1504 | msg = mailbox.MaildirMessage(_sample_message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1505 | self.assertEqual(msg.get_info(), '') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1506 | msg.set_info('1,foo=bar') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1507 | self.assertEqual(msg.get_info(), '1,foo=bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1508 | self.assertRaises(TypeError, lambda: msg.set_info(None)) |
| 1509 | self._check_sample(msg) |
| 1510 | |
| 1511 | def test_info_and_flags(self): |
| 1512 | # Test interaction of info and flag methods |
| 1513 | msg = mailbox.MaildirMessage(_sample_message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1514 | self.assertEqual(msg.get_info(), '') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1515 | msg.set_flags('SF') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1516 | self.assertEqual(msg.get_flags(), 'FS') |
| 1517 | self.assertEqual(msg.get_info(), '2,FS') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1518 | msg.set_info('1,') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1519 | self.assertEqual(msg.get_flags(), '') |
| 1520 | self.assertEqual(msg.get_info(), '1,') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1521 | msg.remove_flag('RPT') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1522 | self.assertEqual(msg.get_flags(), '') |
| 1523 | self.assertEqual(msg.get_info(), '1,') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1524 | msg.add_flag('D') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1525 | self.assertEqual(msg.get_flags(), 'D') |
| 1526 | self.assertEqual(msg.get_info(), '2,D') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1527 | self._check_sample(msg) |
| 1528 | |
| 1529 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 1530 | class _TestMboxMMDFMessage: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1531 | |
| 1532 | _factory = mailbox._mboxMMDFMessage |
| 1533 | |
| 1534 | def _post_initialize_hook(self, msg): |
| 1535 | self._check_from(msg) |
| 1536 | |
| 1537 | def test_initialize_with_unixfrom(self): |
| 1538 | # Initialize with a message that already has a _unixfrom attribute |
| 1539 | msg = mailbox.Message(_sample_message) |
| 1540 | msg.set_unixfrom('From foo@bar blah') |
| 1541 | msg = mailbox.mboxMessage(msg) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1542 | self.assertEqual(msg.get_from(), 'foo@bar blah', msg.get_from()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1543 | |
| 1544 | def test_from(self): |
| 1545 | # Get and set "From " line |
| 1546 | msg = mailbox.mboxMessage(_sample_message) |
| 1547 | self._check_from(msg) |
| 1548 | msg.set_from('foo bar') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1549 | self.assertEqual(msg.get_from(), 'foo bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1550 | msg.set_from('foo@bar', True) |
| 1551 | self._check_from(msg, 'foo@bar') |
| 1552 | msg.set_from('blah@temp', time.localtime()) |
| 1553 | self._check_from(msg, 'blah@temp') |
| 1554 | |
| 1555 | def test_flags(self): |
| 1556 | # Use get_flags(), set_flags(), add_flag(), remove_flag() |
| 1557 | msg = mailbox.mboxMessage(_sample_message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1558 | self.assertEqual(msg.get_flags(), '') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1559 | msg.set_flags('F') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1560 | self.assertEqual(msg.get_flags(), 'F') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1561 | msg.set_flags('XODR') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1562 | self.assertEqual(msg.get_flags(), 'RODX') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1563 | msg.add_flag('FA') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1564 | self.assertEqual(msg.get_flags(), 'RODFAX') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1565 | msg.remove_flag('FDXA') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1566 | self.assertEqual(msg.get_flags(), 'RO') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1567 | self._check_sample(msg) |
| 1568 | |
| 1569 | def _check_from(self, msg, sender=None): |
| 1570 | # Check contents of "From " line |
| 1571 | if sender is None: |
| 1572 | sender = "MAILER-DAEMON" |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 1573 | self.assertIsNotNone(re.match( |
| 1574 | sender + r" \w{3} \w{3} [\d ]\d [\d ]\d:\d{2}:\d{2} \d{4}", |
| 1575 | msg.get_from())) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1576 | |
| 1577 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 1578 | class TestMboxMessage(_TestMboxMMDFMessage, TestMessage): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1579 | |
| 1580 | _factory = mailbox.mboxMessage |
| 1581 | |
| 1582 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 1583 | class TestMHMessage(TestMessage, unittest.TestCase): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1584 | |
| 1585 | _factory = mailbox.MHMessage |
| 1586 | |
| 1587 | def _post_initialize_hook(self, msg): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1588 | self.assertEqual(msg._sequences, []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1589 | |
| 1590 | def test_sequences(self): |
| 1591 | # Get, set, join, and leave sequences |
| 1592 | msg = mailbox.MHMessage(_sample_message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1593 | self.assertEqual(msg.get_sequences(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1594 | msg.set_sequences(['foobar']) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1595 | self.assertEqual(msg.get_sequences(), ['foobar']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1596 | msg.set_sequences([]) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1597 | self.assertEqual(msg.get_sequences(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1598 | msg.add_sequence('unseen') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1599 | self.assertEqual(msg.get_sequences(), ['unseen']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1600 | msg.add_sequence('flagged') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1601 | self.assertEqual(msg.get_sequences(), ['unseen', 'flagged']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1602 | msg.add_sequence('flagged') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1603 | self.assertEqual(msg.get_sequences(), ['unseen', 'flagged']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1604 | msg.remove_sequence('unseen') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1605 | self.assertEqual(msg.get_sequences(), ['flagged']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1606 | msg.add_sequence('foobar') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1607 | self.assertEqual(msg.get_sequences(), ['flagged', 'foobar']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1608 | msg.remove_sequence('replied') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1609 | self.assertEqual(msg.get_sequences(), ['flagged', 'foobar']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1610 | msg.set_sequences(['foobar', 'replied']) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1611 | self.assertEqual(msg.get_sequences(), ['foobar', 'replied']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1612 | |
| 1613 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 1614 | class TestBabylMessage(TestMessage, unittest.TestCase): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1615 | |
| 1616 | _factory = mailbox.BabylMessage |
| 1617 | |
| 1618 | def _post_initialize_hook(self, msg): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1619 | self.assertEqual(msg._labels, []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1620 | |
| 1621 | def test_labels(self): |
| 1622 | # Get, set, join, and leave labels |
| 1623 | msg = mailbox.BabylMessage(_sample_message) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1624 | self.assertEqual(msg.get_labels(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1625 | msg.set_labels(['foobar']) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1626 | self.assertEqual(msg.get_labels(), ['foobar']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1627 | msg.set_labels([]) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1628 | self.assertEqual(msg.get_labels(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1629 | msg.add_label('filed') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1630 | self.assertEqual(msg.get_labels(), ['filed']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1631 | msg.add_label('resent') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1632 | self.assertEqual(msg.get_labels(), ['filed', 'resent']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1633 | msg.add_label('resent') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1634 | self.assertEqual(msg.get_labels(), ['filed', 'resent']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1635 | msg.remove_label('filed') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1636 | self.assertEqual(msg.get_labels(), ['resent']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1637 | msg.add_label('foobar') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1638 | self.assertEqual(msg.get_labels(), ['resent', 'foobar']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1639 | msg.remove_label('unseen') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1640 | self.assertEqual(msg.get_labels(), ['resent', 'foobar']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1641 | msg.set_labels(['foobar', 'answered']) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1642 | self.assertEqual(msg.get_labels(), ['foobar', 'answered']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1643 | |
| 1644 | def test_visible(self): |
| 1645 | # Get, set, and update visible headers |
| 1646 | msg = mailbox.BabylMessage(_sample_message) |
| 1647 | visible = msg.get_visible() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1648 | self.assertEqual(visible.keys(), []) |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 1649 | self.assertIsNone(visible.get_payload()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1650 | visible['User-Agent'] = 'FooBar 1.0' |
| 1651 | visible['X-Whatever'] = 'Blah' |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1652 | self.assertEqual(msg.get_visible().keys(), []) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1653 | msg.set_visible(visible) |
| 1654 | visible = msg.get_visible() |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 1655 | self.assertEqual(visible.keys(), ['User-Agent', 'X-Whatever']) |
| 1656 | self.assertEqual(visible['User-Agent'], 'FooBar 1.0') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1657 | self.assertEqual(visible['X-Whatever'], 'Blah') |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 1658 | self.assertIsNone(visible.get_payload()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1659 | msg.update_visible() |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1660 | self.assertEqual(visible.keys(), ['User-Agent', 'X-Whatever']) |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 1661 | self.assertIsNone(visible.get_payload()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1662 | visible = msg.get_visible() |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1663 | self.assertEqual(visible.keys(), ['User-Agent', 'Date', 'From', 'To', |
| 1664 | 'Subject']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1665 | for header in ('User-Agent', 'Date', 'From', 'To', 'Subject'): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1666 | self.assertEqual(visible[header], msg[header]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1667 | |
| 1668 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 1669 | class TestMMDFMessage(_TestMboxMMDFMessage, TestMessage): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1670 | |
| 1671 | _factory = mailbox.MMDFMessage |
| 1672 | |
| 1673 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 1674 | class TestMessageConversion(TestBase, unittest.TestCase): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1675 | |
| 1676 | def test_plain_to_x(self): |
| 1677 | # Convert Message to all formats |
R David Murray | c988e24 | 2012-04-08 21:00:27 -0400 | [diff] [blame] | 1678 | for class_ in self.all_mailbox_types: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1679 | msg_plain = mailbox.Message(_sample_message) |
| 1680 | msg = class_(msg_plain) |
| 1681 | self._check_sample(msg) |
| 1682 | |
| 1683 | def test_x_to_plain(self): |
| 1684 | # Convert all formats to Message |
R David Murray | c988e24 | 2012-04-08 21:00:27 -0400 | [diff] [blame] | 1685 | for class_ in self.all_mailbox_types: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1686 | msg = class_(_sample_message) |
| 1687 | msg_plain = mailbox.Message(msg) |
| 1688 | self._check_sample(msg_plain) |
| 1689 | |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 1690 | def test_x_from_bytes(self): |
| 1691 | # Convert all formats to Message |
R David Murray | c988e24 | 2012-04-08 21:00:27 -0400 | [diff] [blame] | 1692 | for class_ in self.all_mailbox_types: |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 1693 | msg = class_(_bytes_sample_message) |
| 1694 | self._check_sample(msg) |
| 1695 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1696 | def test_x_to_invalid(self): |
| 1697 | # Convert all formats to an invalid format |
R David Murray | c988e24 | 2012-04-08 21:00:27 -0400 | [diff] [blame] | 1698 | for class_ in self.all_mailbox_types: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1699 | self.assertRaises(TypeError, lambda: class_(False)) |
| 1700 | |
R David Murray | b019ee7 | 2012-04-08 22:36:07 -0400 | [diff] [blame] | 1701 | def test_type_specific_attributes_removed_on_conversion(self): |
| 1702 | reference = {class_: class_(_sample_message).__dict__ |
| 1703 | for class_ in self.all_mailbox_types} |
| 1704 | for class1 in self.all_mailbox_types: |
| 1705 | for class2 in self.all_mailbox_types: |
| 1706 | if class1 is class2: |
| 1707 | continue |
| 1708 | source = class1(_sample_message) |
| 1709 | target = class2(source) |
| 1710 | type_specific = [a for a in reference[class1] |
| 1711 | if a not in reference[class2]] |
| 1712 | for attr in type_specific: |
| 1713 | self.assertNotIn(attr, target.__dict__, |
| 1714 | "while converting {} to {}".format(class1, class2)) |
| 1715 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1716 | def test_maildir_to_maildir(self): |
| 1717 | # Convert MaildirMessage to MaildirMessage |
| 1718 | msg_maildir = mailbox.MaildirMessage(_sample_message) |
| 1719 | msg_maildir.set_flags('DFPRST') |
| 1720 | msg_maildir.set_subdir('cur') |
| 1721 | date = msg_maildir.get_date() |
| 1722 | msg = mailbox.MaildirMessage(msg_maildir) |
| 1723 | self._check_sample(msg) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1724 | self.assertEqual(msg.get_flags(), 'DFPRST') |
| 1725 | self.assertEqual(msg.get_subdir(), 'cur') |
| 1726 | self.assertEqual(msg.get_date(), date) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1727 | |
| 1728 | def test_maildir_to_mboxmmdf(self): |
| 1729 | # Convert MaildirMessage to mboxmessage and MMDFMessage |
| 1730 | pairs = (('D', ''), ('F', 'F'), ('P', ''), ('R', 'A'), ('S', 'R'), |
| 1731 | ('T', 'D'), ('DFPRST', 'RDFA')) |
| 1732 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 1733 | msg_maildir = mailbox.MaildirMessage(_sample_message) |
| 1734 | msg_maildir.set_date(0.0) |
| 1735 | for setting, result in pairs: |
| 1736 | msg_maildir.set_flags(setting) |
| 1737 | msg = class_(msg_maildir) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1738 | self.assertEqual(msg.get_flags(), result) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1739 | self.assertEqual(msg.get_from(), 'MAILER-DAEMON %s' % |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1740 | time.asctime(time.gmtime(0.0))) |
| 1741 | msg_maildir.set_subdir('cur') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1742 | self.assertEqual(class_(msg_maildir).get_flags(), 'RODFA') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1743 | |
| 1744 | def test_maildir_to_mh(self): |
| 1745 | # Convert MaildirMessage to MHMessage |
| 1746 | msg_maildir = mailbox.MaildirMessage(_sample_message) |
| 1747 | pairs = (('D', ['unseen']), ('F', ['unseen', 'flagged']), |
| 1748 | ('P', ['unseen']), ('R', ['unseen', 'replied']), ('S', []), |
| 1749 | ('T', ['unseen']), ('DFPRST', ['replied', 'flagged'])) |
| 1750 | for setting, result in pairs: |
| 1751 | msg_maildir.set_flags(setting) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1752 | self.assertEqual(mailbox.MHMessage(msg_maildir).get_sequences(), |
| 1753 | result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1754 | |
| 1755 | def test_maildir_to_babyl(self): |
| 1756 | # Convert MaildirMessage to Babyl |
| 1757 | msg_maildir = mailbox.MaildirMessage(_sample_message) |
| 1758 | pairs = (('D', ['unseen']), ('F', ['unseen']), |
| 1759 | ('P', ['unseen', 'forwarded']), ('R', ['unseen', 'answered']), |
| 1760 | ('S', []), ('T', ['unseen', 'deleted']), |
| 1761 | ('DFPRST', ['deleted', 'answered', 'forwarded'])) |
| 1762 | for setting, result in pairs: |
| 1763 | msg_maildir.set_flags(setting) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1764 | self.assertEqual(mailbox.BabylMessage(msg_maildir).get_labels(), |
| 1765 | result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1766 | |
| 1767 | def test_mboxmmdf_to_maildir(self): |
| 1768 | # Convert mboxMessage and MMDFMessage to MaildirMessage |
| 1769 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 1770 | msg_mboxMMDF = class_(_sample_message) |
| 1771 | msg_mboxMMDF.set_from('foo@bar', time.gmtime(0.0)) |
| 1772 | pairs = (('R', 'S'), ('O', ''), ('D', 'T'), ('F', 'F'), ('A', 'R'), |
| 1773 | ('RODFA', 'FRST')) |
| 1774 | for setting, result in pairs: |
| 1775 | msg_mboxMMDF.set_flags(setting) |
| 1776 | msg = mailbox.MaildirMessage(msg_mboxMMDF) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1777 | self.assertEqual(msg.get_flags(), result) |
Alexandre Vassalotti | b27a1d2 | 2009-07-22 23:27:17 +0000 | [diff] [blame] | 1778 | self.assertEqual(msg.get_date(), 0.0) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1779 | msg_mboxMMDF.set_flags('O') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1780 | self.assertEqual(mailbox.MaildirMessage(msg_mboxMMDF).get_subdir(), |
| 1781 | 'cur') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1782 | |
| 1783 | def test_mboxmmdf_to_mboxmmdf(self): |
| 1784 | # Convert mboxMessage and MMDFMessage to mboxMessage and MMDFMessage |
| 1785 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 1786 | msg_mboxMMDF = class_(_sample_message) |
| 1787 | msg_mboxMMDF.set_flags('RODFA') |
| 1788 | msg_mboxMMDF.set_from('foo@bar') |
| 1789 | for class2_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 1790 | msg2 = class2_(msg_mboxMMDF) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1791 | self.assertEqual(msg2.get_flags(), 'RODFA') |
| 1792 | self.assertEqual(msg2.get_from(), 'foo@bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1793 | |
| 1794 | def test_mboxmmdf_to_mh(self): |
| 1795 | # Convert mboxMessage and MMDFMessage to MHMessage |
| 1796 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 1797 | msg_mboxMMDF = class_(_sample_message) |
| 1798 | pairs = (('R', []), ('O', ['unseen']), ('D', ['unseen']), |
| 1799 | ('F', ['unseen', 'flagged']), |
| 1800 | ('A', ['unseen', 'replied']), |
| 1801 | ('RODFA', ['replied', 'flagged'])) |
| 1802 | for setting, result in pairs: |
| 1803 | msg_mboxMMDF.set_flags(setting) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1804 | self.assertEqual(mailbox.MHMessage(msg_mboxMMDF).get_sequences(), |
| 1805 | result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1806 | |
| 1807 | def test_mboxmmdf_to_babyl(self): |
| 1808 | # Convert mboxMessage and MMDFMessage to BabylMessage |
| 1809 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 1810 | msg = class_(_sample_message) |
| 1811 | pairs = (('R', []), ('O', ['unseen']), |
| 1812 | ('D', ['unseen', 'deleted']), ('F', ['unseen']), |
| 1813 | ('A', ['unseen', 'answered']), |
| 1814 | ('RODFA', ['deleted', 'answered'])) |
| 1815 | for setting, result in pairs: |
| 1816 | msg.set_flags(setting) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1817 | self.assertEqual(mailbox.BabylMessage(msg).get_labels(), result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1818 | |
| 1819 | def test_mh_to_maildir(self): |
| 1820 | # Convert MHMessage to MaildirMessage |
| 1821 | pairs = (('unseen', ''), ('replied', 'RS'), ('flagged', 'FS')) |
| 1822 | for setting, result in pairs: |
| 1823 | msg = mailbox.MHMessage(_sample_message) |
| 1824 | msg.add_sequence(setting) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1825 | self.assertEqual(mailbox.MaildirMessage(msg).get_flags(), result) |
| 1826 | self.assertEqual(mailbox.MaildirMessage(msg).get_subdir(), 'cur') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1827 | msg = mailbox.MHMessage(_sample_message) |
| 1828 | msg.add_sequence('unseen') |
| 1829 | msg.add_sequence('replied') |
| 1830 | msg.add_sequence('flagged') |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1831 | self.assertEqual(mailbox.MaildirMessage(msg).get_flags(), 'FR') |
| 1832 | self.assertEqual(mailbox.MaildirMessage(msg).get_subdir(), 'cur') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1833 | |
| 1834 | def test_mh_to_mboxmmdf(self): |
| 1835 | # Convert MHMessage to mboxMessage and MMDFMessage |
| 1836 | pairs = (('unseen', 'O'), ('replied', 'ROA'), ('flagged', 'ROF')) |
| 1837 | for setting, result in pairs: |
| 1838 | msg = mailbox.MHMessage(_sample_message) |
| 1839 | msg.add_sequence(setting) |
| 1840 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1841 | self.assertEqual(class_(msg).get_flags(), result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1842 | msg = mailbox.MHMessage(_sample_message) |
| 1843 | msg.add_sequence('unseen') |
| 1844 | msg.add_sequence('replied') |
| 1845 | msg.add_sequence('flagged') |
| 1846 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1847 | self.assertEqual(class_(msg).get_flags(), 'OFA') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1848 | |
| 1849 | def test_mh_to_mh(self): |
| 1850 | # Convert MHMessage to MHMessage |
| 1851 | msg = mailbox.MHMessage(_sample_message) |
| 1852 | msg.add_sequence('unseen') |
| 1853 | msg.add_sequence('replied') |
| 1854 | msg.add_sequence('flagged') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1855 | self.assertEqual(mailbox.MHMessage(msg).get_sequences(), |
| 1856 | ['unseen', 'replied', 'flagged']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1857 | |
| 1858 | def test_mh_to_babyl(self): |
| 1859 | # Convert MHMessage to BabylMessage |
| 1860 | pairs = (('unseen', ['unseen']), ('replied', ['answered']), |
| 1861 | ('flagged', [])) |
| 1862 | for setting, result in pairs: |
| 1863 | msg = mailbox.MHMessage(_sample_message) |
| 1864 | msg.add_sequence(setting) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1865 | self.assertEqual(mailbox.BabylMessage(msg).get_labels(), result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1866 | msg = mailbox.MHMessage(_sample_message) |
| 1867 | msg.add_sequence('unseen') |
| 1868 | msg.add_sequence('replied') |
| 1869 | msg.add_sequence('flagged') |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1870 | self.assertEqual(mailbox.BabylMessage(msg).get_labels(), |
| 1871 | ['unseen', 'answered']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1872 | |
| 1873 | def test_babyl_to_maildir(self): |
| 1874 | # Convert BabylMessage to MaildirMessage |
| 1875 | pairs = (('unseen', ''), ('deleted', 'ST'), ('filed', 'S'), |
| 1876 | ('answered', 'RS'), ('forwarded', 'PS'), ('edited', 'S'), |
| 1877 | ('resent', 'PS')) |
| 1878 | for setting, result in pairs: |
| 1879 | msg = mailbox.BabylMessage(_sample_message) |
| 1880 | msg.add_label(setting) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1881 | self.assertEqual(mailbox.MaildirMessage(msg).get_flags(), result) |
| 1882 | self.assertEqual(mailbox.MaildirMessage(msg).get_subdir(), 'cur') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1883 | msg = mailbox.BabylMessage(_sample_message) |
| 1884 | for label in ('unseen', 'deleted', 'filed', 'answered', 'forwarded', |
| 1885 | 'edited', 'resent'): |
| 1886 | msg.add_label(label) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1887 | self.assertEqual(mailbox.MaildirMessage(msg).get_flags(), 'PRT') |
| 1888 | self.assertEqual(mailbox.MaildirMessage(msg).get_subdir(), 'cur') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1889 | |
| 1890 | def test_babyl_to_mboxmmdf(self): |
| 1891 | # Convert BabylMessage to mboxMessage and MMDFMessage |
| 1892 | pairs = (('unseen', 'O'), ('deleted', 'ROD'), ('filed', 'RO'), |
| 1893 | ('answered', 'ROA'), ('forwarded', 'RO'), ('edited', 'RO'), |
| 1894 | ('resent', 'RO')) |
| 1895 | for setting, result in pairs: |
| 1896 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
| 1897 | msg = mailbox.BabylMessage(_sample_message) |
| 1898 | msg.add_label(setting) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1899 | self.assertEqual(class_(msg).get_flags(), result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1900 | msg = mailbox.BabylMessage(_sample_message) |
| 1901 | for label in ('unseen', 'deleted', 'filed', 'answered', 'forwarded', |
| 1902 | 'edited', 'resent'): |
| 1903 | msg.add_label(label) |
| 1904 | for class_ in (mailbox.mboxMessage, mailbox.MMDFMessage): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1905 | self.assertEqual(class_(msg).get_flags(), 'ODA') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1906 | |
| 1907 | def test_babyl_to_mh(self): |
| 1908 | # Convert BabylMessage to MHMessage |
| 1909 | pairs = (('unseen', ['unseen']), ('deleted', []), ('filed', []), |
| 1910 | ('answered', ['replied']), ('forwarded', []), ('edited', []), |
| 1911 | ('resent', [])) |
| 1912 | for setting, result in pairs: |
| 1913 | msg = mailbox.BabylMessage(_sample_message) |
| 1914 | msg.add_label(setting) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1915 | self.assertEqual(mailbox.MHMessage(msg).get_sequences(), result) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1916 | msg = mailbox.BabylMessage(_sample_message) |
| 1917 | for label in ('unseen', 'deleted', 'filed', 'answered', 'forwarded', |
| 1918 | 'edited', 'resent'): |
| 1919 | msg.add_label(label) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1920 | self.assertEqual(mailbox.MHMessage(msg).get_sequences(), |
| 1921 | ['unseen', 'replied']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1922 | |
| 1923 | def test_babyl_to_babyl(self): |
| 1924 | # Convert BabylMessage to BabylMessage |
| 1925 | msg = mailbox.BabylMessage(_sample_message) |
| 1926 | msg.update_visible() |
| 1927 | for label in ('unseen', 'deleted', 'filed', 'answered', 'forwarded', |
| 1928 | 'edited', 'resent'): |
| 1929 | msg.add_label(label) |
| 1930 | msg2 = mailbox.BabylMessage(msg) |
Guido van Rossum | ea83447 | 2007-07-27 05:17:50 +0000 | [diff] [blame] | 1931 | self.assertEqual(msg2.get_labels(), ['unseen', 'deleted', 'filed', |
| 1932 | 'answered', 'forwarded', 'edited', |
| 1933 | 'resent']) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1934 | self.assertEqual(msg.get_visible().keys(), msg2.get_visible().keys()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1935 | for key in msg.get_visible().keys(): |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1936 | self.assertEqual(msg.get_visible()[key], msg2.get_visible()[key]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1937 | |
| 1938 | |
| 1939 | class TestProxyFileBase(TestBase): |
| 1940 | |
| 1941 | def _test_read(self, proxy): |
| 1942 | # Read by byte |
| 1943 | proxy.seek(0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1944 | self.assertEqual(proxy.read(), b'bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1945 | proxy.seek(1) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1946 | self.assertEqual(proxy.read(), b'ar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1947 | proxy.seek(0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1948 | self.assertEqual(proxy.read(2), b'ba') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1949 | proxy.seek(1) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1950 | self.assertEqual(proxy.read(-1), b'ar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1951 | proxy.seek(2) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1952 | self.assertEqual(proxy.read(1000), b'r') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1953 | |
| 1954 | def _test_readline(self, proxy): |
| 1955 | # Read by line |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1956 | linesep = os.linesep.encode() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1957 | proxy.seek(0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1958 | self.assertEqual(proxy.readline(), b'foo' + linesep) |
| 1959 | self.assertEqual(proxy.readline(), b'bar' + linesep) |
| 1960 | self.assertEqual(proxy.readline(), b'fred' + linesep) |
| 1961 | self.assertEqual(proxy.readline(), b'bob') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1962 | proxy.seek(2) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1963 | self.assertEqual(proxy.readline(), b'o' + linesep) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1964 | proxy.seek(6 + 2 * len(os.linesep)) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1965 | self.assertEqual(proxy.readline(), b'fred' + linesep) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1966 | proxy.seek(6 + 2 * len(os.linesep)) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1967 | self.assertEqual(proxy.readline(2), b'fr') |
| 1968 | self.assertEqual(proxy.readline(-10), b'ed' + linesep) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1969 | |
| 1970 | def _test_readlines(self, proxy): |
| 1971 | # Read multiple lines |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1972 | linesep = os.linesep.encode() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1973 | proxy.seek(0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1974 | self.assertEqual(proxy.readlines(), [b'foo' + linesep, |
| 1975 | b'bar' + linesep, |
| 1976 | b'fred' + linesep, b'bob']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1977 | proxy.seek(0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1978 | self.assertEqual(proxy.readlines(2), [b'foo' + linesep]) |
| 1979 | proxy.seek(3 + len(linesep)) |
| 1980 | self.assertEqual(proxy.readlines(4 + len(linesep)), |
| 1981 | [b'bar' + linesep, b'fred' + linesep]) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1982 | proxy.seek(3) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1983 | self.assertEqual(proxy.readlines(1000), [linesep, b'bar' + linesep, |
| 1984 | b'fred' + linesep, b'bob']) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1985 | |
| 1986 | def _test_iteration(self, proxy): |
| 1987 | # Iterate by line |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1988 | linesep = os.linesep.encode() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1989 | proxy.seek(0) |
| 1990 | iterator = iter(proxy) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1991 | self.assertEqual(next(iterator), b'foo' + linesep) |
| 1992 | self.assertEqual(next(iterator), b'bar' + linesep) |
| 1993 | self.assertEqual(next(iterator), b'fred' + linesep) |
| 1994 | self.assertEqual(next(iterator), b'bob') |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 1995 | self.assertRaises(StopIteration, next, iterator) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1996 | |
| 1997 | def _test_seek_and_tell(self, proxy): |
| 1998 | # Seek and use tell to check position |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1999 | linesep = os.linesep.encode() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2000 | proxy.seek(3) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 2001 | self.assertEqual(proxy.tell(), 3) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2002 | self.assertEqual(proxy.read(len(linesep)), linesep) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2003 | proxy.seek(2, 1) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2004 | self.assertEqual(proxy.read(1 + len(linesep)), b'r' + linesep) |
| 2005 | proxy.seek(-3 - len(linesep), 2) |
| 2006 | self.assertEqual(proxy.read(3), b'bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2007 | proxy.seek(2, 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2008 | self.assertEqual(proxy.read(), b'o' + linesep + b'bar' + linesep) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2009 | proxy.seek(100) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2010 | self.assertFalse(proxy.read()) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2011 | |
| 2012 | def _test_close(self, proxy): |
| 2013 | # Close a file |
R David Murray | c88bce1 | 2011-06-17 22:24:05 -0400 | [diff] [blame] | 2014 | self.assertFalse(proxy.closed) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2015 | proxy.close() |
R David Murray | c88bce1 | 2011-06-17 22:24:05 -0400 | [diff] [blame] | 2016 | self.assertTrue(proxy.closed) |
| 2017 | # Issue 11700 subsequent closes should be a no-op. |
| 2018 | proxy.close() |
| 2019 | self.assertTrue(proxy.closed) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2020 | |
| 2021 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 2022 | class TestProxyFile(TestProxyFileBase, unittest.TestCase): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2023 | |
| 2024 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2025 | self._path = support.TESTFN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2026 | self._file = open(self._path, 'wb+') |
| 2027 | |
| 2028 | def tearDown(self): |
| 2029 | self._file.close() |
| 2030 | self._delete_recursively(self._path) |
| 2031 | |
| 2032 | def test_initialize(self): |
| 2033 | # Initialize and check position |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 2034 | self._file.write(b'foo') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2035 | pos = self._file.tell() |
| 2036 | proxy0 = mailbox._ProxyFile(self._file) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 2037 | self.assertEqual(proxy0.tell(), pos) |
| 2038 | self.assertEqual(self._file.tell(), pos) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2039 | proxy1 = mailbox._ProxyFile(self._file, 0) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 2040 | self.assertEqual(proxy1.tell(), 0) |
| 2041 | self.assertEqual(self._file.tell(), pos) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2042 | |
| 2043 | def test_read(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 2044 | self._file.write(b'bar') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2045 | self._test_read(mailbox._ProxyFile(self._file)) |
| 2046 | |
| 2047 | def test_readline(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 2048 | self._file.write(bytes('foo%sbar%sfred%sbob' % (os.linesep, os.linesep, |
| 2049 | os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2050 | self._test_readline(mailbox._ProxyFile(self._file)) |
| 2051 | |
| 2052 | def test_readlines(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 2053 | self._file.write(bytes('foo%sbar%sfred%sbob' % (os.linesep, os.linesep, |
| 2054 | os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2055 | self._test_readlines(mailbox._ProxyFile(self._file)) |
| 2056 | |
| 2057 | def test_iteration(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 2058 | self._file.write(bytes('foo%sbar%sfred%sbob' % (os.linesep, os.linesep, |
| 2059 | os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2060 | self._test_iteration(mailbox._ProxyFile(self._file)) |
| 2061 | |
| 2062 | def test_seek_and_tell(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 2063 | self._file.write(bytes('foo%sbar%s' % (os.linesep, os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2064 | self._test_seek_and_tell(mailbox._ProxyFile(self._file)) |
| 2065 | |
| 2066 | def test_close(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 2067 | self._file.write(bytes('foo%sbar%s' % (os.linesep, os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2068 | self._test_close(mailbox._ProxyFile(self._file)) |
| 2069 | |
| 2070 | |
R David Murray | 317c8d2 | 2012-04-08 16:46:18 -0400 | [diff] [blame] | 2071 | class TestPartialFile(TestProxyFileBase, unittest.TestCase): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2072 | |
| 2073 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2074 | self._path = support.TESTFN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2075 | self._file = open(self._path, 'wb+') |
| 2076 | |
| 2077 | def tearDown(self): |
| 2078 | self._file.close() |
| 2079 | self._delete_recursively(self._path) |
| 2080 | |
| 2081 | def test_initialize(self): |
| 2082 | # Initialize and check position |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 2083 | self._file.write(bytes('foo' + os.linesep + 'bar', 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2084 | pos = self._file.tell() |
| 2085 | proxy = mailbox._PartialFile(self._file, 2, 5) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 2086 | self.assertEqual(proxy.tell(), 0) |
| 2087 | self.assertEqual(self._file.tell(), pos) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2088 | |
| 2089 | def test_read(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 2090 | self._file.write(bytes('***bar***', 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2091 | self._test_read(mailbox._PartialFile(self._file, 3, 6)) |
| 2092 | |
| 2093 | def test_readline(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 2094 | self._file.write(bytes('!!!!!foo%sbar%sfred%sbob!!!!!' % |
| 2095 | (os.linesep, os.linesep, os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2096 | self._test_readline(mailbox._PartialFile(self._file, 5, |
| 2097 | 18 + 3 * len(os.linesep))) |
| 2098 | |
| 2099 | def test_readlines(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 2100 | self._file.write(bytes('foo%sbar%sfred%sbob?????' % |
| 2101 | (os.linesep, os.linesep, os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2102 | self._test_readlines(mailbox._PartialFile(self._file, 0, |
| 2103 | 13 + 3 * len(os.linesep))) |
| 2104 | |
| 2105 | def test_iteration(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 2106 | self._file.write(bytes('____foo%sbar%sfred%sbob####' % |
| 2107 | (os.linesep, os.linesep, os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2108 | self._test_iteration(mailbox._PartialFile(self._file, 4, |
| 2109 | 17 + 3 * len(os.linesep))) |
| 2110 | |
| 2111 | def test_seek_and_tell(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 2112 | self._file.write(bytes('(((foo%sbar%s$$$' % (os.linesep, os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2113 | self._test_seek_and_tell(mailbox._PartialFile(self._file, 3, |
| 2114 | 9 + 2 * len(os.linesep))) |
| 2115 | |
| 2116 | def test_close(self): |
Neal Norwitz | 9a92310 | 2007-08-30 06:20:05 +0000 | [diff] [blame] | 2117 | self._file.write(bytes('&foo%sbar%s^' % (os.linesep, os.linesep), 'ascii')) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2118 | self._test_close(mailbox._PartialFile(self._file, 1, |
| 2119 | 6 + 3 * len(os.linesep))) |
| 2120 | |
| 2121 | |
| 2122 | ## Start: tests from the original module (for backward compatibility). |
| 2123 | |
Skip Montanaro | 5253da1 | 2004-07-24 19:56:03 +0000 | [diff] [blame] | 2124 | FROM_ = "From some.body@dummy.domain Sat Jul 24 13:43:35 2004\n" |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 2125 | DUMMY_MESSAGE = """\ |
| 2126 | From: some.body@dummy.domain |
| 2127 | To: me@my.domain |
Skip Montanaro | 5253da1 | 2004-07-24 19:56:03 +0000 | [diff] [blame] | 2128 | Subject: Simple Test |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 2129 | |
| 2130 | This is a dummy message. |
| 2131 | """ |
| 2132 | |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 2133 | class MaildirTestCase(unittest.TestCase): |
| 2134 | |
| 2135 | def setUp(self): |
| 2136 | # create a new maildir mailbox to work with: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2137 | self._dir = support.TESTFN |
Vinay Sajip | f959618 | 2012-03-02 01:01:13 +0000 | [diff] [blame] | 2138 | if os.path.isdir(self._dir): |
R David Murray | b3e8384 | 2013-02-11 10:04:26 -0500 | [diff] [blame] | 2139 | support.rmtree(self._dir) |
Vinay Sajip | f959618 | 2012-03-02 01:01:13 +0000 | [diff] [blame] | 2140 | elif os.path.isfile(self._dir): |
R David Murray | b3e8384 | 2013-02-11 10:04:26 -0500 | [diff] [blame] | 2141 | support.unlink(self._dir) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 2142 | os.mkdir(self._dir) |
| 2143 | os.mkdir(os.path.join(self._dir, "cur")) |
| 2144 | os.mkdir(os.path.join(self._dir, "tmp")) |
| 2145 | os.mkdir(os.path.join(self._dir, "new")) |
| 2146 | self._counter = 1 |
| 2147 | self._msgfiles = [] |
| 2148 | |
| 2149 | def tearDown(self): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 2150 | list(map(os.unlink, self._msgfiles)) |
R David Murray | b3e8384 | 2013-02-11 10:04:26 -0500 | [diff] [blame] | 2151 | support.rmdir(os.path.join(self._dir, "cur")) |
| 2152 | support.rmdir(os.path.join(self._dir, "tmp")) |
| 2153 | support.rmdir(os.path.join(self._dir, "new")) |
| 2154 | support.rmdir(self._dir) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 2155 | |
Skip Montanaro | 5253da1 | 2004-07-24 19:56:03 +0000 | [diff] [blame] | 2156 | def createMessage(self, dir, mbox=False): |
Jack Jansen | 97df7b6 | 2001-06-19 20:20:05 +0000 | [diff] [blame] | 2157 | t = int(time.time() % 1000000) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 2158 | pid = self._counter |
| 2159 | self._counter += 1 |
Skip Montanaro | 7a98be2 | 2007-08-16 14:35:24 +0000 | [diff] [blame] | 2160 | filename = ".".join((str(t), str(pid), "myhostname", "mydomain")) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 2161 | tmpname = os.path.join(self._dir, "tmp", filename) |
| 2162 | newname = os.path.join(self._dir, dir, filename) |
Brian Curtin | 5acec04 | 2010-10-13 02:29:46 +0000 | [diff] [blame] | 2163 | with open(tmpname, "w") as fp: |
| 2164 | self._msgfiles.append(tmpname) |
| 2165 | if mbox: |
| 2166 | fp.write(FROM_) |
| 2167 | fp.write(DUMMY_MESSAGE) |
Xavier de Gaye | 452b3a6a | 2016-12-12 09:55:57 +0100 | [diff] [blame] | 2168 | try: |
Tim Peters | 6626c1f | 2001-05-22 16:29:01 +0000 | [diff] [blame] | 2169 | os.link(tmpname, newname) |
Xavier de Gaye | 452b3a6a | 2016-12-12 09:55:57 +0100 | [diff] [blame] | 2170 | except (AttributeError, PermissionError): |
Brian Curtin | 5acec04 | 2010-10-13 02:29:46 +0000 | [diff] [blame] | 2171 | with open(newname, "w") as fp: |
| 2172 | fp.write(DUMMY_MESSAGE) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 2173 | self._msgfiles.append(newname) |
Skip Montanaro | 5253da1 | 2004-07-24 19:56:03 +0000 | [diff] [blame] | 2174 | return tmpname |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 2175 | |
| 2176 | def test_empty_maildir(self): |
| 2177 | """Test an empty maildir mailbox""" |
| 2178 | # Test for regression on bug #117490: |
| 2179 | # Make sure the boxes attribute actually gets set. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2180 | self.mbox = mailbox.Maildir(support.TESTFN) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 2181 | #self.assertTrue(hasattr(self.mbox, "boxes")) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 2182 | #self.assertEqual(len(self.mbox.boxes), 0) |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 2183 | self.assertIsNone(self.mbox.next()) |
| 2184 | self.assertIsNone(self.mbox.next()) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 2185 | |
| 2186 | def test_nonempty_maildir_cur(self): |
| 2187 | self.createMessage("cur") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2188 | self.mbox = mailbox.Maildir(support.TESTFN) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 2189 | #self.assertEqual(len(self.mbox.boxes), 1) |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 2190 | self.assertIsNotNone(self.mbox.next()) |
| 2191 | self.assertIsNone(self.mbox.next()) |
| 2192 | self.assertIsNone(self.mbox.next()) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 2193 | |
| 2194 | def test_nonempty_maildir_new(self): |
| 2195 | self.createMessage("new") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2196 | self.mbox = mailbox.Maildir(support.TESTFN) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 2197 | #self.assertEqual(len(self.mbox.boxes), 1) |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 2198 | self.assertIsNotNone(self.mbox.next()) |
| 2199 | self.assertIsNone(self.mbox.next()) |
| 2200 | self.assertIsNone(self.mbox.next()) |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 2201 | |
| 2202 | def test_nonempty_maildir_both(self): |
| 2203 | self.createMessage("cur") |
| 2204 | self.createMessage("new") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2205 | self.mbox = mailbox.Maildir(support.TESTFN) |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 2206 | #self.assertEqual(len(self.mbox.boxes), 2) |
Serhiy Storchaka | 578c677 | 2014-02-08 15:06:08 +0200 | [diff] [blame] | 2207 | self.assertIsNotNone(self.mbox.next()) |
| 2208 | self.assertIsNotNone(self.mbox.next()) |
| 2209 | self.assertIsNone(self.mbox.next()) |
| 2210 | self.assertIsNone(self.mbox.next()) |
Fred Drake | 0aaed27 | 2000-10-23 13:39:15 +0000 | [diff] [blame] | 2211 | |
Georg Brandl | edb978f | 2008-05-11 08:59:40 +0000 | [diff] [blame] | 2212 | ## End: tests from the original module (for backward compatibility). |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2213 | |
| 2214 | |
| 2215 | _sample_message = """\ |
| 2216 | Return-Path: <gkj@gregorykjohnson.com> |
| 2217 | X-Original-To: gkj+person@localhost |
| 2218 | Delivered-To: gkj+person@localhost |
| 2219 | Received: from localhost (localhost [127.0.0.1]) |
| 2220 | by andy.gregorykjohnson.com (Postfix) with ESMTP id 356ED9DD17 |
| 2221 | for <gkj+person@localhost>; Wed, 13 Jul 2005 17:23:16 -0400 (EDT) |
| 2222 | Delivered-To: gkj@sundance.gregorykjohnson.com |
| 2223 | Received: from localhost [127.0.0.1] |
| 2224 | by localhost with POP3 (fetchmail-6.2.5) |
| 2225 | for gkj+person@localhost (single-drop); Wed, 13 Jul 2005 17:23:16 -0400 (EDT) |
| 2226 | Received: from andy.gregorykjohnson.com (andy.gregorykjohnson.com [64.32.235.228]) |
| 2227 | by sundance.gregorykjohnson.com (Postfix) with ESMTP id 5B056316746 |
| 2228 | for <gkj@gregorykjohnson.com>; Wed, 13 Jul 2005 17:23:11 -0400 (EDT) |
| 2229 | Received: by andy.gregorykjohnson.com (Postfix, from userid 1000) |
| 2230 | id 490CD9DD17; Wed, 13 Jul 2005 17:23:11 -0400 (EDT) |
| 2231 | Date: Wed, 13 Jul 2005 17:23:11 -0400 |
| 2232 | From: "Gregory K. Johnson" <gkj@gregorykjohnson.com> |
| 2233 | To: gkj@gregorykjohnson.com |
| 2234 | Subject: Sample message |
| 2235 | Message-ID: <20050713212311.GC4701@andy.gregorykjohnson.com> |
| 2236 | Mime-Version: 1.0 |
| 2237 | Content-Type: multipart/mixed; boundary="NMuMz9nt05w80d4+" |
| 2238 | Content-Disposition: inline |
| 2239 | User-Agent: Mutt/1.5.9i |
| 2240 | |
| 2241 | |
| 2242 | --NMuMz9nt05w80d4+ |
| 2243 | Content-Type: text/plain; charset=us-ascii |
| 2244 | Content-Disposition: inline |
| 2245 | |
| 2246 | This is a sample message. |
| 2247 | |
| 2248 | -- |
| 2249 | Gregory K. Johnson |
| 2250 | |
| 2251 | --NMuMz9nt05w80d4+ |
| 2252 | Content-Type: application/octet-stream |
| 2253 | Content-Disposition: attachment; filename="text.gz" |
| 2254 | Content-Transfer-Encoding: base64 |
| 2255 | |
| 2256 | H4sICM2D1UIAA3RleHQAC8nILFYAokSFktSKEoW0zJxUPa7wzJIMhZLyfIWczLzUYj0uAHTs |
| 2257 | 3FYlAAAA |
| 2258 | |
| 2259 | --NMuMz9nt05w80d4+-- |
| 2260 | """ |
| 2261 | |
R. David Murray | b7deff1 | 2011-01-30 06:21:28 +0000 | [diff] [blame] | 2262 | _bytes_sample_message = _sample_message.encode('ascii') |
| 2263 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2264 | _sample_headers = { |
| 2265 | "Return-Path":"<gkj@gregorykjohnson.com>", |
| 2266 | "X-Original-To":"gkj+person@localhost", |
| 2267 | "Delivered-To":"gkj+person@localhost", |
| 2268 | "Received":"""from localhost (localhost [127.0.0.1]) |
| 2269 | by andy.gregorykjohnson.com (Postfix) with ESMTP id 356ED9DD17 |
| 2270 | for <gkj+person@localhost>; Wed, 13 Jul 2005 17:23:16 -0400 (EDT)""", |
| 2271 | "Delivered-To":"gkj@sundance.gregorykjohnson.com", |
| 2272 | "Received":"""from localhost [127.0.0.1] |
| 2273 | by localhost with POP3 (fetchmail-6.2.5) |
| 2274 | for gkj+person@localhost (single-drop); Wed, 13 Jul 2005 17:23:16 -0400 (EDT)""", |
| 2275 | "Received":"""from andy.gregorykjohnson.com (andy.gregorykjohnson.com [64.32.235.228]) |
| 2276 | by sundance.gregorykjohnson.com (Postfix) with ESMTP id 5B056316746 |
| 2277 | for <gkj@gregorykjohnson.com>; Wed, 13 Jul 2005 17:23:11 -0400 (EDT)""", |
| 2278 | "Received":"""by andy.gregorykjohnson.com (Postfix, from userid 1000) |
| 2279 | id 490CD9DD17; Wed, 13 Jul 2005 17:23:11 -0400 (EDT)""", |
| 2280 | "Date":"Wed, 13 Jul 2005 17:23:11 -0400", |
| 2281 | "From":""""Gregory K. Johnson" <gkj@gregorykjohnson.com>""", |
| 2282 | "To":"gkj@gregorykjohnson.com", |
| 2283 | "Subject":"Sample message", |
| 2284 | "Mime-Version":"1.0", |
| 2285 | "Content-Type":"""multipart/mixed; boundary="NMuMz9nt05w80d4+\"""", |
| 2286 | "Content-Disposition":"inline", |
| 2287 | "User-Agent": "Mutt/1.5.9i" } |
| 2288 | |
| 2289 | _sample_payloads = ("""This is a sample message. |
| 2290 | |
| 2291 | -- |
| 2292 | Gregory K. Johnson |
| 2293 | """, |
| 2294 | """H4sICM2D1UIAA3RleHQAC8nILFYAokSFktSKEoW0zJxUPa7wzJIMhZLyfIWczLzUYj0uAHTs |
| 2295 | 3FYlAAAA |
| 2296 | """) |
Fred Drake | 0aaed27 | 2000-10-23 13:39:15 +0000 | [diff] [blame] | 2297 | |
Fred Drake | c02bc3e | 2001-05-21 20:23:21 +0000 | [diff] [blame] | 2298 | |
Martin Panter | ed84ab3 | 2016-06-06 01:56:09 +0000 | [diff] [blame] | 2299 | class MiscTestCase(unittest.TestCase): |
| 2300 | def test__all__(self): |
Martin Panter | 83815ae | 2016-06-06 02:49:54 +0000 | [diff] [blame] | 2301 | blacklist = {"linesep", "fcntl"} |
Martin Panter | ed84ab3 | 2016-06-06 01:56:09 +0000 | [diff] [blame] | 2302 | support.check__all__(self, mailbox, blacklist=blacklist) |
| 2303 | |
| 2304 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 2305 | def test_main(): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2306 | tests = (TestMailboxSuperclass, TestMaildir, TestMbox, TestMMDF, TestMH, |
| 2307 | TestBabyl, TestMessage, TestMaildirMessage, TestMboxMessage, |
| 2308 | TestMHMessage, TestBabylMessage, TestMMDFMessage, |
| 2309 | TestMessageConversion, TestProxyFile, TestPartialFile, |
Martin Panter | ed84ab3 | 2016-06-06 01:56:09 +0000 | [diff] [blame] | 2310 | MaildirTestCase, TestFakeMailBox, MiscTestCase) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2311 | support.run_unittest(*tests) |
| 2312 | support.reap_children() |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 2313 | |
| 2314 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2315 | if __name__ == '__main__': |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 2316 | test_main() |