pgwire 0.38.3

Postgresql wire protocol implemented as a library
Documentation
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
//! `messages` module contains postgresql wire protocol message definitions and
//! codecs.
//!
//! `PgWireFrontendMessage` and `PgWireBackendMessage` are enums that define all
//! types of supported messages. `Message` trait allows you to encode/decode
//! them on a `BytesMut` buffer.

use bytes::{Buf, BufMut, BytesMut};

use crate::error::{PgWireError, PgWireResult};

#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
pub enum ProtocolVersion {
    PROTOCOL3_0,
    #[default]
    PROTOCOL3_2,
}

impl ProtocolVersion {
    pub fn version_number(&self) -> (u16, u16) {
        match &self {
            Self::PROTOCOL3_0 => (3, 0),
            Self::PROTOCOL3_2 => (3, 2),
        }
    }

    /// Get ProtocolVersion from (major, minor) version tuple
    ///
    /// Return none if protocol is not supported.
    pub fn from_version_number(major: u16, minor: u16) -> Option<Self> {
        match (major, minor) {
            (3, 0) => Some(Self::PROTOCOL3_0),
            (3, 2) => Some(Self::PROTOCOL3_2),
            _ => None,
        }
    }
}

// 1 gigabyte - 1
pub(crate) const LARGE_PACKET_SIZE_LIMIT: usize = 0x3fffffff - 1;
pub(crate) const SMALL_PACKET_SIZE_LIMIT: usize = 10000;
// libpq has a limit on backend message length, except those
// VALID_LONG_MESSAGE_TYPE
pub(crate) const SMALL_BACKEND_PACKET_SIZE_LIMIT: usize = 30000;
pub(crate) const LONG_BACKEND_PACKET_SIZE_LIMIT: usize = i32::MAX as usize;

#[non_exhaustive]
#[derive(Debug, PartialEq, Eq, new)]
pub struct DecodeContext {
    pub protocol_version: ProtocolVersion,
    #[new(value = "true")]
    pub awaiting_frontend_ssl: bool,
    #[new(value = "true")]
    pub awaiting_frontend_startup: bool,
    #[new(value = "false")]
    pub awaiting_backend_ssl_response: bool,
    #[new(value = "false")]
    pub awaiting_backend_gss_response: bool,
}

impl Default for DecodeContext {
    fn default() -> Self {
        DecodeContext {
            protocol_version: ProtocolVersion::default(),
            awaiting_frontend_ssl: true,
            awaiting_frontend_startup: true,
            awaiting_backend_ssl_response: false,
            awaiting_backend_gss_response: false,
        }
    }
}

/// Define how message encode and decoded.
pub trait Message: Sized {
    /// Return the type code of the message. In order to maintain backward
    /// compatibility, `Startup` has no message type.
    #[inline]
    fn message_type() -> Option<u8> {
        None
    }

    /// Return the length of the message, including the length integer itself.
    fn message_length(&self) -> usize;

    /// Return the max length of message in this type.
    ///
    /// This is to validate the length field in decode
    #[inline]
    fn max_message_length() -> usize {
        SMALL_PACKET_SIZE_LIMIT
    }

    /// Encode body part of the message.
    fn encode_body(&self, buf: &mut BytesMut) -> PgWireResult<()>;

    /// Decode body part of the message.
    fn decode_body(buf: &mut BytesMut, full_len: usize, _ctx: &DecodeContext)
    -> PgWireResult<Self>;

    /// Default implementation for encoding message.
    ///
    /// Message type and length are encoded in this implementation and it calls
    /// `encode_body` for remaining parts.
    fn encode(&self, buf: &mut BytesMut) -> PgWireResult<()> {
        if let Some(mt) = Self::message_type() {
            buf.put_u8(mt);
        }

        let len = self.message_length();
        if len > Self::max_message_length() {
            return Err(PgWireError::MessageTooLarge(
                Self::max_message_length(),
                len,
            ));
        }

        buf.put_i32(len as i32);
        self.encode_body(buf)
    }

