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

Commit 07760ab7 authored by Pomai Ahlo's avatar Pomai Ahlo
Browse files

RFCOMM Client BumbleBluetooth: Send

Add Send Data tests

Bug: 331415222
Test: atest BumbleBluetoothTests:android.bluetooth.RfcommTest
Flag: TEST_ONLY
Change-Id: I03707a5b1bc67d4b9b5768a17b4ae66c3d1a115a
parent d8db1278
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.android.compatibility.common.util.AdoptShellPermissionsRule
import com.google.common.truth.Truth
import com.google.protobuf.ByteString
import io.grpc.stub.StreamObserver
import java.time.Duration
import java.util.UUID
@@ -203,6 +204,46 @@ class RfcommTest {
        }
    }

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

            val (insecureSocket, connection) = createAndConnectSocket(isSecure = false, serverId)
            val data: ByteArray = "Test data for clientSendDataOverInsecureSocket".toByteArray()
            val socketOs = insecureSocket.outputStream

            socketOs.write(data)
            val rxResponse: RfcommProto.RxResponse =
                mBumble
                    .rfcommBlocking()
                    .withDeadlineAfter(GRPC_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS)
                    .receive(RfcommProto.RxRequest.newBuilder().setConnection(connection).build())
            Truth.assertThat(rxResponse.data).isEqualTo(ByteString.copyFrom(data))
        }
    }

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

            val (secureSocket, connection) = createAndConnectSocket(isSecure = true, serverId)
            val data: ByteArray = "Test data for clientSendDataOverSecureSocket".toByteArray()
            val socketOs = secureSocket.outputStream

            socketOs.write(data)
            val rxResponse: RfcommProto.RxResponse =
                mBumble
                    .rfcommBlocking()
                    .withDeadlineAfter(GRPC_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS)
                    .receive(RfcommProto.RxRequest.newBuilder().setConnection(connection).build())
            Truth.assertThat(rxResponse.data).isEqualTo(ByteString.copyFrom(data))
        }
    }

    private fun createAndConnectSocket(
        isSecure: Boolean,
        server: ServerId
+6 −0
Original line number Diff line number Diff line
@@ -106,3 +106,9 @@ class RFCOMMService(RFCOMMServicer):
        self.server_uuid = None

        return StopServerResponse()

    @utils.rpc
    async def Receive(self, request: RxRequest, context: grpc.ServicerContext) -> RxResponse:
        logging.info(f"Receive")
        received_data = await self.data_queue.get()
        return RxResponse(data=received_data)