tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

220 lignes · 6924 octetsBrut · Historique
1// The wire codec against recorded byte transcripts (computed once with
2// the reference Python `struct` and checked in as base64; no database
3// needed).
4
5import protocol
6import std/base64
7import std/bytes
8import std/list
9import std/test.{assert, assert_equal}
10
11fn encoded :: msg: protocol.Out transcript: String -> ()
12{
13 assert_equal (base64.encode (protocol.encode msg)) transcript
14}
15
16fn decoded :: transcript: String -> protocol.In
17{
18 match base64.decode transcript
19 { Ok(data) -> match protocol.next data 0
20 { protocol.Message(msg, past) ->
21 {
22 assert_equal past (bytes.length data)
23 msg
24 }
25 , _ -> panic "the transcript did not parse"
26 }
27 , Error(e) -> panic e
28 }
29}
30
31/// Equal messages, with their `Debug` text on a failure (Bytes has no
32/// `Show`, so `assert_equal` can't compare messages).
33fn assert_msg :: actual: protocol.In expected: protocol.In -> ()
34{
35 assert (actual == expected) "expected {expected:?}, got {actual:?}"
36}
37
38@test
39fn encoding_a_simple_query ::
40{
41 encoded protocol.Query("select 1") "UQAAAA1zZWxlY3QgMQA="
42}
43
44@test
45fn encoding_the_startup_message ::
46{
47 // The transcript has: the length, protocol 3.0 (0x00030000), then
48 // user/database/application_name pairs and the final zero byte.
49 encoded
50 protocol.Startup("postgres", "orders", "pg_demo")
51 "AAAAQAADAAB1c2VyAHBvc3RncmVzAGRhdGFiYXNlAG9yZGVycwBhcHBsaWNhdGlvbl9uYW1lAHBnX2RlbW8AAA=="
52}
53
54@test
55fn encoding_ssl_request_and_parse_and_bind ::
56{
57 encoded protocol.SslRequest "AAAACATSFi8="
58 // No declared parameter types: the server infers (the empty list is the
59 // historical encoding).
60 encoded protocol.Parse("q", "select $1", []) "UAAAABJxAHNlbGVjdCAkMQAAAA=="
61 // A declared bytea parameter (oid 17): 'P', the length, "q", the SQL,
62 // one oid, then 0x00000011.
63 encoded
64 protocol.Parse("q", "insert into t (d) values ($1)", [17])
65 "UAAAACpxAGluc2VydCBpbnRvIHQgKGQpIHZhbHVlcyAoJDEpAAABAAAAEQ=="
66 // A text parameter and a NULL one (both text format, no result codes).
67 encoded
68 protocol.Bind( ""
69 , "q"
70 , [ protocol.BindParam(Some(bytes.from_string "42"), 0)
71 , protocol.BindParam(None, 0)
72 ]
73 , []
74 )
75 "QgAAABsAcQAAAgAAAAAAAgAAAAI0Mv////8AAA=="
76 // A binary parameter (int4 42), binary results wanted.
77 encoded
78 protocol.Bind( ""
79 , "q"
80 , [protocol.BindParam(Some(bytes.from_list [0, 0, 0, 42]), 1)]
81 , [1]
82 )
83 "QgAAABkAcQAAAQABAAEAAAAEAAAAKgABAAE="
84 // A CancelRequest: no type byte, the code 80877102, then the key.
85 encoded protocol.CancelRequest(4242, 12345678) "AAAAEATSFi4AABCSALxhTg=="
86}
87
88@test
89fn decoding_ready_and_parameter_status ::
90{
91 assert_msg (decoded "WgAAAAVJ") protocol.ReadyForQuery(protocol.Idle)
92 assert_msg
93 (decoded "UwAAABhzZXJ2ZXJfdmVyc2lvbgAxNi40AA==")
94 protocol.ParameterStatus("server_version", "16.4")
95}
96
97@test
98fn decoding_authentication ::
99{
100 assert_msg (decoded "UgAAAAgAAAAA") protocol.AuthenticationOk
101 match decoded "UgAAACoAAAAKU0NSQU0tU0hBLTI1Ni1QTFVTAFNDUkFNLVNIQS0yNTYAAA=="
102 { protocol.AuthenticationSasl(mechanisms) ->
103 assert_equal mechanisms ["SCRAM-SHA-256-PLUS", "SCRAM-SHA-256"]
104 , _ -> panic "not a SASL offer"
105 }
106 assert_msg
107 (decoded "UgAAAAsAAAALYWJj")
108 protocol.SaslContinue(bytes.from_string "abc")
109}
110
111@test
112fn decoding_errors_and_rows ::
113{
114 assert_msg
115 (decoded "RQAAACJTRVJST1IAQzIzNTA1AE1kdXBsaWNhdGUga2V5AAA=")
116 protocol.ErrorResponse( [ ("S", "ERROR")
117 , ("C", "23505")
118 , ("M", "duplicate key")
119 ]
120 )
121 match decoded "VAAAACAAAXZlcnNpb24AAAAAAAAAAAAAGf///////wAA"
122 { protocol.RowDescription(fields) ->
123 {
124 assert_equal (list.length fields) 1
125 match fields
126 { [f] ->
127 {
128 assert_equal f.name "version"
129 assert_equal f.type_oid 25
130 assert_equal f.format 0
131 }
132 , _ -> panic "one field"
133 }
134 }
135 , _ -> panic "not a row description"
136 }
137 assert_msg
138 (decoded "RAAAABYAAwAAAAI0Mv////8AAAACaGk=")
139 protocol.DataRow( [ Some(bytes.from_string "42")
140 , None
141 , Some(bytes.from_string "hi")
142 ]
143 )
144 assert_msg
145 (decoded "QwAAAA1TRUxFQ1QgMQA=")
146 protocol.CommandComplete("SELECT 1")
147 assert_msg
148 (decoded "QQAAABUAABCSZXZlbnRzAGhlbGxvAA==")
149 protocol.NotificationResponse(4242, "events", "hello")
150}
151
152@test
153fn framing_waits_for_the_whole_message_and_keeps_the_rest ::
154{
155 match base64.decode "WgAAAAVJWgAAAAVU"
156 { Ok(buffer) ->
157 {
158 match protocol.next buffer 0
159 { protocol.Message(first, past) ->
160 {
161 assert_msg first protocol.ReadyForQuery(protocol.Idle)
162 match protocol.next buffer past
163 { protocol.Message(second, past2) ->
164 {
165 assert_msg second protocol.ReadyForQuery(protocol.InTx)
166 assert_equal past2 (bytes.length buffer)
167 }
168 , _ -> panic "a second message"
169 }
170 }
171 , _ -> panic "a first message"
172 }
173 // One byte short of a frame: incomplete, not an error.
174 let short = match base64.decode "WgAAAAVJ"
175 { Ok(b) -> bytes.slice b 0 5, Error(e) -> panic e }
176 match protocol.next short 0
177 { protocol.Incomplete -> (), _ -> panic "one byte short is incomplete" }
178 // A silly length is malformed, not an allocation.
179 match protocol.next (bytes.from_list [90, 255, 255, 255, 255]) 0
180 { protocol.Malformed(_) -> (), _ -> panic "a huge length is malformed" }
181 }
182 , Error(e) -> panic e
183 }
184}
185
186@test
187fn truncated_payloads_are_malformed_not_panics ::
188{
189 // The frame is whole, but its payload's own structure ends early: a
190 // DataRow naming two columns with the bytes of one.
191 match protocol.next
192 (bytes.from_list [68, 0, 0, 0, 11, 0, 2, 0, 0, 0, 1, 65])
193 0
194 { protocol.Malformed(e) -> assert_equal e "the message ends early"
195 , _ -> panic "a truncated DataRow is malformed"
196 }
197 // A RowDescription cut inside a field's fixed part.
198 match protocol.next (bytes.from_list [84, 0, 0, 0, 9, 0, 1, 110, 0, 0]) 0
199 { protocol.Malformed(e) -> assert_equal e "the message ends early"
200 , _ -> panic "a truncated RowDescription is malformed"
201 }
202 // A string without its zero terminator.
203 match protocol.next (bytes.from_list [83, 0, 0, 0, 7, 97, 98, 99]) 0
204 { protocol.Malformed(e) -> assert_equal e "the string is not terminated"
205 , _ -> panic "an unterminated string is malformed"
206 }
207 // A string that is not UTF-8 names the first invalid byte.
208 match protocol.next (bytes.from_list [83, 0, 0, 0, 6, 255, 0]) 0
209 { protocol.Malformed(e) ->
210 assert_equal e "the string is not UTF-8: invalid UTF-8 at byte 0"
211 , _ -> panic "a non-UTF-8 string is malformed"
212 }
213 // A column length that is not -1 or more.
214 match protocol.next
215 (bytes.from_list [68, 0, 0, 0, 10, 0, 1, 255, 255, 255, 254])
216 0
217 { protocol.Malformed(e) -> assert_equal e "a column length of -2"
218 , _ -> panic "a negative column length is malformed"
219 }
220}