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

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

Merge "Add BluetoothShellCommandTest"

parents 06762053 4d280a02
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.os.Process;
import android.os.RemoteException;
import android.util.Log;

import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.utils.BasicShellCommandHandler;

import java.io.PrintWriter;
@@ -33,12 +34,14 @@ class BluetoothShellCommand extends BasicShellCommandHandler {
    private final BluetoothManagerService mManagerService;
    private final Context mContext;

    private final BluetoothCommand[] mBluetoothCommands = {
    @VisibleForTesting
    final BluetoothCommand[] mBluetoothCommands = {
        new Enable(),
        new Disable(),
    };

    private abstract class BluetoothCommand {
    @VisibleForTesting
    abstract class BluetoothCommand {
        abstract String getName();
        // require root permission by default, can be override in command implementation
        boolean isPrivileged() {
@@ -47,7 +50,8 @@ class BluetoothShellCommand extends BasicShellCommandHandler {
        abstract int exec(PrintWriter pw) throws RemoteException;
    }

    private class Enable extends BluetoothCommand {
    @VisibleForTesting
    class Enable extends BluetoothCommand {
        @Override
        String getName() {
            return "enable";
@@ -63,7 +67,8 @@ class BluetoothShellCommand extends BasicShellCommandHandler {
        }
    }

    private class Disable extends BluetoothCommand {
    @VisibleForTesting
    class Disable extends BluetoothCommand {
        @Override
        String getName() {
            return "disable";
+0 −0

File moved.

+0 −0

File moved.

+132 −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.server.bluetooth;

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

import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.os.Binder;
import android.os.RemoteException;

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

import com.android.server.bluetooth.BluetoothShellCommand.BluetoothCommand;

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

import java.io.FileDescriptor;
import java.io.PrintWriter;

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

    @Mock
    BluetoothManagerService mManagerService;

    @Mock
    Context mContext;

    BluetoothShellCommand mShellCommand;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mShellCommand = new BluetoothShellCommand(mManagerService, mContext);
        mShellCommand.init(
                mock(Binder.class), mock(FileDescriptor.class), mock(FileDescriptor.class),
                mock(FileDescriptor.class), new String[0], -1);
    }

    @Test
    public void enableCommand() throws Exception {
        BluetoothCommand enableCmd = mShellCommand.new Enable();

        assertThat(enableCmd.getName()).isEqualTo("enable");
        assertThat(enableCmd.isPrivileged()).isFalse();
        when(mManagerService.enable(any())).thenReturn(true);
        assertThat(enableCmd.exec(mock(PrintWriter.class))).isEqualTo(0);
    }

    @Test
    public void disableCommand() throws Exception {
        BluetoothCommand disableCmd = mShellCommand.new Disable();

        assertThat(disableCmd.getName()).isEqualTo("disable");
        assertThat(disableCmd.isPrivileged()).isFalse();
        when(mManagerService.disable(any(), anyBoolean())).thenReturn(true);
        assertThat(disableCmd.exec(mock(PrintWriter.class))).isEqualTo(0);
    }

    @Test
    public void onCommand_withNullString_callsOnHelp() {
        BluetoothShellCommand command = spy(mShellCommand);

        command.onCommand(null);

        verify(command).onHelp();
    }

    @Test
    public void onCommand_withEnableString_callsEnableCommand() throws Exception {
        BluetoothCommand enableCmd = spy(mShellCommand.new Enable());
        mShellCommand.mBluetoothCommands[0] = enableCmd;

        mShellCommand.onCommand("enable");

        verify(enableCmd).exec(any(PrintWriter.class));
    }

    @Test
    public void onCommand_withPrivilegedCommandName_throwsSecurityException() {
        final String privilegedCommandName = "test_privileged_cmd_name";
        BluetoothCommand privilegedCommand = mShellCommand.new BluetoothCommand() {
            @Override
            String getName() {
                return privilegedCommandName;
            }

            @Override
            boolean isPrivileged() {
                return true;
            }

            @Override
            int exec(PrintWriter pw) throws RemoteException {
                return 0;
            }
        };
        mShellCommand.mBluetoothCommands[0] = privilegedCommand;

        assertThrows(SecurityException.class,
                () -> mShellCommand.onCommand(privilegedCommandName));
    }

}