From 96990fdc02aefe4f27b547d3607f4e6cce225ff4 Mon Sep 17 00:00:00 2001 From: Luke Imhoff Date: Wed, 5 Nov 2014 14:38:43 -0600 Subject: [PATCH] Fail before suite if more than 1 thread exists MSP-11147 Detect thread leaks in a `before(:suite)` configured by `Metasploit::Framework::Spec::Threads::Suite.configure!` and fail if any leaks are found. --- lib/metasploit/framework.rb | 4 +++ lib/metasploit/framework/spec.rb | 5 +++ lib/metasploit/framework/spec/threads.rb | 5 +++ .../framework/spec/threads/suite.rb | 35 +++++++++++++++++++ spec/spec_helper.rb | 2 ++ 5 files changed, 51 insertions(+) create mode 100644 lib/metasploit/framework/spec.rb create mode 100644 lib/metasploit/framework/spec/threads.rb create mode 100644 lib/metasploit/framework/spec/threads/suite.rb diff --git a/lib/metasploit/framework.rb b/lib/metasploit/framework.rb index aa488aea2a..a43afdc93c 100644 --- a/lib/metasploit/framework.rb +++ b/lib/metasploit/framework.rb @@ -32,6 +32,10 @@ module Metasploit # works in compatible manner with activerecord's rake tasks and other # railties. module Framework + extend ActiveSupport::Autoload + + autoload :Spec + # Returns the root of the metasploit-framework project. Use in place of # `Rails.root`. # diff --git a/lib/metasploit/framework/spec.rb b/lib/metasploit/framework/spec.rb new file mode 100644 index 0000000000..857c219d43 --- /dev/null +++ b/lib/metasploit/framework/spec.rb @@ -0,0 +1,5 @@ +module Metasploit::Framework::Spec + extend ActiveSupport::Autoload + + autoload :Threads +end \ No newline at end of file diff --git a/lib/metasploit/framework/spec/threads.rb b/lib/metasploit/framework/spec/threads.rb new file mode 100644 index 0000000000..b442a8f4fc --- /dev/null +++ b/lib/metasploit/framework/spec/threads.rb @@ -0,0 +1,5 @@ +module Metasploit::Framework::Spec::Threads + extend ActiveSupport::Autoload + + autoload :Suite +end \ No newline at end of file diff --git a/lib/metasploit/framework/spec/threads/suite.rb b/lib/metasploit/framework/spec/threads/suite.rb new file mode 100644 index 0000000000..acd17d7ed3 --- /dev/null +++ b/lib/metasploit/framework/spec/threads/suite.rb @@ -0,0 +1,35 @@ +module Metasploit::Framework::Spec::Threads::Suite + # + # CONSTANTS + # + + EXPECTED_THREAD_COUNT_BEFORE_SUITE = 1 + + # + # Module Methods + # + + # Configures `before(:suite)` and `after(:suite)` callback to detect thread leaks. + # + # @return [void] + def self.configure! + unless @configured + RSpec.configure do |config| + config.before(:suite) do + thread_count = Thread.list.count + + expect(thread_count).to( + (be <= EXPECTED_THREAD_COUNT_BEFORE_SUITE), + "#{thread_count} #{'thread'.pluralize(thread_count)} exist(s) when " \ + "only #{EXPECTED_THREAD_COUNT_BEFORE_SUITE} #{'thread'.pluralize(EXPECTED_THREAD_COUNT_BEFORE_SUITE)} " \ + "expected before suite runs" + ) + end + end + + @configured = true + end + + @configured + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1e1092fdec..d7e9db840b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -55,3 +55,5 @@ RSpec.configure do |config| # instead of true. config.use_transactional_fixtures = true end + +Metasploit::Framework::Spec::Threads::Suite.configure!