| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- /*
- * Copyright 2024, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- import GRPCCore
- import NIOCore
- import NIOTestUtils
- import XCTest
- @testable import GRPCHTTP2Core
- final class GRPCMessageDeframerTests: XCTestCase {
- func testReadMultipleMessagesWithoutCompression() throws {
- let firstMessage = {
- var buffer = ByteBuffer()
- buffer.writeInteger(UInt8(0))
- buffer.writeInteger(UInt32(16))
- buffer.writeRepeatingByte(42, count: 16)
- return buffer
- }()
- let secondMessage = {
- var buffer = ByteBuffer()
- buffer.writeInteger(UInt8(0))
- buffer.writeInteger(UInt32(8))
- buffer.writeRepeatingByte(43, count: 8)
- return buffer
- }()
- try ByteToMessageDecoderVerifier.verifyDecoder(
- inputOutputPairs: [
- (firstMessage, [Array(repeating: UInt8(42), count: 16)]),
- (secondMessage, [Array(repeating: UInt8(43), count: 8)]),
- ]) {
- GRPCMessageDeframer()
- }
- }
- func testReadMessageOverSizeLimitWithoutCompression() throws {
- let deframer = GRPCMessageDeframer(maximumPayloadSize: 100)
- let processor = NIOSingleStepByteToMessageProcessor(deframer)
- var buffer = ByteBuffer()
- buffer.writeInteger(UInt8(0))
- buffer.writeInteger(UInt32(101))
- buffer.writeRepeatingByte(42, count: 101)
- XCTAssertThrowsError(
- ofType: RPCError.self,
- try processor.process(buffer: buffer) { _ in
- XCTFail("No message should be produced.")
- }
- ) { error in
- XCTAssertEqual(error.code, .resourceExhausted)
- XCTAssertEqual(
- error.message,
- "Message has exceeded the configured maximum payload size (max: 100, actual: 101)"
- )
- }
- }
- func testReadMessageOverSizeLimitButWithoutActualMessageBytes() throws {
- let deframer = GRPCMessageDeframer(maximumPayloadSize: 100)
- let processor = NIOSingleStepByteToMessageProcessor(deframer)
- var buffer = ByteBuffer()
- buffer.writeInteger(UInt8(0))
- // Set the message length field to be over the maximum payload size, but
- // don't write the actual message bytes. This is to ensure that the payload
- // size limit is enforced _before_ the payload is actually read.
- buffer.writeInteger(UInt32(101))
- XCTAssertThrowsError(
- ofType: RPCError.self,
- try processor.process(buffer: buffer) { _ in
- XCTFail("No message should be produced.")
- }
- ) { error in
- XCTAssertEqual(error.code, .resourceExhausted)
- XCTAssertEqual(
- error.message,
- "Message has exceeded the configured maximum payload size (max: 100, actual: 101)"
- )
- }
- }
- func testCompressedMessageWithoutConfiguringDecompressor() throws {
- let deframer = GRPCMessageDeframer(maximumPayloadSize: 100)
- let processor = NIOSingleStepByteToMessageProcessor(deframer)
- var buffer = ByteBuffer()
- buffer.writeInteger(UInt8(1))
- buffer.writeInteger(UInt32(10))
- buffer.writeRepeatingByte(42, count: 10)
- XCTAssertThrowsError(
- ofType: RPCError.self,
- try processor.process(buffer: buffer) { _ in
- XCTFail("No message should be produced.")
- }
- ) { error in
- XCTAssertEqual(error.code, .internalError)
- XCTAssertEqual(
- error.message,
- "Received a compressed message payload, but no decompressor has been configured."
- )
- }
- }
- private func testReadMultipleMessagesWithCompression(method: Zlib.Method) throws {
- let decompressor = Zlib.Decompressor(method: method)
- let compressor = Zlib.Compressor(method: method)
- var framer = GRPCMessageFramer()
- defer {
- decompressor.end()
- compressor.end()
- }
- let firstMessage = try {
- framer.append(Array(repeating: 42, count: 100))
- return try framer.next(compressor: compressor)!
- }()
- let secondMessage = try {
- framer.append(Array(repeating: 43, count: 110))
- return try framer.next(compressor: compressor)!
- }()
- try ByteToMessageDecoderVerifier.verifyDecoder(
- inputOutputPairs: [
- (firstMessage, [Array(repeating: 42, count: 100)]),
- (secondMessage, [Array(repeating: 43, count: 110)]),
- ]) {
- GRPCMessageDeframer(maximumPayloadSize: 1000, decompressor: decompressor)
- }
- }
- func testReadMultipleMessagesWithDeflateCompression() throws {
- try self.testReadMultipleMessagesWithCompression(method: .deflate)
- }
- func testReadMultipleMessagesWithGZIPCompression() throws {
- try self.testReadMultipleMessagesWithCompression(method: .gzip)
- }
- func testReadCompressedMessageOverSizeLimitBeforeDecompressing() throws {
- let deframer = GRPCMessageDeframer(maximumPayloadSize: 1)
- let processor = NIOSingleStepByteToMessageProcessor(deframer)
- let compressor = Zlib.Compressor(method: .gzip)
- var framer = GRPCMessageFramer()
- defer {
- compressor.end()
- }
- framer.append(Array(repeating: 42, count: 100))
- let framedMessage = try framer.next(compressor: compressor)!
- XCTAssertThrowsError(
- ofType: RPCError.self,
- try processor.process(buffer: framedMessage) { _ in
- XCTFail("No message should be produced.")
- }
- ) { error in
- XCTAssertEqual(error.code, .resourceExhausted)
- XCTAssertEqual(
- error.message,
- """
- Message has exceeded the configured maximum payload size \
- (max: 1, actual: \(framedMessage.readableBytes - GRPCMessageDeframer.metadataLength))
- """
- )
- }
- }
- private func testReadDecompressedMessageOverSizeLimit(method: Zlib.Method) throws {
- let decompressor = Zlib.Decompressor(method: method)
- let deframer = GRPCMessageDeframer(maximumPayloadSize: 100, decompressor: decompressor)
- let processor = NIOSingleStepByteToMessageProcessor(deframer)
- let compressor = Zlib.Compressor(method: method)
- var framer = GRPCMessageFramer()
- defer {
- decompressor.end()
- compressor.end()
- }
- framer.append(Array(repeating: 42, count: 101))
- let framedMessage = try framer.next(compressor: compressor)!
- XCTAssertThrowsError(
- ofType: RPCError.self,
- try processor.process(buffer: framedMessage) { _ in
- XCTFail("No message should be produced.")
- }
- ) { error in
- XCTAssertEqual(error.code, .resourceExhausted)
- XCTAssertEqual(error.message, "Message is too large to decompress.")
- }
- }
- func testReadDecompressedMessageOverSizeLimitWithDeflateCompression() throws {
- try self.testReadDecompressedMessageOverSizeLimit(method: .deflate)
- }
- func testReadDecompressedMessageOverSizeLimitWithGZIPCompression() throws {
- try self.testReadDecompressedMessageOverSizeLimit(method: .gzip)
- }
- }
|