From 07158e455bc30a4a12288096088fde0aeea32c61 Mon Sep 17 00:00:00 2001 From: Roland Walker Date: Mon, 1 Sep 2014 10:04:17 -0400 Subject: [PATCH] Revise gpg stanza order and parameters References: #5971 --- lib/cask/dsl/gpg.rb | 44 +++++++++---------- test/cask/dsl_test.rb | 27 ++++++++++-- .../invalid/invalid-gpg-conflicting-keys.rb | 10 +++++ .../Casks/invalid/invalid-gpg-key-url.rb | 4 +- .../Casks/invalid/invalid-gpg-missing-key.rb | 8 ++++ .../invalid/invalid-gpg-multiple-stanzas.rb | 11 +++++ .../Casks/invalid/invalid-gpg-multiple.rb | 11 ----- .../Casks/invalid/invalid-gpg-parameter.rb | 9 ++++ .../invalid/invalid-gpg-signature-url.rb | 4 +- .../support/Casks/invalid/invalid-gpg-type.rb | 6 +-- test/support/Casks/with-gpg-key-url.rb | 9 ++++ test/support/Casks/with-gpg.rb | 4 +- 12 files changed, 99 insertions(+), 48 deletions(-) create mode 100644 test/support/Casks/invalid/invalid-gpg-conflicting-keys.rb create mode 100644 test/support/Casks/invalid/invalid-gpg-missing-key.rb create mode 100644 test/support/Casks/invalid/invalid-gpg-multiple-stanzas.rb delete mode 100644 test/support/Casks/invalid/invalid-gpg-multiple.rb create mode 100644 test/support/Casks/invalid/invalid-gpg-parameter.rb create mode 100644 test/support/Casks/with-gpg-key-url.rb diff --git a/lib/cask/dsl/gpg.rb b/lib/cask/dsl/gpg.rb index dcac97118dd6..3f5174ae9ed2 100644 --- a/lib/cask/dsl/gpg.rb +++ b/lib/cask/dsl/gpg.rb @@ -1,40 +1,36 @@ class Cask::DSL::Gpg - VALID_TYPES = Set.new [ - :id, # first one is the default - :url, - ] + KEY_PARAMETERS = Set.new [ + :key_id, + :key_url, + ] - REQUIRED_PARAMETERS = [ - :signature - ] + VALID_PARAMETERS = Set.new [ ] + VALID_PARAMETERS.merge KEY_PARAMETERS - attr_reader :key, :parameters, :type, :signature + attr_accessor *VALID_PARAMETERS + attr_accessor :signature - def initialize(key, parameters={}) + def initialize(signature, parameters={}) @parameters = parameters - REQUIRED_PARAMETERS.each do |param| - unless @parameters.key?(param) and not @parameters[param].nil? - raise "gpg #{param.inspect} parameter is required" - end + @signature = Cask::UnderscoreSupportingURI.parse(signature) + parameters.each do |hkey, hvalue| + raise "invalid 'gpg' parameter: '#{hkey.inspect}'" unless VALID_PARAMETERS.include?(hkey) + writer_method = "#{hkey}=".to_sym + hvalue = Cask::UnderscoreSupportingURI.parse(hvalue) if hkey == :key_url + send(writer_method, hvalue) end - @key = key - @signature = Cask::UnderscoreSupportingURI.parse(@parameters[:signature]) - @type = @parameters[:type] - @type = VALID_TYPES.first if @type.nil? - unless VALID_TYPES.include?(@type) - raise "invalid gpg type: '#{@type.inspect}'" - end - if @type == :url - @key = Cask::UnderscoreSupportingURI.parse(@key) + unless KEY_PARAMETERS.intersection(parameters.keys).length == 1 + raise "'gpg' stanza must include exactly one of: '#{KEY_PARAMETERS.to_a}'" end end def to_yaml - [@key, @parameters].to_yaml + # bug, :key_url value is not represented as an instance of Cask::UnderscoreSupportingURI + [@signature, @parameters].to_yaml end def to_s - @key.to_s + @signature.to_s end end diff --git a/test/cask/dsl_test.rb b/test/cask/dsl_test.rb index 52eef1f9f033..99ccbe218065 100644 --- a/test/cask/dsl_test.rb +++ b/test/cask/dsl_test.rb @@ -159,13 +159,32 @@ def caveats; <<-EOS.undent cask.gpg.to_s.must_match %r{\S} end - it "prevents specifying gpg multiple times" do + it "allows gpg stanza to be specified with :key_url" do + cask = Cask.load('with-gpg-key-url') + cask.gpg.to_s.must_match %r{\S} + end + + it "prevents specifying gpg stanza multiple times" do err = lambda { - invalid_cask = Cask.load('invalid/invalid-gpg-multiple') + invalid_cask = Cask.load('invalid/invalid-gpg-multiple-stanzas') }.must_raise(CaskInvalidError) err.message.must_include "'gpg' stanza may only appear once" end + it "prevents missing gpg key parameters" do + err = lambda { + invalid_cask = Cask.load('invalid/invalid-gpg-missing-key') + }.must_raise(CaskInvalidError) + err.message.must_include "'gpg' stanza must include exactly one" + end + + it "prevents conflicting gpg key parameters" do + err = lambda { + invalid_cask = Cask.load('invalid/invalid-gpg-conflicting-keys') + }.must_raise(CaskInvalidError) + err.message.must_include "'gpg' stanza must include exactly one" + end + it "refuses to load invalid gpg signature URLs" do err = lambda { invalid_cask = Cask.load('invalid/invalid-gpg-signature-url') @@ -178,9 +197,9 @@ def caveats; <<-EOS.undent }.must_raise(CaskInvalidError) end - it "refuses to load if gpg :type is invalid" do + it "refuses to load if gpg parameter is unknown" do err = lambda { - invalid_cask = Cask.load('invalid/invalid-gpg-type') + invalid_cask = Cask.load('invalid/invalid-gpg-parameter') }.must_raise(CaskInvalidError) end end diff --git a/test/support/Casks/invalid/invalid-gpg-conflicting-keys.rb b/test/support/Casks/invalid/invalid-gpg-conflicting-keys.rb new file mode 100644 index 000000000000..bea75a664389 --- /dev/null +++ b/test/support/Casks/invalid/invalid-gpg-conflicting-keys.rb @@ -0,0 +1,10 @@ +class InvalidGpgConflictingKeys < TestCask + url TestHelper.local_binary('caffeine.zip') + homepage 'http://example.com/invalid-gpg-conflicting-keys' + gpg 'http://example.com/gpg-signature.asc', + :key_id => 'ID', + :key_url => 'http://example.com/gpg-key-url' + sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853' + version '1.2.3' + link 'Caffeine.app' +end diff --git a/test/support/Casks/invalid/invalid-gpg-key-url.rb b/test/support/Casks/invalid/invalid-gpg-key-url.rb index ccf3828be649..abd1f9301f4c 100644 --- a/test/support/Casks/invalid/invalid-gpg-key-url.rb +++ b/test/support/Casks/invalid/invalid-gpg-key-url.rb @@ -1,8 +1,8 @@ class InvalidGpgKeyUrl < TestCask url TestHelper.local_binary('caffeine.zip') homepage 'http://example.com/invalid-gpg-key-url' - gpg 1, :type => :url, - :signature => 'http://example.com/gpg-signature.asc' + gpg 'http://example.com/gpg-signature.asc', + :key_url => 1 sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853' version '1.2.3' link 'Caffeine.app' diff --git a/test/support/Casks/invalid/invalid-gpg-missing-key.rb b/test/support/Casks/invalid/invalid-gpg-missing-key.rb new file mode 100644 index 000000000000..debd7c857082 --- /dev/null +++ b/test/support/Casks/invalid/invalid-gpg-missing-key.rb @@ -0,0 +1,8 @@ +class InvalidGpgMissingKeys < TestCask + url TestHelper.local_binary('caffeine.zip') + homepage 'http://example.com/invalid-gpg-missing-keys' + gpg 'http://example.com/gpg-signature.asc' + sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853' + version '1.2.3' + link 'Caffeine.app' +end diff --git a/test/support/Casks/invalid/invalid-gpg-multiple-stanzas.rb b/test/support/Casks/invalid/invalid-gpg-multiple-stanzas.rb new file mode 100644 index 000000000000..e0e17ee3e650 --- /dev/null +++ b/test/support/Casks/invalid/invalid-gpg-multiple-stanzas.rb @@ -0,0 +1,11 @@ +class InvalidGpgMultipleStanzas < TestCask + url TestHelper.local_binary('caffeine.zip') + homepage 'http://example.com/invalid-gpg-multiple-stanzas' + gpg 'http://example.com/gpg-signature.asc', + :key_id => 'ID' + gpg 'http://example.com/gpg-signature.asc', + :key_id => 'ID' + sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853' + version '1.2.3' + link 'Caffeine.app' +end diff --git a/test/support/Casks/invalid/invalid-gpg-multiple.rb b/test/support/Casks/invalid/invalid-gpg-multiple.rb deleted file mode 100644 index 5985179b7ed6..000000000000 --- a/test/support/Casks/invalid/invalid-gpg-multiple.rb +++ /dev/null @@ -1,11 +0,0 @@ -class InvalidGpgMultiple < TestCask - url TestHelper.local_binary('caffeine.zip') - homepage 'http://example.com/invalid-gpg-multiple' - gpg 'ID', :type => :id, - :signature => 'http://example.com/gpg-signature.asc' - gpg 'ID', :type => :id, - :signature => 'http://example.com/gpg-signature.asc' - sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853' - version '1.2.3' - link 'Caffeine.app' -end diff --git a/test/support/Casks/invalid/invalid-gpg-parameter.rb b/test/support/Casks/invalid/invalid-gpg-parameter.rb new file mode 100644 index 000000000000..83523433e269 --- /dev/null +++ b/test/support/Casks/invalid/invalid-gpg-parameter.rb @@ -0,0 +1,9 @@ +class InvalidGpgParameter < TestCask + url TestHelper.local_binary('caffeine.zip') + homepage 'http://example.com/invalid-gpg-type' + gpg 'http://example.com/gpg-signature.asc', + :no_such_parameter => :value + sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853' + version '1.2.3' + link 'Caffeine.app' +end diff --git a/test/support/Casks/invalid/invalid-gpg-signature-url.rb b/test/support/Casks/invalid/invalid-gpg-signature-url.rb index 707421ed8c89..2c0b55b9c266 100644 --- a/test/support/Casks/invalid/invalid-gpg-signature-url.rb +++ b/test/support/Casks/invalid/invalid-gpg-signature-url.rb @@ -1,8 +1,8 @@ class InvalidGpgSignatureUrl < TestCask url TestHelper.local_binary('caffeine.zip') homepage 'http://example.com/invalid-gpg-signature-url' - gpg 'ID', :type => :id, - :signature => 1 + gpg 1, + :key_id => 'ID' sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853' version '1.2.3' link 'Caffeine.app' diff --git a/test/support/Casks/invalid/invalid-gpg-type.rb b/test/support/Casks/invalid/invalid-gpg-type.rb index e3ebff89ba52..83523433e269 100644 --- a/test/support/Casks/invalid/invalid-gpg-type.rb +++ b/test/support/Casks/invalid/invalid-gpg-type.rb @@ -1,8 +1,8 @@ -class InvalidGpgType < TestCask +class InvalidGpgParameter < TestCask url TestHelper.local_binary('caffeine.zip') homepage 'http://example.com/invalid-gpg-type' - gpg 'ID', :type => :no_such_type, - :signature => 'http://example.com/gpg-signature.asc' + gpg 'http://example.com/gpg-signature.asc', + :no_such_parameter => :value sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853' version '1.2.3' link 'Caffeine.app' diff --git a/test/support/Casks/with-gpg-key-url.rb b/test/support/Casks/with-gpg-key-url.rb new file mode 100644 index 000000000000..ee73776bf8ce --- /dev/null +++ b/test/support/Casks/with-gpg-key-url.rb @@ -0,0 +1,9 @@ +class WithGpgKeyUrl < TestCask + url TestHelper.local_binary('caffeine.zip') + homepage 'http://example.com/with-gpg-key-url' + gpg 'http://example.com/gpg-signature.asc', + :key_url => 'http://example.com/gpg-key-url' + sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853' + version '1.2.3' + link 'Caffeine.app' +end diff --git a/test/support/Casks/with-gpg.rb b/test/support/Casks/with-gpg.rb index afae2900ce41..eb7618d3ed97 100644 --- a/test/support/Casks/with-gpg.rb +++ b/test/support/Casks/with-gpg.rb @@ -1,8 +1,8 @@ class WithGpg < TestCask url TestHelper.local_binary('caffeine.zip') homepage 'http://example.com/with-gpg' - gpg 'ID', :type => :id, - :signature => 'http://example.com/gpg-signature.asc' + gpg 'http://example.com/gpg-signature.asc', + :key_id => 'ID' sha256 '9203c30951f9aab41ac294bbeb1dcef7bed401ff0b353dcb34d68af32ea51853' version '1.2.3' link 'Caffeine.app'