validations

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TestMsi       = "27e175b8-e525-11ee-842b-7aa2c69b8f1f"
	TestScaScanId = "3d90ec4b-cf33-4846-6831-4bf9576f2235"

	TestPlatformUrl = "https://test-platform-url.jfrog.io/"
	TestMoreInfoUrl = "https://test-more-info-url.jfrog.io/"

	TestConfigProfileName = "default-profile"
)
View Source
const (
	SastToolName = "🐸 JFrog SAST"
	IacToolName  = "JFrog Terraform scanner"
	// #nosec G101 -- Not credentials.
	SecretsToolName            = "JFrog Secrets scanner"
	ContextualAnalysisToolName = "JFrog Applicability Scanner"
)
View Source
const (
	ErrCountFormat = "Expected%s %d %s in %s, but got %d %s."
)

Variables

View Source
var (
	TestMockGitInfo = xscservices.XscGitInfoContext{
		GitRepoHttpsCloneUrl: "https://github.com/jfrog/dummy-repo.git",
		GitProvider:          "github",
		GitRepoName:          "dummy-repo",
		GitProject:           "jfrog",
		BranchName:           "dev",
		LastCommitHash:       "4be861f9a585d8ae5dde0b9550669972ee05c9d7",
	}
)

Functions

func CreateDummyApplicabilityRule added in v1.14.0

func CreateDummyApplicabilityRule(cve string, applicableStatus jasutils.ApplicabilityStatus) *sarif.ReportingDescriptor

func CreateDummyApplicableResults added in v1.14.0

func CreateDummyApplicableResults(cve string, location formats.Location) *sarif.Result

func CreateDummyJasResult added in v1.14.0

func CreateDummyJasResult(id string, level severityutils.SarifSeverityLevel, location formats.Location, codeFlows ...[]formats.Location) *sarif.Result

func CreateDummyJasRule added in v1.14.0

func CreateDummyJasRule(id string, cwe ...string) *sarif.ReportingDescriptor

func CreateDummySastViolationResult added in v1.14.0

func CreateDummySastViolationResult(id string, level severityutils.SarifSeverityLevel, watch, issueId string, policies []string, location formats.Location, codeFlows ...[]formats.Location) *sarif.Result

func CreateDummySecretResult added in v1.14.0

func CreateDummySecretResult(id string, status jasutils.TokenValidationStatus, metadata string, location formats.Location) *sarif.Result

func CreateDummySecretViolationResult added in v1.14.0

func CreateDummySecretViolationResult(id string, status jasutils.TokenValidationStatus, metadata, watch, issueId string, policies []string, location formats.Location) *sarif.Result

func CreateRestsMockServer

func CreateRestsMockServer(testHandler restsTestHandler) *httptest.Server

Create mock server to test REST APIs. testHandler - The HTTP handler of the test

func CreateXrayRestsMockServer

func CreateXrayRestsMockServer(testHandler restsTestHandler) (*httptest.Server, *config.ServerDetails)

func CreateXscRestsMockServer

func CreateXscRestsMockServer(t *testing.T, testHandler restsTestHandler) (*httptest.Server, *config.ServerDetails, artifactory.ArtifactoryServicesManager)

func GetValidationCountErrMsg

func GetValidationCountErrMsg(what, where string, exactMatch bool, expectedCount, actualCount int) string

func NewMockJasRuns added in v1.14.0

func NewMockJasRuns(runs ...*sarif.Run) []results.ScanResult[[]*sarif.Run]

func NewMockScaResults added in v1.14.0

func NewMockScaResults(responses ...services.ScanResponse) (converted []results.ScanResult[services.ScanResponse])

func ValidateApplicabilityStatusCount added in v1.14.0

func ValidateApplicabilityStatusCount(t *testing.T, outputType string, violation, exactMatch bool, params *ApplicabilityStatusCount, applicableResults, undeterminedResults, notCoveredResults, notApplicableResults, missingContextResults, inactiveSecrets int)

func ValidateCommandJsonOutput

func ValidateCommandJsonOutput(t *testing.T, params ValidationParams)

