Skip to content

Commit

Permalink
Add invalid test for RFC 6902 add operation
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Rutkowski <[email protected]>
  • Loading branch information
mrutkows committed Jan 10, 2024
1 parent 88de919 commit b8d097b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 7 deletions.
15 changes: 15 additions & 0 deletions cmd/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ func processPatchRecords(bomDocument *schema.BOM, patchDocument *IETF6902Documen

switch record.Operation {
case IETF_RFC6902_OP_ADD:
// var mapRecord map[string]interface{}
// recordBytes, _ := json.Marshal(record)
// json.Unmarshal(recordBytes, &mapRecord)
// delete(mapRecord, "value")
if err = addValue(bomDocument.JsonMap, record.Path, record.Value); err != nil {
return
}
Expand Down Expand Up @@ -227,10 +231,21 @@ func processPatchRecords(bomDocument *schema.BOM, patchDocument *IETF6902Documen
//
// - If the target location specifies an object member that does exist,
// that member's value is replaced.
//
// The operation object MUST contain a "value" member whose content
// specifies the value to be added.
func addValue(jsonMap map[string]interface{}, path string, value interface{}) (err error) {

// The operation object MUST contain a "value" member whose content
// specifies the value to be added.
if value == nil {
// TODO: make this a declared error type that can be tested
return fmt.Errorf("invalid IETF RFC 6902 patch operation. \"value\" missing")
}

// parse path returns index (-1 if not specified) and either a slice or json map
// if path is a slice, verify that the value matches the expected type
// if path is a map, assure value matches the expected type

return
}
12 changes: 12 additions & 0 deletions cmd/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
const (
// Patch test RFC 6901 patch files
TEST_PATCH_METADATA_PROPERTIES_1 = "test/patch/cdx-patch-metadata-properties-1.json"

TEST_PATCH_ERR_MISSING_VALUE = "test/patch/cdx-patch-add-err-missing-value.json"
)

type PatchTestInfo struct {
Expand Down Expand Up @@ -187,3 +189,13 @@ func TestPatchCdx15(t *testing.T) {
}
fmt.Printf("%s\n", buffer.String())
}

func TestPatchAddErrorMissingValue(t *testing.T) {
ti := NewPatchTestInfo(TEST_PATCH_BOM_1_5_MATURE_BASE, TEST_PATCH_ERR_MISSING_VALUE, nil)
ti.OutputFile = ti.CreateTemporaryTestOutputFilename(TEST_PATCH_BOM_1_5_MATURE_BASE)
_, _, err := innerTestPatch(t, ti)
// Expected an error
if err == nil {
t.Error(err)
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/evanphx/json-patch v0.5.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jessevdk/go-flags v1.4.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/evanphx/json-patch v0.5.2 h1:xVCHIVMUu1wtM/VkR9jVZ45N3FhZfYMMYGorLCR8P3k=
github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
Expand All @@ -13,6 +15,8 @@ github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJ
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jwangsadinata/go-multimap v0.0.0-20190620162914-c29f3d7f33b6 h1:OzCtZaD1uI5Fc1C+4oNAp7kZ4ibh5OIgxI29moH/IbE=
github.com/jwangsadinata/go-multimap v0.0.0-20190620162914-c29f3d7f33b6/go.mod h1:CEusGbCRDFcHX9EgEhPsgJX33kpp9CfSFRBAoSGOems=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand All @@ -32,6 +36,8 @@ github.com/mrutkows/go-jsondiff v0.2.0/go.mod h1:TuasE0Ldrf4r1Gp0uIatS9SnPZPYybj
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/gomega v1.27.7 h1:fVih9JD6ogIiHUN6ePK7HJidyEDpWGVB5mzM7cWNXoU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
3 changes: 3 additions & 0 deletions test/patch/cdx-patch-add-err-missing-value.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
{ "op": "add", "path": "/version" }
]
5 changes: 3 additions & 2 deletions test/patch/cdx-patch-metadata-properties-1.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[
{ "op": "add", "path": "/metadata/properties/-", "value": { "name": "foo", "value": "bar" } },
{ "op": "add", "path": "/metadata/properties/-", "value": { "name": "abc", "value": "123" } },
{ "op": "add", "path": "/metadata/properties/1", "value": { "name": "rush", "value": "yyz" } }
{ "op": "add", "path": "/metadata/properties/1", "value": { "name": "rush", "value": "yyz" } },
{ "op": "add", "path": "/version", "value": 1 },
{ "op": "add", "path": "/modified", "value": true }
]
11 changes: 6 additions & 5 deletions utils/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ func IsJsonSliceType(any interface{}) (isSliceType bool) {
return
}

func IsValidJsonMap(test string) bool {
var js map[string]interface{}
err := json.Unmarshal([]byte(test), &js)
return err == nil
}
// NOTE: currently, an unused function
// func IsValidJsonMap(test string) bool {
// var js map[string]interface{}
// err := json.Unmarshal([]byte(test), &js)
// return err == nil
// }

func IsValidJsonRaw(test []byte) bool {
var js interface{}
Expand Down

0 comments on commit b8d097b

Please sign in to comment.