Skip to content

Commit

Permalink
Clean up more around base64.decodebytes() calls
Browse files Browse the repository at this point in the history
Follow-up to #28747.
  • Loading branch information
foolip committed May 3, 2021
1 parent 377f1b2 commit a7dac97
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 42 deletions.
6 changes: 2 additions & 4 deletions common/security-features/subresource/font.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import os, sys, base64
import os, sys
from base64 import decodebytes

from wptserve.utils import isomorphic_decode
import importlib
subresource = importlib.import_module("common.security-features.subresource.subresource")


def decodebytes(s):
return base64.decodebytes(s)

def generate_payload(request, server_data):
data = (u'{"headers": %(headers)s}') % server_data
if b"id" in request.GET:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import base64
from base64 import decodebytes

def main(req, res):
return 404, [(b'Content-Type', b'image/png')], base64.decodebytes(b"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAhSURBVDhPY3wro/KfgQLABKXJBqMGjBoAAqMGDLwBDAwAEsoCTFWunmQAAAAASUVORK5CYII=")
return 404, [(b'Content-Type', b'image/png')], decodebytes(b"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAhSURBVDhPY3wro/KfgQLABKXJBqMGjBoAAqMGDLwBDAwAEsoCTFWunmQAAAAASUVORK5CYII=")
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from base64 import decodestring
from base64 import decodebytes
import time

png_response = decodestring(b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVR4nGNiAAAABgADNjd8qAAAAABJRU5ErkJggg==')
png_response = decodebytes(b'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVR4nGNiAAAABgADNjd8qAAAAABJRU5ErkJggg==')

def main(request, response):
time.sleep(2)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import base64
from base64 import decodebytes
import json
import os

from wptserve.utils import isomorphic_decode, isomorphic_encode

def decodebytes(s):
return base64.decodebytes(s)

def main(request, response):
headers = []
headers.append((b'X-ServiceWorker-ServerHeader', b'SetInTheServer'))
Expand Down
14 changes: 5 additions & 9 deletions webdriver/tests/print/printcmd.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import base64
from base64 import decodebytes

import pytest

import six

from tests.support.asserts import assert_error, assert_success

def decodebytes(s):
return base64.decodebytes(six.ensure_binary(s))

def do_print(session, options):
def do_print(session, options) -> str:
return session.transport.send(
"POST", "session/{session_id}/print".format(**vars(session)),
options)


def assert_pdf(data):
def assert_pdf(data: bytes):
assert data.startswith(b"%PDF-"), "Decoded data starts with the PDF signature"
assert data.endswith(b"%%EOF\n"), "Decoded data ends with the EOF flag"

Expand All @@ -28,7 +24,7 @@ def test_no_top_browsing_context(session, closed_window):
def test_no_browsing_context(session, closed_frame):
response = do_print(session, {})
value = assert_success(response)
pdf = decodebytes(value)
pdf = decodebytes(value.encode())
assert_pdf(pdf)


Expand All @@ -41,7 +37,7 @@ def test_html_document(session, inline):
"shrinkToFit": False
})
value = assert_success(response)
pdf = decodebytes(value)
pdf = decodebytes(value.encode())
# TODO: Test that the output is reasonable
assert_pdf(pdf)

Expand Down
9 changes: 2 additions & 7 deletions webdriver/tests/print/user_prompts.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
# META: timeout=long
import base64
from base64 import decodebytes

import pytest

import six

from tests.support.asserts import assert_dialog_handled, assert_error, assert_success
from .printcmd import do_print, assert_pdf


def decodebytes(s):
return base64.decodebytes(six.ensure_binary(s))

@pytest.fixture
def check_user_prompt_closed_without_exception(session, create_dialog, inline):
def check_user_prompt_closed_without_exception(dialog_type, retval):
Expand All @@ -22,7 +17,7 @@ def check_user_prompt_closed_without_exception(dialog_type, retval):
response = do_print(session, {})
value = assert_success(response)

pdf = decodebytes(value)
pdf = decodebytes(value.encode())
assert_dialog_handled(session, expected_text=dialog_type, expected_retval=retval)

assert_pdf(pdf)
Expand Down
9 changes: 3 additions & 6 deletions webdriver/tests/support/asserts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import base64
from base64 import decodebytes
import imghdr
import struct

Expand All @@ -7,9 +7,6 @@
from webdriver import Element, NoSuchAlertException, WebDriverException


def decodebytes(s):
return base64.decodebytes(six.ensure_binary(s))

# WebDriver specification ID: dfn-error-response-data
errors = {
"element click intercepted": 400,
Expand Down Expand Up @@ -215,8 +212,8 @@ def assert_move_to_coordinates(point, target, events):
assert e["target"] == target


def assert_png(screenshot):
def assert_png(screenshot: str):
"""Test that screenshot is a Base64 encoded PNG file."""
image = decodebytes(screenshot)
image = decodebytes(screenshot.encode())
mime_type = imghdr.what("", image)
assert mime_type == "png", "Expected image to be PNG, but it was {}".format(mime_type)
11 changes: 3 additions & 8 deletions webdriver/tests/support/image.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import base64
from base64 import decodebytes
import math
import struct

import six

from tests.support.asserts import assert_png


def decodebytes(s):
return base64.decodebytes(six.ensure_binary(s))

def png_dimensions(screenshot):
def png_dimensions(screenshot: str):
assert_png(screenshot)
image = decodebytes(screenshot)
image = decodebytes(screenshot.encode())
width, height = struct.unpack(">LL", image[16:24])
return int(width), int(height)

0 comments on commit a7dac97

Please sign in to comment.