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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
|
# vim: ts=4:sw=4:expandtab
# -*- coding: UTF-8 -*-
## BleachBit
## Copyright (C) 2010 Andrew Ziem
## http://bleachbit.sourceforge.net
##
## 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 case for module FileUtilities
"""
import datetime
import locale
import sys
import tempfile
import unittest
sys.path.append('.')
from bleachbit.FileUtilities import *
class FileUtilitiesTestCase(unittest.TestCase):
"""Test case for module FileUtilities"""
def __touch(self, filename):
"""Create an empty file"""
open(filename, "w")
def test_bytes_to_human(self):
"""Unit test for class bytes_to_human"""
if 'posix' == os.name:
old_locale = locale.getlocale(locale.LC_NUMERIC)
locale.setlocale(locale.LC_NUMERIC, 'en_US.UTF-8')
# test one-way conversion for predefined values
tests = [ ("-1B", bytes_to_human(-1)),
("0", bytes_to_human(0)),
("1B", bytes_to_human(1)),
("1kB", bytes_to_human(1000)),
("1.1kB", bytes_to_human(1110)),
("1MB", bytes_to_human(1000**2)),
("1.3MB", bytes_to_human(1289748)),
("1GB", bytes_to_human(1000**3)),
("1.32GB", bytes_to_human(1320702444)),
("1TB", bytes_to_human(1000**4)) ]
for test in tests:
self.assertEqual(test[0], test[1])
# test roundtrip conversion for random values
import random
for n in range(0, 1000):
bytes1 = random.randrange(0, 1000**4)
human = bytes_to_human(bytes1)
bytes2 = human_to_bytes(human)
error = abs(float(bytes2 - bytes1) / bytes1)
self.assert_(abs(error) < 0.01, \
"%d (%s) is %.2f%% different than %d" % \
(bytes1, human, error * 100, bytes2))
# test localization
if hasattr(locale, 'format_string'):
try:
locale.setlocale(locale.LC_NUMERIC, 'de_DE.utf8')
except:
print "Warning: exception when setlocale to de_DE.utf8"
else:
self.assertEqual("1,01GB", bytes_to_human(1000**3 + 5812389))
# clean up
if 'posix' == os.name:
locale.setlocale(locale.LC_NUMERIC, old_locale)
def test_children_in_directory(self):
"""Unit test for function children_in_directory()"""
# test an existing directory that usually exists
dirname = os.path.expanduser("~/.config")
for filename in children_in_directory(dirname, True):
self.assert_ (type(filename) is str)
self.assert_ (os.path.isabs(filename))
for filename in children_in_directory(dirname, False):
self.assert_ (type(filename) is str)
self.assert_ (os.path.isabs(filename))
self.assert_ (not os.path.isdir(filename))
# test a constructed file in a constructed directory
dirname = tempfile.mkdtemp()
filename = os.path.join(dirname, "somefile")
self.__touch(filename)
for loopfilename in children_in_directory(dirname, True):
self.assertEqual (loopfilename, filename)
for loopfilename in children_in_directory(dirname, False):
self.assertEqual (loopfilename, filename)
os.remove(filename)
# test subdirectory
subdirname = os.path.join(dirname, "subdir")
os.mkdir(subdirname)
for filename in children_in_directory(dirname, True):
self.assertEqual (filename, subdirname)
for filename in children_in_directory(dirname, False):
self.assert_ (False)
os.rmdir(subdirname)
os.rmdir(dirname)
def test_delete(self):
"""Unit test for method delete()"""
print "testing delete() with shred = False"
self.delete_helper(shred = False)
print "testing delete() with shred = True"
self.delete_helper(shred = True)
def delete_helper(self, shred):
"""Called by test_delete() with shred = False and = True"""
hebrew = u"עִבְרִית"
katanana = u"アメリカ"
umlauts = u"ÄäǞǟËëḦḧÏïḮḯÖöȪȫṎṏT̈ẗÜüǕǖǗǘǙǚǛǜṲṳṺṻẄẅẌẍŸÿ"
tests = [ ('.suffix', 'prefix'), # simple
("x".zfill(100), ".y".zfill(100)), # long
(" ", " "), # space
("'", "'"), # quotation mark
("~`!@#$%^&()-_+=", "x"), # non-alphanumeric characters
("[]{};',.", "x"), # non-alphanumeric characters
(u'abcd', u'efgh'), # simple unicode
(u'J\xf8rgen', 'Scandinavian'),
(hebrew, hebrew),
(katanana, katanana),
(umlauts, umlauts) ]
if 'posix' == os.name:
# Windows doesn't allow these characters but Unix systems do
tests.append( ('"', '*') )
tests.append( ('\t', '\\') )
tests.append( (':?', '<>|') )
for test in tests:
(fd, filename) = tempfile.mkstemp(test[0], 'bleachbit-test' + test[1])
self.assert_(os.path.exists(filename))
for x in range(0, 4096):
bytes_written = os.write(fd, "top secret")
self.assertEqual(bytes_written, 10)
os.close(fd)
self.assert_(os.path.exists(filename))
delete(filename, shred)
self.assert_(not os.path.exists(filename))
if 'nt' == os.name:
return
# test file with mode 0444/-r--r--r--
(fd, filename) = tempfile.mkstemp()
os.close(fd)
os.chmod(filename, 0444)
delete(filename, shred)
self.assert_(not os.path.exists(filename))
# test symlink
(fd, filename) = tempfile.mkstemp()
os.close(fd)
self.assert_(os.path.exists(filename))
linkname = '/tmp/bleachbitsymlinktest'
if os.path.lexists(linkname):
delete(linkname, shred)
self.assert_(not os.path.lexists(linkname))
os.symlink(filename, linkname)
self.assert_(os.path.lexists(linkname))
delete(linkname, shred)
self.assert_(os.path.exists(filename))
self.assert_(not os.path.lexists(linkname))
delete(filename, shred)
self.assert_(not os.path.exists(filename))
# test broken symlink
os.symlink(filename, linkname)
self.assert_(os.path.lexists(linkname))
self.assert_(not os.path.exists(linkname))
delete(linkname, shred)
self.assert_(not os.path.exists(linkname))
# test fifo
args = ["mkfifo", filename]
ret = subprocess.call(args)
self.assertEqual(ret, 0)
self.assert_(os.path.exists(filename))
delete(filename, shred)
self.assert_(not os.path.exists(filename))
def test_ego_owner(self):
"""Unit test for ego_owner()"""
if 'nt' == os.name:
return
self.assertEqual(ego_owner('/bin/ls'), os.getuid() == 0)
def test_exists_in_path(self):
"""Unit test for exists_in_path()"""
filename = 'ls'
if 'nt' == os.name:
filename = 'cmd.exe'
self.assert_(exists_in_path(filename))
def test_exe_exists(self):
"""Unit test for exe_exists()"""
tests = [ ("/bin/sh", True), \
("sh", True), \
("doesnotexist", False), \
("/bin/doesnotexist", False) ]
if 'nt' == os.name:
tests = [ ('c:\\windows\\system32\\cmd.exe', True), \
('cmd.exe', True),
('doesnotexist', False),
('c:\\windows\\doesnotexist.exe', False) ]
for test in tests:
self.assertEqual(exe_exists(test[0]), test[1])
def test_expand_glob_join(self):
"""Unit test for expand_glob_join()"""
if 'posix' == os.name:
expand_glob_join('/bin', '*sh')
if 'nt' == os.name:
expand_glob_join('c:\windows', '*.exe')
def test_getsize(self):
"""Unit test for method getsize()"""
# create regular file
(handle, filename) = tempfile.mkstemp("regulartest")
os.write(handle, "abcdefghij" * 12345)
os.close(handle)
if 'nt' == os.name:
self.assertEqual(getsize(filename), 10 * 12345)
return
output = subprocess.Popen(["du", "-h", filename], stdout=subprocess.PIPE).communicate()[0]
output = output.replace("\n", "")
du_size = output.split("\t")[0] + "B"
print "output = '%s', size='%s'" % (output, du_size)
du_bytes = human_to_bytes(du_size, 'du')
print output, du_size, du_bytes
self.assertEqual(getsize(filename), du_bytes)
# create a symlink
linkname = '/tmp/bleachbitsymlinktest'
os.symlink(filename, linkname)
self.assert_(getsize(linkname) < 8192, "Symlink size is %d" % getsize(filename))
delete(filename)
# create sparse file
(handle, filename) = tempfile.mkstemp("sparsetest")
os.ftruncate(handle, 1000**2)
os.close(handle)
self.assertEqual(getsize(filename), 0)
delete(filename)
def test_getsizedir(self):
"""Unit test for getsizedir()"""
path = '/bin'
if 'nt' == os.name:
path = 'c:\\windows\\system32'
self.assert_(getsizedir(path) > 0)
def test_globex(self):
"""Unit test for method globex()"""
for path in globex('/bin/*', '/ls$'):
self.assertEqual(path, '/bin/ls')
def test_listdir(self):
"""Unit test for listdir()"""
for pathname in listdir(('/tmp','~/.config/')):
self.assert_(os.path.lexists(pathname), \
"does not exist: %s" % pathname)
def test_whitelisted(self):
"""Unit test for whitelisted()"""
# setup
old_whitelist = options.get_whitelist_paths()
whitelist = [ ('file', '/home/foo'), ('folder', '/home/folder') ]
options.set_whitelist_paths(whitelist)
self.assertEqual(set(whitelist), set(options.get_whitelist_paths()))
# test
self.assertEqual(False, whitelisted(''))
self.assertEqual(False, whitelisted('/'))
self.assertEqual(False, whitelisted('/home/foo2'))
self.assertEqual(False, whitelisted('/home/fo'))
self.assertEqual(True, whitelisted('/home/foo'))
self.assertEqual(True, whitelisted('/home/folder'))
if 'posix' == os.name:
self.assertEqual(True, whitelisted('/home/folder/'))
self.assertEqual(True, whitelisted('/home/folder/file'))
self.assertEqual(False, whitelisted('/home/fold'))
self.assertEqual(False, whitelisted('/home/folder2'))
if 'nt' == os.name:
whitelist = [ ('folder', 'D:\\'), ('file', 'c:\\windows\\foo.log'), ('folder', 'e:\\users') ]
options.set_whitelist_paths(whitelist)
self.assertEqual(True, whitelisted('e:\\users'))
self.assertEqual(True, whitelisted('e:\\users\\'))
self.assertEqual(True, whitelisted('e:\\users\\foo.log'))
self.assertEqual(False, whitelisted('e:\\users2'))
# case insensitivity
self.assertEqual(True, whitelisted('C:\\WINDOWS\\FOO.LOG'))
self.assertEqual(True, whitelisted('D:\\USERS'))
# drives letters have the seperator at the end while most paths don't
self.assertEqual(True, whitelisted('D:\\FOLDER\\FOO.LOG'))
# test blank
options.set_whitelist_paths([])
self.assertEqual(False, whitelisted('/home/foo'))
self.assertEqual(False, whitelisted('/home/folder'))
self.assertEqual(False, whitelisted('/home/folder/file'))
options.config.remove_section('whitelist/paths')
self.assertEqual(False, whitelisted('/home/foo'))
self.assertEqual(False, whitelisted('/home/folder'))
self.assertEqual(False, whitelisted('/home/folder/file'))
# clean up
options.set_whitelist_paths(old_whitelist)
self.assertEqual(set(old_whitelist), set(options.get_whitelist_paths()))
def test_wipe_contents(self):
"""Unit test for wipe_delete()"""
# create test file
(handle, filename) = tempfile.mkstemp("wipetest")
os.write(handle, "abcdefghij" * 12345)
os.close(handle)
# wipe it
wipe_contents(filename)
# check it
f = open(filename, 'rb')
while True:
byte = f.read(1)
if "" == byte:
break
self.assertEqual(byte, chr(0))
f.close()
# clean up
os.remove(filename)
def test_wipe_path(self):
"""Unit test for wipe_path()"""
if None == os.getenv('ALLTESTS'):
print 'warning: skipping long test test_wipe_path() because environment variable ALLTESTS not set'
return
pathname = tempfile.gettempdir()
wipe_path(pathname)
def test_vacuum_sqlite3(self):
"""Unit test for method vacuum_sqlite3()"""
try:
import sqlite3
except ImportError, e:
if sys.version_info[0] == 2 and sys.version_info[1] < 5:
print "Warning: Skipping test_vacuum_sqlite3() on old Python"
return
else:
raise e
path = 'bleachbit.tmp.sqlite3'
if os.path.lexists(path):
delete(path)
conn = sqlite3.connect(path)
conn.execute('create table numbers (number)')
conn.commit()
empty_size = getsize(path)
def number_generator():
for x in range(1, 10000):
yield (x, )
conn.executemany('insert into numbers (number) values ( ? ) ', number_generator())
conn.commit()
self.assert_( empty_size < getsize(path))
conn.execute('delete from numbers')
conn.commit()
conn.close()
vacuum_sqlite3(path)
self.assertEqual(empty_size , getsize(path))
delete(path)
def test_OpenFiles(self):
"""Unit test for class OpenFiles"""
if 'nt' == os.name:
return
(handle, filename) = tempfile.mkstemp()
openfiles = OpenFiles()
self.assertEqual(openfiles.is_open(filename), True, \
"Expected is_open(%s) to return True)\n" \
"openfiles.last_scan_time (ago)=%s\n" \
"openfiles.files=%s" % \
(filename, \
datetime.datetime.now() - openfiles.last_scan_time, \
openfiles.files))
f = os.fdopen(handle)
f.close()
openfiles.scan()
self.assertEqual(openfiles.is_open(filename), False)
os.unlink(filename)
openfiles.scan()
self.assertEqual(openfiles.is_open(filename), False)
def suite():
return unittest.makeSuite(FileUtilitiesTestCase)
if __name__ == '__main__':
unittest.main()
|