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 Grpc.Core.Internal;
21 using NUnit.Framework;
22 
23 namespace Grpc.Core.Tests
24 {
25     public class ChannelCredentialsTest
26     {
27         [Test]
InsecureCredentials_IsNonComposable()28         public void InsecureCredentials_IsNonComposable()
29         {
30             Assert.IsFalse(ChannelCredentials.Insecure.IsComposable);
31         }
32 
33         [Test]
ChannelCredentials_CreateComposite()34         public void ChannelCredentials_CreateComposite()
35         {
36             var composite = ChannelCredentials.Create(new FakeChannelCredentials(true), new FakeCallCredentials());
37             Assert.IsFalse(composite.IsComposable);
38 
39             Assert.Throws(typeof(ArgumentNullException), () => ChannelCredentials.Create(null, new FakeCallCredentials()));
40             Assert.Throws(typeof(ArgumentNullException), () => ChannelCredentials.Create(new FakeChannelCredentials(true), null));
41 
42             // forbid composing non-composable
43             Assert.Throws(typeof(ArgumentException), () => ChannelCredentials.Create(new FakeChannelCredentials(false), new FakeCallCredentials()));
44         }
45 
46         [Test]
ChannelCredentials_NativeCredentialsAreReused()47         public void ChannelCredentials_NativeCredentialsAreReused()
48         {
49             // always returning the same native object is critical for subchannel sharing to work with secure channels
50             var creds = new SslCredentials();
51             var nativeCreds1 = creds.GetNativeCredentials();
52             var nativeCreds2 = creds.GetNativeCredentials();
53             Assert.AreSame(nativeCreds1, nativeCreds2);
54         }
55 
56         [Test]
ChannelCredentials_CreateExceptionIsCached()57         public void ChannelCredentials_CreateExceptionIsCached()
58         {
59             var creds = new ChannelCredentialsWithCreateNativeThrows();
60             var ex1 = Assert.Throws(typeof(Exception), () => creds.GetNativeCredentials());
61             var ex2 = Assert.Throws(typeof(Exception), () => creds.GetNativeCredentials());
62             Assert.AreSame(ex1, ex2);
63         }
64 
65         internal class ChannelCredentialsWithCreateNativeThrows : ChannelCredentials
66         {
67             internal override bool IsComposable => false;
68 
CreateNativeCredentials()69             internal override ChannelCredentialsSafeHandle CreateNativeCredentials()
70             {
71                 throw new Exception("Creation of native credentials has failed on purpose.");
72             }
73         }
74     }
75 }
76