Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import logging
from typing import IO, Any, Dict, List

from google_crc32c import Checksum
import google_crc32c

from google.cloud import _storage_v2 as storage_v2
from google.cloud.storage.asyncio.retry._helpers import (
Expand Down Expand Up @@ -127,7 +127,7 @@ def update_state_from_response(

if checksummed_data.HasField("crc32c"):
server_checksum = checksummed_data.crc32c
client_checksum = int.from_bytes(Checksum(data).digest(), "big")
client_checksum = google_crc32c.value(data)
if server_checksum != client_checksum:
raise DataCorruption(
response,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,10 @@ def test_init_raises_if_crc32c_c_extension_is_missing(self, mock_google_crc32c):
)

@pytest.mark.asyncio
@mock.patch("google.cloud.storage.asyncio.retry.reads_resumption_strategy.Checksum")
async def test_download_ranges_raises_on_checksum_mismatch(
self, mock_checksum_class
):
@mock.patch(
"google.cloud.storage.asyncio.retry.reads_resumption_strategy.google_crc32c.value"
)
async def test_download_ranges_raises_on_checksum_mismatch(self, mock_crc32c_value):
from google.cloud.storage.asyncio._stream_multiplexer import _StreamMultiplexer
from google.cloud.storage.asyncio.async_multi_range_downloader import (
AsyncMultiRangeDownloader,
Expand All @@ -340,8 +340,7 @@ async def test_download_ranges_raises_on_checksum_mismatch(

test_data = b"some-data"
server_checksum = 12345
mock_checksum_instance = mock_checksum_class.return_value
mock_checksum_instance.digest.return_value = (54321).to_bytes(4, "big")
mock_crc32c_value.return_value = 54321

mock_response = _storage_v2.BidiReadObjectResponse(
object_data_ranges=[
Expand Down Expand Up @@ -372,7 +371,7 @@ async def test_download_ranges_raises_on_checksum_mismatch(
await mrd.download_ranges([(0, len(test_data), BytesIO())])

assert "Checksum mismatch" in str(exc_info.value)
mock_checksum_class.assert_called_once_with(test_data)
mock_crc32c_value.assert_called_once_with(test_data)

@mock.patch(
"google.cloud.storage.asyncio.async_multi_range_downloader.AsyncMultiRangeDownloader.open",
Expand Down Expand Up @@ -575,18 +574,11 @@ async def staged_recv():
# Act
buffer = BytesIO()

# Patch Checksum where it is likely used (reads_resumption_strategy or similar),
# but actually if we use google_crc32c directly, we should patch that or provide valid CRC.
# Since we can't reliably predict where Checksum is imported/used without more digging,
# let's provide a valid CRC for b"data".
# Checksum(b"data").digest() -> needs to match crc32c=123.
# But we can't force b"data" to have crc=123.
# So we MUST patch Checksum.
# It is used in google.cloud.storage.asyncio.retry.reads_resumption_strategy
# Patch google_crc32c.value where it is used in reads_resumption_strategy
with mock.patch(
"google.cloud.storage.asyncio.retry.reads_resumption_strategy.Checksum"
) as mock_chk:
mock_chk.return_value.digest.return_value = (123).to_bytes(4, "big")
"google.cloud.storage.asyncio.retry.reads_resumption_strategy.google_crc32c.value"
) as mock_crc_value:
mock_crc_value.return_value = 123
await mock_mrd.download_ranges([(0, 4, buffer)])

# Assert
Expand Down
Loading