1 /*
2  * Copyright (C) 2018 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 #define LOG_TAG "Cts-NdkBinderTest"
18 
19 #include "utilities.h"
20 
21 #include <android/log.h>
22 
23 static size_t sNumInstances = 0;
numInstances()24 size_t ThisShouldBeDestroyed::numInstances() { return sNumInstances; }
ThisShouldBeDestroyed()25 ThisShouldBeDestroyed::ThisShouldBeDestroyed() { sNumInstances++; }
~ThisShouldBeDestroyed()26 ThisShouldBeDestroyed::~ThisShouldBeDestroyed() { sNumInstances--; }
27 
SampleClassOnCreate(void * args)28 void* SampleClassOnCreate(void* args) {
29   return args;  // SampleData
30 }
31 
SampleClassOnDestroy(void * userData)32 void SampleClassOnDestroy(void* userData) {
33   SampleData* data = static_cast<SampleData*>(userData);
34   if (data->onDestroy != nullptr) {
35     data->onDestroy(data);
36   }
37   delete data;
38 }
39 
SampleClassOnTransact(AIBinder * binder,transaction_code_t code,const AParcel * in,AParcel * out)40 binder_status_t SampleClassOnTransact(AIBinder* binder, transaction_code_t code,
41                                       const AParcel* in, AParcel* out) {
42   SampleData* data = static_cast<SampleData*>(AIBinder_getUserData(binder));
43   if (data == nullptr) {
44     __android_log_write(ANDROID_LOG_FATAL, LOG_TAG, "null user data");
45   }
46   data->numberTransactions++;
47   if (data->onTransact == nullptr) {
48     ADD_FAILURE() << "onTransact not specified, but transactions called";
49     return STATUS_FAILED_TRANSACTION;
50   }
51   return data->onTransact(code, in, out);
52 }
53 
54 const char* SampleData::kDescriptor = "this-is-arbitrary";
55 const AIBinder_Class* SampleData::kClass =
56     AIBinder_Class_define(SampleData::kDescriptor, SampleClassOnCreate,
57                           SampleClassOnDestroy, SampleClassOnTransact);
58 
59 const AIBinder_Class* SampleData::kAnotherClassWithSameDescriptor = AIBinder_Class_define(
60     SampleData::kDescriptor, SampleClassOnCreate, SampleClassOnDestroy, SampleClassOnTransact);
61 
62 const char* SampleData::kAnotherDescriptor = "this-is-another-arbitrary-thing";
63 const AIBinder_Class* SampleData::kAnotherClass =
64     AIBinder_Class_define(SampleData::kAnotherDescriptor, SampleClassOnCreate,
65                           SampleClassOnDestroy, SampleClassOnTransact);
66 
GetEnv()67 JNIEnv* GetEnv() {
68   JavaVM* vm = GetJavaVM();
69   if (vm == nullptr) return nullptr;
70 
71   JNIEnv* result = nullptr;
72   jint attach = vm->AttachCurrentThread(&result, nullptr);
73 
74   EXPECT_EQ(JNI_OK, attach);
75   EXPECT_NE(nullptr, result);
76   return result;
77 }
78 
callStaticJavaMethodForObject(JNIEnv * env,const std::string & clazz,const std::string & method,const std::string & type)79 jobject callStaticJavaMethodForObject(JNIEnv* env, const std::string& clazz,
80                                       const std::string& method, const std::string& type) {
81   jclass cl = env->FindClass(clazz.c_str());
82   if (cl == nullptr) {
83     __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "No class %s", clazz.c_str());
84     return nullptr;
85   }
86 
87   jmethodID mid = env->GetStaticMethodID(cl, method.c_str(), type.c_str());
88   if (mid == nullptr) {
89     __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "No method id %s", method.c_str());
90     return nullptr;
91   }
92 
93   jobject object = env->CallStaticObjectMethod(cl, mid);
94   if (object == nullptr) {
95     __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, "Got null object from Java");
96     return nullptr;
97   }
98 
99   return object;
100 }
101