-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Description
How to reproduce error: 'tests/test_secrets.py'
class Compare_Digest_Tests(unittest.TestCase):
"""Test secrets.compare_digest function."""
def test_equal(self):
# Test compare_digest functionality with equal (byte/text) strings.
for s in ("a", "bcd", "xyz123"):
a = s*100
b = s*100
self.assertTrue(secrets.compare_digest(a, b))
self.assertTrue(secrets.compare_digest(a.encode('utf-8'), b.encode('utf-8')))
# TODO: RUSTPYTHON Fix Leak
@unittest.expectedFailure
def test_unequal(self):
# Test compare_digest functionality with unequal (byte/text) strings.
self.assertFalse(secrets.compare_digest("abc", "abcd"))
self.assertFalse(secrets.compare_digest(b"abc", b"abcd"))
for s in ("x", "mn", "a1b2c3"):
a = s*100 + "q"
b = s*100 + "k"
self.assertFalse(secrets.compare_digest(a, b))
self.assertFalse(secrets.compare_digest(a.encode('utf-8'), b.encode('utf-8')))
some func:
#[pyfunction]
fn compare_digest(
a: PyObjectRef,
b: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyObjectRef> {
// Supported types:
// PyStrRef
// PyBytesRef
let _py_str = vm.ctx.types.str_type;
if a.class().is(_py_str) != b.class().is(_py_str) {
return Err(vm.new_type_error(format!(
"different types in compare_digest {} and {} ",
a.class().name(),
b.class().name()
)));
}
let a_hash = match a.class().is(_py_str) {
true => a.try_to_value::<&str>(vm)?.as_bytes().to_vec(),
_ => b.try_to_value::<PyBytes>(vm)?.as_bytes().to_vec(),
};
let b_hash = match b.class().is(_py_str) {
true => b.try_to_value::<&str>(vm)?.as_bytes().to_vec(),
_ => b.try_to_value::<PyBytes>(vm)?.as_bytes().to_vec(),
};
println!("\na: {:?}", a_hash);
println!("b: {:?}", b_hash);
println!("equal: {}", (a_hash == b_hash));
Ok((a_hash == b_hash).to_pyobject(vm))
}
output test:
test_unequal (test.test_secrets.Compare_Digest_Tests.test_unequal) ...
a: [97, 98, 99]
b: [97, 98, 99, 100]
equal: false
a: [97, 98, 99, 100]
b: [97, 98, 99, 100]
equal: true
FAIL
Metadata
Metadata
Assignees
Labels
No labels