1 from allmydata.test import common
2 from allmydata.interfaces import NotEnoughSharesError
3 from allmydata.util.consumer import download_to_data
4 from twisted.internet import defer
5 from twisted.trial import unittest
8 class Test(common.ShareManglingMixin, common.ShouldFailMixin, unittest.TestCase):
9 def test_test_code(self):
10 # The following process of stashing the shares, running
11 # replace_shares, and asserting that the new set of shares equals the
12 # old is more to test this test code than to test the Tahoe code...
13 d = defer.succeed(None)
14 d.addCallback(self.find_all_shares)
19 d.addCallback(_stash_it)
21 # The following process of deleting 8 of the shares and asserting
22 # that you can't download it is more to test this test code than to
23 # test the Tahoe code...
24 def _then_delete_8(unused=None):
25 self.replace_shares(stash[0], storage_index=self.uri.get_storage_index())
27 self._delete_a_share()
28 d.addCallback(_then_delete_8)
30 def _then_download(unused=None):
31 d2 = download_to_data(self.n)
33 def _after_download_callb(result):
34 self.fail() # should have gotten an errback instead
36 def _after_download_errb(failure):
37 failure.trap(NotEnoughSharesError)
38 return None # success!
39 d2.addCallbacks(_after_download_callb, _after_download_errb)
41 d.addCallback(_then_download)
45 def test_download(self):
46 """ Basic download. (This functionality is more or less already
47 tested by test code in other modules, but this module is also going
48 to test some more specific things about immutable download.)
50 d = defer.succeed(None)
51 before_download_reads = self._count_reads()
52 def _after_download(unused=None):
53 after_download_reads = self._count_reads()
54 #print before_download_reads, after_download_reads
55 self.failIf(after_download_reads-before_download_reads > 41,
56 (after_download_reads, before_download_reads))
57 d.addCallback(self._download_and_check_plaintext)
58 d.addCallback(_after_download)
61 def test_download_from_only_3_remaining_shares(self):
62 """ Test download after 7 random shares (of the 10) have been
64 d = defer.succeed(None)
65 def _then_delete_7(unused=None):
67 self._delete_a_share()
68 before_download_reads = self._count_reads()
69 d.addCallback(_then_delete_7)
70 def _after_download(unused=None):
71 after_download_reads = self._count_reads()
72 #print before_download_reads, after_download_reads
73 self.failIf(after_download_reads-before_download_reads > 37, (after_download_reads, before_download_reads))
74 d.addCallback(self._download_and_check_plaintext)
75 d.addCallback(_after_download)
78 def test_download_from_only_3_shares_with_good_crypttext_hash(self):
79 """ Test download after 7 random shares (of the 10) have had their
80 crypttext hash tree corrupted."""
81 d = defer.succeed(None)
82 def _then_corrupt_7(unused=None):
84 random.shuffle(shnums)
86 self._corrupt_a_share(None, common._corrupt_offset_of_block_hashes_to_truncate_crypttext_hashes, i)
87 #before_download_reads = self._count_reads()
88 d.addCallback(_then_corrupt_7)
89 d.addCallback(self._download_and_check_plaintext)
92 def test_download_abort_if_too_many_missing_shares(self):
93 """ Test that download gives up quickly when it realizes there aren't
94 enough shares out there."""
96 self._delete_a_share()
97 d = self.shouldFail(NotEnoughSharesError, "delete 8", None,
98 download_to_data, self.n)
99 # the new downloader pipelines a bunch of read requests in parallel,
100 # so don't bother asserting anything about the number of reads
103 def test_download_abort_if_too_many_corrupted_shares(self):
104 """Test that download gives up quickly when it realizes there aren't
105 enough uncorrupted shares out there. It should be able to tell
106 because the corruption occurs in the sharedata version number, which
108 d = defer.succeed(None)
109 def _then_corrupt_8(unused=None):
111 random.shuffle(shnums)
112 for shnum in shnums[:8]:
113 self._corrupt_a_share(None, common._corrupt_sharedata_version_number, shnum)
114 d.addCallback(_then_corrupt_8)
116 before_download_reads = self._count_reads()
117 def _attempt_to_download(unused=None):
118 d2 = download_to_data(self.n)
121 self.fail("Should have gotten an error from attempt to download, not %r" % (res,))
123 self.failUnless(f.check(NotEnoughSharesError))
124 d2.addCallbacks(_callb, _errb)
127 d.addCallback(_attempt_to_download)
129 def _after_attempt(unused=None):
130 after_download_reads = self._count_reads()
131 #print before_download_reads, after_download_reads
132 # To pass this test, you are required to give up before reading
133 # all of the share data. Actually, we could give up sooner than
134 # 45 reads, but currently our download code does 45 reads. This
135 # test then serves as a "performance regression detector" -- if
136 # you change download code so that it takes *more* reads, then
137 # this test will fail.
138 self.failIf(after_download_reads-before_download_reads > 45,
139 (after_download_reads, before_download_reads))
140 d.addCallback(_after_attempt)
144 # XXX extend these tests to show bad behavior of various kinds from servers:
145 # raising exception from each remove_foo() method, for example
147 # XXX test disconnect DeadReferenceError from get_buckets and get_block_whatsit
149 # TODO: delete this whole file