1#!/usr/bin/env python
2#
3# Copyright (C) 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#
17
18import logging
19
20from vts.runners.host import asserts
21from vts.runners.host import base_test
22from vts.runners.host import test_runner
23from vts.utils.python.controllers import android_device
24from vts.runners.host import const
25
26
27class ShellBinaryCrashTest(base_test.BaseTestClass):
28    """A binary crash test case for the shell driver."""
29
30    EXIT_CODE_CRASH = 133
31    EXIT_CODE_SEGFAULT = 139
32
33    def setUpClass(self):
34        self.run_as_vts_self_test = False
35        self.dut = self.android_devices[0]
36
37    def testCrashBinary(self):
38        """Tests whether the agent survives when a called binary crashes."""
39        self.dut.shell.InvokeTerminal("my_shell1")
40        target = "/data/local/tmp/vts_test_binary_crash_app"
41        results = self.dut.shell.my_shell1.Execute(
42            ["chmod 755 %s" % target, target])
43        logging.info(str(results[const.STDOUT]))
44        asserts.assertEqual(len(results[const.STDOUT]), 2)
45        asserts.assertEqual(results[const.STDOUT][1].strip(), "")
46        # "crash_app: start" is also valid output.
47        asserts.assertEqual(results[const.EXIT_CODE][1], self.EXIT_CODE_CRASH)
48
49        self.CheckShellDriver("my_shell1")
50        self.CheckShellDriver("my_shell2")
51
52    def testSegmentFaultBinary(self):
53        """Tests whether the agent survives when a binary leads to segfault."""
54        self.dut.shell.InvokeTerminal("my_shell1")
55        target = "/data/local/tmp/vts_test_binary_seg_fault"
56        results = self.dut.shell.my_shell1.Execute(
57            ["chmod 755 %s" % target, target])
58        logging.info(str(results[const.STDOUT]))
59        asserts.assertEqual(len(results[const.STDOUT]), 2)
60        asserts.assertEqual(results[const.STDOUT][1].strip(), "")
61        # TODO: currently the agent doesn't return the stdout log emitted
62        # before a failure.
63        asserts.assertEqual(results[const.EXIT_CODE][1],
64                            self.EXIT_CODE_SEGFAULT)
65
66        self.CheckShellDriver("my_shell1")
67        self.CheckShellDriver("my_shell2")
68
69    def CheckShellDriver(self, shell_name):
70        """Checks whether the shell driver sevice is available.
71
72        Args:
73            shell_name: string, the name of a shell service to create.
74        """
75        self.dut.shell.InvokeTerminal(shell_name)
76        results = getattr(self.dut.shell, shell_name).Execute("which ls")
77        logging.info(str(results[const.STDOUT]))
78        asserts.assertEqual(len(results[const.STDOUT]), 1)
79        asserts.assertEqual(results[const.STDOUT][0].strip(),
80                            "/system/bin/ls")
81        asserts.assertEqual(results[const.EXIT_CODE][0], 0)
82
83
84if __name__ == "__main__":
85    test_runner.main()
86