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 17describe GRPC::Core::ChannelCredentials do 18 ChannelCredentials = GRPC::Core::ChannelCredentials 19 CallCredentials = GRPC::Core::CallCredentials 20 21 def load_test_certs 22 test_root = File.join(File.dirname(__FILE__), 'testdata') 23 files = ['ca.pem', 'server1.key', 'server1.pem'] 24 files.map { |f| File.open(File.join(test_root, f)).read } 25 end 26 27 describe '#new' do 28 it 'can be constructed with fake inputs' do 29 blk = proc { ChannelCredentials.new('root_certs', 'key', 'cert') } 30 expect(&blk).not_to raise_error 31 end 32 33 it 'it can be constructed using specific test certificates' do 34 certs = load_test_certs 35 expect { ChannelCredentials.new(*certs) }.not_to raise_error 36 end 37 38 it 'can be constructed with server roots certs only' do 39 root_cert, _, _ = load_test_certs 40 expect { ChannelCredentials.new(root_cert) }.not_to raise_error 41 end 42 43 it 'can be constructed with a nil server roots' do 44 _, client_key, client_chain = load_test_certs 45 blk = proc { ChannelCredentials.new(nil, client_key, client_chain) } 46 expect(&blk).not_to raise_error 47 end 48 49 it 'can be constructed with no params' do 50 blk = proc { ChannelCredentials.new(nil) } 51 expect(&blk).not_to raise_error 52 end 53 54 it 'fails gracefully with constructed with a nil private key' do 55 blk = proc { GRPC::Core::ChannelCredentials.new(nil, nil, '') } 56 expect(&blk).to raise_error 57 end 58 59 it 'fails gracefully with constructed with a nil cert chain' do 60 blk = proc { GRPC::Core::ChannelCredentials.new(nil, '', nil) } 61 expect(&blk).to raise_error 62 end 63 end 64 65 describe '#compose' do 66 it 'can compose with a CallCredentials' do 67 certs = load_test_certs 68 channel_creds = ChannelCredentials.new(*certs) 69 auth_proc = proc { { 'plugin_key' => 'plugin_value' } } 70 call_creds = CallCredentials.new auth_proc 71 expect { channel_creds.compose call_creds }.not_to raise_error 72 end 73 74 it 'can compose with multiple CallCredentials' do 75 certs = load_test_certs 76 channel_creds = ChannelCredentials.new(*certs) 77 auth_proc = proc { { 'plugin_key' => 'plugin_value' } } 78 call_creds1 = CallCredentials.new auth_proc 79 call_creds2 = CallCredentials.new auth_proc 80 expect do 81 channel_creds.compose(call_creds1, call_creds2) 82 end.not_to raise_error 83 end 84 85 it 'cannot compose with ChannelCredentials' do 86 certs = load_test_certs 87 channel_creds1 = ChannelCredentials.new(*certs) 88 channel_creds2 = ChannelCredentials.new(*certs) 89 expect { channel_creds1.compose channel_creds2 }.to raise_error(TypeError) 90 end 91 end 92end 93