-
Notifications
You must be signed in to change notification settings - Fork 370
/
object_checksum_integration_test.cc
373 lines (333 loc) · 14.4 KB
/
object_checksum_integration_test.cc
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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License 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.
#include "google/cloud/storage/client.h"
#include "google/cloud/storage/testing/storage_integration_test.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/testing_util/scoped_log.h"
#include "google/cloud/testing_util/status_matchers.h"
#include <gmock/gmock.h>
namespace google {
namespace cloud {
namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {
using ::google::cloud::testing_util::IsOk;
using ::google::cloud::testing_util::StatusIs;
using ::testing::HasSubstr;
using ::testing::Not;
class ObjectChecksumIntegrationTest
: public google::cloud::storage::testing::StorageIntegrationTest,
public ::testing::WithParamInterface<std::string> {
protected:
void SetUp() override {
bucket_name_ = google::cloud::internal::GetEnv(
"GOOGLE_CLOUD_CPP_STORAGE_TEST_BUCKET_NAME")
.value_or("");
ASSERT_FALSE(bucket_name_.empty());
}
std::string bucket_name_;
};
/// @test Verify that CRC32C checksums are enabled by default.
TEST_P(ObjectChecksumIntegrationTest, InsertObjectDefault) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
auto meta = client->InsertObject(bucket_name_, object_name, LoremIpsum(),
DisableMD5Hash(true), IfGenerationMatch(0));
ASSERT_STATUS_OK(meta);
ScheduleForDelete(*meta);
if (meta->has_metadata("x_emulator_upload")) {
ASSERT_TRUE(meta->has_metadata("x_emulator_crc32c"));
ASSERT_FALSE(meta->has_metadata("x_emulator_md5"));
}
}
/// @test Verify that `DisableCrc32cChecksum(true)` works as expected.
TEST_P(ObjectChecksumIntegrationTest, InsertObjectExplicitDisable) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
auto meta = client->InsertObject(bucket_name_, object_name, LoremIpsum(),
DisableCrc32cChecksum(true),
DisableMD5Hash(true), IfGenerationMatch(0));
ASSERT_STATUS_OK(meta);
ScheduleForDelete(*meta);
if (meta->has_metadata("x_emulator_upload")) {
ASSERT_FALSE(meta->has_metadata("x_emulator_crc32c"));
ASSERT_FALSE(meta->has_metadata("x_emulator_md5"));
}
}
/// @test Verify that `DisableCrc32cChecksum(false)` works as expected.
TEST_P(ObjectChecksumIntegrationTest, InsertObjectExplicitEnable) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
auto meta = client->InsertObject(bucket_name_, object_name, LoremIpsum(),
DisableCrc32cChecksum(false),
DisableMD5Hash(true), IfGenerationMatch(0));
ASSERT_STATUS_OK(meta);
ScheduleForDelete(*meta);
if (meta->has_metadata("x_emulator_upload")) {
ASSERT_TRUE(meta->has_metadata("x_emulator_crc32c"));
ASSERT_FALSE(meta->has_metadata("x_emulator_md5"));
}
}
TEST_P(ObjectChecksumIntegrationTest, InsertObjectWithValueSuccess) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
auto meta = client->InsertObject(
bucket_name_, object_name, LoremIpsum(),
Crc32cChecksumValue(ComputeCrc32cChecksum(LoremIpsum())),
DisableMD5Hash(true), IfGenerationMatch(0));
ASSERT_STATUS_OK(meta);
ScheduleForDelete(*meta);
if (meta->has_metadata("x_emulator_upload")) {
ASSERT_TRUE(meta->has_metadata("x_emulator_crc32c"));
ASSERT_FALSE(meta->has_metadata("x_emulator_md5"));
}
}
TEST_P(ObjectChecksumIntegrationTest, InsertObjectWithValueFailure) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
auto failure = client->InsertObject(
bucket_name_, object_name, LoremIpsum(), DisableMD5Hash(true),
IfGenerationMatch(0), Crc32cChecksumValue(ComputeCrc32cChecksum("")));
EXPECT_THAT(failure, Not(IsOk()));
}
/// @test Verify that CRC32C checksums are computed by default in WriteObject().
TEST_F(ObjectChecksumIntegrationTest, WriteObjectDefault) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
auto os = client->WriteObject(bucket_name_, object_name, DisableMD5Hash(true),
IfGenerationMatch(0));
os << LoremIpsum();
os.Close();
auto meta = os.metadata();
ASSERT_STATUS_OK(meta);
ScheduleForDelete(*meta);
EXPECT_EQ(os.received_hash(), os.computed_hash());
EXPECT_THAT(os.received_hash(),
HasSubstr(ComputeCrc32cChecksum(LoremIpsum())));
EXPECT_THAT(os.computed_hash(),
HasSubstr(ComputeCrc32cChecksum(LoremIpsum())));
if (meta->has_metadata("x_emulator_upload")) {
// Streaming uploads over REST cannot include checksums
EXPECT_TRUE(meta->has_metadata("x_emulator_no_crc32c")) << *meta;
EXPECT_TRUE(meta->has_metadata("x_emulator_no_md5")) << *meta;
}
}
/// @test Verify that CRC32C checksums can be explicitly disabled in
/// WriteObject().
TEST_F(ObjectChecksumIntegrationTest, WriteObjectExplicitDisable) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
auto os = client->WriteObject(bucket_name_, object_name,
DisableCrc32cChecksum(true),
DisableMD5Hash(true), IfGenerationMatch(0));
os << LoremIpsum();
os.Close();
auto meta = os.metadata();
ASSERT_STATUS_OK(meta);
ScheduleForDelete(*meta);
EXPECT_THAT(os.received_hash(),
Not(HasSubstr(ComputeCrc32cChecksum(LoremIpsum()))));
EXPECT_THAT(os.computed_hash(),
Not(HasSubstr(ComputeCrc32cChecksum(LoremIpsum()))));
if (meta->has_metadata("x_emulator_upload")) {
// Streaming uploads over REST cannot include checksums
EXPECT_TRUE(meta->has_metadata("x_emulator_no_crc32c")) << *meta;
EXPECT_TRUE(meta->has_metadata("x_emulator_no_md5")) << *meta;
}
}
/// @test Verify that CRC32C checksums can be explicitly enabled in
/// WriteObject().
TEST_F(ObjectChecksumIntegrationTest, WriteObjectExplicitEnable) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
auto os = client->WriteObject(bucket_name_, object_name,
DisableCrc32cChecksum(false),
DisableMD5Hash(true), IfGenerationMatch(0));
os << LoremIpsum();
os.Close();
auto meta = os.metadata();
ASSERT_STATUS_OK(meta);
ScheduleForDelete(*meta);
EXPECT_THAT(os.received_hash(),
HasSubstr(ComputeCrc32cChecksum(LoremIpsum())));
EXPECT_THAT(os.computed_hash(),
HasSubstr(ComputeCrc32cChecksum(LoremIpsum())));
if (meta->has_metadata("x_emulator_upload")) {
// Streaming uploads over REST cannot include checksums
EXPECT_TRUE(meta->has_metadata("x_emulator_no_crc32c")) << *meta;
EXPECT_TRUE(meta->has_metadata("x_emulator_no_md5")) << *meta;
}
}
/// @test Verify that valid CRC32C checksums values work in WriteObject().
TEST_F(ObjectChecksumIntegrationTest, WriteObjectWithValueSuccess) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
auto os = client->WriteObject(
bucket_name_, object_name,
Crc32cChecksumValue(ComputeCrc32cChecksum(LoremIpsum())),
DisableMD5Hash(true), IfGenerationMatch(0));
os << LoremIpsum();
os.Close();
auto meta = os.metadata();
ASSERT_STATUS_OK(meta);
ScheduleForDelete(*meta);
if (meta->has_metadata("x_emulator_upload")) {
// Streaming uploads over REST cannot include checksums unless provided by
// the application when the upload begins.
EXPECT_TRUE(meta->has_metadata("x_emulator_crc32c")) << *meta;
EXPECT_TRUE(meta->has_metadata("x_emulator_no_md5")) << *meta;
}
}
/// @test Verify that incorrect CRC32C checksums values work in WriteObject().
TEST_F(ObjectChecksumIntegrationTest, WriteObjectWithValueFailure) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
auto os = client->WriteObject(
bucket_name_, object_name, MD5HashValue(ComputeMD5Hash("")),
DisableCrc32cChecksum(true), IfGenerationMatch(0));
os << LoremIpsum();
os.Close();
auto meta = os.metadata();
EXPECT_THAT(meta, Not(IsOk()));
}
/// @test Verify that CRC32C checksum mismatches are reported when the server
/// receives bad data.
TEST_F(ObjectChecksumIntegrationTest, WriteObjectReceiveBadChecksum) {
// This test is disabled when not using the emulator as it relies on the
// emulator to inject faults.
if (!UsingEmulator()) GTEST_SKIP();
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
// Create a stream to upload an object.
ObjectWriteStream stream = client->WriteObject(
bucket_name_, object_name, DisableMD5Hash(true),
CustomHeader("x-goog-emulator-instructions", "inject-upload-data-error"),
IfGenerationMatch(0));
stream << LoremIpsum() << "\n";
stream.Close();
EXPECT_TRUE(stream.bad());
ASSERT_STATUS_OK(stream.metadata());
ScheduleForDelete(*stream.metadata());
EXPECT_NE(stream.received_hash(), stream.computed_hash());
}
/// @test Verify that CRC32C checksum mismatches are reported by default.
TEST_F(ObjectChecksumIntegrationTest, WriteObjectUploadBadChecksum) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
// Create a stream to upload an object.
ObjectWriteStream stream = client->WriteObject(
bucket_name_, object_name, Crc32cChecksumValue(ComputeCrc32cChecksum("")),
DisableMD5Hash(true), IfGenerationMatch(0));
stream << LoremIpsum() << "\n";
stream.Close();
EXPECT_TRUE(stream.bad());
ASSERT_THAT(stream.metadata(), Not(IsOk()));
}
/// @test Verify that CRC32C checksums are computed by default on downloads.
TEST_P(ObjectChecksumIntegrationTest, ReadObjectDefault) {
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
auto meta = client->InsertObject(bucket_name_, object_name, LoremIpsum(),
IfGenerationMatch(0));
ASSERT_STATUS_OK(meta);
ScheduleForDelete(*meta);
auto stream = client->ReadObject(bucket_name_, object_name);
auto const actual = std::string{std::istreambuf_iterator<char>{stream}, {}};
ASSERT_FALSE(stream.IsOpen());
EXPECT_EQ(stream.received_hash(), stream.computed_hash());
EXPECT_THAT(stream.received_hash(), HasSubstr(meta->crc32c()));
}
/// @test Verify that CRC32C checksum mismatches are reported by default on
/// downloads.
TEST_P(ObjectChecksumIntegrationTest, ReadObjectCorruptedByServerGetc) {
// This test is disabled when not using the emulator as it relies on the
// emulator to inject faults.
if (!UsingEmulator()) GTEST_SKIP();
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
StatusOr<ObjectMetadata> meta = client->InsertObject(
bucket_name_, object_name, LoremIpsum(), IfGenerationMatch(0));
ASSERT_STATUS_OK(meta);
ScheduleForDelete(*meta);
auto stream = client->ReadObject(
bucket_name_, object_name, DisableMD5Hash(true),
CustomHeader("x-goog-emulator-instructions", "return-corrupted-data"));
#if GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
EXPECT_THROW(
try {
std::string actual(std::istreambuf_iterator<char>{stream},
std::istreambuf_iterator<char>{});
} catch (HashMismatchError const& ex) {
EXPECT_NE(ex.received_hash(), ex.computed_hash());
EXPECT_THAT(ex.received_hash(), HasSubstr(meta->crc32c()));
EXPECT_THAT(ex.what(), HasSubstr("mismatched hashes"));
throw;
},
HashMismatchError);
#else
std::string actual(std::istreambuf_iterator<char>{stream}, {});
EXPECT_NE(stream.received_hash(), stream.computed_hash());
EXPECT_THAT(stream.received_hash(), HasSubstr(meta->crc32c()));
EXPECT_THAT(stream.status(), Not(IsOk()));
#endif // GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
}
/// @test Verify that CRC32C checksum mismatches are reported by default on
/// downloads.
TEST_P(ObjectChecksumIntegrationTest, ReadObjectCorruptedByServerRead) {
// This test is disabled when not using the emulator as it relies on the
// emulator to inject faults.
if (!UsingEmulator()) GTEST_SKIP();
StatusOr<Client> client = MakeIntegrationTestClient();
ASSERT_STATUS_OK(client);
auto object_name = MakeRandomObjectName();
StatusOr<ObjectMetadata> meta =
client->InsertObject(bucket_name_, object_name, LoremIpsum(),
IfGenerationMatch(0), Projection::Full());
ASSERT_STATUS_OK(meta);
ScheduleForDelete(*meta);
auto stream = client->ReadObject(
bucket_name_, object_name, DisableMD5Hash(true),
CustomHeader("x-goog-emulator-instructions", "return-corrupted-data"));
// Create a buffer large enough to read the full contents.
std::vector<char> buffer(2 * LoremIpsum().size());
stream.read(buffer.data(), static_cast<std::streamsize>(buffer.size()));
#if GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
EXPECT_TRUE(stream.bad());
#endif // GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
EXPECT_THAT(stream.status(), StatusIs(StatusCode::kDataLoss));
EXPECT_NE(stream.received_hash(), stream.computed_hash());
EXPECT_EQ(stream.received_hash(), meta->crc32c());
}
INSTANTIATE_TEST_SUITE_P(ObjectChecksumIntegrationTestJson,
ObjectChecksumIntegrationTest,
::testing::Values("JSON"));
} // anonymous namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage
} // namespace cloud
} // namespace google