channel_credentials_spec.rb 2.8 KB

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