Jan Tattermusch | 0201776 | 2016-11-23 15:39:56 +0100 | [diff] [blame] | 1 | #region Copyright notice and license |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 2 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | 0201776 | 2016-11-23 15:39:56 +0100 | [diff] [blame] | 3 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Jan Tattermusch | 0201776 | 2016-11-23 15:39:56 +0100 | [diff] [blame] | 7 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | 0201776 | 2016-11-23 15:39:56 +0100 | [diff] [blame] | 9 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | 0201776 | 2016-11-23 15:39:56 +0100 | [diff] [blame] | 15 | #endregion |
| 16 | |
| 17 | using System; |
| 18 | using System.Collections.Generic; |
| 19 | using System.Linq; |
| 20 | using System.Text; |
| 21 | using System.Threading.Tasks; |
| 22 | |
| 23 | using Grpc.Core; |
| 24 | using Grpc.Reflection; |
| 25 | using Grpc.Reflection.V1Alpha; |
| 26 | using NUnit.Framework; |
| 27 | |
| 28 | namespace Grpc.Reflection.Tests |
| 29 | { |
| 30 | /// <summary> |
| 31 | /// Reflection client talks to reflection server. |
| 32 | /// </summary> |
| 33 | public class ReflectionClientServerTest |
| 34 | { |
| 35 | const string Host = "localhost"; |
| 36 | Server server; |
| 37 | Channel channel; |
| 38 | ServerReflection.ServerReflectionClient client; |
| 39 | ReflectionServiceImpl serviceImpl; |
| 40 | |
Jan Tattermusch | b8c77c5 | 2017-08-09 09:05:54 +0200 | [diff] [blame] | 41 | [OneTimeSetUp] |
Jan Tattermusch | 0201776 | 2016-11-23 15:39:56 +0100 | [diff] [blame] | 42 | public void Init() |
| 43 | { |
| 44 | serviceImpl = new ReflectionServiceImpl(ServerReflection.Descriptor); |
| 45 | |
Jan Tattermusch | 09d2f55 | 2017-04-20 11:39:37 +0200 | [diff] [blame] | 46 | // Disable SO_REUSEPORT to prevent https://github.com/grpc/grpc/issues/10755 |
| 47 | server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) }) |
Jan Tattermusch | 0201776 | 2016-11-23 15:39:56 +0100 | [diff] [blame] | 48 | { |
| 49 | Services = { ServerReflection.BindService(serviceImpl) }, |
| 50 | Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } } |
| 51 | }; |
| 52 | server.Start(); |
| 53 | channel = new Channel(Host, server.Ports.Single().BoundPort, ChannelCredentials.Insecure); |
| 54 | |
| 55 | client = new ServerReflection.ServerReflectionClient(channel); |
| 56 | } |
| 57 | |
Jan Tattermusch | b8c77c5 | 2017-08-09 09:05:54 +0200 | [diff] [blame] | 58 | [OneTimeTearDown] |
Jan Tattermusch | 0201776 | 2016-11-23 15:39:56 +0100 | [diff] [blame] | 59 | public void Cleanup() |
| 60 | { |
| 61 | channel.ShutdownAsync().Wait(); |
| 62 | server.ShutdownAsync().Wait(); |
| 63 | } |
| 64 | |
| 65 | [Test] |
| 66 | public async Task FileByFilename_NotFound() |
| 67 | { |
| 68 | var response = await SingleRequestAsync(new ServerReflectionRequest |
| 69 | { |
| 70 | FileByFilename = "somepackage/nonexistent.proto" |
| 71 | }); |
| 72 | Assert.AreEqual((int)StatusCode.NotFound, response.ErrorResponse.ErrorCode); |
| 73 | } |
| 74 | |
| 75 | [Test] |
| 76 | public async Task FileByFilename() |
| 77 | { |
| 78 | var response = await SingleRequestAsync(new ServerReflectionRequest |
| 79 | { |
| 80 | FileByFilename = "grpc/reflection/v1alpha/reflection.proto" |
| 81 | }); |
| 82 | Assert.AreEqual(1, response.FileDescriptorResponse.FileDescriptorProto.Count); |
| 83 | Assert.AreEqual(ReflectionReflection.Descriptor.SerializedData, response.FileDescriptorResponse.FileDescriptorProto[0]); |
| 84 | } |
| 85 | |
| 86 | [Test] |
| 87 | public async Task FileContainingSymbol() |
| 88 | { |
| 89 | var response = await SingleRequestAsync(new ServerReflectionRequest |
| 90 | { |
| 91 | FileContainingSymbol = "grpc.reflection.v1alpha.ServerReflection" |
| 92 | }); |
| 93 | Assert.AreEqual(1, response.FileDescriptorResponse.FileDescriptorProto.Count); |
| 94 | Assert.AreEqual(ReflectionReflection.Descriptor.SerializedData, response.FileDescriptorResponse.FileDescriptorProto[0]); |
| 95 | } |
| 96 | |
| 97 | [Test] |
| 98 | public async Task FileContainingSymbol_NotFound() |
| 99 | { |
| 100 | var response = await SingleRequestAsync(new ServerReflectionRequest |
| 101 | { |
| 102 | FileContainingSymbol = "somepackage.Nonexistent" |
| 103 | }); |
| 104 | Assert.AreEqual((int)StatusCode.NotFound, response.ErrorResponse.ErrorCode); |
| 105 | } |
| 106 | |
| 107 | [Test] |
| 108 | public async Task ListServices() |
| 109 | { |
| 110 | var response = await SingleRequestAsync(new ServerReflectionRequest |
| 111 | { |
| 112 | ListServices = "" |
| 113 | }); |
| 114 | Assert.AreEqual(1, response.ListServicesResponse.Service.Count); |
| 115 | Assert.AreEqual(ServerReflection.Descriptor.FullName, response.ListServicesResponse.Service[0].Name); |
| 116 | } |
| 117 | |
| 118 | [Test] |
| 119 | public async Task FileContainingExtension() |
| 120 | { |
| 121 | var response = await SingleRequestAsync(new ServerReflectionRequest |
| 122 | { |
| 123 | FileContainingExtension = new ExtensionRequest() |
| 124 | }); |
| 125 | Assert.AreEqual((int)StatusCode.Unimplemented, response.ErrorResponse.ErrorCode); |
| 126 | } |
| 127 | |
| 128 | private async Task<ServerReflectionResponse> SingleRequestAsync(ServerReflectionRequest request) |
| 129 | { |
| 130 | var call = client.ServerReflectionInfo(); |
| 131 | await call.RequestStream.WriteAsync(request); |
| 132 | Assert.IsTrue(await call.ResponseStream.MoveNext()); |
| 133 | |
| 134 | var response = call.ResponseStream.Current; |
| 135 | await call.RequestStream.CompleteAsync(); |
| 136 | Assert.IsFalse(await call.ResponseStream.MoveNext()); |
| 137 | return response; |
| 138 | } |
| 139 | } |
| 140 | } |