Skip to content

Commit 6d39f62

Browse files
committed
add unit tests
1 parent 483e430 commit 6d39f62

4 files changed

Lines changed: 60 additions & 12 deletions

File tree

google/cloud/storage/internal/grpc/object_read_source.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ StatusOr<storage::internal::ReadSourceResult> GrpcObjectReadSource::Read(
8080
return result;
8181
}
8282
auto handle_status = HandleResponse(result, buf, n, std::move(response));
83-
if (!handle_status.ok()) return handle_status;
83+
if (!handle_status.ok()) {
84+
status_ = handle_status;
85+
stream_.reset();
86+
return status_;
87+
}
8488
}
8589

8690
return result;
@@ -96,8 +100,7 @@ Status GrpcObjectReadSource::HandleResponse(
96100
// empty.
97101
if (response.has_checksummed_data()) {
98102
auto const& data = response.checksummed_data();
99-
auto status = hash_function_->Update(offset_.value_or(0),
100-
GetContent(data),
103+
auto status = hash_function_->Update(offset_.value_or(0), GetContent(data),
101104
data.crc32c());
102105
if (!status.ok()) return status;
103106

google/cloud/storage/internal/grpc/object_read_source.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_INTERNAL_GRPC_OBJECT_READ_SOURCE_H
1717

1818
#include "google/cloud/storage/internal/grpc/buffer_read_object_data.h"
19+
#include "google/cloud/storage/internal/hash_function.h"
1920
#include "google/cloud/storage/internal/object_read_source.h"
2021
#include "google/cloud/storage/version.h"
2122
#include "google/cloud/future.h"
2223
#include "google/cloud/internal/streaming_read_rpc.h"
2324
#include "absl/functional/function_ref.h"
24-
#include "google/cloud/storage/internal/hash_function.h"
25-
#include "google/storage/v2/storage.pb.h"
2625
#include "absl/types/optional.h"
26+
#include "google/storage/v2/storage.pb.h"
2727
#include <functional>
2828
#include <string>
2929

google/cloud/storage/internal/grpc/object_read_source_test.cc

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
#include "google/cloud/storage/hashing_options.h"
1717
#include "google/cloud/storage/internal/grpc/ctype_cord_workaround.h"
1818
#include "google/cloud/storage/internal/grpc/object_metadata_parser.h"
19+
#include "google/cloud/storage/internal/hash_function_impl.h"
1920
#include "google/cloud/storage/testing/mock_storage_stub.h"
2021
#include "google/cloud/testing_util/status_matchers.h"
21-
#include "google/cloud/storage/internal/hash_function_impl.h"
2222
#include <google/protobuf/text_format.h>
2323
#include <google/protobuf/util/message_differencer.h>
2424
#include <gmock/gmock.h>
@@ -359,17 +359,62 @@ TEST(GrpcObjectReadSource, ChecksumMismatch) {
359359
EXPECT_CALL(*mock, Read)
360360
.WillOnce([&contents](storage_proto::ReadObjectResponse* r) {
361361
SetContent(*r, contents);
362-
r->mutable_checksummed_data()->set_crc32c(123); // Wrong CRC
362+
r->mutable_checksummed_data()->set_crc32c(123); // Wrong CRC
363363
return absl::nullopt;
364364
});
365365

366-
auto hash_function = std::make_shared<storage::internal::Crc32cMessageHashFunction>(
367-
storage::internal::CreateNullHashFunction());
366+
auto hash_function =
367+
std::make_shared<storage::internal::Crc32cMessageHashFunction>(
368+
storage::internal::CreateNullHashFunction());
369+
370+
GrpcObjectReadSource tested(MakeSimpleTimerSource(), std::move(mock),
371+
std::move(hash_function));
372+
std::vector<char> buffer(1024);
373+
auto response = tested.Read(buffer.data(), buffer.size());
374+
EXPECT_THAT(response, StatusIs(StatusCode::kInvalidArgument,
375+
HasSubstr("mismatched crc32c checksum")));
376+
EXPECT_THAT(tested.Close(),
377+
StatusIs(StatusCode::kInvalidArgument,
378+
HasSubstr("mismatched crc32c checksum")));
379+
}
380+
381+
TEST(GrpcObjectReadSource, ChecksumWithNonZeroOffset) {
382+
auto mock = std::make_unique<MockObjectMediaStream>();
383+
std::string const contents1 = "0123456789";
384+
std::string const contents2 = "abcdefghij";
385+
auto const expected_crc32c_1 = storage::ComputeCrc32cChecksum(contents1);
386+
auto const expected_crc32c_2 = storage::ComputeCrc32cChecksum(contents2);
387+
388+
::testing::InSequence sequence;
389+
EXPECT_CALL(*mock, Read)
390+
.WillOnce([&contents1,
391+
expected_crc32c_1](storage_proto::ReadObjectResponse* r) {
392+
SetContent(*r, contents1);
393+
r->mutable_content_range()->set_start(100);
394+
r->mutable_checksummed_data()->set_crc32c(
395+
storage_internal::Crc32cToProto(expected_crc32c_1).value());
396+
return absl::nullopt;
397+
})
398+
.WillOnce([&contents2,
399+
expected_crc32c_2](storage_proto::ReadObjectResponse* r) {
400+
SetContent(*r, contents2);
401+
r->mutable_checksummed_data()->set_crc32c(
402+
storage_internal::Crc32cToProto(expected_crc32c_2).value());
403+
return absl::nullopt;
404+
})
405+
.WillOnce(Return(Status{}));
406+
EXPECT_CALL(*mock, GetRequestMetadata).WillOnce(Return(RpcMetadata{}));
368407

369-
GrpcObjectReadSource tested(MakeSimpleTimerSource(), std::move(mock), std::move(hash_function));
408+
auto hash_function =
409+
std::make_shared<storage::internal::Crc32cMessageHashFunction>(
410+
storage::internal::CreateNullHashFunction());
411+
412+
GrpcObjectReadSource tested(MakeSimpleTimerSource(), std::move(mock),
413+
std::move(hash_function));
370414
std::vector<char> buffer(1024);
371415
auto response = tested.Read(buffer.data(), buffer.size());
372-
EXPECT_THAT(response, StatusIs(StatusCode::kInvalidArgument, HasSubstr("mismatched crc32c checksum")));
416+
ASSERT_STATUS_OK(response);
417+
EXPECT_EQ(contents1.size() + contents2.size(), response->bytes_received);
373418
}
374419

375420
} // namespace

google/cloud/storage/internal/grpc/stub.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
#include "google/cloud/storage/internal/grpc/sign_blob_request_parser.h"
3030
#include "google/cloud/storage/internal/grpc/split_write_object_data.h"
3131
#include "google/cloud/storage/internal/grpc/synthetic_self_link.h"
32+
#include "google/cloud/storage/internal/hash_function_impl.h"
3233
#include "google/cloud/storage/internal/storage_stub_factory.h"
3334
#include "google/cloud/storage/options.h"
34-
#include "google/cloud/storage/internal/hash_function_impl.h"
3535
#include "google/cloud/internal/big_endian.h"
3636
#include "google/cloud/internal/invoke_result.h"
3737
#include "google/cloud/internal/make_status.h"

0 commit comments

Comments
 (0)