SslCredentialsTest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #region Copyright notice and license
  2. // Copyright 2015-2016, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #endregion
  31. using System;
  32. using System.Collections.Generic;
  33. using System.IO;
  34. using System.Linq;
  35. using System.Threading;
  36. using System.Threading.Tasks;
  37. using Google.Protobuf;
  38. using Grpc.Core;
  39. using Grpc.Core.Utils;
  40. using Grpc.Testing;
  41. using NUnit.Framework;
  42. namespace Grpc.IntegrationTesting
  43. {
  44. /// <summary>
  45. /// Test SSL credentials where server authenticates client
  46. /// and client authenticates the server.
  47. /// </summary>
  48. public class SslCredentialsTest
  49. {
  50. const string Host = "localhost";
  51. Server server;
  52. Channel channel;
  53. TestService.TestServiceClient client;
  54. [TestFixtureSetUp]
  55. public void Init()
  56. {
  57. var rootCert = File.ReadAllText(TestCredentials.ClientCertAuthorityPath);
  58. var keyCertPair = new KeyCertificatePair(
  59. File.ReadAllText(TestCredentials.ServerCertChainPath),
  60. File.ReadAllText(TestCredentials.ServerPrivateKeyPath));
  61. var serverCredentials = new SslServerCredentials(new[] { keyCertPair }, rootCert, true);
  62. var clientCredentials = new SslCredentials(rootCert, keyCertPair);
  63. server = new Server
  64. {
  65. Services = { TestService.BindService(new SslCredentialsTestServiceImpl()) },
  66. Ports = { { Host, ServerPort.PickUnused, serverCredentials } }
  67. };
  68. server.Start();
  69. var options = new List<ChannelOption>
  70. {
  71. new ChannelOption(ChannelOptions.SslTargetNameOverride, TestCredentials.DefaultHostOverride)
  72. };
  73. channel = new Channel(Host, server.Ports.Single().BoundPort, clientCredentials, options);
  74. client = new TestService.TestServiceClient(channel);
  75. }
  76. [TestFixtureTearDown]
  77. public void Cleanup()
  78. {
  79. channel.ShutdownAsync().Wait();
  80. server.ShutdownAsync().Wait();
  81. }
  82. [Test]
  83. public void AuthenticatedClientAndServer()
  84. {
  85. var response = client.UnaryCall(new SimpleRequest { ResponseSize = 10 });
  86. Assert.AreEqual(10, response.Payload.Body.Length);
  87. }
  88. [Test]
  89. public async Task AuthContextIsPopulated()
  90. {
  91. var call = client.StreamingInputCall();
  92. await call.RequestStream.CompleteAsync();
  93. var response = await call.ResponseAsync;
  94. Assert.AreEqual(12345, response.AggregatedPayloadSize);
  95. }
  96. private class SslCredentialsTestServiceImpl : TestService.TestServiceBase
  97. {
  98. public override async Task<SimpleResponse> UnaryCall(SimpleRequest request, ServerCallContext context)
  99. {
  100. return new SimpleResponse { Payload = CreateZerosPayload(request.ResponseSize) };
  101. }
  102. public override async Task<StreamingInputCallResponse> StreamingInputCall(IAsyncStreamReader<StreamingInputCallRequest> requestStream, ServerCallContext context)
  103. {
  104. var authContext = context.AuthContext;
  105. await requestStream.ForEachAsync(async request => {});
  106. Assert.IsTrue(authContext.IsPeerAuthenticated);
  107. Assert.AreEqual("x509_subject_alternative_name", authContext.PeerIdentityPropertyName);
  108. Assert.IsTrue(authContext.PeerIdentity.Count() > 0);
  109. Assert.AreEqual("ssl", authContext.FindPropertiesByName("transport_security_type").First().Value);
  110. return new StreamingInputCallResponse { AggregatedPayloadSize = 12345 };
  111. }
  112. private static Payload CreateZerosPayload(int size)
  113. {
  114. return new Payload { Body = ByteString.CopyFrom(new byte[size]) };
  115. }
  116. }
  117. }
  118. }