Commit 60325524 authored by Sudheer Shanka's avatar Sudheer Shanka Committed by Feng Yu
Browse files

DO NOT MERGE: Cts: Add checks for testing CREATE_USERS permission.

Bug: 29189712
Change-Id: Icff148ee55e8339cbde030b547788d424509b9a3
parent 8099c628
......@@ -180,6 +180,7 @@ cts_host_libraries := \
CtsHostJank \
CtsHostUi \
CtsMonkeyTestCases \
CtsMultiUserHostTestCases \
CtsThemeHostTestCases \
CtsSecurityHostTestCases \
CtsTrustedVoiceHostTestCases \
......
# 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-prebuilt
LOCAL_CTS_TEST_PACKAGE := android.host.multiuser
# tag this module as a cts test artifact
LOCAL_COMPATIBILITY_SUITE := cts
include $(BUILD_CTS_HOST_JAVA_LIBRARY)
# Build the test APKs using their own makefiles
include $(call all-makefiles-under,$(LOCAL_PATH))
/*
* 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 {
// From the UserInfo class
private static final int FLAG_PRIMARY = 0x00000001;
protected IBuildInfo mBuildInfo;
protected int mPrimaryUserId;
@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.
mPrimaryUserId = getPrimaryUserId();
switchUser(mPrimaryUserId);
}
protected void switchUser(int userId) throws Exception {
String command = "am switch-user " + userId;
CLog.d("Starting command " + command);
String commandOutput = getDevice().executeShellCommand(command);
CLog.d("Output for command " + command + ": " + commandOutput);
}
public Integer getPrimaryUserId() throws DeviceNotAvailableException {
ArrayList<String[]> users = tokenizeListUsers();
if (users == null) {
return null;
}
for (String[] user : users) {
int flag = Integer.parseInt(user[3], 16);
if ((flag & FLAG_PRIMARY) != 0) {
return Integer.parseInt(user[1]);
}
}
return null;
}
/**
* Tokenizes the output of 'pm list users'.
* The returned tokens for each user have the form: {"\tUserInfo", Integer.toString(id), name,
* Integer.toHexString(flag), "[running]"}; (the last one being optional)
* @return a list of arrays of strings, each element of the list representing the tokens
* for a user, or {@code null} if there was an error while tokenizing the adb command output.
*/
private ArrayList<String[]> tokenizeListUsers() throws DeviceNotAvailableException {
String command = "pm list users";
String commandOutput = getDevice().executeShellCommand(command);
// Extract the id of all existing users.
String[] lines = commandOutput.split("\\r?\\n");
if (lines.length < 1) {
CLog.e("%s should contain at least one line", commandOutput);
return null;
}
if (!lines[0].equals("Users:")) {
CLog.e("%s in not a valid output for 'pm list users'", commandOutput);
return null;
}
ArrayList<String[]> users = new ArrayList<String[]>(lines.length - 1);
for (int i = 1; i < lines.length; i++) {
// Individual user is printed out like this:
// \tUserInfo{$id$:$name$:$Integer.toHexString(flags)$} [running]
String[] tokens = lines[i].split("\\{|\\}|:");
if (tokens.length != 4 && tokens.length != 5) {
CLog.e("%s doesn't contain 4 or 5 tokens", lines[i]);
return null;
}
users.add(tokens);
}
return users;
}
}
/*
* 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;
public class CreateUsersPermissionTest extends BaseMultiUserTest {
public void testCantSetUserRestriction() throws Exception {
final String setRestriction = "pm set-user-restriction no_unmute_microphone ";
final String output = getDevice().executeShellCommand(setRestriction + "1");
assertTrue("Trying to set user restriction should fail with SecurityException",
output.startsWith("Error") && output.contains("SecurityException"));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment