Guido van Rossum | 2f1064c | 2001-01-18 16:46:52 +0000 | [diff] [blame] | 1 | """Test script for the dumbdbm module |
| 2 | Original by Roger E. Masse |
| 3 | """ |
| 4 | |
Guido van Rossum | 6252e10 | 2007-05-23 20:51:02 +0000 | [diff] [blame] | 5 | import io |
Benjamin Peterson | e3083d3 | 2014-04-26 16:56:52 -0400 | [diff] [blame] | 6 | import operator |
Guido van Rossum | 2f1064c | 2001-01-18 16:46:52 +0000 | [diff] [blame] | 7 | import os |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 8 | import unittest |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 9 | import dbm.dumb as dumbdbm |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 10 | from test import support |
Benjamin Peterson | e3083d3 | 2014-04-26 16:56:52 -0400 | [diff] [blame] | 11 | from functools import partial |
Guido van Rossum | 2f1064c | 2001-01-18 16:46:52 +0000 | [diff] [blame] | 12 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 13 | _fname = support.TESTFN |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 14 | |
| 15 | def _delete_files(): |
| 16 | for ext in [".dir", ".dat", ".bak"]: |
| 17 | try: |
| 18 | os.unlink(_fname + ext) |
| 19 | except OSError: |
| 20 | pass |
| 21 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 22 | class DumbDBMTestCase(unittest.TestCase): |
Brett Cannon | 58425d3 | 2008-11-21 00:17:53 +0000 | [diff] [blame] | 23 | _dict = {b'0': b'', |
| 24 | b'a': b'Python:', |
| 25 | b'b': b'Programming', |
| 26 | b'c': b'the', |
| 27 | b'd': b'way', |
| 28 | b'f': b'Guido', |
| 29 | b'g': b'intended', |
| 30 | '\u00fc'.encode('utf-8') : b'!', |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 33 | def test_dumbdbm_creation(self): |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 34 | f = dumbdbm.open(_fname, 'c') |
Brett Cannon | c1b22a0 | 2007-02-22 05:04:32 +0000 | [diff] [blame] | 35 | self.assertEqual(list(f.keys()), []) |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 36 | for key in self._dict: |
Brett Cannon | 58425d3 | 2008-11-21 00:17:53 +0000 | [diff] [blame] | 37 | f[key] = self._dict[key] |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 38 | self.read_helper(f) |
| 39 | f.close() |
| 40 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 41 | @unittest.skipUnless(hasattr(os, 'umask'), 'test needs os.umask()') |
| 42 | @unittest.skipUnless(hasattr(os, 'chmod'), 'test needs os.chmod()') |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 43 | def test_dumbdbm_creation_mode(self): |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 44 | try: |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 45 | old_umask = os.umask(0o002) |
| 46 | f = dumbdbm.open(_fname, 'c', 0o637) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 47 | f.close() |
| 48 | finally: |
| 49 | os.umask(old_umask) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 51 | expected_mode = 0o635 |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 52 | if os.name != 'posix': |
| 53 | # Windows only supports setting the read-only attribute. |
| 54 | # This shouldn't fail, but doesn't work like Unix either. |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 55 | expected_mode = 0o666 |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 56 | |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 57 | import stat |
| 58 | st = os.stat(_fname + '.dat') |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 59 | self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode) |
Thomas Wouters | 902d6eb | 2007-01-09 23:18:33 +0000 | [diff] [blame] | 60 | st = os.stat(_fname + '.dir') |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 61 | self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode) |
Thomas Wouters | 9fe394c | 2007-02-05 01:24:16 +0000 | [diff] [blame] | 62 | |
Tim Peters | 7a6c733 | 2003-07-13 17:21:10 +0000 | [diff] [blame] | 63 | def test_close_twice(self): |
| 64 | f = dumbdbm.open(_fname) |
Martin v. Löwis | e656869 | 2007-08-07 05:37:39 +0000 | [diff] [blame] | 65 | f[b'a'] = b'b' |
| 66 | self.assertEqual(f[b'a'], b'b') |
Tim Peters | 7a6c733 | 2003-07-13 17:21:10 +0000 | [diff] [blame] | 67 | f.close() |
| 68 | f.close() |
| 69 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 70 | def test_dumbdbm_modification(self): |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 71 | self.init_db() |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 72 | f = dumbdbm.open(_fname, 'w') |
Brett Cannon | 58425d3 | 2008-11-21 00:17:53 +0000 | [diff] [blame] | 73 | self._dict[b'g'] = f[b'g'] = b"indented" |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 74 | self.read_helper(f) |
| 75 | f.close() |
| 76 | |
| 77 | def test_dumbdbm_read(self): |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 78 | self.init_db() |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 79 | f = dumbdbm.open(_fname, 'r') |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 80 | self.read_helper(f) |
| 81 | f.close() |
| 82 | |
| 83 | def test_dumbdbm_keys(self): |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 84 | self.init_db() |
Tim Peters | d9fbf35 | 2002-01-30 07:32:53 +0000 | [diff] [blame] | 85 | f = dumbdbm.open(_fname) |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 86 | keys = self.keys_helper(f) |
| 87 | f.close() |
| 88 | |
Gregory P. Smith | 178fefb | 2007-08-24 21:59:45 +0000 | [diff] [blame] | 89 | def test_write_contains(self): |
| 90 | f = dumbdbm.open(_fname) |
| 91 | f[b'1'] = b'hello' |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 92 | self.assertIn(b'1', f) |
Gregory P. Smith | 178fefb | 2007-08-24 21:59:45 +0000 | [diff] [blame] | 93 | f.close() |
| 94 | |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 95 | def test_write_write_read(self): |
| 96 | # test for bug #482460 |
| 97 | f = dumbdbm.open(_fname) |
Martin v. Löwis | e656869 | 2007-08-07 05:37:39 +0000 | [diff] [blame] | 98 | f[b'1'] = b'hello' |
| 99 | f[b'1'] = b'hello2' |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 100 | f.close() |
| 101 | f = dumbdbm.open(_fname) |
Guido van Rossum | 7b4beea | 2007-08-28 00:09:54 +0000 | [diff] [blame] | 102 | self.assertEqual(f[b'1'], b'hello2') |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 103 | f.close() |
| 104 | |
Brett Cannon | 58425d3 | 2008-11-21 00:17:53 +0000 | [diff] [blame] | 105 | def test_str_read(self): |
| 106 | self.init_db() |
| 107 | f = dumbdbm.open(_fname, 'r') |
| 108 | self.assertEqual(f['\u00fc'], self._dict['\u00fc'.encode('utf-8')]) |
| 109 | |
| 110 | def test_str_write_contains(self): |
| 111 | self.init_db() |
| 112 | f = dumbdbm.open(_fname) |
| 113 | f['\u00fc'] = b'!' |
Brett Cannon | 7317c1e | 2008-11-25 19:19:17 +0000 | [diff] [blame] | 114 | f['1'] = 'a' |
Brett Cannon | 58425d3 | 2008-11-21 00:17:53 +0000 | [diff] [blame] | 115 | f.close() |
| 116 | f = dumbdbm.open(_fname, 'r') |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 117 | self.assertIn('\u00fc', f) |
Brett Cannon | 58425d3 | 2008-11-21 00:17:53 +0000 | [diff] [blame] | 118 | self.assertEqual(f['\u00fc'.encode('utf-8')], |
| 119 | self._dict['\u00fc'.encode('utf-8')]) |
Brett Cannon | 7317c1e | 2008-11-25 19:19:17 +0000 | [diff] [blame] | 120 | self.assertEqual(f[b'1'], b'a') |
Brett Cannon | 58425d3 | 2008-11-21 00:17:53 +0000 | [diff] [blame] | 121 | |
Andrew M. Kuchling | ecdad85 | 2005-06-07 19:36:10 +0000 | [diff] [blame] | 122 | def test_line_endings(self): |
| 123 | # test for bug #1172763: dumbdbm would die if the line endings |
| 124 | # weren't what was expected. |
| 125 | f = dumbdbm.open(_fname) |
Martin v. Löwis | e656869 | 2007-08-07 05:37:39 +0000 | [diff] [blame] | 126 | f[b'1'] = b'hello' |
| 127 | f[b'2'] = b'hello2' |
Andrew M. Kuchling | ecdad85 | 2005-06-07 19:36:10 +0000 | [diff] [blame] | 128 | f.close() |
| 129 | |
Christian Heimes | 9289bfc | 2007-10-31 19:40:17 +0000 | [diff] [blame] | 130 | # Mangle the file by changing the line separator to Windows or Unix |
Brett Cannon | c0eee31 | 2010-10-29 22:49:14 +0000 | [diff] [blame] | 131 | with io.open(_fname + '.dir', 'rb') as file: |
| 132 | data = file.read() |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 133 | if os.linesep == '\n': |
Christian Heimes | 9289bfc | 2007-10-31 19:40:17 +0000 | [diff] [blame] | 134 | data = data.replace(b'\n', b'\r\n') |
| 135 | else: |
| 136 | data = data.replace(b'\r\n', b'\n') |
Brett Cannon | c0eee31 | 2010-10-29 22:49:14 +0000 | [diff] [blame] | 137 | with io.open(_fname + '.dir', 'wb') as file: |
| 138 | file.write(data) |
Anthony Baxter | 95a2a4e | 2005-06-08 04:35:50 +0000 | [diff] [blame] | 139 | |
Andrew M. Kuchling | ecdad85 | 2005-06-07 19:36:10 +0000 | [diff] [blame] | 140 | f = dumbdbm.open(_fname) |
Martin v. Löwis | e656869 | 2007-08-07 05:37:39 +0000 | [diff] [blame] | 141 | self.assertEqual(f[b'1'], b'hello') |
| 142 | self.assertEqual(f[b'2'], b'hello2') |
Anthony Baxter | 95a2a4e | 2005-06-08 04:35:50 +0000 | [diff] [blame] | 143 | |
| 144 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 145 | def read_helper(self, f): |
| 146 | keys = self.keys_helper(f) |
| 147 | for key in self._dict: |
Brett Cannon | 58425d3 | 2008-11-21 00:17:53 +0000 | [diff] [blame] | 148 | self.assertEqual(self._dict[key], f[key]) |
Tim Peters | 8876848 | 2001-11-13 21:51:26 +0000 | [diff] [blame] | 149 | |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 150 | def init_db(self): |
| 151 | f = dumbdbm.open(_fname, 'w') |
| 152 | for k in self._dict: |
Brett Cannon | 58425d3 | 2008-11-21 00:17:53 +0000 | [diff] [blame] | 153 | f[k] = self._dict[k] |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 154 | f.close() |
| 155 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 156 | def keys_helper(self, f): |
Brett Cannon | 58425d3 | 2008-11-21 00:17:53 +0000 | [diff] [blame] | 157 | keys = sorted(f.keys()) |
Brett Cannon | c1b22a0 | 2007-02-22 05:04:32 +0000 | [diff] [blame] | 158 | dkeys = sorted(self._dict.keys()) |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 159 | self.assertEqual(keys, dkeys) |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 160 | return keys |
| 161 | |
Tim Peters | 663d1b6 | 2003-07-11 04:09:09 +0000 | [diff] [blame] | 162 | # Perform randomized operations. This doesn't make assumptions about |
| 163 | # what *might* fail. |
| 164 | def test_random(self): |
| 165 | import random |
| 166 | d = {} # mirror the database |
| 167 | for dummy in range(5): |
| 168 | f = dumbdbm.open(_fname) |
| 169 | for dummy in range(100): |
| 170 | k = random.choice('abcdefghijklm') |
| 171 | if random.random() < 0.2: |
| 172 | if k in d: |
| 173 | del d[k] |
Brett Cannon | 58425d3 | 2008-11-21 00:17:53 +0000 | [diff] [blame] | 174 | del f[k] |
Tim Peters | 663d1b6 | 2003-07-11 04:09:09 +0000 | [diff] [blame] | 175 | else: |
Guido van Rossum | 6252e10 | 2007-05-23 20:51:02 +0000 | [diff] [blame] | 176 | v = random.choice((b'a', b'b', b'c')) * random.randrange(10000) |
Tim Peters | 663d1b6 | 2003-07-11 04:09:09 +0000 | [diff] [blame] | 177 | d[k] = v |
Brett Cannon | 58425d3 | 2008-11-21 00:17:53 +0000 | [diff] [blame] | 178 | f[k] = v |
| 179 | self.assertEqual(f[k], v) |
Tim Peters | 663d1b6 | 2003-07-11 04:09:09 +0000 | [diff] [blame] | 180 | f.close() |
| 181 | |
| 182 | f = dumbdbm.open(_fname) |
Guido van Rossum | 7b4beea | 2007-08-28 00:09:54 +0000 | [diff] [blame] | 183 | expected = sorted((k.encode("latin-1"), v) for k, v in d.items()) |
Brett Cannon | c1b22a0 | 2007-02-22 05:04:32 +0000 | [diff] [blame] | 184 | got = sorted(f.items()) |
Tim Peters | 663d1b6 | 2003-07-11 04:09:09 +0000 | [diff] [blame] | 185 | self.assertEqual(expected, got) |
| 186 | f.close() |
| 187 | |
Nick Coghlan | c610aba | 2013-11-17 15:59:51 +1000 | [diff] [blame] | 188 | def test_context_manager(self): |
| 189 | with dumbdbm.open(_fname, 'c') as db: |
| 190 | db["dumbdbm context manager"] = "context manager" |
| 191 | |
| 192 | with dumbdbm.open(_fname, 'r') as db: |
| 193 | self.assertEqual(list(db.keys()), [b"dumbdbm context manager"]) |
| 194 | |
Benjamin Peterson | e3083d3 | 2014-04-26 16:56:52 -0400 | [diff] [blame] | 195 | with self.assertRaises(dumbdbm.error): |
Nick Coghlan | c610aba | 2013-11-17 15:59:51 +1000 | [diff] [blame] | 196 | db.keys() |
| 197 | |
Benjamin Peterson | e3083d3 | 2014-04-26 16:56:52 -0400 | [diff] [blame] | 198 | def test_check_closed(self): |
| 199 | f = dumbdbm.open(_fname, 'c') |
| 200 | f.close() |
| 201 | |
| 202 | for meth in (partial(operator.delitem, f), |
| 203 | partial(operator.setitem, f, 'b'), |
| 204 | partial(operator.getitem, f), |
| 205 | partial(operator.contains, f)): |
| 206 | with self.assertRaises(dumbdbm.error) as cm: |
| 207 | meth('test') |
| 208 | self.assertEqual(str(cm.exception), |
| 209 | "DBM object has already been closed") |
| 210 | |
| 211 | for meth in (operator.methodcaller('keys'), |
| 212 | operator.methodcaller('iterkeys'), |
| 213 | operator.methodcaller('items'), |
| 214 | len): |
| 215 | with self.assertRaises(dumbdbm.error) as cm: |
| 216 | meth(f) |
| 217 | self.assertEqual(str(cm.exception), |
| 218 | "DBM object has already been closed") |
| 219 | |
Serhiy Storchaka | b398d33 | 2014-06-10 21:16:00 +0300 | [diff] [blame] | 220 | def test_create_new(self): |
| 221 | with dumbdbm.open(_fname, 'n') as f: |
| 222 | for k in self._dict: |
| 223 | f[k] = self._dict[k] |
| 224 | |
| 225 | with dumbdbm.open(_fname, 'n') as f: |
| 226 | self.assertEqual(f.keys(), []) |
| 227 | |
Serhiy Storchaka | 74eb8b2 | 2015-02-16 00:30:43 +0200 | [diff] [blame] | 228 | def test_eval(self): |
| 229 | with open(_fname + '.dir', 'w') as stream: |
| 230 | stream.write("str(print('Hacked!')), 0\n") |
| 231 | with support.captured_stdout() as stdout: |
| 232 | with self.assertRaises(ValueError): |
| 233 | with dumbdbm.open(_fname) as f: |
| 234 | pass |
| 235 | self.assertEqual(stdout.getvalue(), '') |
| 236 | |
Skip Montanaro | c08fe82 | 2002-03-17 23:03:42 +0000 | [diff] [blame] | 237 | def tearDown(self): |
| 238 | _delete_files() |
| 239 | |
| 240 | def setUp(self): |
| 241 | _delete_files() |
| 242 | |
Barry Warsaw | 3ca656f | 2001-11-13 20:16:52 +0000 | [diff] [blame] | 243 | |
| 244 | if __name__ == "__main__": |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame] | 245 | unittest.main() |