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 var ex = Assert.Throws(typeof(ArgumentException), () => ChannelCredentials.Create(new FakeChannelCredentials(false), new FakeCallCredentials())); 44 Assert.AreEqual("CallCredentials can't be composed with FakeChannelCredentials. CallCredentials must be used with secure channel credentials like SslCredentials.", ex.Message); 45 } 46 47 [Test] ChannelCredentials_NativeCredentialsAreReused()48 public void ChannelCredentials_NativeCredentialsAreReused() 49 { 50 // always returning the same native object is critical for subchannel sharing to work with secure channels 51 var creds = new SslCredentials(); 52 var nativeCreds1 = creds.ToNativeCredentials(); 53 var nativeCreds2 = creds.ToNativeCredentials(); 54 Assert.AreSame(nativeCreds1, nativeCreds2); 55 } 56 } 57 } 58