|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + malysisv1pb "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/malysis/v1" |
| 8 | + packagev1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/messages/package/v1" |
| 9 | + malysisv1 "buf.build/gen/go/safedep/api/protocolbuffers/go/safedep/services/malysis/v1" |
| 10 | + "github.com/safedep/pmg/config" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "google.golang.org/grpc" |
| 13 | +) |
| 14 | + |
| 15 | +// stubMalwareAnalysisServiceClient is a minimal stub implementing the Malysis gRPC client interface, |
| 16 | +// returning a preconfigured response for testing. |
| 17 | +type stubMalwareAnalysisServiceClient struct { |
| 18 | + resp *malysisv1.QueryPackageAnalysisResponse |
| 19 | + err error |
| 20 | +} |
| 21 | + |
| 22 | +func (s *stubMalwareAnalysisServiceClient) QueryPackageAnalysis(ctx context.Context, req *malysisv1.QueryPackageAnalysisRequest, opts ...grpc.CallOption) (*malysisv1.QueryPackageAnalysisResponse, error) { |
| 23 | + return s.resp, s.err |
| 24 | +} |
| 25 | + |
| 26 | +// helper to make a basic PackageVersion for tests |
| 27 | +func makePkgVersion(name, version string) *packagev1.PackageVersion { |
| 28 | + return &packagev1.PackageVersion{ |
| 29 | + Package: &packagev1.Package{ |
| 30 | + Name: name, |
| 31 | + }, |
| 32 | + Version: version, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestMalysisQueryAnalyzer_DefaultAllowWhenNotMalicious(t *testing.T) { |
| 37 | + resp := &malysisv1.QueryPackageAnalysisResponse{ |
| 38 | + AnalysisId: "analysis-1", |
| 39 | + Report: &malysisv1pb.Report{ |
| 40 | + Inference: &malysisv1pb.Report_Inference{ |
| 41 | + IsMalware: false, |
| 42 | + Summary: "No indicators of compromise", |
| 43 | + }, |
| 44 | + }, |
| 45 | + VerificationRecord: &malysisv1pb.VerificationRecord{ |
| 46 | + IsMalware: false, |
| 47 | + }, |
| 48 | + } |
| 49 | + an := &malysisQueryAnalyzer{ |
| 50 | + client: &stubMalwareAnalysisServiceClient{resp: resp}, |
| 51 | + } |
| 52 | + |
| 53 | + pv := makePkgVersion("safe-pkg", "1.0.0") |
| 54 | + result, err := an.Analyze(context.Background(), pv) |
| 55 | + assert.NoError(t, err) |
| 56 | + assert.Equal(t, ActionAllow, result.Action) |
| 57 | + assert.Equal(t, "analysis-1", result.AnalysisID) |
| 58 | + assert.Equal(t, pv, result.PackageVersion) |
| 59 | + assert.NotEmpty(t, result.ReferenceURL) |
| 60 | + assert.Equal(t, "No indicators of compromise", result.Summary) |
| 61 | +} |
| 62 | + |
| 63 | +func TestMalysisQueryAnalyzer_ConfirmOnSuspiciousWhenNotParanoid(t *testing.T) { |
| 64 | + // Ensure paranoid is disabled |
| 65 | + cfg := config.Get() |
| 66 | + origParanoid := cfg.Config.Paranoid |
| 67 | + cfg.Config.Paranoid = false |
| 68 | + defer func() { cfg.Config.Paranoid = origParanoid }() |
| 69 | + |
| 70 | + // Setup: inference says suspicious/malicious (unverified) |
| 71 | + resp := &malysisv1.QueryPackageAnalysisResponse{ |
| 72 | + AnalysisId: "analysis-2", |
| 73 | + Report: &malysisv1pb.Report{ |
| 74 | + Inference: &malysisv1pb.Report_Inference{ |
| 75 | + IsMalware: true, |
| 76 | + Summary: "Suspicious patterns detected", |
| 77 | + }, |
| 78 | + }, |
| 79 | + VerificationRecord: &malysisv1pb.VerificationRecord{ |
| 80 | + IsMalware: false, |
| 81 | + }, |
| 82 | + } |
| 83 | + an := &malysisQueryAnalyzer{ |
| 84 | + client: &stubMalwareAnalysisServiceClient{resp: resp}, |
| 85 | + } |
| 86 | + |
| 87 | + pv := makePkgVersion("suspicious-pkg", "2.0.0") |
| 88 | + result, err := an.Analyze(context.Background(), pv) |
| 89 | + assert.NoError(t, err) |
| 90 | + assert.Equal(t, ActionConfirm, result.Action) |
| 91 | + assert.Equal(t, "analysis-2", result.AnalysisID) |
| 92 | +} |
| 93 | + |
| 94 | +func TestMalysisQueryAnalyzer_BlockOnSuspiciousWhenParanoid(t *testing.T) { |
| 95 | + // Enable paranoid mode |
| 96 | + cfg := config.Get() |
| 97 | + origParanoid := cfg.Config.Paranoid |
| 98 | + cfg.Config.Paranoid = true |
| 99 | + defer func() { cfg.Config.Paranoid = origParanoid }() |
| 100 | + |
| 101 | + // Setup: inference says suspicious/malicious (unverified) |
| 102 | + resp := &malysisv1.QueryPackageAnalysisResponse{ |
| 103 | + AnalysisId: "analysis-3", |
| 104 | + Report: &malysisv1pb.Report{ |
| 105 | + Inference: &malysisv1pb.Report_Inference{ |
| 106 | + IsMalware: true, |
| 107 | + Summary: "Suspicious patterns detected", |
| 108 | + }, |
| 109 | + }, |
| 110 | + VerificationRecord: &malysisv1pb.VerificationRecord{ |
| 111 | + IsMalware: false, |
| 112 | + }, |
| 113 | + } |
| 114 | + an := &malysisQueryAnalyzer{ |
| 115 | + client: &stubMalwareAnalysisServiceClient{resp: resp}, |
| 116 | + } |
| 117 | + |
| 118 | + pv := makePkgVersion("suspicious-pkg", "3.0.0") |
| 119 | + result, err := an.Analyze(context.Background(), pv) |
| 120 | + assert.NoError(t, err) |
| 121 | + assert.Equal(t, ActionBlock, result.Action, "Paranoid mode should block suspicious packages") |
| 122 | +} |
| 123 | + |
| 124 | +func TestMalysisQueryAnalyzer_AlwaysBlockOnVerifiedMalware(t *testing.T) { |
| 125 | + // Paranoid on/off should not matter |
| 126 | + cfg := config.Get() |
| 127 | + origParanoid := cfg.Config.Paranoid |
| 128 | + cfg.Config.Paranoid = false |
| 129 | + defer func() { cfg.Config.Paranoid = origParanoid }() |
| 130 | + |
| 131 | + resp := &malysisv1.QueryPackageAnalysisResponse{ |
| 132 | + AnalysisId: "analysis-4", |
| 133 | + Report: &malysisv1pb.Report{ |
| 134 | + Inference: &malysisv1pb.Report_Inference{ |
| 135 | + IsMalware: false, |
| 136 | + Summary: "Inference not malicious, but verification is", |
| 137 | + }, |
| 138 | + }, |
| 139 | + VerificationRecord: &malysisv1pb.VerificationRecord{ |
| 140 | + IsMalware: true, |
| 141 | + }, |
| 142 | + } |
| 143 | + an := &malysisQueryAnalyzer{ |
| 144 | + client: &stubMalwareAnalysisServiceClient{resp: resp}, |
| 145 | + } |
| 146 | + |
| 147 | + pv := makePkgVersion("verified-malware", "9.9.9") |
| 148 | + result, err := an.Analyze(context.Background(), pv) |
| 149 | + assert.NoError(t, err) |
| 150 | + assert.Equal(t, ActionBlock, result.Action, "Verified malware must be blocked always") |
| 151 | +} |
| 152 | + |
| 153 | +// Implement the full client interface surface expected by malysisv1grpc.MalwareAnalysisServiceClient |
| 154 | +func (s *stubMalwareAnalysisServiceClient) AnalyzePackage(ctx context.Context, req *malysisv1.AnalyzePackageRequest, opts ...grpc.CallOption) (*malysisv1.AnalyzePackageResponse, error) { |
| 155 | + // Not used in these tests; return a nil response with no error |
| 156 | + return nil, nil |
| 157 | +} |
| 158 | +func (s *stubMalwareAnalysisServiceClient) GetAnalysisReport(ctx context.Context, req *malysisv1.GetAnalysisReportRequest, opts ...grpc.CallOption) (*malysisv1.GetAnalysisReportResponse, error) { |
| 159 | + // Not used in these tests |
| 160 | + return nil, nil |
| 161 | +} |
| 162 | +func (s *stubMalwareAnalysisServiceClient) InternalAnalyzePackage(ctx context.Context, req *malysisv1.InternalAnalyzePackageRequest, opts ...grpc.CallOption) (*malysisv1.InternalAnalyzePackageResponse, error) { |
| 163 | + // Not used in these tests |
| 164 | + return nil, nil |
| 165 | +} |
| 166 | +func (s *stubMalwareAnalysisServiceClient) ListPackageAnalysisRecords(ctx context.Context, req *malysisv1.ListPackageAnalysisRecordsRequest, opts ...grpc.CallOption) (*malysisv1.ListPackageAnalysisRecordsResponse, error) { |
| 167 | + // Not used in these tests |
| 168 | + return nil, nil |
| 169 | +} |
0 commit comments