GRPCMessageLengthLimitTests.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2021, 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 EchoImplementation
  17. import EchoModel
  18. import GRPC
  19. import NIO
  20. import XCTest
  21. final class GRPCMessageLengthLimitTests: GRPCTestCase {
  22. private var group: EventLoopGroup!
  23. private var server: Server!
  24. private var connection: ClientConnection!
  25. private var echo: Echo_EchoClient {
  26. return Echo_EchoClient(channel: self.connection)
  27. }
  28. override func setUp() {
  29. super.setUp()
  30. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  31. }
  32. override func tearDown() {
  33. XCTAssertNoThrow(try self.connection?.close().wait())
  34. XCTAssertNoThrow(try self.server?.close().wait())
  35. XCTAssertNoThrow(try self.group.syncShutdownGracefully())
  36. super.tearDown()
  37. }
  38. private func startEchoServer(receiveLimit: Int) throws {
  39. self.server = try Server.insecure(group: self.group)
  40. .withServiceProviders([EchoProvider()])
  41. .withMaximumReceiveMessageLength(receiveLimit)
  42. .withLogger(self.serverLogger)
  43. .bind(host: "127.0.0.1", port: 0)
  44. .wait()
  45. }
  46. private func startConnection(receiveLimit: Int) {
  47. self.connection = ClientConnection.insecure(group: self.group)
  48. .withMaximumReceiveMessageLength(receiveLimit)
  49. .withBackgroundActivityLogger(self.clientLogger)
  50. .connect(host: "127.0.0.1", port: self.server.channel.localAddress!.port!)
  51. }
  52. private func makeRequest(minimumLength: Int) -> Echo_EchoRequest {
  53. return .with {
  54. $0.text = String(repeating: "x", count: minimumLength)
  55. }
  56. }
  57. func testServerRejectsLongUnaryRequest() throws {
  58. // Server limits request size to 1024, no client limits.
  59. try self.startEchoServer(receiveLimit: 1024)
  60. self.startConnection(receiveLimit: .max)
  61. let get = self.echo.get(self.makeRequest(minimumLength: 1024))
  62. XCTAssertThrowsError(try get.response.wait())
  63. XCTAssertEqual(try get.status.map { $0.code }.wait(), .internalError)
  64. }
  65. func testServerRejectsLongClientStreamingRequest() throws {
  66. try self.startEchoServer(receiveLimit: 1024)
  67. self.startConnection(receiveLimit: .max)
  68. let collect = self.echo.collect()
  69. XCTAssertNoThrow(try collect.sendMessage(self.makeRequest(minimumLength: 1)).wait())
  70. XCTAssertNoThrow(try collect.sendMessage(self.makeRequest(minimumLength: 1024)).wait())
  71. // (No need to send end, the server is going to close the RPC because the message was too long.)
  72. XCTAssertThrowsError(try collect.response.wait())
  73. XCTAssertEqual(try collect.status.map { $0.code }.wait(), .internalError)
  74. }
  75. func testServerRejectsLongServerStreamingRequest() throws {
  76. try self.startEchoServer(receiveLimit: 1024)
  77. self.startConnection(receiveLimit: .max)
  78. let expand = self.echo.expand(self.makeRequest(minimumLength: 1024)) { _ in
  79. XCTFail("Unexpected response")
  80. }
  81. XCTAssertEqual(try expand.status.map { $0.code }.wait(), .internalError)
  82. }
  83. func testServerRejectsLongBidirectionalStreamingRequest() throws {
  84. try self.startEchoServer(receiveLimit: 1024)
  85. self.startConnection(receiveLimit: .max)
  86. let update = self.echo.update { _ in }
  87. XCTAssertNoThrow(try update.sendMessage(self.makeRequest(minimumLength: 1)).wait())
  88. XCTAssertNoThrow(try update.sendMessage(self.makeRequest(minimumLength: 1024)).wait())
  89. // (No need to send end, the server is going to close the RPC because the message was too long.)
  90. XCTAssertEqual(try update.status.map { $0.code }.wait(), .internalError)
  91. }
  92. func testClientRejectsLongUnaryResponse() throws {
  93. // No server limits, client limits response size to 1024.
  94. try self.startEchoServer(receiveLimit: .max)
  95. self.startConnection(receiveLimit: 1024)
  96. let get = self.echo.get(.with { $0.text = String(repeating: "x", count: 1024) })
  97. XCTAssertThrowsError(try get.response.wait())
  98. XCTAssertEqual(try get.status.map { $0.code }.wait(), .internalError)
  99. }
  100. func testClientRejectsLongClientStreamingResponse() throws {
  101. try self.startEchoServer(receiveLimit: .max)
  102. self.startConnection(receiveLimit: 1024)
  103. let collect = self.echo.collect()
  104. XCTAssertNoThrow(try collect.sendMessage(self.makeRequest(minimumLength: 1)).wait())
  105. XCTAssertNoThrow(try collect.sendMessage(self.makeRequest(minimumLength: 1024)).wait())
  106. XCTAssertNoThrow(try collect.sendEnd().wait())
  107. XCTAssertThrowsError(try collect.response.wait())
  108. XCTAssertEqual(try collect.status.map { $0.code }.wait(), .internalError)
  109. }
  110. func testClientRejectsLongServerStreamingRequest() throws {
  111. try self.startEchoServer(receiveLimit: .max)
  112. self.startConnection(receiveLimit: 1024)
  113. let expand = self.echo.expand(self.makeRequest(minimumLength: 1024)) { _ in
  114. // Expand splits on spaces, there are no spaces in the request and it should be too long for
  115. // the client to expect it.
  116. XCTFail("Unexpected response")
  117. }
  118. XCTAssertEqual(try expand.status.map { $0.code }.wait(), .internalError)
  119. }
  120. func testClientRejectsLongServerBidirectionalStreamingResponse() throws {
  121. try self.startEchoServer(receiveLimit: .max)
  122. self.startConnection(receiveLimit: 1024)
  123. let update = self.echo.update { _ in }
  124. XCTAssertNoThrow(try update.sendMessage(self.makeRequest(minimumLength: 1)).wait())
  125. XCTAssertNoThrow(try update.sendMessage(self.makeRequest(minimumLength: 1024)).wait())
  126. // (No need to send end, the client will close the RPC when it receives a response which is too
  127. // long.
  128. XCTAssertEqual(try update.status.map { $0.code }.wait(), .internalError)
  129. }
  130. }