Skip to content

Commit ffc5e53

Browse files
committed
Added tests for close syscall
1 parent 4b7d119 commit ffc5e53

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

src/tests/fs_tests.rs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4210,4 +4210,148 @@ pub mod fs_tests {
42104210
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
42114211
lindrustfinalize();
42124212
}
4213+
4214+
#[test]
4215+
pub fn ut_lind_fs_close_regular_file() {
4216+
// acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4217+
// and also performs clean env setup
4218+
let _thelock = setup::lock_and_init();
4219+
4220+
let cage = interface::cagetable_getref(1);
4221+
4222+
// Create and open a regular file, then close it.
4223+
let fd = cage.open_syscall("/test_file", O_CREAT | O_RDWR, S_IRWXA);
4224+
assert!(fd >= 0);
4225+
4226+
// Write sample data to the file.
4227+
assert_eq!(cage.write_syscall(fd, str2cbuf("hello"), 5), 5);
4228+
4229+
// Close the file descriptor, which should succeed.
4230+
assert_eq!(cage.close_syscall(fd), 0);
4231+
4232+
// Attempt to close the file descriptor again to ensure it's already closed.
4233+
// Expect an error for "Invalid File Descriptor".
4234+
assert_eq!(cage.close_syscall(fd), -(Errno::EBADF as i32));
4235+
4236+
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
4237+
lindrustfinalize();
4238+
}
4239+
4240+
#[test]
4241+
pub fn ut_lind_fs_close_directory() {
4242+
// acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4243+
// and also performs clean env setup
4244+
let _thelock = setup::lock_and_init();
4245+
4246+
let cage = interface::cagetable_getref(1);
4247+
4248+
// Create a directory and open it.
4249+
let path = "/test_dir";
4250+
assert_eq!(cage.mkdir_syscall(path, S_IRWXA), 0);
4251+
let fd = cage.open_syscall(path, O_RDONLY, S_IRWXA);
4252+
assert!(fd >= 0);
4253+
4254+
// Close the directory file descriptor, which should succeed.
4255+
assert_eq!(cage.close_syscall(fd), 0);
4256+
4257+
// Attempt to close the file descriptor again to ensure it's already closed.
4258+
// Expect an error for "Invalid File Descriptor".
4259+
assert_eq!(cage.close_syscall(fd), -(Errno::EBADF as i32));
4260+
4261+
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
4262+
lindrustfinalize();
4263+
}
4264+
4265+
#[test]
4266+
pub fn ut_lind_fs_close_socket() {
4267+
// acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4268+
// and also performs clean env setup
4269+
let _thelock = setup::lock_and_init();
4270+
4271+
let cage = interface::cagetable_getref(1);
4272+
4273+
// Create a socket pair.
4274+
let mut socketpair = interface::SockPair::default();
4275+
assert_eq!(
4276+
Cage::socketpair_syscall(cage.clone(), AF_UNIX, SOCK_STREAM, 0, &mut socketpair),
4277+
0
4278+
);
4279+
4280+
// Close both the socket file descriptors, which should succeed.
4281+
assert_eq!(cage.close_syscall(socketpair.sock1), 0);
4282+
assert_eq!(cage.close_syscall(socketpair.sock2), 0);
4283+
4284+
// Attempt to close the file descriptors again to ensure they are already
4285+
// closed. Expect an error for "Invalid File Descriptor".
4286+
assert_eq!(cage.close_syscall(socketpair.sock1), -(Errno::EBADF as i32));
4287+
assert_eq!(cage.close_syscall(socketpair.sock2), -(Errno::EBADF as i32));
4288+
4289+
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
4290+
lindrustfinalize();
4291+
}
4292+
4293+
#[test]
4294+
pub fn ut_lind_fs_close_pipe() {
4295+
// acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4296+
// and also performs clean env setup
4297+
let _thelock = setup::lock_and_init();
4298+
4299+
let cage = interface::cagetable_getref(1);
4300+
4301+
// Create a pipe.
4302+
let mut pipe_fds = PipeArray::default();
4303+
assert_eq!(cage.pipe_syscall(&mut pipe_fds), 0);
4304+
let read_fd = pipe_fds.readfd;
4305+
let write_fd = pipe_fds.writefd;
4306+
4307+
// Write data to the pipe
4308+
let write_data = "Testing";
4309+
assert_eq!(
4310+
cage.write_syscall(write_fd, write_data.as_ptr(), write_data.len()),
4311+
write_data.len() as i32
4312+
);
4313+
4314+
// Read the data from the pipe.
4315+
let mut buf = sizecbuf(7);
4316+
assert_eq!(
4317+
cage.read_syscall(read_fd, buf.as_mut_ptr(), buf.len()),
4318+
write_data.len() as i32
4319+
);
4320+
assert_eq!(cbuf2str(&buf), write_data);
4321+
4322+
// Close the pipe file descriptors, which should succeed.
4323+
assert_eq!(cage.close_syscall(read_fd), 0);
4324+
assert_eq!(cage.close_syscall(write_fd), 0);
4325+
4326+
// Attempt to close the file descriptor again to ensure they are already closed.
4327+
// Expect an error for "Invalid File Descriptor".
4328+
assert_eq!(cage.close_syscall(read_fd), -(Errno::EBADF as i32));
4329+
assert_eq!(cage.close_syscall(write_fd), -(Errno::EBADF as i32));
4330+
4331+
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
4332+
lindrustfinalize();
4333+
}
4334+
4335+
#[test]
4336+
pub fn ut_lind_fs_close_chardev() {
4337+
// acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4338+
// and also performs clean env setup
4339+
let _thelock = setup::lock_and_init();
4340+
4341+
let cage = interface::cagetable_getref(1);
4342+
4343+
// Open a character device file.
4344+
let fd = cage.open_syscall("/dev/zero", O_RDWR, S_IRWXA);
4345+
assert!(fd >= 0);
4346+
4347+
// Close the character device file descriptor, which should succeed.
4348+
assert_eq!(cage.close_syscall(fd), 0);
4349+
4350+
// Attempt to close the file descriptor again to ensure it's already closed.
4351+
// Expect an error for "Invalid File Descriptor".
4352+
assert_eq!(cage.close_syscall(fd), -(Errno::EBADF as i32));
4353+
4354+
assert_eq!(cage.exit_syscall(EXIT_SUCCESS), EXIT_SUCCESS);
4355+
lindrustfinalize();
4356+
}
42134357
}

0 commit comments

Comments
 (0)