def test_detect_file(self): """Test detect_file function""" tests = [('%windir%\\system32\\kernel32.dll', True), ('%windir%\\system32', True), ('%ProgramFiles%\\Internet Explorer', True), ('%ProgramFiles%\\Internet Explorer\\', True), ('%windir%\\doesnotexist', False), ('%windir%\\system*', True), ('%windir%\\*ystem32', True), ('%windir%\\*ystem3*', True)] # On 64-bit Windows, Winapp2.ini expands the %ProgramFiles% environment # variable to also %ProgramW6432%, so test unique entries in # %ProgramW6432%. import struct if not 32 == 8 * struct.calcsize('P'): raise NotImplementedError('expecting 32-bit Python') if os.getenv('ProgramW6432'): dir_64 = os.listdir(os.getenv('ProgramFiles')) dir_32 = os.listdir(os.getenv('ProgramW6432')) dir_32_unique = set(dir_32) - set(dir_64) if dir_32 and not dir_32_unique: raise RuntimeError( 'Test expects objects in %ProgramW6432% not in %ProgramFiles%') for pathname in dir_32_unique: tests.append(('%%ProgramFiles%%\\%s' % pathname, True)) else: logger.info('skipping %ProgramW6432% tests because WoW64 not detected') for (pathname, expected_return) in tests: actual_return = detect_file(pathname) msg = 'detect_file(%s) returned %s' % (pathname, actual_return) self.assertEqual(expected_return, actual_return, msg)
def test_detect_file(self): """Test detect_file function""" tests = [('%windir%\\system32\\kernel32.dll', True), ('%windir%\\system32', True), ('%ProgramFiles%\\Internet Explorer', True), ('%ProgramFiles%\\Internet Explorer\\', True), ('%windir%\\doesnotexist', False), ('%windir%\\system*', True), ('%windir%\\*ystem32', True), ('%windir%\\*ystem3*', True)] # On 64-bit Windows, Winapp2.ini expands the %ProgramFiles% environment # variable to also %ProgramW6432%, so test unique entries in # %ProgramW6432%. import struct if 8 * struct.calcsize('P') != 32: raise NotImplementedError('expecting 32-bit Python') if os.getenv('ProgramW6432'): dir_64 = os.listdir(os.getenv('ProgramFiles')) dir_32 = os.listdir(os.getenv('ProgramW6432')) dir_32_unique = set(dir_32) - set(dir_64) if dir_32 and not dir_32_unique: raise RuntimeError( 'Test expects objects in %ProgramW6432% not in %ProgramFiles%' ) for pathname in dir_32_unique: tests.append(('%%ProgramFiles%%\\%s' % pathname, True)) else: logger.info( 'skipping %ProgramW6432% tests because WoW64 not detected') for (pathname, expected_return) in tests: actual_return = detect_file(pathname) msg = 'detect_file(%s) returned %s' % (pathname, actual_return) self.assertEqual(expected_return, actual_return, msg)
def test_update_winapp2(self): from bleachbit import personal_cleaners_dir fn = os.path.join(personal_cleaners_dir, 'winapp2.ini') if os.path.exists(fn): logger.info('deleting %s', fn.encode(bleachbit.FSE)) os.unlink(fn) url = 'http://katana.oooninja.com/bleachbit/winapp2/winapp2-2016-03-14.ini' def expect_failure(): raise AssertionError('Call should have failed') def on_success(): succeeded['r'] = True succeeded = {'r': False} # scope # bad hash self.assertRaises(RuntimeError, update_winapp2, url, "notahash", print, expect_failure) # blank hash, download file update_winapp2(url, None, print, on_success) self.assertTrue(succeeded['r']) # blank hash, do not download again update_winapp2(url, None, print, on_success) update_winapp2(url, None, print, expect_failure)
def test_delete_locked_file(self): """Unit test for delete_locked_file""" tests = ('regular', 'unicode-emdash-u\u2014', 'long' + 'x' * 100) for test in tests: f = tempfile.NamedTemporaryFile( prefix='bleachbit-delete-locked-file', suffix=test, delete=False) pathname = f.name f.close() import time time.sleep(5) # avoid race condition self.assertExists(pathname) logger.debug('delete_locked_file(%s) ' % pathname) if not shell.IsUserAnAdmin(): with self.assertRaises(WindowsError): delete_locked_file(pathname) else: try: delete_locked_file(pathname) except WindowsError: logger.exception( 'delete_locked_file() threw an error, which may be a false positive' ) self.assertExists(pathname) logger.info('reboot Windows and check the three files are deleted')
def test_whitelisted_speed(self): """Benchmark the speed of whitelisted() It is called frequently, so the speed is important.""" d = '/usr/bin' whitelist = [('file', '/home/foo'), ('folder', '/home/folder')] if 'nt' == os.name: d = expandvars('%windir%\system32') whitelist = [('file', r'c:\\filename'), ('folder', r'c:\\folder')] reps = 20 paths = [p for p in children_in_directory(d, True)] paths = paths[:1000] # truncate self.assertGreater(len(paths), 10) old_whitelist = options.get_whitelist_paths() options.set_whitelist_paths(whitelist) t0 = time.time() for i in xrange(0, reps): for p in paths: _ = whitelisted(p) t1 = time.time() logger.info('whitelisted() with {} repetitions and {} paths took {:.3g} seconds '.format( reps, len(paths), t1 - t0)) options.set_whitelist_paths(old_whitelist)
def test_delete_locked_file(self): """Unit test for delete_locked_file""" tests = ('regular', u'unicode-emdash-u\u2014', 'long' + 'x' * 100) for test in tests: (fd, pathname) = tempfile.mkstemp( prefix='bleachbit-delete-locked-file', suffix=test) os.close(fd) self.assert_(os.path.exists(pathname)) try: delete_locked_file(pathname) except pywintypes.error as e: if 5 == e.winerror and not shell.IsUserAnAdmin(): pass else: raise self.assert_(os.path.exists(pathname)) logger.info('reboot Windows and check the three files are deleted')
def get_winapp2(): """Download and cache winapp2.ini. Return local filename.""" url = "https://rawgit.com/bleachbit/winapp2.ini/master/Winapp2-combined.ini" tmpdir = None if 'posix' == os.name: tmpdir = '/tmp' if 'nt' == os.name: tmpdir = os.getenv('TMP') fn = os.path.join(tmpdir, 'bleachbit_test_winapp2.ini') if os.path.exists(fn): import time import stat age_seconds = time.time() - os.stat(fn)[stat.ST_MTIME] if age_seconds > (24 * 36 * 36): logger.info('deleting stale file %s ', fn) os.remove(fn) if not os.path.exists(fn): f = open(fn, 'w') import urllib2 txt = urllib2.urlopen(url).read() f.write(txt) return fn
def get_winapp2(): """Download and cache winapp2.ini. Return local filename.""" url = "https://rawgit.com/bleachbit/winapp2.ini/master/Winapp2-BleachBit.ini" tmpdir = None if os.name == 'posix': tmpdir = '/tmp' if os.name == 'nt': tmpdir = os.getenv('TMP') fname = os.path.join(tmpdir, 'bleachbit_test_winapp2.ini') if os.path.exists(fname): import time import stat age_seconds = time.time() - os.stat(fname)[stat.ST_MTIME] if age_seconds > (24 * 36 * 36): logger.info('deleting stale file %s ', fname) os.remove(fname) if not os.path.exists(fname): with open(fname, 'w', encoding='utf-8') as fobj: import urllib.request txt = urllib.request.urlopen(url).read() fobj.write(txt.decode('utf-8')) return fname
def get_winapp2(): """Download and cache winapp2.ini. Return local filename.""" url = "https://rawgit.com/bleachbit/winapp2.ini/master/Winapp2-BleachBit.ini" tmpdir = None if 'posix' == os.name: tmpdir = '/tmp' if 'nt' == os.name: tmpdir = os.getenv('TMP') fn = os.path.join(tmpdir, 'bleachbit_test_winapp2.ini') if os.path.exists(fn): import time import stat age_seconds = time.time() - os.stat(fn)[stat.ST_MTIME] if age_seconds > (24 * 36 * 36): logger.info('deleting stale file %s ', fn) os.remove(fn) if not os.path.exists(fn): f = open(fn, 'w') import urllib2 txt = urllib2.urlopen(url).read() f.write(txt) return fn
def test_delete_locked_file(self): """Unit test for delete_locked_file""" tests = ('regular', u'unicode-emdash-u\u2014', 'long' + 'x' * 100) for test in tests: f = tempfile.NamedTemporaryFile( prefix='bleachbit-delete-locked-file', suffix=test, delete=False) pathname = f.name f.close() import time time.sleep(5) # avoid race condition self.assertExists(pathname) logger.debug('delete_locked_file(%s) ' % pathname) if not shell.IsUserAnAdmin(): with self.assertRaises(WindowsError): delete_locked_file(pathname) else: try: delete_locked_file(pathname) except WindowsError: logger.exception('delete_locked_file() threw an error, which may be a false positive') self.assertExists(pathname) logger.info('reboot Windows and check the three files are deleted')