def test_partition_in_partition(): #TODO Remove "+" on DFXML Schema 1.3.0 tracking. dobj = Objects.DFXMLObject(version="1.2.0+") psobj = Objects.PartitionSystemObject() psobj.pstype_str = "mbr" dobj.append(psobj) pobj_outer = Objects.PartitionObject() psobj.append(pobj_outer) pobj_inner = Objects.PartitionObject() pobj_outer.append(pobj_inner) # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: psobj_reconst = dobj_reconst.partition_systems[0] pobj_outer_reconst = psobj_reconst.partitions[0] pobj_inner_reconst = pobj_outer_reconst.partitions[0] assert isinstance(pobj_inner_reconst, Objects.PartitionObject) except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def test_error_after_file(): #TODO Bump version when feature branch merged into schema. dobj = Objects.DFXMLObject(version="1.2.0+") diobj = Objects.DiskImageObject() dobj.append(diobj) diobj.error = ERROR_1 fobj = Objects.FileObject() fobj.alloc_inode = False fobj.alloc_name = False fobj.error = ERROR_2 diobj.append(fobj) # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: diobj_reconst = dobj_reconst.disk_images[0] fobj_reconst = diobj_reconst.files[0] assert diobj_reconst.error == ERROR_1 assert fobj_reconst.error == ERROR_2 except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def test_disk_image_in_file_system(): dobj = Objects.DFXMLObject(version="1.2.0") vobj = Objects.VolumeObject() vobj.ftype_str = "iso9660" dobj.append(vobj) fobj_vobj = Objects.FileObject() fobj_vobj.sha512 = TEST_HASH_1 vobj.append(fobj_vobj) diobj = Objects.DiskImageObject() vobj.append(diobj) fobj_diobj = Objects.FileObject() fobj_diobj.alloc_inode = False fobj_diobj.alloc_name = False fobj_diobj.sha512 = TEST_HASH_2 diobj.append(fobj_diobj) # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: vobj_reconst = dobj_reconst.volumes[0] diobj_reconst = vobj_reconst.disk_images[0] assert vobj_reconst.files[0].sha512 == TEST_HASH_1 assert diobj_reconst.files[0].sha512 == TEST_HASH_2 except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def test_volume_error_roundtrip_with_file(): dobj = Objects.DFXMLObject(version="1.2.0") vobj = Objects.VolumeObject() dobj.append(vobj) vobj.error = ERROR_STRING_V assert vobj.error == ERROR_STRING_V fobj = Objects.FileObject() vobj.append(fobj) fobj.error = ERROR_STRING_F assert fobj.error == ERROR_STRING_F assert vobj.error == ERROR_STRING_V # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: vobj_reconst = dobj_reconst.volumes[0] fobj_reconst = vobj_reconst.files[0] assert vobj_reconst.error == ERROR_STRING_V assert fobj_reconst.error == ERROR_STRING_F except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def test_error_after_partition_system(): #TODO Bump version when feature branch merged into schema. dobj = Objects.DFXMLObject(version="1.2.0+") diobj = Objects.DiskImageObject() dobj.append(diobj) diobj.error = ERROR_1 psobj = Objects.PartitionSystemObject() #TODO This should be uncommented after the branch add_partition_system_error is merged. #psobj.error = ERROR_2 diobj.append(psobj) # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: diobj_reconst = dobj_reconst.disk_images[0] #TODO This should be uncommented after the branch add_partition_system_error is merged. #psobj_reconst = diobj_reconst.partitionsystems[0] assert diobj_reconst.error == ERROR_1 #TODO This should be uncommented after the branch add_partition_system_error is merged. #assert psobj_reconst.error == ERROR_2 except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def test_error_element_order(): #TODO When schema 1.3.0 is released, update version. dobj = Objects.DFXMLObject(version="1.2.0+") psobj = Objects.PartitionSystemObject() fobj = Objects.FileObject() psobj.pstype_str = "gpt" # The error element should come after the fileobject stream. psobj.error = "foo" # Add a unallocated file object found floating in the partition system. fobj.alloc_inode = False fobj.alloc_name = False dobj.append(psobj) psobj.append(fobj) el = dobj.to_Element() # Confirm error comes after file stream. assert el[-1][0].tag.endswith("pstype_str") assert el[-1][-2].tag.endswith("fileobject") assert el[-1][-1].tag.endswith("error") # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) psobj_reconst = dobj_reconst.partition_systems[0] try: assert psobj_reconst.pstype_str == "gpt" assert psobj_reconst.error == "foo" except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def test_empty_object(): dobj = Objects.DFXMLObject(version="1.2.0") vobj = Objects.VolumeObject() dobj.append(vobj) # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: vobj_reconst = dobj_reconst.volumes[0] except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def test_empty_object(): dobj = Objects.DFXMLObject(version="1.2.0") psobj = Objects.PartitionSystemObject() dobj.append(psobj) # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: psobj_reconst = dobj_reconst.partition_systems[0] except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def test_sector_size(): dobj = Objects.DFXMLObject(version="1.2.0") diobj = Objects.DiskImageObject() dobj.append(diobj) diobj.sector_size = 2048 # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: diobj_reconst = dobj_reconst.disk_images[0] assert diobj_reconst.sector_size == 2048 assert diobj.sector_size == diobj_reconst.sector_size except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def test_solaris_ps_in_partition(): dobj = Objects.DFXMLObject(version="1.2.0") psobj_outer = Objects.PartitionSystemObject() dobj.append(psobj_outer) # Add file to outer partition system. fobj_psobj_outer = Objects.FileObject() fobj_psobj_outer.alloc_inode = False fobj_psobj_outer.alloc_name = False fobj_psobj_outer.sha512 = TEST_HASH_1 psobj_outer.append(fobj_psobj_outer) pobj = Objects.PartitionObject() psobj_outer.append(pobj) # Add file to partition. fobj_pobj = Objects.FileObject() fobj_pobj.alloc_inode = False fobj_pobj.alloc_name = False fobj_pobj.sha512 = TEST_HASH_2 pobj.append(fobj_pobj) psobj_inner = Objects.PartitionSystemObject() pobj.append(psobj_inner) # Add file to inner partition system. fobj_psobj_inner = Objects.FileObject() fobj_psobj_inner.alloc_inode = False fobj_psobj_inner.alloc_name = False fobj_psobj_inner.sha512 = TEST_HASH_3 psobj_inner.append(fobj_psobj_inner) # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: psobj_outer_reconst = dobj_reconst.partition_systems[0] pobj_reconst = psobj_outer_reconst.partitions[0] psobj_inner_reconst = pobj_reconst.partition_systems[0] assert psobj_outer_reconst.files[0].sha512 == TEST_HASH_1 assert pobj_reconst.files[0].sha512 == TEST_HASH_2 assert psobj_inner_reconst.files[0].sha512 == TEST_HASH_3 except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def test_hash_properties(): dobj = Objects.DFXMLObject(version="1.2.0") fobj = Objects.FileObject() dobj.append(fobj) fobj.byte_runs = Objects.ByteRuns() br = Objects.ByteRun() fobj.byte_runs.append(br) fobj.filesize = len(TEST_BYTE_STRING) br.len = len(TEST_BYTE_STRING) hash_functions = {"md5", "sha1", "sha224", "sha256", "sha384", "sha512"} # Key: Hash function. # Value: Hash of the byte string b"test". hash_values = dict() for hash_function in sorted(hash_functions): hash_object = getattr(hashlib, hash_function)() hash_object.update(TEST_BYTE_STRING) hash_values[hash_function] = hash_object.hexdigest() _logger.debug("hash_values[%r] = %r." % (hash_function, hash_values[hash_function])) setattr(fobj, hash_function, hash_values[hash_function]) setattr(br, hash_function, hash_values[hash_function]) assert getattr(fobj, hash_function) == hash_values[hash_function] assert getattr(br, hash_function) == hash_values[hash_function] # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: fobj_reconst = dobj_reconst.files[0] br_reconst = fobj_reconst.byte_runs[0] for hash_function in sorted(hash_functions): assert getattr(fobj_reconst, hash_function) == hash_values[hash_function] assert getattr(br_reconst, hash_function) == hash_values[hash_function] except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def test_cfreds_macwd_properties(): """ These were drawn from a CFReDS sample Mac disk image. """ dobj = Objects.DFXMLObject(version="1.2.0") pobj = Objects.PartitionObject() dobj.append(pobj) pobj.ptype_str = "Apple_Boot" pobj.partition_index = 8 # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: pobj_reconst = dobj_reconst.partitions[0] assert pobj_reconst.ptype_str == "Apple_Boot" assert pobj_reconst.partition_index == "8" except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def test_volume_error_roundtrip_with_file_and_extns(): dobj = Objects.DFXMLObject(version="1.2.0") vobj = Objects.VolumeObject() dobj.append(vobj) ET.register_namespace("testextra", XMLNS_TEST_EXTRA) vobj.error = ERROR_STRING_V # Dummy up a non-DFXML namespace element. This should be appendable. e = ET.Element("{%s}extra_element" % XMLNS_TEST_EXTRA) e.text = "Extra content" vobj.externals.append(e) # Dummy up a non-DFXML namespace 'error' element. This should be appendable. e = ET.Element("{%s}error" % XMLNS_TEST_EXTRA) e.text = "Extra error" vobj.externals.append(e) assert vobj.error == ERROR_STRING_V fobj = Objects.FileObject() vobj.append(fobj) fobj.error = ERROR_STRING_F assert fobj.error == ERROR_STRING_F assert vobj.error == ERROR_STRING_V # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: vobj_reconst = dobj_reconst.volumes[0] fobj_reconst = vobj_reconst.files[0] assert vobj_reconst.error == ERROR_STRING_V assert fobj_reconst.error == ERROR_STRING_F except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def test_hfsplus_in_hfs(): dobj = Objects.DFXMLObject(version="1.2.0") vobj_outer = Objects.VolumeObject() vobj_outer.ftype_str = "hfs" dobj.append(vobj_outer) vobj_inner = Objects.VolumeObject() vobj_inner.ftype_str = "hfsplus" vobj_outer.append(vobj_inner) # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: vobj_outer_reconst = dobj_reconst.volumes[0] vobj_inner_reconst = vobj_outer_reconst.volumes[0] assert isinstance(vobj_inner_reconst, Objects.VolumeObject) assert vobj_outer_reconst.ftype_str == "hfs" assert vobj_inner_reconst.ftype_str == "hfsplus" except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def test_bsd_disklabel_properties(): """ These were drawn from a BSD Disk Label sample image. """ dobj = Objects.DFXMLObject(version="1.2.0") pobj_a = Objects.PartitionObject() pobj_c = Objects.PartitionObject() dobj.append(pobj_a) dobj.append(pobj_c) pobj_a.partition_index = "a" pobj_c.partition_index = "c" # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: pobj_a_reconst = dobj_reconst.partitions[0] pobj_c_reconst = dobj_reconst.partitions[1] assert pobj_a_reconst.partition_index == "a" assert pobj_c_reconst.partition_index == "c" except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def _test_file_in_non_fs_levels_deep(include_disk_image, include_partition_system, include_partition, include_file_system): """ This test follows a simple, vertical storage layer stack, but adds a file at each layer. """ dobj = Objects.DFXMLObject(version="1.2.0") # Add file to top-level document. fobj_dobj = Objects.FileObject() fobj_dobj.alloc_inode = False fobj_dobj.alloc_name = False fobj_dobj.sha512 = TEST_HASH_1 dobj.append(fobj_dobj) appender_stack = [dobj] if include_disk_image: # Add disk image to top-level document. diobj = Objects.DiskImageObject() appender_stack[-1].append(diobj) appender_stack.append(diobj) # Add file to disk image. fobj_diobj = Objects.FileObject() fobj_diobj.alloc_inode = False fobj_diobj.alloc_name = False fobj_diobj.sha512 = TEST_HASH_2 diobj.append(fobj_diobj) if include_partition_system: # Add partition system to disk image. psobj = Objects.PartitionSystemObject() appender_stack[-1].append(psobj) appender_stack.append(psobj) # Add file to partition system. fobj_psobj = Objects.FileObject() fobj_psobj.alloc_inode = False fobj_psobj.alloc_name = False fobj_psobj.sha512 = TEST_HASH_3 psobj.append(fobj_psobj) if include_partition: # Add partition to partition system, but not disk image. if not (include_disk_image and not include_partition_system): pobj = Objects.PartitionObject() appender_stack[-1].append(pobj) appender_stack.append(pobj) # Add file to partition. fobj_pobj = Objects.FileObject() fobj_pobj.alloc_inode = False fobj_pobj.alloc_name = False fobj_pobj.sha512 = TEST_HASH_4 pobj.append(fobj_pobj) if include_file_system: # Add file system to anything but a partition system. if not (include_partition_system and not include_partition): vobj = Objects.VolumeObject() appender_stack[-1].append(vobj) appender_stack.append(vobj) # Add file to file system. fobj_vobj = Objects.FileObject() fobj_vobj.sha512 = TEST_HASH_5 vobj.append(fobj_vobj) # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: container_stack = [dobj_reconst] assert dobj_reconst.files[0].sha512 == TEST_HASH_1 if include_disk_image: diobj_reconst = container_stack[-1].disk_images[0] container_stack.append(diobj_reconst) assert diobj_reconst.files[0].sha512 == TEST_HASH_2 if include_partition_system: psobj_reconst = container_stack[-1].partition_systems[0] container_stack.append(psobj_reconst) assert psobj_reconst.files[0].sha512 == TEST_HASH_3 if include_partition: if not (include_disk_image and not include_partition_system): pobj_reconst = container_stack[-1].partitions[0] container_stack.append(pobj_reconst) assert pobj_reconst.files[0].sha512 == TEST_HASH_4 if include_file_system: if not (include_partition_system and not include_partition): vobj_reconst = container_stack[-1].volumes[0] assert vobj_reconst.files[0].sha512 == TEST_HASH_5 except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)
def _test_file_in_non_fs_levels_flat(include_disk_image, include_partition_system, include_partition, include_file_system): """ This test follows a simple, horizontal storage layer stack (every container attached to top document object), and adds a file for each container. """ dobj = Objects.DFXMLObject(version="1.2.0") # Add file to top-level document. fobj_dobj = Objects.FileObject() fobj_dobj.alloc_inode = False fobj_dobj.alloc_name = False fobj_dobj.sha512 = TEST_HASH_1 dobj.append(fobj_dobj) if include_disk_image: # Add disk image. diobj = Objects.DiskImageObject() dobj.append(diobj) # Add file to disk image. fobj_diobj = Objects.FileObject() fobj_diobj.alloc_inode = False fobj_diobj.alloc_name = False fobj_diobj.sha512 = TEST_HASH_2 diobj.append(fobj_diobj) if include_partition_system: # Add partition system. psobj = Objects.PartitionSystemObject() dobj.append(psobj) # Add file to partition system. fobj_psobj = Objects.FileObject() fobj_psobj.alloc_inode = False fobj_psobj.alloc_name = False fobj_psobj.sha512 = TEST_HASH_3 psobj.append(fobj_psobj) if include_partition: # Add partition. pobj = Objects.PartitionObject() dobj.append(pobj) # Add file to partition. fobj_pobj = Objects.FileObject() fobj_pobj.alloc_inode = False fobj_pobj.alloc_name = False fobj_pobj.sha512 = TEST_HASH_4 pobj.append(fobj_pobj) if include_file_system: # Add file system. vobj = Objects.VolumeObject() dobj.append(vobj) # Add file to file system. fobj_vobj = Objects.FileObject() fobj_vobj.sha512 = TEST_HASH_5 vobj.append(fobj_vobj) # Do file I/O round trip. (tmp_filename, dobj_reconst) = libtest.file_round_trip_dfxmlobject(dobj) try: assert dobj_reconst.files[0].sha512 == TEST_HASH_1 if include_disk_image: diobj_reconst = dobj_reconst.disk_images[0] assert diobj_reconst.files[0].sha512 == TEST_HASH_2 if include_partition_system: psobj_reconst = dobj_reconst.partition_systems[0] assert psobj_reconst.files[0].sha512 == TEST_HASH_3 if include_partition: pobj_reconst = dobj_reconst.partitions[0] assert pobj_reconst.files[0].sha512 == TEST_HASH_4 if include_file_system: vobj_reconst = dobj_reconst.volumes[0] assert vobj_reconst.files[0].sha512 == TEST_HASH_5 except: _logger.debug("tmp_filename = %r." % tmp_filename) raise os.remove(tmp_filename)