1# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging, math, re
6import subprocess
7import time
8from autotest_lib.client.bin import test, utils
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.cros import cros_logging
11
12OPENSSL = '/usr/bin/openssl'
13TLSDATE = '/usr/bin/tlsdate'
14
15class platform_AccurateTime(test.test):
16    version = 1
17
18    def serve(self):
19        self.ca = '%s/ca.pem' % self.srcdir
20        self.cert = '%s/cert.pem' % self.srcdir
21        self.key = '%s/cert.key' % self.srcdir
22        self.server = subprocess.Popen([OPENSSL, 's_server', '-www',
23                                        '-CAfile', self.ca, '-cert', self.cert,
24                                        '-key', self.key, '-port', '4433'])
25        time.sleep(1)
26
27    def tlsdate(self):
28        proc = subprocess.Popen([TLSDATE, '-H', '127.0.0.1', '-p', '4433',
29                                 '-C', self.srcdir,
30                                 '-nv'], stdout=subprocess.PIPE,
31                                 stderr=subprocess.PIPE)
32        (out,err) = proc.communicate()
33        return err
34
35    def run_once(self):
36        self.serve()
37        out = self.tlsdate()
38        print out
39        try:
40            if 'verification passed' not in out:
41                raise error.TestFail('ssl did not verify')
42            if 'difference is about' not in out:
43                raise error.TestFail('no time delta found')
44        finally:
45            self.server.terminate()
46