Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [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.Diagnostics; |
| 37 | using System.IO; |
Jan Tattermusch | 10e85f1 | 2016-04-09 13:16:37 -0700 | [diff] [blame] | 38 | using System.Reflection; |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 39 | using System.Text.RegularExpressions; |
| 40 | using System.Threading.Tasks; |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 41 | using Grpc.Core; |
| 42 | using Grpc.Core.Utils; |
| 43 | using NUnit.Framework; |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 44 | |
| 45 | namespace Grpc.IntegrationTesting |
| 46 | { |
| 47 | /// <summary> |
| 48 | /// SSL Credentials for testing. |
| 49 | /// </summary> |
| 50 | public static class TestCredentials |
| 51 | { |
| 52 | public const string DefaultHostOverride = "foo.test.google.fr"; |
| 53 | |
Jan Tattermusch | b323608 | 2016-04-08 17:08:57 -0700 | [diff] [blame] | 54 | public static string ClientCertAuthorityPath |
| 55 | { |
| 56 | get |
| 57 | { |
| 58 | return GetPath("data/ca.pem"); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public static string ServerCertChainPath |
| 63 | { |
| 64 | get |
| 65 | { |
| 66 | return GetPath("data/server1.pem"); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public static string ServerPrivateKeyPath |
| 71 | { |
| 72 | get |
| 73 | { |
| 74 | return GetPath("data/server1.key"); |
| 75 | } |
| 76 | } |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 77 | |
Jan Tattermusch | beffc77 | 2015-10-22 13:28:22 -0700 | [diff] [blame] | 78 | public static SslCredentials CreateSslCredentials() |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 79 | { |
Jan Tattermusch | beffc77 | 2015-10-22 13:28:22 -0700 | [diff] [blame] | 80 | return new SslCredentials(File.ReadAllText(ClientCertAuthorityPath)); |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Jan Tattermusch | beffc77 | 2015-10-22 13:28:22 -0700 | [diff] [blame] | 83 | public static SslServerCredentials CreateSslServerCredentials() |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 84 | { |
| 85 | var keyCertPair = new KeyCertificatePair( |
| 86 | File.ReadAllText(ServerCertChainPath), |
| 87 | File.ReadAllText(ServerPrivateKeyPath)); |
Jan Tattermusch | eea5955 | 2015-07-23 22:05:32 -0700 | [diff] [blame] | 88 | return new SslServerCredentials(new[] { keyCertPair }); |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 89 | } |
Jan Tattermusch | b323608 | 2016-04-08 17:08:57 -0700 | [diff] [blame] | 90 | |
| 91 | private static string GetPath(string relativePath) |
| 92 | { |
Jan Tattermusch | 317b8ac | 2016-06-16 09:36:11 -0700 | [diff] [blame^] | 93 | var assemblyDir = Path.GetDirectoryName(typeof(TestCredentials).GetTypeInfo().Assembly.Location); |
Jan Tattermusch | 10e85f1 | 2016-04-09 13:16:37 -0700 | [diff] [blame] | 94 | return Path.Combine(assemblyDir, relativePath); |
Jan Tattermusch | b323608 | 2016-04-08 17:08:57 -0700 | [diff] [blame] | 95 | } |
Jan Tattermusch | b0829eb | 2015-03-03 09:30:55 -0800 | [diff] [blame] | 96 | } |
| 97 | } |