serverreflection_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. "github.com/golang/protobuf/proto"
  27. dpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
  28. "google.golang.org/grpc"
  29. "google.golang.org/grpc/internal/grpctest"
  30. rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
  31. pb "google.golang.org/grpc/reflection/grpc_testing"
  32. pbv3 "google.golang.org/grpc/reflection/grpc_testingv3"
  33. )
  34. var (
  35. s = &serverReflectionServer{}
  36. // fileDescriptor of each test proto file.
  37. fdTest *dpb.FileDescriptorProto
  38. fdTestv3 *dpb.FileDescriptorProto
  39. fdProto2 *dpb.FileDescriptorProto
  40. fdProto2Ext *dpb.FileDescriptorProto
  41. fdProto2Ext2 *dpb.FileDescriptorProto
  42. // fileDescriptor marshalled.
  43. fdTestByte []byte
  44. fdTestv3Byte []byte
  45. fdProto2Byte []byte
  46. fdProto2ExtByte []byte
  47. fdProto2Ext2Byte []byte
  48. )
  49. type x struct {
  50. grpctest.Tester
  51. }
  52. func Test(t *testing.T) {
  53. grpctest.RunSubTests(t, x{})
  54. }
  55. func loadFileDesc(filename string) (*dpb.FileDescriptorProto, []byte) {
  56. enc := proto.FileDescriptor(filename)
  57. if enc == nil {
  58. panic(fmt.Sprintf("failed to find fd for file: %v", filename))
  59. }
  60. fd, err := decodeFileDesc(enc)
  61. if err != nil {
  62. panic(fmt.Sprintf("failed to decode enc: %v", err))
  63. }
  64. b, err := proto.Marshal(fd)
  65. if err != nil {
  66. panic(fmt.Sprintf("failed to marshal fd: %v", err))
  67. }
  68. return fd, b
  69. }
  70. func init() {
  71. fdTest, fdTestByte = loadFileDesc("reflection/grpc_testing/test.proto")
  72. fdTestv3, fdTestv3Byte = loadFileDesc("testv3.proto")
  73. fdProto2, fdProto2Byte = loadFileDesc("reflection/grpc_testing/proto2.proto")
  74. fdProto2Ext, fdProto2ExtByte = loadFileDesc("reflection/grpc_testing/proto2_ext.proto")
  75. fdProto2Ext2, fdProto2Ext2Byte = loadFileDesc("reflection/grpc_testing/proto2_ext2.proto")
  76. }
  77. func (x) TestFileDescForType(t *testing.T) {
  78. for _, test := range []struct {
  79. st reflect.Type
  80. wantFd *dpb.FileDescriptorProto
  81. }{
  82. {reflect.TypeOf(pb.SearchResponse_Result{}), fdTest},
  83. {reflect.TypeOf(pb.ToBeExtended{}), fdProto2},
  84. } {
  85. fd, err := s.fileDescForType(test.st)
  86. if err != nil || !proto.Equal(fd, test.wantFd) {
  87. t.Errorf("fileDescForType(%q) = %q, %v, want %q, <nil>", test.st, fd, err, test.wantFd)
  88. }
  89. }
  90. }
  91. func (x) TestTypeForName(t *testing.T) {
  92. for _, test := range []struct {
  93. name string
  94. want reflect.Type
  95. }{
  96. {"grpc.testing.SearchResponse", reflect.TypeOf(pb.SearchResponse{})},
  97. } {
  98. r, err := typeForName(test.name)
  99. if err != nil || r != test.want {
  100. t.Errorf("typeForName(%q) = %q, %v, want %q, <nil>", test.name, r, err, test.want)
  101. }
  102. }
  103. }
  104. func (x) TestTypeForNameNotFound(t *testing.T) {
  105. for _, test := range []string{
  106. "grpc.testing.not_exiting",
  107. } {
  108. _, err := typeForName(test)
  109. if err == nil {
  110. t.Errorf("typeForName(%q) = _, %v, want _, <non-nil>", test, err)
  111. }
  112. }
  113. }
  114. func (x) TestFileDescContainingExtension(t *testing.T) {
  115. for _, test := range []struct {
  116. st reflect.Type
  117. extNum int32
  118. want *dpb.FileDescriptorProto
  119. }{
  120. {reflect.TypeOf(pb.ToBeExtended{}), 13, fdProto2Ext},
  121. {reflect.TypeOf(pb.ToBeExtended{}), 17, fdProto2Ext},
  122. {reflect.TypeOf(pb.ToBeExtended{}), 19, fdProto2Ext},
  123. {reflect.TypeOf(pb.ToBeExtended{}), 23, fdProto2Ext2},
  124. {reflect.TypeOf(pb.ToBeExtended{}), 29, fdProto2Ext2},
  125. } {
  126. fd, err := fileDescContainingExtension(test.st, test.extNum)
  127. if err != nil || !proto.Equal(fd, test.want) {
  128. t.Errorf("fileDescContainingExtension(%q) = %q, %v, want %q, <nil>", test.st, fd, err, test.want)
  129. }
  130. }
  131. }
  132. // intArray is used to sort []int32
  133. type intArray []int32
  134. func (s intArray) Len() int { return len(s) }
  135. func (s intArray) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  136. func (s intArray) Less(i, j int) bool { return s[i] < s[j] }
  137. func (x) TestAllExtensionNumbersForType(t *testing.T) {
  138. for _, test := range []struct {
  139. st reflect.Type
  140. want []int32
  141. }{
  142. {reflect.TypeOf(pb.ToBeExtended{}), []int32{13, 17, 19, 23, 29}},
  143. } {
  144. r, err := s.allExtensionNumbersForType(test.st)
  145. sort.Sort(intArray(r))
  146. if err != nil || !reflect.DeepEqual(r, test.want) {
  147. t.Errorf("allExtensionNumbersForType(%q) = %v, %v, want %v, <nil>", test.st, r, err, test.want)
  148. }
  149. }
  150. }
  151. // Do end2end tests.
  152. type server struct {
  153. pb.UnimplementedSearchServiceServer
  154. }
  155. func (s *server) Search(ctx context.Context, in *pb.SearchRequest) (*pb.SearchResponse, error) {
  156. return &pb.SearchResponse{}, nil
  157. }
  158. func (s *server) StreamingSearch(stream pb.SearchService_StreamingSearchServer) error {
  159. return nil
  160. }
  161. type serverV3 struct{}
  162. func (s *serverV3) Search(ctx context.Context, in *pbv3.SearchRequestV3) (*pbv3.SearchResponseV3, error) {
  163. return &pbv3.SearchResponseV3{}, nil
  164. }
  165. func (s *serverV3) StreamingSearch(stream pbv3.SearchServiceV3_StreamingSearchServer) error {
  166. return nil
  167. }
  168. func (x) TestReflectionEnd2end(t *testing.T) {
  169. // Start server.
  170. lis, err := net.Listen("tcp", "localhost:0")
  171. if err != nil {
  172. t.Fatalf("failed to listen: %v", err)
  173. }
  174. s := grpc.NewServer()
  175. pb.RegisterSearchServiceServer(s, &server{})
  176. pbv3.RegisterSearchServiceV3Server(s, &serverV3{})
  177. // Register reflection service on s.
  178. Register(s)
  179. go s.Serve(lis)
  180. // Create client.
  181. conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
  182. if err != nil {
  183. t.Fatalf("cannot connect to server: %v", err)
  184. }
  185. defer conn.Close()
  186. c := rpb.NewServerReflectionClient(conn)
  187. stream, err := c.ServerReflectionInfo(context.Background(), grpc.WaitForReady(true))
  188. if err != nil {
  189. t.Fatalf("cannot get ServerReflectionInfo: %v", err)
  190. }
  191. testFileByFilename(t, stream)
  192. testFileByFilenameError(t, stream)
  193. testFileContainingSymbol(t, stream)
  194. testFileContainingSymbolError(t, stream)
  195. testFileContainingExtension(t, stream)
  196. testFileContainingExtensionError(t, stream)
  197. testAllExtensionNumbersOfType(t, stream)
  198. testAllExtensionNumbersOfTypeError(t, stream)
  199. testListServices(t, stream)
  200. s.Stop()
  201. }
  202. func testFileByFilename(t *testing.T, stream rpb.ServerReflection_ServerReflectionInfoClient) {
  203. for _, test := range []struct {
  204. filename string
  205. want []byte
  206. }{
  207. {"reflection/grpc_testing/test.proto", fdTestByte},
  208. {"reflection/grpc_testing/proto2.proto", fdProto2Byte},
  209. {"reflection/grpc_testing/proto2_ext.proto", fdProto2ExtByte},
  210. } {
  211. if err := stream.Send(&rpb.ServerReflectionRequest{
  212. MessageRequest: &rpb.ServerReflectionRequest_FileByFilename{
  213. FileByFilename: test.filename,
  214. },
  215. }); err != nil {
  216. t.Fatalf("failed to send request: %v", err)
  217. }
  218. r, err := stream.Recv()
  219. if err != nil {
  220. // io.EOF is not ok.
  221. t.Fatalf("failed to recv response: %v", err)
  222. }
  223. switch r.MessageResponse.(type) {
  224. case *rpb.ServerReflectionResponse_FileDescriptorResponse:
  225. if !reflect.DeepEqual(r.GetFileDescriptorResponse().FileDescriptorProto[0], test.want) {
  226. t.Errorf("FileByFilename(%v)\nreceived: %q,\nwant: %q", test.filename, r.GetFileDescriptorResponse().FileDescriptorProto[0], test.want)
  227. }
  228. default:
  229. t.Errorf("FileByFilename(%v) = %v, want type <ServerReflectionResponse_FileDescriptorResponse>", test.filename, r.MessageResponse)
  230. }
  231. }
  232. }
  233. func testFileByFilenameError(t *testing.T, stream rpb.ServerReflection_ServerReflectionInfoClient) {
  234. for _, test := range []string{
  235. "test.poto",
  236. "proo2.proto",
  237. "proto2_et.proto",
  238. } {
  239. if err := stream.Send(&rpb.ServerReflectionRequest{
  240. MessageRequest: &rpb.ServerReflectionRequest_FileByFilename{
  241. FileByFilename: test,
  242. },
  243. }); err != nil {
  244. t.Fatalf("failed to send request: %v", err)
  245. }
  246. r, err := stream.Recv()
  247. if err != nil {
  248. // io.EOF is not ok.
  249. t.Fatalf("failed to recv response: %v", err)
  250. }
  251. switch r.MessageResponse.(type) {
  252. case *rpb.ServerReflectionResponse_ErrorResponse:
  253. default:
  254. t.Errorf("FileByFilename(%v) = %v, want type <ServerReflectionResponse_ErrorResponse>", test, r.MessageResponse)
  255. }
  256. }
  257. }
  258. func testFileContainingSymbol(t *testing.T, stream rpb.ServerReflection_ServerReflectionInfoClient) {
  259. for _, test := range []struct {
  260. symbol string
  261. want []byte
  262. }{
  263. {"grpc.testing.SearchService", fdTestByte},
  264. {"grpc.testing.SearchService.Search", fdTestByte},
  265. {"grpc.testing.SearchService.StreamingSearch", fdTestByte},
  266. {"grpc.testing.SearchResponse", fdTestByte},
  267. {"grpc.testing.ToBeExtended", fdProto2Byte},
  268. // Test support package v3.
  269. {"grpc.testingv3.SearchServiceV3", fdTestv3Byte},
  270. {"grpc.testingv3.SearchServiceV3.Search", fdTestv3Byte},
  271. {"grpc.testingv3.SearchServiceV3.StreamingSearch", fdTestv3Byte},
  272. {"grpc.testingv3.SearchResponseV3", fdTestv3Byte},
  273. // search for field, oneof, enum, and enum value symbols, too
  274. {"grpc.testingv3.SearchResponseV3.Result.snippets", fdTestv3Byte},
  275. {"grpc.testingv3.SearchResponseV3.Result.Value.val", fdTestv3Byte},
  276. {"grpc.testingv3.SearchResponseV3.Result.Value.str", fdTestv3Byte},
  277. {"grpc.testingv3.SearchResponseV3.State", fdTestv3Byte},
  278. {"grpc.testingv3.SearchResponseV3.State.FRESH", fdTestv3Byte},
  279. } {
  280. if err := stream.Send(&rpb.ServerReflectionRequest{
  281. MessageRequest: &rpb.ServerReflectionRequest_FileContainingSymbol{
  282. FileContainingSymbol: test.symbol,
  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 *rpb.ServerReflectionResponse_FileDescriptorResponse:
  294. if !reflect.DeepEqual(r.GetFileDescriptorResponse().FileDescriptorProto[0], test.want) {
  295. t.Errorf("FileContainingSymbol(%v)\nreceived: %q,\nwant: %q", test.symbol, r.GetFileDescriptorResponse().FileDescriptorProto[0], test.want)
  296. }
  297. default:
  298. t.Errorf("FileContainingSymbol(%v) = %v, want type <ServerReflectionResponse_FileDescriptorResponse>", test.symbol, r.MessageResponse)
  299. }
  300. }
  301. }
  302. func testFileContainingSymbolError(t *testing.T, stream rpb.ServerReflection_ServerReflectionInfoClient) {
  303. for _, test := range []string{
  304. "grpc.testing.SerchService",
  305. "grpc.testing.SearchService.SearchE",
  306. "grpc.tesing.SearchResponse",
  307. "gpc.testing.ToBeExtended",
  308. } {
  309. if err := stream.Send(&rpb.ServerReflectionRequest{
  310. MessageRequest: &rpb.ServerReflectionRequest_FileContainingSymbol{
  311. FileContainingSymbol: test,
  312. },
  313. }); err != nil {
  314. t.Fatalf("failed to send request: %v", err)
  315. }
  316. r, err := stream.Recv()
  317. if err != nil {
  318. // io.EOF is not ok.
  319. t.Fatalf("failed to recv response: %v", err)
  320. }
  321. switch r.MessageResponse.(type) {
  322. case *rpb.ServerReflectionResponse_ErrorResponse:
  323. default:
  324. t.Errorf("FileContainingSymbol(%v) = %v, want type <ServerReflectionResponse_ErrorResponse>", test, r.MessageResponse)
  325. }
  326. }
  327. }
  328. func testFileContainingExtension(t *testing.T, stream rpb.ServerReflection_ServerReflectionInfoClient) {
  329. for _, test := range []struct {
  330. typeName string
  331. extNum int32
  332. want []byte
  333. }{
  334. {"grpc.testing.ToBeExtended", 13, fdProto2ExtByte},
  335. {"grpc.testing.ToBeExtended", 17, fdProto2ExtByte},
  336. {"grpc.testing.ToBeExtended", 19, fdProto2ExtByte},
  337. {"grpc.testing.ToBeExtended", 23, fdProto2Ext2Byte},
  338. {"grpc.testing.ToBeExtended", 29, fdProto2Ext2Byte},
  339. } {
  340. if err := stream.Send(&rpb.ServerReflectionRequest{
  341. MessageRequest: &rpb.ServerReflectionRequest_FileContainingExtension{
  342. FileContainingExtension: &rpb.ExtensionRequest{
  343. ContainingType: test.typeName,
  344. ExtensionNumber: test.extNum,
  345. },
  346. },
  347. }); err != nil {
  348. t.Fatalf("failed to send request: %v", err)
  349. }
  350. r, err := stream.Recv()
  351. if err != nil {
  352. // io.EOF is not ok.
  353. t.Fatalf("failed to recv response: %v", err)
  354. }
  355. switch r.MessageResponse.(type) {
  356. case *rpb.ServerReflectionResponse_FileDescriptorResponse:
  357. if !reflect.DeepEqual(r.GetFileDescriptorResponse().FileDescriptorProto[0], test.want) {
  358. t.Errorf("FileContainingExtension(%v, %v)\nreceived: %q,\nwant: %q", test.typeName, test.extNum, r.GetFileDescriptorResponse().FileDescriptorProto[0], test.want)
  359. }
  360. default:
  361. t.Errorf("FileContainingExtension(%v, %v) = %v, want type <ServerReflectionResponse_FileDescriptorResponse>", test.typeName, test.extNum, r.MessageResponse)
  362. }
  363. }
  364. }
  365. func testFileContainingExtensionError(t *testing.T, stream rpb.ServerReflection_ServerReflectionInfoClient) {
  366. for _, test := range []struct {
  367. typeName string
  368. extNum int32
  369. }{
  370. {"grpc.testing.ToBExtended", 17},
  371. {"grpc.testing.ToBeExtended", 15},
  372. } {
  373. if err := stream.Send(&rpb.ServerReflectionRequest{
  374. MessageRequest: &rpb.ServerReflectionRequest_FileContainingExtension{
  375. FileContainingExtension: &rpb.ExtensionRequest{
  376. ContainingType: test.typeName,
  377. ExtensionNumber: test.extNum,
  378. },
  379. },
  380. }); err != nil {
  381. t.Fatalf("failed to send request: %v", err)
  382. }
  383. r, err := stream.Recv()
  384. if err != nil {
  385. // io.EOF is not ok.
  386. t.Fatalf("failed to recv response: %v", err)
  387. }
  388. switch r.MessageResponse.(type) {
  389. case *rpb.ServerReflectionResponse_ErrorResponse:
  390. default:
  391. t.Errorf("FileContainingExtension(%v, %v) = %v, want type <ServerReflectionResponse_FileDescriptorResponse>", test.typeName, test.extNum, r.MessageResponse)
  392. }
  393. }
  394. }
  395. func testAllExtensionNumbersOfType(t *testing.T, stream rpb.ServerReflection_ServerReflectionInfoClient) {
  396. for _, test := range []struct {
  397. typeName string
  398. want []int32
  399. }{
  400. {"grpc.testing.ToBeExtended", []int32{13, 17, 19, 23, 29}},
  401. } {
  402. if err := stream.Send(&rpb.ServerReflectionRequest{
  403. MessageRequest: &rpb.ServerReflectionRequest_AllExtensionNumbersOfType{
  404. AllExtensionNumbersOfType: test.typeName,
  405. },
  406. }); err != nil {
  407. t.Fatalf("failed to send request: %v", err)
  408. }
  409. r, err := stream.Recv()
  410. if err != nil {
  411. // io.EOF is not ok.
  412. t.Fatalf("failed to recv response: %v", err)
  413. }
  414. switch r.MessageResponse.(type) {
  415. case *rpb.ServerReflectionResponse_AllExtensionNumbersResponse:
  416. extNum := r.GetAllExtensionNumbersResponse().ExtensionNumber
  417. sort.Sort(intArray(extNum))
  418. if r.GetAllExtensionNumbersResponse().BaseTypeName != test.typeName ||
  419. !reflect.DeepEqual(extNum, test.want) {
  420. t.Errorf("AllExtensionNumbersOfType(%v)\nreceived: %v,\nwant: {%q %v}", r.GetAllExtensionNumbersResponse(), test.typeName, test.typeName, test.want)
  421. }
  422. default:
  423. t.Errorf("AllExtensionNumbersOfType(%v) = %v, want type <ServerReflectionResponse_AllExtensionNumbersResponse>", test.typeName, r.MessageResponse)
  424. }
  425. }
  426. }
  427. func testAllExtensionNumbersOfTypeError(t *testing.T, stream rpb.ServerReflection_ServerReflectionInfoClient) {
  428. for _, test := range []string{
  429. "grpc.testing.ToBeExtendedE",
  430. } {
  431. if err := stream.Send(&rpb.ServerReflectionRequest{
  432. MessageRequest: &rpb.ServerReflectionRequest_AllExtensionNumbersOfType{
  433. AllExtensionNumbersOfType: test,
  434. },
  435. }); err != nil {
  436. t.Fatalf("failed to send request: %v", err)
  437. }
  438. r, err := stream.Recv()
  439. if err != nil {
  440. // io.EOF is not ok.
  441. t.Fatalf("failed to recv response: %v", err)
  442. }
  443. switch r.MessageResponse.(type) {
  444. case *rpb.ServerReflectionResponse_ErrorResponse:
  445. default:
  446. t.Errorf("AllExtensionNumbersOfType(%v) = %v, want type <ServerReflectionResponse_ErrorResponse>", test, r.MessageResponse)
  447. }
  448. }
  449. }
  450. func testListServices(t *testing.T, stream rpb.ServerReflection_ServerReflectionInfoClient) {
  451. if err := stream.Send(&rpb.ServerReflectionRequest{
  452. MessageRequest: &rpb.ServerReflectionRequest_ListServices{},
  453. }); err != nil {
  454. t.Fatalf("failed to send request: %v", err)
  455. }
  456. r, err := stream.Recv()
  457. if err != nil {
  458. // io.EOF is not ok.
  459. t.Fatalf("failed to recv response: %v", err)
  460. }
  461. switch r.MessageResponse.(type) {
  462. case *rpb.ServerReflectionResponse_ListServicesResponse:
  463. services := r.GetListServicesResponse().Service
  464. want := []string{
  465. "grpc.testingv3.SearchServiceV3",
  466. "grpc.testing.SearchService",
  467. "grpc.reflection.v1alpha.ServerReflection",
  468. }
  469. // Compare service names in response with want.
  470. if len(services) != len(want) {
  471. t.Errorf("= %v, want service names: %v", services, want)
  472. }
  473. m := make(map[string]int)
  474. for _, e := range services {
  475. m[e.Name]++
  476. }
  477. for _, e := range want {
  478. if m[e] > 0 {
  479. m[e]--
  480. continue
  481. }
  482. t.Errorf("ListService\nreceived: %v,\nwant: %q", services, want)
  483. }
  484. default:
  485. t.Errorf("ListServices = %v, want type <ServerReflectionResponse_ListServicesResponse>", r.MessageResponse)
  486. }
  487. }