Python base64.standard_b64decode() Examples

The following are 30 code examples of base64.standard_b64decode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module base64 , or try the search function .
Example #1
Source File: fields.py    From n6 with GNU Affero General Public License v3.0 6 votes vote down vote up
def _urlsafe_b64decode(self, value):
        value = value.rstrip('\r\n')  # some encoders like to append a newline...
        try:
            # `base64.urlsafe_b64decode()` just ignores illegal
            # characters *but* we want to be *more strict*
            if not self._URLSAFE_B64_VALID_CHARACTERS.issuperset(value):
                raise ValueError
            # `base64.urlsafe_b64decode()` (contrary to `base64.standard_b64decode()`)
            # does *not* accept unicode strings (even not pure-ASCII ones) :-/
            value = string_as_bytes(value)
            value = base64.urlsafe_b64decode(value)
        except (ValueError, TypeError):  # (TypeError is raised on incorrect Base64 padding)
            raise FieldValueError(public_message=(
                '"{}" is not a valid URL-safe-Base64-encoded string '
                '[see: RFC 4648, section 5]'.format(ascii_str(value))))
        return value 
Example #2
Source File: action.py    From insightconnect-plugins with MIT License 6 votes vote down vote up
def run(self, params={}):
        try:
            data = params.get(Input.BASE64)
            errors = params.get(Input.ERRORS)
            result = base64.standard_b64decode(data)
            if errors in ["replace", "ignore"]:
                return {Output.DATA: result.decode('utf-8', errors=errors)}
            else:
                return {Output.DATA: result.decode('utf-8')}
        except Exception as e:
            self.logger.error("An error has occurred while decoding ", e)
            raise PluginException(
                cause="Failed to decode because valid base64 input was not provided.",
                assistance='If you would like continue to attempt to decode the input try setting the value of the error field to ignore errors or to replace the characters.',
                data=e
            ) 
Example #3
Source File: test_base64.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_b64decode_invalid_chars(self):
        # issue 1466065: Test some invalid characters.
        tests = ((b'%3d==', b'\xdd'),
                 (b'$3d==', b'\xdd'),
                 (b'[==', b''),
                 (b'YW]3=', b'am'),
                 (b'3{d==', b'\xdd'),
                 (b'3d}==', b'\xdd'),
                 (b'@@', b''),
                 (b'!', b''),
                 (b'YWJj\nYWI=', b'abcab'))
        for bstr, res in tests:
            self.assertEqual(base64.b64decode(bstr), res)
            self.assertEqual(base64.standard_b64decode(bstr), res)
            self.assertEqual(base64.urlsafe_b64decode(bstr), res)

        # Normal alphabet characters not discarded when alternative given
        res = b'\xFB\xEF\xBE\xFF\xFF\xFF'
        self.assertEqual(base64.b64decode(b'++[[//]]', b'[]'), res)
        self.assertEqual(base64.urlsafe_b64decode(b'++--//__'), res) 
Example #4
Source File: http_server.py    From plugin.video.netflix with MIT License 6 votes vote down vote up
def do_POST(self):
        """Loads the licence for the requested resource"""
        try:
            url_parse = urlparse(self.path)
            common.debug('Handling HTTP POST IPC call to {}', url_parse.path)
            if '/license' not in url_parse:
                self.send_response(404)
                self.end_headers()
                return
            length = int(self.headers.get('content-length', 0))
            data = self.rfile.read(length).decode('utf-8').split('!')
            b64license = self.server.msl_handler.get_license(
                challenge=data[0], sid=base64.standard_b64decode(data[1]).decode('utf-8'))
            self.send_response(200)
            self.end_headers()
            self.wfile.write(base64.standard_b64decode(b64license))
        except Exception as exc:
            import traceback
            common.error(g.py2_decode(traceback.format_exc(), 'latin-1'))
            self.send_response(500 if isinstance(exc, MSLError) else 400)
            self.end_headers() 
