From: Brian Warner Date: Wed, 28 Mar 2007 19:53:12 +0000 (-0700) Subject: pyfec: fix some error-checking, add more unit tests X-Git-Url: https://git.rkrishnan.org/pf/content/vdrive?a=commitdiff_plain;h=bac74e4353f6ea0eab01b81e221c98a3a50f4bc0;p=tahoe-lafs%2Ftahoe-lafs.git pyfec: fix some error-checking, add more unit tests --- diff --git a/pyfec/fec/_fecmodule.c b/pyfec/fec/_fecmodule.c index 66a64485..bf5174ee 100644 --- a/pyfec/fec/_fecmodule.c +++ b/pyfec/fec/_fecmodule.c @@ -151,10 +151,14 @@ Encoder_encode(Encoder *self, PyObject *args) { unsigned c_desired_shares_ids[self->mm]; unsigned c_desired_checkshares_ids[self->mm - self->kk]; unsigned i; + PyObject* fastinshares = NULL; + for (i=0; imm - self->kk; i++) pystrs_produced[i] = NULL; if (desired_shares_ids) { fast_desired_shares_ids = PySequence_Fast(desired_shares_ids, "Second argument (optional) was not a sequence."); + if (!fast_desired_shares_ids) + goto err; num_desired_shares = PySequence_Fast_GET_SIZE(fast_desired_shares_ids); fast_desired_shares_ids_items = PySequence_Fast_ITEMS(fast_desired_shares_ids); for (i=0; imm - self->kk; } - PyObject* fastinshares = PySequence_Fast(inshares, "First argument was not a sequence."); + fastinshares = PySequence_Fast(inshares, "First argument was not a sequence."); if (!fastinshares) goto err; diff --git a/pyfec/fec/test/test_pyfec.py b/pyfec/fec/test/test_pyfec.py index d046c514..f9578910 100644 --- a/pyfec/fec/test/test_pyfec.py +++ b/pyfec/fec/test/test_pyfec.py @@ -101,20 +101,42 @@ def test_random(): def test_bad_args_enc(): encer = fec.Encoder(2, 4) try: - encer.encode(["a", "b", ], ["c", "d",]) + encer.encode(["a", "b", ], ["c", "I am not an integer shareid",]) except fec.Error, e: - assert "econd argument is required to contain int" in str(e), e + assert "Precondition violation: second argument is required to contain int" in str(e), e else: raise "Should have gotten fec.Error for wrong type of second argument." + try: + encer.encode(["a", "b", ], 98) # not a sequence at all + except TypeError, e: + assert "Second argument (optional) was not a sequence" in str(e), e + else: + raise "Should have gotten TypeError for wrong type of second argument." + def test_bad_args_dec(): decer = fec.Decoder(2, 4) + + try: + decer.decode(98, [0, 1]) # first argument is not a sequence + except TypeError, e: + assert "First argument was not a sequence" in str(e), e + else: + raise "Should have gotten TypeError for wrong type of second argument." + try: decer.decode(["a", "b", ], ["c", "d",]) except fec.Error, e: - assert "econd argument is required to contain int" in str(e), e + assert "Precondition violation: second argument is required to contain int" in str(e), e else: raise "Should have gotten fec.Error for wrong type of second argument." + + try: + decer.decode(["a", "b", ], 98) # not a sequence at all + except TypeError, e: + assert "Second argument was not a sequence" in str(e), e + else: + raise "Should have gotten TypeError for wrong type of second argument." if __name__ == "__main__":