2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 11:00:06 +00:00
|
|
|
'use strict';
|
2015-08-19 06:15:25 +00:00
|
|
|
const common = require('../common');
|
2017-10-06 17:15:52 +00:00
|
|
|
const fixtures = require('../common/fixtures');
|
2011-01-02 09:13:56 +00:00
|
|
|
|
2017-06-30 23:29:09 +00:00
|
|
|
if (!common.hasCrypto)
|
2016-05-11 19:34:52 +00:00
|
|
|
common.skip('missing crypto');
|
2015-03-04 01:11:21 +00:00
|
|
|
|
2015-08-19 06:15:25 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const https = require('https');
|
2011-01-02 09:13:56 +00:00
|
|
|
|
2019-06-02 16:11:48 +00:00
|
|
|
// Assert that the IP-as-servername deprecation warning does not occur.
|
|
|
|
process.on('warning', common.mustNotCall());
|
|
|
|
|
2015-08-19 06:15:25 +00:00
|
|
|
const options = {
|
2017-10-06 17:15:52 +00:00
|
|
|
key: fixtures.readKey('agent1-key.pem'),
|
|
|
|
cert: fixtures.readKey('agent1-cert.pem')
|
2011-01-02 09:13:56 +00:00
|
|
|
};
|
|
|
|
|
2015-08-19 06:15:25 +00:00
|
|
|
const body = 'hello world\n';
|
|
|
|
|
|
|
|
const serverCallback = common.mustCall(function(req, res) {
|
2011-01-02 09:13:56 +00:00
|
|
|
res.writeHead(200, { 'content-type': 'text/plain' });
|
|
|
|
res.end(body);
|
2011-10-04 22:08:18 +00:00
|
|
|
});
|
2011-01-02 09:13:56 +00:00
|
|
|
|
2023-01-25 08:52:33 +00:00
|
|
|
const invalid_options = [ 'foo', 42, true, [] ];
|
2023-11-23 16:34:46 +00:00
|
|
|
for (const option of invalid_options) {
|
2023-01-25 08:52:33 +00:00
|
|
|
assert.throws(() => {
|
|
|
|
new https.Server(option);
|
|
|
|
}, {
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
|
|
});
|
2023-11-23 16:34:46 +00:00
|
|
|
}
|
2023-01-25 08:52:33 +00:00
|
|
|
|
2015-08-19 06:15:25 +00:00
|
|
|
const server = https.createServer(options, serverCallback);
|
2011-01-02 09:13:56 +00:00
|
|
|
|
2017-04-28 03:21:01 +00:00
|
|
|
server.listen(0, common.mustCall(() => {
|
2019-05-29 04:31:05 +00:00
|
|
|
let tests = 0;
|
|
|
|
|
|
|
|
function done() {
|
|
|
|
if (--tests === 0)
|
|
|
|
server.close();
|
|
|
|
}
|
|
|
|
|
2015-08-19 06:15:25 +00:00
|
|
|
// Do a request ignoring the unauthorized server certs
|
2017-04-28 03:21:01 +00:00
|
|
|
const port = server.address().port;
|
|
|
|
|
2019-05-29 04:31:05 +00:00
|
|
|
const options = {
|
2015-08-19 06:15:25 +00:00
|
|
|
hostname: '127.0.0.1',
|
2017-04-28 03:21:01 +00:00
|
|
|
port: port,
|
2015-08-19 06:15:25 +00:00
|
|
|
path: '/',
|
|
|
|
method: 'GET',
|
|
|
|
rejectUnauthorized: false
|
|
|
|
};
|
2019-05-29 04:31:05 +00:00
|
|
|
tests++;
|
|
|
|
const req = https.request(options, common.mustCall((res) => {
|
2015-08-19 06:15:25 +00:00
|
|
|
let responseBody = '';
|
|
|
|
res.on('data', function(d) {
|
|
|
|
responseBody = responseBody + d;
|
|
|
|
});
|
|
|
|
|
2017-04-28 03:21:01 +00:00
|
|
|
res.on('end', common.mustCall(() => {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(responseBody, body);
|
2019-05-29 04:31:05 +00:00
|
|
|
done();
|
2017-04-28 03:21:01 +00:00
|
|
|
}));
|
|
|
|
}));
|
2015-08-19 06:15:25 +00:00
|
|
|
req.end();
|
|
|
|
|
2019-05-29 04:31:05 +00:00
|
|
|
// Do a request that errors due to the invalid server certs
|
|
|
|
options.rejectUnauthorized = true;
|
|
|
|
tests++;
|
|
|
|
const checkCertReq = https.request(options, common.mustNotCall()).end();
|
2015-08-19 06:15:25 +00:00
|
|
|
|
2017-04-28 03:21:01 +00:00
|
|
|
checkCertReq.on('error', common.mustCall((e) => {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(e.code, 'UNABLE_TO_VERIFY_LEAF_SIGNATURE');
|
2019-05-29 04:31:05 +00:00
|
|
|
done();
|
2017-04-28 03:21:01 +00:00
|
|
|
}));
|
|
|
|
}));
|