server_credentials_spec.rb 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. def load_test_certs
  31. test_root = File.join(File.dirname(__FILE__), 'testdata')
  32. files = ['ca.pem', 'server1.key', 'server1.pem']
  33. contents = files.map { |f| File.open(File.join(test_root, f)).read }
  34. [contents[0], [{ private_key: contents[1], cert_chain: contents[2] }], false]
  35. end
  36. describe GRPC::Core::ServerCredentials do
  37. Creds = GRPC::Core::ServerCredentials
  38. describe '#new' do
  39. it 'can be constructed from a fake CA PEM, server PEM and a server key' do
  40. creds = Creds.new('a', [{ private_key: 'a', cert_chain: 'b' }], false)
  41. expect(creds).to_not be_nil
  42. end
  43. it 'can be constructed using the test certificates' do
  44. certs = load_test_certs
  45. expect { Creds.new(*certs) }.not_to raise_error
  46. end
  47. it 'cannot be constructed without a nil key_cert pair array' do
  48. root_cert, _, _ = load_test_certs
  49. blk = proc do
  50. Creds.new(root_cert, nil, false)
  51. end
  52. expect(&blk).to raise_error
  53. end
  54. it 'cannot be constructed without any key_cert pairs' do
  55. root_cert, _, _ = load_test_certs
  56. blk = proc do
  57. Creds.new(root_cert, [], false)
  58. end
  59. expect(&blk).to raise_error
  60. end
  61. it 'cannot be constructed without a server cert chain' do
  62. root_cert, server_key, _ = load_test_certs
  63. blk = proc do
  64. Creds.new(root_cert,
  65. [{ server_key: server_key, cert_chain: nil }],
  66. false)
  67. end
  68. expect(&blk).to raise_error
  69. end
  70. it 'cannot be constructed without a server key' do
  71. root_cert, _, _ = load_test_certs
  72. blk = proc do
  73. Creds.new(root_cert,
  74. [{ server_key: nil, cert_chain: cert_chain }])
  75. end
  76. expect(&blk).to raise_error
  77. end
  78. it 'can be constructed without a root_cret' do
  79. _, cert_pairs, _ = load_test_certs
  80. blk = proc { Creds.new(nil, cert_pairs, false) }
  81. expect(&blk).to_not raise_error
  82. end
  83. end
  84. end