Debian Codegraph @

crates/ddccontrol-db/src/monitor_db/compatibility_tests.rs

raw symbols SCIP
1 use crate::{ddccontrol_caps_parse, free_c_vcp_entries, CCaps, CVcpEntry};
2
3 use super::*;
4 use libc::{c_char, c_int, c_uchar, malloc};
5 use std::ffi::{CStr, CString};
6 use std::mem::size_of;
7 use std::path::{Path, PathBuf};
8 use std::{env, fs, ptr};
9
10 static TEST_DB_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
11
12 #[test]
13 fn compatibility_fixture_matches_golden_database_tree() {
14 let _context = DbTestContext::init(&fixture_datadir());
15
16 let mut caps = OwnedCaps::from_str("(prot(monitor)type(LCD)vcp(04 10 60))");
17 let monitor = OwnedMonitor::create("compat-monitor", &mut caps, false).unwrap();
18 let snapshot = monitor.snapshot();
19
20 assert_eq!(
21 snapshot,
22 "\
23 monitor name=Compatibility Fixture Monitor init=1
24 group name=Image
25 subgroup name=Picture pattern=image
26 control id=brightness name=Brightness address=0x10 delay=120 type=0 refresh=1
27 control id=input name=Input Source address=0x60 delay=-1 type=2 refresh=0
28 value id=hdmi1 name=HDMI 1 value=0x11 value16=0x0011
29 value id=displayport name=DisplayPort value=0x0F value16=0x000F
30 group name=Color
31 subgroup name=Presets pattern=color
32 control id=color_preset name=Color Preset address=0xC8 delay=-1 type=2 refresh=0
33 value id=srgb name=sRGB value=0x34 value16=0x1234
34 value id=native name=Native value=0x02 value16=0x0002
35 "
36 );
37 }
38
39 #[test]
40 fn real_database_checkout_profiles_load_when_configured() {
41 let Some(datadir) = env::var_os("DDCCONTROL_DB_TEST_DATADIR") else {
42 return;
43 };
44 let datadir = resolve_real_database_datadir(PathBuf::from(datadir));
45 let profiles = real_database_profiles(&datadir);
46 assert!(
47 !profiles.is_empty(),
48 "no monitor profiles found in {}",
49 datadir.join("monitor").display()
50 );
51
52 let _context = DbTestContext::init(&datadir);
53
54 for profile in profiles {
55 let mut caps = OwnedCaps::with_all_vcp_codes();
56 let monitor = OwnedMonitor::create(&profile, &mut caps, true);
57 assert!(
58 monitor.is_some(),
59 "failed to load monitor profile {profile}"
60 );
61 }
62 }
63
64 fn fixture_datadir() -> PathBuf {
65 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("fixtures/compat-db")
66 }
67
68 fn resolve_real_database_datadir(path: PathBuf) -> PathBuf {
69 if is_database_datadir(&path) {
70 return path;
71 }
72
73 let db_path = path.join("db");
74 if is_database_datadir(&db_path) {
75 return db_path;
76 }
77
78 panic!(
79 "DDCCONTROL_DB_TEST_DATADIR must point to a directory containing options.xml \
80 and monitor/, or to a ddccontrol-db checkout with a db/ directory: {}",
81 path.display()
82 );
83 }
84
85 fn is_database_datadir(path: &Path) -> bool {
86 path.join("options.xml").is_file() && path.join("monitor").is_dir()
87 }
88
89 fn real_database_profiles(datadir: &Path) -> Vec<String> {
90 if let Ok(profiles) = env::var("DDCCONTROL_DB_TEST_PROFILES") {
91 return profiles
92 .split(',')
93 .map(str::trim)
94 .filter(|profile| !profile.is_empty())
95 .map(ToString::to_string)
96 .collect();
97 }
98
99 let mut profiles: Vec<_> = fs::read_dir(datadir.join("monitor"))
100 .unwrap()
101 .filter_map(Result::ok)
102 .filter_map(|entry| {
103 let path = entry.path();
104 (path.extension().and_then(|ext| ext.to_str()) == Some("xml"))
105 .then(|| path.file_stem()?.to_str().map(ToString::to_string))?
106 })
107 .collect();
108 profiles.sort();
109 profiles.truncate(25);
110 profiles
111 }
112
113 fn path_to_cstring(path: &Path) -> CString {
114 #[cfg(unix)]
115 {
116 use std::os::unix::ffi::OsStrExt;
117
118 CString::new(path.as_os_str().as_bytes()).unwrap()
119 }
120 #[cfg(not(unix))]
121 {
122 CString::new(path.to_string_lossy().as_bytes()).unwrap()
123 }
124 }
125
126 struct DbTestContext {
127 _guard: std::sync::MutexGuard<'static, ()>,
128 }
129
130 impl DbTestContext {
131 fn init(datadir: &Path) -> Self {
132 let guard = TEST_DB_LOCK.lock().unwrap();
133 let datadir = path_to_cstring(datadir);
134 assert_eq!(unsafe { ddcci_init_db(datadir.as_ptr() as *mut c_char) }, 1);
135 Self { _guard: guard }
136 }
137 }
138
139 impl Drop for DbTestContext {
140 fn drop(&mut self) {
141 unsafe {
142 ddcci_release_db();
143 }
144 }
145 }
146
147 struct OwnedCaps(CCaps);
148
149 impl OwnedCaps {
150 fn empty() -> Self {
151 Self(CCaps {
152 vcp: [ptr::null_mut(); 256],
153 monitor_type: 0,
154 raw_caps: ptr::null_mut(),
155 })
156 }
157
158 fn from_str(input: &str) -> Self {
159 let mut caps = Self::empty();
160 let input = CString::new(input).unwrap();
161 assert!(unsafe { ddccontrol_caps_parse(input.as_ptr(), &mut caps.0, 1) } > 0);
162 caps
163 }
164
165 fn with_all_vcp_codes() -> Self {
166 let mut caps = Self::empty();
167 caps.0.monitor_type = 1;
168 for entry in &mut caps.0.vcp {
169 let c_entry = unsafe { malloc(size_of::<CVcpEntry>()) as *mut CVcpEntry };
170 assert!(!c_entry.is_null());
171 unsafe {
172 ptr::write(
173 c_entry,
174 CVcpEntry {
175 values_len: -1,
176 values: ptr::null_mut(),
177 },
178 );
179 }
180 *entry = c_entry;
181 }
182 caps
183 }
184
185 fn as_mut_ptr(&mut self) -> *mut CCaps {
186 &mut self.0
187 }
188 }
189
190 impl Drop for OwnedCaps {
191 fn drop(&mut self) {
192 unsafe {
193 free_c_vcp_entries(&mut self.0);
194 }
195 }
196 }
197
198 struct OwnedMonitor(*mut CMonitorDb);
199
200 impl OwnedMonitor {
201 fn create(profile: &str, caps: &mut OwnedCaps, faulttolerance: bool) -> Option<Self> {
202 let profile = CString::new(profile).unwrap();
203 let monitor = unsafe {
204 ddcci_create_db(
205 profile.as_ptr(),
206 caps.as_mut_ptr(),
207 c_int::from(faulttolerance),
208 )
209 };
210 (!monitor.is_null()).then_some(Self(monitor))
211 }
212
213 fn snapshot(&self) -> String {
214 snapshot_monitor(self.0)
215 }
216 }
217
218 impl Drop for OwnedMonitor {
219 fn drop(&mut self) {
220 unsafe {
221 ddcci_free_db(self.0);
222 }
223 }
224 }
225
226 fn snapshot_monitor(monitor: *mut CMonitorDb) -> String {
227 let mut snapshot = String::new();
228 let monitor_ref = unsafe { &*monitor };
229 snapshot.push_str(&format!(
230 "monitor name={} init={}\n",
231 c_xml_string(monitor_ref.name),
232 monitor_ref.init
233 ));
234
235 let mut group = monitor_ref.group_list;
236 while !group.is_null() {
237 let group_ref = unsafe { &*group };
238 snapshot.push_str(&format!("group name={}\n", c_xml_string(group_ref.name)));
239
240 let mut subgroup = group_ref.subgroup_list;
241 while !subgroup.is_null() {
242 let subgroup_ref = unsafe { &*subgroup };
243 let pattern = if subgroup_ref.pattern.is_null() {
244 String::new()
245 } else {
246 c_xml_string(subgroup_ref.pattern)
247 };
248 snapshot.push_str(&format!(
249 " subgroup name={} pattern={}\n",
250 c_xml_string(subgroup_ref.name),
251 pattern
252 ));
253
254 let mut control = subgroup_ref.control_list;
255 while !control.is_null() {
256 let control_ref = unsafe { &*control };
257 snapshot.push_str(&format!(
258 " control id={} name={} address=0x{:02X} delay={} type={} refresh={}\n",
259 c_xml_string(control_ref.id),
260 c_xml_string(control_ref.name),
261 control_ref.address,
262 control_ref.delay,
263 control_ref.control_type,
264 control_ref.refresh
265 ));
266
267 let mut value = control_ref.value_list;
268 while !value.is_null() {
269 let value_ref = unsafe { &*value };
270 let private = value as *mut CValueDbPrivate;
271 let private_ref = unsafe { &*private };
272 snapshot.push_str(&format!(
273 " value id={} name={} value=0x{:02X} value16=0x{:04X}\n",
274 c_xml_string(value_ref.id),
275 c_xml_string(value_ref.name),
276 value_ref.value,
277 private_ref.value16
278 ));
279 value = value_ref.next;
280 }
281
282 control = control_ref.next;
283 }
284
285 subgroup = subgroup_ref.next;
286 }
287
288 group = group_ref.next;
289 }
290
291 snapshot
292 }
293
294 fn c_xml_string(ptr: *mut c_uchar) -> String {
295 unsafe {
296 CStr::from_ptr(ptr as *const c_char)
297 .to_string_lossy()
298 .into_owned()
299 }
300 }