From a1cd71c5ef32e6f6dbebd9f5f69a09751a41cd7f Mon Sep 17 00:00:00 2001 From: Postmodern Date: Mon, 5 Aug 2024 19:03:44 -0700 Subject: [PATCH] Use `software_version` instead of just `version` for consistency (closes #148). --- lib/ronin/exploits/mixins/has_targets.rb | 21 +++++++---- lib/ronin/exploits/target.rb | 26 +++++++++----- spec/mixins/has_targets_spec.rb | 46 ++++++++++++------------ spec/target_spec.rb | 28 ++++++++++++--- 4 files changed, 78 insertions(+), 43 deletions(-) diff --git a/lib/ronin/exploits/mixins/has_targets.rb b/lib/ronin/exploits/mixins/has_targets.rb index 4b7920ef..6db3b39c 100644 --- a/lib/ronin/exploits/mixins/has_targets.rb +++ b/lib/ronin/exploits/mixins/has_targets.rb @@ -91,9 +91,12 @@ def targets # @option kwargs [Symbol, nil] :software # The software name of the target. # - # @option kwargs [String, nil] :version + # @option kwargs [String, nil] :software_version # The software version of the target. # + # @option kwargs [String, nil] :version + # Alias for `software_version:`. + # # @example # target arch: :x86_64, os: :linux, software: 'Apache' do |t| # t.foo = 0x123456 @@ -172,7 +175,7 @@ def perform_validate # @param [Symbol] software # The targeted software. # - # @param [Symbol] version + # @param [Symbol] software_version # The targeted software version. # # @raise [NoMatchingTarget] @@ -180,7 +183,7 @@ def perform_validate # # @api semipublic # - def select_target(arch: nil, os: nil, os_version: nil, software: nil, version: nil) + def select_target(arch: nil, os: nil, os_version: nil, software: nil, software_version: nil) targets = self.class.targets.lazy if arch @@ -199,9 +202,9 @@ def select_target(arch: nil, os: nil, os_version: nil, software: nil, version: n targets = targets.select { |target| target.software == software } end - if version + if software_version targets = targets.select do |target| - target.version == version + target.software_version == software_version end end @@ -289,9 +292,13 @@ def software # # @api public # - def version - target.version if target + # @since 1.2.0 + # + def software_version + target.software_version if target end + + alias version software_version end end end diff --git a/lib/ronin/exploits/target.rb b/lib/ronin/exploits/target.rb index 060479a0..f63ca56a 100644 --- a/lib/ronin/exploits/target.rb +++ b/lib/ronin/exploits/target.rb @@ -54,7 +54,11 @@ class Target < OpenStruct # The target's software version. # # @return [String, nil] - attr_reader :version + # + # @since 1.2.0 + attr_reader :software_version + + alias version software_version # # Creates a new ExploitTarget object @@ -71,20 +75,24 @@ class Target < OpenStruct # @param [String, nil] software # The software name of the target (ex: `"Apache"`). # - # @param [String, nil] version + # @param [String, nil] software_version # The software version of the target (ex: `"2.4.54"`). # + # @param [String, nil] version + # Alias for `software_version:`. + # # @yield [target] # If a block is given, it will be passed the new target object. # # @yieldparam [Target] target # The newly created target object. # - def initialize(arch: nil, - os: nil, - os_version: nil, - software: nil, - version: nil, + def initialize(arch: nil, + os: nil, + os_version: nil, + software: nil, + software_version: nil, + version: nil, **params) super(**params) @@ -93,8 +101,8 @@ def initialize(arch: nil, @os = os @os_version = os_version - @software = software - @version = version + @software = software + @software_version = software_version || version yield self if block_given? end diff --git a/spec/mixins/has_targets_spec.rb b/spec/mixins/has_targets_spec.rb index 59948386..68abc521 100644 --- a/spec/mixins/has_targets_spec.rb +++ b/spec/mixins/has_targets_spec.rb @@ -122,14 +122,14 @@ class ExampleExploit < Ronin::Exploits::Exploit os: :linux, os_version: '5.18.1', software: 'Apache', - version: '2.4.53', + software_version: '2.4.53', foo: 1 target arch: :arm, os: :macos, os_version: '10.13', software: 'nginx', - version: '1.22.0', + software_version: '1.22.0', foo: 2 end end @@ -289,22 +289,22 @@ class ExampleExploit < Ronin::Exploits::Exploit end end - context "when given the version: keyword argument" do - let(:version) { '1.22.0' } + context "when given the software_version: keyword argument" do + let(:software_version) { '1.22.0' } - it "must find the target in .targets with the matching #version" do - subject.select_target(version: version) + it "must find the target in .targets with the matching #software_version" do + subject.select_target(software_version: software_version) - expect(subject.target.version).to eq(version) + expect(subject.target.software_version).to eq(software_version) end end context "when given multiple keyword arguments" do - let(:arch) { :arm } - let(:os) { :macos } - let(:os_version) { '10.13' } - let(:software) { 'nginx' } - let(:version) { '1.22.0' } + let(:arch) { :arm } + let(:os) { :macos } + let(:os_version) { '10.13' } + let(:software) { 'nginx' } + let(:software_version) { '1.22.0' } it "must find the target in .targets which matches all given values" do subject.select_target( @@ -312,14 +312,14 @@ class ExampleExploit < Ronin::Exploits::Exploit os: os, os_version: os_version, software: software, - version: version + software_version: software_version ) - expect(subject.target.arch).to eq(arch) - expect(subject.target.os).to eq(os) - expect(subject.target.os_version).to eq(os_version) - expect(subject.target.software).to eq(software) - expect(subject.target.version).to eq(version) + expect(subject.target.arch).to eq(arch) + expect(subject.target.os).to eq(os) + expect(subject.target.os_version).to eq(os_version) + expect(subject.target.software).to eq(software) + expect(subject.target.software_version).to eq(software_version) end end @@ -412,14 +412,14 @@ class ExampleExploit < Ronin::Exploits::Exploit end end - describe "#version" do + describe "#software_version" do context "when a target has been set" do subject do - test_class.new(target: {version: '1.22.0'}) + test_class.new(target: {software_version: '1.22.0'}) end - it "must return the #target's #version" do - expect(subject.version).to eq(subject.target.version) + it "must return the #target's #software_version" do + expect(subject.software_version).to eq(subject.target.software_version) end end @@ -427,7 +427,7 @@ class ExampleExploit < Ronin::Exploits::Exploit subject { test_class.new } it "must return nil" do - expect(subject.version).to be(nil) + expect(subject.software_version).to be(nil) end end end diff --git a/spec/target_spec.rb b/spec/target_spec.rb index 083ff71d..15b75fb3 100644 --- a/spec/target_spec.rb +++ b/spec/target_spec.rb @@ -23,8 +23,8 @@ expect(subject.software).to be(nil) end - it "must default #version to nil" do - expect(subject.version).to be(nil) + it "must default #software_version to nil" do + expect(subject.software_version).to be(nil) end context "when given the arch: keyword argument" do @@ -67,13 +67,23 @@ end end + context "when given the software_version: keyword argument" do + let(:software_version) { '1.2.3' } + + subject { described_class.new(software_version: software_version) } + + it "must set #software_version" do + expect(subject.software_version).to be(software_version) + end + end + context "when given the version: keyword argument" do let(:version) { '1.2.3' } subject { described_class.new(version: version) } - it "must set #version" do - expect(subject.version).to be(version) + it "must set #software_version" do + expect(subject.software_version).to be(version) end end @@ -89,4 +99,14 @@ end end end + + describe "#version" do + let(:software_version) { '1.2.3' } + + subject { described_class.new(software_version: software_version) } + + it "must be an alias for #software_version" do + expect(subject.version).to eq(subject.software_version) + end + end end