Skip to content

Commit

Permalink
added checks to raw pixelbuffer size arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterverweirder committed Dec 10, 2014
1 parent 9de8fae commit 63060b5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
30 changes: 27 additions & 3 deletions lib/obtain.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,33 @@
});
} else if (source instanceof Buffer) {
if(typeof type === 'object') {
if(type.width && type.height) {
callback(null, new Image(source, type.width, type.height, type.trans));
} else throw Error("Missing width and height");
if(!type.width && !type.height) {
throw Error("Missing width and height");
}
if(!type.width) {
throw Error("Missing width");
}
if(!type.height) {
throw Error("Missing height");
}
if(typeof type.width !== "number") {
throw Error("Width must be numeric");
}
if(typeof type.height !== "number") {
throw Error("Height must be numeric");
}
var channelSize = type.width * type.height;
var numChannels = 3;
var size = channelSize * numChannels;
var bufferSize = source.length;
if(size !== bufferSize) {
numChannels++;
size += channelSize;
}
if(size !== bufferSize) {
throw Error("Invalid width or height");
}
callback(null, new Image(source, type.width, type.height, (numChannels === 4)));
} else {
var opener = getOpener(type);
opener(source, function(err, pixelsBuf, width, height, channels, trans) {
Expand Down
52 changes: 52 additions & 0 deletions tests/00.argsValidation/001.open.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,56 @@ describe('lwip.open arguments validation', function() {

});

describe('pixelbuffer', function() {

var buffer;
before(function(done) {
buffer = new Buffer(100 * 100 * 4);
done();
});

describe('without width', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { height: 100 }, function() {}).should.throwError();
});
});

describe('without height', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { width: 100 }, function() {}).should.throwError();
});
});

describe('without width and height', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { }, function() {}).should.throwError();
});
});

describe('with non numeric width', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { width: "lorem", height: 100 }, function() {}).should.throwError();
});
});

describe('with non numeric height', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { width: 100, height: "lorem" }, function() {}).should.throwError();
});
});

describe('with non numeric width and height', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { width: "lorem", height: "ipsum" }, function() {}).should.throwError();
});
});

describe('with incorrect width and height', function() {
it('should throw an error', function() {
lwip.open.bind(lwip, buffer, { width: 123, height: 321 }, function() {}).should.throwError();
});
});

});

});
2 changes: 1 addition & 1 deletion tests/02.operations/001.open.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('lwip.open', function() {
});

it('should succeed', function(done) {
lwip.open(buffer, { width: 100, height: 100, trans: true }, function(err, img) {
lwip.open(buffer, { width: 100, height: 100 }, function(err, img) {
should(err).not.be.Error;
img.should.be.OK;
done();
Expand Down

0 comments on commit 63060b5

Please sign in to comment.