Manual test suite: use more precise data when comparing image size.
[tails.git] / features / step_definitions / time_syncing.rb
blobfcbab1df06e9cfdbd1db97cba73f4aa0e971f05d
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.
5 def max_time_drift
6   15
7 end
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}'")
18 end
20 When /^I bump the (hardware clock's|system) time with "([^"]+)"$/ do |clock_type, timediff|
21   case clock_type
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
26   when 'system'
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
30   end
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}'")
38 end
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)
43   host_time = Time.now
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"
48 end
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.
60   max_diff = 30*60
61   assert(diff > 0,
62          "The system time (#{system_time}) is before the Tails " +
63          "source date (#{source_time})")
65   if diff <= max_diff
66     # In this case the only acceptable explanation is that systemd
67     # adjusted the time.
68     systemd_has_adjusted_time = $vm.execute(
69       "journalctl | grep 'System time before build time, advancing clock'"
70     ).success?
71     if ! systemd_has_adjusted_time
72       raise(
73         "The system time (#{system_time}) is more than #{max_diff} seconds " +
74         "past the source date (#{source_time}) and systemd was not involved"
75       )
76     end
77   end
78 end
80 Then /^Tails' hardware clock is close to the host system's time$/ do
81   host_time = Time.now
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)
86 end
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}'")
97 end