Add tests for BackendKeyData and ParameterStatus

This commit is contained in:
Ryan Leckey 2019-06-28 00:29:56 -07:00
parent 5c73e220b6
commit 631f91ea16
3 changed files with 54 additions and 9 deletions

View file

@ -1,6 +1,7 @@
use crate::Decode;
use bytes::{Buf, Bytes};
use std::io::{self, Cursor};
use byteorder::{BigEndian, ByteOrder};
use bytes::Bytes;
use std::io;
#[derive(Debug)]
pub struct BackendKeyData {
@ -23,10 +24,30 @@ impl BackendKeyData {
impl Decode for BackendKeyData {
fn decode(src: Bytes) -> io::Result<Self> {
let mut reader = Cursor::new(src);
let process_id = reader.get_u32_be();
let secret_key = reader.get_u32_be();
let process_id = BigEndian::read_u32(&src[..4]);
let secret_key = BigEndian::read_u32(&src[4..]);
Ok(Self { process_id, secret_key })
}
}
#[cfg(test)]
mod tests {
use super::{BackendKeyData};
use crate::{Decode};
use bytes::Bytes;
use std::io;
const BACKEND_KEY_DATA: &[u8] = b"\0\0'\xc6\x89R\xc5+";
#[test]
fn it_decodes_backend_key_data() -> io::Result<()> {
let src = Bytes::from_static(BACKEND_KEY_DATA);
let message = BackendKeyData::decode(src)?;
assert_eq!(message.process_id(), 10182);
assert_eq!(message.secret_key(), 2303903019);
Ok(())
}
}

View file

@ -23,9 +23,33 @@ impl ParameterStatus {
impl Decode for ParameterStatus {
fn decode(src: Bytes) -> io::Result<Self> {
let name = get_str_bytes_unchecked(&src);
let value = get_str_bytes_unchecked(&src.slice_from(name.len() + 1));
let name_end = memchr::memchr(0, &src).unwrap();
let value_end = memchr::memchr(0, &src[(name_end + 1)..]).unwrap();
let name = src.slice_to(name_end);
let value = src.slice(name_end + 1, name_end + 1 + value_end);
Ok(Self { name, value })
}
}
#[cfg(test)]
mod tests {
use super::{ParameterStatus};
use crate::{Decode};
use bytes::Bytes;
use std::io;
const PARAM_STATUS: &[u8] = b"session_authorization\0postgres\0";
#[test]
fn it_decodes_param_status() -> io::Result<()> {
let src = Bytes::from_static(PARAM_STATUS);
let message = ParameterStatus::decode(src)?;
assert_eq!(message.name(), "session_authorization");
assert_eq!(message.value(), "postgres");
Ok(())
}
}

View file

@ -47,7 +47,7 @@ pub struct StartupMessageBuilder {
impl Default for StartupMessageBuilder {
fn default() -> Self {
StartupMessageBuilder { version: (3, 0), params: BytesMut::with_capacity(128) }
StartupMessageBuilder { version: (3, 0), params: BytesMut::with_capacity(156) }
}
}
@ -56,7 +56,7 @@ impl StartupMessageBuilder {
StartupMessageBuilder::default()
}
/// Set the protocol version number. Defaults to `3` and `0`.
/// Set the protocol version number. Defaults to `3.0`.
pub fn version(mut self, major: u16, minor: u16) -> Self {
self.version = (major, minor);
self