################################################# # Execution Setup ################################################# Given(/^I am going to run the program$/) do @params = "" @command = "" @debug_opts = DebugOptions.new @input = StringIO.new @output = "" @errors = "" @pid = nil @exit_status = nil @sync = false end When(/^I run the application$/) do # Generate the command string toRun = "#{File.join(File.dirname(__FILE__), '..', '..', 'bin', 'insxsync')}#{@command}#{@debug_opts.generate}#{@params}" #puts toRun # run the command in a virtual stty Open3.popen3(toRun) do |stdin, stdout, stderr, wait_thr| # save the PID @pid = wait_thr.pid # send imput to the application @input.rewind @input.each do |line| stdin.puts line.chomp if not stdin.closed? end stdin.close # save the output stdout.each_line do |line| @output += line end stdout.close # save any errors stderr.each_line do |line| @errors += line end stderr.close # save the exit status @exit_status = wait_thr.value end #puts @output #puts @errors end Then(/^The application should fail$/) do raise "Application did not fail!" unless @exit_status != 0 end And(/^(A|No) configuration exists$/) do |arg| if arg == 'A' # ensure the directory exists Dir.mkdir(File.join(Dir.home, '.insxsync')) if not File.exists?(File.join(Dir.home, '.insxsync')) #open the config file for writing File.open(File.join(Dir.home, '.insxsync', 'config.ini'), 'w+') do |file| # generate the encryption key using the password "Password001" and the salt 'P@ssw0rd00!' key = OpenSSL::PKCS5.pbkdf2_hmac_sha1('Password001', 'P@ssw0rd00!', 5813, 32) # This key ID and secret key are for testing only data = "AccessKeyId = 'AKIAIW6QO5LAD6KXBDUA'\nSecretAccessKey = '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'" # Generate an 512-bit SHA2 hash of the password 'Password001' and map it to an array of bytes temp = Digest::SHA2.new(512).update('Password001').to_s.scan(/../).map(&:hex) # set the array equal to an array of each of its halves temp = temp.each_slice(32).to_a # Add the second half of the array to the first temp[1].each_index do |index| temp[0][index] += temp[1][index] end # set the array equal to an array of each half of the zeroth array element temp = temp[0].each_slice(16).to_a # Add the second half of the array to the first temp[1].each_index do |index| temp[0][index] += temp[1][index] end # set the IV to a character representation of the first half of the array of bytes iv = temp[0].pack('c*') # create the AES cipher and load the key and IV cipher = OpenSSL::Cipher::AES256.new(:CFB) cipher.encrypt cipher.key = key cipher.iv = iv # encrypt the config encrypted = cipher.update(data) + cipher.final # generate the contents of the file as a delimited Base64 representation of the encrypted config contents = "===BEGIN AWS CREDENTIALS===\n" + Base64.encode64(encrypted) + "===END AWS CREDENTIALS===" # write out the file file.write(contents) end end end ################################################# # Arguments and Options ################################################# And(/^I specify flags for (sync|export)$/) do |arg| if arg == 'export' @params += " -i 1" else @params += " -d 1 -a ./tmp -P 3306 -u root -p 'Password001'" end end And(/^I select the option "([^"]*)"$/) do |arg| @input.print arg end Given(/^I specify "([^"]*)"$/) do |arg| @params = @params + " #{arg}" end And(/^I input the following "([^"]*)"$/) do |arg| @input.puts arg end Given(/^I specify the command "([^"]*)"$/) do |arg| @command = " #{arg}" end ################################################# # Output Inspection ################################################# And(/^I should not see "([^"]*)"$/) do |arg| raise "Specified string was found." if @output =~ /#{arg}/ end And(/^I should see "([^"]*)"$/) do |arg| raise "Specified string was not found. Text:\n#{@output}" unless @output =~ /#{arg}/ end And(/^I should see an error message containing "([^"]*)"$/) do |arg| raise "Error message does not contain \"#{arg}\".\nContents: #{@errors}\n" unless @errors =~ /#{arg}/ end ################################################# # Environment Setup ################################################# And(/^Configurations do not exist at "([^"]*)"$/) do |arg| FileUtils.rm_rf(arg) if File.exists?(arg) @config_dir = arg.end_with?('/') ? arg.chop : arg end And(/^Configurations exist at "([^"]*)"$/) do |arg| FileUtils.rm_rf arg if File.exists?(arg) Dir.mkdir(arg) `tar xf features/support/subdomain.tar.bz2 -C #{arg}` @config_dir = arg.end_with?('/') ? arg.chop : arg end And(/^Configurations exist at the default location$/) do step %Q{Configurations exist at "/var/www/exchange_app/data/subdomain"} end And(/^Configurations do not exist at the default location$/) do step %Q{Configurations do not exist at "/var/www/exchange_app/data/subdomain"} end And(/^Configurations exist at the default location for export$/) do step %Q{Configurations exist at "/usr/share/exchange_app/data/subdomain"} end And(/^Configurations do not exist at the default location for export$/) do step %Q{Configurations do not exist at "/usr/share/exchange_app/data/subdomain"} end