Skip to content
This repository was archived by the owner on Jul 26, 2023. It is now read-only.

Add unit test for DeviceIOControl #503

Merged
merged 4 commits into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Kernel32.Tests/storebanned/Kernel32Facts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,43 @@ unsafe uint GetProcessIdThreadProc(void* data)
GC.KeepAlive(threadStartRoutine); // Make sure that the delegate stays alive until the test is done
}

[Fact]
public unsafe void DeviceIOControl_Works()
{
const uint IOCTL_DISK_GET_DRIVE_GEOMETRY = 0x070000;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Common I/O codes are defined in https://docs.microsoft.com/en-us/windows/win32/api/winioctl/ , might make sense to add them to dotnet/pinvoke (as a separate effort)

const string drive = @"\\.\PhysicalDrive0";

using (var device = CreateFile(
filename: drive,
access: 0,
share: Kernel32.FileShare.FILE_SHARE_READ | Kernel32.FileShare.FILE_SHARE_WRITE,
securityAttributes: (SECURITY_ATTRIBUTES*)null,
creationDisposition: CreationDisposition.OPEN_EXISTING,
flagsAndAttributes: 0,
SafeObjectHandle.Null))
{
Assert.False(device.IsInvalid);

DISK_GEOMETRY pdg = default;

Assert.True(DeviceIoControl(
device,
(int)IOCTL_DISK_GET_DRIVE_GEOMETRY,
null,
0,
&pdg,
sizeof(DISK_GEOMETRY),
out int pBytesReturned,
(OVERLAPPED*)null));

Assert.NotEqual(0u, pdg.BytesPerSector);
Assert.NotEqual(0, pdg.Cylinders);
Assert.Equal(MEDIA_TYPE.FixedMedia, pdg.MediaType);
Assert.NotEqual(0u, pdg.SectorsPerTrack);
Assert.NotEqual(0u, pdg.TracksPerCylinder);
}
}

