forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSQLBackendMessageTests.swift
295 lines (255 loc) · 14.2 KB
/
PSQLBackendMessageTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import NIOCore
import NIOEmbedded
import NIOTestUtils
import XCTest
@testable import PostgresNIO
class PSQLBackendMessageTests: XCTestCase {
// MARK: ID
func testInitMessageIDWithBytes() {
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "R")), .authentication)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "K")), .backendKeyData)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "2")), .bindComplete)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "3")), .closeComplete)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "C")), .commandComplete)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "d")), .copyData)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "c")), .copyDone)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "G")), .copyInResponse)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "H")), .copyOutResponse)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "W")), .copyBothResponse)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "D")), .dataRow)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "I")), .emptyQueryResponse)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "E")), .error)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "V")), .functionCallResponse)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "v")), .negotiateProtocolVersion)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "n")), .noData)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "N")), .noticeResponse)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "A")), .notificationResponse)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "t")), .parameterDescription)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "S")), .parameterStatus)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "1")), .parseComplete)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "s")), .portalSuspended)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "Z")), .readyForQuery)
XCTAssertEqual(PSQLBackendMessage.ID(rawValue: UInt8(ascii: "T")), .rowDescription)
XCTAssertNil(PSQLBackendMessage.ID(rawValue: 0))
}
func testMessageIDHasCorrectRawValue() {
XCTAssertEqual(PSQLBackendMessage.ID.authentication.rawValue, UInt8(ascii: "R"))
XCTAssertEqual(PSQLBackendMessage.ID.backendKeyData.rawValue, UInt8(ascii: "K"))
XCTAssertEqual(PSQLBackendMessage.ID.bindComplete.rawValue, UInt8(ascii: "2"))
XCTAssertEqual(PSQLBackendMessage.ID.closeComplete.rawValue, UInt8(ascii: "3"))
XCTAssertEqual(PSQLBackendMessage.ID.commandComplete.rawValue, UInt8(ascii: "C"))
XCTAssertEqual(PSQLBackendMessage.ID.copyData.rawValue, UInt8(ascii: "d"))
XCTAssertEqual(PSQLBackendMessage.ID.copyDone.rawValue, UInt8(ascii: "c"))
XCTAssertEqual(PSQLBackendMessage.ID.copyInResponse.rawValue, UInt8(ascii: "G"))
XCTAssertEqual(PSQLBackendMessage.ID.copyOutResponse.rawValue, UInt8(ascii: "H"))
XCTAssertEqual(PSQLBackendMessage.ID.copyBothResponse.rawValue, UInt8(ascii: "W"))
XCTAssertEqual(PSQLBackendMessage.ID.dataRow.rawValue, UInt8(ascii: "D"))
XCTAssertEqual(PSQLBackendMessage.ID.emptyQueryResponse.rawValue, UInt8(ascii: "I"))
XCTAssertEqual(PSQLBackendMessage.ID.error.rawValue, UInt8(ascii: "E"))
XCTAssertEqual(PSQLBackendMessage.ID.functionCallResponse.rawValue, UInt8(ascii: "V"))
XCTAssertEqual(PSQLBackendMessage.ID.negotiateProtocolVersion.rawValue, UInt8(ascii: "v"))
XCTAssertEqual(PSQLBackendMessage.ID.noData.rawValue, UInt8(ascii: "n"))
XCTAssertEqual(PSQLBackendMessage.ID.noticeResponse.rawValue, UInt8(ascii: "N"))
XCTAssertEqual(PSQLBackendMessage.ID.notificationResponse.rawValue, UInt8(ascii: "A"))
XCTAssertEqual(PSQLBackendMessage.ID.parameterDescription.rawValue, UInt8(ascii: "t"))
XCTAssertEqual(PSQLBackendMessage.ID.parameterStatus.rawValue, UInt8(ascii: "S"))
XCTAssertEqual(PSQLBackendMessage.ID.parseComplete.rawValue, UInt8(ascii: "1"))
XCTAssertEqual(PSQLBackendMessage.ID.portalSuspended.rawValue, UInt8(ascii: "s"))
XCTAssertEqual(PSQLBackendMessage.ID.readyForQuery.rawValue, UInt8(ascii: "Z"))
XCTAssertEqual(PSQLBackendMessage.ID.rowDescription.rawValue, UInt8(ascii: "T"))
}
// MARK: Decoder
func testSSLSupportedAsFirstByte() {
var buffer = ByteBuffer()
buffer.writeInteger(UInt8(ascii: "S"))
var expectedMessages: [PSQLBackendMessage] = [.sslSupported]
// we test tons of ParameterStatus messages after the SSLSupported message, since those are
// also identified by an "S"
let parameterStatus: [PSQLBackendMessage.ParameterStatus] = [
.init(parameter: "DateStyle", value: "ISO, MDY"),
.init(parameter: "application_name", value: ""),
.init(parameter: "server_encoding", value: "UTF8"),
.init(parameter: "integer_datetimes", value: "on"),
.init(parameter: "client_encoding", value: "UTF8"),
.init(parameter: "TimeZone", value: "Etc/UTC"),
.init(parameter: "is_superuser", value: "on"),
.init(parameter: "server_version", value: "13.1 (Debian 13.1-1.pgdg100+1)"),
.init(parameter: "session_authorization", value: "postgres"),
.init(parameter: "IntervalStyle", value: "postgres"),
.init(parameter: "standard_conforming_strings", value: "on"),
]
parameterStatus.forEach { parameterStatus in
buffer.writeBackendMessage(id: .parameterStatus) { buffer in
buffer.writeNullTerminatedString(parameterStatus.parameter)
buffer.writeNullTerminatedString(parameterStatus.value)
}
expectedMessages.append(.parameterStatus(parameterStatus))
}
let handler = ByteToMessageHandler(PSQLBackendMessageDecoder())
let embedded = EmbeddedChannel(handler: handler)
XCTAssertNoThrow(try embedded.writeInbound(buffer))
for expected in expectedMessages {
var message: PSQLBackendMessage?
XCTAssertNoThrow(message = try embedded.readInbound(as: PSQLBackendMessage.self))
XCTAssertEqual(message, expected)
}
}
func testSSLUnsupportedAsFirstByte() {
var buffer = ByteBuffer()
buffer.writeInteger(UInt8(ascii: "N"))
// we test a NoticeResponse messages after the SSLUnupported message, since NoticeResponse
// is identified by a "N"
let fields: [PSQLBackendMessage.Field : String] = [
.file: "auth.c",
.routine: "auth_failed",
.line: "334",
.localizedSeverity: "FATAL",
.sqlState: "28P01",
.severity: "FATAL",
.message: "password authentication failed for user \"postgre3\"",
]
let expectedMessages: [PSQLBackendMessage] = [
.sslUnsupported,
.notice(.init(fields: fields))
]
buffer.writeBackendMessage(id: .noticeResponse) { buffer in
fields.forEach { (key, value) in
buffer.writeInteger(key.rawValue, as: UInt8.self)
buffer.writeNullTerminatedString(value)
}
buffer.writeInteger(0, as: UInt8.self) // signal done
}
let handler = ByteToMessageHandler(PSQLBackendMessageDecoder())
let embedded = EmbeddedChannel(handler: handler)
XCTAssertNoThrow(try embedded.writeInbound(buffer))
for expected in expectedMessages {
var message: PSQLBackendMessage?
XCTAssertNoThrow(message = try embedded.readInbound(as: PSQLBackendMessage.self))
XCTAssertEqual(message, expected)
}
}
func testPayloadsWithoutAssociatedValues() {
let messageIDs: [PSQLBackendMessage.ID] = [
.bindComplete,
.closeComplete,
.emptyQueryResponse,
.noData,
.parseComplete,
.portalSuspended
]
var buffer = ByteBuffer()
messageIDs.forEach { messageID in
buffer.writeBackendMessage(id: messageID) { _ in }
}
let expected: [PSQLBackendMessage] = [
.bindComplete,
.closeComplete,
.emptyQueryResponse,
.noData,
.parseComplete,
.portalSuspended
]
XCTAssertNoThrow(try ByteToMessageDecoderVerifier.verifyDecoder(
inputOutputPairs: [(buffer, expected)],
decoderFactory: { PSQLBackendMessageDecoder(hasAlreadyReceivedBytes: false) }))
}
func testPayloadsWithoutAssociatedValuesInvalidLength() {
let messageIDs: [PSQLBackendMessage.ID] = [
.bindComplete,
.closeComplete,
.emptyQueryResponse,
.noData,
.parseComplete,
.portalSuspended
]
for messageID in messageIDs {
var buffer = ByteBuffer()
buffer.writeBackendMessage(id: messageID) { buffer in
buffer.writeInteger(UInt8(0))
}
XCTAssertThrowsError(try ByteToMessageDecoderVerifier.verifyDecoder(
inputOutputPairs: [(buffer, [])],
decoderFactory: { PSQLBackendMessageDecoder(hasAlreadyReceivedBytes: false) })) {
XCTAssert($0 is PSQLDecodingError)
}
}
}
func testDecodeCommandCompleteMessage() {
let expected: [PSQLBackendMessage] = [
.commandComplete("SELECT 100"),
.commandComplete("INSERT 0 1"),
.commandComplete("UPDATE 1"),
.commandComplete("DELETE 1")
]
var okBuffer = ByteBuffer()
expected.forEach { message in
guard case .commandComplete(let commandTag) = message else {
return XCTFail("Programming error!")
}
okBuffer.writeBackendMessage(id: .commandComplete) { buffer in
buffer.writeNullTerminatedString(commandTag)
}
}
XCTAssertNoThrow(try ByteToMessageDecoderVerifier.verifyDecoder(
inputOutputPairs: [(okBuffer, expected)],
decoderFactory: { PSQLBackendMessageDecoder(hasAlreadyReceivedBytes: false) }))
// test commandTag is not null terminated
for message in expected {
guard case .commandComplete(let commandTag) = message else {
return XCTFail("Programming error!")
}
var failBuffer = ByteBuffer()
failBuffer.writeBackendMessage(id: .commandComplete) { buffer in
buffer.writeString(commandTag)
}
XCTAssertThrowsError(try ByteToMessageDecoderVerifier.verifyDecoder(
inputOutputPairs: [(failBuffer, [])],
decoderFactory: { PSQLBackendMessageDecoder(hasAlreadyReceivedBytes: false) })) {
XCTAssert($0 is PSQLDecodingError)
}
}
}
func testDecodeMessageWithUnknownMessageID() {
var buffer = ByteBuffer()
buffer.writeInteger(UInt8(ascii: "x"))
buffer.writeInteger(Int32(4))
XCTAssertThrowsError(try ByteToMessageDecoderVerifier.verifyDecoder(
inputOutputPairs: [(buffer, [])],
decoderFactory: { PSQLBackendMessageDecoder(hasAlreadyReceivedBytes: false) })) {
XCTAssert($0 is PSQLDecodingError)
}
}
func testDebugDescription() {
XCTAssertEqual("\(PSQLBackendMessage.authentication(.ok))", ".authentication(.ok)")
XCTAssertEqual("\(PSQLBackendMessage.authentication(.kerberosV5))",
".authentication(.kerberosV5)")
XCTAssertEqual("\(PSQLBackendMessage.authentication(.md5(salt: (0, 1, 2, 3))))",
".authentication(.md5(salt: (0, 1, 2, 3)))")
XCTAssertEqual("\(PSQLBackendMessage.authentication(.plaintext))",
".authentication(.plaintext)")
XCTAssertEqual("\(PSQLBackendMessage.authentication(.scmCredential))",
".authentication(.scmCredential)")
XCTAssertEqual("\(PSQLBackendMessage.authentication(.gss))",
".authentication(.gss)")
XCTAssertEqual("\(PSQLBackendMessage.authentication(.sspi))",
".authentication(.sspi)")
XCTAssertEqual("\(PSQLBackendMessage.parameterStatus(.init(parameter: "foo", value: "bar")))",
#".parameterStatus(parameter: "foo", value: "bar")"#)
XCTAssertEqual("\(PSQLBackendMessage.backendKeyData(.init(processID: 1234, secretKey: 4567)))",
".backendKeyData(processID: 1234, secretKey: 4567)")
XCTAssertEqual("\(PSQLBackendMessage.bindComplete)", ".bindComplete")
XCTAssertEqual("\(PSQLBackendMessage.closeComplete)", ".closeComplete")
XCTAssertEqual("\(PSQLBackendMessage.commandComplete("SELECT 123"))", #".commandComplete("SELECT 123")"#)
XCTAssertEqual("\(PSQLBackendMessage.emptyQueryResponse)", ".emptyQueryResponse")
XCTAssertEqual("\(PSQLBackendMessage.noData)", ".noData")
XCTAssertEqual("\(PSQLBackendMessage.parseComplete)", ".parseComplete")
XCTAssertEqual("\(PSQLBackendMessage.portalSuspended)", ".portalSuspended")
XCTAssertEqual("\(PSQLBackendMessage.readyForQuery(.idle))", ".readyForQuery(.idle)")
XCTAssertEqual("\(PSQLBackendMessage.readyForQuery(.inTransaction))",
".readyForQuery(.inTransaction)")
XCTAssertEqual("\(PSQLBackendMessage.readyForQuery(.inFailedTransaction))",
".readyForQuery(.inFailedTransaction)")
XCTAssertEqual("\(PSQLBackendMessage.sslSupported)", ".sslSupported")
XCTAssertEqual("\(PSQLBackendMessage.sslUnsupported)", ".sslUnsupported")
}
}