serverreflection_test.go 16 KB

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