1 # In some steps below we allow some slack when verifying that the date
2 # was set appropriately because it may take time to send the `date`
3 # command over the remote shell and get the answer back, parsing and
4 # post-processing of the result, etc.
9 When /^I set the system time to "([^"]+)"$/ do |time|
10 $vm.execute_successfully("date -s '#{time}'")
11 new_time = DateTime.parse($vm.execute_successfully("date").stdout).to_time
12 expected_time_lower_bound = DateTime.parse(time).to_time
13 expected_time_upper_bound = expected_time_lower_bound + max_time_drift
14 assert(expected_time_lower_bound <= new_time &&
15 new_time <= expected_time_upper_bound,
16 "The guest's time was supposed to be set to " \
17 "'#{expected_time_lower_bound}' but is '#{new_time}'")
20 When /^I bump the (hardware clock's|system) time with "([^"]+)"$/ do |clock_type, timediff|
22 when "hardware clock's"
23 old_time = DateTime.parse($vm.execute_successfully("hwclock -r").stdout).to_time
24 $vm.execute_successfully("hwclock --set --date 'now #{timediff}'")
25 new_time = DateTime.parse($vm.execute_successfully("hwclock -r").stdout).to_time
27 old_time = DateTime.parse($vm.execute_successfully("date").stdout).to_time
28 $vm.execute_successfully("date -s 'now #{timediff}'")
29 new_time = DateTime.parse($vm.execute_successfully("date").stdout).to_time
31 expected_time_lower_bound = DateTime.parse(
32 cmd_helper(["date", "-d", "#{old_time} #{timediff}"])).to_time
33 expected_time_upper_bound = expected_time_lower_bound + max_time_drift
34 assert(expected_time_lower_bound <= new_time &&
35 new_time <= expected_time_upper_bound,
36 "The #{clock_type} time was supposed to be bumped to " \
37 "'#{expected_time_lower_bound}' but is '#{new_time}'")
40 Then /^Tails clock is less than (\d+) minutes incorrect$/ do |max_diff_mins|
41 guest_time_str = $vm.execute("date --rfc-2822").stdout.chomp
42 guest_time = Time.rfc2822(guest_time_str)
44 diff = (host_time - guest_time).abs
45 assert(diff < max_diff_mins.to_i*60,
46 "The guest's clock is off by #{diff} seconds (#{guest_time})")
47 puts "Time was #{diff} seconds off"
50 Then /^the system clock is just past Tails' source date$/ do
51 system_time_str = $vm.execute_successfully('date').to_s
52 system_time = DateTime.parse(system_time_str).to_time
53 source_time_cmd = 'sed -n -e "1s/^.* - \([0-9]\+\)$/\1/p;q" ' +
54 '/etc/amnesia/version'
55 source_time_str = $vm.execute_successfully(source_time_cmd).to_s
56 source_time = DateTime.parse(source_time_str).to_time
57 diff = system_time - source_time # => in seconds
58 # Half an hour should be enough to boot Tails on any reasonable
59 # hardware and VM setup.
62 "The system time (#{system_time}) is before the Tails " +
63 "source date (#{source_time})")
66 # In this case the only acceptable explanation is that systemd
68 systemd_has_adjusted_time = $vm.execute(
69 "journalctl | grep 'System time before build time, advancing clock'"
71 if ! systemd_has_adjusted_time
73 "The system time (#{system_time}) is more than #{max_diff} seconds " +
74 "past the source date (#{source_time}) and systemd was not involved"
80 Then /^Tails' hardware clock is close to the host system's time$/ do
82 hwclock_time_str = $vm.execute('hwclock -r').stdout.chomp
83 hwclock_time = DateTime.parse(hwclock_time_str).to_time
84 diff = (hwclock_time - host_time).abs
85 assert(diff <= max_time_drift)
88 Then /^the hardware clock is still off by "([^"]+)"$/ do |timediff|
89 hwclock = DateTime.parse($vm.execute_successfully("hwclock -r").stdout.chomp).to_time
90 expected_time_lower_bound = DateTime.parse(
91 cmd_helper(["date", "-d", "now #{timediff}"])).to_time - max_time_drift
92 expected_time_upper_bound = expected_time_lower_bound + max_time_drift
93 assert(expected_time_lower_bound <= hwclock &&
94 hwclock <= expected_time_upper_bound,
95 "The host's hwclock should be approximately " \
96 "'#{expected_time_lower_bound}' but is actually '#{hwclock}'")