package host import "testing" func TestIsWindowsRawDevicePath(t *testing.T) { cases := []struct { path string want bool }{ {`\\?\Volume{11111111-2222-3333-4444-555555555555}`, true}, {`//?/volume{11111111-2222-3333-4444-555555555555}`, true}, {`\\.\PhysicalDrive1`, true}, {`\\.\HarddiskVolume3`, true}, {`\\.\Harddisk1Partition2`, true}, {`\\.\Volume{11111111-2222-3333-4444-555555555555}`, true}, {`\\?\C:\VMs\macOS\disk.qcow2`, false}, {`\\?\UNC\server\share\disk.qcow2`, false}, {`D:\ReimsData\disk.qcow2`, false}, {``, false}, } for _, tc := range cases { if got := IsWindowsRawDevicePath(tc.path); got != tc.want { t.Errorf("IsWindowsRawDevicePath(%q) = %v, want %v", tc.path, got, tc.want) } } } func TestFilterRawPartitionCandidatesSafety(t *testing.T) { obs := []RawVolumeObservation{ {DevicePath: `\\?\Volume{os-c}`, DiskNumber: 0, PartitionNumber: 3, SizeBytes: 200 << 30, HasMountPoints: true, Filesystem: "NTFS"}, {DevicePath: `\\?\Volume{os-efi}`, DiskNumber: 0, PartitionNumber: 1, SizeBytes: 260 << 20, PartitionTypeGUID: "c12a7328-f81f-11d2-ba4b-00a0c93ec93b"}, {DevicePath: `\\?\Volume{data-apfs}`, DiskNumber: 1, PartitionNumber: 2, SizeBytes: 256 << 30, PartitionTypeGUID: "7c3457ef-0000-11aa-aa11-00306543ecac"}, {DevicePath: `\\?\Volume{mounted-ntfs}`, DiskNumber: 2, PartitionNumber: 2, SizeBytes: 500 << 30, Filesystem: "NTFS"}, {DevicePath: `\\?\Volume{efi-disk2}`, DiskNumber: 2, PartitionNumber: 1, SizeBytes: 200 << 20, PartitionTypeGUID: "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"}, {DevicePath: `\\?\Volume{other-raw}`, DiskNumber: 2, PartitionNumber: 3, SizeBytes: 128 << 30, PartitionTypeGUID: "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7"}, {DevicePath: `\\.\PhysicalDrive3`, DiskNumber: 3, SizeBytes: 512 << 30, Offline: true}, } excluded := map[uint32]bool{0: true} got := filterRawPartitionCandidates(obs, excluded) if len(got) != 3 { t.Fatalf("got %d candidates %+v, want 3", len(got), got) } if !got[0].IsAPFS || got[0].DevicePath != `\\?\Volume{data-apfs}` { t.Fatalf("expected APFS partition sorted first, got %+v", got[0]) } if got[1].DevicePath != `\\?\Volume{other-raw}` { t.Fatalf("unexpected second candidate: %+v", got[1]) } if !got[2].Offline || got[2].DevicePath != `\\.\PhysicalDrive3` { t.Fatalf("unexpected third candidate: %+v", got[2]) } } func TestApplyPartitionExtentMatches(t *testing.T) { extents := []indexedVolumeExtent{ { start: 1 << 20, end: 100 << 20, volDev: `\\?\Volume{spanned}`, multiExtent: true, occupied: true, }, { start: 200 << 20, end: 300 << 20, volDev: `\\?\Volume{probe-err}`, occupied: true, }, { start: 400 << 20, end: 500 << 20, volDev: `\\?\Volume{apfs}`, }, } // Overlapping a multi-extent volume excludes the partition fail-closed. spannedObs := RawVolumeObservation{DevicePath: `\\.\Harddisk1Partition1`, DiskNumber: 1, PartitionNumber: 1, SizeBytes: 99 << 20} applyPartitionExtentMatches(&spannedObs, 1<<20, 99<<20, extents) if !spannedObs.HasMountPoints { t.Fatalf("expected spanned volume overlap to mark partition occupied") } // Probe error on the volume also marks it occupied fail-closed. errObs := RawVolumeObservation{DevicePath: `\\.\Harddisk1Partition2`, DiskNumber: 1, PartitionNumber: 2, SizeBytes: 100 << 20} applyPartitionExtentMatches(&errObs, 200<<20, 100<<20, extents) if !errObs.HasMountPoints { t.Fatalf("expected volume probe error to mark partition occupied") } // Clean unmounted APFS partition passes through with resolved volume device path. apfsObs := RawVolumeObservation{DevicePath: `\\.\Harddisk1Partition3`, DiskNumber: 1, PartitionNumber: 3, SizeBytes: 100 << 20, PartitionTypeGUID: AppleAPFSPartitionTypeGUID} applyPartitionExtentMatches(&apfsObs, 400<<20, 100<<20, extents) if apfsObs.HasMountPoints || apfsObs.DevicePath != `\\?\Volume{apfs}` { t.Fatalf("unexpected APFS observation after match: %+v", apfsObs) } got := filterRawPartitionCandidates([]RawVolumeObservation{spannedObs, errObs, apfsObs}, nil) if len(got) != 1 || got[0].DevicePath != `\\?\Volume{apfs}` { t.Fatalf("expected only clean APFS candidate, got %+v", got) } } func TestInspectDiskUsageSkipsRawDevice(t *testing.T) { du, err := InspectDiskUsage(`\\.\Harddisk1Partition2`) if err != nil { t.Fatalf("InspectDiskUsage on raw device returned error: %v", err) } if du.Format != "raw" || du.Path != `\\.\Harddisk1Partition2` { t.Fatalf("unexpected DiskUsage for raw device: %+v", du) } }