From c9cdb0c534f4d14c5bbcc064960fa1eb1a8142d5 Mon Sep 17 00:00:00 2001 From: James Hodgkinson Date: Mon, 18 Mar 2024 17:10:42 +0700 Subject: [PATCH] adding tests for CachedValue size --- src/proxy.rs | 10 +++++----- tests/tests.rs | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/proxy.rs b/src/proxy.rs index 727dfeb..d143b62 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -35,14 +35,14 @@ pub struct SearchCacheKey { #[derive(Debug, Clone)] pub struct CachedValue { - valid_until: Instant, - entries: Vec<(LdapSearchResultEntry, Vec)>, - result: LdapResult, - ctrl: Vec, + pub valid_until: Instant, + pub entries: Vec<(LdapSearchResultEntry, Vec)>, + pub result: LdapResult, + pub ctrl: Vec, } impl CachedValue { - fn size(&self) -> usize { + pub fn size(&self) -> usize { std::mem::size_of::() + self.entries.iter().map(|(e, _)| e.size()).sum::() } } diff --git a/tests/tests.rs b/tests/tests.rs index 72fb916..a66097f 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,6 +1,9 @@ // use ldap_proxy::proxy::BasicLdapClient; +use ldap3_proto::proto::LdapResult; +use ldap_proxy::proxy::CachedValue; use ldap_proxy::Config; +use std::time::{Duration, Instant}; #[test] fn hello_world() { @@ -16,3 +19,19 @@ fn test_config_load() { assert_eq!(config.ldap_ca.to_str(), Some("/etc/ldap-proxy/ldap-ca.pem")); } + +#[test] +fn test_cachedvalue() { + let cv = CachedValue { + valid_until: Instant::now() + Duration::from_secs(60), + entries: Vec::with_capacity(5), + result: LdapResult { + code: ldap3_proto::LdapResultCode::Busy, + matcheddn: "dn=doo".to_string(), + message: "ohno".to_string(), + referral: Vec::with_capacity(5), + }, + ctrl: Vec::with_capacity(5), + }; + assert_eq!(cv.size(), 144); +}