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

     1  package cyclonedxxml
     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  		false,
    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  		false,
    31  		cycloneDxRedactor,
    32  	)
    33  }
    34  
    35  func cycloneDxRedactor(s []byte) []byte {
    36  	serialPattern := regexp.MustCompile(`serialNumber="[a-zA-Z0-9\-:]+"`)
    37  	rfc3339Pattern := regexp.MustCompile(`([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]))`)
    38  	sha256Pattern := regexp.MustCompile(`sha256:[A-Fa-f0-9]{64}`)
    39  
    40  	for _, pattern := range []*regexp.Regexp{serialPattern, rfc3339Pattern, sha256Pattern} {
    41  		s = pattern.ReplaceAll(s, []byte("redacted"))
    42  	}
    43  
    44  	// the bom-ref will be autogenerated every time, the value here should not be directly tested in snapshot tests
    45  	bomRefPattern := regexp.MustCompile(` bom-ref="[a-zA-Z0-9\-:]+"`)
    46  	bomRef3339Pattern := regexp.MustCompile(`([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]))`)
    47  	for _, pattern := range []*regexp.Regexp{bomRefPattern, bomRef3339Pattern} {
    48  		s = pattern.ReplaceAll(s, []byte(""))
    49  	}
    50  
    51  	return s
    52  }