|
13 | 13 | //===----------------------------------------------------------------------===//
|
14 | 14 |
|
15 | 15 | import NIO
|
| 16 | +import NIOTestUtils |
16 | 17 | @testable import RediStack
|
17 | 18 | import XCTest
|
18 | 19 |
|
@@ -299,3 +300,76 @@ extension RedisByteDecoderTests {
|
299 | 300 | return try decoder.decode(context: context, buffer: &buffer)
|
300 | 301 | }
|
301 | 302 | }
|
| 303 | + |
| 304 | +// MARK: ByteToMessageDecoderVerifier |
| 305 | + |
| 306 | +extension RedisByteDecoderTests { |
| 307 | + func test_validatesBasicAssumptions() throws { |
| 308 | + let inputExpectedOutputPairs: [(String, [RedisByteDecoder.InboundOut])] = [ |
| 309 | + (":1000\r\n:1000\r\n", [.integer(1000), .integer(1000)]), |
| 310 | + (":0\r\n", [.integer(0)]), |
| 311 | + ("*3\r\n+foo\r\n$3\r\nbar\r\n:3\r\n", |
| 312 | + [.array([.simpleString("foo".byteBuffer), .bulkString("bar".byteBuffer), .integer(3)])]), |
| 313 | + ("+👩🏼✈️\r\n++\r\n", [.simpleString("👩🏼✈️".byteBuffer), .simpleString("+".byteBuffer)]), |
| 314 | + ("*2\r\n:1\r\n:2\r\n", [.array([.integer(1), .integer(2)])]), |
| 315 | + ("*2\r\n*1\r\n:1\r\n:2\r\n", [.array([.array([.integer(1)]), .integer(2)])]), |
| 316 | + ("-ERR test\r\n", [.error(.init(reason: "ERR test"))]), |
| 317 | + ("$2\r\n\r\n\r\n$1\r\n\r\r\n", [.bulkString("\r\n".byteBuffer), .bulkString("\r".byteBuffer)]), |
| 318 | + ("$-1\r\n", [.null]), |
| 319 | + (":00000\r\n:\(Int.max)\r\n:\(Int.min)\r\n", [.integer(0), .integer(Int.max), .integer(Int.min)]), |
| 320 | + ] |
| 321 | + XCTAssertNoThrow(try ByteToMessageDecoderVerifier.verifyDecoder( |
| 322 | + stringInputOutputPairs: inputExpectedOutputPairs, |
| 323 | + decoderFactory: RedisByteDecoder.init |
| 324 | + )) |
| 325 | + } |
| 326 | + |
| 327 | + func test_validatesBasicAssumptions_withNonStringRepresentables() throws { |
| 328 | + var buffer = self.allocator.buffer(capacity: 128) |
| 329 | + var incompleteUTF8CodeUnitsAsSimpleAndBulkString: (ByteBuffer, [RESPValue]) { |
| 330 | + buffer.clear() |
| 331 | + var expectedBuffer1 = buffer |
| 332 | + var expectedBuffer2 = buffer |
| 333 | + buffer.writeString("+") |
| 334 | + // UTF8 2 byte sequence with only 1 byte present |
| 335 | + expectedBuffer1.writeInteger(0b110_10101, as: UInt8.self) |
| 336 | + buffer.writeBytes(expectedBuffer1.readableBytesView) |
| 337 | + buffer.writeString("\r\n") |
| 338 | + buffer.writeString("$2\r\n") |
| 339 | + // UTF8 3 byte sequence with only 2 bytes present |
| 340 | + expectedBuffer2.writeInteger(0b1110_1010, as: UInt8.self) |
| 341 | + expectedBuffer2.writeInteger(0b10_101010, as: UInt8.self) |
| 342 | + buffer.writeBytes(expectedBuffer2.readableBytesView) |
| 343 | + buffer.writeString("\r\n") |
| 344 | + return (buffer, [.simpleString(expectedBuffer1), .bulkString(expectedBuffer2)]) |
| 345 | + } |
| 346 | + var boms: (ByteBuffer, [RESPValue]) { |
| 347 | + buffer.clear() |
| 348 | + var expectedBuffer1 = buffer |
| 349 | + var expectedBuffer2 = buffer |
| 350 | + buffer.writeString("+") |
| 351 | + // UTF16 LE BOM |
| 352 | + expectedBuffer1.writeInteger(0xff, as: UInt8.self) |
| 353 | + expectedBuffer1.writeInteger(0xfe, as: UInt8.self) |
| 354 | + buffer.writeBytes(expectedBuffer1.readableBytesView) |
| 355 | + buffer.writeString("\r\n") |
| 356 | + buffer.writeString("$4\r\n") |
| 357 | + // UTF32 BE BOM |
| 358 | + expectedBuffer2.writeInteger(0x00, as: UInt8.self) |
| 359 | + expectedBuffer2.writeInteger(0x00, as: UInt8.self) |
| 360 | + expectedBuffer2.writeInteger(0xFE, as: UInt8.self) |
| 361 | + expectedBuffer2.writeInteger(0xFF, as: UInt8.self) |
| 362 | + buffer.writeBytes(expectedBuffer2.readableBytesView) |
| 363 | + buffer.writeString("\r\n") |
| 364 | + return (buffer, [.simpleString(expectedBuffer1), .bulkString(expectedBuffer2)]) |
| 365 | + } |
| 366 | + let inputExpectedOutputPairs: [(ByteBuffer, [RedisByteDecoder.InboundOut])] = [ |
| 367 | + incompleteUTF8CodeUnitsAsSimpleAndBulkString, |
| 368 | + boms, |
| 369 | + ] |
| 370 | + XCTAssertNoThrow(try ByteToMessageDecoderVerifier.verifyDecoder( |
| 371 | + inputOutputPairs: inputExpectedOutputPairs, |
| 372 | + decoderFactory: RedisByteDecoder.init |
| 373 | + )) |
| 374 | + } |
| 375 | +} |
0 commit comments