• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cutils/log.h>
20 
21 #include <stdarg.h>
22 #include <stdlib.h>
23 
24 #define LOG_TAG "cts"
25 
26 /* See helper.h for docs. */
failure(const char * format,...)27 char *failure(const char *format, ...) {
28     va_list args;
29     char *result;
30 
31     va_start(args, format);
32     LOG_PRI_VA(ANDROID_LOG_ERROR, LOG_TAG, format, args);
33     va_end(args);
34 
35     va_start(args, format);
36     int count = vasprintf(&result, format, args);
37     va_end(args);
38 
39     if (count < 0) {
40         return NULL;
41     }
42 
43     return result;
44 }
45 
46 /* See helper.h for docs. */
runJniTests(JNIEnv * env,...)47 char *runJniTests(JNIEnv *env, ...) {
48     va_list args;
49     char *result = NULL;
50 
51     va_start(args, env);
52 
53     for (;;) {
54         const char *name = va_arg(args, const char *);
55         if (name == NULL) {
56             break;
57         }
58 
59         JniTestFunction *function = va_arg(args, JniTestFunction *);
60 
61         ALOGI("running %s", name);
62 
63         char *oneResult = function(env);
64         if (oneResult != NULL) {
65             char *newResult;
66             asprintf(&newResult, "%s%s: %s\n",
67                     (result == NULL) ? "" : result,
68                     name, oneResult);
69             free(result);
70             if (newResult == NULL) {
71                 // Shouldn't happen, but deal as gracefully as possible.
72                 return NULL;
73             }
74             result = newResult;
75         }
76 
77         jthrowable oneException = (*env)->ExceptionOccurred(env);
78         if (oneException != NULL) {
79             (*env)->ExceptionDescribe(env);
80             (*env)->ExceptionClear(env);
81         }
82     }
83 
84     va_end(args);
85 
86     return result;
87 }
88