Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 84a74b74 authored by Pomai Ahlo's avatar Pomai Ahlo
Browse files

RFCOMM Client BumbleBluetooth: Receive

Add tests for receiving data

Bug: 331415222
Test: atest BumbleBluetoothTests:android.bluetooth.RfcommTest
Flag: TEST_ONLY
Change-Id: I02c7b284110fd15e6cad20a135ac74b902e85464
parent 07760ab7
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
@@ -244,6 +244,48 @@ class RfcommTest {
        }
    }

    @Test
    fun clientReceiveDataOverInsecureSocket() {
        startServer {
            val serverId = it
            runBlocking { withTimeout(BOND_TIMEOUT.toMillis()) { bondDevice(mBumbleDevice) } }

            val (insecureSocket, connection) = createAndConnectSocket(isSecure = false, serverId)
            val buffer = ByteArray(64)
            val socketIs = insecureSocket.inputStream
            val data: ByteString =
                ByteString.copyFromUtf8("Test data for clientReceiveDataOverInsecureSocket")

            val txRequest =
                RfcommProto.TxRequest.newBuilder().setConnection(connection).setData(data).build()
            mBumble.rfcommBlocking().send(txRequest)
            val numBytesFromBumble = socketIs.read(buffer)
            Truth.assertThat(ByteString.copyFrom(buffer).substring(0, numBytesFromBumble))
                .isEqualTo(data)
        }
    }

    @Test
    fun clientReceiveDataOverSecureSocket() {
        startServer {
            val serverId = it
            runBlocking { withTimeout(BOND_TIMEOUT.toMillis()) { bondDevice(mBumbleDevice) } }

            val (secureSocket, connection) = createAndConnectSocket(isSecure = true, serverId)
            val buffer = ByteArray(64)
            val socketIs = secureSocket.inputStream
            val data: ByteString =
                ByteString.copyFromUtf8("Test data for clientReceiveDataOverSecureSocket")

            val txRequest =
                RfcommProto.TxRequest.newBuilder().setConnection(connection).setData(data).build()
            mBumble.rfcommBlocking().send(txRequest)
            val numBytesFromBumble = socketIs.read(buffer)
            Truth.assertThat(ByteString.copyFrom(buffer).substring(0, numBytesFromBumble))
                .isEqualTo(data)
        }
    }

    private fun createAndConnectSocket(
        isSecure: Boolean,
        server: ServerId
+7 −0
Original line number Diff line number Diff line
@@ -107,6 +107,13 @@ class RFCOMMService(RFCOMMServicer):

        return StopServerResponse()

    @utils.rpc
    async def Send(self, request: TxRequest, context: grpc.ServicerContext) -> TxResponse:
        logging.info(f"Send")
        dlc = self.connections[request.connection.id]
        dlc.write(request.data)
        return TxResponse()

    @utils.rpc
    async def Receive(self, request: RxRequest, context: grpc.ServicerContext) -> RxResponse:
        logging.info(f"Receive")