time_syncing.rb 4.19 KB
Newer Older
1
2
3
4
5
6
7
8
# 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
  5
end

9
10
When /^I set the system time to "([^"]+)"$/ do |time|
  next if @skip_steps_while_restoring_background
11
  @vm.execute_successfully("date -s '#{time}'")
12
13
  new_time = DateTime.parse(@vm.execute_successfully("date").stdout).to_time
  expected_time_lower_bound = DateTime.parse(time).to_time
14
  expected_time_upper_bound = expected_time_lower_bound + max_time_drift
15
  assert(expected_time_lower_bound <= new_time &&
Tails developers's avatar
Tails developers committed
16
         new_time <= expected_time_upper_bound,
Tails developers's avatar
Tails developers committed
17
         "The guest's time was supposed to be set to " \
18
         "'#{expected_time_lower_bound}' but is '#{new_time}'")
19
20
end

kytv's avatar
kytv committed
21
When /^I bump the (hardware clock's|system) time with "([^"]+)"$/ do |clock_type, timediff|
22
  next if @skip_steps_while_restoring_background
kytv's avatar
kytv committed
23
24
25
26
27
28
29
30
31
32
  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
33
  expected_time_lower_bound = DateTime.parse(
34
      cmd_helper(["date", "-d", "#{old_time} #{timediff}"])).to_time
35
  expected_time_upper_bound = expected_time_lower_bound + max_time_drift
36
  assert(expected_time_lower_bound <= new_time &&
Tails developers's avatar
Tails developers committed
37
         new_time <= expected_time_upper_bound,
kytv's avatar
kytv committed
38
         "The #{clock_type} time was supposed to be bumped to " \
39
         "'#{expected_time_lower_bound}' but is '#{new_time}'")
40
41
42
43
44
45
46
47
48
49
50
51
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 = Time.rfc2822(guest_time_str)
  host_time = Time.now
  diff = (host_time - guest_time).abs
  assert(diff < max_diff_mins.to_i*60,
         "The guest's clock is off by #{diff} seconds (#{guest_time})")
  puts "Time was #{diff} seconds off"
end
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

Then /^the system clock is just past Tails' build date$/ do
  next if @skip_steps_while_restoring_background
  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
72
73
74
75
76
77
78
79
80

Then /^the hardware clock was not updated when Tails shut down$/ do
  host_time_str = cmd_helper(["date", "--rfc-2822"]).to_s
  host_time = Time.rfc2822(host_time_str).to_time
  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
81
82
83
84
85
86
87
88
89
90
91
92

Then /^the hardware clock is still off by "([^"]+)"$/ do |timediff|
  next if @skip_steps_while_restoring_background
  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