Example #5
Source File: release_builder.py    From dcos-kafka-service with Apache License 2.0 6 votes vote down vote up
def _update_marathon_json(self, package_json):
        """Updates the marathon.json definition to contain the desired name and version strings.
        """
        # note: the file isn't valid JSON, so we edit the raw content instead
        marathon_encoded = package_json.get("marathon", {}).get("v2AppMustacheTemplate")
        orig_marathon_lines = base64.standard_b64decode(marathon_encoded).decode().split("\n")

        marathon_lines = []
        for line in orig_marathon_lines:
            name_match = re.match(r'^ *"PACKAGE_NAME": ?"(.*)",?$', line.rstrip("\n"))
            version_match = re.match(r'^ *"PACKAGE_VERSION": ?"(.*)",?$', line.rstrip("\n"))
            if name_match:
                line = line.replace(name_match.group(1), self._pkg_name)
            elif version_match:
                line = line.replace(version_match.group(1), self._pkg_version)
            marathon_lines.append(line)

        log.info("Updated marathon.json.mustache:")
        log.info("\n".join(difflib.unified_diff(orig_marathon_lines, marathon_lines, lineterm="")))

        # Update parent package object with changes:
        package_json["marathon"]["v2AppMustacheTemplate"] = base64.standard_b64encode(
            "\n".join(marathon_lines).encode("utf-8")
        ).decode() 
Example #6
Source File: kube_config.py    From kubernetes_asyncio with Apache License 2.0 6 votes vote down vote up
def as_file(self):
        """If obj[%data_key_name] exists, return name of a file with base64
        decoded obj[%data_key_name] content otherwise obj[%file_key_name]."""
        use_data_if_no_file = not self._file and self._data
        if use_data_if_no_file:
            if self._base64_file_content:
                if isinstance(self._data, str):
                    content = self._data.encode()
                else:
                    content = self._data
                self._file = _create_temp_file_with_content(
                    base64.standard_b64decode(content))
            else:
                self._file = _create_temp_file_with_content(self._data)
        if self._file and not os.path.isfile(self._file):
            raise ConfigException("File does not exists: %s" % self._file)
        return self._file 
Example #7
Source File: aws_srp.py    From warrant with Apache License 2.0 6 votes vote down vote up
def process_challenge(self, challenge_parameters):
        user_id_for_srp = challenge_parameters['USER_ID_FOR_SRP']
        salt_hex = challenge_parameters['SALT']
        srp_b_hex = challenge_parameters['SRP_B']
        secret_block_b64 = challenge_parameters['SECRET_BLOCK']
        # re strips leading zero from a day number (required by AWS Cognito)
        timestamp = re.sub(r" 0(\d) ", r" \1 ",
                           datetime.datetime.utcnow().strftime("%a %b %d %H:%M:%S UTC %Y"))
        hkdf = self.get_password_authentication_key(user_id_for_srp,
                                                    self.password, hex_to_long(srp_b_hex), salt_hex)
        secret_block_bytes = base64.standard_b64decode(secret_block_b64)
        msg = bytearray(self.pool_id.split('_')[1], 'utf-8') + bytearray(user_id_for_srp, 'utf-8') + \
              bytearray(secret_block_bytes) + bytearray(timestamp, 'utf-8')
        hmac_obj = hmac.new(hkdf, msg, digestmod=hashlib.sha256)
        signature_string = base64.standard_b64encode(hmac_obj.digest())
        response = {'TIMESTAMP': timestamp,
                    'USERNAME': user_id_for_srp,
                    'PASSWORD_CLAIM_SECRET_BLOCK': secret_block_b64,
                    'PASSWORD_CLAIM_SIGNATURE': signature_string.decode('utf-8')}
        if self.client_secret is not None:
            response.update({
                "SECRET_HASH":
                self.get_secret_hash(self.username, self.client_id, self.client_secret)})
        return response 
Example #8
Source File: config.py    From virt-who with GNU General Public License v2.0 6 votes vote down vote up
def as_file(self):
        """If obj[%data_key_name] exists, return name of a file with base64
        decoded obj[%data_key_name] content otherwise obj[%file_key_name]."""
        use_data_if_no_file = not self._file and self._data
        if use_data_if_no_file:
            if self._base64_file_content:
                if isinstance(self._data, str):
                    content = self._data.encode()
                else:
                    content = self._data
                self._file = _create_temp_file_with_content(
                    base64.standard_b64decode(content))
            else:
                self._file = _create_temp_file_with_content(self._data)
        if self._file and not os.path.isfile(self._file):
            raise ConfigException("File does not exists: %s" % self._file)
        return self._file 
