|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import asyncio |
| 16 | +import avatar |
| 17 | +import logging |
| 18 | + |
| 19 | +from avatar import BumblePandoraDevice |
| 20 | +from avatar import PandoraDevice |
| 21 | +from avatar import PandoraDevices |
| 22 | +from avatar.common import make_bredr_connection |
| 23 | +from avatar.common import make_le_connection |
| 24 | +from mobly import base_test |
| 25 | +from mobly import test_runner |
| 26 | +from mobly.asserts import assert_equal # type: ignore |
| 27 | +from mobly.asserts import assert_is_not_none # type: ignore |
| 28 | +from pandora import host_pb2 |
| 29 | +from pandora import l2cap_pb2 |
| 30 | +from typing import Any, Awaitable, Callable, Dict, Literal, Optional, Tuple, Union |
| 31 | + |
| 32 | +CONNECTORS: Dict[ |
| 33 | + str, |
| 34 | + Callable[[avatar.PandoraDevice, avatar.PandoraDevice], Awaitable[Tuple[host_pb2.Connection, host_pb2.Connection]]], |
| 35 | +] = { |
| 36 | + 'Classic': make_bredr_connection, |
| 37 | + 'LE': make_le_connection, |
| 38 | +} |
| 39 | + |
| 40 | +FIXED_CHANNEL_CID = 0x3E |
| 41 | +CLASSIC_PSM = 0xFEFF |
| 42 | +LE_SPSM = 0xF0 |
| 43 | + |
| 44 | + |
| 45 | +class L2capTest(base_test.BaseTestClass): # type: ignore[misc] |
| 46 | + devices: Optional[PandoraDevices] = None |
| 47 | + |
| 48 | + # pandora devices. |
| 49 | + dut: PandoraDevice |
| 50 | + ref: PandoraDevice |
| 51 | + |
| 52 | + def setup_class(self) -> None: |
| 53 | + self.devices = PandoraDevices(self) |
| 54 | + self.dut, self.ref, *_ = self.devices |
| 55 | + |
| 56 | + # Enable BR/EDR mode for Bumble devices. |
| 57 | + for device in self.devices: |
| 58 | + if isinstance(device, BumblePandoraDevice): |
| 59 | + device.config.setdefault("classic_enabled", True) |
| 60 | + |
| 61 | + def teardown_class(self) -> None: |
| 62 | + if self.devices: |
| 63 | + self.devices.stop_all() |
| 64 | + |
| 65 | + @avatar.asynchronous |
| 66 | + async def setup_test(self) -> None: # pytype: disable=wrong-arg-types |
| 67 | + await asyncio.gather(self.dut.reset(), self.ref.reset()) |
| 68 | + |
| 69 | + @avatar.parameterized( |
| 70 | + ('Classic', dict(fixed=l2cap_pb2.FixedChannelRequest(cid=FIXED_CHANNEL_CID))), |
| 71 | + ('LE', dict(fixed=l2cap_pb2.FixedChannelRequest(cid=FIXED_CHANNEL_CID))), |
| 72 | + ('Classic', dict(basic=l2cap_pb2.ConnectionOrientedChannelRequest(psm=CLASSIC_PSM))), |
| 73 | + ( |
| 74 | + 'LE', |
| 75 | + dict( |
| 76 | + le_credit_based=l2cap_pb2.CreditBasedChannelRequest( |
| 77 | + spsm=LE_SPSM, mtu=2046, mps=2048, initial_credit=256 |
| 78 | + ) |
| 79 | + ), |
| 80 | + ), |
| 81 | + ) # type: ignore[misc] |
| 82 | + @avatar.asynchronous |
| 83 | + async def test_connect( |
| 84 | + self, |
| 85 | + transport: Union[Literal['Classic'], Literal['LE']], |
| 86 | + request: Dict[str, Any], |
| 87 | + ) -> None: |
| 88 | + dut_ref_acl, ref_dut_acl = await CONNECTORS[transport](self.dut, self.ref) |
| 89 | + server = self.ref.aio.l2cap.OnConnection(connection=ref_dut_acl, **request) |
| 90 | + ref_dut_res, dut_ref_res = await asyncio.gather( |
| 91 | + anext(aiter(server)), |
| 92 | + self.dut.aio.l2cap.Connect(connection=dut_ref_acl, **request), |
| 93 | + ) |
| 94 | + assert_is_not_none(ref_dut_res.channel) |
| 95 | + assert_is_not_none(dut_ref_res.channel) |
| 96 | + |
| 97 | + @avatar.parameterized( |
| 98 | + ('Classic', dict(fixed=l2cap_pb2.FixedChannelRequest(cid=FIXED_CHANNEL_CID))), |
| 99 | + ('LE', dict(fixed=l2cap_pb2.FixedChannelRequest(cid=FIXED_CHANNEL_CID))), |
| 100 | + ('Classic', dict(basic=l2cap_pb2.ConnectionOrientedChannelRequest(psm=CLASSIC_PSM))), |
| 101 | + ( |
| 102 | + 'LE', |
| 103 | + dict( |
| 104 | + le_credit_based=l2cap_pb2.CreditBasedChannelRequest( |
| 105 | + spsm=LE_SPSM, mtu=2046, mps=2048, initial_credit=256 |
| 106 | + ) |
| 107 | + ), |
| 108 | + ), |
| 109 | + ) # type: ignore[misc] |
| 110 | + @avatar.asynchronous |
| 111 | + async def test_on_connection( |
| 112 | + self, |
| 113 | + transport: Union[Literal['Classic'], Literal['LE']], |
| 114 | + request: Dict[str, Any], |
| 115 | + ) -> None: |
| 116 | + dut_ref_acl, ref_dut_acl = await CONNECTORS[transport](self.dut, self.ref) |
| 117 | + server = self.dut.aio.l2cap.OnConnection(connection=dut_ref_acl, **request) |
| 118 | + ref_dut_res, dut_ref_res = await asyncio.gather( |
| 119 | + self.ref.aio.l2cap.Connect(connection=ref_dut_acl, **request), |
| 120 | + anext(aiter(server)), |
| 121 | + ) |
| 122 | + assert_is_not_none(ref_dut_res.channel) |
| 123 | + assert_is_not_none(dut_ref_res.channel) |
| 124 | + |
| 125 | + @avatar.parameterized( |
| 126 | + ('Classic', dict(basic=l2cap_pb2.ConnectionOrientedChannelRequest(psm=CLASSIC_PSM))), |
| 127 | + ( |
| 128 | + 'LE', |
| 129 | + dict( |
| 130 | + le_credit_based=l2cap_pb2.CreditBasedChannelRequest( |
| 131 | + spsm=LE_SPSM, mtu=2046, mps=2048, initial_credit=256 |
| 132 | + ) |
| 133 | + ), |
| 134 | + ), |
| 135 | + ) # type: ignore[misc] |
| 136 | + @avatar.asynchronous |
| 137 | + async def test_disconnect( |
| 138 | + self, |
| 139 | + transport: Union[Literal['Classic'], Literal['LE']], |
| 140 | + request: Dict[str, Any], |
| 141 | + ) -> None: |
| 142 | + dut_ref_acl, ref_dut_acl = await CONNECTORS[transport](self.dut, self.ref) |
| 143 | + server = self.ref.aio.l2cap.OnConnection(connection=ref_dut_acl, **request) |
| 144 | + ref_dut_res, dut_ref_res = await asyncio.gather( |
| 145 | + anext(aiter(server)), |
| 146 | + self.dut.aio.l2cap.Connect(connection=dut_ref_acl, **request), |
| 147 | + ) |
| 148 | + assert dut_ref_res.channel and ref_dut_res.channel |
| 149 | + |
| 150 | + await asyncio.gather( |
| 151 | + self.dut.aio.l2cap.Disconnect(channel=dut_ref_res.channel), |
| 152 | + self.ref.aio.l2cap.WaitDisconnection(channel=ref_dut_res.channel), |
| 153 | + ) |
| 154 | + |
| 155 | + @avatar.parameterized( |
| 156 | + ('Classic', dict(basic=l2cap_pb2.ConnectionOrientedChannelRequest(psm=CLASSIC_PSM))), |
| 157 | + ( |
| 158 | + 'LE', |
| 159 | + dict( |
| 160 | + le_credit_based=l2cap_pb2.CreditBasedChannelRequest( |
| 161 | + spsm=LE_SPSM, mtu=2046, mps=2048, initial_credit=256 |
| 162 | + ) |
| 163 | + ), |
| 164 | + ), |
| 165 | + ) # type: ignore[misc] |
| 166 | + @avatar.asynchronous |
| 167 | + async def test_wait_disconnection( |
| 168 | + self, |
| 169 | + transport: Union[Literal['Classic'], Literal['LE']], |
| 170 | + request: Dict[str, Any], |
| 171 | + ) -> None: |
| 172 | + dut_ref_acl, ref_dut_acl = await CONNECTORS[transport](self.dut, self.ref) |
| 173 | + server = self.ref.aio.l2cap.OnConnection(connection=ref_dut_acl, **request) |
| 174 | + ref_dut_res, dut_ref_res = await asyncio.gather( |
| 175 | + anext(aiter(server)), |
| 176 | + self.dut.aio.l2cap.Connect(connection=dut_ref_acl, **request), |
| 177 | + ) |
| 178 | + assert dut_ref_res.channel and ref_dut_res.channel |
| 179 | + |
| 180 | + await asyncio.gather( |
| 181 | + self.ref.aio.l2cap.Disconnect(channel=ref_dut_res.channel), |
| 182 | + self.dut.aio.l2cap.WaitDisconnection(channel=dut_ref_res.channel), |
| 183 | + ) |
| 184 | + |
| 185 | + @avatar.parameterized( |
| 186 | + ('Classic', dict(fixed=l2cap_pb2.FixedChannelRequest(cid=FIXED_CHANNEL_CID))), |
| 187 | + ('LE', dict(fixed=l2cap_pb2.FixedChannelRequest(cid=FIXED_CHANNEL_CID))), |
| 188 | + ('Classic', dict(basic=l2cap_pb2.ConnectionOrientedChannelRequest(psm=CLASSIC_PSM))), |
| 189 | + ( |
| 190 | + 'LE', |
| 191 | + dict( |
| 192 | + le_credit_based=l2cap_pb2.CreditBasedChannelRequest( |
| 193 | + spsm=LE_SPSM, mtu=2046, mps=2048, initial_credit=256 |
| 194 | + ) |
| 195 | + ), |
| 196 | + ), |
| 197 | + ) # type: ignore[misc] |
| 198 | + @avatar.asynchronous |
| 199 | + async def test_send( |
| 200 | + self, |
| 201 | + transport: Union[Literal['Classic'], Literal['LE']], |
| 202 | + request: Dict[str, Any], |
| 203 | + ) -> None: |
| 204 | + dut_ref_acl, ref_dut_acl = await CONNECTORS[transport](self.dut, self.ref) |
| 205 | + server = self.dut.aio.l2cap.OnConnection(connection=dut_ref_acl, **request) |
| 206 | + ref_dut_res, dut_ref_res = await asyncio.gather( |
| 207 | + self.ref.aio.l2cap.Connect(connection=ref_dut_acl, **request), |
| 208 | + anext(aiter(server)), |
| 209 | + ) |
| 210 | + ref_dut_channel = ref_dut_res.channel |
| 211 | + dut_ref_channel = dut_ref_res.channel |
| 212 | + assert_is_not_none(ref_dut_res.channel) |
| 213 | + assert_is_not_none(dut_ref_res.channel) |
| 214 | + assert ref_dut_channel and dut_ref_channel |
| 215 | + |
| 216 | + dut_ref_stream = self.ref.aio.l2cap.Receive(channel=dut_ref_channel) |
| 217 | + _send_res, recv_res = await asyncio.gather( |
| 218 | + self.dut.aio.l2cap.Send(channel=ref_dut_channel, data=b"The quick brown fox jumps over the lazy dog"), |
| 219 | + anext(aiter(dut_ref_stream)), |
| 220 | + ) |
| 221 | + assert recv_res.data |
| 222 | + assert_equal(recv_res.data, b"The quick brown fox jumps over the lazy dog") |
| 223 | + |
| 224 | + |
| 225 | +if __name__ == "__main__": |
| 226 | + logging.basicConfig(level=logging.DEBUG) |
| 227 | + test_runner.main() # type: ignore |
0 commit comments