Skip to content

Commit 362be9f

Browse files
evanrittenhouseyouknowone
authored andcommitted
Add 'invalid base64-encoded string' error
1 parent 904e3a3 commit 362be9f

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

Lib/test/test_binascii.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ def assertDiscontinuousPadding(data, non_strict_mode_expected_result: bytes):
155155
assertDiscontinuousPadding(b'ab=c=', b'i\xb7')
156156
assertDiscontinuousPadding(b'ab=ab==', b'i\xb6\x9b')
157157

158-
# TODO: RUSTPYTHON
159-
@unittest.expectedFailure
160158
def test_base64errors(self):
161159
# Test base64 with invalid padding
162160
def assertIncorrectPadding(data):

stdlib/src/binascii.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ mod decl {
203203
if quad_pos >= 2 && quad_pos + pads >= 4 {
204204
if strict_mode && i + 1 < b.len() {
205205
// Represents excess data after padding error
206-
return Err(base64::DecodeError::InvalidLastSymbol(i, el));
206+
return Err(base64::DecodeError::InvalidLastSymbol(i, PAD));
207207
}
208208

209209
return Ok(decoded);
@@ -246,18 +246,11 @@ mod decl {
246246
}
247247
}
248248

249-
if quad_pos == 1 {
250-
// Ensure that a PAD never gets passed, since that'd mistakenly cause an excess
251-
// data after padding error
252-
return Err(base64::DecodeError::InvalidLastSymbol(
253-
decoded.len() / 3 * 4 + 1,
254-
0,
255-
));
256-
} else if quad_pos > 1 {
257-
return Err(base64::DecodeError::InvalidLength);
258-
}
259-
260-
Ok(decoded)
249+
return match quad_pos {
250+
0 => Ok(decoded),
251+
1 => Err(base64::DecodeError::InvalidLastSymbol(decoded.len() / 3 * 4 + 1, 0)),
252+
_ => Err(base64::DecodeError::InvalidLength)
253+
};
261254
})
262255
.map_err(|err| {
263256
let python_error = match err {
@@ -270,10 +263,13 @@ mod decl {
270263
base64::DecodeError::InvalidByte(_, _) => {
271264
String::from("Only base64 data is allowed")
272265
}
273-
base64::DecodeError::InvalidLastSymbol(_, _) => {
266+
base64::DecodeError::InvalidLastSymbol(_, PAD) => {
274267
String::from("Excess data after padding")
275268
}
276-
base64::DecodeError::InvalidLength => String::from("Not implemented (yet)"),
269+
base64::DecodeError::InvalidLastSymbol(length, _) => {
270+
format!("Invalid base64-encoded string: number of data characters {} cannot be 1 more than a multiple of 4", length)
271+
}
272+
base64::DecodeError::InvalidLength => String::from("Incorrect padding"),
277273
};
278274

279275
new_binascii_error(format!("error decoding base64: {python_error}"), vm)

0 commit comments

Comments
 (0)