upload android base code part4

This commit is contained in:
August 2018-08-08 17:00:29 +08:00
parent b9e30e05b1
commit 78ea2404cd
23455 changed files with 5250148 additions and 0 deletions

View file

@ -0,0 +1,35 @@
# 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.
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := CtsMultiUserHostTestCases
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_JAVA_LIBRARIES := tools-common-prebuilt cts-tradefed tradefed
LOCAL_CTS_TEST_PACKAGE := android.host.multiuser
# tag this module as a cts test artifact
LOCAL_COMPATIBILITY_SUITE := cts vts general-tests
include $(BUILD_CTS_HOST_JAVA_LIBRARY)
# Build the test APKs using their own makefiles
include $(call all-makefiles-under,$(LOCAL_PATH))

View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<configuration description="Config for the CTS multiuser host tests">
<option name="config-descriptor:metadata" key="component" value="framework" />
<test class="com.android.compatibility.common.tradefed.testtype.JarHostTest" >
<option name="jar" value="CtsMultiUserHostTestCases.jar" />
<option name="runtime-hint" value="8m" />
</test>
</configuration>

View file

@ -0,0 +1,131 @@
/*
* 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 android.host.multiuser;
import com.android.tradefed.build.IBuildInfo;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.log.LogUtil.CLog;
import com.android.tradefed.testtype.DeviceTestCase;
import com.android.tradefed.testtype.IBuildReceiver;
import java.util.ArrayList;
/**
* Base class for multi user tests.
*/
public class BaseMultiUserTest extends DeviceTestCase implements IBuildReceiver {
protected static final int USER_SYSTEM = 0; // From the UserHandle class.
protected IBuildInfo mBuildInfo;
/** Whether multi-user is supported. */
protected boolean mSupportsMultiUser;
protected boolean mIsSplitSystemUser;
protected int mPrimaryUserId;
/** Users we shouldn't delete in the tests */
private ArrayList<Integer> mFixedUsers;
@Override
public void setBuild(IBuildInfo buildInfo) {
mBuildInfo = buildInfo;
}
@Override
protected void setUp() throws Exception {
super.setUp();
assertNotNull(mBuildInfo); // ensure build has been set before test is run.
mSupportsMultiUser = getDevice().getMaxNumberOfUsersSupported() > 1;
mIsSplitSystemUser = checkIfSplitSystemUser();
mPrimaryUserId = getDevice().getPrimaryUserId();
mFixedUsers = new ArrayList<>();
mFixedUsers.add(mPrimaryUserId);
if (mPrimaryUserId != USER_SYSTEM) {
mFixedUsers.add(USER_SYSTEM);
}
getDevice().switchUser(mPrimaryUserId);
removeTestUsers();
}
@Override
protected void tearDown() throws Exception {
if (getDevice().getCurrentUser() != mPrimaryUserId) {
CLog.w("User changed during test. Switching back to " + mPrimaryUserId);
getDevice().switchUser(mPrimaryUserId);
}
removeTestUsers();
super.tearDown();
}
protected int createRestrictedProfile(int userId)
throws DeviceNotAvailableException, IllegalStateException{
final String command = "pm create-user --profileOf " + userId + " --restricted "
+ "TestUser_" + System.currentTimeMillis();
CLog.d("Starting command: " + command);
final String output = getDevice().executeShellCommand(command);
CLog.d("Output for command " + command + ": " + output);
if (output.startsWith("Success")) {
try {
return Integer.parseInt(output.substring(output.lastIndexOf(" ")).trim());
} catch (NumberFormatException e) {
CLog.e("Failed to parse result: %s", output);
}
} else {
CLog.e("Failed to create restricted profile: %s", output);
}
throw new IllegalStateException();
}
/**
* @return the userid of the created user
*/
protected int createUser()
throws DeviceNotAvailableException, IllegalStateException {
final String command = "pm create-user "
+ "TestUser_" + System.currentTimeMillis();
CLog.d("Starting command: " + command);
final String output = getDevice().executeShellCommand(command);
CLog.d("Output for command " + command + ": " + output);
if (output.startsWith("Success")) {
try {
return Integer.parseInt(output.substring(output.lastIndexOf(" ")).trim());
} catch (NumberFormatException e) {
CLog.e("Failed to parse result: %s", output);
}
} else {
CLog.e("Failed to create user: %s", output);
}
throw new IllegalStateException();
}
private void removeTestUsers() throws Exception {
for (int userId : getDevice().listUsers()) {
if (!mFixedUsers.contains(userId)) {
getDevice().removeUser(userId);
}
}
}
private boolean checkIfSplitSystemUser() throws DeviceNotAvailableException {
final String commandOuput = getDevice().executeShellCommand(
"getprop ro.fw.system_user_split");
return "y".equals(commandOuput) || "yes".equals(commandOuput)
|| "1".equals(commandOuput) || "true".equals(commandOuput)
|| "on".equals(commandOuput);
}
}