Validation on SCA content only (No JAS in this content) Actual (and optional Expected) content should be a slice of services.ScanResponse in the validation params

func ValidateCommandSarifOutput

func ValidateCommandSarifOutput(t *testing.T, params ValidationParams)

Validate sarif report according to the expected values and issue counts in the validation params. Value/Actual content should be a *sarif.Report in the validation params If ExactResultsMatch is true, the validation will check exact values and not only the 'equal or grater' counts / existence of expected attributes. For Integration tests with JFrog API, ExactResultsMatch should be set to false.

func ValidateCommandSimpleJsonOutput

func ValidateCommandSimpleJsonOutput(t *testing.T, params ValidationParams)

Validate simple-json report results according to the expected values and issue counts in the validation params. Actual/Expected content should be a formats.SimpleJsonResults in the validation params. If Expected is provided, the validation will check if the Actual content matches the expected results. If ExactResultsMatch is true, the validation will check exact values and not only the 'equal or grater' counts / existence of expected attributes. (For Integration tests with JFrog API, ExactResultsMatch should be set to false)

func ValidateCommandSummaryOutput

func ValidateCommandSummaryOutput(t *testing.T, params ValidationParams)

Validate summary results according to the expected values and issue counts in the validation params. Content/Expected should be a formats.ResultsSummary in the validation params. If Expected is provided, the validation will check if the Actual content matches the expected results. If ExactResultsMatch is true, the validation will check exact values and not only the 'equal or grater' counts / existence of expected attributes. (For Integration tests with JFrog API, ExactResultsMatch should be set to false)

func ValidateContent

func ValidateContent(t *testing.T, exactMatch bool, validations ...Validation) bool

ValidateContent validates the content of the given Validations. If exactMatch is true, the content must match exactly. If at least one validation fails, the function returns false and stops validating the rest of the pairs.

func ValidateCount added in v1.14.0

func ValidateCount(t *testing.T, outputType string, params ValidationParams, actual validationCountActualValues)

func ValidatePointersAndNotNil

func ValidatePointersAndNotNil[T any](t *testing.T, exactMatch bool, pair PointerValidation[T]) bool

func ValidateSarifIssuesCount

func ValidateSarifIssuesCount(t *testing.T, params ValidationParams, report *sarif.Report)

Validate sarif report according to the expected counts in the validation params. Actual content should be a *sarif.Report in the validation params. If Expected is provided, the validation will check if the Actual content matches the expected results. If ExactResultsMatch is true, the validation will check exact values and not only the 'equal or grater' counts / existence of expected attributes. (For Integration tests with JFrog API, ExactResultsMatch should be set to false)

func ValidateSarifReport

func ValidateSarifReport(t *testing.T, exactMatch bool, expected, actual *sarif.Report)

func ValidateScaViolationCount added in v1.14.0

func ValidateScaViolationCount(t *testing.T, outputType string, exactMatch bool, params *ScaViolationCount, securityViolations, licenseViolations, operationalViolations int)

func ValidateScanResponseIssuesCount

func ValidateScanResponseIssuesCount(t *testing.T, params ValidationParams, content ...services.ScanResponse)

func ValidateScanResponses

func ValidateScanResponses(t *testing.T, exactMatch bool, expected, actual []services.ScanResponse)

func ValidateScanTypeCount added in v1.14.0

func ValidateScanTypeCount(t *testing.T, outputType string, violation, exactMatch bool, params *ScanCount, scaViolations, sastViolations, secretsViolations, iacViolations int)

func ValidateSimpleJsonIssuesCount

func ValidateSimpleJsonIssuesCount(t *testing.T, params ValidationParams, results formats.SimpleJsonResults)

Validate simple-json report results according to the expected counts in the validation params. Actual content should be a formats.SimpleJsonResults in the validation params. If Expected is provided, the validation will check if the Actual content matches the expected results. If ExactResultsMatch is true, the validation will check exact values and not only the 'equal or grater' counts / existence of expected attributes. (For Integration tests with JFrog API, ExactResultsMatch should be set to false)

func ValidateSimpleJsonResults

