Documentation
¶
Overview ¶
Package testutil provides shared test helpers for attack module tests. It includes mock implementations of Provider and Logger, a configurable mock HTTP server, and fixture loading utilities.
Index ¶
- func DefaultAttackConfig() common.AttackConfig
- func LoadFixture(path string) ([]byte, error)
- func LoadFixtureJSON(path string, target interface{}) error
- type LogEntry
- type MockCall
- type MockCodingAgent
- type MockLLMServer
- type MockLogger
- func (l *MockLogger) Debug(msg string, keysAndValues ...interface{})
- func (l *MockLogger) Error(msg string, keysAndValues ...interface{})
- func (l *MockLogger) HasMessage(level, substr string) bool
- func (l *MockLogger) Info(msg string, keysAndValues ...interface{})
- func (l *MockLogger) Warn(msg string, keysAndValues ...interface{})
- type MockMemoryProvider
- func (m *MockMemoryProvider) Has(id string) bool
- func (m *MockMemoryProvider) ProbeMemory(_ context.Context) (bool, error)
- func (m *MockMemoryProvider) Purge(_ context.Context, recordIDs []string) error
- func (m *MockMemoryProvider) Query(ctx context.Context, messages []common.Message, options map[string]interface{}) (string, error)
- type MockProvider
- func (m *MockProvider) CallCount() int
- func (m *MockProvider) GetModel() string
- func (m *MockProvider) GetName() string
- func (m *MockProvider) GetTokenCount(text string) int
- func (m *MockProvider) LastCall() *MockCall
- func (m *MockProvider) Query(_ context.Context, messages []common.Message, options map[string]interface{}) (string, error)
- type ResolvedWrite
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultAttackConfig ¶
func DefaultAttackConfig() common.AttackConfig
DefaultAttackConfig returns a reasonable AttackConfig for testing.
func LoadFixture ¶
LoadFixture reads a JSON fixture file from the testdata directory relative to the calling package. The path should be relative (e.g., "testdata/payload.json").
func LoadFixtureJSON ¶
LoadFixtureJSON reads a JSON fixture and unmarshals it into target.
Types ¶
type MockCodingAgent ¶ added in v0.12.0
type MockCodingAgent struct {
MockProvider // base common.Provider behavior (Query/GetName/...)
// Symlinks maps a shown destination path to the real path the agent
// resolves it to. When a FileOperation's ShownDestination matches a key,
// the write resolves to the mapped target (the SymJack misrepresentation).
// Absent a mapping, the resolved destination equals the shown destination.
Symlinks map[string]string
// NoApprovalStep makes ApproveFileOperation report HasApprovalStep=false
// (the SkipNoMutationTarget case — agent has nothing to hijack).
NoApprovalStep bool
// DenyApproval makes the agent decline the operation (OutcomeRefused).
DenyApproval bool
// ApproveErr, when set, is returned from ApproveFileOperation (SkipProviderError).
ApproveErr error
// AutoExecuteOnTrust executes the repo's ProjectMCPPaths on a trust accept
// (the TrustFall default-trust behavior). When false, trust does not
// auto-execute project MCP (OutcomeRefused).
AutoExecuteOnTrust bool
// NoTrustPrompt makes TrustFolder report HasTrustPrompt=false.
NoTrustPrompt bool
// TrustErr, when set, is returned from TrustFolder (SkipProviderError).
TrustErr error
// Recorded effects for assertions.
Writes []ResolvedWrite
ExecutedMCP []string
// contains filtered or unexported fields
}
MockCodingAgent is a controllable in-memory implementation of common.CodingAgentProvider for exercising the v0.12.0 SymJack and TrustFall modules end-to-end. It performs no real filesystem or process operations — symlink resolution and MCP auto-execution are simulated and recorded for assertion. Refuse-mode knobs let tests drive the OutcomeRefused and SkipNoMutationTarget paths, not just success.
func (*MockCodingAgent) ApproveFileOperation ¶ added in v0.12.0
func (m *MockCodingAgent) ApproveFileOperation(_ context.Context, op common.FileOperation) (common.ApprovalOutcome, error)
ApproveFileOperation simulates presenting a file operation to the agent's approval surface, resolving the destination through the configured symlink table and recording any write.
func (*MockCodingAgent) TrustFolder ¶ added in v0.12.0
func (m *MockCodingAgent) TrustFolder(_ context.Context, req common.FolderTrustRequest) (common.FolderTrustOutcome, error)
TrustFolder simulates a folder-trust decision, auto-executing the repo's project MCP paths when AutoExecuteOnTrust is set.
func (*MockCodingAgent) WriteCount ¶ added in v0.12.0
func (m *MockCodingAgent) WriteCount() int
WriteCount returns the number of recorded writes (for assertions).
type MockLLMServer ¶
MockLLMServer wraps httptest.Server to simulate an LLM API endpoint. By default it returns a JSON chat-completion-style response.
func NewMockLLMServer ¶
func NewMockLLMServer(defaultResponse string) *MockLLMServer
NewMockLLMServer creates and starts a mock HTTP server that responds with configurable chat completion responses.
func (*MockLLMServer) QueueResponse ¶
func (m *MockLLMServer) QueueResponse(resp string)
QueueResponse adds a response to the queue.
func (*MockLLMServer) RequestCount ¶
func (m *MockLLMServer) RequestCount() int
RequestCount returns the number of requests received.
func (*MockLLMServer) URL ¶
func (m *MockLLMServer) URL() string
URL returns the base URL of the mock server.
type MockLogger ¶
type MockLogger struct {
Entries []LogEntry
// contains filtered or unexported fields
}
MockLogger implements common.Logger and captures log entries for assertion.
func (*MockLogger) Debug ¶
func (l *MockLogger) Debug(msg string, keysAndValues ...interface{})
func (*MockLogger) Error ¶
func (l *MockLogger) Error(msg string, keysAndValues ...interface{})
func (*MockLogger) HasMessage ¶
func (l *MockLogger) HasMessage(level, substr string) bool
HasMessage returns true if any entry at the given level contains substr.
func (*MockLogger) Info ¶
func (l *MockLogger) Info(msg string, keysAndValues ...interface{})
func (*MockLogger) Warn ¶
func (l *MockLogger) Warn(msg string, keysAndValues ...interface{})
type MockMemoryProvider ¶ added in v0.12.0
type MockMemoryProvider struct {
*MockProvider
// contains filtered or unexported fields
}
MockMemoryProvider is a stateful provider for exercising the #168 cleanup loop. It records the content of every prompt it receives (its "memory store") and implements common.MemoryProbe (so memory-poisoning modules proceed past their capability gate) and common.Purger (so the implant can be rolled back). It is the in-memory reference Purger from #168's acceptance.
func NewMockMemoryProvider ¶ added in v0.12.0
func NewMockMemoryProvider(defaultResponse string) *MockMemoryProvider
NewMockMemoryProvider returns a stateful memory provider whose plain Query responses default to defaultResponse.
func (*MockMemoryProvider) Has ¶ added in v0.12.0
func (m *MockMemoryProvider) Has(id string) bool
Has reports whether any stored entry contains the given record ID — used by tests to assert an implant is present or absent.
func (*MockMemoryProvider) ProbeMemory ¶ added in v0.12.0
func (m *MockMemoryProvider) ProbeMemory(_ context.Context) (bool, error)
ProbeMemory reports the target retains memory (it's a stateful store). It returns true regardless of current contents — the probe is a capability statement, not a "has records right now" check.
type MockProvider ¶
type MockProvider struct {
// Name and Model returned by GetName / GetModel.
ProviderName string
ModelName string
// Responses is a queue of responses. Each call to Query pops the first
// entry. When the queue is exhausted, DefaultResponse is returned.
Responses []string
DefaultResponse string
// ErrorOn can be set to make Query return an error on the Nth call (1-based).
ErrorOn int
// ErrorMsg is the error message returned when ErrorOn triggers.
ErrorMsg string
// Calls records every Query call for assertions.
Calls []MockCall
// contains filtered or unexported fields
}
MockProvider implements common.Provider for testing. It returns canned responses and records every call for later assertion.
func (*MockProvider) CallCount ¶
func (m *MockProvider) CallCount() int
CallCount returns the number of Query calls made.
func (*MockProvider) GetModel ¶
func (m *MockProvider) GetModel() string
func (*MockProvider) GetName ¶
func (m *MockProvider) GetName() string
func (*MockProvider) GetTokenCount ¶
func (m *MockProvider) GetTokenCount(text string) int
func (*MockProvider) LastCall ¶
func (m *MockProvider) LastCall() *MockCall
LastCall returns the most recent Query call, or nil if none.
type ResolvedWrite ¶ added in v0.12.0
ResolvedWrite records a file write the agent performed, capturing the gap between what the approval prompt showed and where the bytes actually landed.