serverreflection_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package reflection
  19. import (
  20. "context"
  21. "fmt"
  22. "net"
  23. "reflect"
  24. "sort"
  25. "testing"
  26. "time"
  27. "google.golang.org/grpc"
  28. "google.golang.org/grpc/credentials/insecure"
  29. "google.golang.org/grpc/internal/grpctest"
  30. "google.golang.org/protobuf/proto"
  31. "google.golang.org/protobuf/reflect/protodesc"
  32. "google.golang.org/protobuf/reflect/protoreflect"
  33. "google.golang.org/protobuf/reflect/protoregistry"
  34. "google.golang.org/protobuf/types/descriptorpb"
  35. "google.golang.org/protobuf/types/dynamicpb"
  36. v1alphagrpc "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
  37. v1alphapb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
  38. pb "google.golang.org/grpc/reflection/grpc_testing"
  39. pbv3 "google.golang.org/grpc/reflection/grpc_testing_not_regenerate"
  40. )
  41. var (
  42. s = NewServer(ServerOptions{}).(*serverReflectionServer)
  43. // fileDescriptor of each test proto file.
  44. fdTest *descriptorpb.FileDescriptorProto
  45. fdTestv3 *descriptorpb.FileDescriptorProto
  46. fdProto2 *descriptorpb.FileDescriptorProto
  47. fdProto2Ext *descriptorpb.FileDescriptorProto
  48. fdProto2Ext2 *descriptorpb.FileDescriptorProto
  49. fdDynamic *descriptorpb.FileDescriptorProto
  50. // reflection descriptors.
  51. fdDynamicFile protoreflect.FileDescriptor
  52. // fileDescriptor marshalled.
  53. fdTestByte []byte
  54. fdTestv3Byte []byte
  55. fdProto2Byte []byte
  56. fdProto2ExtByte []byte
  57. fdProto2Ext2Byte []byte
  58. fdDynamicByte []byte
  59. )
  60. const defaultTestTimeout = 10 * time.Second
  61. type x struct {
  62. grpctest.Tester
  63. }
  64. func Test(t *testing.T) {
  65. grpctest.RunSubTests(t, x{})
  66. }
  67. func loadFileDesc(filename string) (*descriptorpb.FileDescriptorProto, []byte) {
  68. fd, err := protoregistry.GlobalFiles.FindFileByPath(filename)
  69. if err != nil {
  70. panic(err)
  71. }
  72. fdProto := protodesc.ToFileDescriptorProto(fd)
  73. b, err := proto.Marshal(fdProto)
  74. if err != nil {
  75. panic(fmt.Sprintf("failed to marshal fd: %v", err))
  76. }
  77. return fdProto, b
  78. }
  79. func loadFileDescDynamic(b []byte) (*descriptorpb.FileDescriptorProto, protoreflect.FileDescriptor, []byte) {
  80. m := new(descriptorpb.FileDescriptorProto)
  81. if err := proto.Unmarshal(b, m); err != nil {
  82. panic("failed to unmarshal dynamic proto raw descriptor")
  83. }
  84. fd, err := protodesc.NewFile(m, nil)
  85. if err != nil {
  86. panic(err)
  87. }
  88. err = protoregistry.GlobalFiles.RegisterFile(fd)
  89. if err != nil {
  90. panic(err)
  91. }
  92. for i := 0; i < fd.Messages().Len(); i++ {
  93. m := fd.Messages().Get(i)
  94. if err := protoregistry.GlobalTypes.RegisterMessage(dynamicpb.NewMessageType(m)); err != nil {
  95. panic(err)
  96. }
  97. }
  98. return m, fd, b
  99. }
  100. func init() {
  101. fdTest, fdTestByte = loadFileDesc("reflection/grpc_testing/test.proto")
  102. fdTestv3, fdTestv3Byte = loadFileDesc("testv3.proto")
  103. fdProto2, fdProto2Byte = loadFileDesc("reflection/grpc_testing/proto2.proto")
  104. fdProto2Ext, fdProto2ExtByte = loadFileDesc("reflection/grpc_testing/proto2_ext.proto")
  105. fdProto2Ext2, fdProto2Ext2Byte = loadFileDesc("reflection/grpc_testing/proto2_ext2.proto")
  106. fdDynamic, fdDynamicFile, fdDynamicByte = loadFileDescDynamic(pbv3.FileDynamicProtoRawDesc)
  107. }
  108. func (x) TestFileDescContainingExtension(t *testing.T) {
  109. for _, test := range []struct {
  110. st string
  111. extNum int32
  112. want *descriptorpb.FileDescriptorProto
  113. }{
  114. {"grpc.testing.ToBeExtended", 13, fdProto2Ext},
  115. {"grpc.testing.ToBeExtended", 17, fdProto2Ext},
  116. {"grpc.testing.ToBeExtended", 19, fdProto2Ext},
  117. {"grpc.testing.ToBeExtended", 23, fdProto2Ext2},
  118. {"grpc.testing.ToBeExtended", 29, fdProto2Ext2},
  119. } {
  120. fd, err := s.fileDescEncodingContainingExtension(test.st, test.extNum, map[string]bool{})
  121. if err != nil {
  122. t.Errorf("fileDescContainingExtension(%q) return error: %v", test.st, err)
  123. continue
  124. }
  125. var actualFd descriptorpb.FileDescriptorProto
  126. if err := proto.Unmarshal(fd[0], &actualFd); err != nil {
  127. t.Errorf("fileDescContainingExtension(%q) return invalid bytes: %v", test.st, err)
  128. continue
  129. }
  130. if !proto.Equal(&actualFd, test.want) {
  131. t.Errorf("fileDescContainingExtension(%q) returned %q, but wanted %q", test.st, &actualFd, test.want)
  132. }
  133. }
  134. }
  135. // intArray is used to sort []int32
  136. type intArray []int32
  137. func (s intArray) Len() int { return len(s) }
  138. func (s intArray) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  139. func (s intArray) Less(i, j int) bool { return s[i] < s[j] }
  140. func (x) TestAllExtensionNumbersForTypeName(t *testing.T) {
  141. for _, test := range []struct {
  142. st string
  143. want []int32
  144. }{
  145. {"grpc.testing.ToBeExtended", []int32{13, 17, 19, 23, 29}},
  146. } {
  147. r, err := s.allExtensionNumbersForTypeName(test.st)
  148. sort.Sort(intArray(r))
  149. if err != nil || !reflect.DeepEqual(r, test.want) {
  150. t.Errorf("allExtensionNumbersForType(%q) = %v, %v, want %v, <nil>", test.st, r, err, test.want)
  151. }
  152. }
  153. }
  154. // Do end2end tests.
  155. type server struct {
  156. pb.UnimplementedSearchServiceServer
  157. }
  158. func (s *server) Search(ctx context.Context, in *pb.SearchRequest) (*pb.SearchResponse, error) {
  159. return &pb.SearchResponse{}, nil
  160. }
  161. func (s *server) StreamingSearch(stream pb.SearchService_StreamingSearchServer) error {
  162. return nil
  163. }
  164. type serverV3 struct{}
  165. func (s *serverV3) Search(ctx context.Context, in *pbv3.SearchRequestV3) (*pbv3.SearchResponseV3, error) {
  166. return &pbv3.SearchResponseV3{}, nil
  167. }
  168. func (s *serverV3) StreamingSearch(stream pbv3.SearchServiceV3_StreamingSearchServer) error {
  169. return nil
  170. }
  171. func (x) TestReflectionEnd2end(t *testing.T) {
  172. // Start server.
  173. lis, err := net.Listen("tcp", "localhost:0")
  174. if err != nil {
  175. t.Fatalf("failed to listen: %v", err)
  176. }
  177. s := grpc.NewServer()
  178. pb.RegisterSearchServiceServer(s, &server{})
  179. pbv3.RegisterSearchServiceV3Server(s, &serverV3{})
  180. registerDynamicProto(s, fdDynamic, fdDynamicFile)
  181. // Register reflection service on s.
  182. Register(s)
  183. go s.Serve(lis)
  184. // Create client.
  185. conn, err := grpc.Dial(lis.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
  186. if err != nil {
  187. t.Fatalf("cannot connect to server: %v", err)
  188. }
  189. defer conn.Close()
  190. c := v1alphagrpc.NewServerReflectionClient(conn)
  191. ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
  192. defer cancel()
  193. stream, err := c.ServerReflectionInfo(ctx, grpc.WaitForReady(true))
  194. if err != nil {
  195. t.Fatalf("cannot get ServerReflectionInfo: %v", err)
  196. }
  197. testFileByFilenameTransitiveClosure(t, stream, true)
  198. testFileByFilenameTransitiveClosure(t, stream, false)
  199. testFileByFilename(t, stream)
  200. testFileByFilenameError(t, stream)
  201. testFileContainingSymbol(t, stream)
  202. testFileContainingSymbolError(t, stream)
  203. testFileContainingExtension(t, stream)
  204. testFileContainingExtensionError(t, stream)
  205. testAllExtensionNumbersOfType(t, stream)
  206. testAllExtensionNumbersOfTypeError(t, stream)
  207. testListServices(t, stream)
  208. s.Stop()
  209. }
  210. func testFileByFilenameTransitiveClosure(t *testing.T, stream v1alphagrpc.ServerReflection_ServerReflectionInfoClient, expectClosure bool) {
  211. filename := "reflection/grpc_testing/proto2_ext2.proto"
  212. if err := stream.Send(&v1alphapb.ServerReflectionRequest{
  213. MessageRequest: &v1alphapb.ServerReflectionRequest_FileByFilename{
  214. FileByFilename: filename,
  215. },
  216. }); err != nil {
  217. t.Fatalf("failed to send request: %v", err)
  218. }
  219. r, err := stream.Recv()
  220. if err != nil {
  221. // io.EOF is not ok.
  222. t.Fatalf("failed to recv response: %v", err)
  223. }
  224. switch r.MessageResponse.(type) {
  225. case *v1alphapb.ServerReflectionResponse_FileDescriptorResponse:
  226. if !reflect.DeepEqual(r.GetFileDescriptorResponse().FileDescriptorProto[0], fdProto2Ext2Byte) {
  227. t.Errorf("FileByFilename(%v)\nreceived: %q,\nwant: %q", filename, r.GetFileDescriptorResponse().FileDescriptorProto[0], fdProto2Ext2Byte)
  228. }
  229. if expectClosure {
  230. if len(r.GetFileDescriptorResponse().FileDescriptorProto) != 2 {
  231. t.Errorf("FileByFilename(%v) returned %v file descriptors, expected 2", filename, len(r.GetFileDescriptorResponse().FileDescriptorProto))
  232. } else if !reflect.DeepEqual(r.GetFileDescriptorResponse().FileDescriptorProto[1], fdProto2Byte) {
  233. t.Errorf("FileByFilename(%v)\nreceived: %q,\nwant: %q", filename, r.GetFileDescriptorResponse().FileDescriptorProto[1], fdProto2Byte)
  234. }
  235. } else if len(r.GetFileDescriptorResponse().FileDescriptorProto) != 1 {
  236. t.Errorf("FileByFilename(%v) returned %v file descriptors, expected 1", filename, len(r.GetFileDescriptorResponse().FileDescriptorProto))
  237. }
  238. default:
  239. t.Errorf("FileByFilename(%v) = %v, want type <ServerReflectionResponse_FileDescriptorResponse>", filename, r.MessageResponse)
  240. }
  241. }
  242. func testFileByFilename(t *testing.T, stream v1alphagrpc.ServerReflection_ServerReflectionInfoClient) {
  243. for _, test := range []struct {
  244. filename string
  245. want []byte
  246. }{
  247. {"reflection/grpc_testing/test.proto", fdTestByte},
  248. {"reflection/grpc_testing/proto2.proto", fdProto2Byte},
  249. {"reflection/grpc_testing/proto2_ext.proto", fdProto2ExtByte},
  250. {"dynamic.proto", fdDynamicByte},
  251. } {
  252. if err := stream.Send(&v1alphapb.ServerReflectionRequest{
  253. MessageRequest: &v1alphapb.ServerReflectionRequest_FileByFilename{
  254. FileByFilename: test.filename,
  255. },
  256. }); err != nil {
  257. t.Fatalf("failed to send request: %v", err)
  258. }
  259. r, err := stream.Recv()
  260. if err != nil {
  261. // io.EOF is not ok.
  262. t.Fatalf("failed to recv response: %v", err)
  263. }
  264. switch r.MessageResponse.(type) {
  265. case *v1alphapb.ServerReflectionResponse_FileDescriptorResponse:
  266. if !reflect.DeepEqual(r.GetFileDescriptorResponse().FileDescriptorProto[0], test.want) {
  267. t.Errorf("FileByFilename(%v)\nreceived: %q,\nwant: %q", test.filename, r.GetFileDescriptorResponse().FileDescriptorProto[0], test.want)
  268. }
  269. default:
  270. t.Errorf("FileByFilename(%v) = %v, want type <ServerReflectionResponse_FileDescriptorResponse>", test.filename, r.MessageResponse)
  271. }
  272. }
  273. }
  274. func testFileByFilenameError(t *testing.T, stream v1alphagrpc.ServerReflection_ServerReflectionInfoClient) {
  275. for _, test := range []string{
  276. "test.poto",
  277. "proo2.proto",
  278. "proto2_et.proto",
  279. } {
  280. if err := stream.Send(&v1alphapb.ServerReflectionRequest{
  281. MessageRequest: &v1alphapb.ServerReflectionRequest_FileByFilename{
  282. FileByFilename: test,
  283. },
  284. }); err != nil {
  285. t.Fatalf("failed to send request: %v", err)
  286. }
  287. r, err := stream.Recv()
  288. if err != nil {
  289. // io.EOF is not ok.
  290. t.Fatalf("failed to recv response: %v", err)
  291. }
  292. switch r.MessageResponse.(type) {
  293. case *v1alphapb.ServerReflectionResponse_ErrorResponse:
  294. default:
  295. t.Errorf("FileByFilename(%v) = %v, want type <ServerReflectionResponse_ErrorResponse>", test, r.MessageResponse)
  296. }
  297. }
  298. }
  299. func testFileContainingSymbol(t *testing.T, stream v1alphagrpc.ServerReflection_ServerReflectionInfoClient) {
  300. for _, test := range []struct {
  301. symbol string
  302. want []byte
  303. }{
  304. {"grpc.testing.SearchService", fdTestByte},
  305. {"grpc.testing.SearchService.Search", fdTestByte},
  306. {"grpc.testing.SearchService.StreamingSearch", fdTestByte},
  307. {"grpc.testing.SearchResponse", fdTestByte},
  308. {"grpc.testing.ToBeExtended", fdProto2Byte},
  309. // Test support package v3.
  310. {"grpc.testingv3.SearchServiceV3", fdTestv3Byte},
  311. {"grpc.testingv3.SearchServiceV3.Search", fdTestv3Byte},
  312. {"grpc.testingv3.SearchServiceV3.StreamingSearch", fdTestv3Byte},
  313. {"grpc.testingv3.SearchResponseV3", fdTestv3Byte},
  314. // search for field, oneof, enum, and enum value symbols, too
  315. {"grpc.testingv3.SearchResponseV3.Result.snippets", fdTestv3Byte},
  316. {"grpc.testingv3.SearchResponseV3.Result.Value.val", fdTestv3Byte},
  317. {"grpc.testingv3.SearchResponseV3.Result.Value.str", fdTestv3Byte},
  318. {"grpc.testingv3.SearchResponseV3.State", fdTestv3Byte},
  319. {"grpc.testingv3.SearchResponseV3.FRESH", fdTestv3Byte},
  320. // Test dynamic symbols
  321. {"grpc.testing.DynamicService", fdDynamicByte},
  322. {"grpc.testing.DynamicReq", fdDynamicByte},
  323. {"grpc.testing.DynamicRes", fdDynamicByte},
  324. } {
  325. if err := stream.Send(&v1alphapb.ServerReflectionRequest{
  326. MessageRequest: &v1alphapb.ServerReflectionRequest_FileContainingSymbol{
  327. FileContainingSymbol: test.symbol,
  328. },
  329. }); err != nil {
  330. t.Fatalf("failed to send request: %v", err)
  331. }
  332. r, err := stream.Recv()
  333. if err != nil {
  334. // io.EOF is not ok.
  335. t.Fatalf("failed to recv response: %v", err)
  336. }
  337. switch r.MessageResponse.(type) {
  338. case *v1alphapb.ServerReflectionResponse_FileDescriptorResponse:
  339. if !reflect.DeepEqual(r.GetFileDescriptorResponse().FileDescriptorProto[0], test.want) {
  340. t.Errorf("FileContainingSymbol(%v)\nreceived: %q,\nwant: %q", test.symbol, r.GetFileDescriptorResponse().FileDescriptorProto[0], test.want)
  341. }
  342. default:
  343. t.Errorf("FileContainingSymbol(%v) = %v, want type <ServerReflectionResponse_FileDescriptorResponse>", test.symbol, r.MessageResponse)
  344. }
  345. }
  346. }
  347. func testFileContainingSymbolError(t *testing.T, stream v1alphagrpc.ServerReflection_ServerReflectionInfoClient) {
  348. for _, test := range []string{
  349. "grpc.testing.SerchService",
  350. "grpc.testing.SearchService.SearchE",
  351. "grpc.tesing.SearchResponse",
  352. "gpc.testing.ToBeExtended",
  353. } {
  354. if err := stream.Send(&v1alphapb.ServerReflectionRequest{
  355. MessageRequest: &v1alphapb.ServerReflectionRequest_FileContainingSymbol{
  356. FileContainingSymbol: test,
  357. },
  358. }); err != nil {
  359. t.Fatalf("failed to send request: %v", err)
  360. }
  361. r, err := stream.Recv()
  362. if err != nil {
  363. // io.EOF is not ok.
  364. t.Fatalf("failed to recv response: %v", err)
  365. }
  366. switch r.MessageResponse.(type) {
  367. case *v1alphapb.ServerReflectionResponse_ErrorResponse:
  368. default:
  369. t.Errorf("FileContainingSymbol(%v) = %v, want type <ServerReflectionResponse_ErrorResponse>", test, r.MessageResponse)
  370. }
  371. }
  372. }
  373. func testFileContainingExtension(t *testing.T, stream v1alphagrpc.ServerReflection_ServerReflectionInfoClient) {
  374. for _, test := range []struct {
  375. typeName string
  376. extNum int32
  377. want []byte
  378. }{
  379. {"grpc.testing.ToBeExtended", 13, fdProto2ExtByte},
  380. {"grpc.testing.ToBeExtended", 17, fdProto2ExtByte},
  381. {"grpc.testing.ToBeExtended", 19, fdProto2ExtByte},
  382. {"grpc.testing.ToBeExtended", 23, fdProto2Ext2Byte},
  383. {"grpc.testing.ToBeExtended", 29, fdProto2Ext2Byte},
  384. } {
  385. if err := stream.Send(&v1alphapb.ServerReflectionRequest{
  386. MessageRequest: &v1alphapb.ServerReflectionRequest_FileContainingExtension{
  387. FileContainingExtension: &v1alphapb.ExtensionRequest{
  388. ContainingType: test.typeName,
  389. ExtensionNumber: test.extNum,
  390. },
  391. },
  392. }); err != nil {
  393. t.Fatalf("failed to send request: %v", err)
  394. }
  395. r, err := stream.Recv()
  396. if err != nil {
  397. // io.EOF is not ok.
  398. t.Fatalf("failed to recv response: %v", err)
  399. }
  400. switch r.MessageResponse.(type) {
  401. case *v1alphapb.ServerReflectionResponse_FileDescriptorResponse:
  402. if !reflect.DeepEqual(r.GetFileDescriptorResponse().FileDescriptorProto[0], test.want) {
  403. t.Errorf("FileContainingExtension(%v, %v)\nreceived: %q,\nwant: %q", test.typeName, test.extNum, r.GetFileDescriptorResponse().FileDescriptorProto[0], test.want)
  404. }
  405. default:
  406. t.Errorf("FileContainingExtension(%v, %v) = %v, want type <ServerReflectionResponse_FileDescriptorResponse>", test.typeName, test.extNum, r.MessageResponse)
  407. }
  408. }
  409. }
  410. func testFileContainingExtensionError(t *testing.T, stream v1alphagrpc.ServerReflection_ServerReflectionInfoClient) {
  411. for _, test := range []struct {
  412. typeName string
  413. extNum int32
  414. }{
  415. {"grpc.testing.ToBExtended", 17},
  416. {"grpc.testing.ToBeExtended", 15},
  417. } {
  418. if err := stream.Send(&v1alphapb.ServerReflectionRequest{
  419. MessageRequest: &v1alphapb.ServerReflectionRequest_FileContainingExtension{
  420. FileContainingExtension: &v1alphapb.ExtensionRequest{
  421. ContainingType: test.typeName,
  422. ExtensionNumber: test.extNum,
  423. },
  424. },
  425. }); err != nil {
  426. t.Fatalf("failed to send request: %v", err)
  427. }
  428. r, err := stream.Recv()
  429. if err != nil {
  430. // io.EOF is not ok.
  431. t.Fatalf("failed to recv response: %v", err)
  432. }
  433. switch r.MessageResponse.(type) {
  434. case *v1alphapb.ServerReflectionResponse_ErrorResponse:
  435. default:
  436. t.Errorf("FileContainingExtension(%v, %v) = %v, want type <ServerReflectionResponse_FileDescriptorResponse>", test.typeName, test.extNum, r.MessageResponse)
  437. }
  438. }
  439. }
  440. func testAllExtensionNumbersOfType(t *testing.T, stream v1alphagrpc.ServerReflection_ServerReflectionInfoClient) {
  441. for _, test := range []struct {
  442. typeName string
  443. want []int32
  444. }{
  445. {"grpc.testing.ToBeExtended", []int32{13, 17, 19, 23, 29}},
  446. {"grpc.testing.DynamicReq", nil},
  447. } {
  448. if err := stream.Send(&v1alphapb.ServerReflectionRequest{
  449. MessageRequest: &v1alphapb.ServerReflectionRequest_AllExtensionNumbersOfType{
  450. AllExtensionNumbersOfType: test.typeName,
  451. },
  452. }); err != nil {
  453. t.Fatalf("failed to send request: %v", err)
  454. }
  455. r, err := stream.Recv()
  456. if err != nil {
  457. // io.EOF is not ok.
  458. t.Fatalf("failed to recv response: %v", err)
  459. }
  460. switch r.MessageResponse.(type) {
  461. case *v1alphapb.ServerReflectionResponse_AllExtensionNumbersResponse:
  462. extNum := r.GetAllExtensionNumbersResponse().ExtensionNumber
  463. sort.Sort(intArray(extNum))
  464. if r.GetAllExtensionNumbersResponse().BaseTypeName != test.typeName ||
  465. !reflect.DeepEqual(extNum, test.want) {
  466. t.Errorf("AllExtensionNumbersOfType(%v)\nreceived: %v,\nwant: {%q %v}", r.GetAllExtensionNumbersResponse(), test.typeName, test.typeName, test.want)
  467. }
  468. default:
  469. t.Errorf("AllExtensionNumbersOfType(%v) = %v, want type <ServerReflectionResponse_AllExtensionNumbersResponse>", test.typeName, r.MessageResponse)
  470. }
  471. }
  472. }
  473. func testAllExtensionNumbersOfTypeError(t *testing.T, stream v1alphagrpc.ServerReflection_ServerReflectionInfoClient) {
  474. for _, test := range []string{
  475. "grpc.testing.ToBeExtendedE",
  476. } {
  477. if err := stream.Send(&v1alphapb.ServerReflectionRequest{
  478. MessageRequest: &v1alphapb.ServerReflectionRequest_AllExtensionNumbersOfType{
  479. AllExtensionNumbersOfType: test,
  480. },
  481. }); err != nil {
  482. t.Fatalf("failed to send request: %v", err)
  483. }
  484. r, err := stream.Recv()
  485. if err != nil {
  486. // io.EOF is not ok.
  487. t.Fatalf("failed to recv response: %v", err)
  488. }
  489. switch r.MessageResponse.(type) {
  490. case *v1alphapb.ServerReflectionResponse_ErrorResponse:
  491. default:
  492. t.Errorf("AllExtensionNumbersOfType(%v) = %v, want type <ServerReflectionResponse_ErrorResponse>", test, r.MessageResponse)
  493. }
  494. }
  495. }
  496. func testListServices(t *testing.T, stream v1alphagrpc.ServerReflection_ServerReflectionInfoClient) {
  497. if err := stream.Send(&v1alphapb.ServerReflectionRequest{
  498. MessageRequest: &v1alphapb.ServerReflectionRequest_ListServices{},
  499. }); err != nil {
  500. t.Fatalf("failed to send request: %v", err)
  501. }
  502. r, err := stream.Recv()
  503. if err != nil {
  504. // io.EOF is not ok.
  505. t.Fatalf("failed to recv response: %v", err)
  506. }
  507. switch r.MessageResponse.(type) {
  508. case *v1alphapb.ServerReflectionResponse_ListServicesResponse:
  509. services := r.GetListServicesResponse().Service
  510. want := []string{
  511. "grpc.testingv3.SearchServiceV3",
  512. "grpc.testing.SearchService",
  513. "grpc.reflection.v1alpha.ServerReflection",
  514. "grpc.testing.DynamicService",
  515. }
  516. // Compare service names in response with want.
  517. if len(services) != len(want) {
  518. t.Errorf("= %v, want service names: %v", services, want)
  519. }
  520. m := make(map[string]int)
  521. for _, e := range services {
  522. m[e.Name]++
  523. }
  524. for _, e := range want {
  525. if m[e] > 0 {
  526. m[e]--
  527. continue
  528. }
  529. t.Errorf("ListService\nreceived: %v,\nwant: %q", services, want)
  530. }
  531. default:
  532. t.Errorf("ListServices = %v, want type <ServerReflectionResponse_ListServicesResponse>", r.MessageResponse)
  533. }
  534. }
  535. func registerDynamicProto(srv *grpc.Server, fdp *descriptorpb.FileDescriptorProto, fd protoreflect.FileDescriptor) {
  536. type emptyInterface interface{}
  537. for i := 0; i < fd.Services().Len(); i++ {
  538. s := fd.Services().Get(i)
  539. sd := &grpc.ServiceDesc{
  540. ServiceName: string(s.FullName()),
  541. HandlerType: (*emptyInterface)(nil),
  542. Metadata: fdp.GetName(),
  543. }
  544. for j := 0; j < s.Methods().Len(); j++ {
  545. m := s.Methods().Get(j)
  546. sd.Methods = append(sd.Methods, grpc.MethodDesc{
  547. MethodName: string(m.Name()),
  548. })
  549. }
  550. srv.RegisterService(sd, struct{}{})
  551. }
  552. }