• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 char* SampleData::kAnotherDescriptor = "this-is-another-arbitrary-thing";
60 const AIBinder_Class* SampleData::kAnotherClass =
61     AIBinder_Class_define(SampleData::kAnotherDescriptor, SampleClassOnCreate,
62                           SampleClassOnDestroy, SampleClassOnTransact);
63 
GetEnv()64 JNIEnv* GetEnv() {
65   JavaVM* vm = GetJavaVM();
66   if (vm == nullptr) return nullptr;
67 
68   JNIEnv* result = nullptr;
69   jint attach = vm->AttachCurrentThread(&result, nullptr);
70 
71   EXPECT_EQ(JNI_OK, attach);
72   EXPECT_NE(nullptr, result);
73   return result;
74 }
75