Skip to content

Commit

Permalink
storage: improve err msg if MD5 spec'd but absent
Browse files Browse the repository at this point in the history
Ref #1749
  • Loading branch information
zbjornson committed Oct 25, 2016
1 parent 3a23f83 commit 25b92ba
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
16 changes: 15 additions & 1 deletion packages/storage/src/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 = [
Expand Down
39 changes: 38 additions & 1 deletion packages/storage/test/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand All @@ -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();
});
});
});

Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 25b92ba

Please sign in to comment.