166
167
self.assertTrue(db.live_file.has_bad_crc, 'has_bad_crc should be True.')
170
def test_iter_entries_bad_header_unpack(self):
171
"""Test that unpack error during iter_entries is the same as EOF."""
172
db = Tritcask(self.base_dir)
174
db.put(i, 'foo%d' % (i,), 'bar%s' % (i,))
175
# truncate the file at the start of the header of the last value.
176
curr_pos = db.live_file.fd.tell()
177
db.live_file.fd.seek(curr_pos - header_size+4)
178
db.live_file.fd.truncate()
180
for i, entry in enumerate(db.live_file.iter_entries()):
181
entries.append(entry)
182
self.assertEqual(entry[4], i)
183
self.assertEqual(entry[5], 'foo%d' % (i,))
184
self.assertEqual(entry[6], 'bar%d' % (i,))
185
self.assertEqual(9, len(entries))
186
self.assertTrue(db.live_file.has_bad_data, 'has_bad_data should be True.')
187
self.assertTrue(self.memento.check_warning('Found corrupted header on'))
188
self.assertTrue(self.memento.check_warning(
189
'the rest of the file will be ignored.'))
169
192
def test__getitem__(self):
170
193
"""Test our slicing support."""
171
194
data_file = self.file_class(self.base_dir)
247
270
with contextlib.closing(fmap):
248
271
self.assertRaises(BadCrc, data_file.read, fmap, 0)
273
def test_read_bad_header(self):
274
"""Test for read method with a bad header/unpack error."""
275
data_file = self.file_class(self.base_dir)
276
orig_tstamp, _, _ = data_file.write(0, 'foo', 'bar')
277
# mess with the data on disk to make this entry crc32 invalid
278
# seek to the end of crc32+header+key
280
with open(data_file.filename, 'r+b') as fd:
281
fd.read(crc32_size+4)
283
# write a different value -> random bytes
284
fd.write(os.urandom(header_size/2))
286
fmap = mmap.mmap(fd.fileno(), 0, access=mmap.ACCESS_READ)
287
with contextlib.closing(fmap):
288
self.assertRaises(BadHeader, data_file.read, fmap, 0)
251
291
class TempDataFileTest(DataFileTest):
252
292
"""Tests for TempDataFile."""
394
434
'the rest of the file will be ignored.'))
437
def test_iter_entries_bad_header_unpack(self):
438
"""Test that unpack error during iter_entries is the same as EOF."""
439
db = Tritcask(self.base_dir)
441
db.put(i, 'foo%d' % (i,), 'bar%s' % (i,))
442
# truncate the file at the start of the header of the last value.
443
db.live_file.fd.seek(crc32_size+header_size+8+crc32_size+4)
444
db.live_file.fd.truncate()
445
file_id = db.live_file.file_id
448
db = Tritcask(self.base_dir)
450
for i, entry in enumerate(db._immutable[file_id].iter_entries()):
451
entries.append(entry)
452
self.assertEqual(entry[4], i)
453
self.assertEqual(entry[5], 'foo%d' % (i,))
454
self.assertEqual(entry[6], 'bar%d' % (i,))
455
self.assertEqual(1, len(entries))
456
self.assertTrue(db._immutable[file_id].has_bad_data,
457
'has_bad_data should be True.')
458
self.assertTrue(self.memento.check_warning('Found corrupted header on'))
459
self.assertTrue(self.memento.check_warning(
460
'the rest of the file will be ignored.'))
397
464
def test__getitem__(self):
398
465
"""Test our slicing support."""
399
466
rw_file = DataFile(self.base_dir)
461
528
self.assertRaises(NotImplementedError, dead_file.read)
463
530
test_read_bad_crc = test_read
531
test_read_bad_header = test_read
465
533
def test__open(self):
466
534
"""Test that always fails with NotImplementedError."""