|
| 1 | +/* |
| 2 | + * Copyright 2024, 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 | + |
| 17 | +import GRPCCore |
| 18 | +import NIOCore |
| 19 | +import NIOTestUtils |
| 20 | +import XCTest |
| 21 | + |
| 22 | +@testable import GRPCHTTP2Core |
| 23 | + |
| 24 | +final class GRPCMessageDeframerTests: XCTestCase { |
| 25 | + func testReadMultipleMessagesWithoutCompression() throws { |
| 26 | + let firstMessage = { |
| 27 | + var buffer = ByteBuffer() |
| 28 | + buffer.writeInteger(UInt8(0)) |
| 29 | + buffer.writeInteger(UInt32(16)) |
| 30 | + buffer.writeRepeatingByte(42, count: 16) |
| 31 | + return buffer |
| 32 | + }() |
| 33 | + |
| 34 | + let secondMessage = { |
| 35 | + var buffer = ByteBuffer() |
| 36 | + buffer.writeInteger(UInt8(0)) |
| 37 | + buffer.writeInteger(UInt32(8)) |
| 38 | + buffer.writeRepeatingByte(43, count: 8) |
| 39 | + return buffer |
| 40 | + }() |
| 41 | + |
| 42 | + try ByteToMessageDecoderVerifier.verifyDecoder( |
| 43 | + inputOutputPairs: [ |
| 44 | + (firstMessage, [Array(repeating: UInt8(42), count: 16)]), |
| 45 | + (secondMessage, [Array(repeating: UInt8(43), count: 8)]), |
| 46 | + ]) { |
| 47 | + GRPCMessageDeframer() |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + func testReadMessageOverSizeLimitWithoutCompression() throws { |
| 52 | + let deframer = GRPCMessageDeframer(maximumPayloadSize: 100) |
| 53 | + let processor = NIOSingleStepByteToMessageProcessor(deframer) |
| 54 | + |
| 55 | + var buffer = ByteBuffer() |
| 56 | + buffer.writeInteger(UInt8(0)) |
| 57 | + buffer.writeInteger(UInt32(101)) |
| 58 | + buffer.writeRepeatingByte(42, count: 101) |
| 59 | + |
| 60 | + XCTAssertThrowsError( |
| 61 | + ofType: RPCError.self, |
| 62 | + try processor.process(buffer: buffer) { _ in |
| 63 | + XCTFail("No message should be produced.") |
| 64 | + } |
| 65 | + ) { error in |
| 66 | + XCTAssertEqual(error.code, .resourceExhausted) |
| 67 | + XCTAssertEqual( |
| 68 | + error.message, |
| 69 | + "Message has exceeded the configured maximum payload size (max: 100, actual: 101)" |
| 70 | + ) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + func testReadMessageOverSizeLimitButWithoutActualMessageBytes() throws { |
| 75 | + let deframer = GRPCMessageDeframer(maximumPayloadSize: 100) |
| 76 | + let processor = NIOSingleStepByteToMessageProcessor(deframer) |
| 77 | + |
| 78 | + var buffer = ByteBuffer() |
| 79 | + buffer.writeInteger(UInt8(0)) |
| 80 | + // Set the message length field to be over the maximum payload size, but |
| 81 | + // don't write the actual message bytes. This is to ensure that the payload |
| 82 | + // size limit is enforced _before_ the payload is actually read. |
| 83 | + buffer.writeInteger(UInt32(101)) |
| 84 | + |
| 85 | + XCTAssertThrowsError( |
| 86 | + ofType: RPCError.self, |
| 87 | + try processor.process(buffer: buffer) { _ in |
| 88 | + XCTFail("No message should be produced.") |
| 89 | + } |
| 90 | + ) { error in |
| 91 | + XCTAssertEqual(error.code, .resourceExhausted) |
| 92 | + XCTAssertEqual( |
| 93 | + error.message, |
| 94 | + "Message has exceeded the configured maximum payload size (max: 100, actual: 101)" |
| 95 | + ) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + func testCompressedMessageWithoutConfiguringDecompressor() throws { |
| 100 | + let deframer = GRPCMessageDeframer(maximumPayloadSize: 100) |
| 101 | + let processor = NIOSingleStepByteToMessageProcessor(deframer) |
| 102 | + |
| 103 | + var buffer = ByteBuffer() |
| 104 | + buffer.writeInteger(UInt8(1)) |
| 105 | + buffer.writeInteger(UInt32(10)) |
| 106 | + buffer.writeRepeatingByte(42, count: 10) |
| 107 | + |
| 108 | + XCTAssertThrowsError( |
| 109 | + ofType: RPCError.self, |
| 110 | + try processor.process(buffer: buffer) { _ in |
| 111 | + XCTFail("No message should be produced.") |
| 112 | + } |
| 113 | + ) { error in |
| 114 | + XCTAssertEqual(error.code, .internalError) |
| 115 | + XCTAssertEqual( |
| 116 | + error.message, |
| 117 | + "Received a compressed message payload, but no decompressor has been configured." |
| 118 | + ) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private func testReadMultipleMessagesWithCompression(method: Zlib.Method) throws { |
| 123 | + let decompressor = Zlib.Decompressor(method: method) |
| 124 | + let compressor = Zlib.Compressor(method: method) |
| 125 | + var framer = GRPCMessageFramer() |
| 126 | + defer { |
| 127 | + decompressor.end() |
| 128 | + compressor.end() |
| 129 | + } |
| 130 | + |
| 131 | + let firstMessage = try { |
| 132 | + framer.append(Array(repeating: 42, count: 100)) |
| 133 | + return try framer.next(compressor: compressor)! |
| 134 | + }() |
| 135 | + |
| 136 | + let secondMessage = try { |
| 137 | + framer.append(Array(repeating: 43, count: 110)) |
| 138 | + return try framer.next(compressor: compressor)! |
| 139 | + }() |
| 140 | + |
| 141 | + try ByteToMessageDecoderVerifier.verifyDecoder( |
| 142 | + inputOutputPairs: [ |
| 143 | + (firstMessage, [Array(repeating: 42, count: 100)]), |
| 144 | + (secondMessage, [Array(repeating: 43, count: 110)]), |
| 145 | + ]) { |
| 146 | + GRPCMessageDeframer(maximumPayloadSize: 1000, decompressor: decompressor) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + func testReadMultipleMessagesWithDeflateCompression() throws { |
| 151 | + try self.testReadMultipleMessagesWithCompression(method: .deflate) |
| 152 | + } |
| 153 | + |
| 154 | + func testReadMultipleMessagesWithGZIPCompression() throws { |
| 155 | + try self.testReadMultipleMessagesWithCompression(method: .gzip) |
| 156 | + } |
| 157 | + |
| 158 | + func testReadCompressedMessageOverSizeLimitBeforeDecompressing() throws { |
| 159 | + let deframer = GRPCMessageDeframer(maximumPayloadSize: 1) |
| 160 | + let processor = NIOSingleStepByteToMessageProcessor(deframer) |
| 161 | + let compressor = Zlib.Compressor(method: .gzip) |
| 162 | + var framer = GRPCMessageFramer() |
| 163 | + defer { |
| 164 | + compressor.end() |
| 165 | + } |
| 166 | + |
| 167 | + framer.append(Array(repeating: 42, count: 100)) |
| 168 | + let framedMessage = try framer.next(compressor: compressor)! |
| 169 | + |
| 170 | + XCTAssertThrowsError( |
| 171 | + ofType: RPCError.self, |
| 172 | + try processor.process(buffer: framedMessage) { _ in |
| 173 | + XCTFail("No message should be produced.") |
| 174 | + } |
| 175 | + ) { error in |
| 176 | + XCTAssertEqual(error.code, .resourceExhausted) |
| 177 | + XCTAssertEqual( |
| 178 | + error.message, |
| 179 | + """ |
| 180 | + Message has exceeded the configured maximum payload size \ |
| 181 | + (max: 1, actual: \(framedMessage.readableBytes - GRPCMessageDeframer.metadataLength)) |
| 182 | + """ |
| 183 | + ) |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + private func testReadDecompressedMessageOverSizeLimit(method: Zlib.Method) throws { |
| 188 | + let decompressor = Zlib.Decompressor(method: method) |
| 189 | + let deframer = GRPCMessageDeframer(maximumPayloadSize: 100, decompressor: decompressor) |
| 190 | + let processor = NIOSingleStepByteToMessageProcessor(deframer) |
| 191 | + let compressor = Zlib.Compressor(method: method) |
| 192 | + var framer = GRPCMessageFramer() |
| 193 | + defer { |
| 194 | + decompressor.end() |
| 195 | + compressor.end() |
| 196 | + } |
| 197 | + |
| 198 | + framer.append(Array(repeating: 42, count: 101)) |
| 199 | + let framedMessage = try framer.next(compressor: compressor)! |
| 200 | + |
| 201 | + XCTAssertThrowsError( |
| 202 | + ofType: RPCError.self, |
| 203 | + try processor.process(buffer: framedMessage) { _ in |
| 204 | + XCTFail("No message should be produced.") |
| 205 | + } |
| 206 | + ) { error in |
| 207 | + XCTAssertEqual(error.code, .resourceExhausted) |
| 208 | + XCTAssertEqual(error.message, "Message is too large to decompress.") |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + func testReadDecompressedMessageOverSizeLimitWithDeflateCompression() throws { |
| 213 | + try self.testReadDecompressedMessageOverSizeLimit(method: .deflate) |
| 214 | + } |
| 215 | + |
| 216 | + func testReadDecompressedMessageOverSizeLimitWithGZIPCompression() throws { |
| 217 | + try self.testReadDecompressedMessageOverSizeLimit(method: .gzip) |
| 218 | + } |
| 219 | +} |
0 commit comments