1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
# vim: ts=4:sw=4:expandtab
# BleachBit
# Copyright (C) 2008-2025 Andrew Ziem
# https://www.bleachbit.org
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Test FileUtilities.wipe_path
"""
from bleachbit.FileUtilities import delete, free_space, listdir, wipe_path
from bleachbit.General import run_external
from tests import common
import logging
import os
import sys
import tempfile
import time
import traceback
logger = logging.getLogger('bleachbit')
def create_disk_image(n_bytes):
"""Make blank file and return filename"""
(fd, filename) = tempfile.mkstemp(
suffix='disk-image', prefix='bleachbit-wipe-test')
for _x in range(1, int(n_bytes / 1e5)):
os.write(fd, b'\x00' * 100000)
os.close(fd)
return filename
def format_filesystem(filename, mkfs_cmd):
args = []
for arg in mkfs_cmd:
if arg == 'filename':
args.append(filename)
else:
args.append(arg)
(rc, stdout, stderr) = run_external(args)
assert (rc == 0)
def make_dirty(mountpoint):
create_counter = 0
write_counter = 0
contents = 'sssshhhh' * 512
while True:
try:
fn = os.path.join(mountpoint, 'secret' + str(create_counter))
f = open(fn, 'w')
create_counter += 1
except:
logger.error('while creating temporary file #%d', create_counter)
break
try:
f.write(contents)
f.flush()
write_counter += 1
except IOError:
break
try:
f.close()
except:
logger.error('while closing temporary file %s', f.name)
break
logger.debug('created %d files and wrote to %d files',
create_counter, write_counter)
def mount_filesystem(filename, mountpoint):
args = ['mount', '-o', 'loop', filename, mountpoint]
(rc, stdout, stderr) = run_external(args)
if stderr:
print(stderr)
assert (rc == 0)
print('mounted %s at %s', filename, mountpoint)
def unmount_filesystem(mountpoint):
time.sleep(0.5) # avoid "in use" error
args = ['umount', mountpoint]
attempts = 0
while True:
(rc, stdout, stderr) = run_external(args)
if stderr:
print(stderr)
if 0 == rc:
break
attempts += 1
time.sleep(attempts * 2)
if attempts > 5:
raise RuntimeError('cannot umount')
def verify_cleanliness(filename):
"""Return True if the file is clean"""
strings_ret = run_external(['strings', filename])
secret_count = strings_ret[1].count('secret') # filename
sssshhhh_count = strings_ret[1].count('sssshhhh') # contents
logger.debug('found %d sssshhhhh in image (contents) and %d secret (filename)',
sssshhhh_count, secret_count)
clean = ((secret_count > 0) * 1) + ((sssshhhh_count > 0) * 10)
print('%s is clean: %s', filename, clean)
return clean
@common.skipIfWindows
def test_wipe_sub(n_bytes, mkfs_cmd):
"""Test FileUtilities.wipe_path"""
filename = create_disk_image(n_bytes)
print('created disk image %s' % filename)
# format filesystem
format_filesystem(filename, mkfs_cmd)
# mount
mountpoint = tempfile.mkdtemp(prefix='bleachbit-wipe-mountpoint')
mount_filesystem(filename, mountpoint)
# baseline free disk space
print('df for clean filesystem')
print(run_external(['df', mountpoint])[1])
# make dirty
make_dirty(mountpoint)
# verify dirtiness
unmount_filesystem(mountpoint)
assert (verify_cleanliness(filename) == 11)
mount_filesystem(filename, mountpoint)
# standard delete
logger.info('standard delete')
delete_counter = 0
for secretfile in listdir(mountpoint):
if 'secret' not in secretfile:
# skip lost+found
continue
delete(secretfile, shred=False)
delete_counter += 1
logger.debug('deleted %d files', delete_counter)
# check
print('df for empty, dirty filesystem')
print(run_external(['df', mountpoint])[1])
# verify dirtiness
unmount_filesystem(mountpoint)
assert (verify_cleanliness(filename) == 11)
mount_filesystem(filename, mountpoint)
expected_free_space = free_space(mountpoint)
# measure effectiveness of multiple wipes
for i in range(1, 10):
print('*' * 30)
print('* pass %d *' % i)
print('*' * 30)
# remount
if i > 1:
mount_filesystem(filename, mountpoint)\
# really wipe
print('wiping %s' % mountpoint)
for _w in wipe_path(mountpoint):
pass
# verify cleaning process freed all space it allocated
actual_free_space = free_space(mountpoint)
if not expected_free_space == actual_free_space:
print('expecting %d free space but got %d' %
(expected_free_space, actual_free_space))
import pdb
pdb.set_trace()
# unmount
unmount_filesystem(mountpoint)
# verify cleanliness
cleanliness = verify_cleanliness(filename)
assert (cleanliness < 2)
# remove temporary
delete(filename)
delete(mountpoint)
def test_wipe():
"""Test wiping on several kinds of file systems"""
n_bytes = 10000000
mkfs_cmds = (('/sbin/mkfs.ext3', '-q', '-F', 'filename'),)
# ('/sbin/mkfs.ext4', '-q', '-F', 'filename'))
# ('/sbin/mkntfs', '-F', 'filename'),
# ('/sbin/mkfs.vfat', 'filename') )
for mkfs_cmd in mkfs_cmds:
print()
print('*' * 70)
print(' '.join(mkfs_cmd))
print('*' * 70)
print()
try:
test_wipe_sub(n_bytes, mkfs_cmd)
except:
print(sys.exc_info()[1])
traceback.print_exc()
test_wipe()
|