1 /*
2  * Copyright (C) 2009 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 /*
18  * Native implementation for the InstanceNonce class. See the test code
19  * in JniInstanceTest for more info.
20  */
21 
22 #include <jni.h>
23 #include <nativehelper/JNIHelp.h>
24 
25 #include <stdbool.h>
26 #include <string.h>
27 
28 // public native void nop();
InstanceNonce_nop(JNIEnv * env,jobject this)29 static void InstanceNonce_nop(JNIEnv *env, jobject this) {
30     // This space intentionally left blank.
31 }
32 
33 // public native boolean returnBoolean();
InstanceNonce_returnBoolean(JNIEnv * env,jobject this)34 static jboolean InstanceNonce_returnBoolean(JNIEnv *env, jobject this) {
35     return (jboolean) false;
36 }
37 
38 // public native byte returnByte();
InstanceNonce_returnByte(JNIEnv * env,jobject this)39 static jbyte InstanceNonce_returnByte(JNIEnv *env, jobject this) {
40     return (jbyte) 123;
41 }
42 
43 // public native short returnShort();
InstanceNonce_returnShort(JNIEnv * env,jobject this)44 static jshort InstanceNonce_returnShort(JNIEnv *env, jobject this) {
45     return (jshort) -12345;
46 }
47 
48 // public native char returnChar();
InstanceNonce_returnChar(JNIEnv * env,jobject this)49 static jchar InstanceNonce_returnChar(JNIEnv *env, jobject this) {
50     return (jchar) 34567;
51 }
52 
53 // public native int returnInt();
InstanceNonce_returnInt(JNIEnv * env,jobject this)54 static jint InstanceNonce_returnInt(JNIEnv *env, jobject this) {
55     return 12345678;
56 }
57 
58 // public native long returnLong();
InstanceNonce_returnLong(JNIEnv * env,jobject this)59 static jlong InstanceNonce_returnLong(JNIEnv *env, jobject this) {
60     return (jlong) -1098765432109876543LL;
61 }
62 
63 // public native float returnFloat();
InstanceNonce_returnFloat(JNIEnv * env,jobject this)64 static jfloat InstanceNonce_returnFloat(JNIEnv *env, jobject this) {
65     return (jfloat) -98765.4321F;
66 }
67 
68 // public native double returnDouble();
InstanceNonce_returnDouble(JNIEnv * env,jobject this)69 static jdouble InstanceNonce_returnDouble(JNIEnv *env, jobject this) {
70     return 12345678.9;
71 }
72 
73 // public native Object returnNull();
InstanceNonce_returnNull(JNIEnv * env,jobject this)74 static jobject InstanceNonce_returnNull(JNIEnv *env, jobject this) {
75     return NULL;
76 }
77 
78 // public native String returnString();
InstanceNonce_returnString(JNIEnv * env,jobject this)79 static jstring InstanceNonce_returnString(JNIEnv *env, jobject this) {
80     return (*env)->NewStringUTF(env, "blort");
81 }
82 
83 // public native short[] returnShortArray();
InstanceNonce_returnShortArray(JNIEnv * env,jobject this)84 static jshortArray InstanceNonce_returnShortArray(JNIEnv *env, jobject this) {
85     static jshort contents[] = { 10, 20, 30 };
86 
87     jshortArray result = (*env)->NewShortArray(env, 3);
88 
89     if (result == NULL) {
90         return NULL;
91     }
92 
93     (*env)->SetShortArrayRegion(env, result, 0, 3, contents);
94     return result;
95 }
96 
97 // public String[] returnStringArray();
InstanceNonce_returnStringArray(JNIEnv * env,jobject this)98 static jobjectArray InstanceNonce_returnStringArray(JNIEnv *env,
99         jobject this) {
100     static int indices[] = { 0, 50, 99 };
101     static const char *contents[] = { "blort", "zorch", "fizmo" };
102 
103     jclass stringClass = (*env)->FindClass(env, "java/lang/String");
104 
105     if ((*env)->ExceptionOccurred(env) != NULL) {
106         return NULL;
107     }
108 
109     if (stringClass == NULL) {
110         jniThrowException(env, "java/lang/AssertionError",
111                 "class String not found");
112         return NULL;
113     }
114 
115     jobjectArray result = (*env)->NewObjectArray(env, 100, stringClass, NULL);
116 
117     if (result == NULL) {
118         return NULL;
119     }
120 
121     jsize i;
122     for (i = 0; i < 3; i++) {
123         jstring s = (*env)->NewStringUTF(env, contents[i]);
124 
125         if (s == NULL) {
126             return NULL;
127         }
128 
129         (*env)->SetObjectArrayElement(env, result, indices[i], s);
130 
131         if ((*env)->ExceptionOccurred(env) != NULL) {
132             return NULL;
133         }
134     }
135 
136     return result;
137 }
138 
139 // public native Class returnThisClass();
InstanceNonce_returnThis(JNIEnv * env,jobject this)140 static jobject InstanceNonce_returnThis(JNIEnv *env, jobject this) {
141     return this;
142 }
143 
144 // public native boolean takeBoolean(boolean v);
InstanceNonce_takeBoolean(JNIEnv * env,jobject this,jboolean v)145 static jboolean InstanceNonce_takeBoolean(JNIEnv *env, jobject this,
146         jboolean v) {
147     return v == false;
148 }
149 
150 // public native boolean takeByte(byte v);
InstanceNonce_takeByte(JNIEnv * env,jobject this,jbyte v)151 static jboolean InstanceNonce_takeByte(JNIEnv *env, jobject this, jbyte v) {
152     return v == -99;
153 }
154 
155 // public native boolean takeShort(short v);
InstanceNonce_takeShort(JNIEnv * env,jobject this,jshort v)156 static jboolean InstanceNonce_takeShort(JNIEnv *env, jobject this, jshort v) {
157     return v == 19991;
158 }
159 
160 // public native boolean takeChar(char v);
InstanceNonce_takeChar(JNIEnv * env,jobject this,jchar v)161 static jboolean InstanceNonce_takeChar(JNIEnv *env, jobject this, jchar v) {
162     return v == 999;
163 }
164 
165 // public native boolean takeInt(int v);
InstanceNonce_takeInt(JNIEnv * env,jobject this,jint v)166 static jboolean InstanceNonce_takeInt(JNIEnv *env, jobject this, jint v) {
167     return v == -999888777;
168 }
169 
170 // public native boolean takeLong(long v);
InstanceNonce_takeLong(JNIEnv * env,jobject this,jlong v)171 static jboolean InstanceNonce_takeLong(JNIEnv *env, jobject this, jlong v) {
172     return v == 999888777666555444LL;
173 }
174 
175 // public native boolean takeFloat(float v);
InstanceNonce_takeFloat(JNIEnv * env,jobject this,jfloat v)176 static jboolean InstanceNonce_takeFloat(JNIEnv *env, jobject this, jfloat v) {
177     return v == -9988.7766F;
178 }
179 
180 // public native boolean takeDouble(double v);
InstanceNonce_takeDouble(JNIEnv * env,jobject this,jdouble v)181 static jboolean InstanceNonce_takeDouble(JNIEnv *env, jobject this,
182         jdouble v) {
183     return v == 999888777.666555;
184 }
185 
186 // public native boolean takeNull(Object v);
InstanceNonce_takeNull(JNIEnv * env,jobject this,jobject v)187 static jboolean InstanceNonce_takeNull(JNIEnv *env, jobject this, jobject v) {
188     return v == NULL;
189 }
190 
191 // public native boolean takeString(String v);
InstanceNonce_takeString(JNIEnv * env,jobject this,jstring v)192 static jboolean InstanceNonce_takeString(JNIEnv *env, jobject this,
193         jstring v) {
194     if (v == NULL) {
195         return false;
196     }
197 
198     const char *utf = (*env)->GetStringUTFChars(env, v, NULL);
199     jboolean result = (strcmp("fuzzbot", utf) == 0);
200 
201     (*env)->ReleaseStringUTFChars(env, v, utf);
202     return result;
203 }
204 
205 // public native boolean takeThis(InstanceNonce v);
InstanceNonce_takeThis(JNIEnv * env,jobject this,jobject v)206 static jboolean InstanceNonce_takeThis(JNIEnv *env, jobject this, jobject v) {
207     return (*env)->IsSameObject(env, this, v);
208 }
209 
210 // public native boolean takeIntLong(int v1, long v2);
InstanceNonce_takeIntLong(JNIEnv * env,jobject this,jint v1,jlong v2)211 static jboolean InstanceNonce_takeIntLong(JNIEnv *env, jobject this,
212         jint v1, jlong v2) {
213     return (v1 == 914) && (v2 == 9140914091409140914LL);
214 }
215 
216 // public native boolean takeLongInt(long v1, int v2);
InstanceNonce_takeLongInt(JNIEnv * env,jobject this,jlong v1,jint v2)217 static jboolean InstanceNonce_takeLongInt(JNIEnv *env, jobject this,
218         jlong v1, jint v2) {
219     return (v1 == -4321LL) && (v2 == 12341234);
220 }
221 
222 // public native boolean takeOneOfEach(boolean v0, byte v1, short v2,
223 //         char v3, int v4, long v5, String v6, float v7, double v8,
224 //         int[] v9);
InstanceNonce_takeOneOfEach(JNIEnv * env,jobject this,jboolean v0,jbyte v1,jshort v2,jchar v3,jint v4,jlong v5,jstring v6,jfloat v7,jdouble v8,jintArray v9)225 static jboolean InstanceNonce_takeOneOfEach(JNIEnv *env, jobject this,
226         jboolean v0, jbyte v1, jshort v2, jchar v3, jint v4, jlong v5,
227         jstring v6, jfloat v7, jdouble v8, jintArray v9) {
228     jsize length;
229     jboolean result;
230 
231     if ((v0 != false) || (v1 != 1) || (v2 != 2) || (v3 != 3) ||
232             (v4 != 4) || (v5 != 5) || (v7 != 7.0f) || (v8 != 8.0)) {
233         return false;
234     }
235 
236     length = (*env)->GetStringUTFLength(env, v6);
237 
238     if (length != 3) {
239         jniThrowException(env, "java/lang/AssertionError",
240                 "bad string length");
241         return false;
242     }
243 
244     const char *utf = (*env)->GetStringUTFChars(env, v6, NULL);
245     result = (strncmp("six", utf, 3) == 0);
246 
247     (*env)->ReleaseStringUTFChars(env, v6, utf);
248 
249     if (! result) {
250         return false;
251     }
252 
253     length = (*env)->GetArrayLength(env, v9);
254     if (length != 2) {
255         jniThrowException(env, "java/lang/AssertionError",
256                 "bad array length");
257         return false;
258     }
259 
260     jint *elements = (*env)->GetIntArrayElements(env, v9, NULL);
261     result = (elements[0] == 9) && (elements[1] == 10);
262     (*env)->ReleaseIntArrayElements(env, v9, elements, JNI_ABORT);
263 
264     return result;
265 }
266 
267 // public native boolean takeCoolHandLuke(
268 //         int v1, int v2, int v3, int v4,
269 //         int v5, int v6, int v7, int v8, int v9,
270 //         int v10, int v11, int v12, int v13, int v14,
271 //         int v15, int v16, int v17, int v18, int v19,
272 //         int v20, int v21, int v22, int v23, int v24,
273 //         int v25, int v26, int v27, int v28, int v29,
274 //         int v30, int v31, int v32, int v33, int v34,
275 //         int v35, int v36, int v37, int v38, int v39,
276 //         int v40, int v41, int v42, int v43, int v44,
277 //         int v45, int v46, int v47, int v48, int v49,
278 //         int v50);
InstanceNonce_takeCoolHandLuke(JNIEnv * env,jobject this,jint v1,jint v2,jint v3,jint v4,jint v5,jint v6,jint v7,jint v8,jint v9,jint v10,jint v11,jint v12,jint v13,jint v14,jint v15,jint v16,jint v17,jint v18,jint v19,jint v20,jint v21,jint v22,jint v23,jint v24,jint v25,jint v26,jint v27,jint v28,jint v29,jint v30,jint v31,jint v32,jint v33,jint v34,jint v35,jint v36,jint v37,jint v38,jint v39,jint v40,jint v41,jint v42,jint v43,jint v44,jint v45,jint v46,jint v47,jint v48,jint v49,jint v50)279 static jboolean InstanceNonce_takeCoolHandLuke(JNIEnv *env, jobject this,
280         jint v1, jint v2, jint v3, jint v4,
281         jint v5, jint v6, jint v7, jint v8, jint v9,
282         jint v10, jint v11, jint v12, jint v13, jint v14,
283         jint v15, jint v16, jint v17, jint v18, jint v19,
284         jint v20, jint v21, jint v22, jint v23, jint v24,
285         jint v25, jint v26, jint v27, jint v28, jint v29,
286         jint v30, jint v31, jint v32, jint v33, jint v34,
287         jint v35, jint v36, jint v37, jint v38, jint v39,
288         jint v40, jint v41, jint v42, jint v43, jint v44,
289         jint v45, jint v46, jint v47, jint v48, jint v49,
290         jint v50) {
291     return (v1 == 1) && (v2 == 2) && (v3 == 3) &&
292         (v4 == 4) && (v5 == 5) && (v6 == 6) && (v7 == 7) &&
293         (v8 == 8) && (v9 == 9) &&
294         (v10 == 10) && (v11 == 11) && (v12 == 12) && (v13 == 13) &&
295         (v14 == 14) && (v15 == 15) && (v16 == 16) && (v17 == 17) &&
296         (v18 == 18) && (v19 == 19) &&
297         (v20 == 20) && (v21 == 21) && (v22 == 22) && (v23 == 23) &&
298         (v24 == 24) && (v25 == 25) && (v26 == 26) && (v27 == 27) &&
299         (v28 == 28) && (v29 == 29) &&
300         (v30 == 30) && (v31 == 31) && (v32 == 32) && (v33 == 33) &&
301         (v34 == 34) && (v35 == 35) && (v36 == 36) && (v37 == 37) &&
302         (v38 == 38) && (v39 == 39) &&
303         (v40 == 40) && (v41 == 41) && (v42 == 42) && (v43 == 43) &&
304         (v44 == 44) && (v45 == 45) && (v46 == 46) && (v47 == 47) &&
305         (v48 == 48) && (v49 == 49) &&
306         (v50 == 50);
307 }
308 
309 static JNINativeMethod methods[] = {
310     // name, signature, function
311     { "nop",               "()V", InstanceNonce_nop },
312     { "returnBoolean",     "()Z", InstanceNonce_returnBoolean },
313     { "returnByte",        "()B", InstanceNonce_returnByte },
314     { "returnShort",       "()S", InstanceNonce_returnShort },
315     { "returnChar",        "()C", InstanceNonce_returnChar },
316     { "returnInt",         "()I", InstanceNonce_returnInt },
317     { "returnLong",        "()J", InstanceNonce_returnLong },
318     { "returnFloat",       "()F", InstanceNonce_returnFloat },
319     { "returnDouble",      "()D", InstanceNonce_returnDouble },
320     { "returnNull",        "()Ljava/lang/Object;", InstanceNonce_returnNull },
321     { "returnString",      "()Ljava/lang/String;",
322       InstanceNonce_returnString },
323     { "returnShortArray",  "()[S", InstanceNonce_returnShortArray },
324     { "returnStringArray", "()[Ljava/lang/String;",
325       InstanceNonce_returnStringArray },
326     { "returnThis",        "()Landroid/jni/cts/InstanceNonce;",
327       InstanceNonce_returnThis },
328     { "takeBoolean",       "(Z)Z", InstanceNonce_takeBoolean },
329     { "takeByte",          "(B)Z", InstanceNonce_takeByte },
330     { "takeShort",         "(S)Z", InstanceNonce_takeShort },
331     { "takeChar",          "(C)Z", InstanceNonce_takeChar },
332     { "takeInt",           "(I)Z", InstanceNonce_takeInt },
333     { "takeLong",          "(J)Z", InstanceNonce_takeLong },
334     { "takeFloat",         "(F)Z", InstanceNonce_takeFloat },
335     { "takeDouble",        "(D)Z", InstanceNonce_takeDouble },
336     { "takeNull",          "(Ljava/lang/Object;)Z", InstanceNonce_takeNull },
337     { "takeString",        "(Ljava/lang/String;)Z", InstanceNonce_takeString },
338     { "takeThis",          "(Landroid/jni/cts/InstanceNonce;)Z",
339       InstanceNonce_takeThis },
340     { "takeIntLong",       "(IJ)Z", InstanceNonce_takeIntLong },
341     { "takeLongInt",       "(JI)Z", InstanceNonce_takeLongInt },
342     { "takeOneOfEach",     "(ZBSCIJLjava/lang/String;FD[I)Z",
343       InstanceNonce_takeOneOfEach },
344     { "takeCoolHandLuke",
345       "(IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII)Z",
346       InstanceNonce_takeCoolHandLuke },
347 };
348 
register_InstanceNonce(JNIEnv * env)349 int register_InstanceNonce(JNIEnv *env) {
350     return jniRegisterNativeMethods(
351             env, "android/jni/cts/InstanceNonce",
352             methods, sizeof(methods) / sizeof(JNINativeMethod));
353 }
354