|
1 | 1 | import pytest |
2 | | -from turbo_sdk.bundle.utils import set_bytes, byte_array_to_long |
| 2 | +from turbo_sdk.bundle.utils import set_bytes, byte_array_to_long, b64url_decode |
3 | 3 |
|
4 | 4 |
|
5 | 5 | class TestUtils: |
@@ -86,3 +86,41 @@ def test_byte_array_to_long_two_bytes(self): |
86 | 86 | data = bytearray([1, 1]) # 257 in little-endian |
87 | 87 | result = byte_array_to_long(data) |
88 | 88 | assert result == 257 |
| 89 | + |
| 90 | + def test_b64url_decode_with_padding(self): |
| 91 | + """Test decoding base64url with proper padding""" |
| 92 | + # "hello" in base64url with padding |
| 93 | + result = b64url_decode("aGVsbG8=") |
| 94 | + assert result == b"hello" |
| 95 | + |
| 96 | + def test_b64url_decode_without_padding(self): |
| 97 | + """Test decoding base64url without padding""" |
| 98 | + # "hello" in base64url without padding |
| 99 | + result = b64url_decode("aGVsbG8") |
| 100 | + assert result == b"hello" |
| 101 | + |
| 102 | + def test_b64url_decode_url_safe_chars(self): |
| 103 | + """Test that url-safe characters are decoded correctly""" |
| 104 | + import base64 |
| 105 | + |
| 106 | + # Data that would have + and / in standard base64 |
| 107 | + data = b"\xfb\xff\xfe" |
| 108 | + encoded = base64.urlsafe_b64encode(data).decode().rstrip("=") |
| 109 | + result = b64url_decode(encoded) |
| 110 | + assert result == data |
| 111 | + |
| 112 | + def test_b64url_decode_empty(self): |
| 113 | + """Test decoding empty string""" |
| 114 | + result = b64url_decode("") |
| 115 | + assert result == b"" |
| 116 | + |
| 117 | + def test_b64url_decode_various_lengths(self): |
| 118 | + """Test decoding strings of various lengths (different padding needs)""" |
| 119 | + import base64 |
| 120 | + |
| 121 | + for length in [1, 2, 3, 4, 5, 10, 100]: |
| 122 | + data = bytes(range(length % 256)) * (length // 256 + 1) |
| 123 | + data = data[:length] |
| 124 | + encoded = base64.urlsafe_b64encode(data).decode().rstrip("=") |
| 125 | + result = b64url_decode(encoded) |
| 126 | + assert result == data |
0 commit comments