Skip to content

Commit 128eeb9

Browse files
authored
add byte to message verifier test, fixes #41 (#68)
1 parent c860e4a commit 128eeb9

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

Package.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
// swift-tools-version:5.0
1+
// swift-tools-version:5.1
22
import PackageDescription
33

44
let package = Package(
55
name: "postgres-nio",
6+
platforms: [
7+
.macOS(.v10_14)
8+
],
69
products: [
710
.library(name: "PostgresNIO", targets: ["PostgresNIO"]),
811
],
@@ -14,7 +17,11 @@ let package = Package(
1417
],
1518
targets: [
1619
.target(name: "CMD5", dependencies: []),
17-
.target(name: "PostgresNIO", dependencies: ["CMD5", "Logging", "Metrics", "NIO", "NIOSSL"]),
18-
.testTarget(name: "PostgresNIOTests", dependencies: ["PostgresNIO"]),
20+
.target(name: "PostgresNIO", dependencies: [
21+
"CMD5", "Logging", "Metrics", "NIO", "NIOSSL"
22+
]),
23+
.testTarget(name: "PostgresNIOTests", dependencies: [
24+
"PostgresNIO", "NIOTestUtils"
25+
]),
1926
]
2027
)

Sources/PostgresNIO/Message/PostgresMessage+0.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
/// A frontend or backend Postgres message.
2-
public struct PostgresMessage {
3-
public var identifier: Identifier
4-
2+
public struct PostgresMessage: Equatable {
3+
public var identifier: Identifier
54
public var data: ByteBuffer
6-
5+
6+
public init<Data>(identifier: Identifier, bytes: Data)
7+
where Data: Sequence, Data.Element == UInt8
8+
{
9+
var buffer = ByteBufferAllocator().buffer(capacity: 0)
10+
buffer.writeBytes(bytes)
11+
self.init(identifier: identifier, data: buffer)
12+
}
13+
714
public init(identifier: Identifier, data: ByteBuffer) {
815
self.identifier = identifier
916
self.data = data

Sources/PostgresNIO/Message/PostgresMessage+Authentication.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ extension PostgresMessage {
2424
throw PostgresError.protocol("Unkonwn authentication request type: \(type)")
2525
}
2626
}
27+
28+
public func serialize(into buffer: inout ByteBuffer) throws {
29+
switch self {
30+
case .ok:
31+
buffer.writeInteger(0, as: Int32.self)
32+
case .plaintext:
33+
buffer.writeInteger(3, as: Int32.self)
34+
case .md5(let salt):
35+
buffer.writeInteger(5, as: Int32.self)
36+
buffer.writeBytes(salt)
37+
}
38+
}
2739

2840
/// AuthenticationOk
2941
/// Specifies that the authentication was successful.

Sources/PostgresNIO/Message/PostgresMessageDecoder.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public final class PostgresMessageDecoder: ByteToMessageDecoder {
2424
guard let identifier = peekBuffer.readInteger(as: UInt8.self).map(PostgresMessage.Identifier.init) else {
2525
return .needMoreData
2626
}
27-
27+
2828
let message: PostgresMessage
2929

3030
// special ssl case, no body
@@ -48,7 +48,6 @@ public final class PostgresMessageDecoder: ByteToMessageDecoder {
4848

4949
// there is sufficient data, use this buffer
5050
buffer = peekBuffer
51-
5251
context.fireChannelRead(wrapInboundOut(message))
5352
return .continue
5453
}

Tests/PostgresNIOTests/PostgresNIOTests.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Logging
22
import PostgresNIO
33
import XCTest
4+
import NIOTestUtils
45

56
final class PostgresNIOTests: XCTestCase {
67
private var group: EventLoopGroup!
@@ -992,8 +993,42 @@ final class PostgresNIOTests: XCTestCase {
992993
XCTAssertEqual(rows[0].column("int")?.uint8, 5)
993994
}
994995

995-
}
996+
func testMessageDecoder() throws {
997+
let sample: [UInt8] = [
998+
0x52, // R - authentication
999+
0x00, 0x00, 0x00, 0x0C, // length = 12
1000+
0x00, 0x00, 0x00, 0x05, // md5
1001+
0x01, 0x02, 0x03, 0x04, // salt
1002+
0x4B, // B - backend key data
1003+
0x00, 0x00, 0x00, 0x0C, // length = 12
1004+
0x05, 0x05, 0x05, 0x05, // process id
1005+
0x01, 0x01, 0x01, 0x01, // secret key
1006+
]
1007+
var input = ByteBufferAllocator().buffer(capacity: 0)
1008+
input.writeBytes(sample)
9961009

1010+
let output: [PostgresMessage] = [
1011+
PostgresMessage(identifier: .authentication, bytes: [
1012+
0x00, 0x00, 0x00, 0x05,
1013+
0x01, 0x02, 0x03, 0x04,
1014+
]),
1015+
PostgresMessage(identifier: .backendKeyData, bytes: [
1016+
0x05, 0x05, 0x05, 0x05,
1017+
0x01, 0x01, 0x01, 0x01,
1018+
])
1019+
]
1020+
do {
1021+
try ByteToMessageDecoderVerifier.verifyDecoder(
1022+
inputOutputPairs: [(input, output)],
1023+
decoderFactory: {
1024+
PostgresMessageDecoder()
1025+
}
1026+
)
1027+
} catch {
1028+
XCTFail("\(error)")
1029+
}
1030+
}
1031+
}
9971032

9981033
private func performance(function: String = #function) -> Bool {
9991034
if _isDebugAssertConfiguration() {

0 commit comments

Comments
 (0)