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
|
#!/usr/bin/python
# coding: utf-8
import os
import subprocess
import unittest
import tempfile
import shutil
import errno
import platform
import time
import traceback
import uuid
import sys
import stat
import traceback
def find_securefs_binary():
for f in (os.path.join(
os.environ.get('build_config_type', '.'), 'securefs'),
'./securefs'):
try:
st = os.stat(f)
except EnvironmentError:
continue
else:
mode = st.st_mode
if stat.S_ISREG(mode) and (mode & stat.S_IEXEC):
return f
raise RuntimeError('securefs binary not found')
SECUREFS_BINARY = find_securefs_binary()
if platform.system() == 'Darwin':
UNMOUNT = ['umount']
try:
import xattr
except ImportError:
sys.stderr.write(
'Importing module "xattr" failed. Testing for extended attribute support is skipped\n'
)
xattr = None
else:
UNMOUNT = ['fusermount', '-u']
xattr = None
class TimeoutException(BaseException):
def __init__(self):
BaseException.__init__(self, "Operation timeout")
def securefs_mount(data_dir, mount_point, password):
p = subprocess.Popen(
[
SECUREFS_BINARY, 'mount', '--log', 'XXXX.log', '--trace',
'--background', data_dir, mount_point
],
stdin=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate(input=password + '\n')
if p.returncode:
raise RuntimeError(err)
for i in xrange(100):
time.sleep(0.1)
try:
if os.path.ismount(mount_point):
return
except EnvironmentError:
traceback.print_exc()
raise TimeoutException()
def securefs_unmount(mount_point):
p = subprocess.Popen(UNMOUNT + [mount_point], stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode:
raise RuntimeError(err)
for i in xrange(100):
time.sleep(0.1)
try:
if not os.path.ismount(mount_point):
return
except EnvironmentError:
traceback.print_exc()
raise TimeoutException()
def securefs_create(data_dir, password, version):
p = subprocess.Popen(
[
SECUREFS_BINARY, 'create', '--format',
str(version), data_dir, '--rounds', '4'
],
stdin=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate(input=password + '\n')
if p.returncode:
raise RuntimeError(err)
def make_test_case(format_version):
class SimpleSecureFSTestBase(unittest.TestCase):
@classmethod
def setUpClass(cls):
try:
os.mkdir('tmp')
except EnvironmentError as e:
if e.errno != errno.EEXIST:
raise
cls.data_dir = tempfile.mkdtemp(
prefix='securefs.format{}.data_dir'.format(format_version),
dir='tmp')
cls.mount_point = tempfile.mkdtemp(
prefix='securefs.format{}.mount_point'.format(format_version),
dir='tmp')
cls.password = 'madoka'
securefs_create(cls.data_dir, cls.password, format_version)
securefs_mount(cls.data_dir, cls.mount_point, cls.password)
@classmethod
def tearDownClass(cls):
try:
securefs_unmount(cls.mount_point)
except:
if os.path.ismount(cls.mount_point):
raise # Still mounted
def mount(self):
securefs_mount(self.data_dir, self.mount_point, self.password)
def unmount(self):
securefs_unmount(self.mount_point)
def test_long_name(self):
try:
os.mkdir(os.path.join(self.mount_point, 'k' * 256))
self.fail('mkdir should fail')
except EnvironmentError as e:
self.assertEqual(e.errno, errno.ENAMETOOLONG)
if xattr:
def test_xattr(self):
fn = os.path.join(self.mount_point, str(uuid.uuid4()))
try:
with open(fn, 'wb') as f:
f.write('hello\n')
x = xattr.xattr(fn)
x.set('abc', 'def')
x.set('123', '456')
self.unmount()
self.mount()
self.assertEqual(x.get('abc'), 'def')
self.assertEqual(set(x.list()), {'abc', '123'})
xattr.removexattr(fn, 'abc')
self.assertEqual(set(x.list()), {'123'})
finally:
try:
os.remove(fn)
except EnvironmentError:
pass
if format_version < 4:
def test_hardlink(self):
data = os.urandom(16)
source = os.path.join(self.mount_point, str(uuid.uuid4()))
dest = os.path.join(self.mount_point, str(uuid.uuid4()))
try:
with open(source, 'wb') as f:
f.write(data)
os.link(source, dest)
source_stat = os.stat(source)
dest_stat = os.stat(dest)
self.assertEqual(source_stat.st_mode, dest_stat.st_mode)
self.assertEqual(source_stat.st_mtime, dest_stat.st_mtime)
self.assertEqual(source_stat.st_size, dest_stat.st_size)
self.assertEqual(source_stat.st_nlink, 2)
with open(dest, 'rb') as f:
self.assertEqual(data, f.read())
# Moving hard links onto each other is a no-op
os.rename(dest, source)
self.assertTrue(
os.path.isfile(dest) and os.path.isfile(source))
finally:
try:
os.remove(source)
except EnvironmentError:
pass
try:
os.remove(dest)
except EnvironmentError:
pass
def test_symlink(self):
data = os.urandom(16)
source = os.path.join(self.mount_point, str(uuid.uuid4()))
dest = os.path.join(self.mount_point, str(uuid.uuid4()))
try:
with open(source, 'wb') as f:
f.write(data)
os.symlink(source, dest)
self.assertEqual(os.readlink(dest), source)
os.remove(source)
with self.assertRaises(EnvironmentError):
with open(dest, 'rb') as f:
f.read()
finally:
try:
os.remove(source)
except EnvironmentError:
pass
try:
os.remove(dest)
except EnvironmentError:
pass
def test_rename(self):
data = os.urandom(32)
source = os.path.join(self.mount_point, str(uuid.uuid4()))
dest = os.path.join(self.mount_point, str(uuid.uuid4()))
try:
with open(source, 'wb') as f:
f.write(data)
source_stat = os.stat(source)
self.assertFalse(os.path.isfile(dest))
os.rename(source, dest)
self.assertFalse(os.path.isfile(source))
self.assertTrue(os.path.isfile(dest))
dest_stat = os.stat(dest)
self.assertEqual(source_stat.st_ino, dest_stat.st_ino)
self.assertEqual(source_stat.st_size, dest_stat.st_size)
finally:
try:
os.remove(source)
except EnvironmentError:
pass
try:
os.remove(dest)
except EnvironmentError:
pass
def test_rename_dir(self):
a = str(uuid.uuid4())
b = str(uuid.uuid4())
c = str(uuid.uuid4())
cwd = os.getcwd()
os.chdir(self.mount_point)
try:
os.mkdir(a)
os.mkdir(os.path.join(a, b))
os.mkdir(c)
os.rename(a, os.path.join(c, a))
self.assertTrue(os.path.isdir(os.path.join(c, a, b)))
finally:
try:
shutil.rmtree(a)
except EnvironmentError:
pass
try:
shutil.rmtree(c)
except:
pass
os.chdir(cwd)
def test_read_write_mkdir_listdir_remove(self):
dir_names = set(str(i) for i in xrange(3))
random_data = os.urandom(11111)
rng_filename = os.path.join(self.mount_point, 'rng')
with open(rng_filename, 'wb') as f:
f.write(random_data)
self.unmount()
self.mount()
with open(rng_filename, 'rb') as f:
self.assertEqual(f.read(), random_data)
data = '\0' * len(random_data) + '0'
with open(rng_filename, 'wb') as f:
f.write(data)
with open(rng_filename, 'rb') as f:
self.assertEqual(f.read(), data)
os.remove(rng_filename)
for n in dir_names:
os.mkdir(os.path.join(self.mount_point, n))
for n in dir_names:
os.mkdir(os.path.join(self.mount_point, '0', n))
for n in dir_names:
os.mkdir(os.path.join(self.mount_point, '0', '1', n))
self.unmount()
self.mount()
self.assertEqual(set(os.listdir(self.mount_point)), dir_names)
self.assertEqual(
set(os.listdir(os.path.join(self.mount_point, '0'))),
dir_names)
self.assertEqual(
set(os.listdir(os.path.join(self.mount_point, '0', '1'))),
dir_names)
for dn in dir_names:
shutil.rmtree(os.path.join(self.mount_point, dn))
if format_version == 3:
def test_time(self):
rand_dirname = os.path.join(self.mount_point,
str(uuid.uuid4()))
os.mkdir(rand_dirname)
st = os.stat(rand_dirname)
self.assertTrue(st.st_atime == st.st_ctime and
st.st_ctime == st.st_mtime)
self.assertAlmostEqual(st.st_atime, time.time(), delta=10)
rand_filename = os.path.join(rand_dirname, 'abc')
with open(rand_filename, 'w') as f:
f.write('1')
os.utime(rand_filename, (1000., 1000.))
st = os.stat(rand_filename)
self.assertEqual(st.st_mtime, 1000)
with open(rand_filename, 'w') as f:
f.write('1')
st = os.stat(rand_filename)
self.assertAlmostEqual(st.st_ctime, time.time(), delta=10)
return SimpleSecureFSTestBase
class TestVersion1(make_test_case(1)):
pass
class TestVersion2(make_test_case(2)):
pass
class TestVersion3(make_test_case(3)):
pass
class TestVersion4(make_test_case(4)):
pass
if __name__ == '__main__':
unittest.main()
|