Example #9
Source File: request_filter.py    From openbrokerapi with MIT License 6 votes vote down vote up
def check_originating_identity():
    """
    Check and decode the "X-Broker-API-Originating-Identity" header
    https://github.com/openservicebrokerapi/servicebroker/blob/v2.13/spec.md#originating-identity
    """
    from flask import request, json
    if "X-Broker-API-Originating-Identity" in request.headers:
        try:
            platform, value = request.headers["X-Broker-API-Originating-Identity"].split(None, 1)
            request.originating_identity = {
                'platform': platform,
                'value': json.loads(base64.standard_b64decode(value))
            }
        except ValueError as e:
            return to_json_response(ErrorResponse(
                description='Improper "X-Broker-API-Originating-Identity" header. ' + str(e))
            ), HTTPStatus.BAD_REQUEST
    else:
        request.originating_identity = None 
Example #10
Source File: handshake.py    From rethinkdb-python with Apache License 2.0 6 votes vote down vote up
def _read_auth_response(self, response):
        """
        Read the authentication request's response sent by the database
        and validate the server signature which was returned.

        :param response: Response from the database
        :raises: ReqlDriverError | ReqlAuthError
        :return: None
        """

        json_response = self._decode_json_response(response, with_utf8=True)

        (
            first_client_message,
            authentication,
        ) = self._get_authentication_and_first_client_message(json_response)
        server_signature = base64.standard_b64decode(authentication[b"v"])

        if not self._compare_digest(server_signature, self._server_signature):
            raise ReqlAuthError("Invalid server signature", self._host, self._port)

        self._next_state() 
Example #11
Source File: send_text.py    From kitty with GNU General Public License v3.0 6 votes vote down vote up
def response_from_kitty(self, boss: Boss, window: Optional[Window], payload_get: PayloadGetType) -> ResponseType:
        windows = [boss.active_window]
        match = payload_get('match')
        if match:
            windows = list(boss.match_windows(match))
        mt = payload_get('match_tab')
        if mt:
            windows = []
            tabs = tuple(boss.match_tabs(mt))
            if not tabs:
                raise MatchError(payload_get('match_tab'), 'tabs')
            for tab in tabs:
                windows += tuple(tab)
        encoding, _, q = payload_get('data').partition(':')
        if encoding == 'text':
            data = q.encode('utf-8')
        elif encoding == 'base64':
            data = base64.standard_b64decode(q)
        else:
            raise TypeError(f'Invalid encoding for send-text data: {encoding}')
        for window in windows:
            if window is not None:
                window.write_to_child(data) 
Example #12
Source File: Crawler.py    From dl_coursera with MIT License 6 votes vote down vote up
def _login(sess, cookies_file=None, cookies_base64=None):
        if cookies_file is None:
            if cookies_base64 is None:
                cookies_base64 = os.environ.get('DL_COURSERA_COOKIES_BASE64')
                assert cookies_base64

            cookies = base64.standard_b64decode(cookies_base64)

            with TmpFile() as tmpfile:
                with open(tmpfile, 'wb') as ofs:
                    ofs.write(cookies)

                cj = MozillaCookieJar()
                cj.load(tmpfile)
        else:
            cj = MozillaCookieJar()
            cj.load(cookies_file)

        sess.cookies.update(cj) 
Example #13
Source File: cfs.py    From collection with MIT License 6 votes vote down vote up
def sign_extract(signature):
	if not signature:
		return None, None, None
	try:
		text = base64.standard_b64decode(signature)
	except:
		return None, None, None
	part = text.split(':')
	if len(part) != 3:
		return None, None, None
	user = part[0].strip('\r\n\t ')
	try:
		ts = long(part[1])
	except:
		return None, None, None
	verify = part[2].strip('\r\n\t ').lower()
	return user, ts, verify

# 检查文件名 
Example #14
Source File: Screenshot.py    From AIL-framework with GNU Affero General Public License v3.0 6 votes vote down vote up
def save_crawled_screeshot(b64_screenshot, max_size, f_save=False):
    screenshot_size = (len(b64_screenshot)*3) /4
    if screenshot_size < max_size or f_save:
        image_content = base64.standard_b64decode(b64_screenshot.encode())
        sha256_string = sha256(image_content).hexdigest()
        filepath = get_screenshot_filepath(sha256_string)
        if os.path.isfile(filepath):
            #print('File already exist')
            return sha256_string
        # create dir
        dirname = os.path.dirname(filepath)
        if not os.path.exists(dirname):
            os.makedirs(dirname)
        with open(filepath, 'wb') as f:
            f.write(image_content)
        return sha256_string
    return False 
