| 1 | use crate::io::ProtocolEncode; |
| 2 | |
| 3 | pub struct SslRequest; |
| 4 | |
| 5 | impl SslRequest { |
| 6 | // https://www.postgresql.org/docs/current/protocol-message-formats.html#PROTOCOL-MESSAGE-FORMATS-SSLREQUEST |
| 7 | pub const BYTES: &'static [u8] = b"\x00\x00\x00\x08\x04\xd2\x16\x2f"; |
| 8 | } |
| 9 | |
| 10 | // Cannot impl FrontendMessage because it does not have a format code |
| 11 | impl ProtocolEncode<'_> for SslRequest { |
| 12 | #[inline(always)] |
| 13 | fn encode_with(&self, buf: &mut Vec<u8>, _context: ()) -> Result<(), crate::Error> { |
| 14 | buf.extend_from_slice(Self::BYTES); |
| 15 | Ok(()) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | #[test] |
| 20 | fn test_encode_ssl_request() { |
| 21 | let mut buf = Vec::new(); |
| 22 | |
| 23 | // Int32(8) |
| 24 | // Length of message contents in bytes, including self. |
| 25 | buf.extend_from_slice(&8_u32.to_be_bytes()); |
| 26 | |
| 27 | // Int32(80877103) |
| 28 | // The SSL request code. The value is chosen to contain 1234 in the most significant 16 bits, |
| 29 | // and 5679 in the least significant 16 bits. |
| 30 | // (To avoid confusion, this code must not be the same as any protocol version number.) |
| 31 | buf.extend_from_slice(&(((1234 << 16) | 5679) as u32).to_be_bytes()); |
| 32 | |
| 33 | let mut encoded = Vec::new(); |
| 34 | SslRequest.encode(&mut encoded).unwrap(); |
| 35 | |
| 36 | assert_eq!(buf, SslRequest::BYTES); |
| 37 | assert_eq!(buf, encoded); |
| 38 | } |