/// <summary>
/// Helper for <see cref="CreateThread_Test"/>, <see cref="CreateRemoteThread_PseudoTest"/> and
/// <see cref="CreateRemoteThreadEx_PseudoTest"/> tests.
Expand Down
34 changes: 34 additions & 0 deletions src/Kernel32/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ PInvoke.Kernel32.CreateThreadFlags
PInvoke.Kernel32.CreateThreadFlags.CREATE_SUSPENDED = 4 -> PInvoke.Kernel32.CreateThreadFlags
PInvoke.Kernel32.CreateThreadFlags.None = 0 -> PInvoke.Kernel32.CreateThreadFlags
PInvoke.Kernel32.CreateThreadFlags.STACK_SIZE_PARAM_IS_A_RESERVATION = 65536 -> PInvoke.Kernel32.CreateThreadFlags
PInvoke.Kernel32.DISK_GEOMETRY
PInvoke.Kernel32.DISK_GEOMETRY.BytesPerSector -> uint
PInvoke.Kernel32.DISK_GEOMETRY.Cylinders -> long
PInvoke.Kernel32.DISK_GEOMETRY.DISK_GEOMETRY() -> void
PInvoke.Kernel32.DISK_GEOMETRY.MediaType -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.DISK_GEOMETRY.SectorsPerTrack -> uint
PInvoke.Kernel32.DISK_GEOMETRY.TracksPerCylinder -> uint
PInvoke.Kernel32.FileSystemFlags
PInvoke.Kernel32.FileSystemFlags.FILE_CASE_PRESERVED_NAMES = 2 -> PInvoke.Kernel32.FileSystemFlags
PInvoke.Kernel32.FileSystemFlags.FILE_CASE_SENSITIVE_SEARCH = 1 -> PInvoke.Kernel32.FileSystemFlags
Expand All @@ -32,6 +39,33 @@ PInvoke.Kernel32.FileSystemFlags.FILE_SUPPORTS_USN_JOURNAL = 33554432 -> PInvoke
PInvoke.Kernel32.FileSystemFlags.FILE_UNICODE_ON_DISK = 4 -> PInvoke.Kernel32.FileSystemFlags
PInvoke.Kernel32.FileSystemFlags.FILE_VOLUME_IS_COMPRESSED = 32768 -> PInvoke.Kernel32.FileSystemFlags
PInvoke.Kernel32.FileSystemFlags.FILE_VOLUME_QUOTAS = 32 -> PInvoke.Kernel32.FileSystemFlags
PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F3_120M_512 = 13 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F3_128Mb_512 = 20 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F3_1Pt23_1024 = 18 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F3_1Pt2_512 = 17 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F3_1Pt44_512 = 2 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F3_200Mb_512 = 23 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F3_20Pt8_512 = 4 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F3_230Mb_512 = 21 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F3_240M_512 = 24 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F3_2Pt88_512 = 3 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F3_32M_512 = 25 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F3_640_512 = 14 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F3_720_512 = 5 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F5_160_512 = 10 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F5_180_512 = 9 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F5_1Pt23_1024 = 19 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F5_1Pt2_512 = 1 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F5_320_1024 = 8 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F5_320_512 = 7 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F5_360_512 = 6 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F5_640_512 = 15 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F5_720_512 = 16 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.F8_256_128 = 22 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.FixedMedia = 12 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.RemovableMedia = 11 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEDIA_TYPE.Unknown = 0 -> PInvoke.Kernel32.MEDIA_TYPE
PInvoke.Kernel32.MEMORY_PRIORITY_INFORMATION
PInvoke.Kernel32.MEMORY_PRIORITY_INFORMATION.MEMORY_PRIORITY_INFORMATION() -> void
PInvoke.Kernel32.MEMORY_PRIORITY_INFORMATION.MemoryPriority -> PInvoke.Kernel32.MemoryPriority
Expand Down
43 changes: 43 additions & 0 deletions src/Kernel32/storebanned/Kernel32+DISK_GEOMETRY.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright © .NET Foundation and Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace PInvoke
{
/// <content>
/// Contains the <see cref="DISK_GEOMETRY"/> nested type.
/// </content>
public partial class Kernel32
{
/// <summary>
/// Describes the geometry of disk devices and media.
/// </summary>
/// <seealso href="https://docs.microsoft.com/en-us/windows/win32/api/winioctl/ns-winioctl-disk_geometry"/>
public struct DISK_GEOMETRY
{
/// <summary>
/// The number of cylinders.
/// </summary>
public long Cylinders;

/// <summary>
/// The type of media.
/// </summary>
public MEDIA_TYPE MediaType;

/// <summary>
/// The number of tracks per cylinder.
/// </summary>
public uint TracksPerCylinder;

/// <summary>
/// The number of sectors per track.
/// </summary>
public uint SectorsPerTrack;

/// <summary>
/// The number of bytes per sector.
/// </summary>
public uint BytesPerSector;
}
}
}
148 changes: 148 additions & 0 deletions src/Kernel32/storebanned/Kernel32+MEDIA_TYPE.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright © .NET Foundation and Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace PInvoke
{
/// <content>
/// Contains the <see cref="MEDIA_TYPE"/> nested type.
/// </content>
public partial class Kernel32
{
/// <summary>
/// Represents the various forms of device media.
/// </summary>
/// <seealso href="https://docs.microsoft.com/en-us/windows/win32/api/winioctl/ne-winioctl-media_type"/>
public enum MEDIA_TYPE
{
/// <summary>
/// Format is unknown
/// </summary>
Unknown,

/// <summary>
/// A 5.25" floppy, with 1.2MB and 512 bytes/sector.
/// </summary>
F5_1Pt2_512,

/// <summary>
/// A 3.5" floppy, with 1.44MB and 512 bytes/sector.
/// </summary>
F3_1Pt44_512,

/// <summary>
/// A 3.5" floppy, with 2.88MB and 512 bytes/sector.
/// </summary>
F3_2Pt88_512,

/// <summary>
/// A 3.5" floppy, with 20.8MB and 512 bytes/sector.
/// </summary>
F3_20Pt8_512,

/// <summary>
/// A 3.5" floppy, with 720KB and 512 bytes/sector.
/// </summary>
F3_720_512,

/// <summary>
/// A 5.25" floppy, with 360KB and 512 bytes/sector.
/// </summary>
F5_360_512,

/// <summary>
/// A 5.25" floppy, with 320KB and 512 bytes/sector.
/// </summary>
F5_320_512,

/// <summary>
/// A 5.25" floppy, with 320KB and 1024 bytes/sector.
/// </summary>
F5_320_1024,

/// <summary>
/// A 5.25" floppy, with 180KB and 512 bytes/sector.
/// </summary>
F5_180_512,

/// <summary>
/// A 5.25" floppy, with 160KB and 512 bytes/sector.
/// </summary>
F5_160_512,

/// <summary>
/// Removable media other than floppy.
/// </summary>
RemovableMedia,

/// <summary>
/// Fixed hard disk media.
/// </summary>
FixedMedia,

/// <summary>
/// A 3.5" floppy, with 120MB and 512 bytes/sector.
/// </summary>
F3_120M_512,

/// <summary>
/// A 3.5" floppy, with 640KB and 512 bytes/sector.
/// </summary>
F3_640_512,

/// <summary>
/// A 5.25" floppy, with 640KB and 512 bytes/sector.
/// </summary>
F5_640_512,

/// <summary>
/// A 5.25" floppy, with 720KB and 512 bytes/sector.
/// </summary>
F5_720_512,

/// <summary>
/// A 3.5" floppy, with 1.2MB and 512 bytes/sector.
/// </summary>
F3_1Pt2_512,

/// <summary>
/// A 3.5" floppy, with 1.23MB and 1024 bytes/sector.
/// </summary>
F3_1Pt23_1024,

/// <summary>
/// A 5.25" floppy, with 1.23MB and 1024 bytes/sector.
/// </summary>
F5_1Pt23_1024,

/// <summary>
/// A 3.5" floppy, with 128MB and 512 bytes/sector.
/// </summary>
F3_128Mb_512,

/// <summary>
/// A 3.5" floppy, with 230MB and 512 bytes/sector.
/// </summary>
F3_230Mb_512,

/// <summary>
/// An 8" floppy, with 256KB and 128 bytes/sector.
/// </summary>
F8_256_128,

/// <summary>
/// A 3.5" floppy, with 200MB and 512 bytes/sector. (HiFD).
/// </summary>
F3_200Mb_512,

/// <summary>
/// A 3.5" floppy, with 240MB and 512 bytes/sector. (HiFD).
/// </summary>
F3_240M_512,

/// <summary>
/// A 3.5" floppy, with 32MB and 512 bytes/sector.
/// </summary>
F3_32M_512,
}
}
}