Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Trim command supporting rewriting of BOMs without selected fields and their data #62

Merged
merged 22 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
238ffc5
Expand functional tests for trim command
mrutkows Nov 2, 2023
e65dbfd
Support slice of keys on trim command vs single key
mrutkows Nov 2, 2023
0de3114
Improve Trim functional test validation method
mrutkows Nov 2, 2023
fbb23e9
Improve Trim functional test validation method
mrutkows Nov 2, 2023
4c68cfc
Create test infra. for schema package
mrutkows Nov 2, 2023
c8107cc
Create test infra. for schema package
mrutkows Nov 2, 2023
2895864
Automate schema functional tests and add more component hash tests
mrutkows Nov 2, 2023
bb0217e
Update Makefile and README to account for schema testing
mrutkows Nov 2, 2023
22e4d91
Add hash CDX services test
mrutkows Nov 2, 2023
7191419
Update schema and marshaling to account for new/changed fields
mrutkows Nov 3, 2023
0379baa
Address golangci-lint errors
mrutkows Nov 3, 2023
39130e6
Address golangci-lint errors
mrutkows Nov 3, 2023
ae8fbc3
Fix schema test init. logic for loading schema comfig. file
mrutkows Nov 3, 2023
c0d43b8
Fix schema test init. logic for loading schema comfig. file
mrutkows Nov 3, 2023
2825dd2
Add vulnerability hashing tests
mrutkows Nov 3, 2023
85706a9
Add vulnerability empty(zero) hashing test
mrutkows Nov 3, 2023
c10f5b1
Add vulnerability empty(zero) hashing test
mrutkows Nov 3, 2023
24cb9f6
Add vulnerability empty(zero) hashing test
mrutkows Nov 3, 2023
4c8a9be
export trim command as experimental to the command-line
mrutkows Nov 3, 2023
a04ecd9
Refine error/warning handling
mrutkows Nov 3, 2023
48f3d24
Refine error/warning handling
mrutkows Nov 3, 2023
5445328
Add trim command flags and
mrutkows Nov 3, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ jobs:
- name: Build
run: go build -v ./...

- name: Test
- name: Test package ./schema
run: go test ./schema -v --args --quiet


- name: Test package ./cmd
run: go test ./cmd -v --args --quiet
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ test: test_clean
test_cmd: test_clean
@echo "Testing `cmd` package"
go test ./cmd -v --args --quiet
go test ./schema -v --args --quiet

test_schema: test_clean
@echo "Testing `schema` package"
go test ./schema -v --args --quiet

# Run the unit tests
unit_tests: test_clean
Expand All @@ -97,6 +102,11 @@ lint: format
@echo "Linting"
golint .

# install: go install github.com/golangci/golangci-lint/cmd/golangci-lint@x.y.z
ci-lint:
@echo "golangci-lint run"
golangci-lint run

install:
go install

Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1464,21 +1464,38 @@ The `Makefile` includes a `test` target for convenience which will use `go test`
make test
```

#### Running tests for a single package

The `test_cmd` target will use run only the test found in the `cmd` package:

```bash
make test_cmd
```

#### Using go test
The `test_schema` target will use run only the test found in the `schema` package:

```bash
make test_schema
```

#### Using `go test`

Example: running all tests in the `cmd` package:

```bash
go test github.com/CycloneDX/sbom-utility/cmd -v
```

Example: running all tests in the `schema` package:

```bash
go test github.com/CycloneDX/sbom-utility/schema -v
```

#### Running tests in quiet mode

Run in "quiet" mode to not see error test output:

```bash
go test github.com/CycloneDX/sbom-utility/cmd -v --quiet
```
Expand All @@ -1489,7 +1506,7 @@ run an individual test within the `cmd` package:
go test github.com/CycloneDX/sbom-utility/cmd -v -run TestValidateCdx14MinRequiredBasic
```

#### Debugging go tests
##### Debugging `go test`

Simply append the flags `--args --trace` or `--args --debug` to your `go test` command to enable trace or debug output for your designated test(s):

