pyfec: fix some error-checking, add more unit tests

This commit is contained in:
Brian Warner 2007-03-28 12:53:12 -07:00
parent dc25f327a3
commit bac74e4353
2 changed files with 30 additions and 4 deletions

View File

@ -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; i<self->mm - 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; i<num_desired_shares; i++) {
@ -173,7 +177,7 @@ Encoder_encode(Encoder *self, PyObject *args) {
num_check_shares_produced = self->mm - 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;

View File

@ -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__":