From 25b92ba8058e8e9838ac4afff8268c6896782f71 Mon Sep 17 00:00:00 2001 From: Zach Bjornson Date: Mon, 24 Oct 2016 18:24:15 -0700 Subject: [PATCH] storage: improve err msg if MD5 spec'd but absent Ref #1749 --- packages/storage/src/file.js | 16 +++++++++++++- packages/storage/test/file.js | 39 ++++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/packages/storage/src/file.js b/packages/storage/src/file.js index b3cd8642e6f..23a7e1e8397 100644 --- a/packages/storage/src/file.js +++ b/packages/storage/src/file.js @@ -689,7 +689,15 @@ File.prototype.createReadStream = function(options) { failed = !validateStream.test('md5', hashes.md5); } - if (failed) { + if (md5 && !hashes.md5) { + var hashError = new Error([ + 'MD5 verification was specified, but is not available for the', + 'requested object. MD5 is not available for composite objects.' + ].join(' ')); + hashError.code = 'MD5_NOT_AVAILABLE'; + + throughStream.destroy(hashError); + } else if (failed) { var mismatchError = new Error([ 'The downloaded data did not match the data from the server.', 'To be sure the content is the same, you should download the', @@ -1029,6 +1037,12 @@ File.prototype.createWriteStream = function(options) { '\n\nThe delete attempt failed with this message:', '\n\n ' + err.message ].join(' '); + } else if (md5 && !metadata.md5Hash) { + code = 'MD5_NOT_AVAILABLE'; + message = [ + 'MD5 verification was specified, but is not available for the', + 'requested object. MD5 is not available for composite objects.' + ].join(' '); } else { code = 'FILE_NO_UPLOAD'; message = [ diff --git a/packages/storage/test/file.js b/packages/storage/test/file.js index 6bda832e186..fe9f24a0174 100644 --- a/packages/storage/test/file.js +++ b/packages/storage/test/file.js @@ -896,7 +896,7 @@ describe('File', function() { it('should destroy after failed validation', function(done) { file.requestStream = getFakeSuccessfulRequest( 'bad-data', - fakeResponse.crc32c + fakeResponse.md5 ); var readStream = file.createReadStream({ validation: 'md5' }); @@ -906,6 +906,20 @@ describe('File', function() { }; readStream.resume(); }); + + it('should destroy if MD5 is requested but absent', function(done) { + file.requestStream = getFakeSuccessfulRequest( + 'bad-data', + fakeResponse.crc32c + ); + + var readStream = file.createReadStream({ validation: 'md5' }); + readStream.destroy = function(err) { + assert.strictEqual(err.code, 'MD5_NOT_AVAILABLE'); + done(); + }; + readStream.resume(); + }); }); }); @@ -1317,6 +1331,29 @@ describe('File', function() { writable.end(); }); + it('should emit an error if MD5 is requested but absent', function(done) { + var writable = file.createWriteStream({validation: 'md5'}); + + file.startResumableUpload_ = function(stream) { + setImmediate(function() { + file.metadata = { crc32c: 'not-md5' }; + stream.emit('complete'); + }); + }; + + file.delete = function(cb) { + cb(); + }; + + writable.write(data); + writable.end(); + + writable.on('error', function(err) { + assert.equal(err.code, 'MD5_NOT_AVAILABLE'); + done(); + }); + }); + it('should emit a different error if delete fails', function(done) { var writable = file.createWriteStream();