Skip to content

Commit

Permalink
Zero enter disk in tests
Browse files Browse the repository at this point in the history
use the disk.create() function to seek to the end of the buffer
effectively zeroing the buffer so checksums will work before writing to
disk
  • Loading branch information
swysocki committed Nov 26, 2021
1 parent ed111ad commit 48e827d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
29 changes: 29 additions & 0 deletions pygpt_disk/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def _write_header(self, header: str = "primary") -> None:
# to the backup header
self._primary_header_lba.offset = 32
self._secondary_header_lba.offset = 24
# the backup header's partition array starts at LBA -33
self._partition_array_start.content = int(self.disk.sectors - 33)

self._write_section(self._header_sig, start_byte)
self._write_section(self._revision, start_byte)
Expand All @@ -91,6 +93,11 @@ def _write_header(self, header: str = "primary") -> None:
# once the header is written the CRC is calculated
self._checksum_header(start_byte)

# @TODO: write partition entries

# once the partitions are written the CRC is calculated
self._checksum_partitions(start_byte)

def _write_section(self, entry: HeaderEntry, buffer_position: int):
"""Write a GPT header entry
Expand Down Expand Up @@ -120,6 +127,28 @@ def _checksum_header(self, offset: int):
self._header_crc.content = binascii.crc32(raw_header)
self._write_section(self._header_crc, offset)

def _checksum_partitions(self, offset: int):
"""Write the partition checksum
Move to the start of the partition entries. Read the partition
entry array length * the partition entry size. CRC that value.
Args:
offset: start bytes of the header we are writing (primary or backup)
"""
# zero field before calculating
self._partition_array_crc.content = 0
self._write_section(self._partition_array_crc, offset)
# read partitions entries
self.disk.buffer.seek(
self._partition_array_start.content * self.disk.sector_size
)
raw_partitions = self.disk.buffer.read(
self._partition_array_length.content * self._partition_entry_size.content
)
self._partition_array_crc.content = binascii.crc32(raw_partitions)
self._write_section(self._partition_array_crc, offset)


if __name__ == "__main__":
disk = Disk(8 * 1024 * 1024, "/tmp/testgpt.img")
Expand Down
8 changes: 6 additions & 2 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_init(fresh_disk: disk.Disk):

def test__write_header(fresh_disk: disk.Disk):
t = table.Table(fresh_disk)
t.disk.create()

def read_header(primary=True):
assert t.disk.buffer.read(8) == SIGNATURE
Expand All @@ -51,14 +52,17 @@ def read_header(primary=True):
disk_guid = t.disk.buffer.read(16)
assert type(uuid.UUID(bytes_le=disk_guid)) == uuid.UUID
# partition array start LBA
assert t.disk.buffer.read(8) == (2).to_bytes(8, "little")
if primary:
assert t.disk.buffer.read(8) == (2).to_bytes(8, "little")
else:
assert t.disk.buffer.read(8) == (LAST_LBA - 33).to_bytes(8, "little")
# partition array length
assert t.disk.buffer.read(4) == (128).to_bytes(4, "little")
# partition entry length
assert t.disk.buffer.read(4) == (128).to_bytes(4, "little")
# partition array crc
# initially zeroed
assert t.disk.buffer.read(4) == (0).to_bytes(4, "little")
assert t.disk.buffer.read(4) != (0).to_bytes(4, "little")

# test primary header
t._write_header("primary")
Expand Down

0 comments on commit 48e827d

Please sign in to comment.