MetadataCredentialsTest.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #region Copyright notice and license
  2. // Copyright 2015, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #endregion
  31. using System;
  32. using System.Collections.Generic;
  33. using System.IO;
  34. using System.Linq;
  35. using System.Threading;
  36. using System.Threading.Tasks;
  37. using Grpc.Core;
  38. using Grpc.Core.Utils;
  39. using Grpc.Testing;
  40. using NUnit.Framework;
  41. namespace Grpc.IntegrationTesting
  42. {
  43. public class MetadataCredentialsTest
  44. {
  45. const string Host = "localhost";
  46. Server server;
  47. Channel channel;
  48. TestService.ITestServiceClient client;
  49. [TestFixtureSetUp]
  50. public void Init()
  51. {
  52. var serverCredentials = new SslServerCredentials(new[] { new KeyCertificatePair(File.ReadAllText(TestCredentials.ServerCertChainPath), File.ReadAllText(TestCredentials.ServerPrivateKeyPath)) });
  53. server = new Server
  54. {
  55. Services = { TestService.BindService(new TestServiceImpl()) },
  56. Ports = { { Host, ServerPort.PickUnused, serverCredentials } }
  57. };
  58. server.Start();
  59. var options = new List<ChannelOption>
  60. {
  61. new ChannelOption(ChannelOptions.SslTargetNameOverride, TestCredentials.DefaultHostOverride)
  62. };
  63. var asyncAuthInterceptor = new AsyncAuthInterceptor(async (context, metadata) =>
  64. {
  65. await Task.Delay(100); // make sure the operation is asynchronous.
  66. metadata.Add("authorization", "SECRET_TOKEN");
  67. });
  68. var clientCredentials = ChannelCredentials.Create(
  69. new SslCredentials(File.ReadAllText(TestCredentials.ClientCertAuthorityPath)),
  70. CallCredentials.FromInterceptor(asyncAuthInterceptor));
  71. channel = new Channel(Host, server.Ports.Single().BoundPort, clientCredentials, options);
  72. client = TestService.NewClient(channel);
  73. }
  74. [TestFixtureTearDown]
  75. public void Cleanup()
  76. {
  77. channel.ShutdownAsync().Wait();
  78. server.ShutdownAsync().Wait();
  79. }
  80. [Test]
  81. public void MetadataCredentials()
  82. {
  83. var response = client.UnaryCall(new SimpleRequest { ResponseSize = 10 });
  84. Assert.AreEqual(10, response.Payload.Body.Length);
  85. }
  86. }
  87. }