1 from allmydata.test import common
2 from allmydata.interfaces import NotEnoughSharesError
3 from twisted.internet import defer
4 from twisted.trial import unittest
7 class Test(common.ShareManglingMixin, unittest.TestCase):
8 def test_test_code(self):
9 # The following process of stashing the shares, running
10 # replace_shares, and asserting that the new set of shares equals the
11 # old is more to test this test code than to test the Tahoe code...
12 d = defer.succeed(None)
13 d.addCallback(self.find_shares)
18 d.addCallback(_stash_it)
20 # The following process of deleting 8 of the shares and asserting that you can't
21 # download it is more to test this test code than to test the Tahoe code...
22 def _then_delete_8(unused=None):
23 self.replace_shares(stash[0], storage_index=self.uri.storage_index)
25 self._delete_a_share()
26 d.addCallback(_then_delete_8)
28 def _then_download(unused=None):
29 self.downloader = self.clients[1].getServiceNamed("downloader")
30 d2 = self.downloader.download_to_data(self.uri)
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 downloader = self.clients[1].getServiceNamed("downloader")
98 d2 = downloader.download_to_data(self.uri)
101 self.fail("Should have gotten an error from attempt to download, not %r" % (res,))
103 self.failUnless(f.check(NotEnoughSharesError))
104 d2.addCallbacks(_callb, _errb)
107 d.addCallback(_attempt_to_download)
109 def _after_attempt(unused=None):
110 after_download_reads = self._count_reads()
111 # To pass this test, you are required to give up before actually trying to read any
113 self.failIf(after_download_reads-before_download_reads > 0, (after_download_reads, before_download_reads))
114 d.addCallback(_after_attempt)
117 def test_download_abort_if_too_many_corrupted_shares(self):
118 """ Test that download gives up quickly when it realizes there aren't enough uncorrupted
119 shares out there. It should be able to tell because the corruption occurs in the
120 sharedata version number, which it checks first."""
121 d = defer.succeed(None)
122 def _then_corrupt_8(unused=None):
124 random.shuffle(shnums)
125 for shnum in shnums[:8]:
126 self._corrupt_a_share(None, common._corrupt_sharedata_version_number, shnum)
127 d.addCallback(_then_corrupt_8)
129 before_download_reads = self._count_reads()
130 def _attempt_to_download(unused=None):
131 downloader = self.clients[1].getServiceNamed("downloader")
132 d2 = downloader.download_to_data(self.uri)
135 self.fail("Should have gotten an error from attempt to download, not %r" % (res,))
137 self.failUnless(f.check(NotEnoughSharesError))
138 d2.addCallbacks(_callb, _errb)
141 d.addCallback(_attempt_to_download)
143 def _after_attempt(unused=None):
144 after_download_reads = self._count_reads()
145 # To pass this test, you are required to give up before reading all of the share
146 # data. Actually, we could give up sooner than 45 reads, but currently our download
147 # code does 45 reads. This test then serves as a "performance regression detector"
148 # -- if you change download code so that it takes *more* reads, then this test will
150 self.failIf(after_download_reads-before_download_reads > 45, (after_download_reads, before_download_reads))
151 d.addCallback(_after_attempt)
155 # XXX extend these tests to show bad behavior of various kinds from servers: raising exception from each remove_foo() method, for example
157 # XXX test disconnect DeadReferenceError from get_buckets and get_block_whatsit