    /// Default implementation for decoding message.
    ///
    /// Message type and length are decoded in this implementation and it calls
    /// `decode_body` for remaining parts. Return `None` if the packet is not
    /// complete for parsing.
    fn decode(buf: &mut BytesMut, ctx: &DecodeContext) -> PgWireResult<Option<Self>> {
        let offset = Self::message_type().is_some().into();

        codec::decode_packet(buf, offset, Self::max_message_length(), |buf, full_len| {
            Self::decode_body(buf, full_len, ctx)
        })
    }
}

/// Cancel message
pub mod cancel;
mod codec;
/// Copy messages
pub mod copy;
/// Data related messages
pub mod data;
/// Extended query messages, including request/response for parse, bind and etc.
pub mod extendedquery;
/// General response messages
pub mod response;
/// Simple query messages, including descriptions
pub mod simplequery;
/// Startup messages
pub mod startup;
/// Termination messages
pub mod terminate;

#[derive(Debug)]
pub enum SslNegotiationMetaMessage {
    PostgresSsl(startup::SslRequest),
    PostgresGss(startup::GssEncRequest),
    None,
}

/// Messages sent from Frontend
#[derive(Debug)]
pub enum PgWireFrontendMessage {
    SslNegotiation(SslNegotiationMetaMessage),

    Startup(startup::Startup),
    CancelRequest(cancel::CancelRequest),
    PasswordMessageFamily(startup::PasswordMessageFamily),

    Query(simplequery::Query),

    Parse(extendedquery::Parse),
    Close(extendedquery::Close),
    Bind(extendedquery::Bind),
    Describe(extendedquery::Describe),
    Execute(extendedquery::Execute),
    PortalSuspended(extendedquery::PortalSuspended),
    Flush(extendedquery::Flush),
    Sync(extendedquery::Sync),

    Terminate(terminate::Terminate),

    CopyData(copy::CopyData),
    CopyFail(copy::CopyFail),
    CopyDone(copy::CopyDone),
}

impl PgWireFrontendMessage {
    pub fn is_extended_query(&self) -> bool {
        matches!(
            self,
            Self::Parse(_)
                | Self::Bind(_)
                | Self::Close(_)
                | Self::Describe(_)
                | Self::Execute(_)
                | Self::PortalSuspended(_)
                | Self::Flush(_)
                | Self::Sync(_)
        )
    }

    pub fn encode(&self, buf: &mut BytesMut) -> PgWireResult<()> {
        match self {
            Self::Startup(msg) => msg.encode(buf),
            Self::CancelRequest(msg) => msg.encode(buf),
            Self::SslNegotiation(ssl_negotiation) => match ssl_negotiation {
                SslNegotiationMetaMessage::PostgresSsl(msg) => msg.encode(buf),
                SslNegotiationMetaMessage::PostgresGss(msg) => msg.encode(buf),
                _ => Ok(()),
            },

            Self::PasswordMessageFamily(msg) => msg.encode(buf),

            Self::Query(msg) => msg.encode(buf),

            Self::Parse(msg) => msg.encode(buf),
            Self::Bind(msg) => msg.encode(buf),
            Self::Close(msg) => msg.encode(buf),
            Self::Describe(msg) => msg.encode(buf),
            Self::Execute(msg) => msg.encode(buf),
            Self::PortalSuspended(msg) => msg.encode(buf),
            Self::Flush(msg) => msg.encode(buf),
            Self::Sync(msg) => msg.encode(buf),

            Self::Terminate(msg) => msg.encode(buf),

            Self::CopyData(msg) => msg.encode(buf),
            Self::CopyFail(msg) => msg.encode(buf),
            Self::CopyDone(msg) => msg.encode(buf),
        }
    }

