blob: f85e272711f96dd8e6b921b149bf0cfc7fbdc5c2 [file] [log] [blame]
Jan Tattermuscheea59552015-07-23 22:05:32 -07001#region Copyright notice and license
2
Jan Tattermuschacb842c2016-03-25 16:54:14 -07003// Copyright 2015-2016, Google Inc.
Jan Tattermuscheea59552015-07-23 22:05:32 -07004// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32#endregion
33
34using System;
35using System.Collections.Generic;
36using System.IO;
Jan Tattermusch31ba0632015-08-04 22:02:55 -070037using System.Linq;
Jan Tattermuscheea59552015-07-23 22:05:32 -070038using System.Threading;
39using System.Threading.Tasks;
Jan Tattermuscheea59552015-07-23 22:05:32 -070040using Grpc.Core;
41using Grpc.Core.Utils;
Jan Tattermusch8644aea2015-08-03 10:21:18 -070042using Grpc.Testing;
Jan Tattermuscheea59552015-07-23 22:05:32 -070043using NUnit.Framework;
44
45namespace Grpc.IntegrationTesting
46{
47 /// <summary>
48 /// Test SSL credentials where server authenticates client
49 /// and client authenticates the server.
50 /// </summary>
51 public class SslCredentialsTest
52 {
Jan Tattermusch31ba0632015-08-04 22:02:55 -070053 const string Host = "localhost";
Jan Tattermuscheea59552015-07-23 22:05:32 -070054 Server server;
55 Channel channel;
Jan Tattermusch809148d2016-03-22 15:09:41 -070056 TestService.TestServiceClient client;
Jan Tattermuscheea59552015-07-23 22:05:32 -070057
58 [TestFixtureSetUp]
59 public void Init()
60 {
61 var rootCert = File.ReadAllText(TestCredentials.ClientCertAuthorityPath);
62 var keyCertPair = new KeyCertificatePair(
63 File.ReadAllText(TestCredentials.ServerCertChainPath),
64 File.ReadAllText(TestCredentials.ServerPrivateKeyPath));
65
Jan Tattermuschd27dfa72015-08-04 18:10:54 -070066 var serverCredentials = new SslServerCredentials(new[] { keyCertPair }, rootCert, true);
Jan Tattermuscheea59552015-07-23 22:05:32 -070067 var clientCredentials = new SslCredentials(rootCert, keyCertPair);
68
Jan Tattermusch021df8a2015-08-04 20:31:11 -070069 server = new Server
70 {
Jan Tattermusch31ba0632015-08-04 22:02:55 -070071 Services = { TestService.BindService(new TestServiceImpl()) },
72 Ports = { { Host, ServerPort.PickUnused, serverCredentials } }
Jan Tattermusch021df8a2015-08-04 20:31:11 -070073 };
Jan Tattermuscheea59552015-07-23 22:05:32 -070074 server.Start();
75
76 var options = new List<ChannelOption>
77 {
78 new ChannelOption(ChannelOptions.SslTargetNameOverride, TestCredentials.DefaultHostOverride)
79 };
80
Jan Tattermusch31ba0632015-08-04 22:02:55 -070081 channel = new Channel(Host, server.Ports.Single().BoundPort, clientCredentials, options);
Jan Tattermuschf41ebc32016-06-22 12:47:14 -070082 client = new TestService.TestServiceClient(channel);
Jan Tattermuscheea59552015-07-23 22:05:32 -070083 }
84
85 [TestFixtureTearDown]
86 public void Cleanup()
87 {
Jan Tattermusch2b357952015-08-20 14:54:33 -070088 channel.ShutdownAsync().Wait();
Jan Tattermuscheea59552015-07-23 22:05:32 -070089 server.ShutdownAsync().Wait();
Jan Tattermuscheea59552015-07-23 22:05:32 -070090 }
91
92 [Test]
93 public void AuthenticatedClientAndServer()
94 {
Jan Tattermusch8644aea2015-08-03 10:21:18 -070095 var response = client.UnaryCall(new SimpleRequest { ResponseSize = 10 });
Jan Tattermuscheea59552015-07-23 22:05:32 -070096 Assert.AreEqual(10, response.Payload.Body.Length);
97 }
Jan Tattermuscheea59552015-07-23 22:05:32 -070098 }
99}