Example #15
Source File: base.py    From azure-cosmos-python with MIT License 6 votes vote down vote up
def IsValidBase64String(string_to_validate):
    """Verifies if a string is a valid Base64 encoded string, after replacing '-' with '/'

    :param string string_to_validate:
        String to validate.

    :return:
        Whether given string is a valid base64 string or not.
    :rtype: str
    """
    # '-' is not supported char for decoding in Python(same as C# and Java) which has similar logic while parsing ResourceID generated by backend
    try:
        buffer = base64.standard_b64decode(string_to_validate.replace('-', '/'))
        if len(buffer) != 4:
            return False
    except Exception as e:
        if six.PY2:
            e = e.message
        if type(e) == binascii.Error:
            return False
        else:
            raise e
    return True 
Example #16
Source File: kube_config.py    From ops-cli with Apache License 2.0 6 votes vote down vote up
def as_file(self):
        """If obj[%data_key_name] exists, return name of a file with base64
        decoded obj[%data_key_name] content otherwise obj[%file_key_name]."""
        use_data_if_no_file = not self._file and self._data
        if use_data_if_no_file:
            if self._base64_file_content:
                if isinstance(self._data, str):
                    content = self._data.encode()
                else:
                    content = self._data
                self._file = _create_temp_file_with_content(
                    base64.standard_b64decode(content))
            else:
                self._file = _create_temp_file_with_content(self._data)
        if self._file and not os.path.isfile(self._file):
            raise ConfigException("File does not exists: %s" % self._file)
        return self._file 
Example #17
Source File: test_squeakssl.py    From RSqueak with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def fix(name):
    f = getattr(squeakssl_data, name)
    return w(base64.standard_b64decode(f)) 
Example #18
Source File: _helpers.py    From python-bigquery with Apache License 2.0 5 votes vote down vote up
def _bytes_from_json(value, field):
    """Base64-decode value"""
    if _not_null(value, field):
        return base64.standard_b64decode(_to_bytes(value)) 
Example #19
Source File: requirements.py    From ebonite with Apache License 2.0 5 votes vote down vote up
def decompress(s: str) -> str:
        """
        Helper method to decompress source code

        :param s: compressed source code
        :return: decompressed source code
        """
        zp = base64.standard_b64decode(s.encode('utf8'))
        src = zlib.decompress(zp)
        return src.decode('utf8') 
Example #20
Source File: set_background_image.py    From kitty with GNU General Public License v3.0 5 votes vote down vote up
def response_from_kitty(self, boss: Boss, window: Optional[Window], payload_get: PayloadGetType) -> ResponseType:
        data = payload_get('data')
        if data != '-':
            img_id = payload_get('img_id')
            if img_id != set_background_image.current_img_id:
                set_background_image.current_img_id = img_id
                set_background_image.current_file_obj = tempfile.NamedTemporaryFile()
            if data:
                assert set_background_image.current_file_obj is not None
                set_background_image.current_file_obj.write(standard_b64decode(data))
                return None

        windows = self.windows_for_payload(boss, window, payload_get)
        os_windows = tuple({w.os_window_id for w in windows})
        layout = payload_get('layout')
        if data == '-':
            path = None
        else:
            assert set_background_image.current_file_obj is not None
            f = set_background_image.current_file_obj
            path = f.name
            set_background_image.current_file_obj = None
            f.flush()

        try:
            boss.set_background_image(path, os_windows, payload_get('configured'), layout)
        except ValueError as err:
            err.hide_traceback = True  # type: ignore
            raise 
Example #21
Source File: test_base64.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_b64decode(self):
        eq = self.assertEqual
        eq(base64.b64decode("d3d3LnB5dGhvbi5vcmc="), "www.python.org")
        eq(base64.b64decode('AA=='), '\x00')
        eq(base64.b64decode("YQ=="), "a")
        eq(base64.b64decode("YWI="), "ab")
        eq(base64.b64decode("YWJj"), "abc")
        eq(base64.b64decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
                            "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
                            "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="),
           "abcdefghijklmnopqrstuvwxyz"
           "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
           "0123456789!@#0^&*();:<>,. []{}")
        eq(base64.b64decode(''), '')
        # Test with arbitrary alternative characters
        eq(base64.b64decode('01a*b$cd', altchars='*$'), '\xd3V\xbeo\xf7\x1d')
        # Non-bytes
        eq(base64.b64decode(bytearray("YWJj")), "abc")
        # Test standard alphabet
        eq(base64.standard_b64decode("d3d3LnB5dGhvbi5vcmc="), "www.python.org")
        eq(base64.standard_b64decode("YQ=="), "a")
        eq(base64.standard_b64decode("YWI="), "ab")
        eq(base64.standard_b64decode("YWJj"), "abc")
        eq(base64.standard_b64decode(""), "")
        eq(base64.standard_b64decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
                                     "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
                                     "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="),
           "abcdefghijklmnopqrstuvwxyz"
           "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
           "0123456789!@#0^&*();:<>,. []{}")
        # Non-bytes
        eq(base64.standard_b64decode(bytearray("YWJj")), "abc")
        # Test with 'URL safe' alternative characters
        eq(base64.urlsafe_b64decode('01a-b_cd'), '\xd3V\xbeo\xf7\x1d')
        # Non-bytes
        eq(base64.urlsafe_b64decode(bytearray('01a-b_cd')), '\xd3V\xbeo\xf7\x1d') 
