windows

package
v0.15.0-rc.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Rendered for windows/amd64

Overview

Package windows provides an abstraction layer over Windows API calls to enable better testability.

Purpose

The API interface abstracts Windows system calls, allowing them to be mocked in tests. This is particularly useful for testing code that interacts with processes, tokens, and security identifiers without requiring actual Windows system resources.

Usage

## Production Code

In production code, use the NewWindowsAPI() function to get the real implementation:

import (
	"github.com/Microsoft/hcsshim/internal/windows"
)

func MyFunction(ctx context.Context) error {
	winAPI := &windows.WinAPI{}
	// Use winAPI instead of calling windows package directly
	snapshot, err := winAPI.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
	// ...
}

## Testing

In tests, create a mock implementation of the API interface. Here's a simple example:

// Function to test
func GetProcessSnapshot(api windows.API) (windows.Handle, error) {
	snapshot, err := api.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
	if err != nil {
		return 0, err
	}
	return snapshot, nil
}

// Mock implementation for testing
type mockAPI struct {
	snapshot windows.Handle
	err      error
}

func (m *mockAPI) CreateToolhelp32Snapshot(flags uint32, processID uint32) (windows.Handle, error) {
	return m.snapshot, m.err
}

func (m *mockAPI) CloseHandle(h windows.Handle) error {
	return nil
}

// ... implement other API methods as needed ...

// Test using the mock
func TestGetProcessSnapshot(t *testing.T) {
	mockWinAPI := &mockAPI{
		snapshot: windows.Handle(12345),
		err:      nil,
	}

	snapshot, err := GetProcessSnapshot(mockWinAPI)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if snapshot != windows.Handle(12345) {
		t.Fatalf("expected snapshot 12345, got %v", snapshot)
	}
}

All method names match the underlying Windows API calls for easy understanding and reference.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type API

type API interface {
	// CreateToolhelp32Snapshot takes a snapshot of the specified processes.
	CreateToolhelp32Snapshot(flags uint32, processID uint32) (windows.Handle, error)

	// CloseHandle closes an open object handle.
	CloseHandle(h windows.Handle) error

	// Process32First retrieves information about the first process in a snapshot.
	Process32First(snapshot windows.Handle, pe *windows.ProcessEntry32) error

	// Process32Next retrieves information about the next process in a snapshot.
	Process32Next(snapshot windows.Handle, pe *windows.ProcessEntry32) error

	// OpenProcess opens an existing local process object.
	OpenProcess(desiredAccess uint32, inheritHandle bool, processID uint32) (windows.Handle, error)

	// OpenProcessToken opens the access token associated with a process.
	OpenProcessToken(process windows.Handle, desiredAccess uint32, token *windows.Token) error

	// GetTokenUser retrieves the user account of the token.
	GetTokenUser(token windows.Token) (*windows.Tokenuser, error)

	// LookupAccount retrieves the name of the account for a SID and the name of the first domain on which this SID is found.
	LookupAccount(sid *windows.SID, system string) (account string, domain string, accType uint32, err error)

	// CloseToken closes a token handle.
	CloseToken(token windows.Token) error
}

API abstracts Windows API calls for testing purposes.

type WinAPI

type WinAPI struct{}

WinAPI is the real implementation of windows.API that delegates to the actual Windows API.

func (*WinAPI) CloseHandle

func (w *WinAPI) CloseHandle(h windows.Handle) error

CloseHandle closes an open object handle.

func (*WinAPI) CloseToken

func (w *WinAPI) CloseToken(token windows.Token) error

CloseToken closes a token handle.

func (*WinAPI) CreateToolhelp32Snapshot

func (w *WinAPI) CreateToolhelp32Snapshot(flags uint32, processID uint32) (windows.Handle, error)

CreateToolhelp32Snapshot takes a snapshot of the specified processes.

func (*WinAPI) GetTokenUser

func (w *WinAPI) GetTokenUser(token windows.Token) (*windows.Tokenuser, error)

GetTokenUser retrieves the user account of the token.

func (*WinAPI) LookupAccount

func (w *WinAPI) LookupAccount(sid *windows.SID, system string) (account string, domain string, accType uint32, err error)

LookupAccount retrieves the name of the account for a SID and the name of the first domain on which this SID is found.

func (*WinAPI) OpenProcess

func (w *WinAPI) OpenProcess(desiredAccess uint32, inheritHandle bool, processID uint32) (windows.Handle, error)

OpenProcess opens an existing local process object.

func (*WinAPI) OpenProcessToken

func (w *WinAPI) OpenProcessToken(process windows.Handle, desiredAccess uint32, token *windows.Token) error

OpenProcessToken opens the access token associated with a process.

func (*WinAPI) Process32First

func (w *WinAPI) Process32First(snapshot windows.Handle, pe *windows.ProcessEntry32) error

Process32First retrieves information about the first process in a snapshot.

func (*WinAPI) Process32Next

func (w *WinAPI) Process32Next(snapshot windows.Handle, pe *windows.ProcessEntry32) error

Process32Next retrieves information about the next process in a snapshot.

Directories

Path Synopsis
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL