mocking

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package mocking contains different Windows mocking implementations

Currently contains mocking implementations for: Windows version, Windows registry, and command execution

Index

Constants

This section is empty.

Variables

Functions

func CheckKey

func CheckKey(key RegistryKey, elem string) string

CheckKey retrieves the value of a specified element within a given registry key.

Parameters:

  • key: A RegistryKey object representing the registry key to be checked.
  • elem: A string representing the name of the element whose value is to be retrieved.

Returns:

  • A string representing the value of the specified element within the registry key. If the element does not exist or an error occurs while retrieving its value, the function returns "-1".

Note: This function is designed to handle the retrieval of values from the registry. It encapsulates the process of accessing the registry and retrieving a value, providing a simplified interface for this operation.

func CloseRegistryKey

func CloseRegistryKey(key RegistryKey)

CloseRegistryKey is a function that closes a specified registry key and logs any associated errors.

Parameter:

  • key: A RegistryKey object representing the registry key to be closed.

Returns: None. If an error occurs while closing the registry key, the error is logged and not returned.

Note: This function is designed to handle errors that may occur when closing a registry key. If an error occurs, it is logged with additional context, allowing for easier debugging and error tracking.

func FindEntries

func FindEntries(entries []string, key RegistryKey) []string

FindEntries scans a specified registry key for a list of entries and returns the values of those entries.

Parameters:

  • entries: A slice of strings representing the names of the entries to be checked within the registry key.
  • key: A RegistryKey object representing the registry key to be scanned.

Returns:

  • A slice of strings containing the values of the specified entries within the registry key. Only entries that are enabled on startup are included. This is determined by checking the binary values of the entries; entries with a binary value of 0 at indices 4, 5, and 6 are considered enabled.

Note: This function is designed to handle the retrieval of startup-related programs from the registry. It filters out disabled programs to provide a list of only the enabled ones.

Types

type CommandExecutor

type CommandExecutor interface {
	Execute(command string, args ...string) ([]byte, error)
}

CommandExecutor is an interface that defines a contract for executing system commands. It abstracts the details of command execution, allowing for different implementations that can either execute real system commands or simulate command execution for testing purposes.

type File

type File interface {
	Close() error
	io.Closer
	Read(p []byte) (int, error)
	Write(p []byte) (int, error)
	Copy(destination File, source File) (int64, error)
}

type FileMock

type FileMock struct {
	FileName string
	IsOpen   bool
	Buffer   []byte
	Bytes    int
	Err      error
}

func (*FileMock) Close

func (f *FileMock) Close() error

Close closes the mock file

Parameters: _

Returns: an error if the file cannot be closed, nil if the file is closed successfully

func (*FileMock) Copy

func (f *FileMock) Copy(source File, destination File) (int64, error)

func (*FileMock) Read

func (f *FileMock) Read(_ []byte) (int, error)

Read reads from the mock file

Parameters: p - the buffer to read into

Returns: the number of bytes read and an error if the file cannot be read from

func (*FileMock) Write

func (f *FileMock) Write(_ []byte) (int, error)

Write writes to the mock file

Parameters: p - the buffer to write from

Returns: the number of bytes written and an error if the file cannot be written to

type FileWrapper

type FileWrapper struct {
	Err    error
	Writer *os.File
	Reader *os.File
	// contains filtered or unexported fields
}

FileWrapper is a wrapper for the os.File type

func Wrap

func Wrap(file *os.File) *FileWrapper

Wrap creates a new FileWrapper struct

Parameters: file - the file to wrap

Returns: a pointer to a new FileWrapper struct

func (*FileWrapper) Close

func (f *FileWrapper) Close() error

Close closes the file

Parameters: _

Returns: an error if the file cannot be closed, nil if the file is closed successfully

func (*FileWrapper) Copy

func (f *FileWrapper) Copy(source File, destination File) (int64, error)

func (*FileWrapper) Read

func (f *FileWrapper) Read(p []byte) (int, error)

Read reads from the file

Parameters: p - the buffer to read into

Returns: the number of bytes read and an error if the file cannot be read from

func (*FileWrapper) Write

func (f *FileWrapper) Write(p []byte) (int, error)

Write writes to the file

Parameters: p - the buffer to write from

Returns: the number of bytes written and an error if the file cannot be written to

type MockCommandExecutor

type MockCommandExecutor struct {
	Output string
	Err    error
}

MockCommandExecutor is a mock implementation of the CommandExecutor interface. It is used for testing purposes to simulate the behavior of a real command executor. This allows tests to control the output and error conditions of command execution, ensuring that the code under test can handle various scenarios correctly.

func (*MockCommandExecutor) Execute

func (m *MockCommandExecutor) Execute(_ string, _ ...string) ([]byte, error)

Execute simulates the execution of a system command for testing purposes. This method is part of the MockCommandExecutor struct, which is a mock implementation of the CommandExecutor interface.

Parameters:

  • _: A string representing the system command to be executed. This parameter is ignored in the mock implementation.
  • _: A variadic string slice representing the arguments to be passed to the command. This parameter is ignored in the mock implementation.

Returns:

  • A byte slice representing the predefined output of the simulated command execution.
  • An error that will be non-nil if a predefined error condition is simulated.

This method allows tests to control the outcomes of command execution, ensuring that the code under test can handle various scenarios correctly.

type MockRegistryKey

type MockRegistryKey struct {
	KeyName       string
	StringValues  map[string]string
	BinaryValues  map[string][]byte
	IntegerValues map[string]uint64
	SubKeys       []MockRegistryKey
	StatReturn    *registry.KeyInfo
	Err           error
}

MockRegistryKey is a struct that provides a mock implementation of the RegistryKey interface. It is designed for testing purposes and allows for the simulation of registry key operations without interacting with the actual Windows registry.

The struct contains fields that represent the possible values a registry key can hold, including string, binary, and integer values. It also includes a list of subkeys, allowing for the simulation of a registry key hierarchy.

The methods of this struct mimic the behavior of their counterparts in the RegistryKey interface, returning the values stored in the struct's fields instead of interacting with the Windows registry.

func (*MockRegistryKey) Close

func (m *MockRegistryKey) Close() error

Close terminates the connection to the MockRegistryKey.

This function is a method of the MockRegistryKey struct and simulates the Close method of the RegistryKey interface. It is used to clean up any resources associated with the MockRegistryKey. In the context of the MockRegistryKey, this function does not perform any operations as there are no resources to release.

Returns:

  • error: Always returns nil as there are no resources to release in the MockRegistryKey.

func (*MockRegistryKey) GetBinaryValue

func (m *MockRegistryKey) GetBinaryValue(name string) ([]byte, uint32, error)

GetBinaryValue retrieves the binary value associated with a specified name from the MockRegistryKey.

Parameters:

  • name string: The name of the value to retrieve.

Returns:

  • []byte: The binary value associated with the specified name. If the name does not exist in the BinaryValues map, nil is returned.
  • uint32: The data type of the value. This is always 0 for binary values in the MockRegistryKey.
  • error: An error object if the specified name does not exist in the BinaryValues map. Otherwise, nil.

This function is a method of the MockRegistryKey struct and simulates the GetBinaryValue method of the RegistryKey interface by returning the values stored in the BinaryValues map of the MockRegistryKey.

func (*MockRegistryKey) GetIntegerValue

func (m *MockRegistryKey) GetIntegerValue(name string) (uint64, uint32, error)

GetIntegerValue retrieves the integer value associated with a specified name from the MockRegistryKey.

Parameters:

  • name string: The name of the value to retrieve.

Returns:

  • uint64: The integer value associated with the specified name. If the name does not exist in the IntegerValues map, zero is returned.
  • uint32: The data type of the value. This is always 0 for integer values in the MockRegistryKey.
  • error: An error object if the specified name does not exist in the IntegerValues map. Otherwise, nil.

This function is a method of the MockRegistryKey struct and simulates the GetIntegerValue method of the RegistryKey interface by returning the values stored in the IntegerValues map of the MockRegistryKey.

func (*MockRegistryKey) GetStringValue

func (m *MockRegistryKey) GetStringValue(name string) (string, uint32, error)

GetStringValue retrieves the string value associated with a specified name from the MockRegistryKey.

Parameters:

  • name string: The name of the value to retrieve.

Returns:

  • string: The string value associated with the specified name. If the name does not exist in the StringValues map, an empty string is returned.
  • uint32: The data type of the value. This is always 0 for string values in the MockRegistryKey.
  • error: An error object if the specified name does not exist in the StringValues map. Otherwise, nil.

This function is a method of the MockRegistryKey struct and simulates the GetStringvalue method of the RegistryKey interface by returning the values stored in the StringValues map of the MockRegistryKey.

