1#!/usr/bin/env python
2#
3# Copyright 2016 - The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import difflib
18import filecmp
19import getopt
20import logging
21import os
22import shutil
23import subprocess
24import sys
25import unittest
26
27from vts.utils.python.common import cmd_utils
28
29
30class VtscTester(unittest.TestCase):
31    """Integration test runner for vtsc in generating the driver/profiler code.
32
33    Runs vtsc with specified mode on a bunch of files and compares the output
34    results with canonical ones. Exit code is 0 iff all tests pass.
35    Note: need to run the script from the source root to preserve the correct
36          path.
37
38    Usage:
39        python test_vtsc.py path_to_vtsc canonical_dir output_dir
40
41    example:
42        python test/vts/compilation_tools/vtsc/test/test_vtsc.py vtsc
43        test/vts/compilation_tools/vtsc/test/golden/ temp_output
44
45    Attributes:
46        _hidl_gen_path: the path to run hidl-gen
47        _vtsc_path: the path to run vtsc.
48        _canonical_dir: root directory contains canonical files for comparison.
49        _output_dir: root directory that stores all output files.
50        _errors: number of errors generates during the test.
51        _temp_dir: temp dir to store the .vts file generated by hidl-gen.
52    """
53
54    def __init__(self, testName, hidl_gen_path, vtsc_path, canonical_dir,
55                 output_dir, temp_dir):
56        super(VtscTester, self).__init__(testName)
57        self._hidl_gen_path = hidl_gen_path
58        self._vtsc_path = vtsc_path
59        self._canonical_dir = canonical_dir
60        self._output_dir = output_dir
61        self._errors = 0
62        self._temp_dir = temp_dir
63
64    def setUp(self):
65        """Removes output dir to prevent interference from previous runs."""
66        self.RemoveOutputDir()
67
68    def tearDown(self):
69        """If successful, removes the output dir for clean-up."""
70        if self._errors == 0:
71            self.RemoveOutputDir()
72
73    def testAll(self):
74        """Run all tests. """
75        self.TestDriver()
76        self.TestProfiler()
77        self.TestFuzzer()
78        self.assertEqual(self._errors, 0)
79
80    def TestDriver(self):
81        """Run tests for DRIVER mode. """
82        logging.info("Running TestDriver test case.")
83        # Tests for Hidl Hals.
84        for package_path, component_names in zip(
85            ["android.hardware.nfc@1.0",
86             "android.hardware.tests.bar@1.0",
87             "android.hardware.tests.msgq@1.0",
88             "android.hardware.tests.memory@1.0"],
89            [["Nfc", "NfcClientCallback", "types"],
90             ["Bar"], ["TestMsgQ"], ["MemoryTest"]]):
91            self.GenerateVtsFile(package_path)
92            for component_name in component_names:
93                self.RunTest(
94                    "DRIVER",
95                    os.path.join(self._temp_dir, component_name + ".vts"),
96                    "%s.vts.h" % component_name,
97                    header_file_name="%s.vts.h" % component_name,
98                    file_type="HEADER")
99                self.RunTest(
100                    "DRIVER",
101                    os.path.join(self._temp_dir, component_name + ".vts"),
102                    "%s.driver.cpp" % component_name,
103                    file_type="SOURCE")
104        # Tests for shared libraries.
105        for component_name in ["libcV1"]:
106            self.RunTest("DRIVER",
107                         "test/vts/specification/lib/ndk/bionic/1.0/%s.vts" %
108                         component_name, "%s.driver.cpp" % component_name)
109
110    def TestProfiler(self):
111        """Run tests for PROFILER mode. """
112        logging.info("Running TestProfiler test case.")
113        #self.GenerateVtsFile("android.hardware.nfc@1.0")
114        for package_path, component_names in zip(
115            ["android.hardware.nfc@1.0",
116             "android.hardware.tests.bar@1.0",
117             "android.hardware.tests.msgq@1.0",
118             "android.hardware.tests.memory@1.0"],
119            [["Nfc", "NfcClientCallback", "types"],
120             ["Bar"], ["TestMsgQ"], ["MemoryTest"]]):
121            self.GenerateVtsFile(package_path)
122            for component_name in component_names:
123                self.RunTest(
124                    "PROFILER",
125                    os.path.join(self._temp_dir, component_name + ".vts"),
126                    "%s.vts.h" % component_name,
127                    header_file_name="%s.vts.h" % component_name,
128                    file_type="HEADER")
129                self.RunTest(
130                    "PROFILER",
131                    os.path.join(self._temp_dir, component_name + ".vts"),
132                    "%s.profiler.cpp" % component_name,
133                    file_type="SOURCE")
134
135    def TestFuzzer(self):
136        """Run tests for Fuzzer mode. """
137        logging.info("Running TestProfiler test case.")
138        self.GenerateVtsFile("android.hardware.renderscript@1.0")
139        for component_name in ["Context", "Device", "types"]:
140            self.RunTest(
141                "FUZZER",
142                os.path.join(self._temp_dir, component_name + ".vts"),
143                "%s.fuzzer.cpp" % component_name,
144                file_type="SOURCE")
145
146    def RunFuzzerTest(self, mode, vts_file_path, source_file_name):
147        vtsc_cmd = [
148            self._vtsc_path, "-m" + mode, vts_file_path,
149            os.path.join(self._output_dir, mode),
150            os.path.join(self._output_dir, mode, "")
151        ]
152        return_code = cmd_utils.RunCommand(vtsc_cmd)
153
154        canonical_source_file = os.path.join(self._canonical_dir, mode,
155                                             source_file_name)
156        output_source_file = os.path.join(self._output_dir, mode,
157                                          source_file_name)
158        self.CompareOutputFile(output_source_file, canonical_source_file)
159
160    def GenerateVtsFile(self, hal_package_name):
161        """Run hidl-gen to generate the .vts files for the give hal package.
162
163        Args:
164            hal_package_name: name of hal package e.g. android.hardware.nfc@1.0
165        """
166        hidl_gen_cmd = [
167            self._hidl_gen_path, "-o" + self._temp_dir, "-Lvts",
168            "-randroid.hardware:hardware/interfaces",
169            "-randroid.hidl:system/libhidl/transport", hal_package_name
170        ]
171        return_code = cmd_utils.RunCommand(hidl_gen_cmd)
172        if (return_code != 0):
173            self.Error("Fail to execute command: %s" % hidl_gen_cmd)
174        [hal_name, hal_version] = hal_package_name.split("@")
175        output_dir = os.path.join(self._temp_dir,
176                                  hal_name.replace(".", "/"), hal_version)
177        for file in os.listdir(output_dir):
178            if file.endswith(".vts"):
179                os.rename(
180                    os.path.join(output_dir, file),
181                    os.path.join(self._temp_dir, file))
182
183    def RunTest(self,
184                mode,
185                vts_file_path,
186                output_file_name,
187                header_file_name="",
188                file_type="BOTH"):
189        """Run vtsc with given mode for the give vts file and compare the
190           output results.
191
192        Args:
193            mode: the vtsc mode for generated code. e.g. DRIVER / PROFILER.
194            vts_file_path: path of the input vts file.
195            source_file_name: name of the generated source file.
196            file_type: type of file e.g. HEADER / SOURCE / BOTH.
197        """
198        if (file_type == "BOTH"):
199            vtsc_cmd = [
200                self._vtsc_path, "-m" + mode, vts_file_path,
201                os.path.join(self._output_dir, mode),
202                os.path.join(self._output_dir, mode, output_file_name)
203            ]
204        else:
205            vtsc_cmd = [
206                self._vtsc_path, "-m" + mode, "-t" + file_type, vts_file_path,
207                os.path.join(self._output_dir, mode, output_file_name)
208            ]
209        return_code = cmd_utils.RunCommand(vtsc_cmd)
210        if (return_code != 0):
211            self.Error("Fail to execute command: %s" % vtsc_cmd)
212
213        if (file_type == "HEADER" or file_type == "BOTH"):
214            if not header_file_name:
215                header_file_name = vts_file_path + ".h"
216            canonical_header_file = os.path.join(self._canonical_dir, mode,
217                                                 header_file_name)
218            output_header_file = os.path.join(self._output_dir, mode,
219                                              header_file_name)
220            self.CompareOutputFile(output_header_file, canonical_header_file)
221        elif (file_type == "SOURCE" or file_type == "BOTH"):
222            canonical_source_file = os.path.join(self._canonical_dir, mode,
223                                                 output_file_name)
224            output_source_file = os.path.join(self._output_dir, mode,
225                                              output_file_name)
226            self.CompareOutputFile(output_source_file, canonical_source_file)
227        else:
228            self.Error("No such file_type: %s" % file_type)
229
230    def CompareOutputFile(self, output_file, canonical_file):
231        """Compares a given file and the corresponding one under canonical_dir.
232
233        Args:
234            canonical_file: name of the canonical file.
235            output_file: name of the output file.
236        """
237        if not os.path.isfile(canonical_file):
238            self.Error("Generated unexpected file: %s (for %s)" %
239                       (output_file, canonical_file))
240        else:
241            if not filecmp.cmp(output_file, canonical_file):
242                self.Error(
243                    "output file: %s does not match the canonical_file: "
244                    "%s" % (output_file, canonical_file))
245                self.PrintDiffFiles(output_file, canonical_file)
246
247    def PrintDiffFiles(self, output_file, canonical_file):
248        with open(output_file, 'r') as file1:
249            with open(canonical_file, 'r') as file2:
250                diff = difflib.unified_diff(
251                    file1.readlines(),
252                    file2.readlines(),
253                    fromfile=output_file,
254                    tofile=canonical_file)
255        for line in diff:
256            logging.error(line)
257
258    def Error(self, string):
259        """Prints an error message and increments error count."""
260        logging.error(string)
261        self._errors += 1
262
263    def RemoveOutputDir(self):
264        """Remove the output_dir if it exists."""
265        if os.path.exists(self._output_dir):
266            logging.info("rm -rf %s", self._output_dir)
267            shutil.rmtree(self._output_dir)
268        if os.path.exists(self._temp_dir):
269            shutil.rmtree(self._temp_dir)
270
271
272if __name__ == "__main__":
273    # Default values of the input parameter, could be overridden by command.
274    vtsc_path = "vtsc"
275    canonical_dir = "test/vts/compilation_tools/vtsc/test/golden/"
276    output_dir = "test/vts/compilation_tools/vtsc/test/temp_coutput/"
277    # Parse the arguments and set the provided value for
278    # hidl-gen/vtsc_path/canonical_dar/output_dir.
279    try:
280        opts, _ = getopt.getopt(sys.argv[1:], "h:p:c:o:t:")
281    except getopt.GetoptError, err:
282        print "Usage: python test_vtsc.py [-h hidl_gen_path] [-p vtsc_path] " \
283              "[-c canonical_dir] [-o output_dir] [-t temp_dir]"
284        sys.exit(1)
285    for opt, val in opts:
286        if opt == "-h":
287            hidl_gen_path = val
288        elif opt == "-p":
289            vtsc_path = val
290        elif opt == "-c":
291            canonical_dir = val
292        elif opt == "-o":
293            output_dir = val
294        elif opt == "-t":
295            temp_dir = val
296        else:
297            print "unhandled option %s" % (opt, )
298            sys.exit(1)
299
300    suite = unittest.TestSuite()
301    suite.addTest(
302        VtscTester('testAll', hidl_gen_path, vtsc_path, canonical_dir,
303                   output_dir, temp_dir))
304    result = unittest.TextTestRunner(verbosity=2).run(suite)
305    if not result.wasSuccessful():
306        sys.exit(-1)
307