|
| 1 | +/* |
| 2 | + * Copyright 2019 gRPC authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +// Allow `any` data type for testing runtime type checking. |
| 19 | +// tslint:disable no-any |
| 20 | +import * as assert from 'assert'; |
| 21 | +import {readFileSync} from 'fs'; |
| 22 | +import {join} from 'path'; |
| 23 | +import {ServerCredentials} from '../src'; |
| 24 | + |
| 25 | +const ca = readFileSync(join(__dirname, 'fixtures', 'ca.pem')); |
| 26 | +const key = readFileSync(join(__dirname, 'fixtures', 'server1.key')); |
| 27 | +const cert = readFileSync(join(__dirname, 'fixtures', 'server1.pem')); |
| 28 | + |
| 29 | +describe('Server Credentials', () => { |
| 30 | + describe('createInsecure', () => { |
| 31 | + it('creates insecure credentials', () => { |
| 32 | + const creds = ServerCredentials.createInsecure(); |
| 33 | + |
| 34 | + assert.strictEqual(creds._isSecure(), false); |
| 35 | + assert.strictEqual(creds._getSettings(), null); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('createSsl', () => { |
| 40 | + it('accepts a buffer and array as the first two arguments', () => { |
| 41 | + const creds = ServerCredentials.createSsl(ca, []); |
| 42 | + |
| 43 | + assert.strictEqual(creds._isSecure(), true); |
| 44 | + assert.deepStrictEqual( |
| 45 | + creds._getSettings(), {ca, cert: [], key: [], requestCert: false}); |
| 46 | + }); |
| 47 | + |
| 48 | + it('accepts a boolean as the third argument', () => { |
| 49 | + const creds = ServerCredentials.createSsl(ca, [], true); |
| 50 | + |
| 51 | + assert.strictEqual(creds._isSecure(), true); |
| 52 | + assert.deepStrictEqual( |
| 53 | + creds._getSettings(), {ca, cert: [], key: [], requestCert: true}); |
| 54 | + }); |
| 55 | + |
| 56 | + it('accepts an object with two buffers in the second argument', () => { |
| 57 | + const keyCertPairs = [{private_key: key, cert_chain: cert}]; |
| 58 | + const creds = ServerCredentials.createSsl(null, keyCertPairs); |
| 59 | + |
| 60 | + assert.strictEqual(creds._isSecure(), true); |
| 61 | + assert.deepStrictEqual( |
| 62 | + creds._getSettings(), |
| 63 | + {ca: undefined, cert: [cert], key: [key], requestCert: false}); |
| 64 | + }); |
| 65 | + |
| 66 | + it('accepts multiple objects in the second argument', () => { |
| 67 | + const keyCertPairs = [ |
| 68 | + {private_key: key, cert_chain: cert}, |
| 69 | + {private_key: key, cert_chain: cert} |
| 70 | + ]; |
| 71 | + const creds = ServerCredentials.createSsl(null, keyCertPairs, false); |
| 72 | + |
| 73 | + assert.strictEqual(creds._isSecure(), true); |
| 74 | + assert.deepStrictEqual(creds._getSettings(), { |
| 75 | + ca: undefined, |
| 76 | + cert: [cert, cert], |
| 77 | + key: [key, key], |
| 78 | + requestCert: false |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('fails if the second argument is not an Array', () => { |
| 83 | + assert.throws(() => { |
| 84 | + ServerCredentials.createSsl(ca, 'test' as any); |
| 85 | + }, /TypeError: keyCertPairs must be an array/); |
| 86 | + }); |
| 87 | + |
| 88 | + it('fails if the first argument is a non-Buffer value', () => { |
| 89 | + assert.throws(() => { |
| 90 | + ServerCredentials.createSsl('test' as any, []); |
| 91 | + }, /TypeError: rootCerts must be null or a Buffer/); |
| 92 | + }); |
| 93 | + |
| 94 | + it('fails if the third argument is a non-boolean value', () => { |
| 95 | + assert.throws(() => { |
| 96 | + ServerCredentials.createSsl(ca, [], 'test' as any); |
| 97 | + }, /TypeError: checkClientCertificate must be a boolean/); |
| 98 | + }); |
| 99 | + |
| 100 | + it('fails if the array elements are not objects', () => { |
| 101 | + assert.throws(() => { |
| 102 | + ServerCredentials.createSsl(ca, ['test'] as any); |
| 103 | + }, /TypeError: keyCertPair\[0\] must be an object/); |
| 104 | + |
| 105 | + assert.throws(() => { |
| 106 | + ServerCredentials.createSsl(ca, [null] as any); |
| 107 | + }, /TypeError: keyCertPair\[0\] must be an object/); |
| 108 | + }); |
| 109 | + |
| 110 | + it('fails if the object does not have a Buffer private key', () => { |
| 111 | + const keyCertPairs: any = [{private_key: 'test', cert_chain: cert}]; |
| 112 | + |
| 113 | + assert.throws(() => { |
| 114 | + ServerCredentials.createSsl(null, keyCertPairs); |
| 115 | + }, /TypeError: keyCertPair\[0\].private_key must be a Buffer/); |
| 116 | + }); |
| 117 | + |
| 118 | + it('fails if the object does not have a Buffer cert chain', () => { |
| 119 | + const keyCertPairs: any = [{private_key: key, cert_chain: 'test'}]; |
| 120 | + |
| 121 | + assert.throws(() => { |
| 122 | + ServerCredentials.createSsl(null, keyCertPairs); |
| 123 | + }, /TypeError: keyCertPair\[0\].cert_chain must be a Buffer/); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments