Best Python code snippet using avocado_python
httpchecksum.py
Source:httpchecksum.py  
...166    def read(self, amt=None):167        chunk = super().read(amt=amt)168        self._checksum.update(chunk)169        if amt is None or (not chunk and amt > 0):170            self._validate_checksum()171        return chunk172    def _validate_checksum(self):173        if self._checksum.digest() != base64.b64decode(self._expected):174            error_msg = (175                f"Expected checksum {self._expected} did not match calculated "176                f"checksum: {self._checksum.b64digest()}"177            )178            raise FlexibleChecksumError(error_msg=error_msg)179def resolve_checksum_context(request, operation_model, params):180    resolve_request_checksum_algorithm(request, operation_model, params)181    resolve_response_checksum_algorithms(request, operation_model, params)182def resolve_request_checksum_algorithm(183    request,184    operation_model,185    params,186    supported_algorithms=None,187):188    http_checksum = operation_model.http_checksum189    algorithm_member = http_checksum.get("requestAlgorithmMember")190    if algorithm_member and algorithm_member in params:191        # If the client has opted into using flexible checksums and the192        # request supports it, use that instead of checksum required193        if supported_algorithms is None:194            supported_algorithms = _SUPPORTED_CHECKSUM_ALGORITHMS195        algorithm_name = params[algorithm_member].lower()196        if algorithm_name not in supported_algorithms:197            raise FlexibleChecksumError(198                error_msg="Unsupported checksum algorithm: %s" % algorithm_name199            )200        location_type = "header"201        if operation_model.has_streaming_input:202            # Operations with streaming input must support trailers.203            if request["url"].startswith("https:"):204                # We only support unsigned trailer checksums currently. As this205                # disables payload signing we'll only use trailers over TLS.206                location_type = "trailer"207        algorithm = {208            "algorithm": algorithm_name,209            "in": location_type,210            "name": "x-amz-checksum-%s" % algorithm_name,211        }212        if algorithm["name"] in request["headers"]:213            # If the header is already set by the customer, skip calculation214            return215        checksum_context = request["context"].get("checksum", {})216        checksum_context["request_algorithm"] = algorithm217        request["context"]["checksum"] = checksum_context218    elif operation_model.http_checksum_required or http_checksum.get(219        "requestChecksumRequired"220    ):221        # Otherwise apply the old http checksum behavior via Content-MD5222        checksum_context = request["context"].get("checksum", {})223        checksum_context["request_algorithm"] = "conditional-md5"224        request["context"]["checksum"] = checksum_context225def apply_request_checksum(request):226    checksum_context = request.get("context", {}).get("checksum", {})227    algorithm = checksum_context.get("request_algorithm")228    if not algorithm:229        return230    if algorithm == "conditional-md5":231        # Special case to handle the http checksum required trait232        conditionally_calculate_md5(request)233    elif algorithm["in"] == "header":234        _apply_request_header_checksum(request)235    elif algorithm["in"] == "trailer":236        _apply_request_trailer_checksum(request)237    else:238        raise FlexibleChecksumError(239            error_msg="Unknown checksum variant: %s" % algorithm["in"]240        )241def _apply_request_header_checksum(request):242    checksum_context = request.get("context", {}).get("checksum", {})243    algorithm = checksum_context.get("request_algorithm")244    location_name = algorithm["name"]245    if location_name in request["headers"]:246        # If the header is already set by the customer, skip calculation247        return248    checksum_cls = _CHECKSUM_CLS.get(algorithm["algorithm"])249    digest = checksum_cls().handle(request["body"])250    request["headers"][location_name] = digest251def _apply_request_trailer_checksum(request):252    checksum_context = request.get("context", {}).get("checksum", {})253    algorithm = checksum_context.get("request_algorithm")254    location_name = algorithm["name"]255    checksum_cls = _CHECKSUM_CLS.get(algorithm["algorithm"])256    headers = request["headers"]257    body = request["body"]258    if location_name in headers:259        # If the header is already set by the customer, skip calculation260        return261    headers["Transfer-Encoding"] = "chunked"262    headers["Content-Encoding"] = "aws-chunked"263    headers["X-Amz-Trailer"] = location_name264    content_length = determine_content_length(body)265    if content_length is not None:...test_computechecksums.py
Source:test_computechecksums.py  
...10    pp = ComputeChecksums()11    pp.comment_index = 012    return pp13class TestComputeChecksums(BaseTestPreprocessor):14    def test_code_cell_no_checksum(self, preprocessor):15        """Test that no checksum is computed for a regular code cell"""16        cell, _ = preprocessor.preprocess_cell(17            create_code_cell(), {}, 0)18        assert "nbgrader" not in cell.metadata or "checksum" not in cell.metadata.nbgrader19    def test_text_cell_no_checksum(self, preprocessor):20        """Test that no checksum is computed for a regular text cell"""21        cell, _ = preprocessor.preprocess_cell(22            create_text_cell(), {}, 0)23        assert "nbgrader" not in cell.metadata or "checksum" not in cell.metadata.nbgrader24    def test_checksum_grade_cell_type(self, preprocessor):25        """Test that the checksum is computed for grade cells of different cell types"""26        cell1 = create_grade_cell("", "code", "foo", 1)27        cell1 = preprocessor.preprocess_cell(cell1, {}, 0)[0]28        cell2 = create_grade_cell("", "markdown", "foo", 1)29        cell2 = preprocessor.preprocess_cell(cell2, {}, 0)[0]30        assert cell1.metadata.nbgrader["checksum"] == compute_checksum(cell1)31        assert cell2.metadata.nbgrader["checksum"] == compute_checksum(cell2)32        assert cell1.metadata.nbgrader["checksum"] != cell2.metadata.nbgrader["checksum"]33    def test_checksum_solution_cell_type(self, preprocessor):34        """Test that the checksum is computed for solution cells of different cell types"""35        cell1 = create_solution_cell("", "code", "foo")36        cell1 = preprocessor.preprocess_cell(cell1, {}, 0)[0]37        cell2 = create_solution_cell("", "markdown", "foo")38        cell2 = preprocessor.preprocess_cell(cell2, {}, 0)[0]39        assert cell1.metadata.nbgrader["checksum"] == compute_checksum(cell1)40        assert cell2.metadata.nbgrader["checksum"] == compute_checksum(cell2)41        assert cell1.metadata.nbgrader["checksum"] != cell2.metadata.nbgrader["checksum"]42    def test_checksum_locked_cell_type(self, preprocessor):43        """Test that the checksum is computed for locked cells"""44        cell1 = create_locked_cell("", "code", "foo")45        cell1 = preprocessor.preprocess_cell(cell1, {}, 0)[0]46        cell2 = create_locked_cell("", "markdown", "foo")47        cell2 = preprocessor.preprocess_cell(cell2, {}, 0)[0]48        assert cell1.metadata.nbgrader["checksum"] == compute_checksum(cell1)49        assert cell2.metadata.nbgrader["checksum"] == compute_checksum(cell2)50        assert cell1.metadata.nbgrader["checksum"] != cell2.metadata.nbgrader["checksum"]51    def test_checksum_points(self, preprocessor):52        """Test that the checksum is computed for grade cells with different points"""53        cell1 = create_grade_cell("", "code", "foo", 1)54        cell1 = preprocessor.preprocess_cell(cell1, {}, 0)[0]55        cell2 = create_grade_cell("", "code", "foo", 2)56        cell2 = preprocessor.preprocess_cell(cell2, {}, 0)[0]57        assert cell1.metadata.nbgrader["checksum"] == compute_checksum(cell1)58        assert cell2.metadata.nbgrader["checksum"] == compute_checksum(cell2)59        assert cell1.metadata.nbgrader["checksum"] != cell2.metadata.nbgrader["checksum"]60    def test_checksum_grade_id(self, preprocessor):61        """Test that the checksum is computed for grade cells with different ids"""62        cell1 = create_grade_cell("", "code", "foo", 1)63        cell1 = preprocessor.preprocess_cell(cell1, {}, 0)[0]64        cell2 = create_grade_cell("", "code", "bar", 1)65        cell2 = preprocessor.preprocess_cell(cell2, {}, 0)[0]66        assert cell1.metadata.nbgrader["checksum"] == compute_checksum(cell1)67        assert cell2.metadata.nbgrader["checksum"] == compute_checksum(cell2)68        assert cell1.metadata.nbgrader["checksum"] != cell2.metadata.nbgrader["checksum"]69    def test_checksum_grade_source(self, preprocessor):70        """Test that the checksum is computed for grade cells with different sources"""71        cell1 = create_grade_cell("a", "code", "foo", 1)72        cell1 = preprocessor.preprocess_cell(cell1, {}, 0)[0]73        cell2 = create_grade_cell("b", "code", "foo", 1)74        cell2 = preprocessor.preprocess_cell(cell2, {}, 0)[0]75        assert cell1.metadata.nbgrader["checksum"] == compute_checksum(cell1)76        assert cell2.metadata.nbgrader["checksum"] == compute_checksum(cell2)77        assert cell1.metadata.nbgrader["checksum"] != cell2.metadata.nbgrader["checksum"]78    def test_checksum_solution_source(self, preprocessor):79        """Test that the checksum is computed for solution cells with different sources"""80        cell1 = create_solution_cell("a", "code", "foo")81        cell1 = preprocessor.preprocess_cell(cell1, {}, 0)[0]82        cell2 = create_solution_cell("b", "code", "foo")83        cell2 = preprocessor.preprocess_cell(cell2, {}, 0)[0]84        assert cell1.metadata.nbgrader["checksum"] == compute_checksum(cell1)85        assert cell2.metadata.nbgrader["checksum"] == compute_checksum(cell2)86        assert cell1.metadata.nbgrader["checksum"] != cell2.metadata.nbgrader["checksum"]87    def test_checksum_grade_and_solution(self, preprocessor):88        """Test that a checksum is created for grade cells that are also solution cells"""89        cell1 = create_grade_cell("", "markdown", "foo", 1)90        cell1 = preprocessor.preprocess_cell(cell1, {}, 0)[0]91        cell2 = create_grade_cell("", "markdown", "foo", 1)92        cell2.metadata.nbgrader["solution"] = True93        cell2 = preprocessor.preprocess_cell(cell2, {}, 0)[0]94        assert cell1.metadata.nbgrader["checksum"] == compute_checksum(cell1)95        assert cell2.metadata.nbgrader["checksum"] == compute_checksum(cell2)...ChecksumEncoder.py
Source:ChecksumEncoder.py  
1from Classes.CPPDefs import CPPDefs2from Classes.Debugger import Debugger3from Classes.Logic.LogicLong import LogicLong4class ChecksumEncoder:5    def __init__(self):6        self.checksum = 07        self.checksum2 = 08        self.checksumEnabled = True9    def destruct(self):10        self.checksum = 011        self.checksum2 = 012        self.checksumEnabled = True13    def enableCheckSum(self, state):14        if not self.checksumEnabled or state:15            if not self.checksumEnabled and state:16                self.checksum = self.checksum217            self.checksumEnabled = state18        else:19            self.checksum2 = self.checksum20            self.checksumEnabled = False21    def equals(self, checksum_instance):22        if not checksum_instance:23            return False24        if not checksum_instance.checksumEnabled:25            checksum = checksum_instance.checksum26        else:27            checksum2 = checksum_instance.checksum228        if not self.checksumEnabled:29            checksum = self.checksum30        else:31            checksum2 = self.checksum232        return checksum == checksum233    def getCheckSum(self):34        if not self.checksumEnabled:35            checksum = self.checksum236        else:37            checksum = self.checksum38        return checksum39    @staticmethod40    def hashCode():41        Debugger.error("ChecksumEncoder hashCode not designed")42        return 4243    @staticmethod44    def isByteStream():45        return False46    def isCheckSumEnabled(self):47        return self.checksumEnabled48    @staticmethod49    def isCheckSumOnlyMode():50        return True51    def resetCheckSum(self):52        self.checksum = 053    def writeBoolean(self, value):54        if value: integer = 1355        else: integer = 756        self.checksum = integer + CPPDefs.__ROR4__(self.checksum, 31)57    def writeByte(self, value):58        self.checksum = CPPDefs.__ROR4__(self.checksum, 31) + value + 1159    def writeBytes(self, value, length):60        if value: integer = length + 3861        else: integer = 3762        self.checksum = CPPDefs.__ROR4__(self.checksum, 31)63    def writeInt8(self, value):64        if value + 0x80 >= 0x100:65            Debugger.error("")66        self.checksum = CPPDefs.__ROR4__(self.checksum, 31) + value + 1167    def writeInt16(self, value):68        if value + 0x8000 >= 0x10000:69            Debugger.error("")70        self.checksum = CPPDefs.__ROR4__(self.checksum, 31) + value + 1971    def writeInt24(self, value):72        if value + 0x800000 >= 0x1000000:73            Debugger.error("")74        self.checksum = (value & 0xFFFFFF) + CPPDefs.__ROR4__(self.checksum, 31) + value + 2175    def writeInt(self, value):76        self.checksum = CPPDefs.__ROR4__(self.checksum, 31) + value + 977    @staticmethod78    def writeLong(bytestream, logicLong):79        logicLong.encode(bytestream)80    def writeLongLong(self, logicLong):81        self.checksum = logicLong.getLowerInt() + CPPDefs.__ROR4__(logicLong.getHigherInt() + CPPDefs.__ROR4__(self.checksum, 31) + 67, 31) + 9182    def writeShort(self, value):83        self.checksum = CPPDefs.__ROR4__(self.checksum, 31) + value + 1984    def writeString(self, value):85        checksum = CPPDefs.__ROR4__(self.checksum, 31)86        if value:87            self.checksum = checksum + len(value) + 2888        else:89            self.checksum = checksum + 2790    def writeStringReference(self, value):91        self.checksum = len(value) + CPPDefs.__ROR4__(self.checksum, 31) + 3892    def writeVInt(self, value):93        self.checksum = value + CPPDefs.__ROR4__(self.checksum, 31) + 3394    def writeVLong(self, high, low):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
