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.
master
itycodes 3 weeks ago
parent 20cf8a355d
commit d2a397d909

@ -13,7 +13,7 @@ pub enum EngineClass {
Render, Copy, Video, VideoEnhance, Compute, Invalid Render, Copy, Video, VideoEnhance, Compute, Invalid
} }
#[derive(Debug)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EngineClassInstance { pub struct EngineClassInstance {
pub engine_class: EngineClass, pub engine_class: EngineClass,
pub engine_instance: u16, pub engine_instance: u16,
@ -21,7 +21,7 @@ pub struct EngineClassInstance {
pub type EngineInfoFlags = u64; pub type EngineInfoFlags = u64;
#[derive(Debug)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EngineInfo { pub struct EngineInfo {
pub engine: EngineClassInstance, pub engine: EngineClassInstance,
pub logical_instance: u16, pub logical_instance: u16,
@ -154,6 +154,6 @@ pub fn find_node() -> Option<DrmDeviceNode> {
None None
} }
pub fn find_fd() -> Option<RawFd> { pub fn find_fd() -> Option<OwnedFd> {
return find_node().map(|node| node.fd.as_raw_fd()); return find_node().map(|node| node.fd);
} }

@ -13,6 +13,25 @@ fn test_i915_gpu_find_device() {
assert_eq!(drm_version.desc.to_str().unwrap(), "Intel Graphics"); 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] #[test]
fn test_i915_gpu_get_version() { fn test_i915_gpu_get_version() {
let device = i915::find_device(); let device = i915::find_device();

@ -6,12 +6,15 @@ use nix::{self};
#[test] #[test]
fn test_raw_drm_version() { fn test_raw_drm_version() {
// TODO more robust render node detection // 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()) 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/renderD129"); .expect("Failed to open /dev/dri/renderD128");
unsafe { unsafe {
let name_vec = libc::malloc(128); let name_vec = libc::malloc(128);
libc::memset(name_vec, 0, 128);
let date_vec = libc::malloc(128); let date_vec = libc::malloc(128);
libc::memset(date_vec, 0, 128);
let desc_vec = libc::malloc(128); let desc_vec = libc::malloc(128);
libc::memset(desc_vec, 0, 128);
let mut version = native::drm_version { let mut version = native::drm_version {
version_major: 0, version_major: 0,
@ -37,8 +40,8 @@ fn test_raw_drm_version() {
#[test] #[test]
fn test_i915_raw_query() { 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()) 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/renderD129"); .expect("Failed to open /dev/dri/renderD128");
unsafe { unsafe {
let mut query_item = native::drm_i915_query_item { let mut query_item = native::drm_i915_query_item {
query_id: native::DRM_I915_QUERY_ENGINE_INFO as u64, 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); assert!(libc::ioctl(fd, native::DRM_IOCTL_I915_QUERY, &mut query) == 0);
let engine_info = libc::malloc(query_item.length as usize) let engine_info = libc::malloc(query_item.length as usize)
as *mut native::drm_i915_query_engine_info; 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; query_item.data_ptr = engine_info as u64;
assert!(libc::ioctl(fd, native::DRM_IOCTL_I915_QUERY, &mut query) == 0); 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 engine_data = engine_info.as_mut().unwrap();
let engines = engine_data.engines.as_mut_slice(engine_data.num_engines as usize); let engines = engine_data.engines.as_mut_slice(engine_data.num_engines as usize);
for engine in engines { for engine in engines {
println!("engine: {:?}", engine); assert!(engine.engine.engine_class != uapi::i915::EngineClass::Invalid as u16);
} }
} }
} }

@ -3,18 +3,42 @@
use intel_gpu_uapi::*; use intel_gpu_uapi::*;
use uapi::i915; use uapi::i915;
use std::fs; use std::fs;
use std::{fs::File, os::fd::AsRawFd}; use std::{fs::File, os::fd::{AsRawFd, RawFd}};
#[test] #[test]
fn test_i915_uapi_get_version() { fn test_i915_uapi_get_version() {
let node = i915::find_node().expect("Failed to find i915 fd"); 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"); let _drm_version = uapi::get_drm_version(node.fd.as_raw_fd()).expect("Failed to get drm version");
println!("{:?}", drm_version);
} }
#[test] #[test]
fn test_i915_uapi_get_engines() { 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 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 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");
} }
Loading…
Cancel
Save