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 #include "helper.h"
18
19 #include <android/log.h>
20
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #define LOG_TAG "cts"
26
27 /* See helper.h for docs. */
failure(const char * format,...)28 char *failure(const char *format, ...) {
29 va_list args;
30 char *result;
31
32 va_start(args, format);
33 __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, format, args);
34 va_end(args);
35
36 va_start(args, format);
37 int count = vasprintf(&result, format, args);
38 va_end(args);
39
40 if (count < 0) {
41 return NULL;
42 }
43
44 return result;
45 }
46
47 /* See helper.h for docs. */
runJniTests(JNIEnv * env,...)48 char *runJniTests(JNIEnv *env, ...) {
49 va_list args;
50 char *result = NULL;
51
52 va_start(args, env);
53
54 for (;;) {
55 const char *name = va_arg(args, const char *);
56 if (name == NULL) {
57 break;
58 }
59
60 JniTestFunction *function = va_arg(args, JniTestFunction *);
61
62 __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "running %s", name);
63
64 char *oneResult = function(env);
65 if (oneResult != NULL) {
66 char *newResult;
67 asprintf(&newResult, "%s%s: %s\n",
68 (result == NULL) ? "" : result,
69 name, oneResult);
70 free(result);
71 if (newResult == NULL) {
72 // Shouldn't happen, but deal as gracefully as possible.
73 va_end(args);
74 return NULL;
75 }
76 result = newResult;
77 }
78
79 jthrowable oneException = (*env)->ExceptionOccurred(env);
80 if (oneException != NULL) {
81 (*env)->ExceptionDescribe(env);
82 (*env)->ExceptionClear(env);
83 }
84 }
85
86 va_end(args);
87
88 return result;
89 }
90
registerJniMethods(JNIEnv * env,const char * className,const JNINativeMethod * methods,int numMethods)91 int registerJniMethods(JNIEnv* env, const char* className, const JNINativeMethod* methods,
92 int numMethods)
93 {
94 __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG,
95 "Registering %s's %d native methods...", className, numMethods);
96 jclass clazz = (*env)->FindClass(env, className);
97 int result = (*env)->RegisterNatives(env, clazz, methods, numMethods);
98 (*env)->DeleteLocalRef(env, clazz);
99 if (result == 0) {
100 return 0;
101 }
102
103 __android_log_print(ANDROID_LOG_FATAL, LOG_TAG,
104 "Failed to register JNI methods for %s: %d\n", className, result);
105 return result;
106 }
107
throwException(JNIEnv * env,const char * className,const char * message)108 void throwException(JNIEnv* env, const char* className, const char* message) {
109 jclass clazz = (*env)->FindClass(env, className);
110 int result = (*env)->ThrowNew(env, clazz, message);
111 if (result != 0) {
112 __android_log_print(ANDROID_LOG_FATAL, LOG_TAG,
113 "Failed to throw %s: d\n", className, result);
114
115 }
116 }
117