Robert Shih | e943923 | 2015-09-03 15:49:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "MediaPlayerInfoLeakTest-JNI" |
| 18 | |
| 19 | #include <jni.h> |
| 20 | |
| 21 | #include <binder/Parcel.h> |
| 22 | #include <binder/IServiceManager.h> |
| 23 | |
| 24 | #include <media/IMediaPlayer.h> |
| 25 | #include <media/IMediaPlayerService.h> |
| 26 | #include <media/IMediaPlayerClient.h> |
| 27 | |
| 28 | #include <sys/stat.h> |
| 29 | |
| 30 | using namespace android; |
| 31 | |
| 32 | static status_t connectMediaPlayer(sp<IMediaPlayer>& iMP) |
| 33 | { |
| 34 | sp<IServiceManager> sm = defaultServiceManager(); |
| 35 | sp<IBinder> mediaPlayerService = sm->checkService(String16("media.player")); |
| 36 | |
| 37 | sp<IMediaPlayerService> iMPService = IMediaPlayerService::asInterface(mediaPlayerService); |
| 38 | sp<IMediaPlayerClient> client; |
| 39 | Parcel data, reply; |
| 40 | int dummyAudioSessionId = 1; |
| 41 | data.writeInterfaceToken(iMPService->getInterfaceDescriptor()); |
| 42 | data.writeStrongBinder(IInterface::asBinder(client)); |
| 43 | data.writeInt32(dummyAudioSessionId); |
| 44 | |
| 45 | // Keep synchronized with IMediaPlayerService.cpp! |
| 46 | enum { |
| 47 | CREATE = IBinder::FIRST_CALL_TRANSACTION, |
| 48 | }; |
| 49 | status_t err = IInterface::asBinder(iMPService)->transact(CREATE, data, &reply); |
| 50 | if (err == NO_ERROR) { |
| 51 | iMP = interface_cast<IMediaPlayer>(reply.readStrongBinder()); |
| 52 | } |
| 53 | return err; |
| 54 | } |
| 55 | |
| 56 | int testMediaPlayerInfoLeak(int command) |
| 57 | { |
| 58 | sp<IMediaPlayer> iMP; |
| 59 | if (NO_ERROR != connectMediaPlayer(iMP)) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | Parcel data, reply; |
| 65 | data.writeInterfaceToken(iMP->getInterfaceDescriptor()); |
| 66 | IInterface::asBinder(iMP)->transact(command, data, &reply); |
| 67 | |
| 68 | int leak = reply.readInt32(); |
| 69 | status_t err = reply.readInt32(); |
| 70 | return leak; |
| 71 | } |
| 72 | |
| 73 | jint android_security_cts_MediaPlayer_test_getCurrentPositionLeak(JNIEnv* env __unused, |
| 74 | jobject thiz __unused) |
| 75 | { |
| 76 | // Keep synchronized with IMediaPlayer.cpp! |
| 77 | enum { |
| 78 | GET_CURRENT_POSITION = 16, |
| 79 | }; |
| 80 | return testMediaPlayerInfoLeak(GET_CURRENT_POSITION); |
| 81 | } |
| 82 | |
| 83 | jint android_security_cts_MediaPlayer_test_getDurationLeak(JNIEnv* env __unused, |
| 84 | jobject thiz __unused) |
| 85 | { |
| 86 | // Keep synchronized with IMediaPlayer.cpp! |
| 87 | enum { |
| 88 | GET_DURATION = 17, |
| 89 | }; |
| 90 | return testMediaPlayerInfoLeak(GET_DURATION); |
| 91 | } |
| 92 | |
| 93 | static JNINativeMethod gMethods[] = { |
| 94 | { "native_test_getCurrentPositionLeak", "()I", |
| 95 | (void *) android_security_cts_MediaPlayer_test_getCurrentPositionLeak }, |
| 96 | { "native_test_getDurationLeak", "()I", |
| 97 | (void *) android_security_cts_MediaPlayer_test_getDurationLeak }, |
| 98 | }; |
| 99 | |
| 100 | int register_android_security_cts_MediaPlayerInfoLeakTest(JNIEnv* env) |
| 101 | { |
| 102 | jclass clazz = env->FindClass("android/security/cts/MediaPlayerInfoLeakTest"); |
| 103 | return env->RegisterNatives(clazz, gMethods, |
| 104 | sizeof(gMethods) / sizeof(JNINativeMethod)); |
| 105 | } |