Example #22
Source File: utils.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def decompress_obj(blob):
    decoded_blob = base64.standard_b64decode(blob)
    str_data = zlib.decompress(decoded_blob)
    obj = cPickle.loads(str_data)
    del decoded_blob
    del str_data
    return obj 
Example #23
Source File: window.py    From kitty with GNU General Public License v3.0 5 votes vote down vote up
def handle_remote_print(self, msg: bytes) -> None:
        from base64 import standard_b64decode
        text = standard_b64decode(msg).decode('utf-8')
        print(text, end='', file=sys.stderr)
        sys.stderr.flush() 
Example #24
Source File: graphics.py    From kitty with GNU General Public License v3.0 5 votes vote down vote up
def test_load_png_simple(self):
        # 1x1 transparent PNG
        png_data = standard_b64decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wlseKgAAAABJRU5ErkJggg==')
        expected = b'\x00\xff\xff\x7f'
        self.ae(load_png_data(png_data), (expected, 1, 1))
        s, g, l, sl = load_helpers(self)
        sl(png_data, f=100, expecting_data=expected)
        # test error handling for loading bad png data
        self.assertRaisesRegex(ValueError, '[EBADPNG]', load_png_data, b'dsfsdfsfsfd') 
Example #25
Source File: recipe-501148.py    From code with MIT License 5 votes vote down vote up
def authenticate_client(self):
        validuser = False

        if self.headers.has_key('Authorization'):
            # handle Basic authentication
            (enctype, encstr) =  self.headers.get('Authorization').split()
            (emailid, machine_name) = base64.standard_b64decode(encstr).split(':')
            (auth_machine, auth_uuidstr, auth_email) = authenticate(machine_name)

            if emailid == auth_email:
                print "Authenticated"
                # set authentication cookies on client machines
                validuser = True
                if auth_uuidstr:
                    self.setCookie('UUID',auth_uuidstr)
                    
        elif self.headers.has_key('UUID'):
            # handle cookie based authentication
            id =  self.headers.get('UUID')
            (auth_machine, auth_uuidstr, auth_email) = authenticate(id)

            if auth_uuidstr :
                print "Authenticated"
                validuser = True
        else:
            print 'Authentication failed'

        return validuser 
Example #26
Source File: test_base64.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_decode_nonascii_str(self):
        decode_funcs = (base64.b64decode,
                        base64.standard_b64decode,
                        base64.urlsafe_b64decode,
                        base64.b32decode,
                        base64.b16decode,
                        base64.b85decode,
                        base64.a85decode)
        for f in decode_funcs:
            self.assertRaises(ValueError, f, 'with non-ascii \xcb') 