func (*MockRegistryKey) OpenKey

func (m *MockRegistryKey) OpenKey(path string, _ uint32) (RegistryKey, error)

OpenKey opens a subkey with a specified path relative to the current key in the MockRegistryKey.

Parameters:

  • path string: The path of the subkey to open, relative to the current key.
  • _ uint32: This parameter is ignored in the MockRegistryKey implementation as no access rights are required to open a mock key.

Returns:

  • RegistryKey: A RegistryKey object representing the opened subkey. If the specified path does not exist in the SubKeys slice, the method returns the current key.
  • error: An error object if the specified path does not exist in the SubKeys slice. Otherwise, nil.

This function is a method of the MockRegistryKey struct and simulates the OpenKey method of the RegistryKey interface by returning a subkey from the SubKeys slice of the MockRegistryKey.

func (*MockRegistryKey) ReadSubKeyNames

func (m *MockRegistryKey) ReadSubKeyNames(count int) ([]string, error)

ReadSubKeyNames retrieves the names of the subkeys in the MockRegistryKey.

Parameters:

  • count int: The maximum number of subkey names to retrieve. If count is less than or equal to zero, all subkey names are retrieved.

Returns:

  • []string: A slice of strings containing the names of the subkeys in the MockRegistryKey. The length of the slice is the lesser of the number of subkeys in the key and the count parameter.
  • error: An error object if an error occurred while retrieving the subkey names. Otherwise, nil.

This function is a method of the MockRegistryKey struct and simulates the ReadSubKeyNames method of the RegistryKey interface by returning the names of the subkeys stored in the SubKeys slice of the MockRegistryKey.

func (*MockRegistryKey) ReadValueNames

func (m *MockRegistryKey) ReadValueNames(maxCount int) ([]string, error)

ReadValueNames retrieves the names of the values stored in the MockRegistryKey.

Parameters:

  • maxCount int: The maximum number of value names to retrieve. If maxCount is less than or equal to zero, all value names are retrieved.

Returns:

  • []string: A slice of strings containing the names of the values in the MockRegistryKey. The length of the slice is the lesser of the number of values in the key and the maxCount parameter.
  • error: An error object if an error occurred while retrieving the value names. Otherwise, nil.

This function is a method of the MockRegistryKey struct and simulates the ReadValueNames method of the RegistryKey interface by returning the names of the values stored in the StringValues, BinaryValues, and IntegerValues maps of the MockRegistryKey. It removes any duplicate names before returning the result.

func (*MockRegistryKey) Stat

func (m *MockRegistryKey) Stat() (*registry.KeyInfo, error)

Stat retrieves the information about the MockRegistryKey.

Returns:

  • *registry.KeyInfo: A pointer to a KeyInfo object that contains information about the MockRegistryKey. This includes the number of subkeys and values, and the time of the last write operation.
  • error: An error object if an error occurred while retrieving the information. Otherwise, nil.

This function is a method of the MockRegistryKey struct and simulates the Stat method of the RegistryKey interface by returning the KeyInfo stored in the StatReturn field of the MockRegistryKey.

type MockWindowsVersion

type MockWindowsVersion struct {
	MajorVersion uint32
	MinorVersion uint32
	BuildNumber  uint32
}

MockWindowsVersion is a struct that simulates the version information of a Windows operating system.

It is designed to be used in testing scenarios where control over the reported version of Windows is required.

Fields:

  • MajorVersion (uint32): Represents the major version of the Windows OS. For example, for Windows 10 or 11, this would be 10 or 11 respectively.
  • MinorVersion (uint32): Represents the minor version of the Windows OS. This is typically used for minor updates or revisions.
  • BuildNumber (uint32): Represents the build number of the Windows OS. This is typically incremented with each build release by Microsoft.

The MockWindowsVersion struct implements the WindowsVersion interface by providing a RtlGetVersion method that returns a custom OsVersionInfoEx object.

func (*MockWindowsVersion) RtlGetVersion

func (m *MockWindowsVersion) RtlGetVersion() *windows.OsVersionInfoEx

RtlGetVersion returns a custom Windows version information when called on a MockWindowsVersion instance. This method is primarily used in testing scenarios where specific Windows version information is required.

The returned OsVersionInfoEx object contains the following fields:

  • MajorVersion: Simulates the major version of the Windows OS.
  • MinorVersion: Simulates the minor version of the Windows OS.
  • BuildNumber: Simulates the build number of the Windows OS.