Expand Down
4 changes: 2 additions & 2 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ const (
func NewCommandDiff() *cobra.Command {
var command = new(cobra.Command)
command.Use = CMD_USAGE_DIFF
command.Short = "Report on differences between two BOM files using RFC 6902 format"
command.Long = "Report on differences between two BOM files using RFC 6902 format"
command.Short = "(experimental) Report on differences between two similar BOM files using RFC 6902 format"
command.Long = "(experimental) Report on differences between two similar BOM files using RFC 6902 format"
command.Flags().StringVarP(&utils.GlobalFlags.PersistentFlags.OutputFormat, FLAG_FILE_OUTPUT_FORMAT, "", FORMAT_TEXT,
FLAG_DIFF_OUTPUT_FORMAT_HELP+DIFF_OUTPUT_SUPPORTED_FORMATS)
command.Flags().StringVarP(&utils.GlobalFlags.DiffFlags.RevisedFile,
Expand Down
45 changes: 36 additions & 9 deletions cmd/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,75 +75,102 @@ func innerDiffError(t *testing.T, baseFilename string, revisedFilename string, f
}

func TestDiffCdx14MatureDeltaDefault(t *testing.T) {
innerDiffError(t,
err := innerDiffError(t,
TEST_CDX_1_4_MATURITY_EXAMPLE_1_BASE,
TEST_CDX_1_4_MATURITY_EXAMPLE_1_DELTA,
FORMAT_DEFAULT,
nil)
if err != nil {
t.Error(err)
}
}

func TestDiffCdx14MatureDeltaText(t *testing.T) {
innerDiffError(t,
err := innerDiffError(t,
TEST_CDX_1_4_MATURITY_EXAMPLE_1_BASE,
TEST_CDX_1_4_MATURITY_EXAMPLE_1_DELTA,
FORMAT_TEXT,
nil)
if err != nil {
t.Error(err)
}
}

func TestDiffCdx14MatureDeltaJson(t *testing.T) {
innerDiffError(t,
err := innerDiffError(t,
TEST_CDX_1_4_MATURITY_EXAMPLE_1_BASE,
TEST_CDX_1_4_MATURITY_EXAMPLE_1_DELTA,
FORMAT_JSON,
nil)
if err != nil {
t.Error(err)
}
}

func TestDiffJsonArrayOrderMove2ObjectsFormatJson(t *testing.T) {
innerDiffError(t,
err := innerDiffError(t,
TEST_ARRAY_ORDER_2_CHANGES_BASE,
TEST_ARRAY_ORDER_2_CHANGES_DELTA,
FORMAT_JSON,
nil)
if err != nil {
t.Error(err)
}
}

func TestDiffJsonArrayOrderMove1ObjectFormatJson(t *testing.T) {
innerDiffError(t,
err := innerDiffError(t,
TEST_ARRAY_ORDER_CHANGE_BASE,
TEST_ARRAY_ORDER_CHANGE_DELTA,
FORMAT_JSON,
nil)
if err != nil {
t.Error(err)
}
}

func TestDiffJsonArrayOrderMove1ObjectFormatText(t *testing.T) {
innerDiffError(t,
err := innerDiffError(t,
TEST_ARRAY_ORDER_CHANGE_BASE,
TEST_ARRAY_ORDER_CHANGE_DELTA,
FORMAT_TEXT,
nil)
if err != nil {
t.Error(err)
}
}

func TestDiffJsonArrayOrderMove1ObjectWithDeleteFormatText(t *testing.T) {
innerDiffError(t,
err := innerDiffError(t,
TEST_ARRAY_ORDER_CHANGE_WITH_DELETE_BASE,
TEST_ARRAY_ORDER_CHANGE_WITH_DELETE_DELTA,
FORMAT_TEXT,
nil)
if err != nil {
t.Error(err)
}
}

func TestDiffJsonArrayOrderMove1ObjectWithAddFormatText(t *testing.T) {
innerDiffError(t,
err := innerDiffError(t,
TEST_ARRAY_ORDER_CHANGE_WITH_ADD_BASE,
TEST_ARRAY_ORDER_CHANGE_WITH_ADD_DELTA,
FORMAT_TEXT,
nil)
if err != nil {
t.Error(err)
}
}

func TestDiffJsonArrayOrderMove1ObjectWithAddAndDeleteFormatText(t *testing.T) {
innerDiffError(t,
err := innerDiffError(t,
TEST_ARRAY_ORDER_CHANGE_WITH_ADD_AND_DELETE_BASE,
TEST_ARRAY_ORDER_CHANGE_WITH_ADD_AND_DELETE_DELTA,
FORMAT_TEXT,
nil)
if err != nil {
t.Error(err)
}
}

// func debugDeltas(deltas []diff.Delta, indent string) (err error) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ func hashComponentLicense(bom *schema.BOM, policyConfig *schema.LicensePolicyCon

getLogger().Warningf("%s: %s (name:`%s`, version: `%s`, package-url: `%s`)",
"No license found for component. bomRef",
cdxComponent.BOMRef,
cdxComponent.Name,
licenseInfo.BOMRef,
licenseInfo.ResourceName,
cdxComponent.Version,
cdxComponent.Purl)
// No actual licenses to process
Expand Down
60 changes: 48 additions & 12 deletions cmd/license_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,10 @@ func TestLicensePolicyListWrapFalse(t *testing.T) {
// sanity (spot) check row values
lti.ResultLineContainsValuesAtLineNum = 2
lti.ResultLineContainsValues = []string{"0BSD", schema.POLICY_ALLOW}
innerTestLicensePolicyList(t, lti)
_, err := innerTestLicensePolicyList(t, lti)
if err != nil {
t.Error(err)
}
}

func TestLicensePolicyListWrapTrue(t *testing.T) {
Expand All @@ -523,7 +526,10 @@ func TestLicensePolicyListWrapTrue(t *testing.T) {
// sanity (spot) check row values
lti.ResultLineContainsValuesAtLineNum = 2
lti.ResultLineContainsValues = []string{"0BSD", schema.POLICY_ALLOW}
innerTestLicensePolicyList(t, lti)
_, err := innerTestLicensePolicyList(t, lti)
if err != nil {
t.Error(err)
}
}

//--------------------------------------------------------------
Expand All @@ -536,7 +542,10 @@ func TestLicensePolicyCustomListWhereTestUsagePolicyAllow(t *testing.T) {
lti.PolicyFile = POLICY_FILE_GOOD_BAD_MAYBE
lti.WhereClause = "usage-policy=allow"
lti.ResultExpectedLineCount = 3
innerTestLicensePolicyList(t, lti)
_, err := innerTestLicensePolicyList(t, lti)
if err != nil {
t.Error(err)
}
}

// Test using custom policy file with just 3 entries: good|bad|maybe
Expand All @@ -545,7 +554,10 @@ func TestLicensePolicyListCustomWhereTestUsagePolicyDeny(t *testing.T) {
lti.PolicyFile = POLICY_FILE_GOOD_BAD_MAYBE
lti.WhereClause = "usage-policy=deny"
lti.ResultExpectedLineCount = 3
innerTestLicensePolicyList(t, lti)
_, err := innerTestLicensePolicyList(t, lti)
if err != nil {
t.Error(err)
}
}

// Test using custom policy file with just 3 entries: good|bad|maybe
Expand All @@ -554,7 +566,10 @@ func TestLicensePolicyListCustomWhereTestUsagePolicyNeedsReview(t *testing.T) {
lti.PolicyFile = POLICY_FILE_GOOD_BAD_MAYBE
lti.WhereClause = "usage-policy=needs-review"
lti.ResultExpectedLineCount = 3
innerTestLicensePolicyList(t, lti)
_, err := innerTestLicensePolicyList(t, lti)
if err != nil {
t.Error(err)
}
}

// Test using custom policy file with just 3 entries: good|bad|maybe
Expand All @@ -563,7 +578,10 @@ func TestLicensePolicyListCustomCSVWhereTestUsagePolicyAllow(t *testing.T) {
lti.PolicyFile = POLICY_FILE_GOOD_BAD_MAYBE
lti.WhereClause = "usage-policy=allow"
lti.ResultExpectedLineCount = 2
innerTestLicensePolicyList(t, lti)
_, err := innerTestLicensePolicyList(t, lti)
if err != nil {
t.Error(err)
}
}

// Test using custom policy file with just 3 entries: good|bad|maybe
Expand All @@ -572,7 +590,10 @@ func TestLicensePolicyListCustomMarkdownWhereTestUsagePolicyAllow(t *testing.T)
lti.PolicyFile = POLICY_FILE_GOOD_BAD_MAYBE
lti.WhereClause = "usage-policy=allow"
lti.ResultExpectedLineCount = 3
innerTestLicensePolicyList(t, lti)
_, err := innerTestLicensePolicyList(t, lti)
if err != nil {
t.Error(err)
}
}

//--------------------------------------------------------------
Expand All @@ -584,14 +605,20 @@ func TestLicensePolicyListTextWhereId0BSD(t *testing.T) {
lti.WhereClause = "id=0B"
lti.ResultLineContainsValuesAtLineNum = 3
lti.ResultExpectedLineCount = 3
innerTestLicensePolicyList(t, lti)
_, err := innerTestLicensePolicyList(t, lti)
if err != nil {
t.Error(err)
}
}

func TestLicensePolicyListWhereUsagePolicyDeny(t *testing.T) {
lti := NewLicensePolicyTestInfoBasic(FORMAT_TEXT, false)
lti.WhereClause = "usage-policy=deny"
lti.ResultExpectedLineCount = 5
innerTestLicensePolicyList(t, lti)
_, err := innerTestLicensePolicyList(t, lti)
if err != nil {
t.Error(err)
}
}

func TestLicensePolicyListWhereAnnotationNeedsIPApproval(t *testing.T) {
Expand Down Expand Up @@ -629,21 +656,30 @@ func TestLicensePolicyListWhereFamilyApache(t *testing.T) {
// sanity (spot) check row values
lti.ResultLineContainsValuesAtLineNum = 2
lti.ResultLineContainsValues = []string{"Apache v1.0", schema.POLICY_ALLOW}
innerTestLicensePolicyList(t, lti)
_, err := innerTestLicensePolicyList(t, lti)
if err != nil {
t.Error(err)
}
}

func TestLicensePolicyListWhereAliases(t *testing.T) {
lti := NewLicensePolicyTestInfoBasic(FORMAT_TEXT, false)
lti.WhereClause = "aliases=Apache"
lti.ResultExpectedLineCount = 3
innerTestLicensePolicyList(t, lti)
_, err := innerTestLicensePolicyList(t, lti)
if err != nil {
t.Error(err)
}
}

func TestLicensePolicyListWhereDeprecatedTrue(t *testing.T) {
lti := NewLicensePolicyTestInfoBasic(FORMAT_TEXT, false)
lti.WhereClause = "deprecated=true"
lti.ResultExpectedLineCount = 17 // 15 matches + 2 title rows
innerTestLicensePolicyList(t, lti)
_, err := innerTestLicensePolicyList(t, lti)
if err != nil {
t.Error(err)
}
}

//------------------------------------------------
Expand Down
14 changes: 1 addition & 13 deletions cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,6 @@ func queryCmdImpl(cmd *cobra.Command, args []string) (err error) {
return errQuery
}

// // Convert query results to formatted JSON for output
// fResult, errFormat := utils.ConvertMapToJson(result)

// if errFormat != nil {
// return errFormat
// }

// // Always, output the (JSON) formatted data directly to stdout (for now)
// // NOTE: This output is NOT subject to log-level settings; use `fmt` package
// // TODO: support --output to file
// fmt.Printf("%s\n", fResult)

return
}

Expand Down Expand Up @@ -420,7 +408,7 @@ func Query(writer io.Writer, request *QueryRequest, response *QueryResponse) (re
}

// Convert query results to formatted JSON for output
fResult, err := utils.ConvertMapToJson(resultJson)
fResult, err := utils.ConvertAnyToFormattedJson(resultJson)

if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion cmd/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func VerifySelectedFieldsInJsonMap(t *testing.T, request *QueryRequest, results
func printResult(iResult interface{}) {
if !*TestLogQuiet {
// Format results in JSON
fResult, _ := utils.ConvertMapToJson(iResult)
fResult, _ := utils.ConvertAnyToFormattedJson(iResult)
// Output the JSON data directly to stdout (not subject to log-level)
fmt.Printf("%s\n", fResult)
}
Expand Down
Loading