From 5c01e42719142e660d0c4297a7c3af938e579252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20S=C3=A5gfors?= Date: Tue, 20 Aug 2019 01:00:52 +0900 Subject: [PATCH 1/5] Add test script to package.json Fixes tests not running in TravisCI --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 3750a1a..76db4ea 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,9 @@ "type": "git", "url": "git@github.com:khrome/bit-mask.git" }, + "scripts": { + "test": "mocha" + }, "dependencies": { "array-events": "^0.2.0" }, From 07968eb85c890908958f74e5da22bd041603c984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20S=C3=A5gfors?= Date: Tue, 20 Aug 2019 01:20:02 +0900 Subject: [PATCH 2/5] Update NPM versions on Travis The tests wouldn't run on versions < 6, removed those. Added current & LTS versions: https://nodejs.org/en/about/releases/ --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index b320edc..c7739b0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: node_js node_js: - "6" - - "6.1" - - "5.11" - - "iojs" \ No newline at end of file + - "8" + - "10" + - "12" From cfd4aac03d96f72a98d7804ffae82708666d80c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20S=C3=A5gfors?= Date: Tue, 20 Aug 2019 01:15:17 +0900 Subject: [PATCH 3/5] Add failing tests for getBit behaviour --- test.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/test.js b/test.js index ca062ca..faee301 100644 --- a/test.js +++ b/test.js @@ -4,6 +4,36 @@ var BitMask = require('./bit-mask'); var OwnershipMask = BitMask.OwnershipMask; describe('BitMask', function(){ + describe('.getBit()', function(){ + it('reads bits set on initialization', function(){ + var mask = new BitMask('01111010', 2); + mask.getBit(0).should.equal(false, 'bit 0'); + mask.getBit(1).should.equal(true, 'bit 0'); + mask.getBit(2).should.equal(false, 'bit 2'); + mask.getBit(3).should.equal(true, 'bit 3'); + mask.getBit(4).should.equal(true, 'bit 4'); + mask.getBit(5).should.equal(true, 'bit 5'); + mask.getBit(6).should.equal(true, 'bit 6'); + mask.getBit(7).should.equal(false, 'bit 7'); + + for(var n=8; n < 31; n++){ + mask.getBit(n).should.equal(false, 'bit ' + n); + } + }); + + it('reads bits set by setBit()', function(){ + var mask = new BitMask('0'); + + mask.setBit(14, true); + mask.getBit(14).should.equal(true, 'bit 14 should be set'); + + mask.setBit(3, true); + mask.getBit(3).should.equal(true, 'bit 3 should be set'); + mask.setBit(3, false); + mask.getBit(3).should.equal(false, 'bit 3 should be unset'); + }); + }); + describe('> OwnershipMask', function(){ var mask; From 43a7b25dd7cffafd8ef7acc77375646a98927570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20S=C3=A5gfors?= Date: Tue, 20 Aug 2019 01:54:46 +0900 Subject: [PATCH 4/5] Reverse the order permission bits are counted This fixes the disparity between setBit and getBit --- bit-mask.js | 6 ++--- test.js | 70 ++++++++++++++++++++++++++--------------------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/bit-mask.js b/bit-mask.js index ab74481..5e4dc7d 100644 --- a/bit-mask.js +++ b/bit-mask.js @@ -16,7 +16,7 @@ BitMask.prototype.setBit = function(position, value){ } }; BitMask.prototype.getBit = function(position){ - return !!((1 << (this.base-position) ) & this.value); + return !!((1 << position) & this.value); } BitMask.prototype.bits = function(){ return this.value.toString(2); @@ -88,8 +88,8 @@ OwnershipMask.prototype.modify = function(clause){ } } } -OwnershipMask.prototype.contexts = ['user', 'group', 'world']; -OwnershipMask.prototype.permissions = ['read', 'write', 'execute']; +OwnershipMask.prototype.contexts = ['user', 'group', 'world'].reverse(); +OwnershipMask.prototype.permissions = ['read', 'write', 'execute'].reverse(); OwnershipMask.prototype.setBit = BitMask.prototype.setBit; OwnershipMask.prototype.getBit = BitMask.prototype.getBit; OwnershipMask.prototype.bits = BitMask.prototype.bits; diff --git a/test.js b/test.js index faee301..17eb159 100644 --- a/test.js +++ b/test.js @@ -36,62 +36,62 @@ describe('BitMask', function(){ describe('> OwnershipMask', function(){ var mask; - + before(function(){ mask = new OwnershipMask('755'); }); - - it('user read [1]', function(){ - mask.getBit(0).should.equal(mask.hasPermission('user', 'read')); - mask.getBit(0).should.equal(true); + + it('user read [8]', function(){ + mask.getBit(8).should.equal(mask.hasPermission('user', 'read')); + mask.getBit(8).should.equal(true); mask.hasPermission('user', 'read').should.equal(true); }); - - it('user write [2]', function(){ - mask.getBit(1).should.equal(mask.hasPermission('user', 'write')); - mask.getBit(1).should.equal(true); + + it('user write [7]', function(){ + mask.getBit(7).should.equal(mask.hasPermission('user', 'write')); + mask.getBit(7).should.equal(true); mask.hasPermission('user', 'write').should.equal(true); }); - it('user execute [3]', function(){ - mask.getBit(2).should.equal(mask.hasPermission('user', 'execute')); - mask.getBit(2).should.equal(true); + it('user execute [6]', function(){ + mask.getBit(6).should.equal(mask.hasPermission('user', 'execute')); + mask.getBit(6).should.equal(true); mask.hasPermission('user', 'execute').should.equal(true); }); - - it('group read [4]', function(){ - mask.getBit(3).should.equal(mask.hasPermission('group', 'read')); - mask.getBit(3).should.equal(true); + + it('group read [5]', function(){ + mask.getBit(5).should.equal(mask.hasPermission('group', 'read')); + mask.getBit(5).should.equal(true); mask.hasPermission('group', 'read').should.equal(true); }); - - it('group write [5]', function(){ + + it('group write [4]', function(){ mask.getBit(4).should.equal(mask.hasPermission('group', 'write')); mask.getBit(4).should.equal(false); mask.hasPermission('group', 'write').should.equal(false); }); - - it('group execute [6]', function(){ - mask.getBit(5).should.equal(mask.hasPermission('group', 'execute')); - mask.getBit(5).should.equal(true); + + it('group execute [3]', function(){ + mask.getBit(3).should.equal(mask.hasPermission('group', 'execute')); + mask.getBit(3).should.equal(true); mask.hasPermission('group', 'execute').should.equal(true); }); - - it('world read [7]', function(){ - mask.getBit(6).should.equal(mask.hasPermission('world', 'read')); - mask.getBit(6).should.equal(true); + + it('world read [2]', function(){ + mask.getBit(2).should.equal(mask.hasPermission('world', 'read')); + mask.getBit(2).should.equal(true); }); - - it('world write [8]', function(){ - mask.getBit(7).should.equal(mask.hasPermission('world', 'write')); - mask.getBit(7).should.equal(false); + + it('world write [1]', function(){ + mask.getBit(1).should.equal(mask.hasPermission('world', 'write')); + mask.getBit(1).should.equal(false); mask.hasPermission('world', 'write').should.equal(false); }); - - it('world execute [9]', function(){ - mask.getBit(8).should.equal(mask.hasPermission('world', 'execute')); - mask.getBit(8).should.equal(true); + + it('world execute [0]', function(){ + mask.getBit(0).should.equal(mask.hasPermission('world', 'execute')); + mask.getBit(0).should.equal(true); mask.hasPermission('world', 'execute').should.equal(true); }); }); -}); \ No newline at end of file +}); From 247a483e9ec45ddeae8acc8bf4f152d4a23c4dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20S=C3=A5gfors?= Date: Tue, 20 Aug 2019 01:56:18 +0900 Subject: [PATCH 5/5] Ensure getPosition returns the expected result --- test.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test.js b/test.js index 17eb159..5bce9ca 100644 --- a/test.js +++ b/test.js @@ -42,53 +42,62 @@ describe('BitMask', function(){ }); it('user read [8]', function(){ + mask.getPosition('user', 'read').should.equal(8); mask.getBit(8).should.equal(mask.hasPermission('user', 'read')); mask.getBit(8).should.equal(true); mask.hasPermission('user', 'read').should.equal(true); }); it('user write [7]', function(){ + mask.getPosition('user', 'write').should.equal(7); mask.getBit(7).should.equal(mask.hasPermission('user', 'write')); mask.getBit(7).should.equal(true); mask.hasPermission('user', 'write').should.equal(true); }); - + it('user execute [6]', function(){ + mask.getPosition('user', 'execute').should.equal(6); mask.getBit(6).should.equal(mask.hasPermission('user', 'execute')); mask.getBit(6).should.equal(true); mask.hasPermission('user', 'execute').should.equal(true); }); it('group read [5]', function(){ + mask.getPosition('group', 'read').should.equal(5); mask.getBit(5).should.equal(mask.hasPermission('group', 'read')); mask.getBit(5).should.equal(true); mask.hasPermission('group', 'read').should.equal(true); }); it('group write [4]', function(){ + mask.getPosition('group', 'write').should.equal(4); mask.getBit(4).should.equal(mask.hasPermission('group', 'write')); mask.getBit(4).should.equal(false); mask.hasPermission('group', 'write').should.equal(false); }); it('group execute [3]', function(){ + mask.getPosition('group', 'execute').should.equal(3); mask.getBit(3).should.equal(mask.hasPermission('group', 'execute')); mask.getBit(3).should.equal(true); mask.hasPermission('group', 'execute').should.equal(true); }); it('world read [2]', function(){ + mask.getPosition('world', 'read').should.equal(2); mask.getBit(2).should.equal(mask.hasPermission('world', 'read')); mask.getBit(2).should.equal(true); }); it('world write [1]', function(){ + mask.getPosition('world', 'write').should.equal(1); mask.getBit(1).should.equal(mask.hasPermission('world', 'write')); mask.getBit(1).should.equal(false); mask.hasPermission('world', 'write').should.equal(false); }); it('world execute [0]', function(){ + mask.getPosition('world', 'execute').should.equal(0); mask.getBit(0).should.equal(mask.hasPermission('world', 'execute')); mask.getBit(0).should.equal(true); mask.hasPermission('world', 'execute').should.equal(true);