From c91b74e0ba9e7e30ff5b4fd48758ace4d80af97a Mon Sep 17 00:00:00 2001 From: zinduolis Date: Sat, 6 Sep 2025 11:10:56 +1000 Subject: [PATCH] Refactor database reset handling in AutoRunEngine and Websocket hooked browser specs for improved synchronization --- .../main/autorun_engine/autorun_engine_spec.rb | 17 +++++++++++++---- .../extensions/websocket_hooked_browser_spec.rb | 6 +++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/spec/beef/core/main/autorun_engine/autorun_engine_spec.rb b/spec/beef/core/main/autorun_engine/autorun_engine_spec.rb index 370a9588c..4c7ce1f94 100644 --- a/spec/beef/core/main/autorun_engine/autorun_engine_spec.rb +++ b/spec/beef/core/main/autorun_engine/autorun_engine_spec.rb @@ -10,6 +10,8 @@ require_relative '../../../../spec_helper' require_relative '../../../../support/constants' require_relative '../../../../support/beef_test' +MUTEX = Mutex.new + RSpec.describe 'AutoRunEngine Test', run_on_browserstack: true do before(:all) do @config = BeEF::Core::Configuration.instance @@ -18,7 +20,10 @@ RSpec.describe 'AutoRunEngine Test', run_on_browserstack: true do print_info 'Loading database' db_file = @config.get('beef.database.file') print_info 'Resetting the database for BeEF.' - File.delete(db_file) if File.exist?(db_file) + + if ENV['RESET_DB'] + File.delete(db_file) if File.exist?(db_file) + end @config.set('beef.credentials.user', 'beef') @config.set('beef.credentials.passwd', 'beef') @@ -49,9 +54,13 @@ RSpec.describe 'AutoRunEngine Test', run_on_browserstack: true do OTR::ActiveRecord.establish_connection! end ActiveRecord::Migrator.migrations_paths = [File.join('core', 'main', 'ar-migrations')] - context = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths) - ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate if context.needs_migration? - + MUTEX.synchronize do + context = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths) + if context.needs_migration? + ActiveRecord::Migrator.new(:up, context.migrations, context.schema_migration, context.internal_metadata).migrate + end + end + BeEF::Core::Migration.instance.update_db! # add AutoRunEngine rule diff --git a/spec/beef/extensions/websocket_hooked_browser_spec.rb b/spec/beef/extensions/websocket_hooked_browser_spec.rb index fa5f668ba..2e9d242e2 100644 --- a/spec/beef/extensions/websocket_hooked_browser_spec.rb +++ b/spec/beef/extensions/websocket_hooked_browser_spec.rb @@ -20,7 +20,11 @@ RSpec.describe 'Browser hooking with Websockets', run_on_browserstack: true do print_info 'Loading database' db_file = @config.get('beef.database.file') print_info 'Resetting the database for BeEF.' - File.delete(db_file) if File.exist?(db_file) + + if ENV['RESET_DB'] + File.delete(db_file) if File.exist?(db_file) + end + @config.set('beef.credentials.user', 'beef') @config.set('beef.credentials.passwd', 'beef') @config.set('beef.http.websocket.secure', false)