Fix bug in binascii uu encoding. Pass more related unit tests. (#5160)

* Fix bug in binascii, passes more unit tests.

* Pass more additional tests due to this PR.
This commit is contained in:
kingiler authored and GitHub committed 2024-02-09 22:42:39 +09:00
1 parent c4b1cc69be
commit bdf228eb42
4 files changed
+10 -32

No files matched your search

-6
View File
@@ -38,8 +38,6 @@ class BinASCIITest(unittest.TestCase):
self.assertTrue(hasattr(getattr(binascii, name), '__call__'))
self.assertRaises(TypeError, getattr(binascii, name))
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_returned_value(self):
# Limit to the minimum of all limits (b2a_uu)
MAX_ALL = 45
@@ -186,8 +184,6 @@ class BinASCIITest(unittest.TestCase):
assertInvalidLength(b'a' * (4 * 87 + 1))
assertInvalidLength(b'A\tB\nC ??DE') # only 5 valid characters
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_uu(self):
MAX_UU = 45
for backtick in (True, False):
@@ -404,8 +400,6 @@ class BinASCIITest(unittest.TestCase):
# crc_hqx needs 2 arguments
self.assertRaises(TypeError, binascii.crc_hqx, "test", 0)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_unicode_a2b(self):
# Unicode strings are accepted by a2b_* functions.
MAX_ALL = 45
-6
View File
@@ -2946,8 +2946,6 @@ else:
class TransformCodecTest(unittest.TestCase):
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_basics(self):
binput = bytes(range(256))
for encoding in bytes_transform_encodings:
@@ -2959,8 +2957,6 @@ class TransformCodecTest(unittest.TestCase):
self.assertEqual(size, len(o))
self.assertEqual(i, binput)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_read(self):
for encoding in bytes_transform_encodings:
with self.subTest(encoding=encoding):
@@ -2969,8 +2965,6 @@ class TransformCodecTest(unittest.TestCase):
sout = reader.read()
self.assertEqual(sout, b"\x80")
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_readline(self):
for encoding in bytes_transform_encodings:
with self.subTest(encoding=encoding):
-14
View File
@@ -75,8 +75,6 @@ class UUTest(unittest.TestCase):
with self.assertRaises(TypeError):
uu.encode(inp, out, "t1", 0o644, True)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_decode(self):
for backtick in True, False:
inp = io.BytesIO(encodedtextwrapped(0o666, "t1", backtick=backtick))
@@ -110,8 +108,6 @@ class UUTest(unittest.TestCase):
except uu.Error as e:
self.assertEqual(str(e), "No valid begin line found in input file")
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_garbage_padding(self):
# Issue #22406
encodedtext1 = (
@@ -163,8 +159,6 @@ class UUStdIOTest(unittest.TestCase):
sys.stdin = self.stdin
sys.stdout = self.stdout
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_encode(self):
sys.stdin = FakeIO(plaintext.decode("ascii"))
sys.stdout = FakeIO()
@@ -172,8 +166,6 @@ class UUStdIOTest(unittest.TestCase):
self.assertEqual(sys.stdout.getvalue(),
encodedtextwrapped(0o666, "t1").decode("ascii"))
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_decode(self):
sys.stdin = FakeIO(encodedtextwrapped(0o666, "t1").decode("ascii"))
sys.stdout = FakeIO()
@@ -192,8 +184,6 @@ class UUFileTest(unittest.TestCase):
self.addCleanup(os_helper.unlink, self.tmpin)
self.addCleanup(os_helper.unlink, self.tmpout)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_encode(self):
with open(self.tmpin, 'wb') as fin:
fin.write(plaintext)
@@ -212,8 +202,6 @@ class UUFileTest(unittest.TestCase):
s = fout.read()
self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_decode(self):
with open(self.tmpin, 'wb') as f:
f.write(encodedtextwrapped(0o644, self.tmpout))
@@ -226,8 +214,6 @@ class UUFileTest(unittest.TestCase):
self.assertEqual(s, plaintext)
# XXX is there an xp way to verify the mode?
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_decode_filename(self):
with open(self.tmpin, 'wb') as f:
f.write(encodedtextwrapped(0o644, self.tmpout))
+10 -6
View File
@@ -289,7 +289,7 @@ mod decl {
if [b'\r', b'\n'].contains(c) {
return Ok(0);
}
return Err(vm.new_value_error("Illegal char".to_string()));
return Err(super::new_binascii_error("Illegal char".to_owned(), vm));
}
Ok((*c - b' ') & 0x3f)
}
@@ -645,7 +645,8 @@ mod decl {
// Allocate the buffer
let mut res = Vec::<u8>::with_capacity(length);
let trailing_garbage_error = || Err(vm.new_value_error("Trailing garbage".to_string()));
let trailing_garbage_error =
|| Err(super::new_binascii_error("Trailing garbage".to_owned(), vm));
for chunk in b.get(1..).unwrap_or_default().chunks(4) {
let (char_a, char_b, char_c, char_d) = {
@@ -666,7 +667,7 @@ mod decl {
}
if res.len() < length {
res.push((char_b & 0xf) | char_c >> 2);
res.push((char_b & 0xf) << 4 | char_c >> 2);
} else if char_c != 0 {
return trailing_garbage_error();
}
@@ -688,7 +689,7 @@ mod decl {
#[derive(FromArgs)]
struct BacktickArg {
#[pyarg(named, default = "true")]
#[pyarg(named, default = "false")]
backtick: bool,
}
@@ -700,7 +701,7 @@ mod decl {
) -> PyResult<Vec<u8>> {
#[inline]
fn uu_b2a(num: u8, backtick: bool) -> u8 {
if backtick && num != 0 {
if backtick && num == 0 {
0x60
} else {
b' ' + num
@@ -710,7 +711,10 @@ mod decl {
data.with_ref(|b| {
let length = b.len();
if length > 45 {
return Err(vm.new_value_error("At most 45 bytes at once".to_string()));
return Err(super::new_binascii_error(
"At most 45 bytes at once".to_owned(),
vm,
));
}
let mut res = Vec::<u8>::with_capacity(2 + ((length + 2) / 3) * 4);
res.push(uu_b2a(length as u8, backtick));