    pub fn decode(buf: &mut BytesMut, ctx: &DecodeContext) -> PgWireResult<Option<Self>> {
        if ctx.awaiting_frontend_ssl {
            // Connection just established, the incoming message can be:
            // - SSLRequest
            // - GssRequest
            // - Startup
            // - CancelRequest

            // Try to read the magic number to tell whether it SSLRequest or
            // Startup, all these messages should have at least 8 bytes
            if buf.remaining() >= 8 {
                if startup::SslRequest::is_ssl_request_packet(buf) {
                    startup::SslRequest::decode(buf, ctx).map(|opt| {
                        opt.map(|msg| {
                            Self::SslNegotiation(SslNegotiationMetaMessage::PostgresSsl(msg))
                        })
                    })
                } else if startup::GssEncRequest::is_gss_enc_request_packet(buf) {
                    startup::GssEncRequest::decode(buf, ctx).map(|opt| {
                        opt.map(|msg| {
                            Self::SslNegotiation(SslNegotiationMetaMessage::PostgresGss(msg))
                        })
                    })
                } else {
                    // the message can be cancel or startup, we will update the
                    // state in `DecodeContext` and read them in next loop
                    Ok(Some(Self::SslNegotiation(SslNegotiationMetaMessage::None)))
                }
            } else {
                Ok(None)
            }
        } else if ctx.awaiting_frontend_startup {
            // we will check for cancel request again in case it's sent in ssl connection
            if buf.remaining() >= 8 {
                if cancel::CancelRequest::is_cancel_request_packet(buf) {
                    cancel::CancelRequest::decode(buf, ctx)
                        .map(|opt| opt.map(PgWireFrontendMessage::CancelRequest))
                } else {
                    startup::Startup::decode(buf, ctx).map(|v| v.map(Self::Startup))
                }
            } else {
                Ok(None)
            }
        } else if buf.remaining() > 1 {
            let first_byte = buf[0];

            match first_byte {
                // Password, SASLInitialResponse, SASLResponse can only be
                // decoded under certain context
                startup::MESSAGE_TYPE_BYTE_PASSWORD_MESSAGE_FAMILY => {
                    startup::PasswordMessageFamily::decode(buf, ctx)
                        .map(|v| v.map(Self::PasswordMessageFamily))
                }

                simplequery::MESSAGE_TYPE_BYTE_QUERY => {
                    simplequery::Query::decode(buf, ctx).map(|v| v.map(Self::Query))
                }

                extendedquery::MESSAGE_TYPE_BYTE_PARSE => {
                    extendedquery::Parse::decode(buf, ctx).map(|v| v.map(Self::Parse))
                }
                extendedquery::MESSAGE_TYPE_BYTE_BIND => {
                    extendedquery::Bind::decode(buf, ctx).map(|v| v.map(Self::Bind))
                }
                extendedquery::MESSAGE_TYPE_BYTE_CLOSE => {
                    extendedquery::Close::decode(buf, ctx).map(|v| v.map(Self::Close))
                }
                extendedquery::MESSAGE_TYPE_BYTE_DESCRIBE => {
                    extendedquery::Describe::decode(buf, ctx).map(|v| v.map(Self::Describe))
                }
                extendedquery::MESSAGE_TYPE_BYTE_EXECUTE => {
                    extendedquery::Execute::decode(buf, ctx).map(|v| v.map(Self::Execute))
                }
                extendedquery::MESSAGE_TYPE_BYTE_FLUSH => {
                    extendedquery::Flush::decode(buf, ctx).map(|v| v.map(Self::Flush))
                }
                extendedquery::MESSAGE_TYPE_BYTE_SYNC => {
                    extendedquery::Sync::decode(buf, ctx).map(|v| v.map(Self::Sync))
                }

                terminate::MESSAGE_TYPE_BYTE_TERMINATE => {
                    terminate::Terminate::decode(buf, ctx).map(|v| v.map(Self::Terminate))
                }

                copy::MESSAGE_TYPE_BYTE_COPY_DATA => {
                    copy::CopyData::decode(buf, ctx).map(|v| v.map(Self::CopyData))
                }
                copy::MESSAGE_TYPE_BYTE_COPY_FAIL => {
                    copy::CopyFail::decode(buf, ctx).map(|v| v.map(Self::CopyFail))
                }
                copy::MESSAGE_TYPE_BYTE_COPY_DONE => {
                    copy::CopyDone::decode(buf, ctx).map(|v| v.map(Self::CopyDone))
                }
                _ => Err(PgWireError::InvalidMessageType(first_byte)),
            }
        } else {
            Ok(None)
        }
    }
}

