From d2a397d909d21f730c5236cd742a820532cfcd7d Mon Sep 17 00:00:00 2001 From: itycodes Date: Mon, 4 Nov 2024 00:11:45 +0100 Subject: [PATCH] Improve test coverage Additionally fixed a bug in uapi::i915::find_fd where it was using a non-owned RawFd and then closing the file descriptor at the end of the scope of the closure. --- src/uapi/i915/mod.rs | 8 ++++---- tests/tests_i915_gpu.rs | 19 +++++++++++++++++++ tests/tests_raw.rs | 17 ++++++++++------- tests/tests_uapi.rs | 32 ++++++++++++++++++++++++++++---- 4 files changed, 61 insertions(+), 15 deletions(-) diff --git a/src/uapi/i915/mod.rs b/src/uapi/i915/mod.rs index 46b055b..e53fcfb 100644 --- a/src/uapi/i915/mod.rs +++ b/src/uapi/i915/mod.rs @@ -13,7 +13,7 @@ pub enum EngineClass { Render, Copy, Video, VideoEnhance, Compute, Invalid } -#[derive(Debug)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct EngineClassInstance { pub engine_class: EngineClass, pub engine_instance: u16, @@ -21,7 +21,7 @@ pub struct EngineClassInstance { pub type EngineInfoFlags = u64; -#[derive(Debug)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct EngineInfo { pub engine: EngineClassInstance, pub logical_instance: u16, @@ -154,6 +154,6 @@ pub fn find_node() -> Option { None } -pub fn find_fd() -> Option { - return find_node().map(|node| node.fd.as_raw_fd()); +pub fn find_fd() -> Option { + return find_node().map(|node| node.fd); } \ No newline at end of file diff --git a/tests/tests_i915_gpu.rs b/tests/tests_i915_gpu.rs index cbe845e..71bc193 100644 --- a/tests/tests_i915_gpu.rs +++ b/tests/tests_i915_gpu.rs @@ -13,6 +13,25 @@ fn test_i915_gpu_find_device() { assert_eq!(drm_version.desc.to_str().unwrap(), "Intel Graphics"); } +#[test] +fn test_i915_gpu_from_path() { + let device = i915::find_device().expect("Failed to find i915 device"); + let path = device.node.path.clone().expect("Failed to get path"); + let device_2 = i915::Device::from_path(path).expect("Failed to create device from path"); + assert_eq!(device.driver.name, device_2.driver.name); + assert_eq!(device.driver.desc, device_2.driver.desc); + assert_eq!(device.node.path, device_2.node.path); +} + +#[test] +fn test_i915_gpu_from_fd() { + let device = i915::find_device().expect("Failed to find i915 device"); + let fd = device.node.fd; + let device_2 = i915::Device::from_fd(OwnedFd::from(fd)).expect("Failed to create device from fd"); + assert_eq!(device.driver.name, device_2.driver.name); + assert_eq!(device.driver.desc, device_2.driver.desc); +} + #[test] fn test_i915_gpu_get_version() { let device = i915::find_device(); diff --git a/tests/tests_raw.rs b/tests/tests_raw.rs index bf01456..28d80cb 100644 --- a/tests/tests_raw.rs +++ b/tests/tests_raw.rs @@ -6,13 +6,16 @@ use nix::{self}; #[test] fn test_raw_drm_version() { // TODO more robust render node detection - let fd = nix::fcntl::open("/dev/dri/renderD129", nix::fcntl::OFlag::O_RDWR | nix::fcntl::OFlag::O_CLOEXEC, nix::sys::stat::Mode::empty()) - .expect("Failed to open /dev/dri/renderD129"); + let fd = nix::fcntl::open("/dev/dri/renderD128", nix::fcntl::OFlag::O_RDWR | nix::fcntl::OFlag::O_CLOEXEC, nix::sys::stat::Mode::empty()) + .expect("Failed to open /dev/dri/renderD128"); unsafe { let name_vec = libc::malloc(128); + libc::memset(name_vec, 0, 128); let date_vec = libc::malloc(128); + libc::memset(date_vec, 0, 128); let desc_vec = libc::malloc(128); - + libc::memset(desc_vec, 0, 128); + let mut version = native::drm_version { version_major: 0, version_minor: 0, @@ -37,8 +40,8 @@ fn test_raw_drm_version() { #[test] fn test_i915_raw_query() { - let fd = nix::fcntl::open("/dev/dri/renderD129", nix::fcntl::OFlag::O_RDWR | nix::fcntl::OFlag::O_CLOEXEC, nix::sys::stat::Mode::empty()) - .expect("Failed to open /dev/dri/renderD129"); + let fd = nix::fcntl::open("/dev/dri/renderD128", nix::fcntl::OFlag::O_RDWR | nix::fcntl::OFlag::O_CLOEXEC, nix::sys::stat::Mode::empty()) + .expect("Failed to open /dev/dri/renderD128"); unsafe { let mut query_item = native::drm_i915_query_item { query_id: native::DRM_I915_QUERY_ENGINE_INFO as u64, @@ -54,13 +57,13 @@ fn test_i915_raw_query() { assert!(libc::ioctl(fd, native::DRM_IOCTL_I915_QUERY, &mut query) == 0); let engine_info = libc::malloc(query_item.length as usize) as *mut native::drm_i915_query_engine_info; + libc::memset(engine_info as *mut c_void, 0, query_item.length as usize); query_item.data_ptr = engine_info as u64; assert!(libc::ioctl(fd, native::DRM_IOCTL_I915_QUERY, &mut query) == 0); - println!("engine_info: {:?}", *engine_info); let engine_data = engine_info.as_mut().unwrap(); let engines = engine_data.engines.as_mut_slice(engine_data.num_engines as usize); for engine in engines { - println!("engine: {:?}", engine); + assert!(engine.engine.engine_class != uapi::i915::EngineClass::Invalid as u16); } } } diff --git a/tests/tests_uapi.rs b/tests/tests_uapi.rs index 475d3ce..9788952 100644 --- a/tests/tests_uapi.rs +++ b/tests/tests_uapi.rs @@ -3,18 +3,42 @@ use intel_gpu_uapi::*; use uapi::i915; use std::fs; -use std::{fs::File, os::fd::AsRawFd}; +use std::{fs::File, os::fd::{AsRawFd, RawFd}}; #[test] fn test_i915_uapi_get_version() { let node = i915::find_node().expect("Failed to find i915 fd"); - let drm_version = uapi::get_drm_version(node.fd.as_raw_fd()).expect("Failed to get drm version"); - println!("{:?}", drm_version); + let _drm_version = uapi::get_drm_version(node.fd.as_raw_fd()).expect("Failed to get drm version"); } #[test] fn test_i915_uapi_get_engines() { + let node = i915::find_node().expect("Failed to find i915 fd"); + let _engines = i915::get_engines(node.fd.as_raw_fd()).expect("Failed to get engines"); + let fail_fd = File::open("/dev/null").expect("Failed to open /dev/null"); + assert!(i915::get_engines(fail_fd.as_raw_fd()).is_none()); +} + +#[test] +fn test_i915_uapi_get_param_fail() { + let fd = File::open("/dev/null").expect("Failed to open /dev/null"); + let param = i915::get_param(fd.as_raw_fd(), native::I915_PARAM_CHIPSET_ID as i32); + assert!(param.is_none()); +} + +#[test] +fn test_i915_uapi_native_engine_info() { let node = i915::find_node().expect("Failed to find i915 fd"); let engines = i915::get_engines(node.fd.as_raw_fd()).expect("Failed to get engines"); - println!("{:?}", engines); + for engine in engines { + let native_engine = engine.to_native(); + let from_native = i915::EngineInfo::from_native(native_engine); + assert_eq!(&engine, &from_native); + } +} + +#[test] +fn test_i915_find_fd() { + let fd = i915::find_fd().expect("Failed to find i915 fd"); + let _drm_version = uapi::get_drm_version(fd.as_raw_fd()).expect("Failed to get drm version"); } \ No newline at end of file