func ValidateSimpleJsonResults(t *testing.T, exactMatch bool, expected, actual formats.SimpleJsonResults)

func ValidateSummaryIssuesCount

func ValidateSummaryIssuesCount(t *testing.T, params ValidationParams, results formats.ResultsSummary)

func ValidateTotalCount added in v1.14.0

func ValidateTotalCount(t *testing.T, outputType string, exactMatch bool, params *TotalCount, vulnerabilities, violations, license int)

func ValidateViolationCount added in v1.14.0

func ValidateViolationCount(t *testing.T, outputType string, exactMatch bool, params *ViolationCount, actual validationCountActualValues)

func ValidateVulnerabilitiesCount added in v1.14.0

func ValidateVulnerabilitiesCount(t *testing.T, outputType string, exactMatch bool, params *VulnerabilityCount, actual validationCountActualValues)

func VerifyJsonResults

func VerifyJsonResults(t *testing.T, content string, params ValidationParams)

Validate SCA content only (No JAS in this content) according to the expected values and issue counts in the validation params. Content/Expected should be a []services.ScanResponse in the validation params. If Expected is provided, the validation will check if the Actual content matches the expected results. If ExactResultsMatch is true, the validation will check exact values and not only the 'equal or grater' counts / existence of expected attributes. (For Integration tests with JFrog API, ExactResultsMatch should be set to false)

func VerifySimpleJsonResults

func VerifySimpleJsonResults(t *testing.T, content string, params ValidationParams)

Validate simple-json report results according to the expected values and issue counts in the validation params. Content/Expected should be a formats.SimpleJsonResults in the validation params. If Expected is provided, the validation will check if the Actual content matches the expected results. If ExactResultsMatch is true, the validation will check exact values and not only the 'equal or grater' counts / existence of expected attributes. (For Integration tests with JFrog API, ExactResultsMatch should be set to false)

func XrayServer

func XrayServer(t *testing.T, params MockServerParams) (*httptest.Server, *config.ServerDetails)

Mock Xray server (with Xsc inner service if supported based on version - not backward compatible to XSC API)

func XscServer

func XscServer(t *testing.T, params MockServerParams) (*httptest.Server, *config.ServerDetails)

Mock Only Xsc server API (backward compatible)

Types

type ApplicabilityStatusCount added in v1.14.0

type ApplicabilityStatusCount struct {
	// Expected number of 'Applicable' contextual-analysis statuses for the issues (sca)
	Applicable int
	// Expected number of 'Undetermined' contextual-analysis statuses for the issues (sca)
	Undetermined int
	// Expected number of 'NotCovered' contextual-analysis statuses for the issues (sca)
	NotCovered int
	// Expected number of 'NotApplicable' contextual-analysis statuses for the issues (sca)
	NotApplicable int
	// Expected number of 'MissingContext' contextual-analysis statuses for the issues (sca)
	MissingContext int
	// Expected number of 'Inactive' contextual-analysis statuses for the issues (secrets)
	Inactive int
}

type CountValidation

type CountValidation[T any] struct {
	Expected int
	Actual   int
	Msg      string
}

CountValidation validates the content of the given numbers. Not ExactMatch: The actual content must be greater or equal to the expected content.

func (CountValidation[T]) ErrMsgs

func (cv CountValidation[T]) ErrMsgs(_ *testing.T) []string

func (CountValidation[T]) Validate

func (cv CountValidation[T]) Validate(t *testing.T, exactMatch bool) bool

type ListValidation

type ListValidation[T any] struct {
	Expected []T
	Actual   []T
	Msg      string
}

ListValidation validates the content of the given lists. Not ExactMatch: The expected content must be subset of the actual content.

func (ListValidation[T]) ErrMsgs

func (lvp ListValidation[T]) ErrMsgs(t *testing.T) []string

func (ListValidation[T]) Validate

func (lvp ListValidation[T]) Validate(t *testing.T, exactMatch bool) bool

type MockServerParams added in v1.13.2

type MockServerParams struct {
	// General params to mock Xray and Xsc (backward compatible and inner service based on the following params)
	XrayVersion  string
	XscVersion   string
	XscNotExists bool
	// Xsc/Event Api
	ReturnMsi string
	// Xsc/Watch/Resource Api
	ReturnMockPlatformWatches xrayutils.ResourcesWatchesBody
}

