def test_scan_worldwritable_directories_with_no_sticky_bit_set(self, mock_os): # Prepare data and mocks test_subject = WorldWritable(None) mock_os.walk.return_value = [ ('/world/writable/not/sticky', (), ()), ('/not/world/writable/not/sticky', (), ()), ('/world/writable/sticky', (), ()), ] test_subject.is_world_writable = MagicMock(side_effect=[True, False, True]) test_subject.is_sticky_bit_set = MagicMock(side_effect=[False, True]) # Run test scenario result = test_subject.scan_worldwritable_directories_with_no_sticky_bit_set() # Assertions self.assertEqual(result, ['/world/writable/not/sticky']) mock_os.walk.assert_called_once_with('/') test_subject.is_world_writable.assert_has_calls( [ call('/world/writable/not/sticky'), call('/not/world/writable/not/sticky'), call('/world/writable/sticky'), ] ) test_subject.is_sticky_bit_set.assert_has_calls( [ call('/world/writable/not/sticky'), call('/world/writable/sticky'), ] )
def test_scan_worldwritable_files_owned_by_root(self, mock_os): # Prepare data and mocks test_subject = WorldWritable(None) mock_os.walk.return_value = [ ('/dir1', ('/dir1/subdir1',), ('world_writable_owned_by_root', 'not_world_writable_owned_by_root')), ('/dir2', (), ()), ('/dir3', ('/dir3/subdir3',), ('world_writable_not_owned_by_root',)), ] test_subject.is_world_writable = MagicMock(side_effect=[True, False, True]) test_subject.is_owned_by_root = MagicMock(side_effect=[True, False]) # Run test scenario result = test_subject.scan_worldwritable_files_owned_by_root() # Assertions self.assertEqual(result, ['world_writable_owned_by_root']) mock_os.walk.assert_called_once_with('/') test_subject.is_world_writable.assert_has_calls( [ call('world_writable_owned_by_root'), call('not_world_writable_owned_by_root'), call('world_writable_not_owned_by_root'), ] ) test_subject.is_owned_by_root.assert_has_calls( [ call('world_writable_owned_by_root'), call('world_writable_not_owned_by_root'), ] )
def test_scan_worldwritable_directories_with_no_sticky_bit_set( self, mock_os): # Prepare data and mocks test_subject = WorldWritable(None) mock_os.walk.return_value = [ ('/world/writable/not/sticky', (), ()), ('/not/world/writable/not/sticky', (), ()), ('/world/writable/sticky', (), ()), ] test_subject.is_world_writable = MagicMock( side_effect=[True, False, True]) test_subject.is_sticky_bit_set = MagicMock(side_effect=[False, True]) # Run test scenario result = test_subject.scan_worldwritable_directories_with_no_sticky_bit_set( ) # Assertions self.assertEqual(result, ['/world/writable/not/sticky']) mock_os.walk.assert_called_once_with('/') test_subject.is_world_writable.assert_has_calls([ call('/world/writable/not/sticky'), call('/not/world/writable/not/sticky'), call('/world/writable/sticky'), ]) test_subject.is_sticky_bit_set.assert_has_calls([ call('/world/writable/not/sticky'), call('/world/writable/sticky'), ])
def test_scan_worldwritable_files_owned_by_root(self, mock_os): # Prepare data and mocks test_subject = WorldWritable(None) mock_os.walk.return_value = [ ('/dir1', ('/dir1/subdir1', ), ('world_writable_owned_by_root', 'not_world_writable_owned_by_root')), ('/dir2', (), ()), ('/dir3', ('/dir3/subdir3', ), ('world_writable_not_owned_by_root', )), ] test_subject.is_world_writable = MagicMock( side_effect=[True, False, True]) test_subject.is_owned_by_root = MagicMock(side_effect=[True, False]) # Run test scenario result = test_subject.scan_worldwritable_files_owned_by_root() # Assertions self.assertEqual(result, ['world_writable_owned_by_root']) mock_os.walk.assert_called_once_with('/') test_subject.is_world_writable.assert_has_calls([ call('world_writable_owned_by_root'), call('not_world_writable_owned_by_root'), call('world_writable_not_owned_by_root'), ]) test_subject.is_owned_by_root.assert_has_calls([ call('world_writable_owned_by_root'), call('world_writable_not_owned_by_root'), ])
def test_is_world_writable_when_is_not(self, mock_os): # Prepare data and mocks test_subject = WorldWritable(None) mock_os.stat.return_value = os.stat_result((0o40005, 0, 0, 0, 0, 0, 0, 0, 0, 0)) path = '/does/not/actually/exist' # Run test scenario result = test_subject.is_world_writable(path) # Assertions self.assertFalse(result) mock_os.stat.assert_called_once_with(path)
def test_is_world_writable_when_file_does_not_exist(self, mock_os): # Prepare data and mocks test_subject = WorldWritable(None) mock_os.stat.side_effect = raise_file_not_found_error path = '/does/not/actually/exist' # Run test scenario result = test_subject.is_world_writable(path) # Assertions self.assertFalse(result) mock_os.stat.assert_called_once_with(path)
def test_is_world_writable_when_is_not(self, mock_os): # Prepare data and mocks test_subject = WorldWritable(None) mock_os.stat.return_value = os.stat_result( (0o40005, 0, 0, 0, 0, 0, 0, 0, 0, 0)) path = '/does/not/actually/exist' # Run test scenario result = test_subject.is_world_writable(path) # Assertions self.assertFalse(result) mock_os.stat.assert_called_once_with(path)