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
23
24from vts.runners.host import const
25
26
27class LibcTest(base_test.BaseTestClass):
28    """A basic test of the libc API."""
29
30    def setUpClass(self):
31        self.dut = self.android_devices[0]
32        self.dut.lib.InitSharedLib(
33            target_type="bionic_libc",
34            target_basepaths=["/system/lib64"],
35            target_version=1.0,
36            target_filename="libc.so",
37            bits=64,
38            handler_name="libc",
39            target_package="lib.ndk.bionic")
40
41    def testOpenCloseLocalSocketStream(self):
42        """Tests open and close socket operations for local communication.
43
44        Uses local addresses and a streaming socket.
45        """
46        result = self.dut.lib.libc.socket(self.dut.lib.libc.PF_UNIX,
47                                          self.dut.lib.libc.SOCK_STREAM, 0)
48        asserts.assertNotEqual(result.return_type.scalar_value.int32_t, -1,
49                               "libc.socket: could not create socket.")
50
51        result = self.dut.lib.libc.close(
52            result.return_type.scalar_value.int32_t)
53        asserts.assertNotEqual(result.return_type.scalar_value.int32_t, -1,
54                               "libc.close: unable to close socket.")
55
56    def testOpenCloseLocalSocketDatagram(self):
57        """Tests open and close socket operations for local communication.
58
59        Uses local addresses and a datagram socket.
60        """
61        result = self.dut.lib.libc.socket(self.dut.lib.libc.PF_UNIX,
62                                          self.dut.lib.libc.SOCK_DGRAM, 0)
63        asserts.assertNotEqual(result.return_type.scalar_value.int32_t, -1,
64                               "libc.socket: could not create socket.")
65
66        result = self.dut.lib.libc.close(
67            result.return_type.scalar_value.int32_t)
68        asserts.assertNotEqual(result.return_type.scalar_value.int32_t, -1,
69                               "libc.close: unable to close socket.")
70
71    def testOpenCloseLocalSocketRaw(self):
72        """Tests open and close socket operations for local communication.
73
74        Uses local addresses and a raw socket.
75        """
76        result = self.dut.lib.libc.socket(self.dut.lib.libc.PF_UNIX,
77                                          self.dut.lib.libc.SOCK_RAW, 0)
78        asserts.assertNotEqual(result.return_type.scalar_value.int32_t, -1,
79                               "libc.socket: could not create socket.")
80
81        result = self.dut.lib.libc.close(
82            result.return_type.scalar_value.int32_t)
83        asserts.assertNotEqual(result.return_type.scalar_value.int32_t, -1,
84                               "libc.close: unable to close socket.")
85
86    def testOpenCloseLocalSocketSequential(self):
87        """Tests open and close socket operations for local communication.
88
89        Uses local addresses and a sequential socket.
90        """
91        result = self.dut.lib.libc.socket(self.dut.lib.libc.PF_UNIX,
92                                          self.dut.lib.libc.SOCK_SEQPACKET, 0)
93        asserts.assertNotEqual(result.return_type.scalar_value.int32_t, -1,
94                               "libc.socket: could not create socket.")
95
96        result = self.dut.lib.libc.close(
97            result.return_type.scalar_value.int32_t)
98        asserts.assertNotEqual(result.return_type.scalar_value.int32_t, -1,
99                               "libc.close: unable to close socket.")
100
101    def testOpenCloseINETSocketStream(self):
102        """Tests open and close socket operations for INET communication.
103
104        Uses IP addresses and a streaming socket.
105        """
106        result = self.dut.lib.libc.socket(self.dut.lib.libc.PF_INET,
107                                          self.dut.lib.libc.SOCK_STREAM, 0)
108        asserts.assertNotEqual(result.return_type.scalar_value.int32_t, -1,
109                               "libc.socket: could not create socket.")
110
111        result = self.dut.lib.libc.close(
112            result.return_type.scalar_value.int32_t)
113        asserts.assertNotEqual(result.return_type.scalar_value.int32_t, -1,
114                               "libc.close: unable to close socket.")
115
116    def testOpenCloseINETSocketDatagram(self):
117        """Tests open and close socket operations for INET communication.
118
119        Uses IP addresses and a datagram socket.
120        """
121        result = self.dut.lib.libc.socket(self.dut.lib.libc.PF_INET,
122                                          self.dut.lib.libc.SOCK_DGRAM, 0)
123        asserts.assertNotEqual(result.return_type.scalar_value.int32_t, -1,
124                               "libc.socket: could not create socket.")
125
126        result = self.dut.lib.libc.close(
127            result.return_type.scalar_value.int32_t)
128        asserts.assertNotEqual(result.return_type.scalar_value.int32_t, -1,
129                               "libc.close: unable to close socket.")
130
131
132if __name__ == "__main__":
133    test_runner.main()
134