1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
|
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import unittest
from io import BytesIO
from botocore.awsrequest import AWSResponse
from botocore.compat import HAS_CRT
from botocore.config import Config
from botocore.exceptions import (
AwsChunkedWrapperError,
FlexibleChecksumError,
MissingDependencyException,
)
from botocore.httpchecksum import (
AwsChunkedWrapper,
Crc32Checksum,
CrtCrc32cChecksum,
CrtCrc32Checksum,
CrtCrc64NvmeChecksum,
Sha1Checksum,
Sha256Checksum,
StreamingChecksumBody,
apply_request_checksum,
handle_checksum_body,
resolve_request_checksum_algorithm,
resolve_response_checksum_algorithms,
)
from botocore.model import OperationModel, StringShape, StructureShape
from tests import get_checksum_cls, mock, requires_crt
class TestHttpChecksumHandlers(unittest.TestCase):
def _make_operation_model(
self,
http_checksum=None,
streaming_output=False,
streaming_input=False,
required=False,
):
operation = mock.Mock(spec=OperationModel)
if http_checksum is None:
http_checksum = {}
operation.http_checksum = http_checksum
operation.http_checksum_required = required
operation.has_streaming_output = streaming_output
operation.has_streaming_input = streaming_input
if http_checksum and "requestAlgorithmMember" in http_checksum:
shape = mock.Mock(spec=StringShape)
shape.serialization = {"name": "x-amz-request-algorithm"}
operation.input_shape = mock.Mock(spec=StructureShape)
operation.input_shape.members = {
http_checksum["requestAlgorithmMember"]: shape
}
return operation
def _make_http_response(
self,
body,
headers=None,
context=None,
streaming=False,
):
if context is None:
context = {}
if headers is None:
headers = {}
http_response = mock.Mock(spec=AWSResponse)
http_response.raw = BytesIO(body)
http_response.content = body
http_response.status_code = 200
http_response.headers = headers
response_dict = {
"headers": http_response.headers,
"status_code": http_response.status_code,
"context": context,
}
if streaming:
response_dict["body"] = BytesIO(body)
else:
response_dict["body"] = body
return http_response, response_dict
def _build_request(self, body):
request = {
"headers": {},
"body": body,
"context": {
"client_config": Config(
request_checksum_calculation="when_supported",
)
},
"url": "https://example.com",
}
return request
def test_request_checksum_algorithm_no_model(self):
request = self._build_request(b"")
operation_model = self._make_operation_model()
params = {}
resolve_request_checksum_algorithm(request, operation_model, params)
self.assertNotIn("checksum", request["context"])
def test_request_checksum_algorithm_model_default(self):
operation_model = self._make_operation_model(
http_checksum={"requestAlgorithmMember": "Algorithm"}
)
# Param is not present, crc32 checksum will be set by default
params = {}
request = self._build_request(b"")
resolve_request_checksum_algorithm(request, operation_model, params)
expected_algorithm = {
"algorithm": "crc32",
"in": "header",
"name": "x-amz-checksum-crc32",
}
expected_request_algorithm_header = {
"name": "x-amz-request-algorithm",
"value": "CRC32",
}
actual_algorithm = request["context"]["checksum"]["request_algorithm"]
actual_request_algorithm_header = request["context"]["checksum"][
"request_algorithm_header"
]
self.assertEqual(actual_algorithm, expected_algorithm)
self.assertEqual(
actual_request_algorithm_header, expected_request_algorithm_header
)
# Param is present, sha256 checksum will be set
params = {"Algorithm": "sha256"}
request = self._build_request(b"")
resolve_request_checksum_algorithm(request, operation_model, params)
expected_algorithm = {
"algorithm": "sha256",
"in": "header",
"name": "x-amz-checksum-sha256",
}
actual_algorithm = request["context"]["checksum"]["request_algorithm"]
self.assertEqual(actual_algorithm, expected_algorithm)
# Param present but header already set, checksum should be skipped
params = {"Algorithm": "crc32"}
request = self._build_request(b"")
request["headers"]["x-amz-checksum-crc32"] = "foo"
resolve_request_checksum_algorithm(request, operation_model, params)
self.assertNotIn("checksum", request["context"])
def test_request_checksum_algorithm_model_default_streaming(self):
request = self._build_request(b"")
operation_model = self._make_operation_model(
http_checksum={"requestAlgorithmMember": "Algorithm"},
streaming_input=True,
)
# Param is not present, crc32 checksum will be set
params = {}
resolve_request_checksum_algorithm(request, operation_model, params)
expected_algorithm = {
"algorithm": "crc32",
"in": "trailer",
"name": "x-amz-checksum-crc32",
}
expected_request_algorithm_header = {
"name": "x-amz-request-algorithm",
"value": "CRC32",
}
actual_algorithm = request["context"]["checksum"]["request_algorithm"]
actual_request_algorithm_header = request["context"]["checksum"][
"request_algorithm_header"
]
self.assertEqual(actual_algorithm, expected_algorithm)
self.assertEqual(
actual_request_algorithm_header, expected_request_algorithm_header
)
# Param is present, sha256 checksum will be set in the trailer
params = {"Algorithm": "sha256"}
resolve_request_checksum_algorithm(request, operation_model, params)
expected_algorithm = {
"algorithm": "sha256",
"in": "trailer",
"name": "x-amz-checksum-sha256",
}
actual_algorithm = request["context"]["checksum"]["request_algorithm"]
self.assertEqual(actual_algorithm, expected_algorithm)
# Trailer should not be used for http endpoints
request = self._build_request(b"")
request["url"] = "http://example.com"
resolve_request_checksum_algorithm(request, operation_model, params)
expected_algorithm = {
"algorithm": "sha256",
"in": "header",
"name": "x-amz-checksum-sha256",
}
actual_algorithm = request["context"]["checksum"]["request_algorithm"]
self.assertEqual(actual_algorithm, expected_algorithm)
def test_request_checksum_algorithm_s3_signature_version_input_streaming(
self,
):
config = Config(signature_version="s3")
request = self._build_request(b"")
request["context"]["client_config"] = config
operation_model = self._make_operation_model(
http_checksum={"requestChecksumRequired": True},
streaming_input=True,
)
params = {}
resolve_request_checksum_algorithm(request, operation_model, params)
actual_algorithm = request["context"]["checksum"]["request_algorithm"]
# Operations with streaming input using the "s3" signature_version should send
# checksums in the header, not trailer.
expected_algorithm = {
"algorithm": "crc32",
"in": "header",
"name": "x-amz-checksum-crc32",
}
self.assertEqual(actual_algorithm, expected_algorithm)
def test_request_checksum_algorithm_presigned_request(self):
request = self._build_request(b"")
request["context"]["is_presign_request"] = True
operation_model = self._make_operation_model(
http_checksum={"requestChecksumRequired": True},
)
params = {}
resolve_request_checksum_algorithm(request, operation_model, params)
# Presigned requests shouldn't use flexible checksums by default.
self.assertNotIn("checksum", request["context"])
def test_request_checksum_algorithm_model_unsupported_algorithm(self):
request = self._build_request(b"")
operation_model = self._make_operation_model(
http_checksum={"requestAlgorithmMember": "Algorithm"},
)
params = {"Algorithm": "sha256"}
with self.assertRaises(FlexibleChecksumError):
resolve_request_checksum_algorithm(
request, operation_model, params, supported_algorithms=[]
)
@unittest.skipIf(HAS_CRT, "Error only expected when CRT is not available")
def test_request_checksum_algorithm_model_no_crt_crc32c_unsupported(self):
request = self._build_request(b"")
operation_model = self._make_operation_model(
http_checksum={"requestAlgorithmMember": "Algorithm"},
)
params = {"Algorithm": "crc32c"}
with self.assertRaises(MissingDependencyException) as context:
resolve_request_checksum_algorithm(
request, operation_model, params
)
self.assertIn(
"Using CRC32C requires an additional dependency",
str(context.exception),
)
@unittest.skipIf(HAS_CRT, "Error only expected when CRT is not available")
def test_request_checksum_algorithm_model_no_crt_crc64nvme_unsupported(
self,
):
request = self._build_request(b"")
operation_model = self._make_operation_model(
http_checksum={"requestAlgorithmMember": "Algorithm"},
)
params = {"Algorithm": "crc64nvme"}
with self.assertRaises(MissingDependencyException) as context:
resolve_request_checksum_algorithm(
request, operation_model, params
)
self.assertIn(
"Using CRC64NVME requires an additional dependency",
str(context.exception),
)
def test_request_checksum_algorithm_model_legacy_crc32(self):
request = self._build_request(b"")
operation_model = self._make_operation_model(required=True)
params = {}
resolve_request_checksum_algorithm(request, operation_model, params)
expected_algorithm = {
"algorithm": "crc32",
"in": "header",
"name": "x-amz-checksum-crc32",
}
actual_algorithm = request["context"]["checksum"]["request_algorithm"]
self.assertEqual(actual_algorithm, expected_algorithm)
def test_request_checksum_algorithm_model_new_crc32(self):
request = self._build_request(b"")
operation_model = self._make_operation_model(
http_checksum={"requestChecksumRequired": True}
)
params = {}
resolve_request_checksum_algorithm(request, operation_model, params)
actual_algorithm = request["context"]["checksum"]["request_algorithm"]
expected_algorithm = {
"algorithm": "crc32",
"in": "header",
"name": "x-amz-checksum-crc32",
}
self.assertEqual(actual_algorithm, expected_algorithm)
def test_apply_request_checksum_handles_no_checksum_context(self):
request = self._build_request(b"")
apply_request_checksum(request)
# Build another request and assert the original request is the same
expected_request = self._build_request(b"")
self.assertEqual(request["headers"], expected_request["headers"])
self.assertEqual(request["body"], expected_request["body"])
self.assertEqual(request["url"], expected_request["url"])
def test_apply_request_checksum_handles_invalid_context(self):
request = self._build_request(b"")
request["context"]["checksum"] = {
"request_algorithm": {
"in": "http-trailer",
"algorithm": "crc32",
"name": "x-amz-checksum-crc32",
}
}
with self.assertRaises(FlexibleChecksumError):
apply_request_checksum(request)
def test_apply_request_checksum_flex_header_bytes(self):
request = self._build_request(b"")
request["context"]["checksum"] = {
"request_algorithm": {
"in": "header",
"algorithm": "crc32",
"name": "x-amz-checksum-crc32",
}
}
apply_request_checksum(request)
self.assertIn("x-amz-checksum-crc32", request["headers"])
def test_apply_request_checksum_flex_header_readable(self):
request = self._build_request(BytesIO(b""))
request["context"]["checksum"] = {
"request_algorithm": {
"in": "header",
"algorithm": "crc32",
"name": "x-amz-checksum-crc32",
}
}
apply_request_checksum(request)
self.assertIn("x-amz-checksum-crc32", request["headers"])
def test_apply_request_checksum_flex_header_explicit_digest(self):
request = self._build_request(b"")
request["context"]["checksum"] = {
"request_algorithm": {
"in": "header",
"algorithm": "crc32",
"name": "x-amz-checksum-crc32",
}
}
request["headers"]["x-amz-checksum-crc32"] = "foo"
apply_request_checksum(request)
# The checksum should not have been modified
self.assertEqual(request["headers"]["x-amz-checksum-crc32"], "foo")
def test_apply_request_checksum_flex_trailer_bytes(self):
request = self._build_request(b"")
request["context"]["checksum"] = {
"request_algorithm": {
"in": "trailer",
"algorithm": "crc32",
"name": "x-amz-checksum-crc32",
}
}
apply_request_checksum(request)
self.assertNotIn("x-amz-checksum-crc32", request["headers"])
self.assertIsInstance(request["body"], AwsChunkedWrapper)
def test_apply_request_checksum_flex_trailer_readable(self):
request = self._build_request(BytesIO(b""))
request["context"]["checksum"] = {
"request_algorithm": {
"in": "trailer",
"algorithm": "crc32",
"name": "x-amz-checksum-crc32",
}
}
apply_request_checksum(request)
self.assertNotIn("x-amz-checksum-crc32", request["headers"])
self.assertIsInstance(request["body"], AwsChunkedWrapper)
def test_apply_request_checksum_flex_header_trailer_explicit_digest(self):
request = self._build_request(b"")
request["context"]["checksum"] = {
"request_algorithm": {
"in": "trailer",
"algorithm": "crc32",
"name": "x-amz-checksum-crc32",
}
}
request["headers"]["x-amz-checksum-crc32"] = "foo"
apply_request_checksum(request)
# The checksum should not have been modified
self.assertEqual(request["headers"]["x-amz-checksum-crc32"], "foo")
# The body should not have been wrapped
self.assertIsInstance(request["body"], bytes)
def test_apply_request_checksum_content_encoding_preset(self):
request = self._build_request(b"")
request["context"]["checksum"] = {
"request_algorithm": {
"in": "trailer",
"algorithm": "crc32",
"name": "x-amz-checksum-crc32",
}
}
request["headers"]["Content-Encoding"] = "foo"
apply_request_checksum(request)
# The content encoding should only have been appended
self.assertEqual(
request["headers"]["Content-Encoding"], "foo,aws-chunked"
)
def test_apply_request_checksum_content_encoding_default(self):
request = self._build_request(b"")
request["context"]["checksum"] = {
"request_algorithm": {
"in": "trailer",
"algorithm": "crc32",
"name": "x-amz-checksum-crc32",
}
}
apply_request_checksum(request)
self.assertEqual(request["headers"]["Content-Encoding"], "aws-chunked")
def test_apply_request_checksum_extra_headers(self):
request = self._build_request(b"")
request["context"]["checksum"] = {
"request_algorithm": {
"in": "trailer",
"algorithm": "crc32",
"name": "x-amz-checksum-crc32",
},
"request_algorithm_header": {
"name": "foo",
"value": "bar",
},
}
apply_request_checksum(request)
self.assertEqual(request["headers"]["foo"], "bar")
def test_response_checksum_algorithm_no_model(self):
request = self._build_request(b"")
operation_model = self._make_operation_model()
params = {}
resolve_response_checksum_algorithms(request, operation_model, params)
self.assertNotIn("checksum", request["context"])
def test_response_checksum_algorithm_model_default(self):
request = self._build_request(b"")
operation_model = self._make_operation_model(
http_checksum={
"responseAlgorithms": ["crc32", "sha1", "sha256"],
"requestValidationModeMember": "ChecksumMode",
}
)
# Param is not present, no algorithms will be set
params = {}
resolve_response_checksum_algorithms(request, operation_model, params)
self.assertNotIn("checksum", request["context"])
# Param is present, algorithms will be set
params = {"ChecksumMode": "enabled"}
resolve_response_checksum_algorithms(
request,
operation_model,
params,
supported_algorithms=["sha1", "sha256"],
)
# CRC32 should have been filtered it out as it was not supported
expected_algorithms = ["sha1", "sha256"]
actual_algorithms = request["context"]["checksum"][
"response_algorithms"
]
self.assertEqual(actual_algorithms, expected_algorithms)
def test_handle_checksum_body_checksum(self):
context = {"checksum": {"response_algorithms": ["sha1", "crc32"]}}
headers = {"x-amz-checksum-crc32": "DUoRhQ=="}
http_response, response_dict = self._make_http_response(
b"hello world",
headers=headers,
context=context,
)
operation_model = self._make_operation_model()
handle_checksum_body(
http_response,
response_dict,
context,
operation_model,
)
body = response_dict["body"]
self.assertEqual(body, b"hello world")
algorithm = response_dict["context"]["checksum"]["response_algorithm"]
self.assertEqual(algorithm, "crc32")
headers = {"x-amz-checksum-crc32": "WrOonG=="}
http_response, response_dict = self._make_http_response(
b"hello world",
headers=headers,
context=context,
)
with self.assertRaises(FlexibleChecksumError):
handle_checksum_body(
http_response,
response_dict,
context,
operation_model,
)
# This header should not be checked, we won't calculate a checksum
# but a proper body should still come out at the end
headers = {"x-amz-checksum-foo": "FOO=="}
http_response, response_dict = self._make_http_response(
b"hello world",
headers=headers,
context=context,
)
handle_checksum_body(
http_response,
response_dict,
context,
operation_model,
)
body = response_dict["body"]
self.assertEqual(body, b"hello world")
algorithm = response_dict["context"]["checksum"]["response_algorithm"]
self.assertEqual(algorithm, "crc32")
def test_handle_checksum_body_checksum_streaming(self):
context = {"checksum": {"response_algorithms": ["sha1", "crc32"]}}
headers = {"x-amz-checksum-crc32": "DUoRhQ=="}
http_response, response_dict = self._make_http_response(
b"hello world",
headers=headers,
context=context,
streaming=True,
)
operation_model = self._make_operation_model(streaming_output=True)
handle_checksum_body(
http_response,
response_dict,
context,
operation_model,
)
body = response_dict["body"]
self.assertEqual(body.read(), b"hello world")
algorithm = response_dict["context"]["checksum"]["response_algorithm"]
self.assertEqual(algorithm, "crc32")
headers = {"x-amz-checksum-crc32": "WrOonG=="}
http_response, response_dict = self._make_http_response(
b"hello world",
headers=headers,
context=context,
streaming=True,
)
handle_checksum_body(
http_response,
response_dict,
context,
operation_model,
)
body = response_dict["body"]
with self.assertRaises(FlexibleChecksumError):
body.read()
# This header should not be checked, we won't calculate a checksum
# but a proper body should still come out at the end
headers = {"x-amz-checksum-foo": "FOOO=="}
http_response, response_dict = self._make_http_response(
b"hello world",
headers=headers,
context=context,
streaming=True,
)
handle_checksum_body(
http_response,
response_dict,
context,
operation_model,
)
body = response_dict["body"]
self.assertEqual(body.read(), b"hello world")
algorithm = response_dict["context"]["checksum"]["response_algorithm"]
self.assertEqual(algorithm, "crc32")
def test_handle_checksum_body_checksum_skip_non_streaming(self):
context = {"checksum": {"response_algorithms": ["sha1", "crc32"]}}
# S3 will return checksums over the checksums of parts which are a
# special case that end with -#. These cannot be validated and are
# instead skipped
headers = {"x-amz-checksum-crc32": "FOOO==-123"}
http_response, response_dict = self._make_http_response(
b"hello world",
headers=headers,
context=context,
)
operation_model = self._make_operation_model()
handle_checksum_body(
http_response,
response_dict,
context,
operation_model,
)
body = response_dict["body"]
self.assertEqual(body, b"hello world")
def test_handle_checksum_body_checksum_skip_streaming(self):
context = {"checksum": {"response_algorithms": ["sha1", "crc32"]}}
# S3 will return checksums over the checksums of parts which are a
# special case that end with -#. These cannot be validated and are
# instead skipped
headers = {"x-amz-checksum-crc32": "FOOO==-123"}
http_response, response_dict = self._make_http_response(
b"hello world",
headers=headers,
context=context,
streaming=True,
)
operation_model = self._make_operation_model(streaming_output=True)
handle_checksum_body(
http_response,
response_dict,
context,
operation_model,
)
body = response_dict["body"]
self.assertEqual(body.read(), b"hello world")
class TestAwsChunkedWrapper(unittest.TestCase):
def test_single_chunk_body(self):
# Test a small body that fits in a single chunk
bytes = BytesIO(b"abcdefghijklmnopqrstuvwxyz")
wrapper = AwsChunkedWrapper(bytes)
body = wrapper.read()
expected = b"1a\r\n" b"abcdefghijklmnopqrstuvwxyz\r\n" b"0\r\n\r\n"
self.assertEqual(body, expected)
def test_multi_chunk_body(self):
# Test a body that requires multiple chunks
bytes = BytesIO(b"abcdefghijklmnopqrstuvwxyz")
wrapper = AwsChunkedWrapper(bytes, chunk_size=10)
body = wrapper.read()
expected = (
b"a\r\n"
b"abcdefghij\r\n"
b"a\r\n"
b"klmnopqrst\r\n"
b"6\r\n"
b"uvwxyz\r\n"
b"0\r\n\r\n"
)
self.assertEqual(body, expected)
def test_read_returns_less_data(self):
class OneLessBytesIO(BytesIO):
def read(self, size=-1):
# Return 1 less byte than was asked for
return super().read(size - 1)
bytes = OneLessBytesIO(b"abcdefghijklmnopqrstuvwxyz")
wrapper = AwsChunkedWrapper(bytes, chunk_size=10)
body = wrapper.read()
# NOTE: This particular body is not important, but it is important that
# the actual size of the chunk matches the length sent which may not
# always be the configured chunk_size if the read does not return that
# much data.
expected = (
b"9\r\n"
b"abcdefghi\r\n"
b"9\r\n"
b"jklmnopqr\r\n"
b"8\r\n"
b"stuvwxyz\r\n"
b"0\r\n\r\n"
)
self.assertEqual(body, expected)
def test_single_chunk_body_with_checksum(self):
wrapper = AwsChunkedWrapper(
BytesIO(b"hello world"),
checksum_cls=Crc32Checksum,
checksum_name="checksum",
)
body = wrapper.read()
expected = (
b"b\r\n" b"hello world\r\n" b"0\r\n" b"checksum:DUoRhQ==\r\n\r\n"
)
self.assertEqual(body, expected)
def test_multi_chunk_body_with_checksum(self):
wrapper = AwsChunkedWrapper(
BytesIO(b"hello world"),
chunk_size=5,
checksum_cls=Crc32Checksum,
checksum_name="checksum",
)
body = wrapper.read()
expected = (
b"5\r\n"
b"hello\r\n"
b"5\r\n"
b" worl\r\n"
b"1\r\n"
b"d\r\n"
b"0\r\n"
b"checksum:DUoRhQ==\r\n\r\n"
)
self.assertEqual(body, expected)
def test_multi_chunk_body_with_checksum_iter(self):
wrapper = AwsChunkedWrapper(
BytesIO(b"hello world"),
chunk_size=5,
checksum_cls=Crc32Checksum,
checksum_name="checksum",
)
expected_chunks = [
b"5\r\nhello\r\n",
b"5\r\n worl\r\n",
b"1\r\nd\r\n",
b"0\r\nchecksum:DUoRhQ==\r\n\r\n",
]
self.assertListEqual(expected_chunks, list(wrapper))
def test_wrapper_can_be_reset(self):
wrapper = AwsChunkedWrapper(
BytesIO(b"hello world"),
chunk_size=5,
checksum_cls=Crc32Checksum,
checksum_name="checksum",
)
first_read = wrapper.read()
self.assertEqual(b"", wrapper.read())
wrapper.seek(0)
second_read = wrapper.read()
self.assertEqual(first_read, second_read)
self.assertIn(b"checksum:DUoRhQ==", first_read)
def test_wrapper_can_only_seek_to_start(self):
wrapper = AwsChunkedWrapper(BytesIO())
with self.assertRaises(AwsChunkedWrapperError):
wrapper.seek(1)
with self.assertRaises(AwsChunkedWrapperError):
wrapper.seek(0, whence=1)
with self.assertRaises(AwsChunkedWrapperError):
wrapper.seek(1, whence=2)
class TestChecksumImplementations(unittest.TestCase):
def assert_base64_checksum(self, checksum_cls, expected_digest):
checksum = checksum_cls()
checksum.update(b"hello world")
actual_digest = checksum.b64digest()
self.assertEqual(actual_digest, expected_digest)
def test_crc32(self):
self.assert_base64_checksum(Crc32Checksum, "DUoRhQ==")
def test_sha1(self):
self.assert_base64_checksum(
Sha1Checksum,
"Kq5sNclPz7QV2+lfQIuc6R7oRu0=",
)
def test_sha256(self):
self.assert_base64_checksum(
Sha256Checksum,
"uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=",
)
@requires_crt()
def test_crt_crc32(self):
self.assert_base64_checksum(CrtCrc32Checksum, "DUoRhQ==")
@requires_crt()
def test_crt_crc32c(self):
self.assert_base64_checksum(CrtCrc32cChecksum, "yZRlqg==")
@requires_crt()
def test_crt_crc64nvme(self):
self.assert_base64_checksum(CrtCrc64NvmeChecksum, "jSnVw/bqjr4=")
class TestCrtChecksumOverrides(unittest.TestCase):
@requires_crt()
def test_crt_crc32_available(self):
actual_cls = get_checksum_cls("crc32")
self.assertEqual(actual_cls, CrtCrc32Checksum)
@requires_crt()
def test_crt_crc32c_available(self):
actual_cls = get_checksum_cls("crc32c")
self.assertEqual(actual_cls, CrtCrc32cChecksum)
@requires_crt()
def test_crt_crc64nvme_available(self):
actual_cls = get_checksum_cls("crc64nvme")
self.assertEqual(actual_cls, CrtCrc64NvmeChecksum)
class TestStreamingChecksumBody(unittest.TestCase):
def setUp(self):
self.raw_bytes = b"hello world"
self.fake_body = BytesIO(self.raw_bytes)
self._make_wrapper("DUoRhQ==")
def _make_wrapper(self, checksum):
self.wrapper = StreamingChecksumBody(
self.fake_body,
None,
Crc32Checksum(),
checksum,
)
def test_basic_read_good(self):
actual = self.wrapper.read()
self.assertEqual(actual, self.raw_bytes)
def test_many_reads_good(self):
actual = b""
actual += self.wrapper.read(5)
actual += self.wrapper.read(5)
actual += self.wrapper.read(1)
self.assertEqual(actual, self.raw_bytes)
def test_basic_read_bad(self):
self._make_wrapper("duorhq==")
with self.assertRaises(FlexibleChecksumError):
self.wrapper.read()
def test_many_reads_bad(self):
self._make_wrapper("duorhq==")
self.wrapper.read(5)
self.wrapper.read(6)
# Whole body has been read, next read signals the end of the stream and
# validates the checksum of the body contents read
with self.assertRaises(FlexibleChecksumError):
self.wrapper.read(1)
def test_handles_variable_padding(self):
# This digest is equivalent but with more padding
self._make_wrapper("DUoRhQ=====")
actual = self.wrapper.read()
self.assertEqual(actual, self.raw_bytes)
def test_iter_raises_error(self):
self._make_wrapper("duorhq==")
with self.assertRaises(FlexibleChecksumError):
for chunk in self.wrapper:
pass
|