1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import java.lang.reflect.Method; 18 import java.lang.System; 19 20 // This is named Main as it is a copy of JniTest, so that we can re-use the native implementations 21 // from libarttest. 22 class Main { main(String[] args)23 public static void main(String[] args) { 24 testFindClassOnAttachedNativeThread(); 25 testFindFieldOnAttachedNativeThread(); 26 testCallStaticVoidMethodOnSubClass(); 27 testGetMirandaMethod(); 28 testZeroLengthByteBuffers(); 29 testByteMethod(); 30 testShortMethod(); 31 testBooleanMethod(); 32 testCharMethod(); 33 testEnvironment(); 34 testNewStringObject(); 35 testSignalHandler(); 36 } 37 testFindClassOnAttachedNativeThread()38 public static native void testFindClassOnAttachedNativeThread(); 39 40 public static boolean testFindFieldOnAttachedNativeThreadField; 41 testFindFieldOnAttachedNativeThread()42 public static void testFindFieldOnAttachedNativeThread() { 43 testFindFieldOnAttachedNativeThreadNative(); 44 if (!testFindFieldOnAttachedNativeThreadField) { 45 throw new AssertionError(); 46 } 47 } 48 testFindFieldOnAttachedNativeThreadNative()49 private static native void testFindFieldOnAttachedNativeThreadNative(); 50 testCallStaticVoidMethodOnSubClass()51 private static void testCallStaticVoidMethodOnSubClass() { 52 testCallStaticVoidMethodOnSubClassNative(); 53 if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) { 54 throw new AssertionError(); 55 } 56 } 57 testCallStaticVoidMethodOnSubClassNative()58 private static native void testCallStaticVoidMethodOnSubClassNative(); 59 60 private static class testCallStaticVoidMethodOnSubClass_SuperClass { 61 private static boolean executed = false; execute()62 private static void execute() { 63 executed = true; 64 } 65 } 66 67 private static class testCallStaticVoidMethodOnSubClass_SubClass 68 extends testCallStaticVoidMethodOnSubClass_SuperClass { 69 } 70 testGetMirandaMethodNative()71 private static native Method testGetMirandaMethodNative(); 72 testGetMirandaMethod()73 private static void testGetMirandaMethod() { 74 Method m = testGetMirandaMethodNative(); 75 if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) { 76 throw new AssertionError(); 77 } 78 } 79 testZeroLengthByteBuffers()80 private static native void testZeroLengthByteBuffers(); 81 82 private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface { inAbstract()83 public boolean inAbstract() { 84 return true; 85 } 86 } 87 88 private static interface testGetMirandaMethod_MirandaInterface { inInterface()89 public boolean inInterface(); 90 } 91 92 // Test sign-extension for values < 32b 93 byteMethod(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8, byte b9, byte b10)94 native static byte byteMethod(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, 95 byte b8, byte b9, byte b10); 96 testByteMethod()97 public static void testByteMethod() { 98 byte returns[] = { 0, 1, 2, 127, -1, -2, -128 }; 99 for (int i = 0; i < returns.length; i++) { 100 byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6, 101 (byte)(-7), (byte)8, (byte)(-9), (byte)10); 102 if (returns[i] != result) { 103 System.out.println("Run " + i + " with " + returns[i] + " vs " + result); 104 throw new AssertionError(); 105 } 106 } 107 } 108 shortMethod(short s1, short s2, short s3, short s4, short s5, short s6, short s7, short s8, short s9, short s10)109 native static short shortMethod(short s1, short s2, short s3, short s4, short s5, short s6, short s7, 110 short s8, short s9, short s10); 111 testShortMethod()112 private static void testShortMethod() { 113 short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 }; 114 for (int i = 0; i < returns.length; i++) { 115 short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6, 116 (short)(-7), (short)8, (short)(-9), (short)10); 117 if (returns[i] != result) { 118 System.out.println("Run " + i + " with " + returns[i] + " vs " + result); 119 throw new AssertionError(); 120 } 121 } 122 } 123 124 // Test zero-extension for values < 32b 125 booleanMethod(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7, boolean b8, boolean b9, boolean b10)126 native static boolean booleanMethod(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7, 127 boolean b8, boolean b9, boolean b10); 128 testBooleanMethod()129 public static void testBooleanMethod() { 130 if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) { 131 throw new AssertionError(); 132 } 133 134 if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) { 135 throw new AssertionError(); 136 } 137 } 138 charMethod(char c1, char c2, char c3, char c4, char c5, char c6, char c7, char c8, char c9, char c10)139 native static char charMethod(char c1, char c2, char c3, char c4, char c5, char c6, char c7, 140 char c8, char c9, char c10); 141 testCharMethod()142 private static void testCharMethod() { 143 char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000, 144 (char)34000 }; 145 for (int i = 0; i < returns.length; i++) { 146 char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345, 147 (char)3456); 148 if (returns[i] != result) { 149 System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result); 150 throw new AssertionError(); 151 } 152 } 153 } 154 testEnvironment()155 private static void testEnvironment() { 156 String osArch = System.getProperty("os.arch"); 157 if (!"os.arch".equals(osArch)) { 158 throw new AssertionError("unexpected value for os.arch: " + osArch); 159 } 160 // TODO: improve the build script to get these running as well. 161 // if (!"cpu_abi".equals(Build.CPU_ABI)) { 162 // throw new AssertionError("unexpected value for cpu_abi"); 163 // } 164 // if (!"cpu_abi2".equals(Build.CPU_ABI2)) { 165 // throw new AssertionError("unexpected value for cpu_abi2"); 166 // } 167 // String[] expectedSupportedAbis = {"supported1", "supported2", "supported3"}; 168 // if (Arrays.equals(expectedSupportedAbis, Build.SUPPORTED_ABIS)) { 169 // throw new AssertionError("unexpected value for supported_abis"); 170 // } 171 } 172 testNewStringObject()173 private static native void testNewStringObject(); 174 175 // Test v2 special signal handlers. This uses the native code from 004-SignalTest to cause 176 // a non-managed segfault. testSignalHandler()177 private static void testSignalHandler() { 178 // This uses code from 004-SignalTest. 179 int x = testSignal(); 180 if (x != 1234) { 181 throw new AssertionError(); 182 } 183 } 184 testSignal()185 private static native int testSignal(); 186 } 187 188 public class NativeBridgeMain { main(String[] args)189 static public void main(String[] args) throws Exception { 190 System.out.println("Ready for native bridge tests."); 191 192 System.loadLibrary("arttest"); 193 194 Main.main(null); 195 } 196 } 197