1#!/usr/bin/env python3.4
2#
3# Copyright (C) 2020 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
19import os
20import unittest
21
22from vts.testcases.vndk import utils
23from vts.utils.python.android import api
24
25
26class VtsTreblePlatformVersionTest(unittest.TestCase):
27    """VTS should run on devices launched with O or later."""
28
29    def setUp(self):
30        serial_number = os.environ.get("ANDROID_SERIAL")
31        self.assertTrue(serial_number, "$ANDROID_SERIAL is empty.")
32        self.dut = utils.AndroidDevice(serial_number)
33
34    def getProp(self, prop, required=True):
35        """Helper to retrieve a property from device."""
36
37        out, err, return_code = self.dut.Execute("getprop " + prop)
38        if required:
39            self.assertEqual(return_code, 0,
40                "getprop must succeed")
41            self.assertTrue(len(out.strip()) > 0,
42                "getprop must return a value")
43        else:
44            if (return_code != 0 or
45                len(out.strip()) == 0):
46                logging.info("sysprop %s undefined", prop)
47                return None
48
49        result = out.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 variable from device."""
57
58        out, err, return_code = self.dut.Execute("printenv " + env)
59        if (return_code != 0 or
60            len(out.strip()) == 0):
61            logging.info("environment variable %s undefined", env)
62            return None
63
64        result = out.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        firstApiLevel = self.dut.GetLaunchApiLevel()
73        self.assertTrue(firstApiLevel >= api.PLATFORM_API_LEVEL_O,
74                        "VTS can only be run for new launches in O or above")
75
76    def testTrebleEnabled(self):
77        """Test that device has Treble enabled."""
78        trebleIsEnabledStr = self.getProp("ro.treble.enabled")
79        self.assertEqual(
80            trebleIsEnabledStr, "true",
81            "VTS can only be run for Treble enabled devices")
82
83    def testSdkVersion(self):
84        """Test that SDK version >= O (26)."""
85        try:
86            sdkVersion = int(self.getProp("ro.build.version.sdk"))
87            self.assertTrue(sdkVersion >= api.PLATFORM_API_LEVEL_O,
88                "VTS is for devices launching in O or above")
89        except ValueError as e:
90            asserts.fail("Unexpected value returned from getprop: %s" % e)
91
92    def testVndkVersion(self):
93        """Test that VNDK version is specified.
94
95        If ro.vndk.version is not defined on boot, GSI sets LD_CONFIG_FILE to
96        temporary configuration file and ro.vndk.version to default value.
97        """
98
99        vndkVersion = self.getProp("ro.vndk.version")
100        if vndkVersion is None:
101            asserts.fail("VNDK version is not defined")
102
103        firstApiLevel = self.dut.GetLaunchApiLevel()
104        if firstApiLevel > api.PLATFORM_API_LEVEL_O_MR1:
105            vndkLite = self.getProp("ro.vndk.lite", False)
106            if vndkLite is not None:
107                asserts.fail("ro.vndk.lite is defined as %s" % vndkLite)
108            envLdConfigFile = self.getEnv("LD_CONFIG_FILE")
109            if envLdConfigFile is not None:
110                asserts.fail("LD_CONFIG_FILE is defined as %s" % envLdConfigFile)
111
112if __name__ == "__main__":
113    # Setting verbosity is required to generate output that the TradeFed test
114    # runner can parse.
115    unittest.main(verbosity=3)
116