@@ -4394,4 +4394,332 @@ pub mod fs_tests {
43944394 assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
43954395 lindrustfinalize ( ) ;
43964396 }
4397+
4398+ #[ test]
4399+ pub fn ut_lind_fs_lseek_on_file ( ) {
4400+ // acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4401+ // and also performs clean env setup
4402+ let _thelock = setup:: lock_and_init ( ) ;
4403+
4404+ let cage = interface:: cagetable_getref ( 1 ) ;
4405+
4406+ // Test to create a file and check if seeking to a new location is possible.
4407+ let fd = cage. open_syscall ( "/test_file" , O_CREAT | O_WRONLY , S_IRWXA ) ;
4408+ assert ! ( fd >= 0 ) ;
4409+
4410+ // Attempt to seek within the file and check if it succeeds
4411+ assert_eq ! ( cage. lseek_syscall( fd, 10 , SEEK_SET ) , 10 ) ;
4412+
4413+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
4414+ lindrustfinalize ( ) ;
4415+ }
4416+
4417+ #[ test]
4418+ pub fn ut_lind_fs_lseek_on_directory ( ) {
4419+ // acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4420+ // and also performs clean env setup
4421+ let _thelock = setup:: lock_and_init ( ) ;
4422+
4423+ let cage = interface:: cagetable_getref ( 1 ) ;
4424+
4425+ // Create a directory and try to seek within it.
4426+ let path = "/test_dir" ;
4427+ assert_eq ! ( cage. mkdir_syscall( path, S_IRWXA ) , 0 ) ;
4428+ let fd = cage. open_syscall ( path, O_RDONLY , S_IRWXA ) ;
4429+ assert ! ( fd >= 0 ) ;
4430+
4431+ // Attempt to seek within the directory and check if it succeeds
4432+ assert_eq ! ( cage. lseek_syscall( fd, 1 , SEEK_SET ) , 1 ) ;
4433+
4434+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
4435+ lindrustfinalize ( ) ;
4436+ }
4437+
4438+ #[ test]
4439+ pub fn ut_lind_fs_lseek_invalid_whence ( ) {
4440+ // acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4441+ // and also performs clean env setup
4442+ let _thelock = setup:: lock_and_init ( ) ;
4443+
4444+ let cage = interface:: cagetable_getref ( 1 ) ;
4445+
4446+ // Test to create a file and check for invalid `whence` value
4447+ let fd = cage. open_syscall ( "/test_file" , O_CREAT | O_RDWR , S_IRWXA ) ;
4448+ assert ! ( fd >= 0 ) ;
4449+
4450+ // Attempt to seek with an invalid `whence` value and check if it returns an
4451+ // error
4452+ assert_eq ! (
4453+ cage. lseek_syscall( fd, 10 , 999 ) , // Invalid whence value
4454+ -( Errno :: EINVAL as i32 )
4455+ ) ;
4456+
4457+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
4458+ lindrustfinalize ( ) ;
4459+ }
4460+
4461+ #[ test]
4462+ pub fn ut_lind_fs_lseek_beyond_file_size ( ) {
4463+ // acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4464+ // and also performs clean env setup
4465+ let _thelock = setup:: lock_and_init ( ) ;
4466+
4467+ let cage = interface:: cagetable_getref ( 1 ) ;
4468+
4469+ // Test to create a file and seek beyond its size
4470+ let fd = cage. open_syscall ( "/test_file" , O_CREAT | O_RDWR , S_IRWXA ) ;
4471+ assert ! ( fd >= 0 ) ;
4472+
4473+ // Write sample data to the file.
4474+ assert_eq ! ( cage. write_syscall( fd, str2cbuf( "hello" ) , 5 ) , 5 ) ;
4475+
4476+ // Seek beyond the end of the file and verify if it succeeds
4477+ assert_eq ! (
4478+ cage. lseek_syscall( fd, 10 , SEEK_END ) ,
4479+ 15 // 5 (file size) + 10 (offset)
4480+ ) ;
4481+
4482+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
4483+ lindrustfinalize ( ) ;
4484+ }
4485+
4486+ #[ test]
4487+ pub fn ut_lind_fs_lseek_before_start_of_file ( ) {
4488+ // acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4489+ // and also performs clean env setup
4490+ let _thelock = setup:: lock_and_init ( ) ;
4491+
4492+ let cage = interface:: cagetable_getref ( 1 ) ;
4493+
4494+ // Test to create a file and attempt to seek before the start of the file
4495+ let fd = cage. open_syscall ( "/test_file" , O_CREAT | O_RDWR , S_IRWXA ) ;
4496+ assert ! ( fd >= 0 ) ;
4497+
4498+ // Attempt to seek to a negative offset and check if it returns an error
4499+ // using "SEEK_SET" whence, where we are explicitly setting the file
4500+ // offset to -10 value.
4501+ assert_eq ! (
4502+ cage. lseek_syscall( fd, -10 , SEEK_SET ) ,
4503+ -( Errno :: EINVAL as i32 )
4504+ ) ;
4505+
4506+ // Attempt to seek to a negative offset and check if it returns an error
4507+ // using "SEEK_CUR" whence, where current position of the file is 0,
4508+ // as it's empty initially, and we are adding -10 to the offset.
4509+ assert_eq ! (
4510+ cage. lseek_syscall( fd, -10 , SEEK_CUR ) ,
4511+ -( Errno :: EINVAL as i32 )
4512+ ) ;
4513+
4514+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
4515+ lindrustfinalize ( ) ;
4516+ }
4517+
4518+ #[ test]
4519+ pub fn ut_lind_fs_lseek_on_pipe ( ) {
4520+ // acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4521+ // and also performs clean env setup
4522+ let _thelock = setup:: lock_and_init ( ) ;
4523+
4524+ let cage = interface:: cagetable_getref ( 1 ) ;
4525+
4526+ // Create a pipe and attempt to seek within it
4527+ let mut pipe_fds = PipeArray :: default ( ) ;
4528+ assert_eq ! ( cage. pipe_syscall( & mut pipe_fds) , 0 ) ;
4529+ let read_fd = pipe_fds. readfd ;
4530+
4531+ // Attempt to seek within the pipe and check if it returns an error
4532+ assert_eq ! (
4533+ cage. lseek_syscall( read_fd, 10 , SEEK_SET ) ,
4534+ -( Errno :: ESPIPE as i32 )
4535+ ) ;
4536+
4537+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
4538+ lindrustfinalize ( ) ;
4539+ }
4540+
4541+ #[ test]
4542+ pub fn ut_lind_fs_lseek_on_chardev ( ) {
4543+ // acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4544+ // and also performs clean env setup
4545+ let _thelock = setup:: lock_and_init ( ) ;
4546+
4547+ let cage = interface:: cagetable_getref ( 1 ) ;
4548+
4549+ // Attempt to seek within a character device file
4550+ let path = "/dev/null" ;
4551+ let fd = cage. open_syscall ( path, O_RDWR , S_IRWXA ) ;
4552+
4553+ // Seek within the character device and check if it returns 0 (no operation)
4554+ assert_eq ! ( cage. lseek_syscall( fd, 10 , SEEK_SET ) , 0 ) ;
4555+
4556+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
4557+ lindrustfinalize ( ) ;
4558+ }
4559+
4560+ #[ test]
4561+ pub fn ut_lind_fs_lseek_on_epoll ( ) {
4562+ // acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4563+ // and also performs clean env setup
4564+ let _thelock = setup:: lock_and_init ( ) ;
4565+
4566+ let cage = interface:: cagetable_getref ( 1 ) ;
4567+
4568+ // Create an Epoll and try to seek from it.
4569+ let epfd = cage. epoll_create_syscall ( 1 ) ;
4570+ assert ! ( epfd > 0 ) ;
4571+
4572+ // Attempt to seek from the epoll and check if it returns an error
4573+ assert_eq ! (
4574+ cage. lseek_syscall( epfd, 10 , SEEK_SET ) ,
4575+ -( Errno :: ESPIPE as i32 )
4576+ ) ;
4577+
4578+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
4579+ lindrustfinalize ( ) ;
4580+ }
4581+
4582+ #[ test]
4583+ pub fn ut_lind_fs_close_regular_file ( ) {
4584+ // acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4585+ // and also performs clean env setup
4586+ let _thelock = setup:: lock_and_init ( ) ;
4587+
4588+ let cage = interface:: cagetable_getref ( 1 ) ;
4589+
4590+ // Create and open a regular file, then close it.
4591+ let fd = cage. open_syscall ( "/test_file" , O_CREAT | O_RDWR , S_IRWXA ) ;
4592+ assert ! ( fd >= 0 ) ;
4593+
4594+ // Write sample data to the file.
4595+ assert_eq ! ( cage. write_syscall( fd, str2cbuf( "hello" ) , 5 ) , 5 ) ;
4596+
4597+ // Close the file descriptor, which should succeed.
4598+ assert_eq ! ( cage. close_syscall( fd) , 0 ) ;
4599+
4600+ // Attempt to close the file descriptor again to ensure it's already closed.
4601+ // Expect an error for "Invalid File Descriptor".
4602+ assert_eq ! ( cage. close_syscall( fd) , -( Errno :: EBADF as i32 ) ) ;
4603+
4604+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
4605+ lindrustfinalize ( ) ;
4606+ }
4607+
4608+ #[ test]
4609+ pub fn ut_lind_fs_close_directory ( ) {
4610+ // acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4611+ // and also performs clean env setup
4612+ let _thelock = setup:: lock_and_init ( ) ;
4613+
4614+ let cage = interface:: cagetable_getref ( 1 ) ;
4615+
4616+ // Create a directory and open it.
4617+ let path = "/test_dir" ;
4618+ assert_eq ! ( cage. mkdir_syscall( path, S_IRWXA ) , 0 ) ;
4619+ let fd = cage. open_syscall ( path, O_RDONLY , S_IRWXA ) ;
4620+ assert ! ( fd >= 0 ) ;
4621+
4622+ // Close the directory file descriptor, which should succeed.
4623+ assert_eq ! ( cage. close_syscall( fd) , 0 ) ;
4624+
4625+ // Attempt to close the file descriptor again to ensure it's already closed.
4626+ // Expect an error for "Invalid File Descriptor".
4627+ assert_eq ! ( cage. close_syscall( fd) , -( Errno :: EBADF as i32 ) ) ;
4628+
4629+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
4630+ lindrustfinalize ( ) ;
4631+ }
4632+
4633+ #[ test]
4634+ pub fn ut_lind_fs_close_socket ( ) {
4635+ // acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4636+ // and also performs clean env setup
4637+ let _thelock = setup:: lock_and_init ( ) ;
4638+
4639+ let cage = interface:: cagetable_getref ( 1 ) ;
4640+
4641+ // Create a socket pair.
4642+ let mut socketpair = interface:: SockPair :: default ( ) ;
4643+ assert_eq ! (
4644+ Cage :: socketpair_syscall( cage. clone( ) , AF_UNIX , SOCK_STREAM , 0 , & mut socketpair) ,
4645+ 0
4646+ ) ;
4647+
4648+ // Close both the socket file descriptors, which should succeed.
4649+ assert_eq ! ( cage. close_syscall( socketpair. sock1) , 0 ) ;
4650+ assert_eq ! ( cage. close_syscall( socketpair. sock2) , 0 ) ;
4651+
4652+ // Attempt to close the file descriptors again to ensure they are already
4653+ // closed. Expect an error for "Invalid File Descriptor".
4654+ assert_eq ! ( cage. close_syscall( socketpair. sock1) , -( Errno :: EBADF as i32 ) ) ;
4655+ assert_eq ! ( cage. close_syscall( socketpair. sock2) , -( Errno :: EBADF as i32 ) ) ;
4656+
4657+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
4658+ lindrustfinalize ( ) ;
4659+ }
4660+
4661+ #[ test]
4662+ pub fn ut_lind_fs_close_pipe ( ) {
4663+ // acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4664+ // and also performs clean env setup
4665+ let _thelock = setup:: lock_and_init ( ) ;
4666+
4667+ let cage = interface:: cagetable_getref ( 1 ) ;
4668+
4669+ // Create a pipe.
4670+ let mut pipe_fds = PipeArray :: default ( ) ;
4671+ assert_eq ! ( cage. pipe_syscall( & mut pipe_fds) , 0 ) ;
4672+ let read_fd = pipe_fds. readfd ;
4673+ let write_fd = pipe_fds. writefd ;
4674+
4675+ // Write data to the pipe
4676+ let write_data = "Testing" ;
4677+ assert_eq ! (
4678+ cage. write_syscall( write_fd, write_data. as_ptr( ) , write_data. len( ) ) ,
4679+ write_data. len( ) as i32
4680+ ) ;
4681+
4682+ // Read the data from the pipe.
4683+ let mut buf = sizecbuf ( 7 ) ;
4684+ assert_eq ! (
4685+ cage. read_syscall( read_fd, buf. as_mut_ptr( ) , buf. len( ) ) ,
4686+ write_data. len( ) as i32
4687+ ) ;
4688+ assert_eq ! ( cbuf2str( & buf) , write_data) ;
4689+
4690+ // Close the pipe file descriptors, which should succeed.
4691+ assert_eq ! ( cage. close_syscall( read_fd) , 0 ) ;
4692+ assert_eq ! ( cage. close_syscall( write_fd) , 0 ) ;
4693+
4694+ // Attempt to close the file descriptor again to ensure they are already closed.
4695+ // Expect an error for "Invalid File Descriptor".
4696+ assert_eq ! ( cage. close_syscall( read_fd) , -( Errno :: EBADF as i32 ) ) ;
4697+ assert_eq ! ( cage. close_syscall( write_fd) , -( Errno :: EBADF as i32 ) ) ;
4698+
4699+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
4700+ lindrustfinalize ( ) ;
4701+ }
4702+
4703+ #[ test]
4704+ pub fn ut_lind_fs_close_chardev ( ) {
4705+ // acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
4706+ // and also performs clean env setup
4707+ let _thelock = setup:: lock_and_init ( ) ;
4708+
4709+ let cage = interface:: cagetable_getref ( 1 ) ;
4710+
4711+ // Open a character device file.
4712+ let fd = cage. open_syscall ( "/dev/zero" , O_RDWR , S_IRWXA ) ;
4713+ assert ! ( fd >= 0 ) ;
4714+
4715+ // Close the character device file descriptor, which should succeed.
4716+ assert_eq ! ( cage. close_syscall( fd) , 0 ) ;
4717+
4718+ // Attempt to close the file descriptor again to ensure it's already closed.
4719+ // Expect an error for "Invalid File Descriptor".
4720+ assert_eq ! ( cage. close_syscall( fd) , -( Errno :: EBADF as i32 ) ) ;
4721+
4722+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
4723+ lindrustfinalize ( ) ;
4724+ }
43974725}
0 commit comments