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_wipe(contents, deny_access=False, is_sparse=False): shortname = _write_file(longname, contents) if deny_access or is_sparse: fh = open_file(extended_path(longname), mode=GENERIC_WRITE | WRITE_DAC) if is_sparse: file_make_sparse(fh) if deny_access: _deny_access(fh) close_file(fh) logger.debug( 'test_file_wipe(): filename length={}, shortname length ={}, contents length={}, is_sparse={}' .format(len(longname), len(shortname), len(contents), is_sparse)) if shell.IsUserAnAdmin(): # wiping requires admin privileges file_wipe(shortname) file_wipe(longname) else: with self.assertRaises(pywintypes.error): file_wipe(shortname) file_wipe(longname) self.assertExists(shortname) os.remove(extended_path(shortname)) self.assertNotExists(shortname)
def test_file_wipe(self): """Unit test for file_wipe There are more tests in testwipe.py """ from bleachbit.WindowsWipe import file_wipe dirname = tempfile.mkdtemp(prefix='bleachbit-file-wipe') filenames = ('short', 'long' + 'x' * 250, u'utf8-ɡælɪk') for filename in filenames: longname = os.path.join(dirname, filename) logger.debug('file_wipe(%s)', longname) def _write_file(longname, contents): self.write_file(longname, contents) return longname import win32api shortname = extended_path_undo( win32api.GetShortPathName(extended_path(longname))) self.assertExists(shortname) return shortname def _test_wipe(contents): shortname = _write_file(longname, contents) logger.debug( 'test_file_wipe(): filename length={}, shortname length ={}, contents length={}' .format(len(longname), len(shortname), len(contents))) if shell.IsUserAnAdmin(): # wiping requires admin privileges file_wipe(shortname) file_wipe(longname) else: with self.assertRaises(pywintypes.error): file_wipe(shortname) file_wipe(longname) self.assertExists(shortname) os.remove(extended_path(shortname)) self.assertNotExists(shortname) # A small file that fits in MFT _test_wipe('') # requires wiping of extents _test_wipe('secret' * 100000) import shutil shutil.rmtree(dirname, True) if shell.IsUserAnAdmin(): logger.warning( 'You should also run test_file_wipe() without admin privileges.' ) else: logger.warning( 'You should also run test_file_wipe() with admin privileges.')
def test_file_wipe(self): """Unit test for file_wipe There are more tests in testwipe.py """ from bleachbit.WindowsWipe import file_wipe dirname = tempfile.mkdtemp(prefix='bleachbit-file-wipe') filenames = ('short', 'long' + 'x' * 250, u'utf8-ɡælɪk') for filename in filenames: longname = os.path.join(dirname, filename) logger.debug('file_wipe(%s)', longname) def _write_file(longname, contents): self.write_file(longname, contents) return longname import win32api shortname = extended_path_undo( win32api.GetShortPathName(extended_path(longname))) self.assertExists(shortname) return shortname def _test_wipe(contents): shortname = _write_file(longname, contents) logger.debug('test_file_wipe(): filename length={}, shortname length ={}, contents length={}'.format( len(longname), len(shortname), len(contents))) if shell.IsUserAnAdmin(): # wiping requires admin privileges file_wipe(shortname) file_wipe(longname) else: with self.assertRaises(pywintypes.error): file_wipe(shortname) file_wipe(longname) self.assertExists(shortname) os.remove(extended_path(shortname)) self.assertNotExists(shortname) # A small file that fits in MFT _test_wipe('') # requires wiping of extents _test_wipe('secret' * 100000) import shutil shutil.rmtree(dirname, True) if shell.IsUserAnAdmin(): logger.warning('You should also run test_file_wipe() without admin privileges.') else: logger.warning('You should also run test_file_wipe() with admin privileges.')
def _test_wipe(contents): shortname = _write_file(longname, contents) logger.debug('test_file_wipe(): filename length={}, shortname length ={}, contents length={}'.format( len(longname), len(shortname), len(contents))) if shell.IsUserAnAdmin(): # wiping requires admin privileges file_wipe(shortname) file_wipe(longname) else: with self.assertRaises(pywintypes.error): file_wipe(shortname) file_wipe(longname) self.assertExists(shortname) os.remove(extended_path(shortname)) self.assertNotExists(shortname)
def test_getrealuid(self): """Test for getrealuid()""" if 'posix' != os.name: self.assertRaises(RuntimeError, getrealuid) return uid = getrealuid() self.assertIsInstance(uid, int) self.assertTrue(0 <= uid <= 65535) if sudo_mode(): self.assertGreater(uid, 0) logger.debug("os.getenv('LOGNAME') = %s", os.getenv('LOGNAME')) logger.debug("os.getenv('SUDO_UID') = %s", os.getenv('SUDO_UID')) logger.debug('os.geteuid() = %d', os.geteuid()) logger.debug('os.getuid() = %d', os.getuid()) try: logger.debug('os.login() = %s', os.getlogin()) except: logger.exception('os.login() raised exception')
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')
def test_file_wipe(self): """Unit test for file_wipe There are more tests in testwipe.py """ from bleachbit.WindowsWipe import file_wipe, open_file, close_file, file_make_sparse from bleachbit.Windows import elevate_privileges from win32con import GENERIC_WRITE, WRITE_DAC dirname = tempfile.mkdtemp(prefix='bleachbit-file-wipe') filenames = ('short', 'long' + 'x' * 250, 'utf8-ɡælɪk') for filename in filenames: longname = os.path.join(dirname, filename) logger.debug('file_wipe(%s)', longname) def _write_file(longname, contents): self.write_file(longname, contents) import win32api shortname = extended_path_undo( win32api.GetShortPathName(extended_path(longname))) self.assertExists(shortname) return shortname def _deny_access(fh): import win32security import ntsecuritycon as con user, _, _ = win32security.LookupAccountName("", win32api.GetUserName()) dacl = win32security.ACL() dacl.AddAccessDeniedAce(win32security.ACL_REVISION, con.FILE_GENERIC_READ | con.FILE_GENERIC_WRITE, user) win32security.SetSecurityInfo(fh, win32security.SE_FILE_OBJECT, win32security.DACL_SECURITY_INFORMATION, None, None, dacl, None) def _test_wipe(contents, deny_access = False, is_sparse=False): shortname = _write_file(longname, contents) if deny_access or is_sparse: fh = open_file(extended_path(longname), mode=GENERIC_WRITE | WRITE_DAC) if is_sparse: file_make_sparse(fh) if deny_access: _deny_access(fh) close_file(fh) logger.debug('test_file_wipe(): filename length={}, shortname length ={}, contents length={}, is_sparse={}'.format( len(longname), len(shortname), len(contents), is_sparse)) if shell.IsUserAnAdmin(): # wiping requires admin privileges file_wipe(shortname) file_wipe(longname) else: with self.assertRaises(pywintypes.error): file_wipe(shortname) file_wipe(longname) self.assertExists(shortname) os.remove(extended_path(shortname)) self.assertNotExists(shortname) # A small file that fits in MFT _test_wipe(b'') # requires wiping of extents _test_wipe(b'secret' * 100000) # requires wiping of extents: special file case elevate_privileges(False) _test_wipe(b'secret' * 100000, deny_access = True, is_sparse=True) shutil.rmtree(dirname, True) if shell.IsUserAnAdmin(): logger.warning( 'You should also run test_file_wipe() without admin privileges.') else: logger.warning( 'You should also run test_file_wipe() with admin privileges.')
def test_user_agent(self): """Unit test for method user_agent()""" agent = user_agent() logger.debug("user agent = '%s'", agent) self.assertIsString(agent)
def delete_helper(self, shred): """Called by test_delete() with shred = False and = True""" # test deleting with various kinds of filenames hebrew = u"עִבְרִית" katanana = u"アメリカ" umlauts = u"ÄäǞǟËëḦḧÏïḮḯÖöȪȫṎṏT̈ẗÜüǕǖǗǘǙǚǛǜṲṳṺṻẄẅẌẍŸÿ" tests = ['.prefixandsuffix', # simple "x".zfill(150), # long ' begins_with_space', "''", # quotation mark "~`!@#$%^&()-_+=x", # non-alphanumeric characters "[]{};'.,x", # non-alphanumeric characters u'abcdefgh', # simple Unicode u'J\xf8rgen Scandinavian', u'\u2014em-dash', # LP#1454030 hebrew, katanana, umlauts, 'sigil-should$not-change'] if 'posix' == os.name: # Windows doesn't allow these characters but Unix systems do tests += ['"*', '\t\\', ':?<>|', ' ', '.file.'] # Windows filenames cannot end with space or period for test in tests: # create the file filename = self.write_file(test, "top secret") # delete the file delete(filename, shred) self.assertNotExists(filename) # delete an empty directory dirname = self.mkdtemp(prefix=test) self.assertExists(dirname) delete(dirname, shred) self.assertNotExists(dirname) def symlink_helper(link_fn): if 'nt' == os.name: from win32com.shell import shell if not shell.IsUserAnAdmin(): self.skipTest('skipping symlink test because of insufficient privileges') # make regular file srcname = self.mkstemp(prefix='bleachbit-test-delete-regular') # make symlink self.assertExists(srcname) linkname = tempfile.mktemp('bblink') self.assertNotExists(linkname) link_fn(srcname, linkname) self.assertExists(linkname) self.assertLExists(linkname) # delete symlink delete(linkname, shred) self.assertExists(srcname) self.assertNotLExists(linkname) # delete regular file delete(srcname, shred) self.assertNotExists(srcname) # # test broken symlink # srcname = self.mkstemp(prefix='bleachbit-test-delete-sym') self.assertLExists(srcname) link_fn(srcname, linkname) self.assertLExists(linkname) self.assertExists(linkname) # delete regular file first delete(srcname, shred) self.assertNotExists(srcname) self.assertLExists(linkname) # clean up delete(linkname, shred) self.assertNotExists(linkname) self.assertNotLExists(linkname) windows_vista_or_newer = False if 'nt' == os.name: from bleachbit.Windows import parse_windows_build # Windows Vista = 6.0 windows_vista_or_newer = parse_windows_build() >= 6.0 if windows_vista_or_newer: logger.debug('testing symbolic link') import ctypes kern = ctypes.windll.LoadLibrary("kernel32.dll") def win_symlink(src, linkname): rc = kern.CreateSymbolicLinkA(linkname, src, 0) if rc == 0: print('CreateSymbolicLinkA(%s, %s)' % (linkname, src)) print('CreateSymolicLinkA() failed, error = %s' % ctypes.FormatError()) self.assertNotEqual(rc, 0) symlink_helper(win_symlink) # below this point, only posix if 'nt' == os.name: return # test file with mode 0444/-r--r--r-- filename = self.write_file('bleachbit-test-0444') os.chmod(filename, 0o444) delete(filename, shred) self.assertNotExists(filename) # test symlink symlink_helper(os.symlink) # test FIFO args = ["mkfifo", filename] ret = subprocess.call(args) self.assertEqual(ret, 0) self.assertExists(filename) delete(filename, shred) self.assertNotExists(filename) # test directory path = self.mkdtemp(prefix='bleachbit-test-delete-dir') self.assertExists(path) delete(path, shred) self.assertNotExists(path)
def delete_helper(self, shred): """Called by test_delete() with shred = False and = True""" # test deleting with various kinds of filenames hebrew = "עִבְרִית" katanana = "アメリカ" umlauts = "ÄäǞǟËëḦḧÏïḮḯÖöȪȫṎṏT̈ẗÜüǕǖǗǘǙǚǛǜṲṳṺṻẄẅẌẍŸÿ" tests = [ '.prefixandsuffix', # simple "x".zfill(150), # long ' begins_with_space', "''", # quotation mark "~`!@#$%^&()-_+=x", # non-alphanumeric characters "[]{};'.,x", # non-alphanumeric characters 'abcdefgh', # simple Unicode 'J\xf8rgen Scandinavian', '\u2014em-dash', # LP#1454030 hebrew, katanana, umlauts, 'sigil-should$not-change' ] if 'posix' == os.name: # Windows doesn't allow these characters but Unix systems do tests += ['"*', '\t\\', ':?<>|', ' ', '.file.' ] # Windows filenames cannot end with space or period for test in tests: # create the file filename = self.write_file(test, b"top secret") # delete the file delete(filename, shred) self.assertNotExists(filename) # delete an empty directory dirname = self.mkdtemp(prefix=test) self.assertExists(dirname) delete(dirname, shred) self.assertNotExists(dirname) def symlink_helper(link_fn): if 'nt' == os.name: from win32com.shell import shell if not shell.IsUserAnAdmin(): self.skipTest( 'skipping symlink test because of insufficient privileges' ) # make regular file srcname = self.mkstemp(prefix='bleachbit-test-delete-regular') # make symlink self.assertExists(srcname) linkname = tempfile.mktemp('bblink') self.assertNotExists(linkname) link_fn(srcname, linkname) self.assertExists(linkname) self.assertLExists(linkname) # delete symlink delete(linkname, shred) self.assertExists(srcname) self.assertNotLExists(linkname) # delete regular file delete(srcname, shred) self.assertNotExists(srcname) # # test broken symlink # srcname = self.mkstemp(prefix='bleachbit-test-delete-sym') self.assertLExists(srcname) link_fn(srcname, linkname) self.assertLExists(linkname) self.assertExists(linkname) # delete regular file first delete(srcname, shred) self.assertNotExists(srcname) self.assertLExists(linkname) # clean up delete(linkname, shred) self.assertNotExists(linkname) self.assertNotLExists(linkname) windows_vista_or_newer = False if 'nt' == os.name: from bleachbit.Windows import parse_windows_build # Windows Vista = 6.0 windows_vista_or_newer = parse_windows_build() >= 6.0 if windows_vista_or_newer: logger.debug('testing symbolic link') import ctypes kern = ctypes.windll.LoadLibrary("kernel32.dll") def win_symlink(src, linkname): rc = kern.CreateSymbolicLinkW(linkname, src, 0) if rc == 0: print('CreateSymbolicLinkW(%s, %s)' % (linkname, src)) print('CreateSymolicLinkW() failed, error = %s' % ctypes.FormatError()) self.assertNotEqual(rc, 0) symlink_helper(win_symlink) # below this point, only posix if 'nt' == os.name: return # test file with mode 0444/-r--r--r-- filename = self.write_file('bleachbit-test-0444') os.chmod(filename, 0o444) delete(filename, shred) self.assertNotExists(filename) # test symlink symlink_helper(os.symlink) # test FIFO args = ["mkfifo", filename] ret = subprocess.call(args) self.assertEqual(ret, 0) self.assertExists(filename) delete(filename, shred) self.assertNotExists(filename) # test directory path = self.mkdtemp(prefix='bleachbit-test-delete-dir') self.assertExists(path) delete(path, shred) self.assertNotExists(path)