ChannelCredentialsTest.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using Grpc.Core.Internal;
  18. using NUnit.Framework;
  19. namespace Grpc.Core.Tests
  20. {
  21. public class ChannelCredentialsTest
  22. {
  23. [Test]
  24. public void InsecureCredentials_IsNonComposable()
  25. {
  26. Assert.IsFalse(ChannelCredentials.Insecure.IsComposable);
  27. }
  28. [Test]
  29. public void ChannelCredentials_CreateComposite()
  30. {
  31. var composite = ChannelCredentials.Create(new FakeChannelCredentials(true), new FakeCallCredentials());
  32. Assert.IsFalse(composite.IsComposable);
  33. Assert.Throws(typeof(ArgumentNullException), () => ChannelCredentials.Create(null, new FakeCallCredentials()));
  34. Assert.Throws(typeof(ArgumentNullException), () => ChannelCredentials.Create(new FakeChannelCredentials(true), null));
  35. // forbid composing non-composable
  36. Assert.Throws(typeof(ArgumentException), () => ChannelCredentials.Create(new FakeChannelCredentials(false), new FakeCallCredentials()));
  37. }
  38. [Test]
  39. public void ChannelCredentials_NativeCredentialsAreReused()
  40. {
  41. // always returning the same native object is critical for subchannel sharing to work with secure channels
  42. var creds = new SslCredentials();
  43. var nativeCreds1 = creds.GetNativeCredentials();
  44. var nativeCreds2 = creds.GetNativeCredentials();
  45. Assert.AreSame(nativeCreds1, nativeCreds2);
  46. }
  47. [Test]
  48. public void ChannelCredentials_CreateExceptionIsCached()
  49. {
  50. var creds = new ChannelCredentialsWithCreateNativeThrows();
  51. var ex1 = Assert.Throws(typeof(Exception), () => creds.GetNativeCredentials());
  52. var ex2 = Assert.Throws(typeof(Exception), () => creds.GetNativeCredentials());
  53. Assert.AreSame(ex1, ex2);
  54. }
  55. internal class ChannelCredentialsWithCreateNativeThrows : ChannelCredentials
  56. {
  57. internal override bool IsComposable => false;
  58. internal override ChannelCredentialsSafeHandle CreateNativeCredentials()
  59. {
  60. throw new Exception("Creation of native credentials has failed on purpose.");
  61. }
  62. }
  63. }
  64. }