1'use strict'; 2 3var umask = require('..'); 4 5var Code = require('code'); 6var Lab = require('lab'); 7var lab = Lab.script(); 8exports.lab = lab; 9 10var describe = lab.describe; 11var it = lab.it; 12var expect = Code.expect; 13 14describe('validates umask', function () { 15 // signature of validator: validate(obj, key, val) 16 // store valid value in obj[key] 17 // return false if invalid 18 19 it('accepts numbers', function (done) { 20 var o = {}, 21 result = false; 22 23 result = umask.validate(o, 'umask', 0); 24 expect(result).to.equal(true); 25 expect(o.umask).to.equal(0); 26 27 result = umask.validate(o, 'umask', 511); 28 expect(result).to.equal(true); 29 expect(o.umask).to.equal(511); 30 done(); 31 }); 32 33 it('accepts strings', function (done) { 34 var o = {}, 35 result; 36 37 result = umask.validate(o, 'umask', "0"); 38 expect(result).to.equal(true); 39 expect(o.umask).to.equal(0); 40 41 result = umask.validate(o, 'umask', "0777"); 42 expect(result).to.equal(true); 43 expect(o.umask).to.equal(511); 44 45 done(); 46 }); 47 48 it('rejects other types', function (done) { 49 expect(umask.validate(undefined, undefined, false)).to.equal(false); 50 expect(umask.validate(undefined, undefined, {})).to.equal(false); 51 52 done(); 53 }); 54 55 it('rejects non-octalish strings', function (done) { 56 expect(umask.validate(undefined, undefined, "1")).to.equal(false); 57 58 done(); 59 }); 60 61 it('rejects NaN strings', function (done) { 62 expect(umask.validate(undefined, undefined, NaN)).to.equal(false); 63 64 done(); 65 }); 66}); 67 68describe('umask to string', function () { 69 it("converts umask to string", function (done) { 70 expect(umask.toString(0)).to.equal("0000"); 71 expect(umask.toString(1)).to.equal("0001"); 72 expect(umask.toString(7)).to.equal("0007"); 73 expect(umask.toString(8)).to.equal("0010"); 74 expect(umask.toString(511)).to.equal("0777"); 75 expect(umask.toString(18)).to.equal("0022"); 76 expect(umask.toString(16)).to.equal("0020"); 77 done(); 78 }); 79}); 80 81describe('umask from string', function () { 82 it('converts valid values', function (done) { 83 expect(umask.fromString("0000")).to.equal(0); 84 expect(umask.fromString("0")).to.equal(0); 85 expect(umask.fromString("0777")).to.equal(511); 86 expect(umask.fromString("0024")).to.equal(20); 87 88 expect(umask.fromString(0)).to.equal(0); 89 expect(umask.fromString(20)).to.equal(20); 90 expect(umask.fromString(21)).to.equal(21); 91 expect(umask.fromString(511)).to.equal(511); 92 93 done(); 94 }); 95 96 it('converts valid values', function (done) { 97 expect(umask.fromString("0000")).to.equal(0); 98 expect(umask.fromString("0")).to.equal(0); 99 expect(umask.fromString("010")).to.equal(8); 100 expect(umask.fromString("0777")).to.equal(511); 101 expect(umask.fromString("0024")).to.equal(20); 102 103 expect(umask.fromString("8")).to.equal(8); 104 expect(umask.fromString("9")).to.equal(9); 105 expect(umask.fromString("18")).to.equal(18); 106 expect(umask.fromString("16")).to.equal(16); 107 108 expect(umask.fromString(0)).to.equal(0); 109 expect(umask.fromString(20)).to.equal(20); 110 expect(umask.fromString(21)).to.equal(21); 111 expect(umask.fromString(511)).to.equal(511); 112 113 expect(umask.fromString(0.1)).to.equal(0); 114 expect(umask.fromString(511.1)).to.equal(511); 115 116 done(); 117 }); 118 119 it('errors on empty string', function (done) { 120 umask.fromString("", function (err, val) { 121 expect(err.message).to.equal('Expected octal string, got "", defaulting to "0022"'); 122 expect(val).to.equal(18); 123 done(); 124 }); 125 }); 126 127 it('errors on invalid octal string', function (done) { 128 umask.fromString("099", function (err, val) { 129 expect(err.message).to.equal('Expected octal string, got "099", defaulting to "0022"'); 130 expect(val).to.equal(18); 131 done(); 132 }); 133 }); 134 135 it('errors when non-string, non-number (boolean)', function (done) { 136 umask.fromString(false, function (err, val) { 137 expect(err.message).to.equal('Expected number or octal string, got false, defaulting to "0022"'); 138 expect(val).to.equal(18); 139 done(); 140 }); 141 }); 142 143 it('errors when non-string, non-number (object)', function (done) { 144 umask.fromString({}, function (err, val) { 145 expect(err.message).to.equal('Expected number or octal string, got {}, defaulting to "0022"'); 146 expect(val).to.equal(18); 147 done(); 148 }); 149 }); 150 151 it('errors when out of range (<0)', function (done) { 152 umask.fromString(-1, function (err, val) { 153 expect(err.message).to.equal('Must be in range 0..511 (0000..0777), got -1'); 154 expect(val).to.equal(18); 155 done(); 156 }); 157 }); 158 159 it('errors when out of range (>511)', function (done) { 160 umask.fromString(512, function (err, val) { 161 expect(err.message).to.equal('Must be in range 0..511 (0000..0777), got 512'); 162 expect(val).to.equal(18); 163 done(); 164 }); 165 }); 166}); 167