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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ google-cloud-language-v2 = { version = "0.4.4", path = "src/generated/cloud
google-cloud-speech-v2 = { version = "0.4.4", path = "src/generated/cloud/speech/v2" }
google-cloud-secretmanager-v1 = { version = "0.4.4", path = "src/generated/cloud/secretmanager/v1" }
google-cloud-aiplatform-v1 = { version = "0.4.4", default-features = false, path = "src/generated/cloud/aiplatform/v1" }
google-cloud-storage = { version = "0.25.0-preview4", path = "src/storage" }
google-cloud-storage = { version = "0.25.0-preview6", path = "src/storage" }

# Local test package
integration-tests = { path = "src/integration-tests" }
2 changes: 1 addition & 1 deletion src/storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

[package]
name = "google-cloud-storage"
version = "0.25.0-preview5"
version = "0.25.0-preview6"
description = "Google Cloud Client Libraries for Rust - Storage"
# Inherit other attributes from the workspace.
edition.workspace = true
Expand Down
9 changes: 6 additions & 3 deletions src/storage/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! The storage client defines additional error types. These are often returned
//! as the `source()` of an [Error][crate::Error].

use crate::model::ObjectChecksums;
use crate::model::{Object, ObjectChecksums};

#[derive(Clone, Debug)]
#[non_exhaustive]
Expand Down Expand Up @@ -210,8 +210,11 @@ pub enum UploadError {
/// hosting the service.
///
/// If possible, resend the data from a different machine.
#[error("checksum mismatch {0}")]
ChecksumMismatch(ChecksumMismatch),
#[error("checksum mismatch {mismatch} when uploading {} to {}", object.name, object.bucket)]
ChecksumMismatch {
mismatch: ChecksumMismatch,
object: Box<Object>,
},
}

#[cfg(test)]
Expand Down
16 changes: 12 additions & 4 deletions src/storage/src/storage/perform_upload/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,26 @@ where
}

pub(crate) async fn validate_response_object(&self, object: Object) -> Result<Object> {
let err = |mismatch, o: Object| {
Err(Error::ser(UploadError::ChecksumMismatch {
mismatch,
object: o.into(),
}))
};
if let Some(pre) = self
.spec
.resource
.as_ref()
.and_then(|r| r.checksums.as_ref())
{
self::checksum_validate(pre, &object.checksums)
.map_err(|e| Error::ser(UploadError::ChecksumMismatch(e)))?;
if let Err(mismatch) = self::checksum_validate(pre, &object.checksums) {
return err(mismatch, object);
}
}
let computed = self.payload.lock().await.final_checksum();
self::checksum_validate(&computed, &object.checksums)
.map_err(|e| Error::ser(UploadError::ChecksumMismatch(e)))?;
if let Err(mismatch) = self::checksum_validate(&computed, &object.checksums) {
return err(mismatch, object);
}
Ok(object)
}
}
Expand Down
47 changes: 47 additions & 0 deletions src/storage/src/storage/perform_upload/tests/checksums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
//! Verify the client library correctly detects mismatched checksums on uploads.

use super::*;
use crate::error::UploadError;
use crate::storage::upload_source::BytesSource;
use httptest::{Expectation, Server, matchers::*, responders::*};
use serde_json::{Value, json};
use std::error::Error as StdError;

const VEXING: &str = "how vexingly quick daft zebras jump";

Expand All @@ -40,6 +42,11 @@ mod buffered_single_shot {
.await
.expect_err("expected a checksum error");
assert!(err.is_serialization(), "{err:?}");
let source = err.source().and_then(|e| e.downcast_ref::<UploadError>());
assert!(
matches!(source, Some(UploadError::ChecksumMismatch { object, .. }) if object.as_ref() == &bad_checksums_object()),
"{err:?}"
);
Ok(())
}

Expand All @@ -62,6 +69,11 @@ mod buffered_single_shot {
.await
.expect_err("expected a checksum error");
assert!(err.is_serialization(), "{err:?}");
let source = err.source().and_then(|e| e.downcast_ref::<UploadError>());
assert!(
matches!(source, Some(UploadError::ChecksumMismatch { object, .. }) if object.as_ref() == &bad_checksums_object()),
"{err:?}"
);
Ok(())
}

Expand Down Expand Up @@ -98,6 +110,11 @@ mod buffered_resumable {
.await
.expect_err("expected a checksum error");
assert!(err.is_serialization(), "{err:?}");
let source = err.source().and_then(|e| e.downcast_ref::<UploadError>());
assert!(
matches!(source, Some(UploadError::ChecksumMismatch { object, .. }) if object.as_ref() == &bad_checksums_object()),
"{err:?}"
);
Ok(())
}

Expand All @@ -120,6 +137,11 @@ mod buffered_resumable {
.await
.expect_err("expected a checksum error");
assert!(err.is_serialization(), "{err:?}");
let source = err.source().and_then(|e| e.downcast_ref::<UploadError>());
assert!(
matches!(source, Some(UploadError::ChecksumMismatch { object, .. }) if object.as_ref() == &bad_checksums_object()),
"{err:?}"
);
Ok(())
}

Expand Down Expand Up @@ -156,6 +178,11 @@ mod unbuffered_single_shot {
.await
.expect_err("expected a checksum error");
assert!(err.is_serialization(), "{err:?}");
let source = err.source().and_then(|e| e.downcast_ref::<UploadError>());
assert!(
matches!(source, Some(UploadError::ChecksumMismatch { object, .. }) if object.as_ref() == &bad_checksums_object()),
"{err:?}"
);
Ok(())
}

Expand All @@ -178,6 +205,11 @@ mod unbuffered_single_shot {
.await
.expect_err("expected a checksum error");
assert!(err.is_serialization(), "{err:?}");
let source = err.source().and_then(|e| e.downcast_ref::<UploadError>());
assert!(
matches!(source, Some(UploadError::ChecksumMismatch { object, .. }) if object.as_ref() == &bad_checksums_object()),
"{err:?}"
);
Ok(())
}

Expand Down Expand Up @@ -214,6 +246,11 @@ mod unbuffered_resumable {
.await
.expect_err("expected a checksum error");
assert!(err.is_serialization(), "{err:?}");
let source = err.source().and_then(|e| e.downcast_ref::<UploadError>());
assert!(
matches!(source, Some(UploadError::ChecksumMismatch { object, .. }) if object.as_ref() == &bad_checksums_object()),
"{err:?}"
);
Ok(())
}

Expand All @@ -236,6 +273,11 @@ mod unbuffered_resumable {
.await
.expect_err("expected a checksum error");
assert!(err.is_serialization(), "{err:?}");
let source = err.source().and_then(|e| e.downcast_ref::<UploadError>());
assert!(
matches!(source, Some(UploadError::ChecksumMismatch { object, .. }) if object.as_ref() == &bad_checksums_object()),
"{err:?}"
);
Ok(())
}

Expand Down Expand Up @@ -339,6 +381,11 @@ fn bad_checksums_body() -> Value {
})
}

fn bad_checksums_object() -> Object {
let v1 = serde_json::from_value::<v1::Object>(bad_checksums_body()).unwrap();
Object::from(v1)
}

fn good_checksums_body() -> Value {
// The magic strings can be regenerated using:
// echo -n 'how vexingly quick daft zebras jump' > vexing.txt
Expand Down
Loading