Skip to content

Commit

Permalink
add validation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Nov 26, 2014
1 parent 14b48b5 commit a1b2671
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,12 @@ File.prototype.createReadStream = function(options) {
}

if (failed) {
var error = new Error({
code: 911,
message: [
'The downloaded data did not match the data from the server.',
'To be sure the content is the same, you should download the',
'file again.'
].join(' ')
});
var error = 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',
'file again.'
].join(' '));
error.code = 'CONTENT_DOWNLOAD_MISMATCH';

throughStream.emit('error', error);
} else {
Expand Down
99 changes: 99 additions & 0 deletions test/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

var assert = require('assert');
var Bucket = require('../../lib/storage/bucket.js');
var crc = require('fast-crc32c');
var crypto = require('crypto');
var duplexify = require('duplexify');
var extend = require('extend');
var nodeutil = require('util');
Expand Down Expand Up @@ -302,6 +304,103 @@ describe('File', function() {

file.createReadStream();
});

describe('validation', function() {
var data = 'test';

var crc32cBase64 = new Buffer([crc.calculate(data)]).toString('base64');

var md5HashBase64 = crypto.createHash('md5');
md5HashBase64.update(data);
md5HashBase64 = md5HashBase64.digest('base64');

var fakeResponse = {
crc32c: {
headers: { 'x-goog-hash': 'crc32c=####' + crc32cBase64 }
},
md5: {
headers: { 'x-goog-hash': 'md5=' + md5HashBase64 }
}
};

function getFakeRequest(data, fakeResponse) {
function FakeRequest(req) {
if (!(this instanceof FakeRequest)) {
return new FakeRequest(req);
}

var that = this;

stream.Readable.call(this);
this._read = function() {
this.push(data);
this.push(null);
};

setImmediate(function() {
that.emit('complete', fakeResponse);
});
}
nodeutil.inherits(FakeRequest, stream.Readable);
return FakeRequest;
}

beforeEach(function() {
file.metadata.mediaLink = 'http://uri';

file.bucket.storage.makeAuthorizedRequest_ = function(opts, callback) {
(callback.onAuthorized || callback)(null, {});
};
});

it('should validate with crc32c', function(done) {
request_Override = getFakeRequest(data, fakeResponse.crc32c);

file.createReadStream({ validation: 'crc32c' })
.on('error', done)
.on('complete', done);
});

it('should emit an error if crc32c validation fails', function(done) {
request_Override = getFakeRequest('bad-data', fakeResponse.crc32c);

file.createReadStream({ validation: 'crc32c' })
.on('error', function(err) {
assert.equal(err.code, 'CONTENT_DOWNLOAD_MISMATCH');
done();
});
});

it('should validate with md5', function(done) {
request_Override = getFakeRequest(data, fakeResponse.md5);

file.createReadStream({ validation: 'md5' })
.on('error', done)
.on('complete', done);
});

it('should emit an error if md5 validation fails', function(done) {
request_Override = getFakeRequest('bad-data', fakeResponse.crc32c);

file.createReadStream({ validation: 'md5' })
.on('error', function(err) {
assert.equal(err.code, 'CONTENT_DOWNLOAD_MISMATCH');
done();
});
});

it('should default to md5 validation', function(done) {
request_Override = getFakeRequest(data, {
headers: { 'x-goog-hash': 'md5=fakefakefake' }
});

file.createReadStream()
.on('error', function(err) {
assert.equal(err.code, 'CONTENT_DOWNLOAD_MISMATCH');
done();
});
});
});
});

describe('createWriteStream', function() {
Expand Down

0 comments on commit a1b2671

Please sign in to comment.