1# Copyright (c) 2010 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 os
6
7from autotest_lib.client.bin import test, utils
8from autotest_lib.client.common_lib import error
9
10class firmware_VbootCrypto(test.test):
11    """
12    Tests for correctness of verified boot reference crypto implementation.
13    """
14    version = 1
15    preserve_srcdir = True
16
17    # TODO(gauravsh): Disable this autotest until we have a way
18    # of running these in a 64-bit environment (since for x86, this
19    # code is run in 64-bit mode.
20    #
21    # This issue is tracked as Issue 3792 on the Chromium OS Bug Tracker.
22    # http://code.google.com/p/chromium-os/issues/detail?id=3792
23    def setup_Disabled(self):
24        os.chdir(self.srcdir)
25        utils.make('clean')
26        utils.make('all')
27
28
29    # Parses the [result] and output the key-value pairs.
30    def __output_result_keyvals(self, results):
31        for keyval in results.splitlines():
32            if keyval.strip().startswith('#'):
33                continue
34            key, val = keyval.split(':')
35            self.keyvals[key.strip()] = float(val)
36
37
38    def __generate_test_cases(self):
39        gen_test_case_cmd = os.path.join(self.srcdir, "tests",
40                                         "gen_test_cases.sh")
41        return_code = utils.system(gen_test_case_cmd, ignore_status = True)
42        if return_code == 255:
43            return False
44        if return_code == 1:
45            raise error.TestError("Couldn't generate test cases")
46        return True
47
48
49    def __sha_test(self):
50        sha_test_cmd = os.path.join(self.srcdir, "tests", "sha_tests")
51        return_code = utils.system(sha_test_cmd, ignore_status=True)
52        if return_code == 255:
53            return False
54        if return_code == 1:
55            raise error.TestError("SHA Test Error")
56        return True
57
58
59    def __rsa_test(self):
60        os.chdir(self.srcdir)
61        rsa_test_cmd = os.path.join(self.srcdir, "tests",
62                                    "run_rsa_tests.sh")
63        return_code = utils.system(rsa_test_cmd, ignore_status=True)
64        if return_code == 255:
65            return False
66        if return_code == 1:
67            raise error.TestError("RSA Test Error")
68        return True
69
70
71    def __image_verification_test(self):
72        image_verification_cmd = "cd %s && ./run_image_verification_tests.sh" \
73                                 % os.path.join(self.srcdir, "tests")
74        return_code = utils.system(image_verification_cmd,
75                                   ignore_status=True)
76        if return_code == 255:
77            return False
78        if return_code == 1:
79            raise error.TestError("Image Verification Test Error")
80        return True
81
82
83    def __sha_benchmark(self):
84        sha_benchmark_cmd = os.path.join(self.srcdir, "tests",
85                                         "sha_benchmark")
86        self.results = utils.system_output(sha_benchmark_cmd,
87                                           retain_output=True)
88        self.__output_result_keyvals(self.results)
89
90
91    def __rsa_benchmark(self):
92        rsa_benchmark_cmd = "cd %s && ./rsa_verify_benchmark" % \
93                            os.path.join(self.srcdir, "tests")
94        self.results = utils.system_output(rsa_benchmark_cmd,
95                                           retain_output=True)
96        self.__output_result_keyvals(self.results)
97
98
99    def __verify_image_benchmark(self):
100        firmware_benchmark_cmd = "cd %s && ./firmware_verify_benchmark" % \
101                                 os.path.join(self.srcdir, "tests")
102        kernel_benchmark_cmd = "cd %s && ./kernel_verify_benchmark" % \
103                                 os.path.join(self.srcdir, "tests")
104        self.results = utils.system_output(firmware_benchmark_cmd,
105                                           retain_output=True)
106        self.__output_result_keyvals(self.results)
107        self.results = utils.system_output(kernel_benchmark_cmd,
108                                           retain_output=True)
109        self.__output_result_keyvals(self.results)
110
111
112    def __rollback_tests(self):
113        firmware_rollback_test_cmd = "cd %s && ./firmware_rollback_tests" % \
114                                     os.path.join(self.srcdir, "tests")
115        kernel_rollback_test_cmd = "cd %s && ./kernel_rollback_tests" % \
116                                     os.path.join(self.srcdir, "tests")
117        return_code = utils.system(firmware_rollback_test_cmd,
118                                   ignore_status=True)
119        if return_code == 255:
120            return False
121        if return_code == 1:
122            raise error.TestError("Firmware Rollback Test Error")
123
124        return_code = utils.system(kernel_rollback_test_cmd,
125                                   ignore_status=True)
126        if return_code == 255:
127            return False
128        if return_code == 1:
129            raise error.TestError("KernelRollback Test Error")
130        return True
131
132
133    def __splicing_tests(self):
134        firmware_splicing_test_cmd = "cd %s && ./firmware_splicing_tests" % \
135                                     os.path.join(self.srcdir, "tests")
136        kernel_splicing_test_cmd = "cd %s && ./kernel_splicing_tests" % \
137                                     os.path.join(self.srcdir, "tests")
138        return_code = utils.system(firmware_splicing_test_cmd,
139                                   ignore_status=True)
140        if return_code == 255:
141            return False
142        if return_code == 1:
143            raise error.TestError("Firmware Splicing Test Error")
144
145        return_code = utils.system(kernel_splicing_test_cmd,
146                                   ignore_status=True)
147        if return_code == 255:
148            return False
149        if return_code == 1:
150            raise error.TestError("Kernel Splicing Test Error")
151        return True
152
153
154    def run_crypto(self):
155        success = self.__sha_test()
156        if not success:
157            raise error.TestFail("SHA Test Failed")
158        success = self.__rsa_test()
159        if not success:
160            raise error.TestFail("RSA Test Failed")
161
162
163    def run_verification(self):
164        success = self.__image_verification_test()
165        if not success:
166            raise error.TestFail("Image Verification Test Failed")
167
168
169    def run_benchmarks(self):
170        self.keyvals = {}
171        self.__sha_benchmark()
172        self.__rsa_benchmark()
173        self.__verify_image_benchmark()
174        self.write_perf_keyval(self.keyvals)
175
176
177    def run_rollback(self):
178        success = self.__rollback_tests()
179        if not success:
180            raise error.TestFail("Rollback Tests Failed")
181
182
183    def run_splicing(self):
184        success = self.__splicing_tests()
185        if not success:
186            raise error.TestFail("Splicing Tests Failed")
187
188
189    def run_once(self, suite='crypto'):
190        self.__generate_test_cases()
191        getattr(self, 'run_' + suite)()
192