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 64 it 'can be constructed with a fallback credential' do 65 blk = proc { 66 fallback = GRPC::Core::ChannelCredentials.new 67 GRPC::Core::XdsChannelCredentials.new(fallback) 68 } 69 expect(&blk).not_to raise_error 70 end 71 72 it 'fails gracefully constructed with nil' do 73 blk = proc { 74 GRPC::Core::XdsChannelCredentials.new(nil) 75 } 76 expect(&blk).to raise_error TypeError, /expected grpc_channel_credentials/ 77 end 78 79 it 'fails gracefully constructed with a non-C-extension object' do 80 blk = proc { 81 not_a_fallback = 100 82 GRPC::Core::XdsChannelCredentials.new(not_a_fallback) 83 } 84 expect(&blk).to raise_error TypeError, /expected grpc_channel_credentials/ 85 end 86 87 it 'fails gracefully constructed with a non-ChannelCredentials object' do 88 blk = proc { 89 not_a_fallback = GRPC::Core::Channel.new('dummy_host', nil, 90 :this_channel_is_insecure) 91 GRPC::Core::XdsChannelCredentials.new(not_a_fallback) 92 } 93 expect(&blk).to raise_error TypeError, /expected grpc_channel_credentials/ 94 end 95 end 96 97 describe '#compose' do 98 it 'can compose with a CallCredentials' do 99 certs = load_test_certs 100 channel_creds = ChannelCredentials.new(*certs) 101 auth_proc = proc { { 'plugin_key' => 'plugin_value' } } 102 call_creds = CallCredentials.new auth_proc 103 expect { channel_creds.compose call_creds }.not_to raise_error 104 end 105 106 it 'can compose with multiple CallCredentials' do 107 certs = load_test_certs 108 channel_creds = ChannelCredentials.new(*certs) 109 auth_proc = proc { { 'plugin_key' => 'plugin_value' } } 110 call_creds1 = CallCredentials.new auth_proc 111 call_creds2 = CallCredentials.new auth_proc 112 expect do 113 channel_creds.compose(call_creds1, call_creds2) 114 end.not_to raise_error 115 end 116 117 it 'cannot compose with ChannelCredentials' do 118 certs = load_test_certs 119 channel_creds1 = ChannelCredentials.new(*certs) 120 channel_creds2 = ChannelCredentials.new(*certs) 121 expect { channel_creds1.compose channel_creds2 }.to raise_error(TypeError) 122 end 123 end 124end 125