1 #region Copyright notice and license
2 
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #endregion
18 
19 using System;
20 using System.Collections.Generic;
21 using NUnit.Framework;
22 using Grpc.Core;
23 using System.Linq;
24 
25 namespace Grpc.Core.Tests
26 {
27     public class AuthContextTest
28     {
29         [Test]
EmptyContext()30         public void EmptyContext()
31         {
32             var context = new AuthContext(null, new Dictionary<string, List<AuthProperty>>());
33             Assert.IsFalse(context.IsPeerAuthenticated);
34             Assert.IsNull(context.PeerIdentityPropertyName);
35             Assert.AreEqual(0, context.PeerIdentity.Count());
36             Assert.AreEqual(0, context.Properties.Count());
37             Assert.AreEqual(0, context.FindPropertiesByName("nonexistent").Count());
38         }
39 
40         [Test]
AuthenticatedContext()41         public void AuthenticatedContext()
42         {
43             var property1 = AuthProperty.Create("abc", new byte[] { 68, 69, 70 });
44             var context = new AuthContext("some_identity", new Dictionary<string, List<AuthProperty>>
45             {
46                 {"some_identity", new List<AuthProperty> {property1}}
47             });
48             Assert.IsTrue(context.IsPeerAuthenticated);
49             Assert.AreEqual("some_identity", context.PeerIdentityPropertyName);
50             Assert.AreEqual(1, context.PeerIdentity.Count());
51         }
52 
53         [Test]
FindPropertiesByName()54         public void FindPropertiesByName()
55         {
56             var property1 = AuthProperty.Create("abc", new byte[] {68, 69, 70});
57             var property2 = AuthProperty.Create("abc", new byte[] {71, 72, 73 });
58             var property3 = AuthProperty.Create("abc", new byte[] {});
59             var context = new AuthContext(null, new Dictionary<string, List<AuthProperty>>
60             {
61                 {"existent", new List<AuthProperty> {property1, property2}},
62                 {"foobar", new List<AuthProperty> {property3}},
63             });
64             Assert.AreEqual(3, context.Properties.Count());
65             Assert.AreEqual(0, context.FindPropertiesByName("nonexistent").Count());
66 
67             var existentProperties = new List<AuthProperty>(context.FindPropertiesByName("existent"));
68             Assert.AreEqual(2, existentProperties.Count);
69         }
70     }
71 }
72