github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/nix/store_cataloger_test.go (about) 1 package nix 2 3 import ( 4 "testing" 5 6 "github.com/anchore/syft/syft/artifact" 7 "github.com/anchore/syft/syft/file" 8 "github.com/anchore/syft/syft/pkg" 9 "github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest" 10 ) 11 12 func TestStoreCataloger_Image(t *testing.T) { 13 tests := []struct { 14 fixture string 15 wantPkgs []string 16 wantRel []string 17 }{ 18 { 19 // $ nix-store -q --tree $(which jq) 20 // 21 // /nix/store/nzwfgsp28vgxv7n2gl5fxqkca9awh4dz-jq-1.6-bin3.4 22 // ├───/nix/store/02mqs1by2vab9yzw0qc4j7463w78p3ps-glibc-2.37-8 23 // │ ├───/nix/store/cw8fpl8r1x9rmaqj55fwbfnnrgw7b40k-libidn2-2.3.4 24 // │ │ ├───/nix/store/h1ysk4vvw48winwmh38rvnsj0dlsz7c1-libunistring-1.1 25 // │ │ │ └───/nix/store/h1ysk4vvw48winwmh38rvnsj0dlsz7c1-libunistring-1.1 [...] 26 // │ │ └───/nix/store/cw8fpl8r1x9rmaqj55fwbfnnrgw7b40k-libidn2-2.3.4 [...] 27 // │ ├───/nix/store/fmz62d844wf4blb11k21f4m0q6n6hdfp-xgcc-12.3.0-libgcc 28 // │ └───/nix/store/02mqs1by2vab9yzw0qc4j7463w78p3ps-glibc-2.37-8 [...] 29 // ├───/nix/store/mzj90j6m3c3a1vv8j9pl920f98i2yz9q-oniguruma-6.9.8-lib 30 // │ ├───/nix/store/02mqs1by2vab9yzw0qc4j7463w78p3ps-glibc-2.37-8 [...] 31 // │ └───/nix/store/mzj90j6m3c3a1vv8j9pl920f98i2yz9q-oniguruma-6.9.8-lib [...] 32 // └───/nix/store/1x3s2v9wc9m302cspfqcn2iwar0b5w99-jq-1.6-lib 33 // ├───/nix/store/02mqs1by2vab9yzw0qc4j7463w78p3ps-glibc-2.37-8 [...] 34 // ├───/nix/store/mzj90j6m3c3a1vv8j9pl920f98i2yz9q-oniguruma-6.9.8-lib [...] 35 // └───/nix/store/1x3s2v9wc9m302cspfqcn2iwar0b5w99-jq-1.6-lib [...] 36 fixture: "image-nixos-jq-pkg-store", 37 wantPkgs: []string{ 38 "glibc @ 2.37-8 (/nix/store/aw2fw9ag10wr9pf0qk4nk5sxi0q0bn56-glibc-2.37-8)", 39 "jq @ 1.6 (/nix/store/3xpzpmcqmzsdblkzqa9d9s6l302pnk4g-jq-1.6-lib)", // jq lib output 40 "jq @ 1.6 (/nix/store/aj8lqifsyynq8iknivvxkrsqnblj7qzs-jq-1.6-bin)", // jq bin output 41 "libidn2 @ 2.3.4 (/nix/store/k8ivghpggjrq1n49xp8sj116i4sh8lia-libidn2-2.3.4)", 42 "libunistring @ 1.1 (/nix/store/s2gi8pfjszy6rq3ydx0z1vwbbskw994i-libunistring-1.1)", 43 "oniguruma @ 6.9.8 (/nix/store/dpcyirvyblnflf7cp14dnr1420va93zx-oniguruma-6.9.8-lib)", 44 "xgcc @ 12.3.0 (/nix/store/jbwb8d8l28lg9z0xzl784wyb9vlbwss6-xgcc-12.3.0-libgcc)", 45 }, 46 wantRel: []string{ 47 // note: parsing all relationships from only derivations results in partial results! (this is why the DB cataloger exists) 48 "libidn2 @ 2.3.4 (/nix/store/k8ivghpggjrq1n49xp8sj116i4sh8lia-libidn2-2.3.4) [dependency-of] glibc @ 2.37-8 (/nix/store/aw2fw9ag10wr9pf0qk4nk5sxi0q0bn56-glibc-2.37-8)", 49 "libunistring @ 1.1 (/nix/store/s2gi8pfjszy6rq3ydx0z1vwbbskw994i-libunistring-1.1) [dependency-of] libidn2 @ 2.3.4 (/nix/store/k8ivghpggjrq1n49xp8sj116i4sh8lia-libidn2-2.3.4)", 50 "xgcc @ 12.3.0 (/nix/store/jbwb8d8l28lg9z0xzl784wyb9vlbwss6-xgcc-12.3.0-libgcc) [dependency-of] glibc @ 2.37-8 (/nix/store/aw2fw9ag10wr9pf0qk4nk5sxi0q0bn56-glibc-2.37-8)", 51 }, 52 }, 53 } 54 for _, tt := range tests { 55 t.Run(tt.fixture, func(t *testing.T) { 56 c := NewStoreCataloger() 57 pkgtest.NewCatalogTester(). 58 WithImageResolver(t, tt.fixture). 59 ExpectsPackageStrings(tt.wantPkgs). 60 ExpectsRelationshipStrings(tt.wantRel). 61 TestCataloger(t, c) 62 }) 63 } 64 } 65 66 func TestStoreCataloger_Directory(t *testing.T) { 67 tests := []struct { 68 fixture string 69 wantPkgs []pkg.Package 70 wantRel []artifact.Relationship 71 }{ 72 { 73 fixture: "test-fixtures/fixture-1", 74 wantPkgs: []pkg.Package{ 75 { 76 Name: "glibc", 77 Version: "2.34-210", 78 PURL: "pkg:nix/glibc@2.34-210?drvpath=5av396z8xa13jg89g9jws145c0k26k2x-glibc-2.34-210.drv&output=bin&outputhash=h0cnbmfcn93xm5dg2x27ixhag1cwndga", 79 Locations: file.NewLocationSet( 80 file.NewLocation("nix/store/h0cnbmfcn93xm5dg2x27ixhag1cwndga-glibc-2.34-210-bin").WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation), 81 file.NewLocation("nix/store/5av396z8xa13jg89g9jws145c0k26k2x-glibc-2.34-210.drv").WithAnnotation(pkg.EvidenceAnnotationKey, pkg.SupportingEvidenceAnnotation), 82 ), 83 FoundBy: "nix-store-cataloger", 84 Type: pkg.NixPkg, 85 Metadata: pkg.NixStoreEntry{ 86 Path: "/nix/store/h0cnbmfcn93xm5dg2x27ixhag1cwndga-glibc-2.34-210-bin", 87 Derivation: pkg.NixDerivation{ 88 Path: "nix/store/5av396z8xa13jg89g9jws145c0k26k2x-glibc-2.34-210.drv", 89 System: "aarch64-linux", 90 InputDerivations: []pkg.NixDerivationReference{ 91 { 92 Path: "/nix/store/1zi0k7y01rhqr2gfqb42if0icswg65sj-locale-C.diff.drv", 93 Outputs: []string{"out"}, 94 }, 95 { 96 Path: "/nix/store/45j86ggi8mlpfslcrgvjf7m6phia21fp-raw.drv", 97 Outputs: []string{"out"}, 98 }, 99 { 100 Path: "/nix/store/4fnfsd9sc7bam6886hwyaprdsww66dg3-bison-3.8.2.drv", 101 Outputs: []string{"out"}, 102 }, 103 { 104 Path: "/nix/store/51azdrrvcqrk2hbky7ryphlwd99yz25d-linux-headers-5.18.drv", 105 Outputs: []string{"out"}, 106 }, 107 { 108 Path: "/nix/store/67s0qc21gyarmdwc181bqmjc3qzv8zkz-libidn2-2.3.2.drv", 109 Outputs: []string{"out"}, 110 }, 111 { 112 Path: "/nix/store/9rhliwskh3mrrs5nfzgz0x6wrccyfg7k-bootstrap-stage0-glibc-bootstrap.drv", 113 Outputs: []string{"out"}, 114 }, 115 { 116 Path: "/nix/store/cl1wcw2v1ifzjlkzi50h32a6lms9m25s-binutils-2.38.drv", 117 Outputs: []string{"out"}, 118 }, 119 { 120 Path: "/nix/store/ghjc8bkfk8lh53z14mk2nk7h059zh7vx-python3-minimal-3.10.5.drv", 121 Outputs: []string{"out"}, 122 }, 123 { 124 Path: "/nix/store/k3786wfzw637r7sylccdmm92saqp73d8-glibc-2.34.tar.xz.drv", 125 Outputs: []string{"out"}, 126 }, 127 { 128 Path: "/nix/store/l5zr5m1agvvnic49fg6qc44g5fgj3la1-glibc-reinstate-prlimit64-fallback.patch?id=eab07e78b691ae7866267fc04d31c7c3ad6b0eeb.drv", 129 Outputs: []string{"out"}, 130 }, 131 { 132 Path: "/nix/store/mf5kz6d01ab8h0rswzyr04mbcd6g5x9n-bootstrap-stage2-stdenv-linux.drv", 133 Outputs: []string{"out"}, 134 }, 135 { 136 Path: "/nix/store/nd1zy67vp028707pbh466qhrfqh4cpq6-bootstrap-stage2-gcc-wrapper-.drv", 137 Outputs: []string{"out"}, 138 }, 139 { 140 Path: "/nix/store/ra77ww7p2xx8jh8n4m9vmj6wc8wxijdb-bootstrap-tools.drv", 141 Outputs: []string{"out"}, 142 }, 143 { 144 Path: "/nix/store/wlldapf5bg58kivw520ll5bw0fmlaid7-raw.drv", 145 Outputs: []string{"out"}, 146 }, 147 }, 148 InputSources: []string{ 149 "/nix/store/001gp43bjqzx60cg345n2slzg7131za8-nix-nss-open-files.patch", 150 "/nix/store/7kw224hdyxd7115lrqh9a4dv2x8msq2s-fix-x64-abi.patch", 151 "/nix/store/8haph3ng4mgsqr6p4024vj8k6kg3mqc4-nix-locale-archive.patch", 152 "/nix/store/95hp6hs9g73h93safadb8x6vajyqkv6q-0001-Revert-Remove-all-usage-of-BASH-or-BASH-in-installed.patch", 153 "/nix/store/9krlzvny65gdc8s7kpb6lkx8cd02c25b-default-builder.sh", 154 "/nix/store/b1w7zbvm39ff1i52iyjggyvw2rdxz104-dont-use-system-ld-so-cache.patch", 155 "/nix/store/ikmqczy0y20n04a2b8qfflzwihv8139g-separate-debug-info.sh", 156 "/nix/store/mgx19wbmgrh3rblbxhs6vi47sha15n11-2.34-master.patch.gz", 157 "/nix/store/mnglr8rr7nl444h7p50ysyq8qd0fm1lm-dont-use-system-ld-so-preload.patch", 158 "/nix/store/xkd50xxii6k7l1kmw4l5x6xzbhamcs87-allow-kernel-2.6.32.patch", 159 "/nix/store/za0pg7fmysrcwrqcal26fnmzw6vycgdn-fix_path_attribute_in_getconf.patch", 160 }, 161 }, 162 OutputHash: "h0cnbmfcn93xm5dg2x27ixhag1cwndga", 163 Output: "bin", 164 Files: []string{ 165 // the legacy cataloger captures files by default 166 "nix/store/h0cnbmfcn93xm5dg2x27ixhag1cwndga-glibc-2.34-210-bin/lib/glibc.so", 167 "nix/store/h0cnbmfcn93xm5dg2x27ixhag1cwndga-glibc-2.34-210-bin/share/man/glibc.1", 168 }, 169 }, 170 }, 171 }, 172 }, 173 } 174 for _, tt := range tests { 175 t.Run(tt.fixture, func(t *testing.T) { 176 c := NewStoreCataloger() 177 pkgtest.NewCatalogTester(). 178 FromDirectory(t, tt.fixture). 179 Expects(tt.wantPkgs, tt.wantRel). 180 TestCataloger(t, c) 181 }) 182 } 183 }