forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgsql_test.go
157 lines (127 loc) · 4.61 KB
/
pgsql_test.go
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
package main
import (
"encoding/hex"
"testing"
"time"
)
// Test parsing a request with a single query
func TestPgsqlParser_simpleRequest(t *testing.T) {
data := []byte(
"510000001a53454c454354202a2046524f4d20466f6f6261723b00")
message, err := hex.DecodeString(string(data))
if err != nil {
t.Error("Failed to decode hex string")
}
stream := &PgsqlStream{tcpStream: nil, data: message, message: new(PgsqlMessage)}
ok, complete := pgsqlMessageParser(stream)
if !ok {
t.Error("Parsing returned error")
}
if !complete {
t.Error("Expecting a complete message")
}
if !stream.message.IsRequest {
t.Error("Failed to parse postgres request")
}
if stream.message.Query != "SELECT * FROM Foobar;" {
t.Error("Failed to parse query")
}
}
// Test parsing a response with data attached
func TestPgsqlParser_dataResponse(t *testing.T) {
data := []byte(
"5400000033000269640000008fc40001000000170004ffffffff000076616c75650000008fc4000200000019ffffffffffff0000" +
"44000000130002000000013100000004746f746f" +
"440000001500020000000133000000066d617274696e" +
"440000001300020000000134000000046a65616e" +
"430000000b53454c45435400" +
"5a0000000549")
message, err := hex.DecodeString(string(data))
if err != nil {
t.Error("Failed to decode hex string")
}
stream := &PgsqlStream{tcpStream: nil, data: message, message: new(PgsqlMessage)}
ok, complete := pgsqlMessageParser(stream)
if !ok {
t.Error("Parsing returned error")
}
if !complete {
t.Error("Expecting a complete message")
}
if stream.message.IsRequest {
t.Error("Failed to parse postgres response")
}
if !stream.message.IsOK || stream.message.IsError {
t.Error("Failed to parse postgres response")
}
if stream.message.NumberOfFields != 2 {
t.Error("Failed to parse the number of field")
}
if stream.message.NumberOfRows != 3 {
t.Error("Failed to parse the number of rows")
}
}
// Test 3 responses in a row
func TestPgsqlParser_threeResponses(t *testing.T) {
data, err := hex.DecodeString(
"5300000017446174655374796c650049534f2c204d445900430000000853455400430000000853455400540000005700036f696400000004eefffe0000001a0004ffffffff0000656e636f64696e6700000000000000000000130040ffffffff00006461746c6173747379736f696400000004ee00090000001a0004ffffffff0000440000002000030000000531313836350000000455544638000000053131383537430000000d53454c4543542031005a0000000549")
if err != nil {
t.Error("Failed to decode hex string")
}
ts, err := time.Parse(time.RFC3339, "2000-12-26T01:15:06+04:20")
if err != nil {
t.Error("Failed to get ts")
}
pkt := Packet{
payload: data,
ts: ts,
}
tcp := TcpStream{
pgsqlData: [2]*PgsqlStream{nil, nil},
}
var count_handlePgsql = 0
old_handlePgsql := handlePgsql
defer func() {
handlePgsql = old_handlePgsql
}()
handlePgsql = func(m *PgsqlMessage, tcp *TcpStream,
dir uint8, raw_msg []byte) {
count_handlePgsql += 1
}
ParsePgsql(&pkt, &tcp, 1)
if count_handlePgsql != 3 {
t.Error("handlePgsql not called three times")
}
}
// Test parsing an error response
func TestPgsqlParser_errorResponse(t *testing.T) {
data := []byte(
"4500000088534552524f5200433235503032004d63757272656e74207472616e73616374696f6e2069732061626f727465642c20636f6d6d616e64732069676e6f72656420756e74696c20656e64206f66207472616e73616374696f6e20626c6f636b0046706f7374677265732e63004c3932310052657865635f73696d706c655f71756572790000")
message, err := hex.DecodeString(string(data))
if err != nil {
t.Error("Failed to decode hex string")
}
stream := &PgsqlStream{tcpStream: nil, data: message, message: new(PgsqlMessage)}
ok, complete := pgsqlMessageParser(stream)
if !ok {
t.Error("Parsing returned error")
}
if !complete {
t.Error("Expecting a complete message")
}
if stream.message.IsRequest {
t.Error("Failed to parse postgres response")
}
if !stream.message.IsError {
t.Error("Failed to parse error response")
}
if stream.message.ErrorSeverity != "ERROR" {
t.Error("Failed to parse severity")
}
if stream.message.ErrorCode != "25P02" {
t.Error("Failed to parse error code")
}
if stream.message.ErrorInfo != "current transaction is aborted, commands ignored until end of transaction block" {
t.Error("Failed to parse error message")
}
}