|
| 1 | +package sbom_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 9 | + "github.com/spdx/tools-golang/spdx" |
| 10 | + "github.com/spdx/tools-golang/spdx/v2/common" |
| 11 | + |
| 12 | + "github.com/future-architect/vuls/models" |
| 13 | + "github.com/future-architect/vuls/reporter/sbom" |
| 14 | +) |
| 15 | + |
| 16 | +func TestToSPDX(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + args models.ScanResult |
| 20 | + want spdx.Document |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "windows", |
| 24 | + args: models.ScanResult{ |
| 25 | + Family: "windows", |
| 26 | + Release: "Windows Server 2022", |
| 27 | + ReportedAt: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC), |
| 28 | + ReportedVersion: "v0.38.5", |
| 29 | + ReportedRevision: "build-20260311_001506_6827f2d", |
| 30 | + WindowsKB: &models.WindowsKB{ |
| 31 | + Applied: []string{"5025221", "5022282"}, |
| 32 | + Unapplied: []string{"5026370"}, |
| 33 | + }, |
| 34 | + }, |
| 35 | + want: spdx.Document{ |
| 36 | + SPDXVersion: spdx.Version, |
| 37 | + DataLicense: spdx.DataLicense, |
| 38 | + SPDXIdentifier: "DOCUMENT", |
| 39 | + DocumentName: "windows", |
| 40 | + CreationInfo: &spdx.CreationInfo{ |
| 41 | + Creators: []common.Creator{ |
| 42 | + {Creator: "future-architect", CreatorType: "Organization"}, |
| 43 | + {Creator: "test-tool", CreatorType: "Tool"}, |
| 44 | + }, |
| 45 | + Created: "2025-01-01T00:00:00Z", |
| 46 | + }, |
| 47 | + Packages: []*spdx.Package{ |
| 48 | + { |
| 49 | + PackageName: "windows", |
| 50 | + PackageVersion: "Windows Server 2022", |
| 51 | + PackageDownloadLocation: "NONE", |
| 52 | + PrimaryPackagePurpose: "OPERATING-SYSTEM", |
| 53 | + Annotations: []spdx.Annotation{ |
| 54 | + { |
| 55 | + Annotator: spdx.Annotator{Annotator: "future-architect:vuls", AnnotatorType: "Tool"}, |
| 56 | + AnnotationDate: "2025-01-01T00:00:00Z", |
| 57 | + AnnotationType: "Other", |
| 58 | + AnnotationComment: "OsFamily: windows", |
| 59 | + }, |
| 60 | + { |
| 61 | + Annotator: spdx.Annotator{Annotator: "future-architect:vuls", AnnotatorType: "Tool"}, |
| 62 | + AnnotationDate: "2025-01-01T00:00:00Z", |
| 63 | + AnnotationType: "Other", |
| 64 | + AnnotationComment: "WindowsKB:Applied: 5022282", |
| 65 | + }, |
| 66 | + { |
| 67 | + Annotator: spdx.Annotator{Annotator: "future-architect:vuls", AnnotatorType: "Tool"}, |
| 68 | + AnnotationDate: "2025-01-01T00:00:00Z", |
| 69 | + AnnotationType: "Other", |
| 70 | + AnnotationComment: "WindowsKB:Applied: 5025221", |
| 71 | + }, |
| 72 | + { |
| 73 | + Annotator: spdx.Annotator{Annotator: "future-architect:vuls", AnnotatorType: "Tool"}, |
| 74 | + AnnotationDate: "2025-01-01T00:00:00Z", |
| 75 | + AnnotationType: "Other", |
| 76 | + AnnotationComment: "WindowsKB:Unapplied: 5026370", |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + Relationships: []*spdx.Relationship{ |
| 82 | + {Relationship: "DESCRIBES"}, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "non-windows", |
| 88 | + args: models.ScanResult{ |
| 89 | + Family: "centos", |
| 90 | + Release: "7", |
| 91 | + ReportedAt: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC), |
| 92 | + ReportedVersion: "v0.38.5", |
| 93 | + ReportedRevision: "build-20260311_001506_6827f2d", |
| 94 | + }, |
| 95 | + want: spdx.Document{ |
| 96 | + SPDXVersion: spdx.Version, |
| 97 | + DataLicense: spdx.DataLicense, |
| 98 | + SPDXIdentifier: "DOCUMENT", |
| 99 | + DocumentName: "centos", |
| 100 | + CreationInfo: &spdx.CreationInfo{ |
| 101 | + Creators: []common.Creator{ |
| 102 | + {Creator: "future-architect", CreatorType: "Organization"}, |
| 103 | + {Creator: "test-tool", CreatorType: "Tool"}, |
| 104 | + }, |
| 105 | + Created: "2025-01-01T00:00:00Z", |
| 106 | + }, |
| 107 | + Packages: []*spdx.Package{ |
| 108 | + { |
| 109 | + PackageName: "centos", |
| 110 | + PackageVersion: "7", |
| 111 | + PackageDownloadLocation: "NONE", |
| 112 | + PrimaryPackagePurpose: "OPERATING-SYSTEM", |
| 113 | + Annotations: []spdx.Annotation{ |
| 114 | + { |
| 115 | + Annotator: spdx.Annotator{Annotator: "future-architect:vuls", AnnotatorType: "Tool"}, |
| 116 | + AnnotationDate: "2025-01-01T00:00:00Z", |
| 117 | + AnnotationType: "Other", |
| 118 | + AnnotationComment: "OsFamily: centos", |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + Relationships: []*spdx.Relationship{ |
| 124 | + {Relationship: "DESCRIBES"}, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + } |
| 129 | + for _, tt := range tests { |
| 130 | + t.Run(tt.name, func(t *testing.T) { |
| 131 | + got := sbom.ToSPDX(tt.args, "test-tool") |
| 132 | + |
| 133 | + opts := cmp.Options{ |
| 134 | + cmpopts.IgnoreUnexported(spdx.Package{}), |
| 135 | + cmpopts.IgnoreFields(spdx.Document{}, "DocumentNamespace"), |
| 136 | + cmpopts.IgnoreFields(spdx.Package{}, "PackageSPDXIdentifier"), |
| 137 | + cmpopts.IgnoreFields(spdx.Relationship{}, "RefA", "RefB"), |
| 138 | + } |
| 139 | + if diff := cmp.Diff(tt.want, got, opts...); diff != "" { |
| 140 | + t.Errorf("ToSPDX() mismatch (-want +got):\n%s", diff) |
| 141 | + } |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
0 commit comments