Skip to content

Commit

Permalink
mm/vm/file_mappings/tests: enable most tests inside the SVSM
Browse files Browse the repository at this point in the history
Enable most file mapping tests when running inside the SVSM. For now,
page fault tests do not seem to work, so keep them disabled. This
commit also includes a very small refactor in order to be able to
properly clean up opened files.

Signed-off-by: Carlos López <carlos.lopez@suse.com>
  • Loading branch information
00xc committed Nov 24, 2023
1 parent ae5b114 commit 575428c
Showing 1 changed file with 36 additions and 38 deletions.
74 changes: 36 additions & 38 deletions src/mm/vm/mapping/file_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl VirtualMapping for VMFileMapping {
mod tests {
use crate::{
address::{Address, VirtAddr},
fs::{create, open, FileHandle, TestFileSystemGuard},
fs::{create, open, unlink, FileHandle, TestFileSystemGuard},
mm::{
alloc::{TestRootMem, DEFAULT_TEST_MEMORY_SIZE},
pagetable::PTEntryFlags,
Expand All @@ -259,115 +259,116 @@ mod tests {

use super::*;

fn create_512b_test_file() -> FileHandle {
fn create_512b_test_file() -> (FileHandle, &'static str) {
let fh = create("test1").unwrap();
let buf = [0xffu8; 512];
fh.write(&buf).expect("File write failed");
fh
(fh, "test1")
}

fn create_16k_test_file() -> FileHandle {
fn create_16k_test_file() -> (FileHandle, &'static str) {
let fh = create("test1").unwrap();
let mut buf = [0xffu8; PAGE_SIZE * 4];
buf[PAGE_SIZE] = 1;
buf[PAGE_SIZE * 2] = 2;
buf[PAGE_SIZE * 3] = 3;
fh.write(&buf).expect("File write failed");
fh
(fh, "test1")
}

fn create_5000b_test_file() -> FileHandle {
fn create_5000b_test_file() -> (FileHandle, &'static str) {
let fh = create("test1").unwrap();
let buf = [0xffu8; 5000];
fh.write(&buf).expect("File write failed");
fh
(fh, "test1")
}

#[test]
#[cfg_attr(test_in_svsm, ignore = "FIXME")]
fn test_create_mapping() {
let _test_mem = TestRootMem::setup(DEFAULT_TEST_MEMORY_SIZE);
let _test_fs = TestFileSystemGuard::setup();

let fh = create_512b_test_file();
let (fh, name) = create_512b_test_file();
let vm = VMFileMapping::new(fh, 0, 512, VMFileMappingPermission::Read)
.expect("Failed to create new VMFileMapping");
assert_eq!(vm.mapping_size(), PAGE_SIZE);
assert_eq!(vm.permission, VMFileMappingPermission::Read);
assert_eq!(vm.pages.len(), 1);
unlink(name).unwrap();
}

#[test]
#[cfg_attr(test_in_svsm, ignore = "FIXME")]
fn test_create_unaligned_offset() {
let _test_mem = TestRootMem::setup(DEFAULT_TEST_MEMORY_SIZE);
let _test_fs = TestFileSystemGuard::setup();

// Not page aligned
let offset = PAGE_SIZE + 0x60;

let fh = create_16k_test_file();
let fh2 = open("test1").unwrap();
let (fh, name) = create_16k_test_file();
let fh2 = open(name).unwrap();
let vm = VMFileMapping::new(
fh,
offset,
fh2.size() - offset,
VMFileMappingPermission::Read,
);
assert!(vm.is_err());
unlink(name).unwrap();
}

#[test]
#[cfg_attr(test_in_svsm, ignore = "FIXME")]
fn test_create_size_too_large() {
let _test_mem = TestRootMem::setup(DEFAULT_TEST_MEMORY_SIZE);
let _test_fs = TestFileSystemGuard::setup();

let fh = create_16k_test_file();
let fh2 = open("test1").unwrap();
let (fh, name) = create_16k_test_file();
let fh2 = open(name).unwrap();
let vm = VMFileMapping::new(fh, 0, fh2.size() + 1, VMFileMappingPermission::Read);
assert!(vm.is_err());
unlink(name).unwrap();
}

#[test]
#[cfg_attr(test_in_svsm, ignore = "FIXME")]
fn test_create_offset_overflow() {
let _test_mem = TestRootMem::setup(DEFAULT_TEST_MEMORY_SIZE);
let _test_fs = TestFileSystemGuard::setup();

let fh = create_16k_test_file();
let fh2 = open("test1").unwrap();
let (fh, name) = create_16k_test_file();
let fh2 = open(name).unwrap();
let vm = VMFileMapping::new(fh, PAGE_SIZE, fh2.size(), VMFileMappingPermission::Read);
assert!(vm.is_err());
unlink(name).unwrap();
}

fn test_map_first_page(permission: VMFileMappingPermission) {
let _test_mem = TestRootMem::setup(DEFAULT_TEST_MEMORY_SIZE);
let _test_fs = TestFileSystemGuard::setup();

let fh = create_512b_test_file();
let (fh, name) = create_512b_test_file();
let vm =
VMFileMapping::new(fh, 0, 512, permission).expect("Failed to create new VMFileMapping");

let res = vm
.map(0)
.expect("Mapping of first VMFileMapping page failed");

let fh2 = open("test1").unwrap();
let fh2 = open(name).unwrap();
assert_eq!(
fh2.mapping(0)
.expect("Failed to get file page mapping")
.phys_addr(),
res
);
unlink(name).unwrap();
}

fn test_map_multiple_pages(permission: VMFileMappingPermission) {
let _test_mem = TestRootMem::setup(DEFAULT_TEST_MEMORY_SIZE);
let _test_fs = TestFileSystemGuard::setup();

let fh = create_16k_test_file();
let fh2 = open("test1").unwrap();
let (fh, name) = create_16k_test_file();
let fh2 = open(name).unwrap();
let vm = VMFileMapping::new(fh, 0, fh2.size(), permission)
.expect("Failed to create new VMFileMapping");

Expand All @@ -383,14 +384,15 @@ mod tests {
res
);
}
unlink(name).unwrap();
}

fn test_map_unaligned_file_size(permission: VMFileMappingPermission) {
let _test_mem = TestRootMem::setup(DEFAULT_TEST_MEMORY_SIZE);
let _test_fs = TestFileSystemGuard::setup();

let fh = create_5000b_test_file();
let fh2 = open("test1").unwrap();
let (fh, name) = create_5000b_test_file();
let fh2 = open(name).unwrap();
let vm = VMFileMapping::new(fh, 0, fh2.size(), permission)
.expect("Failed to create new VMFileMapping");

Expand All @@ -409,14 +411,15 @@ mod tests {
res
);
}
unlink(name).unwrap();
}

fn test_map_non_zero_offset(permission: VMFileMappingPermission) {
let _test_mem = TestRootMem::setup(DEFAULT_TEST_MEMORY_SIZE);
let _test_fs = TestFileSystemGuard::setup();

let fh = create_16k_test_file();
let fh2 = open("test1").unwrap();
let (fh, name) = create_16k_test_file();
let fh2 = open(name).unwrap();
let vm = VMFileMapping::new(fh, 2 * PAGE_SIZE, PAGE_SIZE, permission)
.expect("Failed to create new VMFileMapping");

Expand All @@ -433,52 +436,45 @@ mod tests {
.phys_addr(),
res
);
unlink(name).unwrap();
}

#[test]
#[cfg_attr(test_in_svsm, ignore = "FIXME")]
fn test_map_first_page_readonly() {
test_map_first_page(VMFileMappingPermission::Read)
}

#[test]
#[cfg_attr(test_in_svsm, ignore = "FIXME")]
fn test_map_multiple_pages_readonly() {
test_map_multiple_pages(VMFileMappingPermission::Read)
}

#[test]
#[cfg_attr(test_in_svsm, ignore = "FIXME")]
fn test_map_unaligned_file_size_readonly() {
test_map_unaligned_file_size(VMFileMappingPermission::Read)
}

#[test]
#[cfg_attr(test_in_svsm, ignore = "FIXME")]
fn test_map_non_zero_offset_readonly() {
test_map_non_zero_offset(VMFileMappingPermission::Read)
}

#[test]
#[cfg_attr(test_in_svsm, ignore = "FIXME")]
fn test_map_first_page_readwrite() {
test_map_first_page(VMFileMappingPermission::Write)
}

#[test]
#[cfg_attr(test_in_svsm, ignore = "FIXME")]
fn test_map_multiple_pages_readwrite() {
test_map_multiple_pages(VMFileMappingPermission::Write)
}

#[test]
#[cfg_attr(test_in_svsm, ignore = "FIXME")]
fn test_map_unaligned_file_size_readwrite() {
test_map_unaligned_file_size(VMFileMappingPermission::Write)
}

#[test]
#[cfg_attr(test_in_svsm, ignore = "FIXME")]
fn test_map_non_zero_offset_readwrite() {
test_map_non_zero_offset(VMFileMappingPermission::Write)
}
Expand All @@ -489,8 +485,8 @@ mod tests {
let _test_mem = TestRootMem::setup(DEFAULT_TEST_MEMORY_SIZE);
let _test_fs = TestFileSystemGuard::setup();

let fh = create_16k_test_file();
let fh2 = open("test1").unwrap();
let (fh, name) = create_16k_test_file();
let fh2 = open(name).unwrap();
let mut vm = VMFileMapping::new(fh, 0, fh2.size(), VMFileMappingPermission::Write)
.expect("Failed to create new VMFileMapping");

Expand Down Expand Up @@ -525,6 +521,7 @@ mod tests {
vm.map(PAGE_SIZE).expect("Failed to map file page"),
res.paddr
);
unlink(name).unwrap();
}

#[test]
Expand All @@ -533,8 +530,8 @@ mod tests {
let _test_mem = TestRootMem::setup(DEFAULT_TEST_MEMORY_SIZE);
let _test_fs = TestFileSystemGuard::setup();

let fh = create_16k_test_file();
let fh2 = open("test1").unwrap();
let (fh, name) = create_16k_test_file();
let fh2 = open(name).unwrap();
let mut vm = VMFileMapping::new(fh, 0, fh2.size(), VMFileMappingPermission::Write)
.expect("Failed to create new VMFileMapping");

Expand Down Expand Up @@ -564,5 +561,6 @@ mod tests {
vm.map(PAGE_SIZE * 2).expect("Failed to map file page"),
res.paddr
);
unlink(name).unwrap();
}
}

0 comments on commit 575428c

Please sign in to comment.