#!/usr/bin/python3 import io import itertools import os.path import subprocess def test(input, output): p = subprocess.Popen([os.path.join(os.path.dirname(__file__), '.make-and-exec-binaries', 'urldecode')], text = False, stdin = subprocess.PIPE, stdout = subprocess.PIPE) stdout, stderr = p.communicate(input) assert not stderr assert stdout == output BUFFER_SIZE = 1024 * 300 test(b'', b'') test(b'a', b'a') test(b'%', b'%') test(b'%2', b'%2') test(b'%20', b' ') test(b'%gg', b'%gg') #test(b'%\xc3\xa4', b'%\xc3\xa4') for i in range(256): test(f'%{i:02X}'.encode('ascii'), bytes.fromhex(f'{i:02x}')) test(f'%{i:02x}'.encode('ascii'), bytes.fromhex(f'{i:02x}')) hexbytes = tuple(f'{i:02x}' for i in range(256)) # ('00', '01', .., 'ff') data = bytes.fromhex(''.join(map(''.join, filter(lambda x: any(c < '30' or '39' < c < '41' or '46' < c < '61' or '66' < c for c in x[1:]), itertools.product(('25',), hexbytes, hexbytes))))) test(data, data) for offset in range(-3, 1): test(b'a' * (BUFFER_SIZE + offset) + b'%25', b'a' * (BUFFER_SIZE + offset) + b'%') test(b'a' * (BUFFER_SIZE + offset) + b'%gg', b'a' * (BUFFER_SIZE + offset) + b'%gg') test(b'a' * (BUFFER_SIZE + offset) + b'%', b'a' * (BUFFER_SIZE + offset) + b'%') test(b'a' * (BUFFER_SIZE + offset) + b'%2', b'a' * (BUFFER_SIZE + offset) + b'%2') test(b'a' * (BUFFER_SIZE + offset) + b'%g', b'a' * (BUFFER_SIZE + offset) + b'%g') for i in range(10): test(b'%' * i + b'%20', b'%' * i + b' ')