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, 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_shares)
19 d.addCallback(_stash_it)
21 # The following process of deleting 8 of the shares and asserting that you can't
22 # download it is more to test this test code than to test the Tahoe code...
23 def _then_delete_8(unused=None):
24 self.replace_shares(stash[0], storage_index=self.uri.storage_index)
26 self._delete_a_share()
27 d.addCallback(_then_delete_8)
29 def _then_download(unused=None):
30 d2 = download_to_data(self.n)
32 def _after_download_callb(result):
33 self.fail() # should have gotten an errback instead
35 def _after_download_errb(failure):
36 failure.trap(NotEnoughSharesError)
37 return None # success!
38 d2.addCallbacks(_after_download_callb, _after_download_errb)
40 d.addCallback(_then_download)
44 def test_download(self):
45 """ Basic download. (This functionality is more or less already tested by test code in
46 other modules, but this module is also going to test some more specific things about
49 d = defer.succeed(None)
50 before_download_reads = self._count_reads()
51 def _after_download(unused=None):
52 after_download_reads = self._count_reads()
53 self.failIf(after_download_reads-before_download_reads > 27, (after_download_reads, before_download_reads))
54 d.addCallback(self._download_and_check_plaintext)
55 d.addCallback(_after_download)
58 def test_download_from_only_3_remaining_shares(self):
59 """ Test download after 7 random shares (of the 10) have been removed. """
60 d = defer.succeed(None)
61 def _then_delete_7(unused=None):
63 self._delete_a_share()
64 before_download_reads = self._count_reads()
65 d.addCallback(_then_delete_7)
66 def _after_download(unused=None):
67 after_download_reads = self._count_reads()
68 self.failIf(after_download_reads-before_download_reads > 27, (after_download_reads, before_download_reads))
69 d.addCallback(self._download_and_check_plaintext)
70 d.addCallback(_after_download)
73 def test_download_from_only_3_shares_with_good_crypttext_hash(self):
74 """ Test download after 7 random shares (of the 10) have had their crypttext hash tree corrupted. """
75 d = defer.succeed(None)
76 def _then_corrupt_7(unused=None):
78 random.shuffle(shnums)
80 self._corrupt_a_share(None, common._corrupt_offset_of_block_hashes_to_truncate_crypttext_hashes, i)
81 #before_download_reads = self._count_reads()
82 d.addCallback(_then_corrupt_7)
83 d.addCallback(self._download_and_check_plaintext)
86 def test_download_abort_if_too_many_missing_shares(self):
87 """ Test that download gives up quickly when it realizes there aren't enough shares out
89 d = defer.succeed(None)
90 def _then_delete_8(unused=None):
92 self._delete_a_share()
93 d.addCallback(_then_delete_8)
95 before_download_reads = self._count_reads()
96 def _attempt_to_download(unused=None):
97 d2 = download_to_data(self.n)
100 self.fail("Should have gotten an error from attempt to download, not %r" % (res,))
102 self.failUnless(f.check(NotEnoughSharesError))
103 d2.addCallbacks(_callb, _errb)
106 d.addCallback(_attempt_to_download)
108 def _after_attempt(unused=None):
109 after_download_reads = self._count_reads()
110 # To pass this test, you are required to give up before actually trying to read any
112 self.failIf(after_download_reads-before_download_reads > 0, (after_download_reads, before_download_reads))
113 d.addCallback(_after_attempt)
116 def test_download_abort_if_too_many_corrupted_shares(self):
117 """ Test that download gives up quickly when it realizes there aren't enough uncorrupted
118 shares out there. It should be able to tell because the corruption occurs in the
119 sharedata version number, which it checks first."""
120 d = defer.succeed(None)
121 def _then_corrupt_8(unused=None):
123 random.shuffle(shnums)
124 for shnum in shnums[:8]:
125 self._corrupt_a_share(None, common._corrupt_sharedata_version_number, shnum)
126 d.addCallback(_then_corrupt_8)
128 before_download_reads = self._count_reads()
129 def _attempt_to_download(unused=None):
130 d2 = download_to_data(self.n)
133 self.fail("Should have gotten an error from attempt to download, not %r" % (res,))
135 self.failUnless(f.check(NotEnoughSharesError))
136 d2.addCallbacks(_callb, _errb)
139 d.addCallback(_attempt_to_download)
141 def _after_attempt(unused=None):
142 after_download_reads = self._count_reads()
143 # To pass this test, you are required to give up before reading all of the share
144 # data. Actually, we could give up sooner than 45 reads, but currently our download
145 # code does 45 reads. This test then serves as a "performance regression detector"
146 # -- if you change download code so that it takes *more* reads, then this test will
148 self.failIf(after_download_reads-before_download_reads > 45, (after_download_reads, before_download_reads))
149 d.addCallback(_after_attempt)
153 # XXX extend these tests to show bad behavior of various kinds from servers: raising exception from each remove_foo() method, for example
155 # XXX test disconnect DeadReferenceError from get_buckets and get_block_whatsit