Skip to content

Commit 556da64

Browse files
committed
56 -- Add ByteToMessageDecoderVerifier unit tests
1 parent cd40320 commit 556da64

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

Package.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ let package = Package(
2929
targets: [
3030
.target(name: "RediStack", dependencies: ["NIO", "Logging", "Metrics"]),
3131
.target(name: "RediStackTestUtils", dependencies: ["NIO", "RediStack"]),
32-
.testTarget(name: "RediStackTests", dependencies: ["RediStack", "NIO", "RediStackTestUtils"]),
32+
.testTarget(name: "RediStackTests", dependencies: [
33+
"RediStack", "NIO", "RediStackTestUtils", "NIOTestUtils"
34+
]),
3335
.testTarget(name: "RediStackIntegrationTests", dependencies: ["RediStack", "NIO", "RediStackTestUtils"])
3436
]
3537
)

Tests/RediStackTests/ChannelHandlers/RedisByteDecoderTests.swift

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import NIO
16+
import NIOTestUtils
1617
@testable import RediStack
1718
import XCTest
1819

@@ -299,3 +300,76 @@ extension RedisByteDecoderTests {
299300
return try decoder.decode(context: context, buffer: &buffer)
300301
}
301302
}
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

Comments
 (0)