| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  | RSpec.describe Cask::Download, :cask do | 
					
						
							|  |  |  |   describe "#verify_download_integrity" do | 
					
						
							|  |  |  |     subject(:verification) { described_class.new(cask).verify_download_integrity(downloaded_path) } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-30 12:55:30 +00:00
										 |  |  |     let(:tap) { nil } | 
					
						
							|  |  |  |     let(:cask) { instance_double(Cask::Cask, token: "cask", sha256: expected_sha256, tap:) } | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |     let(:cafebabe) { "cafebabecafebabecafebabecafebabecafebabecafebabecafebabecafebabe" } | 
					
						
							|  |  |  |     let(:deadbeef) { "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef" } | 
					
						
							|  |  |  |     let(:computed_sha256) { cafebabe } | 
					
						
							|  |  |  |     let(:downloaded_path) { Pathname.new("cask.zip") } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     before do | 
					
						
							|  |  |  |       allow(downloaded_path).to receive_messages(file?: true, sha256: computed_sha256) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-09-05 01:39:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |     context "when the expected checksum is :no_check" do | 
					
						
							|  |  |  |       let(:expected_sha256) { :no_check } | 
					
						
							| 
									
										
										
										
											2018-09-05 01:39:30 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-30 12:55:30 +00:00
										 |  |  |       it "warns about skipping the check" do | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |         expect { verification }.to output(/skipping verification/).to_stderr | 
					
						
							| 
									
										
										
										
											2018-09-05 01:39:30 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-12-30 12:55:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |       context "with an official tap" do | 
					
						
							|  |  |  |         let(:tap) { CoreCaskTap.instance } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it "does not warn about skipping the check" do | 
					
						
							|  |  |  |           expect { verification }.not_to output(/skipping verification/).to_stderr | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |     context "when expected and computed checksums match" do | 
					
						
							|  |  |  |       let(:expected_sha256) { Checksum.new(cafebabe) } | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |       it "does not raise an error" do | 
					
						
							|  |  |  |         expect { verification }.not_to raise_error | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |     context "when the expected checksum is nil" do | 
					
						
							|  |  |  |       let(:expected_sha256) { nil } | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |       it "outputs an error" do | 
					
						
							|  |  |  |         expect { verification }.to output(/sha256 "#{computed_sha256}"/).to_stderr | 
					
						
							| 
									
										
										
										
											2018-09-05 01:39:30 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |     context "when the expected checksum is empty" do | 
					
						
							|  |  |  |       let(:expected_sha256) { Checksum.new("") } | 
					
						
							| 
									
										
										
										
											2018-03-25 13:30:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |       it "outputs an error" do | 
					
						
							|  |  |  |         expect { verification }.to output(/sha256 "#{computed_sha256}"/).to_stderr | 
					
						
							| 
									
										
										
										
											2018-09-05 01:39:30 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |     context "when expected and computed checksums do not match" do | 
					
						
							|  |  |  |       let(:expected_sha256) { Checksum.new(deadbeef) } | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-30 11:18:58 -07:00
										 |  |  |       it "raises an error" do | 
					
						
							|  |  |  |         expect { verification }.to raise_error ChecksumMismatchError | 
					
						
							| 
									
										
										
										
											2018-09-05 01:39:30 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-08-18 22:11:42 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |