blob: 1d0845e276cd141516ce485afb3e025a2db5a6c7 [file] [log] [blame]
Jan Tattermusch02017762016-11-23 15:39:56 +01001#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
32using System;
33using System.Collections.Generic;
34using System.Linq;
35using System.Text;
36using System.Threading.Tasks;
37
38using Grpc.Core;
39using Grpc.Reflection;
40using Grpc.Reflection.V1Alpha;
41using NUnit.Framework;
42
43namespace Grpc.Reflection.Tests
44{
45 /// <summary>
46 /// Reflection client talks to reflection server.
47 /// </summary>
48 public class ReflectionClientServerTest
49 {
50 const string Host = "localhost";
51 Server server;
52 Channel channel;
53 ServerReflection.ServerReflectionClient client;
54 ReflectionServiceImpl serviceImpl;
55
56 [TestFixtureSetUp]
57 public void Init()
58 {
59 serviceImpl = new ReflectionServiceImpl(ServerReflection.Descriptor);
60
61 server = new Server
62 {
63 Services = { ServerReflection.BindService(serviceImpl) },
64 Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } }
65 };
66 server.Start();
67 channel = new Channel(Host, server.Ports.Single().BoundPort, ChannelCredentials.Insecure);
68
69 client = new ServerReflection.ServerReflectionClient(channel);
70 }
71
72 [TestFixtureTearDown]
73 public void Cleanup()
74 {
75 channel.ShutdownAsync().Wait();
76 server.ShutdownAsync().Wait();
77 }
78
79 [Test]
80 public async Task FileByFilename_NotFound()
81 {
82 var response = await SingleRequestAsync(new ServerReflectionRequest
83 {
84 FileByFilename = "somepackage/nonexistent.proto"
85 });
86 Assert.AreEqual((int)StatusCode.NotFound, response.ErrorResponse.ErrorCode);
87 }
88
89 [Test]
90 public async Task FileByFilename()
91 {
92 var response = await SingleRequestAsync(new ServerReflectionRequest
93 {
94 FileByFilename = "grpc/reflection/v1alpha/reflection.proto"
95 });
96 Assert.AreEqual(1, response.FileDescriptorResponse.FileDescriptorProto.Count);
97 Assert.AreEqual(ReflectionReflection.Descriptor.SerializedData, response.FileDescriptorResponse.FileDescriptorProto[0]);
98 }
99
100 [Test]
101 public async Task FileContainingSymbol()
102 {
103 var response = await SingleRequestAsync(new ServerReflectionRequest
104 {
105 FileContainingSymbol = "grpc.reflection.v1alpha.ServerReflection"
106 });
107 Assert.AreEqual(1, response.FileDescriptorResponse.FileDescriptorProto.Count);
108 Assert.AreEqual(ReflectionReflection.Descriptor.SerializedData, response.FileDescriptorResponse.FileDescriptorProto[0]);
109 }
110
111 [Test]
112 public async Task FileContainingSymbol_NotFound()
113 {
114 var response = await SingleRequestAsync(new ServerReflectionRequest
115 {
116 FileContainingSymbol = "somepackage.Nonexistent"
117 });
118 Assert.AreEqual((int)StatusCode.NotFound, response.ErrorResponse.ErrorCode);
119 }
120
121 [Test]
122 public async Task ListServices()
123 {
124 var response = await SingleRequestAsync(new ServerReflectionRequest
125 {
126 ListServices = ""
127 });
128 Assert.AreEqual(1, response.ListServicesResponse.Service.Count);
129 Assert.AreEqual(ServerReflection.Descriptor.FullName, response.ListServicesResponse.Service[0].Name);
130 }
131
132 [Test]
133 public async Task FileContainingExtension()
134 {
135 var response = await SingleRequestAsync(new ServerReflectionRequest
136 {
137 FileContainingExtension = new ExtensionRequest()
138 });
139 Assert.AreEqual((int)StatusCode.Unimplemented, response.ErrorResponse.ErrorCode);
140 }
141
142 private async Task<ServerReflectionResponse> SingleRequestAsync(ServerReflectionRequest request)
143 {
144 var call = client.ServerReflectionInfo();
145 await call.RequestStream.WriteAsync(request);
146 Assert.IsTrue(await call.ResponseStream.MoveNext());
147
148 var response = call.ResponseStream.Current;
149 await call.RequestStream.CompleteAsync();
150 Assert.IsFalse(await call.ResponseStream.MoveNext());
151 return response;
152 }
153 }
154}