1# Copyright 2015 gRPC authors. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15require 'spec_helper' 16 17def load_test_certs 18 test_root = File.join(File.dirname(__FILE__), 'testdata') 19 files = ['ca.pem', 'server1.key', 'server1.pem'] 20 contents = files.map { |f| File.open(File.join(test_root, f)).read } 21 [contents[0], [{ private_key: contents[1], cert_chain: contents[2] }], false] 22end 23 24describe GRPC::Core::ServerCredentials do 25 Creds = GRPC::Core::ServerCredentials 26 27 describe '#new' do 28 it 'can be constructed from a fake CA PEM, server PEM and a server key' do 29 creds = Creds.new('a', [{ private_key: 'a', cert_chain: 'b' }], false) 30 expect(creds).to_not be_nil 31 end 32 33 it 'can be constructed using the test certificates' do 34 certs = load_test_certs 35 expect { Creds.new(*certs) }.not_to raise_error 36 end 37 38 it 'cannot be constructed without a nil key_cert pair array' do 39 root_cert, _, _ = load_test_certs 40 blk = proc do 41 Creds.new(root_cert, nil, false) 42 end 43 expect(&blk).to raise_error 44 end 45 46 it 'cannot be constructed without any key_cert pairs' do 47 root_cert, _, _ = load_test_certs 48 blk = proc do 49 Creds.new(root_cert, [], false) 50 end 51 expect(&blk).to raise_error 52 end 53 54 it 'cannot be constructed without a server cert chain' do 55 root_cert, server_key, _ = load_test_certs 56 blk = proc do 57 Creds.new(root_cert, 58 [{ server_key: server_key, cert_chain: nil }], 59 false) 60 end 61 expect(&blk).to raise_error 62 end 63 64 it 'cannot be constructed without a server key' do 65 root_cert, _, _ = load_test_certs 66 blk = proc do 67 Creds.new(root_cert, 68 [{ server_key: nil, cert_chain: cert_chain }]) 69 end 70 expect(&blk).to raise_error 71 end 72 73 it 'can be constructed without a root_cret' do 74 _, cert_pairs, _ = load_test_certs 75 blk = proc { Creds.new(nil, cert_pairs, false) } 76 expect(&blk).to_not raise_error 77 end 78 end 79end 80