@@ -7,7 +7,7 @@ use crate::mem::MaybeUninit;
77use crate :: path:: Path ;
88use crate :: str;
99use crate :: sync:: Arc ;
10- use crate :: sys_common:: io:: test:: { tmpdir , TempDir } ;
10+ use crate :: sys_common:: io:: test:: { TempDir , tmpdir } ;
1111use crate :: thread;
1212use crate :: time:: { Duration , Instant , SystemTime } ;
1313
@@ -1091,12 +1091,7 @@ fn open_flavors() {
10911091 let mut ra = OO :: new ( ) ;
10921092 ra. read ( true ) . append ( true ) ;
10931093
1094- #[ cfg( windows) ]
1095- let invalid_options = 87 ; // ERROR_INVALID_PARAMETER
1096- #[ cfg( all( unix, not( target_os = "vxworks" ) ) ) ]
1097- let invalid_options = "Invalid argument" ;
1098- #[ cfg( target_os = "vxworks" ) ]
1099- let invalid_options = "invalid argument" ;
1094+ let invalid_options = "creating or truncating a file requires write or append access" ;
11001095
11011096 // Test various combinations of creation modes and access modes.
11021097 //
@@ -1793,3 +1788,34 @@ fn windows_unix_socket_exists() {
17931788 assert_eq ! ( socket_path. try_exists( ) . unwrap( ) , true ) ;
17941789 assert_eq ! ( socket_path. metadata( ) . is_ok( ) , true ) ;
17951790}
1791+
1792+ #[ test]
1793+ fn test_open_options_invalid_combinations ( ) {
1794+ // create without write access
1795+ let result = OpenOptions :: new ( ) . create ( true ) . read ( true ) . open ( "nonexistent.txt" ) ;
1796+ assert ! ( result. is_err( ) ) ;
1797+ let err = result. unwrap_err ( ) ;
1798+ assert_eq ! ( err. kind( ) , ErrorKind :: InvalidInput ) ;
1799+ assert_eq ! ( err. to_string( ) , "creating or truncating a file requires write or append access" ) ;
1800+
1801+ // create_new without write access
1802+ let result = OpenOptions :: new ( ) . create_new ( true ) . read ( true ) . open ( "nonexistent.txt" ) ;
1803+ assert ! ( result. is_err( ) ) ;
1804+ let err = result. unwrap_err ( ) ;
1805+ assert_eq ! ( err. kind( ) , ErrorKind :: InvalidInput ) ;
1806+ assert_eq ! ( err. to_string( ) , "creating or truncating a file requires write or append access" ) ;
1807+
1808+ // truncate without write access
1809+ let result = OpenOptions :: new ( ) . truncate ( true ) . read ( true ) . open ( "nonexistent.txt" ) ;
1810+ assert ! ( result. is_err( ) ) ;
1811+ let err = result. unwrap_err ( ) ;
1812+ assert_eq ! ( err. kind( ) , ErrorKind :: InvalidInput ) ;
1813+ assert_eq ! ( err. to_string( ) , "creating or truncating a file requires write or append access" ) ;
1814+
1815+ // no access mode at all
1816+ let result = OpenOptions :: new ( ) . open ( "nonexistent.txt" ) ;
1817+ assert ! ( result. is_err( ) ) ;
1818+ let err = result. unwrap_err ( ) ;
1819+ assert_eq ! ( err. kind( ) , ErrorKind :: InvalidInput ) ;
1820+ assert_eq ! ( err. to_string( ) , "must specify at least one of read, write, or append access" ) ;
1821+ }
0 commit comments