Skip to content

storage: do not fail transfers where validation is skipped #892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
10 changes: 8 additions & 2 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,10 @@ File.prototype.createReadStream = function(options) {
hashes[hashType] = hash.substr(hash.indexOf('=') + 1);
});

var failed = true;
// If we're doing validation, assume the worst-- a data integrity
// mismatch. If not, these tests won't be performed, and we can assume the
// best.
var failed = crc32c || md5;

if (crc32c && hashes.crc32c) {
// We must remove the first four bytes from the returned checksum.
Expand Down Expand Up @@ -772,7 +775,10 @@ File.prototype.createWriteStream = function(options) {
// Compare our hashed version vs the completed upload's version.
fileWriteStream.on('complete', function() {
var metadata = self.metadata;
var failed = true;

// If we're doing validation, assume the worst-- a data integrity mismatch.
// If not, these tests won't be performed, and we can assume the best.
var failed = crc32c || md5;

if (crc32c && metadata.crc32c) {
// We must remove the first four bytes from the returned checksum.
Expand Down
28 changes: 28 additions & 0 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,17 @@ describe('File', function() {
.resume();
});

it('should ignore a data mismatch if validation: false', function(done) {
requestOverride = getFakeSuccessfulRequest(data, {
headers: { 'x-goog-hash': 'md5=fakefakefake' }
});

file.createReadStream({ validation: false })
.resume()
.on('error', done)
.on('end', done);
});

describe('destroying the through stream', function() {
it('should destroy after failed validation', function(done) {
requestOverride = getFakeSuccessfulRequest(
Expand Down Expand Up @@ -1147,6 +1158,23 @@ describe('File', function() {
});
});

it('should ignore a data mismatch if validation: false', function(done) {
var writable = file.createWriteStream({ validation: false });

file.startResumableUpload_ = function(stream) {
setImmediate(function() {
file.metadata = { md5Hash: 'bad-hash' };
stream.emit('complete');
});
};

writable.write(data);
writable.end();

writable.on('error', done);
writable.on('finish', done);
});

it('should delete the file if validation fails', function(done) {
var writable = file.createWriteStream();

Expand Down