From c3cf7bd4a66e8d316e811ebc27406d3277dbc009 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Thu, 24 Sep 2015 12:43:00 -0400 Subject: [PATCH] storage: do not fail transfers where validation is skipped --- lib/storage/file.js | 10 ++++++++-- test/storage/file.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/storage/file.js b/lib/storage/file.js index f8a748827b8..1870da0d62d 100644 --- a/lib/storage/file.js +++ b/lib/storage/file.js @@ -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. @@ -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. diff --git a/test/storage/file.js b/test/storage/file.js index 62364836c02..a53e81acbf6 100644 --- a/test/storage/file.js +++ b/test/storage/file.js @@ -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( @@ -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();