Back to Repositories

Testing Dynamic Version Finder Configuration Parser in WPScan

This test suite validates the configuration parsing functionality for dynamic version finding in WPScan. It focuses on the proper initialization and configuration of finder classes that handle version detection through config files.

Test Coverage Overview

The test suite covers the dynamic creation and configuration of version finder classes in WPScan.

Key areas tested include:
  • Configuration parameter handling for confidence levels
  • Pattern matching setup for version detection
  • Constant initialization and inheritance
  • Module namespace management

Implementation Analysis

The testing approach utilizes RSpec’s module manipulation capabilities to create temporary test classes. It employs before/after hooks for proper test isolation and cleanup, while using let blocks for flexible test data configuration.

The implementation leverages RSpec’s context blocks to separate different configuration scenarios and verify constant assignments.

Technical Details

Testing tools and setup:
  • RSpec as the testing framework
  • Module manipulation for dynamic class creation
  • Constant management through remove_const
  • Pattern matching using regex
  • Configuration parsing for JSON files

Best Practices Demonstrated

The test suite demonstrates several testing best practices including proper test isolation, clear context separation, and thorough edge case coverage.

Notable practices include:
  • Dynamic test class creation and cleanup
  • Flexible configuration through let blocks
  • Clear separation of test scenarios
  • Proper constant verification

wpscanteam/wpscan

spec/lib/finders/dynamic_finder/version/config_parser_spec.rb

            
# frozen_string_literal: true

describe WPScan::Finders::DynamicFinder::Version::ConfigParser do
  module WPScan
    module Finders
      module Version
        # Needed to be able to test the below
        module Rspec
        end
      end
    end
  end

  let(:finder_module) { WPScan::Finders::Version::Rspec }
  let(:finder_class)  { WPScan::Finders::Version::Rspec::ConfigParser }
  let(:finder_config) { { 'key' => 'some-key', 'path' => 'file.json' } }
  let(:default)       { { 'pattern' => /(?<v>\d+\.[.\d]+)/, 'confidence' => 70 } }

  before { described_class.create_child_class(finder_module, :ConfigParser, finder_config) }
  after  { finder_module.send(:remove_const, :ConfigParser) }

  describe '.create_child_class' do
    context 'when CONFIDENCE' do
      let(:finder_config) { super().merge('confidence' => 30) }

      it 'contains the expected constants' do
        expect(finder_class::KEY).to eql finder_config['key']
        expect(finder_class::CONFIDENCE).to eql finder_config['confidence']
        expect(finder_class::PATH).to eql finder_config['path']

        expect(finder_class::PATTERN).to eql default['pattern']
      end
    end

    context 'when PATTERN' do
      let(:finder_config) { super().merge('pattern' => /another pattern/i) }

      it 'contains the expected constants' do
        expect(finder_class::KEY).to eql finder_config['key']
        expect(finder_class::PATTERN).to eql finder_config['pattern']
        expect(finder_class::PATH).to eql finder_config['path']

        expect(finder_class::CONFIDENCE).to eql default['confidence']
      end
    end
  end

  describe '#passive, #aggressive' do
    # Handled in spec/lib/finders/dynamic_finder/plugin_version_spec
  end
end