github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/formats/cyclonedxjson/encoder_test.go (about)

     1  package cyclonedxjson
     2  
     3  import (
     4  	"flag"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/nextlinux/gosbom/gosbom/formats/internal/testutils"
     9  )
    10  
    11  var updateCycloneDx = flag.Bool("update-cyclonedx", false, "update the *.golden files for cyclone-dx encoders")
    12  
    13  func TestCycloneDxDirectoryEncoder(t *testing.T) {
    14  	testutils.AssertEncoderAgainstGoldenSnapshot(t,
    15  		Format(),
    16  		testutils.DirectoryInput(t),
    17  		*updateCycloneDx,
    18  		true,
    19  		cycloneDxRedactor,
    20  	)
    21  }
    22  
    23  func TestCycloneDxImageEncoder(t *testing.T) {
    24  	testImage := "image-simple"
    25  	testutils.AssertEncoderAgainstGoldenImageSnapshot(t,
    26  		Format(),
    27  		testutils.ImageInput(t, testImage),
    28  		testImage,
    29  		*updateCycloneDx,
    30  		true,
    31  		cycloneDxRedactor,
    32  	)
    33  }
    34  
    35  func cycloneDxRedactor(s []byte) []byte {
    36  	replacements := map[string]string{
    37  		// UUIDs
    38  		`urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}`: `urn:uuid:redacted`,
    39  		// timestamps
    40  		`([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))`: `timestamp:redacted`,
    41  		// image hashes
    42  		`sha256:[A-Fa-f0-9]{64}`: `sha256:redacted`,
    43  		// bom-refs
    44  		`"bom-ref":\s*"[^"]+"`: `"bom-ref": "redacted"`,
    45  	}
    46  	for pattern, replacement := range replacements {
    47  		s = regexp.MustCompile(pattern).ReplaceAll(s, []byte(replacement))
    48  	}
    49  	return s
    50  }