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

Commit 69177fb0 authored by Hyundo Moon's avatar Hyundo Moon Committed by Automerger Merge Worker
Browse files

Merge "Add BluetoothPbapObexAuthenticatorTest/BluetoothPbapRequestTest" am: b9753a90

parents ef378059 b9753a90
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.bluetooth.pbapclient;
import android.os.Handler;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.obex.Authenticator;
import com.android.obex.PasswordAuthentication;

@@ -28,15 +29,14 @@ import java.util.Arrays;
 * with PSE devices prior to PBAP 1.2. With profiles prior to 1.2 the actual initiation of
 * authentication is implementation defined.
 */


class BluetoothPbapObexAuthenticator implements Authenticator {

    private static final String TAG = "BtPbapObexAuthenticator";
    private static final boolean DBG = Utils.DBG;

    //Default session key for legacy devices is 0000
    private String mSessionKey = "0000";
    @VisibleForTesting
    String mSessionKey = "0000";

    private final Handler mCallback;

@@ -69,5 +69,4 @@ class BluetoothPbapObexAuthenticator implements Authenticator {
        /* required only in case PCE challenges PSE which we don't do now */
        return null;
    }

}
+0 −98
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bluetooth.pbapclient;

import android.bluetooth.BluetoothSocket;

import com.android.obex.ObexTransport;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

class BluetoothPbapObexTransport implements ObexTransport {

    private BluetoothSocket mSocket = null;

    BluetoothPbapObexTransport(BluetoothSocket rfs) {
        super();
        mSocket = rfs;
    }

    @Override
    public void close() throws IOException {
        mSocket.close();
    }

    @Override
    public DataInputStream openDataInputStream() throws IOException {
        return new DataInputStream(openInputStream());
    }

    @Override
    public DataOutputStream openDataOutputStream() throws IOException {
        return new DataOutputStream(openOutputStream());
    }

    @Override
    public InputStream openInputStream() throws IOException {
        return mSocket.getInputStream();
    }

    @Override
    public OutputStream openOutputStream() throws IOException {
        return mSocket.getOutputStream();
    }

    @Override
    public void connect() throws IOException {
    }

    @Override
    public void create() throws IOException {
    }

    @Override
    public void disconnect() throws IOException {
    }

    @Override
    public void listen() throws IOException {
    }

    public boolean isConnected() throws IOException {
        // return true;
        return mSocket.isConnected();
    }

    @Override
    public int getMaxTransmitPacketSize() {
        return -1;
    }

    @Override
    public int getMaxReceivePacketSize() {
        return -1;
    }

    @Override
    public boolean isSrmSupported() {
        return false;
    }
}
+63 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bluetooth.pbapclient;

import static com.google.common.truth.Truth.assertThat;

import android.os.Handler;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.obex.PasswordAuthentication;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class BluetoothPbapObexAuthenticatorTest {

    private BluetoothPbapObexAuthenticator mAuthenticator;

    @Mock
    Handler mHandler;

    @Before
    public void setUp() throws Exception {
        mAuthenticator = new BluetoothPbapObexAuthenticator(mHandler);
    }

    @Test
    public void onAuthenticationChallenge() {
        // Note: onAuthenticationChallenge() does not use any arguments
        PasswordAuthentication passwordAuthentication = mAuthenticator.onAuthenticationChallenge(
                /*description=*/ null, /*isUserIdRequired=*/ false, /*isFullAccess=*/ false);

        assertThat(passwordAuthentication.getPassword())
                .isEqualTo(mAuthenticator.mSessionKey.getBytes());
    }

    @Test
    public void onAuthenticationResponse() {
        byte[] userName = new byte[] {};
        // Note: onAuthenticationResponse() does not use any arguments
        assertThat(mAuthenticator.onAuthenticationResponse(userName)).isNull();
    }
}
+91 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bluetooth.pbapclient;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import static org.mockito.Mockito.mock;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.obex.ClientSession;
import com.android.obex.HeaderSet;
import com.android.obex.ObexTransport;
import com.android.obex.ResponseCodes;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.InputStream;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class BluetoothPbapRequestTest {

    private BluetoothPbapRequest mRequest = new BluetoothPbapRequest() {};

    @Mock
    private ObexTransport mObexTransport;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mRequest = new BluetoothPbapRequest() {};
    }

    @Test
    public void isSuccess_true() {
        mRequest.mResponseCode = ResponseCodes.OBEX_HTTP_OK;

        assertThat(mRequest.isSuccess()).isTrue();
    }

    @Test
    public void isSuccess_false() {
        mRequest.mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;

        assertThat(mRequest.isSuccess()).isFalse();
    }

    @Test
    public void execute_afterAbort() throws Exception {
        mRequest.abort();
        ClientSession session = new ClientSession(mObexTransport);
        mRequest.execute(session);

        assertThat(mRequest.mResponseCode).isEqualTo(ResponseCodes.OBEX_HTTP_INTERNAL_ERROR);
    }

    // TODO: Add execute_success test case.

    @Test
    public void emptyMethods() {
        try {
            mRequest.readResponse(mock(InputStream.class));
            mRequest.readResponseHeaders(new HeaderSet());
            mRequest.checkResponseCode(ResponseCodes.OBEX_HTTP_OK);

        } catch (Exception e) {
            assertWithMessage("Exception should not happen.").fail();
        }
    }
}