LCOV - code coverage report
Current view: top level - proxy/src/sasl - messages.rs (source / functions) Coverage Total Hit
Test: fc67f8dc6087a0b4f4f0bcd74f6e1dc25fab8cf3.info Lines: 96.2 % 26 25
Test Date: 2024-09-24 13:57:57 Functions: 100.0 % 3 3

            Line data    Source code
       1              : //! Definitions for SASL messages.
       2              : 
       3              : use crate::parse::{split_at_const, split_cstr};
       4              : use pq_proto::{BeAuthenticationSaslMessage, BeMessage};
       5              : 
       6              : /// SASL-specific payload of [`PasswordMessage`](pq_proto::FeMessage::PasswordMessage).
       7              : #[derive(Debug)]
       8              : pub(crate) struct FirstMessage<'a> {
       9              :     /// Authentication method, e.g. `"SCRAM-SHA-256"`.
      10              :     pub(crate) method: &'a str,
      11              :     /// Initial client message.
      12              :     pub(crate) message: &'a str,
      13              : }
      14              : 
      15              : impl<'a> FirstMessage<'a> {
      16              :     // NB: FromStr doesn't work with lifetimes
      17           13 :     pub(crate) fn parse(bytes: &'a [u8]) -> Option<Self> {
      18           13 :         let (method_cstr, tail) = split_cstr(bytes)?;
      19           13 :         let method = method_cstr.to_str().ok()?;
      20              : 
      21           13 :         let (len_bytes, bytes) = split_at_const(tail)?;
      22           13 :         let len = u32::from_be_bytes(*len_bytes) as usize;
      23           13 :         if len != bytes.len() {
      24            0 :             return None;
      25           13 :         }
      26              : 
      27           13 :         let message = std::str::from_utf8(bytes).ok()?;
      28           13 :         Some(Self { method, message })
      29           13 :     }
      30              : }
      31              : 
      32              : /// A single SASL message.
      33              : /// This struct is deliberately decoupled from lower-level
      34              : /// [`BeAuthenticationSaslMessage`].
      35              : #[derive(Debug)]
      36              : pub(super) enum ServerMessage<T> {
      37              :     /// We expect to see more steps.
      38              :     Continue(T),
      39              :     /// This is the final step.
      40              :     Final(T),
      41              : }
      42              : 
      43              : impl<'a> ServerMessage<&'a str> {
      44           17 :     pub(super) fn to_reply(&self) -> BeMessage<'a> {
      45           17 :         BeMessage::AuthenticationSasl(match self {
      46           11 :             ServerMessage::Continue(s) => BeAuthenticationSaslMessage::Continue(s.as_bytes()),
      47            6 :             ServerMessage::Final(s) => BeAuthenticationSaslMessage::Final(s.as_bytes()),
      48              :         })
      49           17 :     }
      50              : }
      51              : 
      52              : #[cfg(test)]
      53              : mod tests {
      54              :     use super::*;
      55              : 
      56              :     #[test]
      57            1 :     fn parse_sasl_first_message() {
      58            1 :         let proto = "SCRAM-SHA-256";
      59            1 :         let sasl = "n,,n=,r=KHQ2Gjc7NptyB8aov5/TnUy4";
      60            1 :         let sasl_len = (sasl.len() as u32).to_be_bytes();
      61            1 :         let bytes = [proto.as_bytes(), &[0], sasl_len.as_ref(), sasl.as_bytes()].concat();
      62            1 : 
      63            1 :         let password = FirstMessage::parse(&bytes).unwrap();
      64            1 :         assert_eq!(password.method, proto);
      65            1 :         assert_eq!(password.message, sasl);
      66            1 :     }
      67              : }
        

Generated by: LCOV version 2.1-beta