blob: 6e19610c8556f6c5e0f60d90fe5d406291ee568f [file] [log] [blame]
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -08004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -080010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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.
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -080016
17#endregion
18
19using System;
20using System.Collections.Generic;
21using NUnit.Framework;
22using Grpc.Core;
23using System.Linq;
24
25namespace Grpc.Core.Tests
26{
27 public class AuthContextTest
28 {
29 [Test]
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]
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]
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}