Merge "Preload: Add a simple test for the state of classes"

am: fe1c4420c5

Change-Id: I8df9c6ead09679d879bac0edda5fd0820f0adf47
This commit is contained in:
Andreas Gampe
2019-04-12 10:54:00 -07:00
committed by android-build-merger
10 changed files with 319 additions and 0 deletions

7
config/TEST_MAPPING Normal file
View File

@@ -0,0 +1,7 @@
{
"presubmit": [
{
"name": "PreloadCheck"
}
]
}

View File

@@ -0,0 +1,21 @@
// Copyright (C) 2019 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.
java_test_host {
name: "PreloadCheck",
srcs: ["src/**/*.java"],
libs: ["tradefed"],
test_suites: ["general-tests"],
required: ["preload-check-device"],
}

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2019 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 PreloadCheck">
<target_preparer class="com.android.compatibility.common.tradefed.targetprep.FilePusher">
<option name="cleanup" value="true" />
<option name="push" value="preload-check-device.jar->/data/local/tmp/preload-check-device.jar" />
</target_preparer>
<test class="com.android.tradefed.testtype.HostTest" >
<option name="class" value="com.android.preload.check.PreloadCheck" />
</test>
</configuration>

View File

@@ -0,0 +1,7 @@
{
"presubmit": [
{
"name": "PreloadCheck"
}
]
}

View File

@@ -0,0 +1,27 @@
// Copyright (C) 2019 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.
java_test_helper_library {
name: "preload-check-device",
host_supported: false,
device_supported: true,
compile_dex: true,
sdk_version: "current",
srcs: ["src/**/*.java"],
test_suites: ["general-tests"],
dex_preopt: {
enabled: false,
},
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright (C) 2019 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.preload.check;
public class Initialized {
public static void main(String[] args) throws Exception {
Util.assertInitialized(args[0], null);
System.out.println("OK");
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (C) 2019 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.preload.check;
public class IntegrityCheck {
public static void main(String[] args) throws Exception {
ClassLoader loader = IntegrityCheck.class.getClassLoader();
Util.assertNotInitialized("com.android.preload.check.IntegrityCheck$StatusHelper", loader);
Class<?> klass = Class.forName("com.android.preload.check.IntegrityCheck$StatusHelper",
/* initialize */ true, loader);
Util.assertInitialized("com.android.preload.check.IntegrityCheck$StatusHelper", loader);
System.out.println("OK");
}
private static class StatusHelper {
private final static Object defer = new Object();
}
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright (C) 2019 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.preload.check;
public class NotInitialized {
public static void main(String[] args) throws Exception {
Util.assertNotInitialized(args[0], null);
System.out.println("OK");
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright (C) 2019 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.preload.check;
import java.lang.reflect.Field;
public class Util {
private static Field statusField;
static {
try {
Class<?> klass = Class.class;
statusField = klass.getDeclaredField("status");
statusField.setAccessible(true);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
public static boolean isInitialized(Class<?> klass) throws Exception {
Object val = statusField.get(klass);
if (val == null || !(val instanceof Integer)) {
throw new IllegalStateException(String.valueOf(val));
}
int intVal = (int)val;
intVal = (intVal >> (32-4)) & 0xf;
return intVal >= 14;
}
public static void assertTrue(boolean val, String msg) {
if (!val) {
throw new RuntimeException(msg);
}
}
public static void assertInitializedState(String className, boolean expected,
ClassLoader loader) {
boolean initialized;
try {
Class<?> klass = Class.forName(className, /* initialize */ false, loader);
initialized = isInitialized(klass);
} catch (Throwable t) {
throw new RuntimeException(t);
}
assertTrue(expected == initialized, className);
}
public static void assertNotInitialized(String className, ClassLoader loader) {
assertInitializedState(className, false, loader);
}
public static void assertInitialized(String className, ClassLoader loader) {
assertInitializedState(className, true, loader);
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright (C) 2019 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.preload.check;
import static org.junit.Assert.assertEquals;
import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
import com.android.tradefed.testtype.IDeviceTest;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(DeviceJUnit4ClassRunner.class)
public class PreloadCheck implements IDeviceTest {
private ITestDevice mTestDevice;
private static final String TEST_CLASSPATH = "/data/local/tmp/preload-check-device.jar";
@Override
public void setDevice(ITestDevice testDevice) {
mTestDevice = testDevice;
}
@Override
public ITestDevice getDevice() {
return mTestDevice;
}
/**
* Test that checks work as expected.
*/
@Test
public void testStatus() throws Exception {
run("com.android.preload.check.IntegrityCheck");
}
/**
* b/130206915.
*/
@Test
public void testAsyncTask() throws Exception {
run("com.android.preload.check.NotInitialized", "android.os.AsyncTask");
}
/**
* Just a check for something we expect to see initialized.
*/
@Test
public void testAnimator() throws Exception {
run("com.android.preload.check.Initialized", "android.animation.Animator");
}
private void run(String cmd, String... args) throws Exception {
StringBuilder sb = new StringBuilder();
sb.append("app_process ")
.append("-cp ").append(TEST_CLASSPATH)
.append(" /system/bin ")
.append(cmd);
for (String arg : args) {
sb.append(' ').append(arg);
}
String res = mTestDevice.executeShellCommand(sb.toString());
assertEquals(sb.toString(), "OK", res.trim());
}
}