github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/formats/cyclonedxxml/encoder_test.go (about)

     1  package cyclonedxxml
     2  
     3  import (
     4  	"flag"
     5  	"testing"
     6  
     7  	"github.com/anchore/syft/syft/formats/internal/testutils"
     8  )
     9  
    10  var updateSnapshot = flag.Bool("update-cyclonedx-xml", false, "update the *.golden files for cyclone-dx XML encoders")
    11  var updateImage = flag.Bool("update-image", false, "update the golden image used for image encoder testing")
    12  
    13  func TestCycloneDxDirectoryEncoder(t *testing.T) {
    14  	dir := t.TempDir()
    15  	testutils.AssertEncoderAgainstGoldenSnapshot(t,
    16  		testutils.EncoderSnapshotTestConfig{
    17  			Subject:                     testutils.DirectoryInput(t, dir),
    18  			Format:                      Format(),
    19  			UpdateSnapshot:              *updateSnapshot,
    20  			PersistRedactionsInSnapshot: true,
    21  			IsJSON:                      false,
    22  			Redactor:                    redactor(dir),
    23  		},
    24  	)
    25  }
    26  
    27  func TestCycloneDxImageEncoder(t *testing.T) {
    28  	testImage := "image-simple"
    29  	testutils.AssertEncoderAgainstGoldenImageSnapshot(t,
    30  		testutils.ImageSnapshotTestConfig{
    31  			Image:               testImage,
    32  			UpdateImageSnapshot: *updateImage,
    33  		},
    34  		testutils.EncoderSnapshotTestConfig{
    35  			Subject:                     testutils.ImageInput(t, testImage),
    36  			Format:                      Format(),
    37  			UpdateSnapshot:              *updateSnapshot,
    38  			PersistRedactionsInSnapshot: true,
    39  			IsJSON:                      false,
    40  			Redactor:                    redactor(),
    41  		},
    42  	)
    43  }
    44  
    45  func redactor(values ...string) testutils.Redactor {
    46  	return testutils.NewRedactions().
    47  		WithValuesRedacted(values...).
    48  		WithPatternRedactors(
    49  			map[string]string{
    50  				// serial numbers
    51  				`serialNumber="[a-zA-Z0-9\-:]+`: `serialNumber="redacted`,
    52  
    53  				// dates
    54  				`([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]))`: `redacted`,
    55  
    56  				// image hashes
    57  				`sha256:[A-Za-z0-9]{64}`: `sha256:redacted`,
    58  
    59  				// BOM refs
    60  				`bom-ref="[a-zA-Z0-9\-:]+"`: `bom-ref:redacted`,
    61  			},
    62  		)
    63  }