Returns:

  • *windows.OsVersionInfoEx: A pointer to an OsVersionInfoEx object that contains the custom Windows version information.

type RealCommandExecutor

type RealCommandExecutor struct {
}

RealCommandExecutor is a struct that implements the CommandExecutor interface. It is responsible for executing actual system commands. The execution is performed using the os/exec package, which allows the commands to be run and their output to be captured. This struct provides a concrete implementation of the CommandExecutor interface, enabling interaction with the system's command line interface.

func (*RealCommandExecutor) Execute

func (r *RealCommandExecutor) Execute(command string, args ...string) ([]byte, error)

Execute runs a system command and returns its output. This method is part of the RealCommandExecutor struct, which is an implementation of the CommandExecutor interface.

Parameters:

  • command: A string representing the system command to be executed.
  • args: A variadic string slice representing the arguments to be passed to the command.

Returns:

  • A byte slice representing the output of the executed command.
  • An error that will be non-nil if the command execution fails.

This method uses the os/exec package to execute the command, capturing and returning the output. It provides a mechanism for actual interaction with the system's command line interface.

type RealWindowsVersion

type RealWindowsVersion struct {
}

RealWindowsVersion is a struct that provides the actual version information of the Windows operating system.

It implements the WindowsVersion interface by providing a RtlGetVersion method that returns a genuine windows.OsVersionInfoEx object.

This struct is typically used in production scenarios where accurate Windows version information is required.

func (*RealWindowsVersion) RtlGetVersion

func (r *RealWindowsVersion) RtlGetVersion() *windows.OsVersionInfoEx

RtlGetVersion retrieves the actual Windows version information when called on a RealWindowsVersion instance.

This method is typically used in production scenarios where accurate Windows version information is required.

The returned OsVersionInfoEx object contains the actual version information of the Windows operating system, including:

  • MajorVersion: The major version of the Windows OS.
  • MinorVersion: The minor version of the Windows OS.
  • BuildNumber: The build number of the Windows OS.

Returns:

  • *windows.OsVersionInfoEx: A pointer to an OsVersionInfoEx object that contains the actual Windows version information.

type RegistryKey

type RegistryKey interface {
	GetStringValue(name string) (string, uint32, error)
	GetBinaryValue(name string) ([]byte, uint32, error)
	GetIntegerValue(name string) (uint64, uint32, error)
	OpenKey(path string, access uint32) (RegistryKey, error)
	ReadValueNames(count int) ([]string, error)
	ReadSubKeyNames(count int) ([]string, error)
	Close() error
	Stat() (*registry.KeyInfo, error)
}

RegistryKey is an interface for reading values from the Windows registry

func OpenRegistryKey

func OpenRegistryKey(k RegistryKey, path string) (RegistryKey, error)

OpenRegistryKey is a function that opens a specified registry key and handles any associated errors.

Parameters:

  • k: A RegistryKey object representing the base registry key from which the specified path will be opened.
  • path: A string representing the path to the registry key to be opened, relative to the base registry key.

Returns:

  • A RegistryKey object representing the opened registry key.
  • An error object that encapsulates any error that occurred while trying to open the registry key. If no error occurred, this will be nil.

Note: This function is designed to handle errors that may occur when opening a registry key, such as the key not existing. If an error occurs, it will be wrapped with additional context and returned, allowing the caller to handle it appropriately.

type RegistryKeyWrapper

type RegistryKeyWrapper struct {
	// contains filtered or unexported fields
}

func NewRegistryKeyWrapper

func NewRegistryKeyWrapper(key registry.Key) *RegistryKeyWrapper

func (*RegistryKeyWrapper) Close

func (r *RegistryKeyWrapper) Close() error

Close closes the Windows registry key.

Returns:

  • error: An error object if an error occurred while closing the key. Otherwise, nil.

This function is a method of the RegistryKeyWrapper struct and uses the Close method of the registry.Key object it wraps.

func (*RegistryKeyWrapper) GetBinaryValue

func (r *RegistryKeyWrapper) GetBinaryValue(name string) ([]byte, uint32, error)

GetBinaryValue retrieves the binary value of a specified name from the Windows registry key.

Parameters:

  • name string: The name of the value to retrieve.

Returns:

  • []byte: The binary value associated with the specified name.
  • uint32: The data type of the value. This is always REG_BINARY for binary values.
  • error: An error object if an error occurred while retrieving the value. Otherwise, nil.