/// Messages sent from Backend
#[derive(Debug)]
pub enum PgWireBackendMessage {
    // startup
    SslResponse(response::SslResponse),
    GssEncResponse(response::GssEncResponse),
    Authentication(startup::Authentication),
    ParameterStatus(startup::ParameterStatus),
    BackendKeyData(startup::BackendKeyData),
    NegotiateProtocolVersion(startup::NegotiateProtocolVersion),

    // extended query
    ParseComplete(extendedquery::ParseComplete),
    CloseComplete(extendedquery::CloseComplete),
    BindComplete(extendedquery::BindComplete),
    PortalSuspended(extendedquery::PortalSuspended),

    // command response
    CommandComplete(response::CommandComplete),
    EmptyQueryResponse(response::EmptyQueryResponse),
    ReadyForQuery(response::ReadyForQuery),
    ErrorResponse(response::ErrorResponse),
    NoticeResponse(response::NoticeResponse),
    NotificationResponse(response::NotificationResponse),

    // data
    ParameterDescription(data::ParameterDescription),
    RowDescription(data::RowDescription),
    DataRow(data::DataRow),
    NoData(data::NoData),

    // copy
    CopyData(copy::CopyData),
    CopyFail(copy::CopyFail),
    CopyDone(copy::CopyDone),
    CopyInResponse(copy::CopyInResponse),
    CopyOutResponse(copy::CopyOutResponse),
    CopyBothResponse(copy::CopyBothResponse),
}

impl PgWireBackendMessage {
    pub fn encode(&self, buf: &mut BytesMut) -> PgWireResult<()> {
        match self {
            Self::SslResponse(msg) => msg.encode(buf),
            Self::GssEncResponse(msg) => msg.encode(buf),
            Self::Authentication(msg) => msg.encode(buf),
            Self::ParameterStatus(msg) => msg.encode(buf),
            Self::BackendKeyData(msg) => msg.encode(buf),
            Self::NegotiateProtocolVersion(msg) => msg.encode(buf),

            Self::ParseComplete(msg) => msg.encode(buf),
            Self::BindComplete(msg) => msg.encode(buf),
            Self::CloseComplete(msg) => msg.encode(buf),
            Self::PortalSuspended(msg) => msg.encode(buf),

            Self::CommandComplete(msg) => msg.encode(buf),
            Self::EmptyQueryResponse(msg) => msg.encode(buf),
            Self::ReadyForQuery(msg) => msg.encode(buf),
            Self::ErrorResponse(msg) => msg.encode(buf),
            Self::NoticeResponse(msg) => msg.encode(buf),
            Self::NotificationResponse(msg) => msg.encode(buf),

            Self::ParameterDescription(msg) => msg.encode(buf),
            Self::RowDescription(msg) => msg.encode(buf),
            Self::DataRow(msg) => msg.encode(buf),
            Self::NoData(msg) => msg.encode(buf),

            Self::CopyData(msg) => msg.encode(buf),
            Self::CopyFail(msg) => msg.encode(buf),
            Self::CopyDone(msg) => msg.encode(buf),
            Self::CopyInResponse(msg) => msg.encode(buf),
            Self::CopyOutResponse(msg) => msg.encode(buf),
            Self::CopyBothResponse(msg) => msg.encode(buf),
        }
    }

