Home | History | Annotate | Download | only in spec
      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 
     15 require 'spec_helper'
     16 
     17 describe 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   end
     54 
     55   describe '#compose' do
     56     it 'can compose with a CallCredentials' do
     57       certs = load_test_certs
     58       channel_creds = ChannelCredentials.new(*certs)
     59       auth_proc = proc { { 'plugin_key' => 'plugin_value' } }
     60       call_creds = CallCredentials.new auth_proc
     61       expect { channel_creds.compose call_creds }.not_to raise_error
     62     end
     63 
     64     it 'can compose with multiple CallCredentials' do
     65       certs = load_test_certs
     66       channel_creds = ChannelCredentials.new(*certs)
     67       auth_proc = proc { { 'plugin_key' => 'plugin_value' } }
     68       call_creds1 = CallCredentials.new auth_proc
     69       call_creds2 = CallCredentials.new auth_proc
     70       expect do
     71         channel_creds.compose(call_creds1, call_creds2)
     72       end.not_to raise_error
     73     end
     74 
     75     it 'cannot compose with ChannelCredentials' do
     76       certs = load_test_certs
     77       channel_creds1 = ChannelCredentials.new(*certs)
     78       channel_creds2 = ChannelCredentials.new(*certs)
     79       expect { channel_creds1.compose channel_creds2 }.to raise_error(TypeError)
     80     end
     81   end
     82 end
     83