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 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tests for the file entry implementation using the SleuthKit (TSK)."""
from __future__ import unicode_literals
import unittest
import pytsk3
from dfvfs.path import os_path_spec
from dfvfs.path import qcow_path_spec
from dfvfs.path import tsk_path_spec
from dfvfs.resolver import context
from dfvfs.vfs import tsk_file_entry
from dfvfs.vfs import tsk_file_system
from tests import test_lib as shared_test_lib
class TSKTimeTest(unittest.TestCase):
"""Tests for the SleuthKit timestamp."""
def testCopyFromDateTimeString(self):
"""Tests the CopyFromDateTimeString function."""
tsk_time_object = tsk_file_entry.TSKTime()
if pytsk3.TSK_VERSION_NUM >= 0x040200ff:
expected_fraction_of_second = 546875000
else:
expected_fraction_of_second = 5468750
tsk_time_object.CopyFromDateTimeString('2010-08-12 21:06:31.546875')
self.assertEqual(tsk_time_object.timestamp, 1281647191)
self.assertEqual(
tsk_time_object.fraction_of_second, expected_fraction_of_second)
def testCopyToStatTimeTuple(self):
"""Tests the CopyToStatTimeTuple function."""
if pytsk3.TSK_VERSION_NUM >= 0x040200ff:
fraction_of_second = 546875000
else:
fraction_of_second = 5468750
tsk_time_object = tsk_file_entry.TSKTime(
fraction_of_second=fraction_of_second, timestamp=1281643591)
stat_time_tuple = tsk_time_object.CopyToStatTimeTuple()
self.assertEqual(stat_time_tuple, (1281643591, 5468750))
tsk_time_object = tsk_file_entry.TSKTime()
stat_time_tuple = tsk_time_object.CopyToStatTimeTuple()
self.assertEqual(stat_time_tuple, (None, None))
def testCopyToDateTimeString(self):
"""Tests the CopyToDateTimeString function."""
if pytsk3.TSK_VERSION_NUM >= 0x040200ff:
fraction_of_second = 546875000
else:
fraction_of_second = 5468750
tsk_time_object = tsk_file_entry.TSKTime(
fraction_of_second=fraction_of_second, timestamp=1281643591)
if pytsk3.TSK_VERSION_NUM >= 0x040200ff:
expected_date_time_string = '2010-08-12 20:06:31.546875000'
else:
expected_date_time_string = '2010-08-12 20:06:31.5468750'
date_time_string = tsk_time_object.CopyToDateTimeString()
self.assertEqual(date_time_string, expected_date_time_string)
tsk_time_object = tsk_file_entry.TSKTime()
date_time_string = tsk_time_object.CopyToDateTimeString()
self.assertIsNone(date_time_string)
def testGetDate(self):
"""Tests the GetDate function."""
if pytsk3.TSK_VERSION_NUM >= 0x040200ff:
fraction_of_second = 546875000
else:
fraction_of_second = 5468750
tsk_time_object = tsk_file_entry.TSKTime(
fraction_of_second=fraction_of_second, timestamp=1281643591)
date_tuple = tsk_time_object.GetDate()
self.assertEqual(date_tuple, (2010, 8, 12))
tsk_time_object = tsk_file_entry.TSKTime()
date_tuple = tsk_time_object.GetDate()
self.assertEqual(date_tuple, (None, None, None))
def testGetPlasoTimestamp(self):
"""Tests the GetPlasoTimestamp function."""
tsk_time_object = tsk_file_entry.TSKTime(
fraction_of_second=546875000, timestamp=1281643591)
micro_posix_timestamp = tsk_time_object.GetPlasoTimestamp()
self.assertEqual(micro_posix_timestamp, 1281643591546875)
tsk_time_object = tsk_file_entry.TSKTime()
micro_posix_timestamp = tsk_time_object.GetPlasoTimestamp()
self.assertIsNone(micro_posix_timestamp)
@shared_test_lib.skipUnlessHasTestFile(['ímynd.dd'])
class TSKFileEntryTestExt2(shared_test_lib.BaseTestCase):
"""Tests the SleuthKit (TSK) file entry on ext2."""
def setUp(self):
"""Sets up the needed objects used throughout the test."""
self._resolver_context = context.Context()
test_file = self._GetTestFilePath(['ímynd.dd'])
self._os_path_spec = os_path_spec.OSPathSpec(location=test_file)
self._tsk_path_spec = tsk_path_spec.TSKPathSpec(
location='/', parent=self._os_path_spec)
self._file_system = tsk_file_system.TSKFileSystem(self._resolver_context)
self._file_system.Open(self._tsk_path_spec)
def tearDown(self):
"""Cleans up the needed objects used throughout the test."""
self._file_system.Close()
def testInitialize(self):
"""Tests the __init__ function."""
file_entry = tsk_file_entry.TSKFileEntry(
self._resolver_context, self._file_system, self._tsk_path_spec)
self.assertIsNotNone(file_entry)
def testBackupTime(self):
"""Test the backup_time property."""
test_location = '/a_directory/another_file'
path_spec = tsk_path_spec.TSKPathSpec(
inode=16, location=test_location, parent=self._os_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNone(file_entry.backup_time)
def testDeletionTime(self):
"""Test the deletion_time property."""
test_location = '/a_directory/another_file'
path_spec = tsk_path_spec.TSKPathSpec(
inode=16, location=test_location, parent=self._os_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry.deletion_time)
def testGetFileEntryByPathSpec(self):
"""Tests the GetFileEntryByPathSpec function."""
path_spec = tsk_path_spec.TSKPathSpec(inode=15, parent=self._os_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
def testGetLinkedFileEntry(self):
"""Tests the GetLinkedFileEntry function."""
test_location = '/a_link'
path_spec = tsk_path_spec.TSKPathSpec(
inode=13, location=test_location, parent=self._os_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
linked_file_entry = file_entry.GetLinkedFileEntry()
self.assertIsNotNone(linked_file_entry)
self.assertEqual(linked_file_entry.name, 'another_file')
def testGetParentFileEntry(self):
"""Tests the GetParentFileEntry function."""
test_location = '/a_directory/another_file'
path_spec = tsk_path_spec.TSKPathSpec(
inode=16, location=test_location, parent=self._os_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
parent_file_entry = file_entry.GetParentFileEntry()
self.assertIsNotNone(parent_file_entry)
self.assertEqual(parent_file_entry.name, 'a_directory')
def testGetStat(self):
"""Tests the GetStat function."""
test_location = '/a_directory/another_file'
path_spec = tsk_path_spec.TSKPathSpec(
inode=16, location=test_location, parent=self._os_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
stat_object = file_entry.GetStat()
self.assertIsNotNone(stat_object)
self.assertEqual(stat_object.type, stat_object.TYPE_FILE)
self.assertEqual(stat_object.size, 22)
self.assertEqual(stat_object.mode, 384)
self.assertEqual(stat_object.uid, 151107)
self.assertEqual(stat_object.gid, 5000)
self.assertEqual(stat_object.atime, 1337961563)
self.assertFalse(hasattr(stat_object, 'atime_nano'))
self.assertEqual(stat_object.ctime, 1337961563)
self.assertFalse(hasattr(stat_object, 'ctime_nano'))
# EXT2 has no crtime timestamp.
self.assertFalse(hasattr(stat_object, 'crtime'))
self.assertFalse(hasattr(stat_object, 'crtime_nano'))
self.assertEqual(stat_object.mtime, 1337961563)
self.assertFalse(hasattr(stat_object, 'mtime_nano'))
def testIsFunctions(self):
"""Tests the Is? functions."""
test_location = '/a_directory/another_file'
path_spec = tsk_path_spec.TSKPathSpec(
inode=16, location=test_location, parent=self._os_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
self.assertFalse(file_entry.IsRoot())
self.assertFalse(file_entry.IsVirtual())
self.assertTrue(file_entry.IsAllocated())
self.assertFalse(file_entry.IsDevice())
self.assertFalse(file_entry.IsDirectory())
self.assertTrue(file_entry.IsFile())
self.assertFalse(file_entry.IsLink())
self.assertFalse(file_entry.IsPipe())
self.assertFalse(file_entry.IsSocket())
test_location = '/a_directory'
path_spec = tsk_path_spec.TSKPathSpec(
inode=12, location=test_location, parent=self._os_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
self.assertFalse(file_entry.IsRoot())
self.assertFalse(file_entry.IsVirtual())
self.assertTrue(file_entry.IsAllocated())
self.assertFalse(file_entry.IsDevice())
self.assertTrue(file_entry.IsDirectory())
self.assertFalse(file_entry.IsFile())
self.assertFalse(file_entry.IsLink())
self.assertFalse(file_entry.IsPipe())
self.assertFalse(file_entry.IsSocket())
path_spec = tsk_path_spec.TSKPathSpec(
location='/', parent=self._os_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
self.assertTrue(file_entry.IsRoot())
self.assertFalse(file_entry.IsVirtual())
self.assertTrue(file_entry.IsAllocated())
self.assertFalse(file_entry.IsDevice())
self.assertTrue(file_entry.IsDirectory())
self.assertFalse(file_entry.IsFile())
self.assertFalse(file_entry.IsLink())
self.assertFalse(file_entry.IsPipe())
self.assertFalse(file_entry.IsSocket())
def testSubFileEntries(self):
"""Tests the number_of_sub_file_entries and sub_file_entries properties."""
path_spec = tsk_path_spec.TSKPathSpec(
location='/', parent=self._os_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
self.assertEqual(file_entry.number_of_sub_file_entries, 5)
# Note that passwords.txt~ is currently ignored by dfvfs, since
# its directory entry has no pytsk3.TSK_FS_META object.
expected_sub_file_entry_names = [
'a_directory',
'a_link',
'lost+found',
'passwords.txt',
'$OrphanFiles']
sub_file_entry_names = []
for sub_file_entry in file_entry.sub_file_entries:
sub_file_entry_names.append(sub_file_entry.name)
self.assertEqual(
len(sub_file_entry_names), len(expected_sub_file_entry_names))
self.assertEqual(
sorted(sub_file_entry_names), sorted(expected_sub_file_entry_names))
def testDataStreams(self):
"""Tests the data streams functionality."""
test_location = '/a_directory/another_file'
path_spec = tsk_path_spec.TSKPathSpec(
inode=16, location=test_location, parent=self._os_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
self.assertEqual(file_entry.number_of_data_streams, 1)
data_stream_names = []
for data_stream in file_entry.data_streams:
data_stream_names.append(data_stream.name)
self.assertEqual(data_stream_names, [''])
test_location = '/a_directory'
path_spec = tsk_path_spec.TSKPathSpec(
inode=12, location=test_location, parent=self._os_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
self.assertEqual(file_entry.number_of_data_streams, 0)
data_stream_names = []
for data_stream in file_entry.data_streams:
data_stream_names.append(data_stream.name)
self.assertEqual(data_stream_names, [])
def testGetDataStream(self):
"""Tests the GetDataStream function."""
test_location = '/a_directory/another_file'
path_spec = tsk_path_spec.TSKPathSpec(
inode=16, location=test_location, parent=self._os_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
data_stream_name = ''
data_stream = file_entry.GetDataStream(data_stream_name)
self.assertIsNotNone(data_stream)
class TSKFileEntryTestHFS(unittest.TestCase):
"""Tests the SleuthKit (TSK) file entry on HFS."""
# TODO: implement.
@shared_test_lib.skipUnlessHasTestFile(['vsstest.qcow2'])
class TSKFileEntryTestNTFS(shared_test_lib.BaseTestCase):
"""Tests the SleuthKit (TSK) file entry on NTFS."""
def setUp(self):
"""Sets up the needed objects used throughout the test."""
self._resolver_context = context.Context()
test_file = self._GetTestFilePath(['vsstest.qcow2'])
path_spec = os_path_spec.OSPathSpec(location=test_file)
self._qcow_path_spec = qcow_path_spec.QCOWPathSpec(parent=path_spec)
self._tsk_path_spec = tsk_path_spec.TSKPathSpec(
location='\\', parent=self._qcow_path_spec)
self._file_system = tsk_file_system.TSKFileSystem(self._resolver_context)
self._file_system.Open(self._tsk_path_spec)
def tearDown(self):
"""Cleans up the needed objects used throughout the test."""
self._file_system.Close()
def testBackupTime(self):
"""Test the backup_time property."""
test_location = (
'\\System Volume Information\\{3808876b-c176-4e48-b7ae-04046e6cc752}')
path_spec = tsk_path_spec.TSKPathSpec(
inode=38, location=test_location, parent=self._qcow_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNone(file_entry.backup_time)
def testDeletionTime(self):
"""Test the deletion_time property."""
test_location = (
'\\System Volume Information\\{3808876b-c176-4e48-b7ae-04046e6cc752}')
path_spec = tsk_path_spec.TSKPathSpec(
inode=38, location=test_location, parent=self._qcow_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNone(file_entry.deletion_time)
def testGetStat(self):
"""Tests the GetStat function."""
test_location = (
'\\System Volume Information\\{3808876b-c176-4e48-b7ae-04046e6cc752}')
path_spec = tsk_path_spec.TSKPathSpec(
inode=38, location=test_location, parent=self._qcow_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
stat_object = file_entry.GetStat()
self.assertIsNotNone(stat_object)
self.assertEqual(stat_object.type, stat_object.TYPE_FILE)
self.assertEqual(stat_object.size, 65536)
self.assertEqual(stat_object.mode, 365)
self.assertEqual(stat_object.uid, 0)
self.assertEqual(stat_object.gid, 0)
self.assertEqual(stat_object.atime, 1386052509)
self.assertEqual(stat_object.atime_nano, 5023783)
self.assertEqual(stat_object.ctime, 1386052509)
self.assertEqual(stat_object.ctime_nano, 5179783)
self.assertEqual(stat_object.crtime, 1386052509)
self.assertEqual(stat_object.crtime_nano, 5023783)
self.assertEqual(stat_object.mtime, 1386052509)
self.assertEqual(stat_object.mtime_nano, 5179783)
def testAttributes(self):
"""Tests the number_of_attributes property."""
test_location = (
'\\System Volume Information\\{3808876b-c176-4e48-b7ae-04046e6cc752}')
path_spec = tsk_path_spec.TSKPathSpec(
inode=38, location=test_location, parent=self._qcow_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
self.assertEqual(file_entry.number_of_attributes, 4)
def testDataStream(self):
"""Tests the number_of_data_streams and data_streams properties."""
test_location = (
'\\System Volume Information\\{3808876b-c176-4e48-b7ae-04046e6cc752}')
path_spec = tsk_path_spec.TSKPathSpec(
inode=38, location=test_location, parent=self._qcow_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
self.assertEqual(file_entry.number_of_data_streams, 1)
data_stream_names = []
for data_stream in file_entry.data_streams:
data_stream_names.append(data_stream.name)
self.assertEqual(data_stream_names, [''])
path_spec = tsk_path_spec.TSKPathSpec(
inode=36, location='\\System Volume Information',
parent=self._qcow_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
self.assertEqual(file_entry.number_of_data_streams, 0)
data_stream_names = []
for data_stream in file_entry.data_streams:
data_stream_names.append(data_stream.name)
self.assertEqual(data_stream_names, [])
test_location = '\\$Extend\\$RmMetadata\\$Repair'
path_spec = tsk_path_spec.TSKPathSpec(
inode=28, location=test_location, parent=self._qcow_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
self.assertEqual(file_entry.number_of_data_streams, 2)
data_stream_names = []
for data_stream in file_entry.data_streams:
data_stream_names.append(data_stream.name)
self.assertEqual(sorted(data_stream_names), sorted(['', '$Config']))
def testGetDataStream(self):
"""Tests the retrieve data stream functionality."""
test_location = (
'\\System Volume Information\\{3808876b-c176-4e48-b7ae-04046e6cc752}')
path_spec = tsk_path_spec.TSKPathSpec(
inode=38, location=test_location, parent=self._qcow_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
data_stream_name = ''
data_stream = file_entry.GetDataStream(data_stream_name)
self.assertIsNotNone(data_stream)
self.assertEqual(data_stream.name, data_stream_name)
data_stream = file_entry.GetDataStream('bogus')
self.assertIsNone(data_stream)
test_location = '\\$Extend\\$RmMetadata\\$Repair'
path_spec = tsk_path_spec.TSKPathSpec(
inode=28, location=test_location, parent=self._qcow_path_spec)
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
self.assertIsNotNone(file_entry)
data_stream_name = '$Config'
data_stream = file_entry.GetDataStream(data_stream_name)
self.assertIsNotNone(data_stream)
self.assertEqual(data_stream.name, data_stream_name)
if __name__ == '__main__':
unittest.main()
|