Documentation
¶
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 ¶
- type API
- type WinAPI
- func (w *WinAPI) CloseHandle(h windows.Handle) error
- func (w *WinAPI) CloseToken(token windows.Token) error
- func (w *WinAPI) CreateToolhelp32Snapshot(flags uint32, processID uint32) (windows.Handle, error)
- func (w *WinAPI) GetTokenUser(token windows.Token) (*windows.Tokenuser, error)
- func (w *WinAPI) LookupAccount(sid *windows.SID, system string) (account string, domain string, accType uint32, err error)
- func (w *WinAPI) OpenProcess(desiredAccess uint32, inheritHandle bool, processID uint32) (windows.Handle, error)
- func (w *WinAPI) OpenProcessToken(process windows.Handle, desiredAccess uint32, token *windows.Token) error
- func (w *WinAPI) Process32First(snapshot windows.Handle, pe *windows.ProcessEntry32) error
- func (w *WinAPI) Process32Next(snapshot windows.Handle, pe *windows.ProcessEntry32) error
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 ¶
CloseHandle closes an open object handle.
func (*WinAPI) CloseToken ¶
CloseToken closes a token handle.
func (*WinAPI) CreateToolhelp32Snapshot ¶
CreateToolhelp32Snapshot takes a snapshot of the specified processes.
func (*WinAPI) GetTokenUser ¶
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 ¶
Process32First retrieves information about the first process in a snapshot.
func (*WinAPI) Process32Next ¶
Process32Next retrieves information about the next process in a snapshot.