// The wire codec against recorded byte transcripts (computed once with // the reference Python `struct` and checked in as base64; no database // needed). import protocol import std/base64 import std/bytes import std/list import std/test.{assert, assert_equal} fn encoded :: msg: protocol.Out transcript: String -> () { assert_equal (base64.encode (protocol.encode msg)) transcript } fn decoded :: transcript: String -> protocol.In { match base64.decode transcript { Ok(data) -> match protocol.next data 0 { protocol.Message(msg, past) -> { assert_equal past (bytes.length data) msg } , _ -> panic "the transcript did not parse" } , Error(e) -> panic e } } /// Equal messages, with their `Debug` text on a failure (Bytes has no /// `Show`, so `assert_equal` can't compare messages). fn assert_msg :: actual: protocol.In expected: protocol.In -> () { assert (actual == expected) "expected {expected:?}, got {actual:?}" } @test fn encoding_a_simple_query :: { encoded protocol.Query("select 1") "UQAAAA1zZWxlY3QgMQA=" } @test fn encoding_the_startup_message :: { // The transcript has: the length, protocol 3.0 (0x00030000), then // user/database/application_name pairs and the final zero byte. encoded protocol.Startup("postgres", "orders", "pg_demo") "AAAAQAADAAB1c2VyAHBvc3RncmVzAGRhdGFiYXNlAG9yZGVycwBhcHBsaWNhdGlvbl9uYW1lAHBnX2RlbW8AAA==" } @test fn encoding_ssl_request_and_parse_and_bind :: { encoded protocol.SslRequest "AAAACATSFi8=" // No declared parameter types: the server infers (the empty list is the // historical encoding). encoded protocol.Parse("q", "select $1", []) "UAAAABJxAHNlbGVjdCAkMQAAAA==" // A declared bytea parameter (oid 17): 'P', the length, "q", the SQL, // one oid, then 0x00000011. encoded protocol.Parse("q", "insert into t (d) values ($1)", [17]) "UAAAACpxAGluc2VydCBpbnRvIHQgKGQpIHZhbHVlcyAoJDEpAAABAAAAEQ==" // A text parameter and a NULL one (both text format, no result codes). encoded protocol.Bind( "" , "q" , [ protocol.BindParam(Some(bytes.from_string "42"), 0) , protocol.BindParam(None, 0) ] , [] ) "QgAAABsAcQAAAgAAAAAAAgAAAAI0Mv////8AAA==" // A binary parameter (int4 42), binary results wanted. encoded protocol.Bind( "" , "q" , [protocol.BindParam(Some(bytes.from_list [0, 0, 0, 42]), 1)] , [1] ) "QgAAABkAcQAAAQABAAEAAAAEAAAAKgABAAE=" // A CancelRequest: no type byte, the code 80877102, then the key. encoded protocol.CancelRequest(4242, 12345678) "AAAAEATSFi4AABCSALxhTg==" } @test fn decoding_ready_and_parameter_status :: { assert_msg (decoded "WgAAAAVJ") protocol.ReadyForQuery(protocol.Idle) assert_msg (decoded "UwAAABhzZXJ2ZXJfdmVyc2lvbgAxNi40AA==") protocol.ParameterStatus("server_version", "16.4") } @test fn decoding_authentication :: { assert_msg (decoded "UgAAAAgAAAAA") protocol.AuthenticationOk match decoded "UgAAACoAAAAKU0NSQU0tU0hBLTI1Ni1QTFVTAFNDUkFNLVNIQS0yNTYAAA==" { protocol.AuthenticationSasl(mechanisms) -> assert_equal mechanisms ["SCRAM-SHA-256-PLUS", "SCRAM-SHA-256"] , _ -> panic "not a SASL offer" } assert_msg (decoded "UgAAAAsAAAALYWJj") protocol.SaslContinue(bytes.from_string "abc") } @test fn decoding_errors_and_rows :: { assert_msg (decoded "RQAAACJTRVJST1IAQzIzNTA1AE1kdXBsaWNhdGUga2V5AAA=") protocol.ErrorResponse( [ ("S", "ERROR") , ("C", "23505") , ("M", "duplicate key") ] ) match decoded "VAAAACAAAXZlcnNpb24AAAAAAAAAAAAAGf///////wAA" { protocol.RowDescription(fields) -> { assert_equal (list.length fields) 1 match fields { [f] -> { assert_equal f.name "version" assert_equal f.type_oid 25 assert_equal f.format 0 } , _ -> panic "one field" } } , _ -> panic "not a row description" } assert_msg (decoded "RAAAABYAAwAAAAI0Mv////8AAAACaGk=") protocol.DataRow( [ Some(bytes.from_string "42") , None , Some(bytes.from_string "hi") ] ) assert_msg (decoded "QwAAAA1TRUxFQ1QgMQA=") protocol.CommandComplete("SELECT 1") assert_msg (decoded "QQAAABUAABCSZXZlbnRzAGhlbGxvAA==") protocol.NotificationResponse(4242, "events", "hello") } @test fn framing_waits_for_the_whole_message_and_keeps_the_rest :: { match base64.decode "WgAAAAVJWgAAAAVU" { Ok(buffer) -> { match protocol.next buffer 0 { protocol.Message(first, past) -> { assert_msg first protocol.ReadyForQuery(protocol.Idle) match protocol.next buffer past { protocol.Message(second, past2) -> { assert_msg second protocol.ReadyForQuery(protocol.InTx) assert_equal past2 (bytes.length buffer) } , _ -> panic "a second message" } } , _ -> panic "a first message" } // One byte short of a frame: incomplete, not an error. let short = match base64.decode "WgAAAAVJ" { Ok(b) -> bytes.slice b 0 5, Error(e) -> panic e } match protocol.next short 0 { protocol.Incomplete -> (), _ -> panic "one byte short is incomplete" } // A silly length is malformed, not an allocation. match protocol.next (bytes.from_list [90, 255, 255, 255, 255]) 0 { protocol.Malformed(_) -> (), _ -> panic "a huge length is malformed" } } , Error(e) -> panic e } } @test fn truncated_payloads_are_malformed_not_panics :: { // The frame is whole, but its payload's own structure ends early: a // DataRow naming two columns with the bytes of one. match protocol.next (bytes.from_list [68, 0, 0, 0, 11, 0, 2, 0, 0, 0, 1, 65]) 0 { protocol.Malformed(e) -> assert_equal e "the message ends early" , _ -> panic "a truncated DataRow is malformed" } // A RowDescription cut inside a field's fixed part. match protocol.next (bytes.from_list [84, 0, 0, 0, 9, 0, 1, 110, 0, 0]) 0 { protocol.Malformed(e) -> assert_equal e "the message ends early" , _ -> panic "a truncated RowDescription is malformed" } // A string without its zero terminator. match protocol.next (bytes.from_list [83, 0, 0, 0, 7, 97, 98, 99]) 0 { protocol.Malformed(e) -> assert_equal e "the string is not terminated" , _ -> panic "an unterminated string is malformed" } // A string that is not UTF-8 names the first invalid byte. match protocol.next (bytes.from_list [83, 0, 0, 0, 6, 255, 0]) 0 { protocol.Malformed(e) -> assert_equal e "the string is not UTF-8: invalid UTF-8 at byte 0" , _ -> panic "a non-UTF-8 string is malformed" } // A column length that is not -1 or more. match protocol.next (bytes.from_list [68, 0, 0, 0, 10, 0, 1, 255, 255, 255, 254]) 0 { protocol.Malformed(e) -> assert_equal e "a column length of -2" , _ -> panic "a negative column length is malformed" } }