Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
| 3 | // Copyright 2015, Google Inc. |
| 4 | // 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 | |
| 34 | using System; |
| 35 | using System.Collections.Generic; |
| 36 | using System.IO; |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 37 | using System.Linq; |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 38 | using System.Threading; |
| 39 | using System.Threading.Tasks; |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 40 | using Grpc.Core; |
| 41 | using Grpc.Core.Utils; |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 42 | using Grpc.Testing; |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 43 | using NUnit.Framework; |
| 44 | |
| 45 | namespace 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 Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 53 | const string Host = "localhost"; |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 54 | Server server; |
| 55 | Channel channel; |
Jan Tattermusch | 809148d | 2016-03-22 15:09:41 -0700 | [diff] [blame] | 56 | TestService.TestServiceClient client; |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 57 | |
| 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 Tattermusch | d27dfa7 | 2015-08-04 18:10:54 -0700 | [diff] [blame] | 66 | var serverCredentials = new SslServerCredentials(new[] { keyCertPair }, rootCert, true); |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 67 | var clientCredentials = new SslCredentials(rootCert, keyCertPair); |
| 68 | |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 69 | server = new Server |
| 70 | { |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 71 | Services = { TestService.BindService(new TestServiceImpl()) }, |
| 72 | Ports = { { Host, ServerPort.PickUnused, serverCredentials } } |
Jan Tattermusch | 021df8a | 2015-08-04 20:31:11 -0700 | [diff] [blame] | 73 | }; |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 74 | server.Start(); |
| 75 | |
| 76 | var options = new List<ChannelOption> |
| 77 | { |
| 78 | new ChannelOption(ChannelOptions.SslTargetNameOverride, TestCredentials.DefaultHostOverride) |
| 79 | }; |
| 80 | |
Jan Tattermusch | 31ba063 | 2015-08-04 22:02:55 -0700 | [diff] [blame] | 81 | channel = new Channel(Host, server.Ports.Single().BoundPort, clientCredentials, options); |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 82 | client = TestService.NewClient(channel); |
| 83 | } |
| 84 | |
| 85 | [TestFixtureTearDown] |
| 86 | public void Cleanup() |
| 87 | { |
Jan Tattermusch | 2b35795 | 2015-08-20 14:54:33 -0700 | [diff] [blame] | 88 | channel.ShutdownAsync().Wait(); |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 89 | server.ShutdownAsync().Wait(); |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | [Test] |
| 93 | public void AuthenticatedClientAndServer() |
| 94 | { |
Jan Tattermusch | 8644aea | 2015-08-03 10:21:18 -0700 | [diff] [blame] | 95 | var response = client.UnaryCall(new SimpleRequest { ResponseSize = 10 }); |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 96 | Assert.AreEqual(10, response.Payload.Body.Length); |
| 97 | } |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 98 | } |
| 99 | } |