UserProcessCondition Introduction.

This changelist introduces UserProcessCondition, a simple condition
that is true only when the process user handle identifier matches
the current user.

This can be used to limit execution of code to only the process ran by
the currently running user.

Test: atest UserProcessConditionTest
Bug: 261420432
Change-Id: I1efabf11b52d09833dc8a59a94b031bed682cab9
This commit is contained in:
Bryce Lee
2023-02-03 11:43:11 -08:00
parent 099a3b8d20
commit 746c301e55
3 changed files with 180 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
/*
* Copyright (C) 2023 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.systemui.process;
/**
* A simple wrapper that provides access to process-related details. This facilitates testing by
* providing a mockable target around these details.
*/
public class ProcessWrapper {
public int getUserHandleIdentifier() {
return android.os.Process.myUserHandle().getIdentifier();
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (C) 2023 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.systemui.process.condition;
import com.android.systemui.process.ProcessWrapper;
import com.android.systemui.settings.UserTracker;
import com.android.systemui.shared.condition.Condition;
import javax.inject.Inject;
/**
* {@link UserProcessCondition} provides a signal when the process handle belongs to the current
* user.
*/
public class UserProcessCondition extends Condition {
private final ProcessWrapper mProcessWrapper;
private final UserTracker mUserTracker;
@Inject
public UserProcessCondition(ProcessWrapper processWrapper, UserTracker userTracker) {
mProcessWrapper = processWrapper;
mUserTracker = userTracker;
}
@Override
protected void start() {
updateCondition(mUserTracker.getUserId()
== mProcessWrapper.getUserHandleIdentifier());
}
@Override
protected void stop() {
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright (C) 2023 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.systemui.process.condition;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import androidx.test.filters.SmallTest;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.process.ProcessWrapper;
import com.android.systemui.settings.UserTracker;
import com.android.systemui.shared.condition.Condition;
import com.android.systemui.shared.condition.Monitor;
import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.time.FakeSystemClock;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidTestingRunner.class)
@TestableLooper.RunWithLooper
@SmallTest
public class UserProcessConditionTest extends SysuiTestCase {
@Mock
UserTracker mUserTracker;
@Mock
ProcessWrapper mProcessWrapper;
@Mock
Monitor.Callback mCallback;
private final FakeExecutor mExecutor = new FakeExecutor(new FakeSystemClock());
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
/**
* Verifies condition reports false when tracker reports a different user id than the
* identifier from the process handle.
*/
@Test
public void testConditionFailsWithDifferentIds() {
final Condition condition = new UserProcessCondition(mProcessWrapper, mUserTracker);
when(mProcessWrapper.getUserHandleIdentifier()).thenReturn(0);
when(mUserTracker.getUserId()).thenReturn(1);
final Monitor monitor = new Monitor(mExecutor);
monitor.addSubscription(new Monitor.Subscription.Builder(mCallback)
.addCondition(condition)
.build());
mExecutor.runAllReady();
verify(mCallback).onConditionsChanged(false);
}
/**
* Verifies condition reports false when tracker reports a different user id than the
* identifier from the process handle.
*/
@Test
public void testConditionSucceedsWithSameIds() {
final Condition condition = new UserProcessCondition(mProcessWrapper, mUserTracker);
when(mProcessWrapper.getUserHandleIdentifier()).thenReturn(0);
when(mUserTracker.getUserId()).thenReturn(0);
final Monitor monitor = new Monitor(mExecutor);
monitor.addSubscription(new Monitor.Subscription.Builder(mCallback)
.addCondition(condition)
.build());
mExecutor.runAllReady();
verify(mCallback).onConditionsChanged(true);
}
}