From e7c3e19519e43e85eb5303b5d00cafe11f2fbece Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 13 Feb 2022 13:04:19 +0100 Subject: [PATCH] Add (miri) tests for {u16,}strlcpy() --- src/wrapper/util.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/wrapper/util.rs b/src/wrapper/util.rs index 3dfffb49..8af60bac 100644 --- a/src/wrapper/util.rs +++ b/src/wrapper/util.rs @@ -141,3 +141,58 @@ impl Drop for ScopedFtz { } } } + +mod miri { + use std::ffi::CStr; + use widestring::U16CStr; + + use super::*; + + #[test] + fn strlcpy_normal() { + let mut dest = [0; 256]; + strlcpy(&mut dest, "Hello, world!"); + + assert_eq!( + unsafe { CStr::from_ptr(dest.as_ptr()) }.to_str(), + Ok("Hello, world!") + ); + } + + #[test] + fn strlcpy_overflow() { + let mut dest = [0; 6]; + strlcpy(&mut dest, "Hello, world!"); + + assert_eq!( + unsafe { CStr::from_ptr(dest.as_ptr()) }.to_str(), + Ok("Hello") + ); + } + + #[test] + fn u16strlcpy_normal() { + let mut dest = [0; 256]; + u16strlcpy(&mut dest, "Hello, world!"); + + assert_eq!( + unsafe { U16CStr::from_ptr_str(dest.as_ptr() as *const u16) } + .to_string() + .unwrap(), + "Hello, world!" + ); + } + + #[test] + fn u16strlcpy_overflow() { + let mut dest = [0; 6]; + u16strlcpy(&mut dest, "Hello, world!"); + + assert_eq!( + unsafe { U16CStr::from_ptr_str(dest.as_ptr() as *const u16) } + .to_string() + .unwrap(), + "Hello" + ); + } +}