View file

@ -0,0 +1,77 @@
/*
* Copyright (C) 2017 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 android.host.multiuser;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.log.LogUtil.CLog;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;
/**
* Test verifies that users can be created/switched to without error dialogs shown to the user
*/
public class CreateUsersNoAppCrashesTest extends BaseMultiUserTest {
private int mInitialUserId;
private static final long LOGCAT_POLL_INTERVAL_MS = 5000;
private static final long BOOT_COMPLETED_TIMEOUT_MS = 120000;
@Override
protected void setUp() throws Exception {
super.setUp();
mInitialUserId = getDevice().getCurrentUser();
}
public void testCanCreateGuestUser() throws Exception {
if (!mSupportsMultiUser) {
return;
}
int userId = getDevice().createUser(
"TestUser_" + System.currentTimeMillis() /* name */,
true /* guest */,
false /* ephemeral */);
getDevice().executeAdbCommand("logcat", "-c"); // Reset log
assertTrue("Couldn't switch to user " + userId, getDevice().switchUser(userId));
Set<String> appErrors = new LinkedHashSet<>();
assertTrue("Didn't receive BOOT_COMPLETED delivered notification. appErrors=" + appErrors,
waitForBootCompleted(appErrors, userId));
assertTrue("App error dialog(s) are present: " + appErrors, appErrors.isEmpty());
assertTrue("Couldn't switch to user " + userId, getDevice().switchUser(mInitialUserId));
}
private boolean waitForBootCompleted(Set<String> appErrors, int targetUserId)
throws DeviceNotAvailableException, InterruptedException {
long ti = System.currentTimeMillis();
while (System.currentTimeMillis() - ti < BOOT_COMPLETED_TIMEOUT_MS) {
String logs = getDevice().executeAdbCommand("logcat", "-v", "brief", "-d");
Scanner in = new Scanner(logs);
while (in.hasNextLine()) {
String line = in.nextLine();
if (line.contains("Showing crash dialog for package")) {
appErrors.add(line);
} else if (line.contains("Finished processing BOOT_COMPLETED for u" + targetUserId)) {
return true;
}
}
in.close();
Thread.sleep(LOGCAT_POLL_INTERVAL_MS);
}
return false;
}
}

View file

@ -0,0 +1,64 @@
/*
* 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 android.host.multiuser;
import static com.android.tradefed.log.LogUtil.CLog;
import com.android.ddmlib.Log;
public class CreateUsersPermissionTest extends BaseMultiUserTest {
public void testCanCreateGuestUser() throws Exception {
if (!mSupportsMultiUser) {
return;
}
getDevice().createUser(
"TestUser_" + System.currentTimeMillis() /* name */,
true /* guest */,
false /* ephemeral */);
}
public void testCanCreateEphemeralUser() throws Exception {
if (!mSupportsMultiUser || !mIsSplitSystemUser) {
return;
}
getDevice().createUser(
"TestUser_" + System.currentTimeMillis() /* name */,
false /* guest */,
true /* ephemeral */);
}
public void testCanCreateRestrictedUser() throws Exception {
if (!mSupportsMultiUser) {
return;
}
createRestrictedProfile(mPrimaryUserId);
}
public void testCantSetUserRestriction() throws Exception {
if (getDevice().isAdbRoot()) {
CLog.logAndDisplay(Log.LogLevel.WARN,
"Cannot test testCantSetUserRestriction on rooted devices");
return;
}
final String setRestriction = "pm set-user-restriction no_fun ";
final String output = getDevice().executeShellCommand(setRestriction + "1");
final boolean isErrorOutput = output.startsWith("Error")
&& output.contains("SecurityException");
assertTrue("Trying to set user restriction should fail with SecurityException. "
+ "command output: " + output, isErrorOutput);
}
}