github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/pkg/cataloger/arch/parse_alpm_db_test.go (about) 1 package arch 2 3 import ( 4 "bufio" 5 "os" 6 "testing" 7 "time" 8 9 "github.com/google/go-cmp/cmp" 10 "github.com/stretchr/testify/require" 11 12 "github.com/anchore/syft/syft/file" 13 "github.com/anchore/syft/syft/pkg" 14 ) 15 16 func TestDatabaseParser(t *testing.T) { 17 tests := []struct { 18 name string 19 fixture string 20 expected *parsedData 21 }{ 22 { 23 name: "simple desc parsing", 24 fixture: "test-fixtures/files", 25 expected: &parsedData{ 26 AlpmDBEntry: pkg.AlpmDBEntry{ 27 Backup: []pkg.AlpmFileRecord{ 28 { 29 Path: "/etc/pacman.conf", 30 Digests: []file.Digest{{ 31 Algorithm: "md5", 32 Value: "de541390e52468165b96511c4665bff4", 33 }}, 34 }, 35 { 36 Path: "/etc/makepkg.conf", 37 Digests: []file.Digest{{ 38 Algorithm: "md5", 39 Value: "79fce043df7dfc676ae5ecb903762d8b", 40 }}, 41 }, 42 }, 43 Files: []pkg.AlpmFileRecord{ 44 { 45 Path: "/etc/", 46 }, 47 { 48 Path: "/etc/makepkg.conf", 49 }, 50 { 51 Path: "/etc/pacman.conf", 52 }, 53 { 54 Path: "/usr/", 55 }, 56 { 57 Path: "/usr/bin/", 58 }, 59 { 60 Path: "/usr/bin/makepkg", 61 }, 62 { 63 Path: "/usr/bin/makepkg-template", 64 }, 65 { 66 Path: "/usr/bin/pacman", 67 }, 68 { 69 Path: "/usr/bin/pacman-conf", 70 }, 71 { 72 Path: "/var/", 73 }, 74 { 75 Path: "/var/cache/", 76 }, 77 { 78 Path: "/var/cache/pacman/", 79 }, 80 { 81 Path: "/var/cache/pacman/pkg/", 82 }, 83 { 84 Path: "/var/lib/", 85 }, 86 { 87 Path: "/var/lib/pacman/", 88 }, 89 }, 90 }, 91 }, 92 }, 93 { 94 name: "with dependencies", 95 fixture: "test-fixtures/installed/var/lib/pacman/local/gmp-6.2.1-2/desc", 96 expected: &parsedData{ 97 Licenses: "LGPL3\nGPL", 98 AlpmDBEntry: pkg.AlpmDBEntry{ 99 BasePackage: "gmp", 100 Package: "gmp", 101 Version: "6.2.1-2", 102 Description: "A free library for arbitrary precision arithmetic", 103 Architecture: "x86_64", 104 Size: 1044438, 105 Packager: "Antonio Rojas <arojas@archlinux.org>", 106 URL: "https://gmplib.org/", 107 Validation: "pgp", 108 Reason: 1, 109 Files: []pkg.AlpmFileRecord{}, 110 Backup: []pkg.AlpmFileRecord{}, 111 Depends: []string{"gcc-libs", "sh", "libtree-sitter.so=1-64"}, 112 }, 113 }, 114 }, 115 { 116 name: "with provides", 117 fixture: "test-fixtures/installed/var/lib/pacman/local/tree-sitter-0.22.6-1/desc", 118 expected: &parsedData{ 119 Licenses: "MIT", 120 AlpmDBEntry: pkg.AlpmDBEntry{ 121 BasePackage: "tree-sitter", 122 Package: "tree-sitter", 123 Version: "0.22.6-1", 124 Description: "Incremental parsing library", 125 Architecture: "x86_64", 126 Size: 223539, 127 Packager: "Daniel M. Capella <polyzen@archlinux.org>", 128 URL: "https://github.com/tree-sitter/tree-sitter", 129 Validation: "pgp", 130 Reason: 1, 131 Files: []pkg.AlpmFileRecord{}, 132 Backup: []pkg.AlpmFileRecord{}, 133 Provides: []string{"libtree-sitter.so=0-64"}, 134 }, 135 }, 136 }, 137 } 138 139 for _, test := range tests { 140 t.Run(test.name, func(t *testing.T) { 141 f, err := os.Open(test.fixture) 142 require.NoError(t, err) 143 t.Cleanup(func() { require.NoError(t, f.Close()) }) 144 145 reader := bufio.NewReader(f) 146 147 entry, err := parseAlpmDBEntry(reader) 148 require.NoError(t, err) 149 150 if diff := cmp.Diff(test.expected, entry); diff != "" { 151 t.Errorf("parsed data mismatch (-want +got):\n%s", diff) 152 } 153 154 }) 155 } 156 } 157 158 func parseTime(stime string) time.Time { 159 t, _ := time.Parse(time.RFC3339, stime) 160 return t 161 } 162 163 func TestMtreeParse(t *testing.T) { 164 tests := []struct { 165 name string 166 expected []pkg.AlpmFileRecord 167 }{ 168 { 169 name: "test mtree parsing", 170 expected: []pkg.AlpmFileRecord{ 171 { 172 Path: "/etc", 173 Type: "dir", 174 Time: parseTime("2022-04-10T14:59:52+02:00"), 175 Digests: make([]file.Digest, 0), 176 }, 177 { 178 Path: "/etc/pacman.d", 179 Type: "dir", 180 Time: parseTime("2022-04-10T14:59:52+02:00"), 181 Digests: make([]file.Digest, 0), 182 }, 183 { 184 Path: "/etc/pacman.d/mirrorlist", 185 Size: "44683", 186 Time: parseTime("2022-04-10T14:59:52+02:00"), 187 Digests: []file.Digest{ 188 { 189 Algorithm: "md5", 190 Value: "81c39827e38c759d7e847f05db62c233", 191 }, 192 { 193 Algorithm: "sha256", 194 Value: "fc135ab26f2a227b9599b66a2f1ba325c445acb914d60e7ecf6e5997a87abe1e", 195 }, 196 }, 197 }, 198 }, 199 }, 200 } 201 202 for _, test := range tests { 203 t.Run(test.name, func(t *testing.T) { 204 f, err := os.Open("test-fixtures/mtree") 205 require.NoError(t, err) 206 t.Cleanup(func() { require.NoError(t, f.Close()) }) 207 208 reader := bufio.NewReader(f) 209 210 entry, err := parseMtree(reader) 211 require.NoError(t, err) 212 213 if diff := cmp.Diff(entry, test.expected); diff != "" { 214 t.Errorf("Files mismatch (-want +got):\n%s", diff) 215 } 216 }) 217 } 218 219 }