    pub fn decode(buf: &mut BytesMut, ctx: &DecodeContext) -> PgWireResult<Option<Self>> {
        if ctx.awaiting_backend_ssl_response {
            // We are waiting for the SSL response
            response::SslResponse::decode(buf, ctx).map(|opt| opt.map(Self::SslResponse))
        } else if ctx.awaiting_backend_gss_response {
            // We are waiting for the GssEnc response
            response::GssEncResponse::decode(buf, ctx).map(|opt| opt.map(Self::GssEncResponse))
        } else if buf.remaining() > 1 {
            let first_byte = buf[0];
            match first_byte {
                startup::MESSAGE_TYPE_BYTE_AUTHENTICATION => {
                    startup::Authentication::decode(buf, ctx).map(|v| v.map(Self::Authentication))
                }
                startup::MESSAGE_TYPE_BYTE_PARAMETER_STATUS => {
                    startup::ParameterStatus::decode(buf, ctx).map(|v| v.map(Self::ParameterStatus))
                }
                startup::MESSAGE_TYPE_BYTE_BACKEND_KEY_DATA => {
                    startup::BackendKeyData::decode(buf, ctx).map(|v| v.map(Self::BackendKeyData))
                }
                startup::MESSAGE_TYPE_BYTE_NEGOTIATE_PROTOCOL_VERSION => {
                    startup::NegotiateProtocolVersion::decode(buf, ctx)
                        .map(|v| v.map(Self::NegotiateProtocolVersion))
                }

                extendedquery::MESSAGE_TYPE_BYTE_PARSE_COMPLETE => {
                    extendedquery::ParseComplete::decode(buf, ctx)
                        .map(|v| v.map(Self::ParseComplete))
                }
                extendedquery::MESSAGE_TYPE_BYTE_BIND_COMPLETE => {
                    extendedquery::BindComplete::decode(buf, ctx).map(|v| v.map(Self::BindComplete))
                }
                extendedquery::MESSAGE_TYPE_BYTE_CLOSE_COMPLETE => {
                    extendedquery::CloseComplete::decode(buf, ctx)
                        .map(|v| v.map(Self::CloseComplete))
                }
                extendedquery::MESSAGE_TYPE_BYTE_PORTAL_SUSPENDED => {
                    extendedquery::PortalSuspended::decode(buf, ctx)
                        .map(|v| v.map(Self::PortalSuspended))
                }

                response::MESSAGE_TYPE_BYTE_COMMAND_COMPLETE => {
                    response::CommandComplete::decode(buf, ctx)
                        .map(|v| v.map(Self::CommandComplete))
                }
                response::MESSAGE_TYPE_BYTE_EMPTY_QUERY_RESPONSE => {
                    response::EmptyQueryResponse::decode(buf, ctx)
                        .map(|v| v.map(Self::EmptyQueryResponse))
                }
                response::MESSAGE_TYPE_BYTE_READY_FOR_QUERY => {
                    response::ReadyForQuery::decode(buf, ctx).map(|v| v.map(Self::ReadyForQuery))
                }
                response::MESSAGE_TYPE_BYTE_ERROR_RESPONSE => {
                    response::ErrorResponse::decode(buf, ctx).map(|v| v.map(Self::ErrorResponse))
                }
                response::MESSAGE_TYPE_BYTE_NOTICE_RESPONSE => {
                    response::NoticeResponse::decode(buf, ctx).map(|v| v.map(Self::NoticeResponse))
                }
                response::MESSAGE_TYPE_BYTE_NOTIFICATION_RESPONSE => {
                    response::NotificationResponse::decode(buf, ctx)
                        .map(|v| v.map(Self::NotificationResponse))
                }

                data::MESSAGE_TYPE_BYTE_PARAMETER_DESCRITION => {
                    data::ParameterDescription::decode(buf, ctx)
                        .map(|v| v.map(Self::ParameterDescription))
                }
                data::MESSAGE_TYPE_BYTE_ROW_DESCRITION => {
                    data::RowDescription::decode(buf, ctx).map(|v| v.map(Self::RowDescription))
                }
                data::MESSAGE_TYPE_BYTE_DATA_ROW => {
                    data::DataRow::decode(buf, ctx).map(|v| v.map(Self::DataRow))
                }
                data::MESSAGE_TYPE_BYTE_NO_DATA => {
                    data::NoData::decode(buf, ctx).map(|v| v.map(Self::NoData))
                }

                copy::MESSAGE_TYPE_BYTE_COPY_DATA => {
                    copy::CopyData::decode(buf, ctx).map(|v| v.map(Self::CopyData))
                }
                copy::MESSAGE_TYPE_BYTE_COPY_FAIL => {
                    copy::CopyFail::decode(buf, ctx).map(|v| v.map(Self::CopyFail))
                }
                copy::MESSAGE_TYPE_BYTE_COPY_DONE => {
                    copy::CopyDone::decode(buf, ctx).map(|v| v.map(Self::CopyDone))
                }
                copy::MESSAGE_TYPE_BYTE_COPY_IN_RESPONSE => {
                    copy::CopyInResponse::decode(buf, ctx).map(|v| v.map(Self::CopyInResponse))
                }
                copy::MESSAGE_TYPE_BYTE_COPY_OUT_RESPONSE => {
                    copy::CopyOutResponse::decode(buf, ctx).map(|v| v.map(Self::CopyOutResponse))
                }
                copy::MESSAGE_TYPE_BYTE_COPY_BOTH_RESPONSE => {
                    copy::CopyBothResponse::decode(buf, ctx).map(|v| v.map(Self::CopyBothResponse))
                }
                _ => Err(PgWireError::InvalidMessageType(first_byte)),
            }
        } else {
            Ok(None)
        }
    }
}

