diff --git a/src/runtime/ring0/mod.rs b/src/runtime/ring0/mod.rs index a9af9d9a..2a351f06 100644 --- a/src/runtime/ring0/mod.rs +++ b/src/runtime/ring0/mod.rs @@ -70,29 +70,18 @@ impl Ring0Registry { /// Ring0Context を profile に応じて構築 pub fn build(profile: RuntimeProfile) -> Ring0Context { match profile { - RuntimeProfile::Default => Self::build_default(), - RuntimeProfile::NoFs => Self::build_no_fs(), + RuntimeProfile::Default => Self::build_with_fs(Arc::new(StdFs)), + RuntimeProfile::NoFs => Self::build_with_fs(Arc::new(NoFsApi)), } } - fn build_default() -> Ring0Context { + fn build_with_fs(fs: Arc) -> Ring0Context { Ring0Context { mem: Arc::new(StdMem::new()), io: Arc::new(StdIo), time: Arc::new(StdTime), log: Arc::new(StdLog), - fs: Arc::new(StdFs), - thread: Arc::new(StdThread), - } - } - - fn build_no_fs() -> Ring0Context { - Ring0Context { - mem: Arc::new(StdMem::new()), - io: Arc::new(StdIo), - time: Arc::new(StdTime), - log: Arc::new(StdLog), - fs: Arc::new(NoFsApi), // Phase 112: NoFs profile では FsApi を disabled に + fs, thread: Arc::new(StdThread), } } @@ -133,6 +122,15 @@ pub fn get_global_ring0() -> Arc { mod tests { use super::*; + fn unsafe_dealloc(ptr: *mut u8, size: usize) { + unsafe { + std::alloc::dealloc( + ptr, + std::alloc::Layout::from_size_align_unchecked(size, 1), + ) + } + } + #[test] fn test_ring0_context_creation() { let ring0 = default_ring0(); @@ -173,12 +171,7 @@ mod tests { ring0.mem.free(ptr); // Clean up - unsafe { - std::alloc::dealloc( - ptr, - std::alloc::Layout::from_size_align_unchecked(512, 1), - ) - }; + unsafe_dealloc(ptr, 512); } // Phase 112: Ring0Registry tests @@ -217,4 +210,48 @@ mod tests { ctx.log.info("Test from default_ring0()"); assert!(ctx.time.now().is_ok()); } + + #[test] + fn test_nofs_api_read_to_string() { + let api = NoFsApi; + let result = api.read_to_string(std::path::Path::new("/tmp/test.txt")); + assert!(result.is_err()); + assert!(result.unwrap_err().to_string().contains("disabled")); + } + + #[test] + fn test_nofs_api_read() { + let api = NoFsApi; + assert!(api.read(std::path::Path::new("/tmp/test.txt")).is_err()); + } + + #[test] + fn test_nofs_api_write_all() { + let api = NoFsApi; + assert!(api.write_all(std::path::Path::new("/tmp/test.txt"), b"data").is_err()); + } + + #[test] + fn test_nofs_api_append_all() { + let api = NoFsApi; + assert!(api.append_all(std::path::Path::new("/tmp/test.txt"), b"data").is_err()); + } + + #[test] + fn test_nofs_api_exists() { + let api = NoFsApi; + assert!(!api.exists(std::path::Path::new("/tmp/test.txt"))); + } + + #[test] + fn test_nofs_api_metadata() { + let api = NoFsApi; + assert!(api.metadata(std::path::Path::new("/tmp/test.txt")).is_err()); + } + + #[test] + fn test_nofs_api_canonicalize() { + let api = NoFsApi; + assert!(api.canonicalize(std::path::Path::new("/tmp/test.txt")).is_err()); + } } diff --git a/src/runtime/ring0/std_impls.rs b/src/runtime/ring0/std_impls.rs index ba8c2fa0..11a76448 100644 --- a/src/runtime/ring0/std_impls.rs +++ b/src/runtime/ring0/std_impls.rs @@ -232,31 +232,25 @@ impl ThreadApi for StdThread { /// FileSystem 操作がすべて「無効」として機能する。 /// Phase 109 の NoFsFileIo(FileIo trait)と異なり、 /// Ring0 レベルの FsApi trait を実装する。 +const NOFS_ERROR_MSG: &str = "FileSystem operations disabled in no-fs profile"; + pub struct NoFsApi; impl FsApi for NoFsApi { fn read_to_string(&self, _path: &Path) -> Result { - Err(IoError::Other( - "FileSystem operations disabled in no-fs profile".to_string() - )) + Err(IoError::Other(NOFS_ERROR_MSG.to_string())) } fn read(&self, _path: &Path) -> Result, IoError> { - Err(IoError::Other( - "FileSystem operations disabled in no-fs profile".to_string() - )) + Err(IoError::Other(NOFS_ERROR_MSG.to_string())) } fn write_all(&self, _path: &Path, _data: &[u8]) -> Result<(), IoError> { - Err(IoError::Other( - "FileSystem operations disabled in no-fs profile".to_string() - )) + Err(IoError::Other(NOFS_ERROR_MSG.to_string())) } fn append_all(&self, _path: &Path, _data: &[u8]) -> Result<(), IoError> { - Err(IoError::Other( - "FileSystem operations disabled in no-fs profile".to_string() - )) + Err(IoError::Other(NOFS_ERROR_MSG.to_string())) } fn exists(&self, _path: &Path) -> bool { @@ -264,15 +258,11 @@ impl FsApi for NoFsApi { } fn metadata(&self, _path: &Path) -> Result { - Err(IoError::Other( - "FileSystem operations disabled in no-fs profile".to_string() - )) + Err(IoError::Other(NOFS_ERROR_MSG.to_string())) } fn canonicalize(&self, _path: &Path) -> Result { - Err(IoError::Other( - "FileSystem operations disabled in no-fs profile".to_string() - )) + Err(IoError::Other(NOFS_ERROR_MSG.to_string())) } } @@ -282,6 +272,15 @@ impl FsApi for NoFsApi { mod stdmem_tests { use super::*; + fn unsafe_dealloc(ptr: *mut u8, size: usize) { + unsafe { + std::alloc::dealloc( + ptr, + std::alloc::Layout::from_size_align_unchecked(size, 1), + ) + } + } + #[test] fn test_stdmem_alloc() { let mem = StdMem::new(); @@ -293,7 +292,7 @@ mod stdmem_tests { assert_eq!(stats.current, 1024, "current should be 1024"); // Clean up - unsafe { std::alloc::dealloc(ptr, std::alloc::Layout::from_size_align_unchecked(1024, 1)) }; + unsafe_dealloc(ptr, 1024); } #[test] @@ -314,7 +313,7 @@ mod stdmem_tests { assert_eq!(stats.freed, 1, "freed count should be 1"); // Clean up (actual deallocation) - unsafe { std::alloc::dealloc(ptr, std::alloc::Layout::from_size_align_unchecked(512, 1)) }; + unsafe_dealloc(ptr, 512); } #[test]