Repository: boto/botocore
Pull request #2906 (2023-04-11 20:01:33)

change error for missing crt. use list of crt checksums instead
Follow up to https://github.com/boto/botocore/pull/2905. Use `MissingDependencyException` and check against extendible list of CRT checksums rather than single strings. Added test to validate CRT checksum list is in sync with checksum class dictionary keys.

codecov-commenter: ## [Codecov](https://codecov.io/gh/boto/botocore/pull/2906?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto) Report
Patch coverage: **`100.00`**% and project coverage change: **`-0.03`** :warning:
> Comparison is base [(`2318884`)](https://codecov.io/gh/boto/botocore/commit/23188849d075658a6c5931a345e390de1e671234?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto) 93.55% compared to head [(`dcbd69a`)](https://codecov.io/gh/boto/botocore/pull/2906?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto) 93.53%.

> :exclamation: Current head dcbd69a differs from pull request most recent head bdc6db7. Consider uploading reports for the commit bdc6db7 to get more accurate results

:mega: This organization is not using Codecov’s [GitHub App Integration](https://github.com/apps/codecov). We recommend you install it so Codecov can continue to function properly for your repositories. [Learn more](https://about.codecov.io/blog/codecov-is-updating-its-github-integration/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto)

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##           develop    #2906      +/-   ##
===========================================
- Coverage    93.55%   93.53%   -0.03%     
===========================================
  Files           63       63              
  Lines        13521    13524       +3     
===========================================
  Hits         12649    12649              
- Misses         872      875       +3     
```


| [Impacted Files](https://codecov.io/gh/boto/botocore/pull/2906?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto) | Coverage Δ | |
|---|---|---|
| [botocore/httpchecksum.py](https://codecov.io/gh/boto/botocore/pull/2906?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto#diff-Ym90b2NvcmUvaHR0cGNoZWNrc3VtLnB5) | `94.27% <100.00%> (+0.06%)` | :arrow_up: |

... and [2 files with indirect coverage changes](https://codecov.io/gh/boto/botocore/pull/2906/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto)

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto)

</details>

[:umbrella: View full report in Codecov by Sentry](https://codecov.io/gh/boto/botocore/pull/2906?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=boto).


Patch:
diff --git a/botocore/httpchecksum.py b/botocore/httpchecksum.py
index 2fab60c56..b0b84b400 100644
--- a/botocore/httpchecksum.py
+++ b/botocore/httpchecksum.py
@@ -25,7 +25,11 @@ from binascii import crc32
 from hashlib import sha1, sha256
 
 from botocore.compat import HAS_CRT
-from botocore.exceptions import AwsChunkedWrapperError, FlexibleChecksumError
+from botocore.exceptions import (
+    AwsChunkedWrapperError,
+    FlexibleChecksumError,
+    MissingDependencyException,
+)
 from botocore.response import StreamingBody
 from botocore.utils import (
     conditionally_calculate_md5,
@@ -250,14 +254,15 @@ def resolve_request_checksum_algorithm(
             supported_algorithms = _SUPPORTED_CHECKSUM_ALGORITHMS
 
         algorithm_name = params[algorithm_member].lower()
-        if algorithm_name == "crc32c" and not HAS_CRT:
-            raise FlexibleChecksumError(
-                error_msg=(
-                    "Using CRC32C requires an additional dependency. You will "
-                    "need to pip install botocore[crt] before proceeding."
+        if algorithm_name not in supported_algorithms:
+            if not HAS_CRT and algorithm_name in _CRT_CHECKSUM_ALGORITHMS:
+                raise MissingDependencyException(
+                    msg=(
+                        f"Using {algorithm_name.upper()} requires an "
+                        "additional dependency. You will need to pip install "
+                        "botocore[crt] before proceeding."
+                    )
                 )
-            )
-        elif algorithm_name not in supported_algorithms:
             raise FlexibleChecksumError(
                 error_msg="Unsupported checksum algorithm: %s" % algorithm_name
             )
@@ -462,12 +467,17 @@ _CHECKSUM_CLS = {
     "sha1": Sha1Checksum,
     "sha256": Sha256Checksum,
 }
-
+_CRT_CHECKSUM_ALGORITHMS = ["crc32", "crc32c"]
 if HAS_CRT:
     # Use CRT checksum implementations if available
-    _CHECKSUM_CLS.update(
-        {"crc32": CrtCrc32Checksum, "crc32c": CrtCrc32cChecksum}
+    _CRT_CHECKSUM_CLS = {
+        "crc32": CrtCrc32Checksum,
+        "crc32c": CrtCrc32cChecksum,
+    }
+    _CHECKSUM_CLS.update(_CRT_CHECKSUM_CLS)
+    # Validate this list isn't out of sync with _CRT_CHECKSUM_CLS keys
+    assert all(
+        name in _CRT_CHECKSUM_ALGORITHMS for name in _CRT_CHECKSUM_CLS.keys()
     )
-
 _SUPPORTED_CHECKSUM_ALGORITHMS = list(_CHECKSUM_CLS.keys())
 _ALGORITHMS_PRIORITY_LIST = ['crc32c', 'crc32', 'sha1', 'sha256']


Test patch:
diff --git a/tests/unit/test_httpchecksum.py b/tests/unit/test_httpchecksum.py
index e103b97a5..bc8265a9b 100644
--- a/tests/unit/test_httpchecksum.py
+++ b/tests/unit/test_httpchecksum.py
@@ -15,7 +15,11 @@ from io import BytesIO
 
 from botocore.awsrequest import AWSResponse
 from botocore.compat import HAS_CRT
-from botocore.exceptions import AwsChunkedWrapperError, FlexibleChecksumError
+from botocore.exceptions import (
+    AwsChunkedWrapperError,
+    FlexibleChecksumError,
+    MissingDependencyException,
+)
 from botocore.httpchecksum import (
     _CHECKSUM_CLS,
     AwsChunkedWrapper,
@@ -166,7 +170,7 @@ class TestHttpChecksumHandlers(unittest.TestCase):
         operation_model = self._make_operation_model(
             http_checksum={"requestAlgorithmMember": "Algorithm"},
         )
-        params = {"Algorithm": "crc32"}
+        params = {"Algorithm": "sha256"}
 
         with self.assertRaises(FlexibleChecksumError):
             resolve_request_checksum_algorithm(
@@ -180,7 +184,7 @@ class TestHttpChecksumHandlers(unittest.TestCase):
             http_checksum={"requestAlgorithmMember": "Algorithm"},
         )
         params = {"Algorithm": "crc32c"}
-        with self.assertRaises(FlexibleChecksumError) as context:
+        with self.assertRaises(MissingDependencyException) as context:
             resolve_request_checksum_algorithm(
                 request, operation_model, params
             )