#[cfg(test)]
mod test {
    use crate::messages::DecodeContext;

    use super::cancel::CancelRequest;
    use super::copy::*;
    use super::data::*;
    use super::extendedquery::*;
    use super::response::*;
    use super::simplequery::*;
    use super::startup::*;
    use super::terminate::*;
    use super::{Message, ProtocolVersion};
    use bytes::{Buf, BufMut, Bytes, BytesMut};

    macro_rules! roundtrip {
        ($ins:ident, $st:ty, $ctx:expr) => {
            let mut buffer = BytesMut::new();
            $ins.encode(&mut buffer).expect("encode packet");

            assert!(buffer.remaining() > 0);

            let item2 = <$st>::decode(&mut buffer, $ctx)
                .expect("decode packet")
                .expect("packet is none");

            assert_eq!(buffer.remaining(), 0);
            assert_eq!($ins, item2);
        };
    }

    #[test]
    fn test_startup() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);

        let mut s = Startup::default();
        s.parameters.insert("user".to_owned(), "tomcat".to_owned());
        roundtrip!(s, Startup, &ctx);

        ctx.awaiting_frontend_ssl = false;
        roundtrip!(s, Startup, &ctx);

        let mut s_too_large = Startup::default();
        s_too_large
            .parameters
            .insert("user".to_owned(), "a".repeat(10000));
        let mut buffer = BytesMut::new();
        assert!(s_too_large.encode(&mut buffer).is_err());
    }

    #[test]
    fn test_cancel_request() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_2);

        let s = CancelRequest::new(100, SecretKey::Bytes(Bytes::from("server2008")));
        roundtrip!(s, CancelRequest, &ctx);

        ctx.protocol_version = ProtocolVersion::PROTOCOL3_0;
        let s = CancelRequest::new(100, SecretKey::I32(1900));
        roundtrip!(s, CancelRequest, &ctx);
    }

    #[test]
    fn test_authentication() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let ss = vec![
            Authentication::Ok,
            Authentication::CleartextPassword,
            Authentication::KerberosV5,
            Authentication::SASLContinue(Bytes::from("hello")),
            Authentication::SASLFinal(Bytes::from("world")),
        ];
        for s in ss {
            roundtrip!(s, Authentication, &ctx);
        }

        let md5pass = Authentication::MD5Password(vec![b'p', b's', b't', b'g']);
        roundtrip!(md5pass, Authentication, &ctx);
    }

    #[test]
    fn test_password() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let s = Password::new("pgwire".to_owned());
        roundtrip!(s, Password, &ctx);
    }

    #[test]
    fn test_parameter_status() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let pps = ParameterStatus::new("cli".to_owned(), "psql".to_owned());
        roundtrip!(pps, ParameterStatus, &ctx);
    }

    #[test]
    fn test_query() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let query = Query::new("SELECT 1".to_owned());
        roundtrip!(query, Query, &ctx);
    }

    #[test]
    fn test_command_complete() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let cc = CommandComplete::new("DELETE 5".to_owned());
        roundtrip!(cc, CommandComplete, &ctx);

        let cc = CommandComplete::new("DELETE 5".repeat(10_000));
        let mut buffer = BytesMut::new();
        assert!(cc.encode(&mut buffer).is_err());
    }

    #[test]
    fn test_ready_for_query() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let r4q = ReadyForQuery::new(TransactionStatus::Idle);
        roundtrip!(r4q, ReadyForQuery, &ctx);
        let r4q = ReadyForQuery::new(TransactionStatus::Transaction);
        roundtrip!(r4q, ReadyForQuery, &ctx);
        let r4q = ReadyForQuery::new(TransactionStatus::Error);
        roundtrip!(r4q, ReadyForQuery, &ctx);
    }

    #[test]
    fn test_error_response() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let mut error = ErrorResponse::default();
        error.fields.push((b'R', "ERROR".to_owned()));
        error.fields.push((b'K', "cli".to_owned()));

        roundtrip!(error, ErrorResponse, &ctx);
    }

    #[test]
    fn test_notice_response() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let mut error = NoticeResponse::default();
        error.fields.push((b'R', "NOTICE".to_owned()));
        error.fields.push((b'K', "cli".to_owned()));

        roundtrip!(error, NoticeResponse, &ctx);
    }

    #[test]
    fn test_row_description() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let mut row_description = RowDescription::default();

        let f1 = FieldDescription {
            name: "id".into(),
            table_id: 1001,
            column_id: 10001,
            type_id: 1083,
            type_size: 4,
            type_modifier: -1,
            format_code: FORMAT_CODE_TEXT,
        };
        row_description.fields.push(f1);

        let f2 = FieldDescription {
            name: "name".into(),
            table_id: 1001,
            column_id: 10001,
            type_id: 1099,
            type_size: -1,
            type_modifier: -1,
            format_code: FORMAT_CODE_TEXT,
        };
        row_description.fields.push(f2);

        roundtrip!(row_description, RowDescription, &ctx);
    }

    #[test]
    fn test_data_row() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let mut row0 = DataRow::default();
        row0.data.put_i32(4);
        row0.data.put_slice("data".as_bytes());
        row0.data.put_i32(4);
        row0.data.put_i32(1001);
        row0.data.put_i32(-1);

        roundtrip!(row0, DataRow, &ctx);
    }

    #[test]
    fn test_terminate() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let terminate = Terminate::new();
        roundtrip!(terminate, Terminate, &ctx);
    }

    #[test]
    fn test_parse() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let parse = Parse::new(
            Some("find-user-by-id".to_owned()),
            "SELECT * FROM user WHERE id = ?".to_owned(),
            vec![1],
        );
        roundtrip!(parse, Parse, &ctx);
    }

    #[test]
    fn test_parse_65k() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let parse = Parse::new(
            Some("many-params".to_owned()),
            "it won't be parsed anyway".to_owned(),
            vec![25; u16::MAX as usize],
        );
        roundtrip!(parse, Parse, &ctx);
    }

    #[test]
    fn test_parse_complete() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let parse_complete = ParseComplete::new();
        roundtrip!(parse_complete, ParseComplete, &ctx);
    }

    #[test]
    fn test_close() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let close = Close::new(
            TARGET_TYPE_BYTE_STATEMENT,
            Some("find-user-by-id".to_owned()),
        );
        roundtrip!(close, Close, &ctx);
    }

    #[test]
    fn test_bind() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;
        let bind = Bind::new(
            Some("find-user-by-id-0".to_owned()),
            Some("find-user-by-id".to_owned()),
            vec![0],
            vec![Some(Bytes::from_static(b"1234"))],
            vec![0],
        );
        roundtrip!(bind, Bind, &ctx);
    }

    #[test]
    fn test_bind_65k() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let bind = Bind::new(
            Some("lol".to_owned()),
            Some("kek".to_owned()),
            vec![0; u16::MAX as usize],
            vec![Some(Bytes::from_static(b"1234")); u16::MAX as usize],
            vec![0],
        );
        roundtrip!(bind, Bind, &ctx);
    }

    #[test]
    fn test_execute() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let exec = Execute::new(Some("find-user-by-id-0".to_owned()), 100);
        roundtrip!(exec, Execute, &ctx);
    }

    #[test]
    fn test_sslrequest() {
        let ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);

        let sslreq = SslRequest::new();
        roundtrip!(sslreq, SslRequest, &ctx);
    }

    #[test]
    fn test_sslresponse() {
        let ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);

        let sslaccept = SslResponse::Accept;
        roundtrip!(sslaccept, SslResponse, &ctx);
        let sslrefuse = SslResponse::Refuse;
        roundtrip!(sslrefuse, SslResponse, &ctx);
    }

    #[test]
    fn test_gssencrequest() {
        let ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);

        let sslreq = GssEncRequest::new();
        roundtrip!(sslreq, GssEncRequest, &ctx);
    }

    #[test]
    fn test_gssencresponse() {
        let ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);

        let gssenc_accept = GssEncResponse::Accept;
        roundtrip!(gssenc_accept, GssEncResponse, &ctx);
        let gssenc_refuse = GssEncResponse::Refuse;
        roundtrip!(gssenc_refuse, GssEncResponse, &ctx);
    }

    #[test]
    fn test_saslresponse() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let saslinitialresp =
            SASLInitialResponse::new("SCRAM-SHA-256".to_owned(), Some(Bytes::from_static(b"abc")));
        roundtrip!(saslinitialresp, SASLInitialResponse, &ctx);

        let saslresp = SASLResponse::new(Bytes::from_static(b"abc"));
        roundtrip!(saslresp, SASLResponse, &ctx);
    }

    #[test]
    fn test_parameter_description() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let param_desc = ParameterDescription::new(vec![100, 200]);
        roundtrip!(param_desc, ParameterDescription, &ctx);
    }

    #[test]
    fn test_password_family() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let password = Password::new("tomcat".to_owned());

        let mut buffer = BytesMut::new();
        password.encode(&mut buffer).unwrap();
        assert!(buffer.remaining() > 0);

        let item2 = PasswordMessageFamily::decode(&mut buffer, &ctx)
            .unwrap()
            .unwrap();
        assert_eq!(buffer.remaining(), 0);
        assert_eq!(password, item2.into_password().unwrap());

        let saslinitialresp =
            SASLInitialResponse::new("SCRAM-SHA-256".to_owned(), Some(Bytes::from_static(b"abc")));
        let mut buffer = BytesMut::new();
        saslinitialresp.encode(&mut buffer).unwrap();
        assert!(buffer.remaining() > 0);

        let item2 = PasswordMessageFamily::decode(&mut buffer, &ctx)
            .unwrap()
            .unwrap();
        assert_eq!(buffer.remaining(), 0);
        assert_eq!(saslinitialresp, item2.into_sasl_initial_response().unwrap());
    }

    #[test]
    fn test_no_data() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let nodata = NoData::new();
        roundtrip!(nodata, NoData, &ctx);
    }

    #[test]
    fn test_copy_data() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let copydata = CopyData::new(Bytes::from_static("tomcat".as_bytes()));
        roundtrip!(copydata, CopyData, &ctx);
    }

    #[test]
    fn test_copy_done() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let copydone = CopyDone::new();
        roundtrip!(copydone, CopyDone, &ctx);
    }

    #[test]
    fn test_copy_fail() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let copyfail = CopyFail::new("copy failed".to_owned());
        roundtrip!(copyfail, CopyFail, &ctx);
    }

    #[test]
    fn test_copy_response() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let copyresponse = CopyInResponse::new(0, 3, vec![0, 0, 0]);
        roundtrip!(copyresponse, CopyInResponse, &ctx);

        let copyresponse = CopyOutResponse::new(0, 3, vec![0, 0, 0]);
        roundtrip!(copyresponse, CopyOutResponse, &ctx);

        let copyresponse = CopyBothResponse::new(0, 3, vec![0, 0, 0]);
        roundtrip!(copyresponse, CopyBothResponse, &ctx);
    }

    #[test]
    fn test_notification_response() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let notification_response =
            NotificationResponse::new(10087, "channel".to_owned(), "payload".to_owned());
        roundtrip!(notification_response, NotificationResponse, &ctx);
    }

    #[test]
    fn test_negotiate_protocol_version() {
        let mut ctx = DecodeContext::new(ProtocolVersion::PROTOCOL3_0);
        ctx.awaiting_frontend_ssl = false;
        ctx.awaiting_frontend_startup = false;

        let negotiate_protocol_version =
            NegotiateProtocolVersion::new(2, vec!["database".to_owned(), "user".to_owned()]);
        roundtrip!(negotiate_protocol_version, NegotiateProtocolVersion, &ctx);
    }
}