| 
									
										
										
										
											2024-02-05 17:42:27 +01:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "sbom" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-07 17:24:31 +01:00
										 |  |  | RSpec.describe SBOM do | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |   describe "#schema_validation_errors" do | 
					
						
							|  |  |  |     subject(:sbom) { described_class.create(f, tab) } | 
					
						
							| 
									
										
										
										
											2024-02-05 17:42:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |     before { ENV.delete("HOMEBREW_ENFORCE_SBOM") } | 
					
						
							| 
									
										
										
										
											2024-05-09 13:10:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |     let(:f) { formula { url "foo-1.0" } } | 
					
						
							|  |  |  |     let(:tab) { Tab.new } | 
					
						
							| 
									
										
										
										
											2024-05-09 13:10:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |     it "returns true if valid" do | 
					
						
							|  |  |  |       expect(sbom.schema_validation_errors).to be_empty | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2024-05-09 13:10:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |     it "returns true if valid when bottling" do | 
					
						
							|  |  |  |       expect(sbom.schema_validation_errors(bottling: true)).to be_empty | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2024-02-05 17:42:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |     context "with a maximal SBOM" do | 
					
						
							|  |  |  |       let(:f) do | 
					
						
							|  |  |  |         formula do | 
					
						
							|  |  |  |           homepage "https://brew.sh" | 
					
						
							| 
									
										
										
										
											2024-02-05 17:42:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |           url "https://brew.sh/test-0.1.tbz" | 
					
						
							|  |  |  |           sha256 TEST_SHA256 | 
					
						
							| 
									
										
										
										
											2024-02-05 17:42:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |           patch do | 
					
						
							|  |  |  |             url "patch_macos" | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2024-05-09 13:10:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |           bottle do | 
					
						
							|  |  |  |             sha256 all: "9befdad158e59763fb0622083974a6252878019702d8c961e1bec3a5f5305339" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           # some random dependencies to test with | 
					
						
							|  |  |  |           depends_on "cmake" => :build | 
					
						
							|  |  |  |           depends_on "beanstalkd" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           uses_from_macos "python" => :build | 
					
						
							|  |  |  |           uses_from_macos "zlib" | 
					
						
							| 
									
										
										
										
											2024-05-09 13:10:35 +01:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2024-02-05 17:42:27 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |       let(:tab) do | 
					
						
							|  |  |  |         beanstalkd = formula "beanstalkd" do | 
					
						
							|  |  |  |           url "one-1.1" | 
					
						
							| 
									
										
										
										
											2024-02-05 17:42:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |           bottle do | 
					
						
							|  |  |  |             sha256 all: "ac4c0330b70dae06eaa8065bfbea78dda277699d1ae8002478017a1bd9cf1908" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2024-02-05 17:42:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |         zlib = formula "zlib" do | 
					
						
							|  |  |  |           url "two-1.1" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           bottle do | 
					
						
							|  |  |  |             sha256 all: "6a4642964fe5c4d1cc8cd3507541736d5b984e34a303a814ef550d4f2f8242f9" | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2024-02-05 17:42:27 +01:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |         runtime_dependencies = [beanstalkd, zlib] | 
					
						
							|  |  |  |         runtime_deps_hash = runtime_dependencies.map do |dep| | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             "full_name"         => dep.full_name, | 
					
						
							|  |  |  |             "version"           => dep.version.to_s, | 
					
						
							|  |  |  |             "revision"          => dep.revision, | 
					
						
							|  |  |  |             "pkg_version"       => dep.pkg_version.to_s, | 
					
						
							|  |  |  |             "declared_directly" => true, | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         allow(Tab).to receive(:runtime_deps_hash).and_return(runtime_deps_hash) | 
					
						
							|  |  |  |         tab = Tab.create(f, DevelopmentTools.default_compiler, :libcxx) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         allow(Formulary).to receive(:factory).with("beanstalkd").and_return(beanstalkd) | 
					
						
							|  |  |  |         allow(Formulary).to receive(:factory).with("zlib").and_return(zlib) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         tab | 
					
						
							| 
									
										
										
										
											2024-05-09 13:10:35 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |       it "returns true if valid" do | 
					
						
							|  |  |  |         expect(sbom.schema_validation_errors).to be_empty | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-05-09 13:10:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |       it "returns true if valid when bottling" do | 
					
						
							|  |  |  |         expect(sbom.schema_validation_errors(bottling: true)).to be_empty | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-02-05 17:42:27 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2024-08-08 02:20:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-08 09:34:32 +01:00
										 |  |  |     context "with an invalid SBOM" do | 
					
						
							|  |  |  |       before do | 
					
						
							|  |  |  |         allow(sbom).to receive(:to_spdx_sbom).and_return({}) # fake an empty SBOM | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "returns false" do | 
					
						
							|  |  |  |         expect(sbom.schema_validation_errors).not_to be_empty | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it "returns false when bottling" do | 
					
						
							|  |  |  |         expect(sbom.schema_validation_errors(bottling: true)).not_to be_empty | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-08-08 02:20:03 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2024-02-05 17:42:27 +01:00
										 |  |  |   end | 
					
						
							|  |  |  | end |