github.com/google/osv-scalibr@v0.4.1/extractor/filesystem/language/cpp/conanlock/conanlock-v2_test.go (about) 1 // Copyright 2025 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package conanlock_test 16 17 import ( 18 "testing" 19 20 "github.com/google/go-cmp/cmp" 21 "github.com/google/go-cmp/cmp/cmpopts" 22 "github.com/google/osv-scalibr/extractor" 23 "github.com/google/osv-scalibr/extractor/filesystem/language/cpp/conanlock" 24 "github.com/google/osv-scalibr/extractor/filesystem/osv" 25 "github.com/google/osv-scalibr/inventory" 26 "github.com/google/osv-scalibr/purl" 27 "github.com/google/osv-scalibr/testing/extracttest" 28 ) 29 30 func TestExtractor_Extract_v2(t *testing.T) { 31 tests := []extracttest.TestTableEntry{ 32 { 33 Name: "no packages", 34 InputConfig: extracttest.ScanInputMockConfig{ 35 Path: "testdata/empty.v2.json", 36 }, 37 WantPackages: []*extractor.Package{}, 38 }, 39 { 40 Name: "one package", 41 InputConfig: extracttest.ScanInputMockConfig{ 42 Path: "testdata/one-package.v2.json", 43 }, 44 WantPackages: []*extractor.Package{ 45 { 46 Name: "zlib", 47 Version: "1.2.11", 48 PURLType: purl.TypeConan, 49 Locations: []string{"testdata/one-package.v2.json"}, 50 Metadata: osv.DepGroupMetadata{ 51 DepGroupVals: []string{"requires"}, 52 }, 53 }, 54 }, 55 }, 56 { 57 Name: "no name", 58 InputConfig: extracttest.ScanInputMockConfig{ 59 Path: "testdata/no-name.v2.json", 60 }, 61 WantPackages: []*extractor.Package{ 62 { 63 Name: "zlib", 64 Version: "1.2.11", 65 PURLType: purl.TypeConan, 66 Locations: []string{"testdata/no-name.v2.json"}, 67 Metadata: osv.DepGroupMetadata{ 68 DepGroupVals: []string{"requires"}, 69 }, 70 }, 71 }, 72 }, 73 { 74 Name: "two packages", 75 InputConfig: extracttest.ScanInputMockConfig{ 76 Path: "testdata/two-packages.v2.json", 77 }, 78 WantPackages: []*extractor.Package{ 79 { 80 Name: "zlib", 81 Version: "1.2.11", 82 PURLType: purl.TypeConan, 83 Locations: []string{"testdata/two-packages.v2.json"}, 84 Metadata: osv.DepGroupMetadata{ 85 DepGroupVals: []string{"requires"}, 86 }, 87 }, 88 { 89 Name: "bzip2", 90 Version: "1.0.8", 91 PURLType: purl.TypeConan, 92 Locations: []string{"testdata/two-packages.v2.json"}, 93 Metadata: osv.DepGroupMetadata{ 94 DepGroupVals: []string{"requires"}, 95 }, 96 }, 97 }, 98 }, 99 { 100 Name: "nested dependencies", 101 InputConfig: extracttest.ScanInputMockConfig{ 102 Path: "testdata/nested-dependencies.v2.json", 103 }, 104 WantPackages: []*extractor.Package{ 105 { 106 Name: "zlib", 107 Version: "1.2.13", 108 PURLType: purl.TypeConan, 109 Locations: []string{"testdata/nested-dependencies.v2.json"}, 110 Metadata: osv.DepGroupMetadata{ 111 DepGroupVals: []string{"requires"}, 112 }, 113 }, 114 { 115 Name: "bzip2", 116 Version: "1.0.8", 117 PURLType: purl.TypeConan, 118 Locations: []string{"testdata/nested-dependencies.v2.json"}, 119 Metadata: osv.DepGroupMetadata{ 120 DepGroupVals: []string{"requires"}, 121 }, 122 }, 123 { 124 Name: "freetype", 125 Version: "2.12.1", 126 PURLType: purl.TypeConan, 127 Locations: []string{"testdata/nested-dependencies.v2.json"}, 128 Metadata: osv.DepGroupMetadata{ 129 DepGroupVals: []string{"requires"}, 130 }, 131 }, 132 { 133 Name: "libpng", 134 Version: "1.6.39", 135 PURLType: purl.TypeConan, 136 Locations: []string{"testdata/nested-dependencies.v2.json"}, 137 Metadata: osv.DepGroupMetadata{ 138 DepGroupVals: []string{"requires"}, 139 }, 140 }, 141 { 142 Name: "brotli", 143 Version: "1.0.9", 144 PURLType: purl.TypeConan, 145 Locations: []string{"testdata/nested-dependencies.v2.json"}, 146 Metadata: osv.DepGroupMetadata{ 147 DepGroupVals: []string{"requires"}, 148 }, 149 }, 150 }, 151 }, 152 { 153 Name: "one package dev", 154 InputConfig: extracttest.ScanInputMockConfig{ 155 Path: "testdata/one-package-dev.v2.json", 156 }, 157 WantPackages: []*extractor.Package{ 158 { 159 Name: "ninja", 160 Version: "1.11.1", 161 PURLType: purl.TypeConan, 162 Locations: []string{"testdata/one-package-dev.v2.json"}, 163 Metadata: osv.DepGroupMetadata{ 164 DepGroupVals: []string{"build-requires"}, 165 }, 166 }, 167 }, 168 }, 169 } 170 171 for _, tt := range tests { 172 t.Run(tt.Name, func(t *testing.T) { 173 extr := conanlock.Extractor{} 174 175 scanInput := extracttest.GenerateScanInputMock(t, tt.InputConfig) 176 defer extracttest.CloseTestScanInput(t, scanInput) 177 178 got, err := extr.Extract(t.Context(), &scanInput) 179 180 if diff := cmp.Diff(tt.WantErr, err, cmpopts.EquateErrors()); diff != "" { 181 t.Errorf("%s.Extract(%q) error diff (-want +got):\n%s", extr.Name(), tt.InputConfig.Path, diff) 182 return 183 } 184 185 wantInv := inventory.Inventory{Packages: tt.WantPackages} 186 if diff := cmp.Diff(wantInv, got, cmpopts.SortSlices(extracttest.PackageCmpLess)); diff != "" { 187 t.Errorf("%s.Extract(%q) diff (-want +got):\n%s", extr.Name(), tt.InputConfig.Path, diff) 188 } 189 }) 190 } 191 }