channel_credentials_spec.rb 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright 2015, Google Inc.
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. require 'grpc'
  30. describe GRPC::Core::ChannelCredentials do
  31. ChannelCredentials = GRPC::Core::ChannelCredentials
  32. CallCredentials = GRPC::Core::CallCredentials
  33. def load_test_certs
  34. test_root = File.join(File.dirname(__FILE__), 'testdata')
  35. files = ['ca.pem', 'server1.pem', 'server1.key']
  36. files.map { |f| File.open(File.join(test_root, f)).read }
  37. end
  38. describe '#new' do
  39. it 'can be constructed with fake inputs' do
  40. blk = proc { ChannelCredentials.new('root_certs', 'key', 'cert') }
  41. expect(&blk).not_to raise_error
  42. end
  43. it 'it can be constructed using specific test certificates' do
  44. certs = load_test_certs
  45. expect { ChannelCredentials.new(*certs) }.not_to raise_error
  46. end
  47. it 'can be constructed with server roots certs only' do
  48. root_cert, _, _ = load_test_certs
  49. expect { ChannelCredentials.new(root_cert) }.not_to raise_error
  50. end
  51. it 'can be constructed with a nil server roots' do
  52. _, client_key, client_chain = load_test_certs
  53. blk = proc { ChannelCredentials.new(nil, client_key, client_chain) }
  54. expect(&blk).not_to raise_error
  55. end
  56. it 'can be constructed with no params' do
  57. blk = proc { ChannelCredentials.new(nil) }
  58. expect(&blk).not_to raise_error
  59. end
  60. end
  61. describe '#compose' do
  62. it 'can compose with a CallCredentials' do
  63. certs = load_test_certs
  64. channel_creds = ChannelCredentials.new(*certs)
  65. auth_proc = proc { { 'plugin_key' => 'plugin_value' } }
  66. call_creds = CallCredentials.new auth_proc
  67. expect { channel_creds.compose call_creds }.not_to raise_error
  68. end
  69. it 'can compose with multiple CallCredentials' do
  70. certs = load_test_certs
  71. channel_creds = ChannelCredentials.new(*certs)
  72. auth_proc = proc { { 'plugin_key' => 'plugin_value' } }
  73. call_creds1 = CallCredentials.new auth_proc
  74. call_creds2 = CallCredentials.new auth_proc
  75. expect do
  76. channel_creds.compose(call_creds1, call_creds2)
  77. end.not_to raise_error
  78. end
  79. it 'cannot compose with ChannelCredentials' do
  80. certs = load_test_certs
  81. channel_creds1 = ChannelCredentials.new(*certs)
  82. channel_creds2 = ChannelCredentials.new(*certs)
  83. expect { channel_creds1.compose channel_creds2 }.to raise_error(TypeError)
  84. end
  85. end
  86. end