From 134d6a2faab1801e9b3d23b6ee11ba1643eae0fe Mon Sep 17 00:00:00 2001 From: David Cook Date: Sun, 5 Apr 2020 14:55:57 -0500 Subject: [PATCH] Add tests, improve test coverage --- .../libc_pthread_mutex_destroy_locked.rs | 16 ++++++++++++++++ ...libc_pthread_mutex_normal_unlock_unlocked.rs | 17 +++++++++++++++++ .../libc_pthread_rwlock_destroy_read_locked.rs | 13 +++++++++++++ .../libc_pthread_rwlock_destroy_write_locked.rs | 13 +++++++++++++ .../libc_pthread_rwlock_unlock_unlocked.rs | 12 ++++++++++++ tests/run-pass/libc.rs | 8 +++++++- 6 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 tests/compile-fail/libc_pthread_mutex_destroy_locked.rs create mode 100644 tests/compile-fail/libc_pthread_mutex_normal_unlock_unlocked.rs create mode 100644 tests/compile-fail/libc_pthread_rwlock_destroy_read_locked.rs create mode 100644 tests/compile-fail/libc_pthread_rwlock_destroy_write_locked.rs create mode 100644 tests/compile-fail/libc_pthread_rwlock_unlock_unlocked.rs diff --git a/tests/compile-fail/libc_pthread_mutex_destroy_locked.rs b/tests/compile-fail/libc_pthread_mutex_destroy_locked.rs new file mode 100644 index 000000000000..e7ed8ad29621 --- /dev/null +++ b/tests/compile-fail/libc_pthread_mutex_destroy_locked.rs @@ -0,0 +1,16 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + unsafe { + let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed(); + assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0); + let mut mutex: libc::pthread_mutex_t = std::mem::zeroed(); + assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0); + assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0); + libc::pthread_mutex_destroy(&mut mutex as *mut _); //~ ERROR destroyed a locked mutex + } +} diff --git a/tests/compile-fail/libc_pthread_mutex_normal_unlock_unlocked.rs b/tests/compile-fail/libc_pthread_mutex_normal_unlock_unlocked.rs new file mode 100644 index 000000000000..65de62484d5e --- /dev/null +++ b/tests/compile-fail/libc_pthread_mutex_normal_unlock_unlocked.rs @@ -0,0 +1,17 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + unsafe { + let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed(); + assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0); + let mut mutex: libc::pthread_mutex_t = std::mem::zeroed(); + assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0); + assert_eq!(libc::pthread_mutex_lock(&mut mutex as *mut _), 0); + assert_eq!(libc::pthread_mutex_unlock(&mut mutex as *mut _), 0); + libc::pthread_mutex_unlock(&mut mutex as *mut _); //~ ERROR was not locked + } +} diff --git a/tests/compile-fail/libc_pthread_rwlock_destroy_read_locked.rs b/tests/compile-fail/libc_pthread_rwlock_destroy_read_locked.rs new file mode 100644 index 000000000000..8750a7388fca --- /dev/null +++ b/tests/compile-fail/libc_pthread_rwlock_destroy_read_locked.rs @@ -0,0 +1,13 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); + unsafe { + assert_eq!(libc::pthread_rwlock_rdlock(rw.get()), 0); + libc::pthread_rwlock_destroy(rw.get()); //~ ERROR destroyed a locked rwlock + } +} diff --git a/tests/compile-fail/libc_pthread_rwlock_destroy_write_locked.rs b/tests/compile-fail/libc_pthread_rwlock_destroy_write_locked.rs new file mode 100644 index 000000000000..aecccfa50310 --- /dev/null +++ b/tests/compile-fail/libc_pthread_rwlock_destroy_write_locked.rs @@ -0,0 +1,13 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); + unsafe { + assert_eq!(libc::pthread_rwlock_wrlock(rw.get()), 0); + libc::pthread_rwlock_destroy(rw.get()); //~ ERROR destroyed a locked rwlock + } +} diff --git a/tests/compile-fail/libc_pthread_rwlock_unlock_unlocked.rs b/tests/compile-fail/libc_pthread_rwlock_unlock_unlocked.rs new file mode 100644 index 000000000000..8b3de53828df --- /dev/null +++ b/tests/compile-fail/libc_pthread_rwlock_unlock_unlocked.rs @@ -0,0 +1,12 @@ +// ignore-windows: No libc on Windows + +#![feature(rustc_private)] + +extern crate libc; + +fn main() { + let rw = std::cell::UnsafeCell::new(libc::PTHREAD_RWLOCK_INITIALIZER); + unsafe { + libc::pthread_rwlock_unlock(rw.get()); //~ ERROR was not locked + } +} diff --git a/tests/run-pass/libc.rs b/tests/run-pass/libc.rs index c930a034b130..a449d9340a31 100644 --- a/tests/run-pass/libc.rs +++ b/tests/run-pass/libc.rs @@ -15,7 +15,7 @@ fn tmp() -> PathBuf { #[cfg(not(target_os = "macos"))] fn test_posix_fadvise() { use std::convert::TryInto; - use std::fs::{File, remove_file}; + use std::fs::{remove_file, File}; use std::io::Write; use std::os::unix::io::AsRawFd; @@ -66,6 +66,7 @@ fn test_mutex_libc_init_recursive() { fn test_mutex_libc_init_normal() { unsafe { let mut mutexattr: libc::pthread_mutexattr_t = std::mem::zeroed(); + assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, 0x12345678), libc::EINVAL); assert_eq!(libc::pthread_mutexattr_settype(&mut mutexattr as *mut _, libc::PTHREAD_MUTEX_NORMAL), 0); let mut mutex: libc::pthread_mutex_t = std::mem::zeroed(); assert_eq!(libc::pthread_mutex_init(&mut mutex as *mut _, &mutexattr as *const _), 0); @@ -133,6 +134,11 @@ fn test_rwlock_libc_static_initializer() { assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY); assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0); + assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), 0); + assert_eq!(libc::pthread_rwlock_tryrdlock(rw.get()), libc::EBUSY); + assert_eq!(libc::pthread_rwlock_trywrlock(rw.get()), libc::EBUSY); + assert_eq!(libc::pthread_rwlock_unlock(rw.get()), 0); + assert_eq!(libc::pthread_rwlock_destroy(rw.get()), 0); } }