SslCredentialsTest.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #region Copyright notice and license
  2. // Copyright 2015-2016 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using Google.Protobuf;
  23. using Grpc.Core;
  24. using Grpc.Core.Utils;
  25. using Grpc.Testing;
  26. using NUnit.Framework;
  27. namespace Grpc.IntegrationTesting
  28. {
  29. /// <summary>
  30. /// Test SSL credentials where server authenticates client
  31. /// and client authenticates the server.
  32. /// </summary>
  33. public class SslCredentialsTest
  34. {
  35. const string Host = "localhost";
  36. Server server;
  37. Channel channel;
  38. TestService.TestServiceClient client;
  39. [TestFixtureSetUp]
  40. public void Init()
  41. {
  42. var rootCert = File.ReadAllText(TestCredentials.ClientCertAuthorityPath);
  43. var keyCertPair = new KeyCertificatePair(
  44. File.ReadAllText(TestCredentials.ServerCertChainPath),
  45. File.ReadAllText(TestCredentials.ServerPrivateKeyPath));
  46. var serverCredentials = new SslServerCredentials(new[] { keyCertPair }, rootCert, true);
  47. var clientCredentials = new SslCredentials(rootCert, keyCertPair);
  48. // Disable SO_REUSEPORT to prevent https://github.com/grpc/grpc/issues/10755
  49. server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
  50. {
  51. Services = { TestService.BindService(new SslCredentialsTestServiceImpl()) },
  52. Ports = { { Host, ServerPort.PickUnused, serverCredentials } }
  53. };
  54. server.Start();
  55. var options = new List<ChannelOption>
  56. {
  57. new ChannelOption(ChannelOptions.SslTargetNameOverride, TestCredentials.DefaultHostOverride)
  58. };
  59. channel = new Channel(Host, server.Ports.Single().BoundPort, clientCredentials, options);
  60. client = new TestService.TestServiceClient(channel);
  61. }
  62. [TestFixtureTearDown]
  63. public void Cleanup()
  64. {
  65. channel.ShutdownAsync().Wait();
  66. server.ShutdownAsync().Wait();
  67. }
  68. [Test]
  69. public void AuthenticatedClientAndServer()
  70. {
  71. var response = client.UnaryCall(new SimpleRequest { ResponseSize = 10 });
  72. Assert.AreEqual(10, response.Payload.Body.Length);
  73. }
  74. [Test]
  75. public async Task AuthContextIsPopulated()
  76. {
  77. var call = client.StreamingInputCall();
  78. await call.RequestStream.CompleteAsync();
  79. var response = await call.ResponseAsync;
  80. Assert.AreEqual(12345, response.AggregatedPayloadSize);
  81. }
  82. private class SslCredentialsTestServiceImpl : TestService.TestServiceBase
  83. {
  84. public override async Task<SimpleResponse> UnaryCall(SimpleRequest request, ServerCallContext context)
  85. {
  86. return new SimpleResponse { Payload = CreateZerosPayload(request.ResponseSize) };
  87. }
  88. public override async Task<StreamingInputCallResponse> StreamingInputCall(IAsyncStreamReader<StreamingInputCallRequest> requestStream, ServerCallContext context)
  89. {
  90. var authContext = context.AuthContext;
  91. await requestStream.ForEachAsync(async request => {});
  92. Assert.IsTrue(authContext.IsPeerAuthenticated);
  93. Assert.AreEqual("x509_subject_alternative_name", authContext.PeerIdentityPropertyName);
  94. Assert.IsTrue(authContext.PeerIdentity.Count() > 0);
  95. Assert.AreEqual("ssl", authContext.FindPropertiesByName("transport_security_type").First().Value);
  96. return new StreamingInputCallResponse { AggregatedPayloadSize = 12345 };
  97. }
  98. private static Payload CreateZerosPayload(int size)
  99. {
  100. return new Payload { Body = ByteString.CopyFrom(new byte[size]) };
  101. }
  102. }
  103. }
  104. }