1#!/usr/bin/env python
2#
3# Copyright (C) 2018 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
20
21from vts.runners.host import asserts
22from vts.runners.host import base_test
23from vts.runners.host import const
24from vts.runners.host import keys
25from vts.runners.host import test_runner
26from vts.utils.python.os import path_utils
27
28
29class VtsKernelNetTest(base_test.BaseTestClass):
30    """Host test class to run android kernel unit test.
31
32    Attributes:
33        dut: AndroidDevice, the device under test.
34        shell: AdbProxy, instance of adb shell.
35        host_bin_path: string, path to test binary on the host.
36        target_bin_path: string, path to test binary on the target.
37    """
38
39    def setUpClass(self):
40        required_params = [
41            keys.ConfigKeys.IKEY_DATA_FILE_PATH,
42        ]
43        self.getUserParams(required_params)
44        logging.info('%s: %s', keys.ConfigKeys.IKEY_DATA_FILE_PATH,
45                     self.data_file_path)
46
47        self.dut = self.android_devices[0]
48        self.shell = self.dut.adb.shell
49
50        # 32-bit version of the test should only run against 32-bit kernel;
51        # same for 64 bit.
52        bin_path = ('nativetest64' if self.dut.is64Bit else 'nativetest',
53                    'kernel_net_tests', 'kernel_net_tests')
54
55        self.host_bin_path = os.path.join(self.data_file_path, 'DATA', *bin_path)
56        self.target_bin_path = path_utils.JoinTargetPath('data', *bin_path)
57
58    def tearDownClass(self):
59        self.shell('rm -rf %s' % path_utils.TargetDirName(self.target_bin_path))
60
61    def testKernelNetworking(self):
62        """Android kernel unit test."""
63        # Push the test binary to target device.
64        self.shell('mkdir -p %s' % path_utils.TargetDirName(self.target_bin_path))
65        self.dut.adb.push('%s %s' % (self.host_bin_path, self.target_bin_path))
66        self.shell('chmod 777 %s' % self.target_bin_path)
67
68        # Execute the test binary.
69        result = self.shell(self.target_bin_path, no_except=True)
70
71        logging.info('stdout: %s', result[const.STDOUT])
72        logging.error('stderr: %s', result[const.STDERR])
73        logging.info('exit code: %s', result[const.EXIT_CODE])
74        asserts.assertFalse(
75            result[const.EXIT_CODE],
76            'kernel_net_tests binary returned non-zero exit code: \n' + result[const.STDERR])
77
78if __name__ == '__main__':
79    test_runner.main()
80