ReflectionServiceUnitTests.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright 2023, gRPC Authors All rights reserved.
  3. *
  4. * 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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * 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.
  15. */
  16. import Foundation
  17. import GRPC
  18. import GRPCReflectionService
  19. import SwiftProtobuf
  20. import XCTest
  21. @testable import GRPCReflectionService
  22. final class ReflectionServiceUnitTests: GRPCTestCase {
  23. /// Testing the fileDescriptorDataByFilename dictionary of the ReflectionServiceData object.
  24. func testFileDescriptorDataByFilename() throws {
  25. var protos = makeProtosWithDependencies()
  26. let registry = try ReflectionServiceData(fileDescriptors: protos)
  27. let registryFileDescriptorData = registry.fileDescriptorDataByFilename
  28. for (fileName, protoData) in registryFileDescriptorData {
  29. let serializedFiledescriptorData = protoData.serializedFileDescriptorProto
  30. let dependencyFileNames = protoData.dependencyFileNames
  31. guard let index = protos.firstIndex(where: { $0.name == fileName }) else {
  32. return XCTFail(
  33. """
  34. Could not find the file descriptor proto of \(fileName) \
  35. in the original file descriptor protos list.
  36. """
  37. )
  38. }
  39. let originalProto = protos[index]
  40. XCTAssertEqual(originalProto.name, fileName)
  41. XCTAssertEqual(try originalProto.serializedData(), serializedFiledescriptorData)
  42. XCTAssertEqual(originalProto.dependency, dependencyFileNames)
  43. protos.remove(at: index)
  44. }
  45. XCTAssert(protos.isEmpty)
  46. }
  47. /// Testing the serviceNames array of the ReflectionServiceData object.
  48. func testServiceNames() throws {
  49. let protos = makeProtosWithDependencies()
  50. let servicesNames = protos.serviceNames.sorted()
  51. let registry = try ReflectionServiceData(fileDescriptors: protos)
  52. let registryServices = registry.serviceNames.sorted()
  53. XCTAssertEqual(registryServices, servicesNames)
  54. }
  55. /// Testing the fileNameBySymbol array of the ReflectionServiceData object.
  56. func testFileNameBySymbol() throws {
  57. let protos = makeProtosWithDependencies()
  58. let registry = try ReflectionServiceData(fileDescriptors: protos)
  59. let registryFileNameBySymbol = registry.fileNameBySymbol
  60. var symbolsCount = 0
  61. for proto in protos {
  62. let qualifiedSymbolNames = proto.qualifiedSymbolNames
  63. symbolsCount += qualifiedSymbolNames.count
  64. for qualifiedSymbolName in qualifiedSymbolNames {
  65. XCTAssertEqual(registryFileNameBySymbol[qualifiedSymbolName], proto.name)
  66. }
  67. }
  68. XCTAssertEqual(symbolsCount, registryFileNameBySymbol.count)
  69. }
  70. func testFileNameBySymbolDuplicatedSymbol() throws {
  71. var protos = makeProtosWithDependencies()
  72. protos[1].messageType.append(
  73. Google_Protobuf_DescriptorProto.with {
  74. $0.name = "inputMessage"
  75. $0.field = [
  76. Google_Protobuf_FieldDescriptorProto.with {
  77. $0.name = "inputField"
  78. $0.type = .bool
  79. }
  80. ]
  81. }
  82. )
  83. XCTAssertThrowsError(
  84. try ReflectionServiceData(fileDescriptors: protos)
  85. ) { error in
  86. XCTAssertEqual(
  87. error as? GRPCStatus,
  88. GRPCStatus(
  89. code: .alreadyExists,
  90. message:
  91. """
  92. The packagebar2.inputMessage symbol from bar2.proto \
  93. already exists in bar2.proto.
  94. """
  95. )
  96. )
  97. }
  98. }
  99. // Testing the nameOfFileContainingSymbol method for different types of symbols.
  100. func testNameOfFileContainingSymbolEnum() throws {
  101. let protos = makeProtosWithDependencies()
  102. let registry = try ReflectionServiceData(fileDescriptors: protos)
  103. let fileName = registry.nameOfFileContainingSymbol(named: "packagebar2.enumType2")
  104. XCTAssertEqual(fileName, "bar2.proto")
  105. }
  106. func testNameOfFileContainingSymbolMessage() throws {
  107. let protos = makeProtosWithDependencies()
  108. let registry = try ReflectionServiceData(fileDescriptors: protos)
  109. let fileName = registry.nameOfFileContainingSymbol(named: "packagebar1.inputMessage")
  110. XCTAssertEqual(fileName, "bar1.proto")
  111. }
  112. func testNameOfFileContainingSymbolService() throws {
  113. let protos = makeProtosWithDependencies()
  114. let registry = try ReflectionServiceData(fileDescriptors: protos)
  115. let fileName = registry.nameOfFileContainingSymbol(named: "packagebar3.service3")
  116. XCTAssertEqual(fileName, "bar3.proto")
  117. }
  118. func testNameOfFileContainingSymbolMethod() throws {
  119. let protos = makeProtosWithDependencies()
  120. let registry = try ReflectionServiceData(fileDescriptors: protos)
  121. let fileName = registry.nameOfFileContainingSymbol(
  122. named: "packagebar4.service4.testMethod4"
  123. )
  124. XCTAssertEqual(fileName, "bar4.proto")
  125. }
  126. func testNameOfFileContainingSymbolNonExistentSymbol() throws {
  127. let protos = makeProtosWithDependencies()
  128. let registry = try ReflectionServiceData(fileDescriptors: protos)
  129. let fileName = registry.nameOfFileContainingSymbol(named: "packagebar2.enumType3")
  130. XCTAssertEqual(fileName, nil)
  131. }
  132. // Testing the serializedFileDescriptorProto method in different cases.
  133. func testSerialisedFileDescriptorProtosForDependenciesOfFile() throws {
  134. var protos = makeProtosWithDependencies()
  135. let registry = try ReflectionServiceData(fileDescriptors: protos)
  136. let serializedFileDescriptorProtos =
  137. try registry
  138. .serialisedFileDescriptorProtosForDependenciesOfFile(named: "bar1.proto")
  139. let fileDescriptorProtos = try serializedFileDescriptorProtos.map {
  140. try Google_Protobuf_FileDescriptorProto(serializedData: $0)
  141. }
  142. // Tests that the functions returns all the transitive dependencies, with their services and
  143. // methods, together with the initial proto, as serialized data.
  144. XCTAssertEqual(fileDescriptorProtos.count, 4)
  145. for fileDescriptorProto in fileDescriptorProtos {
  146. guard let protoIndex = protos.firstIndex(of: fileDescriptorProto) else {
  147. return XCTFail(
  148. """
  149. Could not find the file descriptor proto of \(fileDescriptorProto.name) \
  150. in the original file descriptor protos list.
  151. """
  152. )
  153. }
  154. for service in fileDescriptorProto.service {
  155. guard let serviceIndex = protos[protoIndex].service.firstIndex(of: service) else {
  156. return XCTFail(
  157. """
  158. Could not find the \(service.name) in the service \
  159. list of the \(fileDescriptorProto.name) file descriptor proto.
  160. """
  161. )
  162. }
  163. let originalMethods = protos[protoIndex].service[serviceIndex].method
  164. for method in service.method {
  165. XCTAssert(originalMethods.contains(method))
  166. }
  167. for messageType in fileDescriptorProto.messageType {
  168. XCTAssert(protos[protoIndex].messageType.contains(messageType))
  169. }
  170. }
  171. protos.removeAll { $0 == fileDescriptorProto }
  172. }
  173. XCTAssert(protos.isEmpty)
  174. }
  175. func testSerialisedFileDescriptorProtosForDependenciesOfFileComplexDependencyGraph() throws {
  176. var protos = makeProtosWithComplexDependencies()
  177. let registry = try ReflectionServiceData(fileDescriptors: protos)
  178. let serializedFileDescriptorProtos =
  179. try registry
  180. .serialisedFileDescriptorProtosForDependenciesOfFile(named: "foo0.proto")
  181. let fileDescriptorProtos = try serializedFileDescriptorProtos.map {
  182. try Google_Protobuf_FileDescriptorProto(serializedData: $0)
  183. }
  184. // Tests that the functions returns all the tranzitive dependencies, with their services and
  185. // methods, together with the initial proto, as serialized data.
  186. XCTAssertEqual(fileDescriptorProtos.count, 21)
  187. for fileDescriptorProto in fileDescriptorProtos {
  188. guard let protoIndex = protos.firstIndex(of: fileDescriptorProto) else {
  189. return XCTFail(
  190. """
  191. Could not find the file descriptor proto of \(fileDescriptorProto.name) \
  192. in the original file descriptor protos list.
  193. """
  194. )
  195. }
  196. for service in fileDescriptorProto.service {
  197. guard let serviceIndex = protos[protoIndex].service.firstIndex(of: service) else {
  198. return XCTFail(
  199. """
  200. Could not find the \(service.name) in the service \
  201. list of the \(fileDescriptorProto.name) file descriptor proto.
  202. """
  203. )
  204. }
  205. let originalMethods = protos[protoIndex].service[serviceIndex].method
  206. for method in service.method {
  207. XCTAssert(originalMethods.contains(method))
  208. }
  209. for messageType in fileDescriptorProto.messageType {
  210. XCTAssert(protos[protoIndex].messageType.contains(messageType))
  211. }
  212. }
  213. protos.removeAll { $0 == fileDescriptorProto }
  214. }
  215. XCTAssert(protos.isEmpty)
  216. }
  217. func testSerialisedFileDescriptorProtosForDependenciesOfFileDependencyLoops() throws {
  218. var protos = makeProtosWithDependencies()
  219. // Making dependencies of the "bar1.proto" to depend on "bar1.proto".
  220. protos[1].dependency.append("bar1.proto")
  221. protos[2].dependency.append("bar1.proto")
  222. protos[3].dependency.append("bar1.proto")
  223. let registry = try ReflectionServiceData(fileDescriptors: protos)
  224. let serializedFileDescriptorProtos =
  225. try registry
  226. .serialisedFileDescriptorProtosForDependenciesOfFile(named: "bar1.proto")
  227. let fileDescriptorProtos = try serializedFileDescriptorProtos.map {
  228. try Google_Protobuf_FileDescriptorProto(serializedData: $0)
  229. }
  230. // Test that we get only 4 serialized File Descriptor Protos as response.
  231. XCTAssertEqual(fileDescriptorProtos.count, 4)
  232. for fileDescriptorProto in fileDescriptorProtos {
  233. guard let protoIndex = protos.firstIndex(of: fileDescriptorProto) else {
  234. return XCTFail(
  235. """
  236. Could not find the file descriptor proto of \(fileDescriptorProto.name) \
  237. in the original file descriptor protos list.
  238. """
  239. )
  240. }
  241. for service in fileDescriptorProto.service {
  242. guard let serviceIndex = protos[protoIndex].service.firstIndex(of: service) else {
  243. return XCTFail(
  244. """
  245. Could not find the \(service.name) in the service \
  246. list of the \(fileDescriptorProto.name) file descriptor proto.
  247. """
  248. )
  249. }
  250. let originalMethods = protos[protoIndex].service[serviceIndex].method
  251. for method in service.method {
  252. XCTAssert(originalMethods.contains(method))
  253. }
  254. for messageType in fileDescriptorProto.messageType {
  255. XCTAssert(protos[protoIndex].messageType.contains(messageType))
  256. }
  257. }
  258. protos.removeAll { $0 == fileDescriptorProto }
  259. }
  260. XCTAssert(protos.isEmpty)
  261. }
  262. func testSerialisedFileDescriptorProtosForDependenciesOfFileInvalidFile() throws {
  263. let protos = makeProtosWithDependencies()
  264. let registry = try ReflectionServiceData(fileDescriptors: protos)
  265. XCTAssertThrowsError(
  266. try registry.serialisedFileDescriptorProtosForDependenciesOfFile(named: "invalid.proto")
  267. ) { error in
  268. XCTAssertEqual(
  269. error as? GRPCStatus,
  270. GRPCStatus(
  271. code: .notFound,
  272. message: "The provided file or a dependency of the provided file could not be found."
  273. )
  274. )
  275. }
  276. }
  277. func testSerialisedFileDescriptorProtosForDependenciesOfFileDependencyNotProto() throws {
  278. var protos = makeProtosWithDependencies()
  279. protos[0].dependency.append("invalidDependency")
  280. let registry = try ReflectionServiceData(fileDescriptors: protos)
  281. XCTAssertThrowsError(
  282. try registry.serialisedFileDescriptorProtosForDependenciesOfFile(named: "bar1.proto")
  283. ) { error in
  284. XCTAssertEqual(
  285. error as? GRPCStatus,
  286. GRPCStatus(
  287. code: .notFound,
  288. message: "The provided file or a dependency of the provided file could not be found."
  289. )
  290. )
  291. }
  292. }
  293. }