1#!/usr/bin/env python3.4
2#
3# Copyright (C) 2017 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#
17
18import logging
19
20from vts.runners.host import asserts
21from vts.runners.host import base_test
22from vts.runners.host import const
23from vts.runners.host import test_runner
24from vts.utils.python.android import api
25
26
27class VtsTreblePlatformVersionTest(base_test.BaseTestClass):
28    """VTS should run on devices launched with O or later."""
29
30    def setUpClass(self):
31        self.dut = self.android_devices[0]
32        self.dut.shell.InvokeTerminal("VtsTreblePlatformVersionTest")
33
34    def getProp(self, prop, required=True):
35        """Helper to retrieve a property from device."""
36
37        results = self.dut.shell.Execute("getprop " + prop)
38        if required:
39            asserts.assertEqual(results[const.EXIT_CODE][0], 0,
40                "getprop must succeed")
41            asserts.assertTrue(len(results[const.STDOUT][0].strip()) > 0,
42                "getprop must return a value")
43        else:
44            if (results[const.EXIT_CODE][0] != 0 or
45                len(results[const.STDOUT][0].strip()) == 0):
46                logging.info("sysprop %s undefined", prop)
47                return None
48
49        result = results[const.STDOUT][0].strip()
50
51        logging.info("getprop {}={}".format(prop, result))
52
53        return result
54
55    def getEnv(self, env):
56        """Helper to retrieve an environment varable from device."""
57
58        results = self.dut.shell.Execute("printenv " + env)
59        if (results[const.EXIT_CODE][0] != 0 or
60            len(results[const.STDOUT][0].strip()) == 0):
61            logging.info("environment variable %s undefined", env)
62            return None
63
64        result = results[const.STDOUT][0].strip()
65
66        logging.info("printenv {}:{}".format(env, result))
67
68        return result
69
70    def testFirstApiLevel(self):
71        """Test that device launched with O or later."""
72        launchApiLevel = self.dut.GetLaunchApiLevel()
73        asserts.assertTrue(launchApiLevel >= api.PLATFORM_API_LEVEL_O,
74            "VTS can only be run for new launches in O or above")
75        # Check first_api_level if device launches with P or above.
76        if launchApiLevel >= api.PLATFORM_API_LEVEL_P:
77            firstApiLevel_str = self.getProp("ro.product.first_api_level")
78            firstApiLevel = 0
79            if not firstApiLevel_str:
80                firstApiLevel = int(firstApiLevel_str)
81            asserts.assertTrue(firstApiLevel >= api.PLATFORM_API_LEVEL_P,
82                "Device running Android 9 or later MUST define PRODUCT_SHIPPING_API_LEVEL")
83
84    def testTrebleEnabled(self):
85        """Test that device has Treble enabled."""
86        trebleIsEnabledStr = self.getProp("ro.treble.enabled")
87        asserts.assertEqual(trebleIsEnabledStr, "true",
88            "VTS can only be run for Treble enabled devices")
89
90    def testSdkVersion(self):
91        """Test that SDK version >= O (26)."""
92        try:
93            sdkVersion = int(self.getProp("ro.build.version.sdk"))
94            asserts.assertTrue(sdkVersion >= api.PLATFORM_API_LEVEL_O,
95                "VTS is for devices launching in O or above")
96        except ValueError as e:
97            asserts.fail("Unexpected value returned from getprop: %s" % e)
98
99    def testVndkVersion(self):
100        """Test that VNDK version is specified.
101
102        If ro.vndk.version is not defined on boot, GSI sets LD_CONFIG_FILE to
103        temporary configuration file and ro.vndk.version to default value.
104        """
105
106        vndkVersion = self.getProp("ro.vndk.version")
107        if vndkVersion is None:
108            asserts.fail("VNDK version is not defined")
109
110        firstApiLevel = self.dut.getLaunchApiLevel()
111        if firstApiLevel > api.PLATFORM_API_LEVEL_O_MR1:
112            vndkLite = self.getProp("ro.vndk.lite", False)
113            if vndkLite is not None:
114                asserts.fail("ro.vndk.lite is defined as %s" % vndkLite)
115            envLdConfigFile = self.getEnv("LD_CONFIG_FILE")
116            if envLdConfigFile is not None:
117                asserts.fail("LD_CONFIG_FILE is defined as %s" % envLdConfigFile)
118
119if __name__ == "__main__":
120    test_runner.main()
121