Skip to content

Commit 5c0eb60

Browse files
parameter validation test
1 parent 6297728 commit 5c0eb60

File tree

2 files changed

+93
-17
lines changed

2 files changed

+93
-17
lines changed

lib/kerberos.js

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const originalStep = KerberosClient.prototype.step;
4343
*/
4444
KerberosClient.prototype.step = async function step(challenge) {
4545
if (typeof challenge !== 'string') {
46-
throw new Error('parameter `challenge` must be a string.');
46+
throw new TypeError('parameter `challenge` must be a string.');
4747
}
4848
return await promisify(originalStep.bind(this))(challenge);
4949
};
@@ -60,15 +60,11 @@ const originalWrap = KerberosClient.prototype.wrap;
6060
* @param {boolean} [options.protect] Indicates if the wrap should request message confidentiality
6161
* @return {Promise<string>}
6262
*/
63-
KerberosClient.prototype.wrap = async function wrap(challenge, options) {
63+
KerberosClient.prototype.wrap = async function wrap(challenge, options = {}) {
6464
if (typeof challenge !== 'string') {
65-
throw new Error('parameter `challenge` must be a string.');
65+
throw new TypeError('parameter `challenge` must be a string.');
6666
}
6767

68-
// eslint-disable-next-line no-restricted-syntax
69-
if (typeof challenge !== 'object' || challenge === null) {
70-
throw new Error('parameter `challenge` must be an object.');
71-
}
7268
return await promisify(originalWrap.bind(this))(challenge, options);
7369
};
7470

@@ -83,7 +79,7 @@ const originalUnwrap = KerberosClient.prototype.unwrap;
8379
*/
8480
KerberosClient.prototype.unwrap = async function unwrap(challenge) {
8581
if (typeof challenge !== 'string') {
86-
throw new Error('parameter `challenge` must be a string.');
82+
throw new TypeError('parameter `challenge` must be a string.');
8783
}
8884
return await promisify(originalUnwrap.bind(this))(challenge);
8985
};
@@ -108,7 +104,7 @@ const originalServerStep = KerberosServer.prototype.step;
108104
*/
109105
KerberosServer.prototype.step = async function step(challenge) {
110106
if (typeof challenge !== 'string') {
111-
throw new Error('parameter `challenge` must be a string.');
107+
throw new TypeError('parameter `challenge` must be a string.');
112108
}
113109
return await promisify(originalServerStep.bind(this))(challenge);
114110
};
@@ -139,16 +135,16 @@ KerberosServer.prototype.step = async function step(challenge) {
139135
*/
140136
async function checkPassword(username, password, service, defaultRealm) {
141137
if (typeof username !== 'string') {
142-
throw new Error('parameter `username` must be a string.');
138+
throw new TypeError('parameter `username` must be a string.');
143139
}
144140
if (typeof password !== 'string') {
145-
throw new Error('parameter `password` must be a string.');
141+
throw new TypeError('parameter `password` must be a string.');
146142
}
147143
if (typeof service !== 'string') {
148-
throw new Error('parameter `service` must be a string.');
144+
throw new TypeError('parameter `service` must be a string.');
149145
}
150146
if (defaultRealm && typeof defaultRealm !== 'string') {
151-
throw new Error('if specified, parameter `defaultRealm` must be a string.');
147+
throw new TypeError('if specified, parameter `defaultRealm` must be a string.');
152148
}
153149
return await promisify(kerberos.checkPassword)(username, password, service, defaultRealm);
154150
}
@@ -165,10 +161,10 @@ async function checkPassword(username, password, service, defaultRealm) {
165161
*/
166162
async function principalDetails(service, hostname) {
167163
if (typeof service !== 'string') {
168-
throw new Error('parameter `service` must be a string.');
164+
throw new TypeError('parameter `service` must be a string.');
169165
}
170166
if (typeof hostname !== 'string') {
171-
throw new Error('parameter `hostname` must be a string.');
167+
throw new TypeError('parameter `hostname` must be a string.');
172168
}
173169
return await promisify(kerberos.principalDetails)(service, hostname);
174170
}
@@ -186,7 +182,7 @@ async function principalDetails(service, hostname) {
186182
*/
187183
async function initializeClient(service, options = { mechOID: GSS_C_NO_OID }) {
188184
if (typeof service !== 'string') {
189-
throw new Error('parameter `service` must be a string.');
185+
throw new TypeError('parameter `service` must be a string.');
190186
}
191187
return await promisify(kerberos.initializeClient)(service, options);
192188
}
@@ -200,7 +196,7 @@ async function initializeClient(service, options = { mechOID: GSS_C_NO_OID }) {
200196
*/
201197
async function initializeServer(service) {
202198
if (typeof service !== 'string') {
203-
throw new Error('parameter `service` must be a string.');
199+
throw new TypeError('parameter `service` must be a string.');
204200
}
205201
return await promisify(kerberos.initializeServer)(service);
206202
}

test/kerberos_tests.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,84 @@ describe('Kerberos', function () {
141141
});
142142
});
143143
});
144+
145+
describe('parameter validation', function () {
146+
test('initializeClient() throws if service is not a string', async function () {
147+
expect(await kerberos.initializeClient().catch(e => e))
148+
.to.be.instanceOf(TypeError)
149+
.to.match(/`service` must be a string/);
150+
});
151+
152+
test('initializeServer() throws if service is not a string', async function () {
153+
expect(await kerberos.initializeServer().catch(e => e))
154+
.to.be.instanceOf(TypeError)
155+
.to.match(/`service` must be a string/);
156+
});
157+
158+
test('principalDetails() throws if service is not a string', async function () {
159+
expect(await kerberos.principalDetails(3, 'foo').catch(e => e))
160+
.to.be.instanceOf(TypeError)
161+
.to.match(/`service` must be a string/);
162+
});
163+
164+
test('principalDetails() throws if hostname is not a string', async function () {
165+
expect(await kerberos.principalDetails('foo', 3).catch(e => e))
166+
.to.be.instanceOf(TypeError)
167+
.to.match(/`hostname` must be a string/);
168+
});
169+
170+
test('checkPassword() throws if username is not a string', async function () {
171+
expect(await kerberos.checkPassword(3, 'password', 'service').catch(e => e))
172+
.to.be.instanceOf(TypeError)
173+
.to.match(/`username` must be a string/);
174+
});
175+
176+
test('checkPassword() throws if password is not a string', async function () {
177+
expect(await kerberos.checkPassword('username', 3, 'service').catch(e => e))
178+
.to.be.instanceOf(TypeError)
179+
.to.match(/`password` must be a string/);
180+
});
181+
182+
test('checkPassword() throws if service is not a string', async function () {
183+
expect(await kerberos.checkPassword('username', 'password', 3).catch(e => e))
184+
.to.be.instanceOf(TypeError)
185+
.to.match(/`service` must be a string/);
186+
});
187+
188+
test('KerberosServer.step() throws if challenge is not a string', async function () {
189+
const service = `HTTP@${hostname}`;
190+
191+
const server = await kerberos.initializeServer(service);
192+
expect(await server.step(3).catch(e => e))
193+
.to.be.instanceOf(TypeError)
194+
.to.match(/`challenge` must be a string/);
195+
});
196+
197+
describe('KerberosClient', function () {
198+
let client;
199+
beforeEach(async function () {
200+
const service = `HTTP@${hostname}`;
201+
202+
client = await kerberos.initializeClient(service);
203+
});
204+
205+
test('KerberosClient.unwrap() throws if challenge is not a string', async function () {
206+
expect(await client.unwrap(3).catch(e => e))
207+
.to.be.instanceOf(TypeError)
208+
.to.match(/`challenge` must be a string/);
209+
});
210+
211+
test('KerberosClient.wrap() throws if challenge is not a string', async function () {
212+
expect(await client.wrap(3).catch(e => e))
213+
.to.be.instanceOf(TypeError)
214+
.to.match(/`challenge` must be a string/);
215+
});
216+
217+
test('KerberosClient.step() throws if challenge is not a string', async function () {
218+
expect(await client.step(3).catch(e => e))
219+
.to.be.instanceOf(TypeError)
220+
.to.match(/`challenge` must be a string/);
221+
});
222+
});
223+
});
144224
});

0 commit comments

Comments
 (0)