@@ -1782,6 +1782,135 @@ pub mod fs_tests {
17821782 assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
17831783 lindrustfinalize ( ) ;
17841784 }
1785+ #[ test]
1786+ fn ut_lind_fs_getdents_invalid_fd ( ) {
1787+ let _thelock = setup:: lock_and_init ( ) ;
1788+ let cage = interface:: cagetable_getref ( 1 ) ;
1789+
1790+ let bufsize = 50 ;
1791+ let mut vec = vec ! [ 0u8 ; bufsize as usize ] ;
1792+ let baseptr: * mut u8 = & mut vec[ 0 ] ;
1793+
1794+ // Create a directory
1795+ assert_eq ! ( cage. mkdir_syscall( "/getdents" , S_IRWXA ) , 0 ) ;
1796+
1797+ // Open the directory
1798+ let fd = cage. open_syscall ( "/getdents" , O_RDWR , S_IRWXA ) ;
1799+
1800+ // Attempt to call `getdents_syscall` with an invalid file descriptor
1801+ let result = cage. getdents_syscall ( -1 , baseptr, bufsize as u32 ) ;
1802+
1803+ // Assert that the return value is EBADF (errno for "Bad file descriptor")
1804+ assert_eq ! ( result, -( Errno :: EBADF as i32 ) ) ;
1805+
1806+ // Close the directory
1807+ assert_eq ! ( cage. close_syscall( fd) , 0 ) ;
1808+
1809+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
1810+ lindrustfinalize ( ) ;
1811+ }
1812+
1813+ #[ test]
1814+ fn ut_lind_fs_getdents_out_of_range_fd ( ) {
1815+ // Acquire a lock on TESTMUTEX to prevent other tests from running concurrently,
1816+ // and also perform clean environment setup.
1817+ let _thelock = setup:: lock_and_init ( ) ;
1818+
1819+ let cage = interface:: cagetable_getref ( 1 ) ;
1820+
1821+ // Allocate a buffer to store directory entries
1822+ let bufsize = 1024 ;
1823+ let mut vec = vec ! [ 0u8 ; bufsize as usize ] ;
1824+ let baseptr: * mut u8 = & mut vec[ 0 ] ;
1825+
1826+ // Attempt to call getdents_syscall with a file descriptor out of range
1827+ let result = cage. getdents_syscall ( MAXFD + 1 , baseptr, bufsize as u32 ) ;
1828+
1829+ // Verify that it returns EBADF (errno for "Bad file descriptor")
1830+ assert_eq ! ( result, -( Errno :: EBADF as i32 ) ) ;
1831+
1832+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
1833+ lindrustfinalize ( ) ;
1834+ }
1835+
1836+ #[ test]
1837+ fn ut_lind_fs_getdents_non_existing_fd ( ) {
1838+ // Acquire a lock on TESTMUTEX to prevent other tests from running concurrently,
1839+ // and also perform clean environment setup.
1840+ let _thelock = setup:: lock_and_init ( ) ;
1841+
1842+ let cage = interface:: cagetable_getref ( 1 ) ;
1843+
1844+ // Allocate a buffer to store directory entries
1845+ let bufsize = 1024 ;
1846+ let mut vec = vec ! [ 0u8 ; bufsize as usize ] ;
1847+ let baseptr: * mut u8 = & mut vec[ 0 ] ;
1848+
1849+ // Attempt to call getdents_syscall with a non-existing file descriptor
1850+ let result = cage. getdents_syscall ( 100 , baseptr, bufsize as u32 ) ;
1851+
1852+ // Verify that it returns EBADF (errno for "Bad file descriptor")
1853+ assert_eq ! ( result, -( Errno :: EBADF as i32 ) ) ;
1854+
1855+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
1856+ lindrustfinalize ( ) ;
1857+ }
1858+
1859+ #[ test]
1860+ fn ut_lind_fs_getdents_bufsize_too_small ( ) {
1861+ let _thelock = setup:: lock_and_init ( ) ;
1862+ let cage = interface:: cagetable_getref ( 1 ) ;
1863+
1864+ let bufsize = interface:: CLIPPED_DIRENT_SIZE - 1 ; // Buffer size smaller than CLIPPED_DIRENT_SIZE
1865+ let mut vec = vec ! [ 0u8 ; bufsize as usize ] ;
1866+ let baseptr: * mut u8 = & mut vec[ 0 ] ;
1867+
1868+ // Create a directory
1869+ assert_eq ! ( cage. mkdir_syscall( "/getdents" , S_IRWXA ) , 0 ) ;
1870+
1871+ // Open the directory
1872+ let fd = cage. open_syscall ( "/getdents" , O_RDWR , S_IRWXA ) ;
1873+
1874+ // Attempt to call `getdents_syscall` with a buffer size smaller than CLIPPED_DIRENT_SIZE
1875+ let result = cage. getdents_syscall ( fd, baseptr, bufsize as u32 ) ;
1876+
1877+ // Assert that the return value is EINVAL (errno for "Invalid argument")
1878+ assert_eq ! ( result, -( Errno :: EINVAL as i32 ) ) ;
1879+
1880+ // Close the directory
1881+ assert_eq ! ( cage. close_syscall( fd) , 0 ) ;
1882+
1883+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
1884+ lindrustfinalize ( ) ;
1885+ }
1886+
1887+ #[ test]
1888+ fn ut_lind_fs_getdents_non_directory_fd ( ) {
1889+ // Acquire a lock on TESTMUTEX to prevent other tests from running concurrently,
1890+ // and also perform clean environment setup.
1891+ let _thelock = setup:: lock_and_init ( ) ;
1892+
1893+ let cage = interface:: cagetable_getref ( 1 ) ;
1894+
1895+ // Create a regular file
1896+ let filepath = "/regularfile" ;
1897+ let fd = cage. open_syscall ( filepath, O_CREAT | O_WRONLY , S_IRWXA ) ;
1898+ assert ! ( fd >= 0 ) ;
1899+ // Allocate a buffer to store directory entries
1900+ let bufsize = 1024 ;
1901+ let mut vec = vec ! [ 0u8 ; bufsize as usize ] ;
1902+ let baseptr: * mut u8 = & mut vec[ 0 ] ;
1903+
1904+ // Attempt to call getdents_syscall on the regular file descriptor
1905+ let result = cage. getdents_syscall ( fd, baseptr, bufsize as u32 ) ;
1906+ // Verify that it returns ENOTDIR
1907+ assert_eq ! ( result, -( Errno :: ENOTDIR as i32 ) ) ;
1908+
1909+ // Clean up: Close the file descriptor and finalize the test environment
1910+ assert_eq ! ( cage. close_syscall( fd) , 0 ) ;
1911+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
1912+ lindrustfinalize ( ) ;
1913+ }
17851914
17861915 #[ test]
17871916 pub fn ut_lind_fs_dir_chdir_getcwd ( ) {
0 commit comments