# Path: testbench/csek.py
# def extract(request, is_source, context):
# def check(algorithm, key_b64, expected_sha256, context):
# def validation(request, expected_sha256, is_source, context):
# 
# Path: testbench/error.py
# class RestException(Exception):
#     def __init__(self, msg, code):
#     def as_response(self):
#     def handler(ex):
# def _simple_json_error(msg):
# def generic(msg, rest_code, grpc_code, context):
# def csek(context, rest_code=400, grpc_code=grpc.StatusCode.INVALID_ARGUMENT):
# def invalid(msg, context, rest_code=400, grpc_code=grpc.StatusCode.INVALID_ARGUMENT):
# def missing(name, context, rest_code=400, grpc_code=grpc.StatusCode.INVALID_ARGUMENT):
# def mismatch(
#     msg,
#     expect,
#     actual,
#     context,
#     rest_code=412,
#     grpc_code=grpc.StatusCode.FAILED_PRECONDITION,
# ):
# def notchanged(msg, context, rest_code=304, grpc_code=grpc.StatusCode.ABORTED):
# def notfound(name, context, rest_code=404, grpc_code=grpc.StatusCode.NOT_FOUND):
# def notallowed(context=None, rest_code=405, grpc_code=None):
# def already_exists(context=None):


import base64
import hashlib
import unittest
import grpc
from unittest.mock import ANY, Mock
from werkzeug.test import create_environ
from werkzeug.wrappers import Request
from google.storage.v2 import storage_pb2
from testbench import csek, error
        key_sha256_b64 = base64.b64encode(key_sha256).decode("utf-8")
        environ = create_environ(
            base_url="http://localhost:8080",
            content_type="application/json",
            method="POST",
            headers={
                "x-goog-encryption-algorithm": "## INVALID ##",
                "x-goog-encryption-key": key_b64,
                "x-goog-encryption-key-sha256": key_sha256_b64,
            },
        )
        with self.assertRaises(error.RestException) as rest:
            csek.validation(Request(environ), key_sha256, False, None)
        self.assertEqual(rest.exception.code, 400)

    def test_validation_nothing_expected_failure(self):
        key = b"1234567890" + b"1234567890" + b"1234567890" + b"AA"
        key_sha256 = hashlib.sha256(key).digest()
        key_b64 = base64.b64encode(key)
        key_sha256_b64 = base64.b64encode(key_sha256).decode("utf-8")
        environ = create_environ(
            base_url="http://localhost:8080",
            content_type="application/json",
            method="POST",
            headers={
                "x-goog-encryption-algorithm": "AES256",
                "x-goog-encryption-key": key_b64,
                "x-goog-encryption-key-sha256": key_sha256_b64,
            },
        )

# Path: testbench/csek.py
# def extract(request, is_source, context):
# def check(algorithm, key_b64, expected_sha256, context):
# def validation(request, expected_sha256, is_source, context):
# 
# Path: testbench/error.py
# class RestException(Exception):
#     def __init__(self, msg, code):
#     def as_response(self):
#     def handler(ex):
# def _simple_json_error(msg):
# def generic(msg, rest_code, grpc_code, context):
# def csek(context, rest_code=400, grpc_code=grpc.StatusCode.INVALID_ARGUMENT):
# def invalid(msg, context, rest_code=400, grpc_code=grpc.StatusCode.INVALID_ARGUMENT):
# def missing(name, context, rest_code=400, grpc_code=grpc.StatusCode.INVALID_ARGUMENT):
# def mismatch(
#     msg,
#     expect,
#     actual,
#     context,
#     rest_code=412,
#     grpc_code=grpc.StatusCode.FAILED_PRECONDITION,
# ):
# def notchanged(msg, context, rest_code=304, grpc_code=grpc.StatusCode.ABORTED):
# def notfound(name, context, rest_code=404, grpc_code=grpc.StatusCode.NOT_FOUND):
# def notallowed(context=None, rest_code=405, grpc_code=None):
# def already_exists(context=None):


# Path: tests/test_csek.py
import base64
import hashlib
import unittest
import grpc
from unittest.mock import ANY, Mock
from werkzeug.test import create_environ
from werkzeug.wrappers import Request
from google.storage.v2 import storage_pb2
from testbench import csek, error

        key_sha256_b64 = base64.b64encode(key_sha256).decode("utf-8")
        environ = create_environ(
            base_url="http://localhost:8080",
            content_type="application/json",
            method="POST",
            headers={
                "x-goog-encryption-algorithm": "## INVALID ##",
                "x-goog-encryption-key": key_b64,
                "x-goog-encryption-key-sha256": key_sha256_b64,
            },
        )
        with self.assertRaises(error.RestException) as rest:
            csek.validation(Request(environ), key_sha256, False, None)
        self.assertEqual(rest.exception.code, 400)

    def test_validation_nothing_expected_failure(self):
        key = b"1234567890" + b"1234567890" + b"1234567890" + b"AA"
        key_sha256 = hashlib.sha256(key).digest()
        key_b64 = base64.b64encode(key)
        key_sha256_b64 = base64.b64encode(key_sha256).decode("utf-8")
        environ = create_environ(
            base_url="http://localhost:8080",
            content_type="application/json",
            method="POST",
            headers={
                "x-goog-encryption-algorithm": "AES256",
                "x-goog-encryption-key": key_b64,
                "x-goog-encryption-key-sha256": key_sha256_b64,
            },
        )

        with self.assertRaises(error.RestException) as rest: