Skip to content

Commit ea8ad39

Browse files
Andrei TrandafirAndrei Trandafir
Andrei Trandafir
authored and
Andrei Trandafir
committed
cli: Update unit tests to sync with driver v0.5 integration
Following the update of the user-space components (CLI and enclave process), the existing unit tests must also be updated. In particular, this change affects the "cpu_info" module (since most of the CPU validation has been moved inside the driver), as well as several tests from the "test_dev_driver" module. Signed-off-by: Andrei Trandafir <[email protected]>
1 parent 1e44dd1 commit ea8ad39

File tree

2 files changed

+182
-394
lines changed

2 files changed

+182
-394
lines changed

src/enclave_proc/cpu_info.rs

Lines changed: 70 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -129,259 +129,126 @@ mod tests {
129129

130130
#[test]
131131
fn test_get_value_correct_format() {
132-
let test_proc0 = String::from("processor:\t3");
133-
let result0 = CpuInfos::get_value(test_proc0);
132+
let result0 = CpuInfo::get_value("\t3");
134133
assert!(result0.is_ok());
135134
assert_eq!(result0.unwrap(), 3);
136135

137-
let test_proc1 = String::from("processor\t : \t 4");
138-
let result1 = CpuInfos::get_value(test_proc1);
136+
let result1 = CpuInfo::get_value(" \t 4");
139137
assert!(result1.is_ok());
140138
assert_eq!(result1.unwrap(), 4);
141139

142-
let test_proc2 = String::from("\t\nprocessor\t: \n 12");
143-
let result2 = CpuInfos::get_value(test_proc2);
140+
let result2 = CpuInfo::get_value(" \n 12");
144141
assert!(result2.is_ok());
145142
assert_eq!(result2.unwrap(), 12);
146143
}
147144

148145
#[test]
149146
fn test_get_value_incorrect_format() {
150-
let test_proc0 = String::from("processor:\t-2");
151-
let result0 = CpuInfos::get_value(test_proc0);
147+
let result0 = CpuInfo::get_value("\t-2");
152148
assert!(result0.is_err());
153149
if let Err(err_str) = result0 {
154-
assert!(err_str.eq("invalid digit found in string"));
150+
assert!(err_str.starts_with("Failed to parse CPU ID"));
155151
}
156152

157-
let test_proc1 = String::from("processor:\n\n0x06");
158-
let result1 = CpuInfos::get_value(test_proc1);
153+
let result1 = CpuInfo::get_value("\n\n0x06");
159154
assert!(result1.is_err());
160155
if let Err(err_str) = result1 {
161-
assert!(err_str.eq("invalid digit found in string"));
156+
assert!(err_str.starts_with("Failed to parse CPU ID"));
162157
}
163158

164-
let test_proc2 = String::from("processor: processor");
165-
let result2 = CpuInfos::get_value(test_proc2);
159+
let result2 = CpuInfo::get_value(" processor");
166160
assert!(result2.is_err());
167161
if let Err(err_str) = result2 {
168-
assert!(err_str.eq("invalid digit found in string"));
162+
assert!(err_str.starts_with("Failed to parse CPU ID"));
169163
}
170164
}
171165

172166
#[test]
173-
fn test_get_core_id_correct() {
174-
let cpu_infos = CpuInfos::new();
175-
176-
if let Ok(cpu_infos) = cpu_infos {
177-
for i in 0..cpu_infos.core_ids.len() {
178-
let cpu_id = i as u32;
179-
let index = cpu_infos.core_ids.iter().position(|r| r.cpu_id == cpu_id);
180-
assert_eq!(
181-
cpu_infos.get_core_id(cpu_id).unwrap(),
182-
cpu_infos.core_ids[index.unwrap()].core_id
183-
);
184-
}
185-
}
186-
}
167+
fn test_get_cpu_config_invalid_input() {
168+
let cpu_info = CpuInfo::new().unwrap();
169+
let mut run_args = RunEnclavesArgs {
170+
eif_path: String::new(),
171+
enclave_cid: None,
172+
memory_mib: 0,
173+
debug_mode: None,
174+
cpu_ids: None,
175+
cpu_count: Some(343),
176+
};
187177

188-
#[test]
189-
fn test_get_core_id_incorrect() {
190-
let cpu_infos = CpuInfos::new();
191-
192-
if let Ok(cpu_infos) = cpu_infos {
193-
let cpu_id0 = 200;
194-
let result0 = cpu_infos.get_core_id(cpu_id0);
178+
let mut result = cpu_info.get_cpu_config(&run_args);
179+
assert!(result.is_err());
195180

196-
assert_eq!(result0, None);
181+
if let Err(err_str) = result {
182+
assert!(err_str.starts_with("Insufficient CPUs available"));
197183
}
198-
}
199-
200-
#[test]
201-
fn test_is_hyperthreading_on_on() {
202-
let mut cpu_info = Vec::<CpuInfo>::new();
203-
204-
cpu_info.push(CpuInfo::new(2, 0));
205-
cpu_info.push(CpuInfo::new(0, 0));
206-
207-
let result = CpuInfos::is_hyper_threading_on(&cpu_info);
208-
209-
assert_eq!(result, true);
210-
}
211184

212-
#[test]
213-
fn test_is_hyperthreading_on_off() {
214-
let mut cpu_info = Vec::<CpuInfo>::new();
215-
216-
cpu_info.push(CpuInfo::new(3, 1));
217-
cpu_info.push(CpuInfo::new(0, 0));
218-
219-
let result = CpuInfos::is_hyper_threading_on(&cpu_info);
185+
run_args.cpu_count = None;
186+
run_args.cpu_ids = Some(vec![1, 2, 3, 4, 5, 6, 7]);
187+
result = cpu_info.get_cpu_config(&run_args);
188+
assert!(result.is_err());
220189

221-
assert_eq!(result, false);
190+
if let Err(err_str) = result {
191+
assert!(err_str.starts_with("The CPU with ID"));
192+
assert!(err_str.ends_with("is not available."));
193+
}
222194
}
223195

224196
#[test]
225-
fn test_get_cpu_ids_invalid_input() {
226-
let cpu_infos = CpuInfos::new();
227-
228-
if let Ok(cpu_infos) = cpu_infos {
229-
if cpu_infos.hyper_threading {
230-
let cpu_count = 1;
231-
let result = cpu_infos.get_cpu_ids(cpu_count);
232-
233-
assert!(result.is_err());
197+
fn test_get_cpu_config_valid_input() {
198+
let cpu_info = CpuInfo::new().unwrap();
199+
let mut run_args = RunEnclavesArgs {
200+
eif_path: String::new(),
201+
enclave_cid: None,
202+
memory_mib: 0,
203+
debug_mode: None,
204+
cpu_ids: None,
205+
cpu_count: Some(2),
206+
};
234207

235-
if let Err(err_str) = result {
236-
assert!(err_str.eq("cpu_count should be an even number."));
237-
}
238-
} else {
239-
let cpu_count = 43;
240-
let result = cpu_infos.get_cpu_ids(cpu_count);
208+
let mut result = cpu_info.get_cpu_config(&run_args);
209+
assert!(result.is_ok());
210+
assert!(result.unwrap() == EnclaveCpuConfig::Count(2));
241211

242-
assert!(result.is_err());
212+
run_args.cpu_count = None;
213+
run_args.cpu_ids = Some(vec![1, 3]);
243214

244-
if let Err(err_str) = result {
245-
assert!(err_str.starts_with("Could not find the requested number of cpus."));
246-
}
247-
}
248-
}
249-
}
250-
251-
#[test]
252-
fn test_get_cpu_ids_valid_input() {
253-
let cpu_infos = CpuInfos::new();
254-
255-
if let Ok(cpu_infos) = cpu_infos {
256-
if cpu_infos.hyper_threading {
257-
let cpu_count = 2;
258-
let result = cpu_infos.get_cpu_ids(cpu_count);
259-
260-
assert!(result.is_ok());
261-
assert_eq!(2, result.unwrap().len());
262-
} else {
263-
let cpu_count = 1;
264-
let result = cpu_infos.get_cpu_ids(cpu_count);
265-
266-
assert!(result.is_ok());
267-
assert_eq!(1, result.unwrap().len());
268-
}
269-
}
270-
}
271-
272-
#[test]
273-
fn test_contains_sibling_pairs() {
274-
let cpu_infos = CpuInfos::new();
275-
276-
if let Ok(cpu_infos) = cpu_infos {
277-
let cpu_ids = cpu_infos
278-
.core_ids
279-
.iter()
280-
.filter(|r| r.core_id == 0)
281-
.map(|r| r.cpu_id)
282-
.collect::<Vec<_>>();
283-
284-
let result = cpu_infos.contains_sibling_pairs(&cpu_ids);
285-
if CpuInfos::is_hyper_threading_on(&cpu_infos.core_ids) {
286-
assert!(result);
287-
} else {
288-
assert!(!result);
289-
}
290-
}
215+
result = cpu_info.get_cpu_config(&run_args);
216+
assert!(result.is_ok());
217+
assert!(result.unwrap() == EnclaveCpuConfig::List(vec![1, 3]));
291218
}
292219

293220
#[test]
294221
fn test_get_cpu_candidates() {
295-
let cpu_infos = CpuInfos::new();
222+
let cpu_info = CpuInfo::new().unwrap();
223+
let candidate_cpus = cpu_info.get_cpu_candidates();
296224

297-
if let Ok(cpu_infos) = cpu_infos {
298-
let candidate_cpus = cpu_infos.get_cpu_candidates();
299-
300-
assert!(candidate_cpus.len() > 0);
301-
}
225+
assert!(candidate_cpus.len() > 0);
302226
}
303227

304228
#[test]
305-
fn test_check_cpu_ids_invalid() {
306-
let cpu_infos = CpuInfos::new();
307-
308-
if let Ok(cpu_infos) = cpu_infos {
309-
if cpu_infos.hyper_threading {
310-
let mut cpu_ids = Vec::<u32>::new();
311-
312-
cpu_ids.push(1);
313-
314-
let result = cpu_infos.check_cpu_ids(&cpu_ids);
315-
316-
assert!(result.is_err());
317-
if let Err(err_str) = result {
318-
assert!(err_str
319-
.eq("Hyper-threading is enabled, so sibling pairs need to be provided"));
320-
}
321-
} else {
322-
let mut cpu_ids = Vec::<u32>::new();
323-
324-
// Add all cpus having core_id set to 0
325-
for i in 0..cpu_infos.core_ids.len() {
326-
// Safe to unwrap since `i` will not exceed the bounds
327-
if cpu_infos.core_ids.get(i).unwrap().core_id == 0 {
328-
cpu_ids.push(cpu_infos.core_ids.get(i).unwrap().cpu_id);
329-
}
330-
}
331-
332-
let result = cpu_infos.check_cpu_ids(&cpu_ids);
333-
334-
assert!(result.is_err());
335-
if let Err(err_str) = result {
336-
assert!(err_str.starts_with("Cpus with core id 0 can't be used."));
337-
}
338-
}
339-
}
340-
}
229+
fn test_check_cpu_ids() {
230+
let cpu_info = CpuInfo::new().unwrap();
231+
let mut cpu_ids: Vec<u32> = vec![1];
341232

342-
#[test]
343-
fn test_check_cpu_ids_valid() {
344-
let cpu_infos = CpuInfos::new();
233+
let mut result = cpu_info.check_cpu_ids(&cpu_ids);
234+
assert!(result.is_ok());
345235

346-
if let Ok(cpu_infos) = cpu_infos {
347-
if cpu_infos.hyper_threading {
348-
let mut cpu_ids = Vec::<u32>::new();
349-
350-
// Add 2 cpus having core_id != 0
351-
for i in 0..cpu_infos.core_ids.len() {
352-
// Safe to unwrap since `i` will not exceed the bounds
353-
if cpu_infos.core_ids.get(i).unwrap().core_id != 0 {
354-
cpu_ids.push(cpu_infos.core_ids.get(i).unwrap().cpu_id);
355-
356-
if cpu_ids.len() == 2 {
357-
break;
358-
}
359-
}
360-
}
361-
362-
let result = cpu_infos.check_cpu_ids(&cpu_ids);
363-
364-
assert!(result.is_ok());
365-
}
236+
cpu_ids = vec![1, 1];
237+
result = cpu_info.check_cpu_ids(&cpu_ids);
238+
assert!(result.is_err());
239+
if let Err(err_str) = result {
240+
assert!(err_str.eq("The CPU ID list contains 1 duplicate(s)."));
366241
}
367-
}
368-
369-
#[test]
370-
fn test_get_siblings() {
371-
let cpu_infos = CpuInfos::new();
372-
373-
if let Ok(cpu_infos) = cpu_infos {
374-
let mut core_ids = HashSet::<u32>::new();
375-
376-
for i in 0..cpu_infos.core_ids.len() {
377-
// Safe to unwrap since `i` will not exceed the bounds
378-
core_ids.insert(cpu_infos.core_ids.get(i).unwrap().core_id);
379-
}
380242

381-
let siblings = cpu_infos.get_siblings();
243+
cpu_ids = vec![1, 3];
244+
result = cpu_info.check_cpu_ids(&cpu_ids);
245+
assert!(result.is_ok());
382246

383-
// Do not consider core_id 0
384-
assert_eq!(core_ids.len() - 1, siblings.len());
247+
cpu_ids = vec![1, 3, 5];
248+
result = cpu_info.check_cpu_ids(&cpu_ids);
249+
assert!(result.is_err());
250+
if let Err(err_str) = result {
251+
assert!(err_str.eq("The CPU with ID 5 is not available."));
385252
}
386253
}
387254
}

0 commit comments

Comments
 (0)