Skip to content

Commit

Permalink
fix: add logic to check if a file's contents or directory are empty
Browse files Browse the repository at this point in the history
Signed-off-by: nayuta-ai <[email protected]>
  • Loading branch information
nayuta-ai committed Oct 19, 2024
1 parent 62e5e1a commit 2baa120
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 41 deletions.
41 changes: 20 additions & 21 deletions tests/contest/contest/src/tests/linux_masked_paths/masked_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,41 +52,40 @@ fn check_masked_paths() -> TestResult {
let spec = get_spec(masked_paths);

test_inside_container(spec, &|bundle_path| {
use std::{fs, io};
use std::fs;
let test_dir = bundle_path.join(&masked_dir_sub);
if let Err(e) = fs::create_dir_all(&test_dir) {
bail!(e)
}

match fs::File::create(test_dir.join("tmp")) {
io::Result::Ok(_) => { /*This is expected*/ }
io::Result::Err(e) => {
bail!(e)
}
if let Err(e) = fs::File::create(test_dir.join("tmp")) {
bail!(e)
}

// runtimetest cannot check the readability of empty files, so
// write something.
let test_sub_sub_file = bundle_path.join(&masked_file_sub_sub);
match fs::File::create(test_sub_sub_file) {
io::Result::Ok(_) => { /*This is expected*/ }
io::Result::Err(e) => {
bail!(e)
}
if let Err(e) = fs::File::create(&test_sub_sub_file) {
bail!(e)
}
if let Err(e) = fs::write(&test_sub_sub_file, b"secrets") {
bail!(e)
}

let test_sub_file = bundle_path.join(&masked_file_sub);
match fs::File::create(test_sub_file) {
io::Result::Ok(_) => { /*This is expected*/ }
io::Result::Err(e) => {
bail!(e)
}
if let Err(e) = fs::File::create(&test_sub_file) {
bail!(e)
}
if let Err(e) = fs::write(&test_sub_file, b"secrets") {
bail!(e)
}

let test_file = bundle_path.join(masked_file);
match fs::File::create(test_file) {
io::Result::Ok(_) => { /*This is expected*/ }
io::Result::Err(e) => {
bail!(e)
}
if let Err(e) = fs::File::create(&test_file) {
bail!(e)
}
if let Err(e) = fs::write(&test_file, b"secrets") {
bail!(e)
}

Ok(())
Expand Down
26 changes: 17 additions & 9 deletions tests/contest/runtimetest/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,17 +564,25 @@ pub fn validate_masked_paths(spec: &Spec) {
// TODO when https://github.com/rust-lang/rust/issues/86442 stabilizes,
// change manual matching of i32 to e.kind() and match statement
for path in masked_paths {
if let std::io::Result::Err(e) = test_read_access(path) {
let errno = Errno::from_raw(e.raw_os_error().unwrap());
if errno == Errno::ENOENT {
/* This is expected */
} else {
eprintln!("in masked paths, error in testing read access for path {path} : {e:?}");
match test_read_access(path) {
Ok(true) => {
eprintln!(
"in masked paths, expected path {path} to be masked, but was found readable"
);
return;
}
} else {
eprintln!("in masked paths, error in testing read access for path {path}");
return;
Ok(false) => { /* This is expected */ }
Err(e) => {
let errno = Errno::from_raw(e.raw_os_error().unwrap());
if errno == Errno::ENOENT {
/* This is expected */
} else {
eprintln!(
"in masked paths, error in testing read access for path {path} : {errno:?}"
);
return;
}
}
}
}
}
52 changes: 41 additions & 11 deletions tests/contest/runtimetest/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,52 @@
use std::fs;
use std::fs::{metadata, symlink_metadata};
use std::fs::{metadata, symlink_metadata, OpenOptions};
use std::io::{self, Read};
use std::os::unix::prelude::MetadataExt;
use std::path::PathBuf;
use std::process::Command;

use nix::sys::stat::{stat, SFlag};

fn test_file_read_access(path: &str) -> Result<(), std::io::Error> {
let _ = std::fs::OpenOptions::new()
.create(false)
.read(true)
.open(path)?;
Ok(())
fn test_file_read_access(path: &str) -> Result<bool, std::io::Error> {
let mut file = OpenOptions::new().create(false).read(true).open(path)?;

// Create a buffer with a capacity of 1 byte
let mut buffer = [0u8; 1];
match file.read(&mut buffer) {
Ok(_) => {
let mut contents = String::new();
file.read_to_string(&mut contents)?;
if contents.is_empty() {
// empty file
return Ok(false);
}
Ok(true)
}
Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => Ok(false),
Err(e) => Err(e),
}
}

fn test_dir_read_access(path: &str) -> Result<(), std::io::Error> {
let _ = std::fs::read_dir(path)?;
Ok(())
fn test_dir_read_access(path: &str) -> Result<bool, std::io::Error> {
let entries = std::fs::read_dir(path);

match entries {
Ok(mut entries_iter) => {
// Get the first entry
match entries_iter.next() {
Some(entry) => {
match entry {
Ok(_) => Ok(true), // If the entry is Ok, then it's readable
Err(_) => Ok(false), // If the entry is Err, then it's not readable
}
}
None => Ok(false), // If there are no entries, then it's not readable
}
}
Err(_) => {
Ok(false) // If there's an error, then it's not readable
}
}
}

fn is_file_like(mode: u32) -> bool {
Expand All @@ -30,7 +60,7 @@ fn is_dir(mode: u32) -> bool {
mode & SFlag::S_IFDIR.bits() != 0
}

pub fn test_read_access(path: &str) -> Result<(), std::io::Error> {
pub fn test_read_access(path: &str) -> Result<bool, std::io::Error> {
let fstat = stat(path)?;
let mode = fstat.st_mode;
if is_file_like(mode) {
Expand Down

0 comments on commit 2baa120

Please sign in to comment.