Example #27
Source File: test_base64.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_b64decode(self):
        eq = self.assertEqual
        eq(base64.b64decode("d3d3LnB5dGhvbi5vcmc="), "www.python.org")
        eq(base64.b64decode('AA=='), '\x00')
        eq(base64.b64decode("YQ=="), "a")
        eq(base64.b64decode("YWI="), "ab")
        eq(base64.b64decode("YWJj"), "abc")
        eq(base64.b64decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
                            "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0\nNT"
                            "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="),
           "abcdefghijklmnopqrstuvwxyz"
           "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
           "0123456789!@#0^&*();:<>,. []{}")
        eq(base64.b64decode(''), '')
        # Test with arbitrary alternative characters
        eq(base64.b64decode('01a*b$cd', altchars='*$'), '\xd3V\xbeo\xf7\x1d')
        # Non-bytes
        eq(base64.b64decode(bytearray("YWJj")), "abc")
        # Test standard alphabet
        eq(base64.standard_b64decode("d3d3LnB5dGhvbi5vcmc="), "www.python.org")
        eq(base64.standard_b64decode("YQ=="), "a")
        eq(base64.standard_b64decode("YWI="), "ab")
        eq(base64.standard_b64decode("YWJj"), "abc")
        eq(base64.standard_b64decode(""), "")
        eq(base64.standard_b64decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXpBQkNE"
                                     "RUZHSElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NT"
                                     "Y3ODkhQCMwXiYqKCk7Ojw+LC4gW117fQ=="),
           "abcdefghijklmnopqrstuvwxyz"
           "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
           "0123456789!@#0^&*();:<>,. []{}")
        # Non-bytes
        eq(base64.standard_b64decode(bytearray("YWJj")), "abc")
        # Test with 'URL safe' alternative characters
        eq(base64.urlsafe_b64decode('01a-b_cd'), '\xd3V\xbeo\xf7\x1d')
        # Non-bytes
        eq(base64.urlsafe_b64decode(bytearray('01a-b_cd')), '\xd3V\xbeo\xf7\x1d') 
Example #28
Source File: test_base64.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_decode_nonascii_str(self):
        decode_funcs = (base64.b64decode,
                        base64.standard_b64decode,
                        base64.urlsafe_b64decode,
                        base64.b32decode,
                        base64.b16decode,
                        base64.b85decode,
                        base64.a85decode)
        for f in decode_funcs:
            self.assertRaises(ValueError, f, 'with non-ascii \xcb') 
Example #29
Source File: test_base64.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def test_b64decode_invalid_chars(self):
        # issue 1466065: Test some invalid characters.
        tests = ((b'%3d==', b'\xdd'),
                 (b'$3d==', b'\xdd'),
                 (b'[==', b''),
                 (b'YW]3=', b'am'),
                 (b'3{d==', b'\xdd'),
                 (b'3d}==', b'\xdd'),
                 (b'@@', b''),
                 (b'!', b''),
                 (b'YWJj\nYWI=', b'abcab'))
        funcs = (
            base64.b64decode,
            base64.standard_b64decode,
            base64.urlsafe_b64decode,
        )
        for bstr, res in tests:
            for func in funcs:
                with self.subTest(bstr=bstr, func=func):
                    self.assertEqual(func(bstr), res)
                    self.assertEqual(func(bstr.decode('ascii')), res)
            with self.assertRaises(binascii.Error):
                base64.b64decode(bstr, validate=True)
            with self.assertRaises(binascii.Error):
                base64.b64decode(bstr.decode('ascii'), validate=True)

        # Normal alphabet characters not discarded when alternative given
        res = b'\xFB\xEF\xBE\xFF\xFF\xFF'
        self.assertEqual(base64.b64decode(b'++[[//]]', b'[]'), res)
        self.assertEqual(base64.urlsafe_b64decode(b'++--//__'), res) 
Example #30
Source File: test_kafka_schema_registry.py    From datacollector-tests with Apache License 2.0 5 votes vote down vote up
def verify_kafka_avro_messages(record, key_capture_mode, key_capture_field, key_capture_attribute, key_schema, message_keys, message_values):

    for key,value in message_values.items():
      assert record.get_field_data(f'/{key}').value == value

    # validate message key fields/attribute based on configuration
    if (key_capture_mode in ['RECORD_FIELD', 'RECORD_HEADER_AND_FIELD']):
      # the message key should have been captured into the configured field
      for key,value in message_keys.items():
        assert record.get_field_data(f"{key_capture_field}/{key}").value == value

    if (key_capture_mode in ['RECORD_HEADER', 'RECORD_HEADER_AND_FIELD']):
      # get the base64 encoded Avro message key
      encoded = record.header['values'][key_capture_attribute]
      # decode into bytes
      key_bytes = base64.standard_b64decode(encoded)
      # create an Avro binary decoder based on those bytes
      decoder = BinaryDecoder(io.BytesIO(key_bytes))
      # parse the key schema out of the record header
      decoded_key_schema = confluent_kafka.avro.loads(record.header['values']['avroKeySchema'])
      # ensure the parsed key schema matches the one we actually produced, earlier
      assert decoded_key_schema == key_schema
      # create a DatumReader to read a full Avro record (the key)
      reader = DatumReader(decoded_key_schema)
      decoded_avro_key = reader.read(decoder)
      # assert the values from the Avro record match what's expected
      for key,value in message_keys.items():
        assert decoded_avro_key[key] == value