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

Commit e20c4024 authored by Hyundo Moon's avatar Hyundo Moon Committed by Gerrit Code Review
Browse files

Merge "Add BluetoothPbapAuthenticatorTest"

parents beb13bfc 65434702
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import android.util.Log;

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

/**
 * BluetoothPbapAuthenticator is a used by BluetoothObexServer for obex
@@ -44,10 +45,10 @@ import com.android.bluetooth.obex.PasswordAuthentication;
public class BluetoothPbapAuthenticator implements Authenticator {
    private static final String TAG = "PbapAuthenticator";

    private boolean mChallenged;
    private boolean mAuthCancelled;
    private String mSessionKey;
    private PbapStateMachine mPbapStateMachine;
    @VisibleForTesting boolean mChallenged;
    @VisibleForTesting boolean mAuthCancelled;
    @VisibleForTesting String mSessionKey;
    @VisibleForTesting PbapStateMachine mPbapStateMachine;

    BluetoothPbapAuthenticator(final PbapStateMachine stateMachine) {
        mPbapStateMachine = stateMachine;
+4 −1
Original line number Diff line number Diff line
@@ -43,6 +43,8 @@ import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.obex.ResponseCodes;
import com.android.bluetooth.obex.ServerSession;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting.Visibility;
import com.android.internal.util.State;
import com.android.internal.util.StateMachine;

@@ -61,7 +63,8 @@ import java.io.IOException;
 *          CONNECTED   ----->  FINISHED
 *                (OBEX Server done)
 */
class PbapStateMachine extends StateMachine {
@VisibleForTesting(visibility = Visibility.PACKAGE)
public class PbapStateMachine extends StateMachine {
    private static final String TAG = "PbapStateMachine";
    private static final boolean DEBUG = true;
    private static final boolean VERBOSE = true;
+128 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 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.pbap;

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

import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;

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

import com.android.bluetooth.obex.PasswordAuthentication;

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

@MediumTest
@RunWith(AndroidJUnit4.class)
public class BluetoothPbapAuthenticatorTest {

    private BluetoothPbapAuthenticator mAuthenticator;

    @Mock
    PbapStateMachine mMockPbapStateMachine;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mAuthenticator = new BluetoothPbapAuthenticator(mMockPbapStateMachine);
    }

    @Test
    public void testConstructor() {
        assertThat(mAuthenticator.mChallenged).isFalse();
        assertThat(mAuthenticator.mAuthCancelled).isFalse();
        assertThat(mAuthenticator.mSessionKey).isNull();
        assertThat(mAuthenticator.mPbapStateMachine).isEqualTo(mMockPbapStateMachine);
    }

    @Test
    public void testSetChallenged() {
        mAuthenticator.setChallenged(true);
        assertThat(mAuthenticator.mChallenged).isTrue();

        mAuthenticator.setChallenged(false);
        assertThat(mAuthenticator.mChallenged).isFalse();
    }

    @Test
    public void testSetCancelled() {
        mAuthenticator.setCancelled(true);
        assertThat(mAuthenticator.mAuthCancelled).isTrue();

        mAuthenticator.setCancelled(false);
        assertThat(mAuthenticator.mAuthCancelled).isFalse();
    }

    @Test
    public void testSetSessionKey() {
        final String sessionKey = "test_session_key";

        mAuthenticator.setSessionKey(sessionKey);
        assertThat(mAuthenticator.mSessionKey).isEqualTo(sessionKey);

        mAuthenticator.setSessionKey(null);
        assertThat(mAuthenticator.mSessionKey).isNull();
    }

    @Test
    public void testOnAuthenticationChallenge() {
        final String sessionKey = "test_session_key";
        doAnswer(invocation -> {
            mAuthenticator.setSessionKey(sessionKey);
            mAuthenticator.setChallenged(true);
            return null;
        }).when(mMockPbapStateMachine).sendMessage(PbapStateMachine.CREATE_NOTIFICATION);

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

        verify(mMockPbapStateMachine).sendMessage(PbapStateMachine.CREATE_NOTIFICATION);
        verify(mMockPbapStateMachine).sendMessageDelayed(PbapStateMachine.REMOVE_NOTIFICATION,
                BluetoothPbapService.USER_CONFIRM_TIMEOUT_VALUE);
        assertThat(passwordAuthentication.getPassword()).isEqualTo(sessionKey.getBytes());
    }

    @Test
    public void testOnAuthenticationChallenge_returnsNullWhenSessionKeyIsEmpty() {
        final String emptySessionKey = "";
        doAnswer(invocation -> {
            mAuthenticator.setSessionKey(emptySessionKey);
            mAuthenticator.setChallenged(true);
            return null;
        }).when(mMockPbapStateMachine).sendMessage(PbapStateMachine.CREATE_NOTIFICATION);

        // Note: onAuthenticationChallenge() does not use any arguments
        PasswordAuthentication passwordAuthentication = mAuthenticator.onAuthenticationChallenge(
                /*description=*/ null, /*isUserIdRequired=*/ false, /*isFullAccess=*/ false);
        assertThat(passwordAuthentication).isNull();
    }

    @Test
    public void testOnAuthenticationResponse() {
        byte[] userName = "test_user_name".getBytes();

        // This assertion should be fixed when the implementation changes.
        assertThat(mAuthenticator.onAuthenticationResponse(userName)).isNull();
    }
}