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
  • Loading branch information
nayuta-ai committed Oct 19, 2024
1 parent 62e5e1a commit ba5800f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 37 deletions.
39 changes: 19 additions & 20 deletions tests/contest/contest/src/tests/linux_masked_paths/masked_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,34 @@ fn check_masked_paths() -> TestResult {
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
24 changes: 15 additions & 9 deletions tests/contest/runtimetest/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,17 +564,23 @@ 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;
}
}
}
}
}
50 changes: 42 additions & 8 deletions tests/contest/runtimetest/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,56 @@
use std::io::{self, Read};
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()
fn test_file_read_access(path: &str) -> Result<bool, std::io::Error> {
let mut file = OpenOptions::new()
.create(false)
.read(true)
.open(path)?;
Ok(())

// 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(err) => {
Ok(false) // If there's an error, then it's not readable
}
}
}

fn is_file_like(mode: u32) -> bool {
Expand Down Expand Up @@ -60,7 +94,7 @@ fn test_dir_write_access(path: &str) -> Result<(), std::io::Error> {
Ok(())
}

pub fn test_write_access(path: &str) -> Result<(), std::io::Error> {
pub fn test_write_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 ba5800f

Please sign in to comment.