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