type NumberValidation

type NumberValidation[T any] struct {
	Expected T
	Actual   T
	Msg      string
}

NumberValidation validates the content of the given numbers. Not ExactMatch: The actual content must not be zero if the expected content is not zero.

func (NumberValidation[T]) ErrMsgs

func (nvp NumberValidation[T]) ErrMsgs(_ *testing.T) []string

func (NumberValidation[T]) Validate

func (nvp NumberValidation[T]) Validate(t *testing.T, exactMatch bool) bool

type PointerValidation

type PointerValidation[T any] struct {
	Expected *T
	Actual   *T
	Msg      string
}

PointerValidation validates the content of the given pointers. Not ExactMatch: The actual content must not be nil if the expected content is not nil.

func (PointerValidation[T]) ErrMsgs

func (pvp PointerValidation[T]) ErrMsgs(t *testing.T) []string

func (PointerValidation[T]) Validate

func (pvp PointerValidation[T]) Validate(t *testing.T, exactMatch bool) bool

type ScaViolationCount added in v1.14.0

type ScaViolationCount struct {
	// Expected number of security violations (Sca, JAS)
	Security int
	// Expected number of license violations
	License int
	// Expected number of operational violations
	Operational int
}

type ScanCount added in v1.14.0

type ScanCount struct {
	// Expected number of Sca issues
	Sca int
	// Expected number of Sast issues
	Sast int
	// Expected number of Iac issues
	Iac int
	// Expected number of Secrets issues
	Secrets int
}

type StringValidation

type StringValidation struct {
	Expected string
	Actual   string
	Msg      string
}

Validate a string content. Not ExactMatch: The actual content must not be empty if the expected content is not empty.

func (StringValidation) ErrMsgs

func (sv StringValidation) ErrMsgs(_ *testing.T) []string

func (StringValidation) Validate

func (sv StringValidation) Validate(t *testing.T, exactMatch bool) bool

type TotalCount added in v1.14.0

type TotalCount struct {
	// Expected number of licenses
	Licenses int
	// Expected number of total vulnerabilities (sca + sast + iac + secrets)
	Vulnerabilities int
	// Expected number of total violations (sca security + sca license + sca operational + sast + iac + secrets)
	Violations int
}

type Validation

type Validation interface {
	Validate(t *testing.T, exactMatch bool) bool
	ErrMsgs(t *testing.T) []string
}

Validation allows to validate/assert a content with expected values. Using the Validation interfaces implementations allows you to assert content for exact value or not exact match (changes base on the implementation).

type ValidationParams

type ValidationParams struct {
	// The actual content to verify.
	Actual interface{}
	// If provided, the test will check if the content matches the expected results.
	Expected interface{}
	// If provided, the test will check exact values and not only the minimum values / existence.
	ExactResultsMatch bool

	// Validate total number of licenses, vulnerabilities and violations
	Total *TotalCount
	// Validate number of vulnerabilities in different contexts
	Vulnerabilities *VulnerabilityCount
	// Validate number of violations in different contexts
	Violations *ViolationCount
}

ValidationParams holds validation/assertion parameters for tests.

type ViolationCount added in v1.14.0

type ViolationCount struct {
	// Expected number of violations by scan type (SCA/JAS)
	ValidateScan *ScanCount
	// Expected number of contextual statuses for violations (sca/secrets)
	ValidateApplicabilityStatus *ApplicabilityStatusCount
	// Expected number of violations by violation type (license, operational, security: SCA+JAS)
	ValidateType *ScaViolationCount
}

type VulnerabilityCount added in v1.14.0

type VulnerabilityCount struct {
	// If exists, validate the total amount of issues in different scan types (SCA/SAST/SECRETS/IAC)
	ValidateScan *ScanCount
	// If exists, validate the total amount of contextual statuses for the issues (sca/secrets)
	ValidateApplicabilityStatus *ApplicabilityStatusCount
}

Jump to

Keyboard shortcuts

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