mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
* fix: Suppress non-PMG error from Critical UI Error * fix: Code review fixes --------- Co-authored-by: Sahil Bansal <bansalsahil315@gmail.com>
69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package executor
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os/exec"
|
|
"testing"
|
|
|
|
"github.com/safedep/pmg/sandbox"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type fakeViolationSandbox struct {
|
|
report *sandbox.ViolationReport
|
|
}
|
|
|
|
func (f *fakeViolationSandbox) Execute(context.Context, *exec.Cmd, *sandbox.SandboxPolicy) (*sandbox.ExecutionResult, error) {
|
|
return sandbox.NewExecutionResult(), nil
|
|
}
|
|
|
|
func (f *fakeViolationSandbox) Name() sandbox.DriverName {
|
|
return sandbox.DriverSeatbelt
|
|
}
|
|
|
|
func (f *fakeViolationSandbox) IsAvailable() bool {
|
|
return true
|
|
}
|
|
|
|
func (f *fakeViolationSandbox) Close() error {
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeViolationSandbox) BestEffortViolation(error) (*sandbox.ViolationReport, error) {
|
|
return f.report, nil
|
|
}
|
|
|
|
func TestObserveViolationsCountsObservedViolations(t *testing.T) {
|
|
result := sandbox.NewExecutionResult(sandbox.WithExecutionResultSandbox(&fakeViolationSandbox{
|
|
report: &sandbox.ViolationReport{
|
|
SandboxName: sandbox.DriverSeatbelt,
|
|
PolicyName: "npm-restrictive",
|
|
CorrelationID: "run-1",
|
|
Violations: []sandbox.Violation{
|
|
{
|
|
Kind: sandbox.ViolationKindFSRead,
|
|
RawKind: "file-read",
|
|
Target: "./.env",
|
|
RuleTarget: "**/.env",
|
|
RuleLabel: "read access denied: ./.env",
|
|
},
|
|
},
|
|
},
|
|
}))
|
|
|
|
assert.Equal(t, 1, ObserveViolations(result, errors.New("npm failed")))
|
|
}
|
|
|
|
func TestObserveViolationsReturnsZeroWhenNoReport(t *testing.T) {
|
|
result := sandbox.NewExecutionResult(sandbox.WithExecutionResultSandbox(&fakeViolationSandbox{
|
|
report: nil,
|
|
}))
|
|
|
|
assert.Equal(t, 0, ObserveViolations(result, errors.New("npm failed")))
|
|
}
|
|
|
|
func TestObserveViolationsReturnsZeroOnNilResult(t *testing.T) {
|
|
assert.Equal(t, 0, ObserveViolations(nil, errors.New("npm failed")))
|
|
}
|