]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/test/test_immutable.py
hush pyflakes-0.4.0 warnings: remove trivial unused variables. For #900.
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / test / test_immutable.py
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
6 import random
7
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)
15         stash = [None]
16         def _stash_it(res):
17             stash[0] = res
18             return res
19         d.addCallback(_stash_it)
20
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)
25             for i in range(8):
26                 self._delete_a_share()
27         d.addCallback(_then_delete_8)
28
29         def _then_download(unused=None):
30             d2 = download_to_data(self.n)
31
32             def _after_download_callb(result):
33                 self.fail() # should have gotten an errback instead
34                 return result
35             def _after_download_errb(failure):
36                 failure.trap(NotEnoughSharesError)
37                 return None # success!
38             d2.addCallbacks(_after_download_callb, _after_download_errb)
39             return d2
40         d.addCallback(_then_download)
41
42         return d
43
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
47         immutable download.)
48         """
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)
56         return d
57
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):
62             for i in range(7):
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)
71         return d
72
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):
77             shnums = range(10)
78             random.shuffle(shnums)
79             for i in shnums[:7]:
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)
84         return d
85
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
88         there."""
89         d = defer.succeed(None)
90         def _then_delete_8(unused=None):
91             for i in range(8):
92                 self._delete_a_share()
93         d.addCallback(_then_delete_8)
94
95         before_download_reads = self._count_reads()
96         def _attempt_to_download(unused=None):
97             d2 = download_to_data(self.n)
98
99             def _callb(res):
100                 self.fail("Should have gotten an error from attempt to download, not %r" % (res,))
101             def _errb(f):
102                 self.failUnless(f.check(NotEnoughSharesError))
103             d2.addCallbacks(_callb, _errb)
104             return d2
105
106         d.addCallback(_attempt_to_download)
107
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
111             # share data.
112             self.failIf(after_download_reads-before_download_reads > 0, (after_download_reads, before_download_reads))
113         d.addCallback(_after_attempt)
114         return d
115
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):
122             shnums = range(10)
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)
127
128         before_download_reads = self._count_reads()
129         def _attempt_to_download(unused=None):
130             d2 = download_to_data(self.n)
131
132             def _callb(res):
133                 self.fail("Should have gotten an error from attempt to download, not %r" % (res,))
134             def _errb(f):
135                 self.failUnless(f.check(NotEnoughSharesError))
136             d2.addCallbacks(_callb, _errb)
137             return d2
138
139         d.addCallback(_attempt_to_download)
140
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
147             # fail.
148             self.failIf(after_download_reads-before_download_reads > 45, (after_download_reads, before_download_reads))
149         d.addCallback(_after_attempt)
150         return d
151
152
153 # XXX extend these tests to show bad behavior of various kinds from servers: raising exception from each remove_foo() method, for example
154
155 # XXX test disconnect DeadReferenceError from get_buckets and get_block_whatsit
156