-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathprotocol_test.go
More file actions
473 lines (384 loc) · 11.6 KB
/
protocol_test.go
File metadata and controls
473 lines (384 loc) · 11.6 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package server
import (
"bytes"
"encoding/binary"
"strings"
"testing"
"github.com/posthog/duckgres/server/wire"
)
func TestReadStartupMessage(t *testing.T) {
t.Run("valid startup message", func(t *testing.T) {
// Build a startup message:
// - 4 bytes length (including itself)
// - 4 bytes protocol version (196608 = 3.0)
// - key-value pairs null-terminated
// - final null byte
var buf bytes.Buffer
params := map[string]string{
"user": "testuser",
"database": "testdb",
}
// Calculate length
bodyLen := 4 // protocol version
for k, v := range params {
bodyLen += len(k) + 1 + len(v) + 1
}
bodyLen++ // final null
// Write length (includes itself)
_ = binary.Write(&buf, binary.BigEndian, int32(bodyLen+4))
// Write protocol version (3.0 = 196608)
_ = binary.Write(&buf, binary.BigEndian, uint32(196608))
// Write params
for k, v := range params {
buf.WriteString(k)
buf.WriteByte(0)
buf.WriteString(v)
buf.WriteByte(0)
}
buf.WriteByte(0) // final null
result, err := wire.ReadStartupMessage(&buf)
if err != nil {
t.Fatalf("wire.ReadStartupMessage() error = %v", err)
}
if result["user"] != "testuser" {
t.Errorf("user = %q, want %q", result["user"], "testuser")
}
if result["database"] != "testdb" {
t.Errorf("database = %q, want %q", result["database"], "testdb")
}
})
t.Run("SSL request", func(t *testing.T) {
var buf bytes.Buffer
// SSL request: length=8, version=80877103
_ = binary.Write(&buf, binary.BigEndian, int32(8))
_ = binary.Write(&buf, binary.BigEndian, uint32(80877103))
result, err := wire.ReadStartupMessage(&buf)
if err != nil {
t.Fatalf("wire.ReadStartupMessage() error = %v", err)
}
if result["__ssl_request"] != "true" {
t.Error("should detect SSL request")
}
})
t.Run("GSSENCRequest", func(t *testing.T) {
var buf bytes.Buffer
// GSSENCRequest: length=8, version=80877104
_ = binary.Write(&buf, binary.BigEndian, int32(8))
_ = binary.Write(&buf, binary.BigEndian, uint32(80877104))
result, err := wire.ReadStartupMessage(&buf)
if err != nil {
t.Fatalf("wire.ReadStartupMessage() error = %v", err)
}
if result["__gssenc_request"] != "true" {
t.Error("should detect GSSENCRequest")
}
})
t.Run("cancel request", func(t *testing.T) {
var buf bytes.Buffer
// Cancel request: length=16, version=80877102, pid, key
_ = binary.Write(&buf, binary.BigEndian, int32(16))
_ = binary.Write(&buf, binary.BigEndian, uint32(80877102))
_ = binary.Write(&buf, binary.BigEndian, uint32(12345)) // pid
_ = binary.Write(&buf, binary.BigEndian, uint32(67890)) // key
result, err := wire.ReadStartupMessage(&buf)
if err != nil {
t.Fatalf("wire.ReadStartupMessage() error = %v", err)
}
if result["__cancel_request"] != "true" {
t.Error("should detect cancel request")
}
})
}
func TestReadMessage(t *testing.T) {
t.Run("simple query message", func(t *testing.T) {
var buf bytes.Buffer
query := "SELECT 1"
// Query message: 'Q' + length + query + null
buf.WriteByte('Q')
_ = binary.Write(&buf, binary.BigEndian, int32(len(query)+5)) // length includes itself and null
buf.WriteString(query)
buf.WriteByte(0)
msgType, body, err := wire.ReadMessage(&buf)
if err != nil {
t.Fatalf("wire.ReadMessage() error = %v", err)
}
if msgType != 'Q' {
t.Errorf("msgType = %c, want Q", msgType)
}
// Body includes the null terminator
expectedBody := query + "\x00"
if string(body) != expectedBody {
t.Errorf("body = %q, want %q", string(body), expectedBody)
}
})
t.Run("terminate message", func(t *testing.T) {
var buf bytes.Buffer
// Terminate message: 'X' + length (4, just the length itself)
buf.WriteByte('X')
_ = binary.Write(&buf, binary.BigEndian, int32(4))
msgType, body, err := wire.ReadMessage(&buf)
if err != nil {
t.Fatalf("wire.ReadMessage() error = %v", err)
}
if msgType != 'X' {
t.Errorf("msgType = %c, want X", msgType)
}
if len(body) != 0 {
t.Errorf("body length = %d, want 0", len(body))
}
})
t.Run("message with binary data", func(t *testing.T) {
var buf bytes.Buffer
data := []byte{0x01, 0x02, 0x03, 0x00, 0xFF}
buf.WriteByte('d') // CopyData
_ = binary.Write(&buf, binary.BigEndian, int32(len(data)+4))
buf.Write(data)
msgType, body, err := wire.ReadMessage(&buf)
if err != nil {
t.Fatalf("wire.ReadMessage() error = %v", err)
}
if msgType != 'd' {
t.Errorf("msgType = %c, want d", msgType)
}
if !bytes.Equal(body, data) {
t.Errorf("body = %v, want %v", body, data)
}
})
}
func TestWriteMessage(t *testing.T) {
t.Run("write simple message", func(t *testing.T) {
var buf bytes.Buffer
data := []byte("test data")
err := wire.WriteMessage(&buf, 'T', data)
if err != nil {
t.Fatalf("wire.WriteMessage() error = %v", err)
}
// Check message type
if buf.Bytes()[0] != 'T' {
t.Errorf("message type = %c, want T", buf.Bytes()[0])
}
// Check length (includes itself = 4)
length := binary.BigEndian.Uint32(buf.Bytes()[1:5])
if length != uint32(len(data)+4) {
t.Errorf("length = %d, want %d", length, len(data)+4)
}
// Check data
if !bytes.Equal(buf.Bytes()[5:], data) {
t.Errorf("data = %v, want %v", buf.Bytes()[5:], data)
}
})
t.Run("write empty message", func(t *testing.T) {
var buf bytes.Buffer
err := wire.WriteMessage(&buf, 'Z', []byte{})
if err != nil {
t.Fatalf("wire.WriteMessage() error = %v", err)
}
// Total should be 5 bytes: type (1) + length (4)
if buf.Len() != 5 {
t.Errorf("buffer length = %d, want 5", buf.Len())
}
// Length should be 4 (just the length field itself)
length := binary.BigEndian.Uint32(buf.Bytes()[1:5])
if length != 4 {
t.Errorf("length = %d, want 4", length)
}
})
}
func TestWriteAuthOK(t *testing.T) {
var buf bytes.Buffer
err := wire.WriteAuthOK(&buf)
if err != nil {
t.Fatalf("wire.WriteAuthOK() error = %v", err)
}
// Message type should be 'R'
if buf.Bytes()[0] != 'R' {
t.Errorf("message type = %c, want R", buf.Bytes()[0])
}
// Length should be 8 (4 for length + 4 for auth type)
length := binary.BigEndian.Uint32(buf.Bytes()[1:5])
if length != 8 {
t.Errorf("length = %d, want 8", length)
}
// Auth type should be 0 (OK)
authType := binary.BigEndian.Uint32(buf.Bytes()[5:9])
if authType != 0 {
t.Errorf("auth type = %d, want 0", authType)
}
}
func TestWriteAuthCleartextPassword(t *testing.T) {
var buf bytes.Buffer
err := wire.WriteAuthCleartextPassword(&buf)
if err != nil {
t.Fatalf("wire.WriteAuthCleartextPassword() error = %v", err)
}
// Auth type should be 3 (cleartext password)
authType := binary.BigEndian.Uint32(buf.Bytes()[5:9])
if authType != 3 {
t.Errorf("auth type = %d, want 3", authType)
}
}
func TestWriteParameterStatus(t *testing.T) {
var buf bytes.Buffer
err := wire.WriteParameterStatus(&buf, "server_version", "15.0")
if err != nil {
t.Fatalf("wire.WriteParameterStatus() error = %v", err)
}
// Message type should be 'S'
if buf.Bytes()[0] != 'S' {
t.Errorf("message type = %c, want S", buf.Bytes()[0])
}
// Check data contains null-terminated name and value
data := buf.Bytes()[5:]
parts := bytes.Split(data, []byte{0})
if string(parts[0]) != "server_version" {
t.Errorf("name = %q, want %q", string(parts[0]), "server_version")
}
if string(parts[1]) != "15.0" {
t.Errorf("value = %q, want %q", string(parts[1]), "15.0")
}
}
func TestWriteBackendKeyData(t *testing.T) {
var buf bytes.Buffer
err := wire.WriteBackendKeyData(&buf, 12345, 67890)
if err != nil {
t.Fatalf("wire.WriteBackendKeyData() error = %v", err)
}
// Message type should be 'K'
if buf.Bytes()[0] != 'K' {
t.Errorf("message type = %c, want K", buf.Bytes()[0])
}
// Length should be 12 (4 for length + 4 for pid + 4 for key)
length := binary.BigEndian.Uint32(buf.Bytes()[1:5])
if length != 12 {
t.Errorf("length = %d, want 12", length)
}
// Check pid
pid := binary.BigEndian.Uint32(buf.Bytes()[5:9])
if pid != 12345 {
t.Errorf("pid = %d, want 12345", pid)
}
// Check key
key := binary.BigEndian.Uint32(buf.Bytes()[9:13])
if key != 67890 {
t.Errorf("key = %d, want 67890", key)
}
}
func TestWriteReadyForQuery(t *testing.T) {
tests := []struct {
name string
txStatus byte
}{
{"idle", 'I'},
{"in transaction", 'T'},
{"failed transaction", 'E'},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
err := wire.WriteReadyForQuery(&buf, tt.txStatus)
if err != nil {
t.Fatalf("wire.WriteReadyForQuery() error = %v", err)
}
// Message type should be 'Z'
if buf.Bytes()[0] != 'Z' {
t.Errorf("message type = %c, want Z", buf.Bytes()[0])
}
// Length should be 5 (4 for length + 1 for status)
length := binary.BigEndian.Uint32(buf.Bytes()[1:5])
if length != 5 {
t.Errorf("length = %d, want 5", length)
}
// Check status
if buf.Bytes()[5] != tt.txStatus {
t.Errorf("txStatus = %c, want %c", buf.Bytes()[5], tt.txStatus)
}
})
}
}
func TestWriteErrorResponse(t *testing.T) {
var buf bytes.Buffer
err := wire.WriteErrorResponse(&buf, "ERROR", "42601", "syntax error")
if err != nil {
t.Fatalf("wire.WriteErrorResponse() error = %v", err)
}
// Message type should be 'E'
if buf.Bytes()[0] != 'E' {
t.Errorf("message type = %c, want E", buf.Bytes()[0])
}
// Parse the error fields
data := buf.Bytes()[5:]
// Should contain 'S' (severity), 'C' (code), 'M' (message)
if !bytes.Contains(data, []byte("SERROR")) {
t.Error("should contain severity ERROR")
}
if !bytes.Contains(data, []byte("C42601")) {
t.Error("should contain code 42601")
}
if !bytes.Contains(data, []byte("Msyntax error")) {
t.Error("should contain message 'syntax error'")
}
}
func TestWriteErrorResponseRedactsPassword(t *testing.T) {
var buf bytes.Buffer
msg := `flight execute update: rpc error: Unable to connect to Postgres at "host=db.example.com port=5432 user=myuser password=superSecret123 dbname=mydb": connection failed`
err := wire.WriteErrorResponse(&buf, "ERROR", "42000", msg)
if err != nil {
t.Fatalf("wire.WriteErrorResponse() error = %v", err)
}
output := buf.String()
if strings.Contains(output, "superSecret123") {
t.Error("password was not redacted from error response sent to client")
}
if !strings.Contains(output, "[REDACTED]") {
t.Error("expected [REDACTED] placeholder in output")
}
}
func TestWriteNoticeResponse(t *testing.T) {
var buf bytes.Buffer
err := wire.WriteNoticeResponse(&buf, "WARNING", "01000", "some warning")
if err != nil {
t.Fatalf("wire.WriteNoticeResponse() error = %v", err)
}
// Message type should be 'N' (notice)
if buf.Bytes()[0] != 'N' {
t.Errorf("message type = %c, want N", buf.Bytes()[0])
}
data := buf.Bytes()[5:]
if !bytes.Contains(data, []byte("SWARNING")) {
t.Error("should contain severity WARNING")
}
}
func TestMessageRoundTrip(t *testing.T) {
// Test that we can write a message and read it back
testCases := []struct {
name string
msgType byte
data []byte
}{
{"empty", 'Z', []byte{}},
{"single byte", 'T', []byte{42}},
{"text", 'Q', []byte("SELECT 1\x00")},
{"binary", 'd', []byte{0x00, 0xFF, 0x01, 0xFE}},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
// Write
err := wire.WriteMessage(&buf, tc.msgType, tc.data)
if err != nil {
t.Fatalf("wire.WriteMessage() error = %v", err)
}
// Read
msgType, body, err := wire.ReadMessage(&buf)
if err != nil {
t.Fatalf("wire.ReadMessage() error = %v", err)
}
if msgType != tc.msgType {
t.Errorf("msgType = %c, want %c", msgType, tc.msgType)
}
if !bytes.Equal(body, tc.data) {
t.Errorf("body = %v, want %v", body, tc.data)
}
})
}
}