Exemplo n.º 1
0
class CorruptingAuthorizedSession(tr_requests.AuthorizedSession):
    """A Requests Session class with credentials, which corrupts responses.

    This class is used for testing checksum validation.

    Args:
        credentials (google.auth.credentials.Credentials): The credentials to
            add to the request.
        refresh_status_codes (Sequence[int]): Which HTTP status codes indicate
            that credentials should be refreshed and the request should be
            retried.
        max_refresh_attempts (int): The maximum number of times to attempt to
            refresh the credentials and retry the request.
        kwargs: Additional arguments passed to the :class:`requests.Session`
            constructor.
    """

    EMPTY_MD5 = base64.b64encode(hashlib.md5(b"").digest()).decode(u"utf-8")
    crc32c = _helpers._get_crc32c_object()
    crc32c.update(b"")
    EMPTY_CRC32C = base64.b64encode(crc32c.digest()).decode(u"utf-8")

    def request(self, method, url, data=None, headers=None, **kwargs):
        """Implementation of Requests' request."""
        response = tr_requests.AuthorizedSession.request(self,
                                                         method,
                                                         url,
                                                         data=data,
                                                         headers=headers,
                                                         **kwargs)
        response.headers[_helpers._HASH_HEADER] = u"crc32c={},md5={}".format(
            self.EMPTY_CRC32C, self.EMPTY_MD5)
        return response
Exemplo n.º 2
0
def test__get_checksum_object(checksum):
    checksum_object = _helpers._get_checksum_object(checksum)

    checksum_types = {
        "md5": type(hashlib.md5()),
        "crc32c": type(_helpers._get_crc32c_object()),
        None: type(None),
    }
    assert isinstance(checksum_object, checksum_types[checksum])
Exemplo n.º 3
0
def test_crc32c_throws_import_error():
    try:
        import builtins
    except ImportError:
        import __builtin__ as builtins
    orig_import = builtins.__import__

    # Raises ImportError for name == "crc32c" or name == "crcmod"
    def mock_import(name, globals, locals, fromlist, level=None):
        raise ImportError

    builtins.__import__ = mock_import

    try:
        with pytest.raises(ImportError):
            _helpers._get_crc32c_object()
    finally:
        builtins.__import__ = orig_import
Exemplo n.º 4
0
    def test__w_header_present(self, _LOGGER, template, checksum):
        checksums = {"md5": u"b2twdXNodGhpc2J1dHRvbg==", "crc32c": u"3q2+7w=="}
        header_value = template.format(checksums["crc32c"], checksums["md5"])
        headers = {_helpers._HASH_HEADER: header_value}
        response = _mock_response(headers=headers)

        def _get_headers(response):
            return response.headers

        url = "https://example.com/"
        expected_checksum, checksum_obj = _helpers._get_expected_checksum(
            response, _get_headers, url, checksum_type=checksum)
        assert expected_checksum == checksums[checksum]

        checksum_types = {
            "md5": type(hashlib.md5()),
            "crc32c": type(_helpers._get_crc32c_object()),
        }
        assert isinstance(checksum_obj, checksum_types[checksum])

        _LOGGER.info.assert_not_called()