1#!/usr/bin/env python3
2#
3# Copyright (C) 2019 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may not
6# use this file except in compliance with the License. You may obtain a copy of
7# 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, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations under
15# the License.
16"""
17Basic SDP Tests.
18
19This only requires a single bluetooth_device
20and exercises adding/removing services, initialization, and
21adding search records.
22"""
23from acts import signals
24from acts.base_test import BaseTestClass
25from acts.test_utils.abstract_devices.bluetooth_device import AndroidBluetoothDevice
26from acts.test_utils.abstract_devices.bluetooth_device import FuchsiaBluetoothDevice
27from acts.test_utils.abstract_devices.bluetooth_device import create_bluetooth_device
28from acts.test_utils.bt.bt_constants import bt_attribute_values
29from acts.test_utils.bt.bt_constants import sig_uuid_constants
30from acts.test_utils.fuchsia.sdp_records import sdp_pts_record_list
31
32
33class SdpSetupTest(BaseTestClass):
34    def setup_class(self):
35        super(SdpSetupTest, self).setup_class()
36        if 'dut' in self.user_params:
37            if self.user_params['dut'] == 'fuchsia_devices':
38                self.dut = create_bluetooth_device(self.fuchsia_devices[0])
39            elif self.user_params['dut'] == 'android_devices':
40                self.dut = create_bluetooth_device(self.android_devices[0])
41            else:
42                raise ValueError('Invalid DUT specified in config. (%s)' %
43                                 self.user_params['dut'])
44        else:
45            # Default is an fuchsia device
46            self.dut = create_bluetooth_device(self.fuchsia_devices[0])
47        self.dut.initialize_bluetooth_controller()
48
49    def setup_test(self):
50        self.dut.sdp_clean_up()
51
52    def cleanup_class(self):
53        self.dut.sdp_clean_up()
54
55    def test_init(self):
56        result = self.dut.sdp_init()
57        if result.get("error") is None:
58            raise signals.TestPass("Success")
59        else:
60            raise signals.TestFailure(
61                "Failed to initialize SDP with {}".format(result.get("error")))
62
63    def test_add_service(self):
64        self.dut.sdp_init()
65        result = self.dut.sdp_add_service(sdp_pts_record_list[0])
66        if result.get("error") is not None:
67            raise signals.TestFailure(
68                "Failed to add SDP service record: {}".format(
69                    result.get("error")))
70        else:
71            raise signals.TestPass("Success")
72
73    def test_malformed_service(self):
74        self.dut.sdp_init()
75        malformed_record = {'malformed_sdp_record_input': ["1101"]}
76        result = self.dut.sdp_add_service(malformed_record)
77        if result.get("error") is not None:
78            raise signals.TestPass("Successfully failed with: {}".format(
79                result.get("error")))
80        else:
81            raise signals.TestFailure(
82                "Expected failure of adding SDP record: {}".format(
83                    malformed_record))
84
85    def test_add_search(self):
86        attributes = [
87            bt_attribute_values['ATTR_PROTOCOL_DESCRIPTOR_LIST'],
88            bt_attribute_values['ATTR_SERVICE_CLASS_ID_LIST'],
89            bt_attribute_values['ATTR_BLUETOOTH_PROFILE_DESCRIPTOR_LIST'],
90            bt_attribute_values['ATTR_A2DP_SUPPORTED_FEATURES'],
91        ]
92
93        self.dut.sdp_init()
94        profile_id = int(sig_uuid_constants['AudioSource'], 16)
95        result = self.dut.sdp_add_search(attributes, profile_id)
96        if result.get("error") is not None:
97            raise signals.TestFailure("Failed to add SDP search: {}".format(
98                result.get("error")))
99        else:
100            raise signals.TestPass("Success")
101
102    def test_include_additional_attributes(self):
103        self.dut.sdp_init()
104        additional_attributes = [{
105            'id': 0x0201,
106            'element': {
107                'data': int(sig_uuid_constants['AVDTP'], 16)
108            }
109        }]
110
111        sdp_pts_record_list[0]['additional_attributes'] = additional_attributes
112        result = self.dut.sdp_add_service(sdp_pts_record_list[0])
113        if result.get("error") is not None:
114            raise signals.TestFailure(
115                "Failed to add SDP service record: {}".format(
116                    result.get("error")))
117        else:
118            raise signals.TestPass("Success")
119
120    def test_add_multiple_services(self):
121        self.dut.sdp_init()
122        number_of_records = 10
123        for i in range(number_of_records):
124            result = self.dut.sdp_add_service(sdp_pts_record_list[i])
125            if result.get("error") is not None:
126                raise signals.TestFailure(
127                    "Failed to add SDP service record: {}".format(
128                        result.get("error")))
129        raise signals.TestPass("Success")
130