@@ -342,6 +342,273 @@ pub mod fs_tests {
342342 lindrustfinalize ( ) ;
343343 }
344344
345+ #[ test]
346+ pub fn ut_lind_fs_mmap_zerolen ( ) {
347+ //acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
348+ // and also performs clean env setup
349+ let _thelock = setup:: lock_and_init ( ) ;
350+
351+ let cage = interface:: cagetable_getref ( 1 ) ;
352+
353+ //Creating a regular file with `O_RDWR` flag
354+ //making it valid for any mapping.
355+ let flags: i32 = O_TRUNC | O_CREAT | O_RDWR ;
356+ let filepath = "/mmapTestFile1" ;
357+ let fd = cage. open_syscall ( filepath, flags, S_IRWXA ) ;
358+ //Writing into that file's first 9 bytes.
359+ assert_eq ! ( cage. write_syscall( fd, str2cbuf( "Test text" ) , 9 ) , 9 ) ;
360+
361+ //Checking if passing 0 as `len` to `mmap_syscall()`
362+ //correctly results in 'The value of len is 0` error.
363+ assert_eq ! ( cage. mmap_syscall( 0 as * mut u8 , 0 , PROT_READ | PROT_WRITE , MAP_SHARED , fd, 0 ) , -( Errno :: EINVAL as i32 ) ) ;
364+
365+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
366+ lindrustfinalize ( ) ;
367+ }
368+
369+ #[ test]
370+ pub fn ut_lind_fs_mmap_invalid_flags_none ( ) {
371+ //acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
372+ // and also performs clean env setup
373+ let _thelock = setup:: lock_and_init ( ) ;
374+
375+ let cage = interface:: cagetable_getref ( 1 ) ;
376+
377+ //Creating a regular file with `O_RDWR` flag
378+ //making it valid for any mapping.
379+ let flags: i32 = O_TRUNC | O_CREAT | O_RDWR ;
380+ let filepath = "/mmapTestFile1" ;
381+ let fd = cage. open_syscall ( filepath, flags, S_IRWXA ) ;
382+ //Writing into that file's first 9 bytes.
383+ assert_eq ! ( cage. write_syscall( fd, str2cbuf( "Test text" ) , 9 ) , 9 ) ;
384+
385+ //Checking if not passing any of the two `MAP_PRIVATE`
386+ //or `MAP_SHARED` flags correctly results in `The value
387+ //of flags is invalid (neither `MAP_PRIVATE` nor
388+ //`MAP_SHARED` is set)` error.
389+ assert_eq ! ( cage. mmap_syscall( 0 as * mut u8 , 5 , PROT_READ | PROT_WRITE , 0 , fd, 0 ) , -( Errno :: EINVAL as i32 ) ) ;
390+
391+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
392+ lindrustfinalize ( ) ;
393+ }
394+
395+ #[ test]
396+ pub fn ut_lind_fs_mmap_invalid_flags_both ( ) {
397+ //acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
398+ // and also performs clean env setup
399+ let _thelock = setup:: lock_and_init ( ) ;
400+
401+ let cage = interface:: cagetable_getref ( 1 ) ;
402+
403+ //Creating a regular file with `O_RDWR` flag
404+ //making it valid for any mapping.
405+ let flags: i32 = O_TRUNC | O_CREAT | O_RDWR ;
406+ let filepath = "/mmapTestFile1" ;
407+ let fd = cage. open_syscall ( filepath, flags, S_IRWXA ) ;
408+ //Writing into that file's first 9 bytes.
409+ assert_eq ! ( cage. write_syscall( fd, str2cbuf( "Test text" ) , 9 ) , 9 ) ;
410+
411+ //Checking if passing both `MAP_PRIVATE`
412+ //and `MAP_SHARED` flags correctly results in `The value
413+ //of flags is invalid (`MAP_PRIVATE` and `MAP_SHARED`
414+ //cannot be both set)` error.
415+ assert_eq ! ( cage. mmap_syscall( 0 as * mut u8 , 5 , PROT_READ | PROT_WRITE , MAP_PRIVATE | MAP_SHARED , fd, 0 ) , -( Errno :: EINVAL as i32 ) ) ;
416+
417+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
418+ lindrustfinalize ( ) ;
419+ }
420+
421+ #[ test]
422+ pub fn ut_lind_fs_mmap_no_read ( ) {
423+ //acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
424+ // and also performs clean env setup
425+ let _thelock = setup:: lock_and_init ( ) ;
426+
427+ let cage = interface:: cagetable_getref ( 1 ) ;
428+
429+ //Creating a regular file without a reading flag
430+ //making it invalid for any mapping.
431+ let flags: i32 = O_TRUNC | O_CREAT | O_WRONLY ;
432+ let filepath = "/mmapTestFile1" ;
433+ let fd = cage. open_syscall ( filepath, flags, S_IRWXA ) ;
434+ //Writing into that file's first 9 bytes.
435+ assert_eq ! ( cage. write_syscall( fd, str2cbuf( "Test text" ) , 9 ) , 9 ) ;
436+
437+ //Checking if trying to map a file that does not
438+ //allow reading correctly results in `File descriptor
439+ //is not open for reading` error.
440+ assert_eq ! ( cage. mmap_syscall( 0 as * mut u8 , 5 , PROT_READ | PROT_WRITE , MAP_SHARED , fd, 0 ) , -( Errno :: EACCES as i32 ) ) ;
441+
442+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
443+ lindrustfinalize ( ) ;
444+ }
445+
446+ #[ test]
447+ pub fn ut_lind_fs_mmap_no_write ( ) {
448+ //acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
449+ // and also performs clean env setup
450+ let _thelock = setup:: lock_and_init ( ) ;
451+
452+ let cage = interface:: cagetable_getref ( 1 ) ;
453+
454+ //Creating a regular file with flags for
455+ //reading and writing
456+ let flags: i32 = O_TRUNC | O_CREAT | O_RDWR ;
457+ let filepath = "/mmapTestFile1" ;
458+ let fd = cage. open_syscall ( filepath, flags, S_IRWXA ) ;
459+ //Writing into that file's first 9 bytes.
460+ assert_eq ! ( cage. write_syscall( fd, str2cbuf( "Test text" ) , 9 ) , 9 ) ;
461+
462+ //Opening a file descriptor for the same file
463+ //but now with a read flag and without a write
464+ //flag making it invalid for shared mapping with
465+ //write protection flag.
466+ let testflags: i32 = O_RDONLY ;
467+ let testfd = cage. open_syscall ( filepath, testflags, 0 ) ;
468+
469+ //Checking if trying to map a file that does not
470+ //allow writing for shared mapping with writing
471+ //protection flag set correctly results in
472+ //``MAP_SHARED` was requested and PROT_WRITE is
473+ //set, but fd is not open in read/write (`O_RDWR`)
474+ //mode` error.
475+ assert_eq ! ( cage. mmap_syscall( 0 as * mut u8 , 5 , PROT_READ | PROT_WRITE , MAP_SHARED , testfd, 0 ) , -( Errno :: EACCES as i32 ) ) ;
476+
477+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
478+ lindrustfinalize ( ) ;
479+ }
480+
481+ #[ test]
482+ pub fn ut_lind_fs_mmap_invalid_offset_len ( ) {
483+ //acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
484+ // and also performs clean env setup
485+ let _thelock = setup:: lock_and_init ( ) ;
486+
487+ let cage = interface:: cagetable_getref ( 1 ) ;
488+
489+ //Creating a regular file with `O_RDWR` flag
490+ //making it valid for any mapping.
491+ let flags: i32 = O_TRUNC | O_CREAT | O_RDWR ;
492+ let filepath = "/mmapTestFile1" ;
493+ let fd = cage. open_syscall ( filepath, flags, S_IRWXA ) ;
494+ //Writing into that file's first 9 bytes.
495+ assert_eq ! ( cage. write_syscall( fd, str2cbuf( "Test text" ) , 9 ) , 9 ) ;
496+
497+ //Checking if passing a negative offset correctly
498+ //results in `Addresses in the range [off,off+len)
499+ //are invalid for the object specified by `fildes`` error.
500+ assert_eq ! ( cage. mmap_syscall( 0 as * mut u8 , 5 , PROT_READ | PROT_WRITE , MAP_SHARED , fd, -10 ) , -( Errno :: ENXIO as i32 ) ) ;
501+
502+ //Checking if passing an offset that seeks beyond the end
503+ //of the file correctly results in `Addresses in the
504+ //range [off,off+len) are invalid for the object specified
505+ //by `fildes`` error.
506+ assert_eq ! ( cage. mmap_syscall( 0 as * mut u8 , 5 , PROT_READ | PROT_WRITE , MAP_SHARED , fd, 25 ) , -( Errno :: ENXIO as i32 ) ) ;
507+
508+ //Checking if passing an offset and length that together
509+ //seek beyond the end of the file correctly results in
510+ //`Addresses in the range [off,off+len) are invalid for
511+ //the object specified by `fildes`` error.
512+ assert_eq ! ( cage. mmap_syscall( 0 as * mut u8 , 7 , PROT_READ | PROT_WRITE , MAP_SHARED , fd, 7 ) , -( Errno :: ENXIO as i32 ) ) ;
513+
514+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
515+ lindrustfinalize ( ) ;
516+ }
517+
518+ #[ test]
519+ pub fn ut_lind_fs_mmap_chardev ( ) {
520+ //acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
521+ // and also performs clean env setup
522+ let _thelock = setup:: lock_and_init ( ) ;
523+
524+ let cage = interface:: cagetable_getref ( 1 ) ;
525+
526+ //Opening a character device file `/dev/zero`.
527+ let fd = cage. open_syscall ( "/dev/zero" , O_RDWR , S_IRWXA ) ;
528+ //Writing into that file's first 9 bytes.
529+ assert_eq ! ( cage. write_syscall( fd, str2cbuf( "Test text" ) , 9 ) , 9 ) ;
530+
531+ //Checking if calling `mmap_syscall()` on the character device
532+ //file correctly results in `Lind currently does not support
533+ //mapping character files` error.
534+ assert_eq ! ( cage. mmap_syscall( 0 as * mut u8 , 5 , PROT_READ | PROT_WRITE , MAP_SHARED , fd, 0 ) , -( Errno :: EOPNOTSUPP as i32 ) ) ;
535+
536+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
537+ lindrustfinalize ( ) ;
538+ }
539+
540+ #[ test]
541+ pub fn ut_lind_fs_mmap_unsupported_file ( ) {
542+ //acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
543+ // and also performs clean env setup
544+ let _thelock = setup:: lock_and_init ( ) ;
545+
546+ let cage = interface:: cagetable_getref ( 1 ) ;
547+
548+ //Creating a directory.
549+ assert_eq ! ( cage. mkdir_syscall( "/testdir" , S_IRWXA ) , 0 ) ;
550+ let fd = cage. open_syscall ( "/testdir" , O_RDWR , S_IRWXA ) ;
551+
552+ //Checking if passing the created directory to
553+ //`mmap_syscall()` correctly results in `The `fildes`
554+ //argument refers to a file whose type is not
555+ //supported by mmap` error.
556+ assert_eq ! ( cage. mmap_syscall( 0 as * mut u8 , 5 , PROT_READ | PROT_WRITE , MAP_PRIVATE , fd, 0 ) , -( Errno :: EACCES as i32 ) ) ;
557+
558+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
559+ lindrustfinalize ( ) ;
560+ }
561+
562+ #[ test]
563+ pub fn ut_lind_fs_mmap_invalid_fildes ( ) {
564+ //acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
565+ // and also performs clean env setup
566+ let _thelock = setup:: lock_and_init ( ) ;
567+
568+ let cage = interface:: cagetable_getref ( 1 ) ;
569+
570+ //Creating a regular file with `O_RDWR` flag
571+ //making it valid for any mapping and then
572+ //closing it, thereby making the obtained
573+ //filede scriptor invalid because no other
574+ //file is opened after it.
575+ let flags: i32 = O_TRUNC | O_CREAT | O_RDWR ;
576+ let filepath = "/mmapTestFile1" ;
577+ let fd = cage. open_syscall ( filepath, flags, S_IRWXA ) ;
578+ assert_eq ! ( cage. close_syscall( fd) , 0 ) ;
579+
580+ //Checking if passing the invalid file descriptor
581+ //correctly results in `Invalid file descriptor` error.
582+ assert_eq ! ( cage. mmap_syscall( 0 as * mut u8 , 5 , PROT_READ | PROT_WRITE , MAP_SHARED , fd, 0 ) , -( Errno :: EBADF as i32 ) ) ;
583+
584+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
585+ lindrustfinalize ( ) ;
586+ }
587+
588+ #[ test]
589+ pub fn ut_lind_fs_munmap_zerolen ( ) {
590+ //acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
591+ // and also performs clean env setup
592+ let _thelock = setup:: lock_and_init ( ) ;
593+
594+ let cage = interface:: cagetable_getref ( 1 ) ;
595+
596+ //Creating a regular file with `O_RDWR` flag
597+ //making it valid for any mapping.
598+ let flags: i32 = O_TRUNC | O_CREAT | O_RDWR ;
599+ let filepath = "/mmapTestFile1" ;
600+ let fd = cage. open_syscall ( filepath, flags, S_IRWXA ) ;
601+ //Writing into that file's first 9 bytes.
602+ assert_eq ! ( cage. write_syscall( fd, str2cbuf( "Test text" ) , 9 ) , 9 ) ;
603+
604+ //Checking if passing 0 as `len` to `munmap_syscall()`
605+ //correctly results in 'The value of len is 0` error.
606+ assert_eq ! ( cage. munmap_syscall( 0 as * mut u8 , 0 ) , -( Errno :: EINVAL as i32 ) ) ;
607+
608+ assert_eq ! ( cage. exit_syscall( EXIT_SUCCESS ) , EXIT_SUCCESS ) ;
609+ lindrustfinalize ( ) ;
610+ }
611+
345612 #[ test]
346613 pub fn ut_lind_fs_dir_chdir ( ) {
347614 //acquiring a lock on TESTMUTEX prevents other tests from running concurrently,
0 commit comments