3 import cStringIO, os, random, re
12 from pyutil import fileutil
14 from base64 import b32encode
15 def ab(x): # debuggery
17 return "%s:%s" % (len(x), b32encode(x[-3:]),)
19 return "%s:%s" % (len(x), b32encode(x[-2:]),)
21 return "%s:%s" % (len(x), b32encode(x[-1:]),)
23 return "%s:%s" % (len(x), "--empty--",)
26 return ''.join(map(chr, map(random.randrange, [0]*n, [256]*n)))
29 encer = zfec.Encoder(k, m)
30 nums_and_blocks = list(enumerate(encer.encode(ss)))
31 assert isinstance(nums_and_blocks, list), nums_and_blocks
32 assert len(nums_and_blocks) == m, (len(nums_and_blocks), m,)
33 nums_and_blocks = random.sample(nums_and_blocks, k)
34 blocks = [ x[1] for x in nums_and_blocks ]
35 nums = [ x[0] for x in nums_and_blocks ]
36 decer = zfec.Decoder(k, m)
37 decoded = decer.decode(blocks, nums)
38 assert len(decoded) == len(ss), (len(decoded), len(ss),)
39 assert tuple([str(s) for s in decoded]) == tuple([str(s) for s in ss]), (tuple([ab(str(s)) for s in decoded]), tuple([ab(str(s)) for s in ss]),)
41 def _help_test_random():
42 m = random.randrange(1, 257)
43 k = random.randrange(1, m+1)
44 l = random.randrange(0, 2**9)
45 ss = [ randstr(l/k) for x in range(k) ]
48 def _help_test_random_with_l(l):
49 m = random.randrange(1, 257)
50 k = random.randrange(1, m+1)
51 ss = [ randstr(l/k) for x in range(k) ]
55 encer = zfec.easyfec.Encoder(k, m)
56 nums_and_blocks = list(enumerate(encer.encode(s)))
57 assert isinstance(nums_and_blocks, list), nums_and_blocks
58 assert len(nums_and_blocks) == m, (len(nums_and_blocks), m,)
59 nums_and_blocks = random.sample(nums_and_blocks, k)
60 blocks = [ x[1] for x in nums_and_blocks ]
61 nums = [ x[0] for x in nums_and_blocks ]
62 decer = zfec.easyfec.Decoder(k, m)
64 decodeds = decer.decode(blocks, nums, padlen=k*len(blocks[0]) - len(s))
65 assert len(decodeds) == len(s), (ab(decodeds), ab(s), k, m)
66 assert decodeds == s, (ab(decodeds), ab(s),)
68 def _help_test_random_easy():
69 m = random.randrange(1, 257)
70 k = random.randrange(1, m+1)
71 l = random.randrange(0, 2**9)
75 def _help_test_random_with_l_easy(l):
76 m = random.randrange(1, 257)
77 k = random.randrange(1, m+1)
81 class ZFecTest(unittest.TestCase):
82 def test_instantiate_encoder_no_args(self):
86 # Okay, so that's because we're required to pass constructor args.
89 # Oops, it should have raised an exception.
90 self.fail("Should have raised exception from incorrect arguments to constructor.")
92 def test_instantiate_decoder_no_args(self):
96 # Okay, so that's because we're required to pass constructor args.
99 # Oops, it should have raised an exception.
100 self.fail("Should have raised exception from incorrect arguments to constructor.")
102 def test_from_agl_c(self):
103 self.failUnless(zfec._fec.test_from_agl())
105 def test_from_agl_py(self):
106 e = zfec.Encoder(3, 5)
107 b0 = '\x01'*8 ; b1 = '\x02'*8 ; b2 = '\x03'*8
108 # print "_from_py before encoding:"
109 # print "b0: %s, b1: %s, b2: %s" % tuple(base64.b16encode(x) for x in [b0, b1, b2])
111 b3, b4 = e.encode([b0, b1, b2], (3, 4))
112 # print "after encoding:"
113 # print "b3: %s, b4: %s" % tuple(base64.b16encode(x) for x in [b3, b4])
115 d = zfec.Decoder(3, 5)
116 r0, r1, r2 = d.decode((b2, b3, b4), (1, 2, 3))
118 # print "after decoding:"
119 # print "b0: %s, b1: %s" % tuple(base64.b16encode(x) for x in [b0, b1])
121 def test_small(self):
123 _help_test_random_with_l(i)
125 print "%d randomized tests pass." % (i+1)
127 def test_random(self):
131 print "%d randomized tests pass." % (i+1)
133 def test_bad_args_construct_decoder(self):
136 except zfec.Error, e:
137 assert "argument is required to be greater than or equal to 1" in str(e), e
139 self.fail("Should have gotten an exception from out-of-range arguments.")
143 except zfec.Error, e:
144 assert "argument is required to be less than or equal to 256" in str(e), e
146 self.fail("Should have gotten an exception from out-of-range arguments.")
150 except zfec.Error, e:
151 assert "first argument is required to be less than or equal to the second argument" in str(e), e
153 self.fail("Should have gotten an exception from out-of-range arguments.")
155 def test_bad_args_construct_encoder(self):
158 except zfec.Error, e:
159 assert "argument is required to be greater than or equal to 1" in str(e), e
161 self.fail("Should have gotten an exception from out-of-range arguments.")
165 except zfec.Error, e:
166 assert "argument is required to be less than or equal to 256" in str(e), e
168 self.fail("Should have gotten an exception from out-of-range arguments.")
170 def test_bad_args_dec(self):
171 decer = zfec.Decoder(2, 4)
174 decer.decode(98, []) # first argument is not a sequence
176 assert "First argument was not a sequence" in str(e), e
178 self.fail("Should have gotten TypeError for wrong type of second argument.")
181 decer.decode(["a", "b", ], ["c", "d",])
182 except zfec.Error, e:
183 assert "Precondition violation: second argument is required to contain int" in str(e), e
185 self.fail("Should have gotten zfec.Error for wrong type of second argument.")
188 decer.decode(["a", "b", ], 98) # not a sequence at all
190 assert "Second argument was not a sequence" in str(e), e
192 self.fail("Should have gotten TypeError for wrong type of second argument.")
194 class EasyFecTest(unittest.TestCase):
195 def test_small(self):
197 _help_test_random_with_l_easy(i)
199 print "%d randomized tests pass." % (i+1)
201 def test_random(self):
203 _help_test_random_easy()
205 print "%d randomized tests pass." % (i+1)
207 def test_bad_args_dec(self):
208 decer = zfec.easyfec.Decoder(2, 4)
211 decer.decode(98, [0, 1], 0) # first argument is not a sequence
213 assert "First argument was not a sequence" in str(e), e
215 self.fail("Should have gotten TypeError for wrong type of second argument.")
218 decer.decode("ab", ["c", "d",], 0)
219 except zfec.Error, e:
220 assert "Precondition violation: second argument is required to contain int" in str(e), e
222 self.fail("Should have gotten zfec.Error for wrong type of second argument.")
225 decer.decode("ab", 98, 0) # not a sequence at all
227 assert "Second argument was not a sequence" in str(e), e
229 self.fail("Should have gotten TypeError for wrong type of second argument.")
231 class FileFec(unittest.TestCase):
232 def test_filefec_header(self):
233 for m in [1, 2, 3, 5, 7, 9, 11, 17, 19, 33, 35, 65, 66, 67, 129, 130, 131, 254, 255, 256,]:
234 for k in [1, 2, 3, 5, 9, 17, 33, 65, 129, 255, 256,]:
237 for pad in [0, 1, k-1,]:
240 for sh in [0, 1, m-1,]:
243 h = zfec.filefec._build_header(m, k, pad, sh)
244 hio = cStringIO.StringIO(h)
245 (rm, rk, rpad, rsh,) = zfec.filefec._parse_header(hio)
246 assert (rm, rk, rpad, rsh,) == (m, k, pad, sh,), h
248 def _help_test_filefec(self, teststr, k, m, numshs=None):
252 TESTFNAME = "testfile.txt"
258 tempdir = fileutil.NamedTemporaryDirectory(cleanup=True)
260 tempf = tempdir.file(TESTFNAME, 'w+b')
266 zfec.filefec.encode_to_files(tempf, fsize, tempdir.name, PREFIX, k, m, SUFFIX, verbose=VERBOSE)
268 # select some share files
269 RE=re.compile(zfec.filefec.RE_FORMAT % (PREFIX, SUFFIX,))
270 fns = os.listdir(tempdir.name)
271 assert len(fns) >= m, (fns, tempdir, tempdir.name,)
272 sharefs = [ open(os.path.join(tempdir.name, fn), "rb") for fn in fns if RE.match(fn) ]
273 for sharef in sharefs:
274 tempdir.register_file(sharef)
275 random.shuffle(sharefs)
278 # decode from the share files
279 outf = tempdir.file('recovered-testfile.txt', 'w+b')
280 zfec.filefec.decode_from_files(outf, sharefs, verbose=VERBOSE)
283 recovereddata = outf.read()
284 assert recovereddata == teststr, (ab(recovereddata), ab(teststr),)
288 def test_filefec_all_shares(self):
289 return self._help_test_filefec("Yellow Whirled!", 3, 8)
291 def test_filefec_all_shares_1_b(self):
292 return self._help_test_filefec("Yellow Whirled!", 4, 16)
294 def test_filefec_all_shares_2(self):
295 return self._help_test_filefec("Yellow Whirled", 3, 8)
297 def test_filefec_all_shares_2_b(self):
298 return self._help_test_filefec("Yellow Whirled", 4, 16)
300 def test_filefec_all_shares_3(self):
301 return self._help_test_filefec("Yellow Whirle", 3, 8)
303 def test_filefec_all_shares_3_b(self):
304 return self._help_test_filefec("Yellow Whirle", 4, 16)
306 def test_filefec_all_shares_with_padding(self, noisy=VERBOSE):
307 return self._help_test_filefec("Yellow Whirled!A", 3, 8)
309 def test_filefec_min_shares_with_padding(self, noisy=VERBOSE):
310 return self._help_test_filefec("Yellow Whirled!A", 3, 8, numshs=3)
312 def test_filefec_min_shares_with_crlf(self, noisy=VERBOSE):
313 return self._help_test_filefec("llow Whirled!A\r\n", 3, 8, numshs=3)
315 def test_filefec_min_shares_with_lf(self, noisy=VERBOSE):
316 return self._help_test_filefec("Yellow Whirled!A\n", 3, 8, numshs=3)
318 def test_filefec_min_shares_with_lflf(self, noisy=VERBOSE):
319 return self._help_test_filefec("Yellow Whirled!A\n\n", 3, 8, numshs=3)
321 def test_filefec_min_shares_with_crcrlflf(self, noisy=VERBOSE):
322 return self._help_test_filefec("Yellow Whirled!A\r\r\n\n", 3, 8, numshs=3)
325 class Cmdline(unittest.TestCase):
326 def test_basic(self, noisy=VERBOSE):
327 tempdir = fileutil.NamedTemporaryDirectory(cleanup=True)
328 fo = tempdir.file("test.data", "w+b")
329 fo.write("WHEHWHJEKWAHDLJAWDHWALKDHA")
336 sys.argv = ["zfec", os.path.join(tempdir.name, "test.data"),]
338 retcode = zfec.cmdline_zfec.main()
339 assert retcode == 0, retcode
341 RE=re.compile(zfec.filefec.RE_FORMAT % ('test.data', ".fec",))
342 fns = os.listdir(tempdir.name)
343 assert len(fns) >= DEFAULT_M, (fns, DEFAULT_M, tempdir, tempdir.name,)
344 sharefns = [ os.path.join(tempdir.name, fn) for fn in fns if RE.match(fn) ]
345 random.shuffle(sharefns)
346 del sharefns[DEFAULT_K:]
348 sys.argv = ["zunfec",]
349 sys.argv.extend(sharefns)
350 sys.argv.extend(['-o', os.path.join(tempdir.name, 'test.data-recovered'),])
352 retcode = zfec.cmdline_zunfec.main()
353 assert retcode == 0, retcode
355 assert filecmp.cmp(os.path.join(tempdir.name, 'test.data'), os.path.join(tempdir.name, 'test.data-recovered'))
359 if __name__ == "__main__":