From da080c472fc415b0ce918f4dd4a1ab143bb1bca4 Mon Sep 17 00:00:00 2001 From: Philip Hands Date: Mon, 14 Mar 2016 15:36:16 +0100 Subject: rough attempt to grab the good cucumber bits from recent tails --- features/step_definitions/time_syncing.rb | 80 ++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 7 deletions(-) (limited to 'features/step_definitions/time_syncing.rb') diff --git a/features/step_definitions/time_syncing.rb b/features/step_definitions/time_syncing.rb index 161a4162..319fb521 100644 --- a/features/step_definitions/time_syncing.rb +++ b/features/step_definitions/time_syncing.rb @@ -1,16 +1,44 @@ +# In some steps below we allow some slack when verifying that the date +# was set appropriately because it may take time to send the `date` +# command over the remote shell and get the answer back, parsing and +# post-processing of the result, etc. +def max_time_drift + 10 +end + When /^I set the system time to "([^"]+)"$/ do |time| - next if @skip_steps_while_restoring_background - @vm.execute("date -s '#{time}'") + $vm.execute_successfully("date -s '#{time}'") + new_time = DateTime.parse($vm.execute_successfully("date").stdout).to_time + expected_time_lower_bound = DateTime.parse(time).to_time + expected_time_upper_bound = expected_time_lower_bound + max_time_drift + assert(expected_time_lower_bound <= new_time && + new_time <= expected_time_upper_bound, + "The guest's time was supposed to be set to " \ + "'#{expected_time_lower_bound}' but is '#{new_time}'") end -When /^I bump the system time with "([^"]+)"$/ do |timediff| - next if @skip_steps_while_restoring_background - @vm.execute("date -s 'now #{timediff}'") +When /^I bump the (hardware clock's|system) time with "([^"]+)"$/ do |clock_type, timediff| + case clock_type + when "hardware clock's" + old_time = DateTime.parse($vm.execute_successfully("hwclock -r").stdout).to_time + $vm.execute_successfully("hwclock --set --date 'now #{timediff}'") + new_time = DateTime.parse($vm.execute_successfully("hwclock -r").stdout).to_time + when 'system' + old_time = DateTime.parse($vm.execute_successfully("date").stdout).to_time + $vm.execute_successfully("date -s 'now #{timediff}'") + new_time = DateTime.parse($vm.execute_successfully("date").stdout).to_time + end + expected_time_lower_bound = DateTime.parse( + cmd_helper(["date", "-d", "#{old_time} #{timediff}"])).to_time + expected_time_upper_bound = expected_time_lower_bound + max_time_drift + assert(expected_time_lower_bound <= new_time && + new_time <= expected_time_upper_bound, + "The #{clock_type} time was supposed to be bumped to " \ + "'#{expected_time_lower_bound}' but is '#{new_time}'") end Then /^Tails clock is less than (\d+) minutes incorrect$/ do |max_diff_mins| - next if @skip_steps_while_restoring_background - guest_time_str = @vm.execute("date --rfc-2822").stdout.chomp + guest_time_str = $vm.execute("date --rfc-2822").stdout.chomp guest_time = Time.rfc2822(guest_time_str) host_time = Time.now diff = (host_time - guest_time).abs @@ -18,3 +46,41 @@ Then /^Tails clock is less than (\d+) minutes incorrect$/ do |max_diff_mins| "The guest's clock is off by #{diff} seconds (#{guest_time})") puts "Time was #{diff} seconds off" end + +Then /^the system clock is just past Tails' build date$/ do + system_time_str = $vm.execute_successfully('date').to_s + system_time = DateTime.parse(system_time_str).to_time + build_time_cmd = 'sed -n -e "1s/^.* - \([0-9]\+\)$/\1/p;q" ' + + '/etc/amnesia/version' + build_time_str = $vm.execute_successfully(build_time_cmd).to_s + build_time = DateTime.parse(build_time_str).to_time + diff = system_time - build_time # => in seconds + # Half an hour should be enough to boot Tails on any reasonable + # hardware and VM setup. + max_diff = 30*60 + assert(diff > 0, + "The system time (#{system_time}) is before the Tails " + + "build date (#{build_time})") + assert(diff <= max_diff, + "The system time (#{system_time}) is more than #{max_diff} seconds " + + "past the build date (#{build_time})") +end + +Then /^Tails' hardware clock is close to the host system's time$/ do + host_time = Time.now + hwclock_time_str = $vm.execute('hwclock -r').stdout.chomp + hwclock_time = DateTime.parse(hwclock_time_str).to_time + diff = (hwclock_time - host_time).abs + assert(diff <= max_time_drift) +end + +Then /^the hardware clock is still off by "([^"]+)"$/ do |timediff| + hwclock = DateTime.parse($vm.execute_successfully("hwclock -r").stdout.chomp).to_time + expected_time_lower_bound = DateTime.parse( + cmd_helper(["date", "-d", "now #{timediff}"])).to_time - max_time_drift + expected_time_upper_bound = expected_time_lower_bound + max_time_drift + assert(expected_time_lower_bound <= hwclock && + hwclock <= expected_time_upper_bound, + "The host's hwclock should be approximately " \ + "'#{expected_time_lower_bound}' but is actually '#{hwclock}'") +end -- cgit v1.2.3-70-g09d2