@@ -381,65 +381,44 @@ def test_comment_at_end_of_machine_line_pass_has_hash(self, make_nrc):
381381 machine bar.domain.com login foo password pass
382382 """ , '#pass' )
383383
384- @unittest .skipUnless (os .name == 'posix' , 'POSIX only test' )
385- @unittest .skipIf (pwd is None , 'security check requires pwd module' )
386- @support .os_helper .skip_unless_working_chmod
387- def test_non_anonymous_security (self ):
388- # This test is incomplete since we are normally not run as root and
389- # therefore can't test the file ownership being wrong.
390- content = """
391- machine foo.domain.com login bar password pass
392- default login foo password pass
393- """
394- mode = 0o662
395-
396- # Use ~/.netrc and login is not anon
397- with self .assertRaises (netrc .NetrcParseError ):
398- NetrcBuilder .use_default_netrc_in_home (content , mode = mode )
399-
400- # Don't use default file
401- nrc = NetrcBuilder .use_file_argument (content , mode = mode )
402- self .assertEqual (nrc .hosts ['foo.domain.com' ],
403- ('bar' , '' , 'pass' ))
404384
405385 @unittest .skipUnless (os .name == 'posix' , 'POSIX only test' )
406386 @unittest .skipIf (pwd is None , 'security check requires pwd module' )
407387 @support .os_helper .skip_unless_working_chmod
408- @support .subTests ('make_nrc' , ALL_NETRC_FILE_SCENARIOS )
409- def test_anonymous_security (self , make_nrc ):
388+ def test_security (self ):
410389 # This test is incomplete since we are normally not run as root and
411390 # therefore can't test the file ownership being wrong.
412- content = """ \
413- machine foo.domain.com login anonymous password pass
414- """
415- mode = 0o662
416-
417- # When it's anonymous, file permissions are not bypassed
418- nrc = make_nrc ( content , mode = mode )
419- self . assertEqual ( nrc . hosts [ ' foo.domain.com' ],
420- ( 'anonymous' , '' , 'pass' ) )
421-
422- @ unittest . skipUnless ( os . name == 'posix' , 'POSIX only test' )
423- @ unittest . skipIf ( pwd is None , 'security check requires pwd module' )
424- @ support . os_helper . skip_unless_working_chmod
425- def test_anonymous_security_with_default ( self ):
426- # This test is incomplete since we are normally not run as root and
427- # therefore can't test the file ownership being wrong.
428- content = """ \
429- machine foo.domain.com login anonymous password pass
430- default login foo password pass
431- """
432- mode = 0o622
433-
434- # "foo" is not anonymous, therefore the security check is triggered when we fallback to default netrc
435- with self . assertRaises ( netrc . NetrcParseError ):
436- NetrcBuilder . use_default_netrc_in_home ( content , mode = mode )
437-
438- # Security check isn't triggered if the file is passed as environment variable or argument
439- for make_nrc in ( NetrcBuilder . use_file_argument , NetrcBuilder . use_netrc_envvar ):
440- nrc = make_nrc ( content , mode = mode )
391+ d = support . os_helper . TESTFN
392+ os . mkdir ( d )
393+ self . addCleanup ( support . os_helper . rmtree , d )
394+ fn = os . path . join ( d , '.netrc' )
395+ with open ( fn , 'wt' ) as f :
396+ f . write ( """ \
397+ machine foo.domain.com login bar password pass
398+ default login foo password pass
399+ """ )
400+ with support . os_helper . EnvironmentVarGuard () as environ :
401+ environ . set ( 'HOME' , d )
402+ os . chmod ( fn , 0o600 )
403+ nrc = netrc . netrc ()
404+ self . assertEqual ( nrc . hosts [ 'foo.domain.com' ],
405+ ( 'bar' , '' , 'pass' ))
406+ os . chmod ( fn , 0o622 )
407+ self . assertRaises ( netrc . NetrcParseError , netrc . netrc )
408+ with open ( fn , 'wt' ) as f :
409+ f . write ( """ \
410+ machine foo.domain.com login anonymous password pass
411+ default login foo password pass
412+ """ )
413+ with support . os_helper . EnvironmentVarGuard () as environ :
414+ environ . set ( 'HOME' , d )
415+ os . chmod ( fn , 0o600 )
416+ nrc = netrc . netrc ()
417+ self . assertEqual ( nrc . hosts [ 'foo.domain.com' ],
418+ ( 'anonymous' , '' , 'pass' ))
419+ os . chmod ( fn , 0o622 )
441420 self .assertEqual (nrc .hosts ['foo.domain.com' ],
442- ('anonymous' , '' , 'pass' ))
421+ ('anonymous' , '' , 'pass' ))
443422
444423
445424if __name__ == "__main__" :
0 commit comments