blob: 41262ac2ce83df27d0fb2905567ec6ec463f94e6 [file] [log] [blame]
Robert Shihaf52b272015-09-03 15:49:14 -07001/*
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
30using namespace android;
31
32static 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(client->asBinder());
43 data.writeInt32(dummyAudioSessionId);
44
45 // Keep synchronized with IMediaPlayerService.cpp!
46 enum {
47 CREATE = IBinder::FIRST_CALL_TRANSACTION,
48 };
49 status_t err = iMPService->asBinder()->transact(CREATE, data, &reply);
50
51 if (err == NO_ERROR) {
52 iMP = interface_cast<IMediaPlayer>(reply.readStrongBinder());
53 }
54 return err;
55}
56
57int testMediaPlayerInfoLeak(int command)
58{
59 sp<IMediaPlayer> iMP;
60 if (NO_ERROR != connectMediaPlayer(iMP)) {
61 return false;
62 }
63
64
65 Parcel data, reply;
66 data.writeInterfaceToken(iMP->getInterfaceDescriptor());
67 iMP->asBinder()->transact(command, data, &reply);
68
69 int leak = reply.readInt32();
70 status_t err = reply.readInt32();
71 return leak;
72}
73
74jint android_security_cts_MediaPlayer_test_getCurrentPositionLeak(JNIEnv* env __unused,
75 jobject thiz __unused)
76{
77 // Keep synchronized with IMediaPlayer.cpp!
78 enum {
79 GET_CURRENT_POSITION = 11,
80 };
81 return testMediaPlayerInfoLeak(GET_CURRENT_POSITION);
82}
83
84jint android_security_cts_MediaPlayer_test_getDurationLeak(JNIEnv* env __unused,
85 jobject thiz __unused)
86{
87 // Keep synchronized with IMediaPlayer.cpp!
88 enum {
89 GET_DURATION = 12,
90 };
91 return testMediaPlayerInfoLeak(GET_DURATION);
92}
93
94static JNINativeMethod gMethods[] = {
95 { "native_test_getCurrentPositionLeak", "()I",
96 (void *) android_security_cts_MediaPlayer_test_getCurrentPositionLeak },
97 { "native_test_getDurationLeak", "()I",
98 (void *) android_security_cts_MediaPlayer_test_getDurationLeak },
99};
100
101int register_android_security_cts_MediaPlayerInfoLeakTest(JNIEnv* env)
102{
103 jclass clazz = env->FindClass("android/security/cts/MediaPlayerInfoLeakTest");
104 return env->RegisterNatives(clazz, gMethods,
105 sizeof(gMethods) / sizeof(JNINativeMethod));
106}