blob: 60b9cf4e0b7385ca802804e43c78fcdd3070746a [file] [log] [blame]
Jan Tattermuschb0829eb2015-03-03 09:30:55 -08001#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
34using System;
35using System.Collections.Generic;
36using System.Diagnostics;
37using System.IO;
Jan Tattermusch10e85f12016-04-09 13:16:37 -070038using System.Reflection;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080039using System.Text.RegularExpressions;
40using System.Threading.Tasks;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080041using Grpc.Core;
42using Grpc.Core.Utils;
43using NUnit.Framework;
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080044
45namespace 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 Tattermuschb3236082016-04-08 17:08:57 -070054 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 Tattermuschb0829eb2015-03-03 09:30:55 -080077
Jan Tattermuschbeffc772015-10-22 13:28:22 -070078 public static SslCredentials CreateSslCredentials()
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080079 {
Jan Tattermuschbeffc772015-10-22 13:28:22 -070080 return new SslCredentials(File.ReadAllText(ClientCertAuthorityPath));
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080081 }
82
Jan Tattermuschbeffc772015-10-22 13:28:22 -070083 public static SslServerCredentials CreateSslServerCredentials()
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080084 {
85 var keyCertPair = new KeyCertificatePair(
86 File.ReadAllText(ServerCertChainPath),
87 File.ReadAllText(ServerPrivateKeyPath));
Jan Tattermuscheea59552015-07-23 22:05:32 -070088 return new SslServerCredentials(new[] { keyCertPair });
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080089 }
Jan Tattermuschb3236082016-04-08 17:08:57 -070090
91 private static string GetPath(string relativePath)
92 {
Jan Tattermusch317b8ac2016-06-16 09:36:11 -070093 var assemblyDir = Path.GetDirectoryName(typeof(TestCredentials).GetTypeInfo().Assembly.Location);
Jan Tattermusch10e85f12016-04-09 13:16:37 -070094 return Path.Combine(assemblyDir, relativePath);
Jan Tattermuschb3236082016-04-08 17:08:57 -070095 }
Jan Tattermuschb0829eb2015-03-03 09:30:55 -080096 }
97}