This function is a method of the RegistryKeyWrapper struct and uses the GetBinaryValue method of the registry.Key object it wraps.

func (*RegistryKeyWrapper) GetIntegerValue

func (r *RegistryKeyWrapper) GetIntegerValue(name string) (uint64, uint32, error)

GetIntegerValue retrieves the integer value of a specified name from the Windows registry key.

Parameters:

  • name string: The name of the value to retrieve.

Returns:

  • uint64: The integer value associated with the specified name.
  • uint32: The data type of the value. This is always REG_DWORD for integer values.
  • error: An error object if an error occurred while retrieving the value. Otherwise, nil.

This function is a method of the RegistryKeyWrapper struct and uses the GetIntegerValue method of the registry.Key object it wraps.

func (*RegistryKeyWrapper) GetStringValue

func (r *RegistryKeyWrapper) GetStringValue(name string) (string, uint32, error)

GetStringValue retrieves the string value of a specified name from the Windows registry key.

Parameters:

  • name string: The name of the value to retrieve.

Returns:

  • string: The string value associated with the specified name.
  • uint32: The data type of the value. This is always REG_SZ for string values.
  • error: An error object if an error occurred while retrieving the value. Otherwise, nil.

This function is a method of the RegistryKeyWrapper struct and uses the GetStringvalue method of the registry.Key object it wraps.

func (*RegistryKeyWrapper) OpenKey

func (r *RegistryKeyWrapper) OpenKey(path string, access uint32) (RegistryKey, error)

OpenKey opens a registry key with a path relative to the current key and the specified access rights.

Parameters:

  • path string: The path of the registry key to open, relative to the current key.
  • access uint32: The access rights to use when opening the key.

Returns:

  • RegistryKey: A RegistryKey object representing the opened key.
  • error: An error object if an error occurred while opening the key. Otherwise, nil.

This function is a method of the RegistryKeyWrapper struct and uses the OpenKey method of the registry.Key object it wraps.

func (*RegistryKeyWrapper) ReadSubKeyNames

func (r *RegistryKeyWrapper) ReadSubKeyNames(count int) ([]string, error)

ReadSubKeyNames retrieves the names of the subkeys in the Windows registry key.

Parameters:

  • count int: The maximum number of subkey names to retrieve. If count is less than or equal to zero, all subkey names are retrieved.

Returns:

  • []string: A slice of strings containing the names of the subkeys in the registry key. The length of the slice is the lesser of the number of subkeys in the key and the count parameter.
  • error: An error object if an error occurred while retrieving the subkey names. Otherwise, nil.

This function is a method of the RegistryKeyWrapper struct and uses the ReadSubKeyNames method of the registry.Key object it wraps.

func (*RegistryKeyWrapper) ReadValueNames

func (r *RegistryKeyWrapper) ReadValueNames(count int) ([]string, error)

ReadValueNames retrieves the names of the values in the Windows registry key.

Parameters:

  • count int: The maximum number of value names to retrieve. If count is less than or equal to zero, all value names are retrieved.

Returns:

  • []string: A slice of strings containing the names of the values in the registry key. The length of the slice is the lesser of the number of values in the key and the count parameter.
  • error: An error object if an error occurred while retrieving the value names. Otherwise, nil.

This function is a method of the RegistryKeyWrapper struct and uses the ReadValueNames method of the registry.Key object it wraps.

func (*RegistryKeyWrapper) Stat

func (r *RegistryKeyWrapper) Stat() (*registry.KeyInfo, error)

Stat retrieves the information about the Windows registry key.

Returns:

  • *registry.KeyInfo: A pointer to a KeyInfo object that contains information about the registry key.
  • error: An error object if an error occurred while retrieving the information. Otherwise, nil.

This function is a method of the RegistryKeyWrapper struct and uses the Stat method of the registry.Key object it wraps.

type WindowsVersion

type WindowsVersion interface {
	RtlGetVersion() *windows.OsVersionInfoEx
}

WindowsVersion is an interface that defines a contract for retrieving Windows version information. It exposes a single method, RtlGetVersion, which is expected to return an instance of windows.OsVersionInfoEx.

Implementations of this interface can provide either real or mocked Windows version information, making it useful in a variety of scenarios such as testing and simulation.

The RtlGetVersion method is designed to return custom Windows version information, allowing for flexibility and control over the reported Windows version.

Jump to

Keyboard shortcuts

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