blob: 76f8cfadcfd4d3e0602c0cdc3353f5e255467335 [file] [log] [blame]
Jan Tattermusch02017762016-11-23 15:39:56 +01001#region Copyright notice and license
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002// Copyright 2015 gRPC authors.
Jan Tattermusch02017762016-11-23 15:39:56 +01003//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004// 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 Tattermusch02017762016-11-23 15:39:56 +01007//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch02017762016-11-23 15:39:56 +01009//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010// 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 Tattermusch02017762016-11-23 15:39:56 +010015#endregion
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using System.Threading.Tasks;
22
23using Grpc.Core;
24using Grpc.Reflection;
25using Grpc.Reflection.V1Alpha;
26using NUnit.Framework;
27
28namespace 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 Tattermuschb8c77c52017-08-09 09:05:54 +020041 [OneTimeSetUp]
Jan Tattermusch02017762016-11-23 15:39:56 +010042 public void Init()
43 {
44 serviceImpl = new ReflectionServiceImpl(ServerReflection.Descriptor);
45
Jan Tattermusch09d2f552017-04-20 11:39:37 +020046 // Disable SO_REUSEPORT to prevent https://github.com/grpc/grpc/issues/10755
47 server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) })
Jan Tattermusch02017762016-11-23 15:39:56 +010048 {
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 Tattermuschb8c77c52017-08-09 09:05:54 +020058 [OneTimeTearDown]
Jan Tattermusch02017762016-11-23 15:39:56 +010059 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}