github.com/google/osv-scalibr@v0.4.1/detector/endoflife/linuxdistro/linuxdistro_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 linuxdistro 16 17 import ( 18 "errors" 19 "io/fs" 20 "os" 21 "testing" 22 "time" 23 24 "github.com/google/go-cmp/cmp" 25 scalibrfs "github.com/google/osv-scalibr/fs" 26 "github.com/google/osv-scalibr/inventory" 27 ) 28 29 type fakeFS map[string]string 30 31 func (f fakeFS) Open(name string) (fs.File, error) { 32 return os.Open(f[name]) 33 } 34 func (fakeFS) ReadDir(name string) ([]fs.DirEntry, error) { 35 return nil, errors.New("not implemented") 36 } 37 func (fakeFS) Stat(name string) (fs.FileInfo, error) { 38 return nil, errors.New("not implemented") 39 } 40 41 func TestEOLLinuxDistro(t *testing.T) { 42 wantDescription := "The system is running a Linux distribution that has reached " + 43 "end-of-life (EOL) and is no longer maintained by the vendor. This means it no " + 44 "longer receives security updates or patches." 45 wantRecommendation := "Upgrade the operating system to a supported release or arrange " + 46 "an extended support with the vendor." 47 tests := []struct { 48 name string 49 now string 50 fsys scalibrfs.FS 51 wantFinding []*inventory.GenericFinding 52 }{ 53 { 54 name: "fedora-42-not-eol", 55 now: "2020-01-01", 56 fsys: fakeFS{ 57 "etc/os-release": "testdata/fedora_42_os_release", 58 }, 59 }, 60 { 61 name: "fedora-42-eol", 62 now: "2030-01-01", 63 fsys: fakeFS{ 64 "etc/os-release": "testdata/fedora_42_os_release", 65 }, 66 wantFinding: []*inventory.GenericFinding{{ 67 Adv: &inventory.GenericFindingAdvisory{ 68 ID: &inventory.AdvisoryID{ 69 Publisher: "SCALIBR", 70 Reference: "linux-end-of-life", 71 }, 72 Title: "End-of-Life operating system", 73 Description: wantDescription, 74 Recommendation: wantRecommendation, 75 Sev: inventory.SeverityCritical, 76 }, 77 Target: &inventory.GenericFindingTargetDetails{Extra: "distro: fedora"}, 78 }}, 79 }, 80 { 81 name: "ubuntu-22-04-not-eol", 82 now: "2026-01-01", 83 fsys: fakeFS{ 84 "etc/os-release": "testdata/ubuntu_22.04_os_release", 85 }, 86 }, 87 { 88 name: "ubuntu-22-04-eol", 89 now: "2030-01-01", 90 fsys: fakeFS{ 91 "etc/os-release": "testdata/ubuntu_22.04_os_release", 92 }, 93 wantFinding: []*inventory.GenericFinding{{ 94 Adv: &inventory.GenericFindingAdvisory{ 95 ID: &inventory.AdvisoryID{ 96 Publisher: "SCALIBR", 97 Reference: "linux-end-of-life", 98 }, 99 Title: "End-of-Life operating system", 100 Description: wantDescription, 101 Recommendation: wantRecommendation, 102 Sev: inventory.SeverityCritical, 103 }, 104 Target: &inventory.GenericFindingTargetDetails{Extra: "distro: ubuntu"}, 105 }}, 106 }, 107 { 108 name: "ubuntu-22-04-pro-not-eol", 109 now: "2030-01-01", 110 fsys: fakeFS{ 111 "etc/os-release": "testdata/ubuntu_22.04_os_release", 112 "/etc/apt/sources.list.d/ubuntu-esm-infra.sources": "testdata/ubuntu-esm-infra.sources", 113 }, 114 }, 115 } 116 117 for _, tc := range tests { 118 t.Run(tc.name, func(t *testing.T) { 119 now = func() time.Time { 120 n, err := time.Parse("2006-01-02", tc.now) 121 if err != nil { 122 t.Fatalf("detector.Scan(%v) invalid date: %v", tc.now, err) 123 } 124 return n 125 } 126 d := Detector{} 127 finding, err := d.Scan(t.Context(), &scalibrfs.ScanRoot{FS: tc.fsys}, nil) 128 if err != nil { 129 t.Errorf("detector.Scan(%v) unexpected error: %v", tc.fsys, err) 130 } 131 if diff := cmp.Diff(tc.wantFinding, finding.GenericFindings); diff != "" { 132 t.Errorf("detector.Scan(%v): unexpected findings (-want +got):\n%s", tc.fsys, diff) 133 } 134 }) 135 } 136 }