forked from grpc/grpc-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrpcProtocolHelpersTests.cs
111 lines (90 loc) · 3.87 KB
/
GrpcProtocolHelpersTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#region Copyright notice and license
// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System.Security.Cryptography.X509Certificates;
using Grpc.AspNetCore.Server.Internal;
using Grpc.Tests.Shared;
using NUnit.Framework;
namespace Grpc.AspNetCore.Server.Tests;
[TestFixture]
public class GrpcProtocolHelpersTests
{
[Test]
public void CreateAuthContext_CertWithAlternativeNames_UseAlternativeNamesAsPeerIdentity()
{
// Arrange
var certificate = LoadCertificate(TestHelpers.ResolvePath(@"Certs/outlookcom.crt"));
// Act
var authContext = GrpcProtocolHelpers.CreateAuthContext(certificate);
// Assert
Assert.AreEqual(true, authContext.IsPeerAuthenticated);
Assert.AreEqual(GrpcProtocolConstants.X509SubjectAlternativeNameKey, authContext.PeerIdentityPropertyName);
var identity = authContext.PeerIdentity.ToList();
Assert.AreEqual(23, identity.Count);
Assert.AreEqual(GrpcProtocolConstants.X509SubjectAlternativeNameKey, identity[0].Name);
Assert.AreEqual("*.internal.outlook.com", identity[0].Value);
var allProperties = authContext.Properties.ToList();
Assert.AreEqual(24, allProperties.Count);
var commonName = authContext.FindPropertiesByName(GrpcProtocolConstants.X509CommonNameKey).Single();
Assert.AreEqual(GrpcProtocolConstants.X509CommonNameKey, commonName.Name);
Assert.AreEqual("outlook.com", commonName.Value);
}
[Test]
public void CreateAuthContext_CertWithCommonName_UseCommonNameAsPeerIdentity()
{
// Arrange
var certificate = LoadCertificate(TestHelpers.ResolvePath(@"Certs/client.crt"));
// Act
var authContext = GrpcProtocolHelpers.CreateAuthContext(certificate);
// Assert
Assert.AreEqual(true, authContext.IsPeerAuthenticated);
Assert.AreEqual(GrpcProtocolConstants.X509CommonNameKey, authContext.PeerIdentityPropertyName);
var identity = authContext.PeerIdentity.ToList();
Assert.AreEqual(1, identity.Count);
Assert.AreEqual(GrpcProtocolConstants.X509CommonNameKey, identity[0].Name);
Assert.AreEqual("localhost", identity[0].Value);
var allProperties = authContext.Properties.ToList();
Assert.AreEqual(1, allProperties.Count);
}
[TestCase("1H", 36000000000, true)]
[TestCase("1M", 600000000, true)]
[TestCase("1S", 10000000, true)]
[TestCase("1m", 10000, true)]
[TestCase("1u", 10, true)]
[TestCase("100n", 1, true)]
[TestCase("1n", 0, true)]
[TestCase("0S", 0, true)]
[TestCase("", 0, false)]
[TestCase("5", 0, false)]
[TestCase("M", 0, false)]
public void TryDecodeTimeout_WithVariousUnits_ShouldMatchExpected(string timeout, long expectedTicks, bool expectedSuccesfullyDecoded)
{
// Arrange
var expectedTimespan = new TimeSpan(expectedTicks);
// Act
var successfullyDecoded = GrpcProtocolHelpers.TryDecodeTimeout(timeout, out var timeSpan);
// Assert
Assert.AreEqual(expectedSuccesfullyDecoded, successfullyDecoded);
Assert.AreEqual(expectedTimespan, timeSpan);
}
public static X509Certificate2 LoadCertificate(string path)
{
#if NET9_0_OR_GREATER
return X509CertificateLoader.LoadCertificateFromFile(path);
#else
return new X509Certificate2(path);
#endif
}
}