blob: d16bd381aeb6fa6ed26e5b4df56b9136f20ffa41 [file] [log] [blame]
jjwongbf2570c2016-04-15 10:38:06 +08001/*
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 "AudioEffectBinderTest-JNI"
18
19#include <cstdio>
20#include <jni.h>
21#include <binder/Parcel.h>
22#include <media/stagefright/foundation/AMessage.h>
23
24using namespace android;
25
26/*
27 * Native methods used by
28 * cts/tests/tests/security/src/android/security/cts/StagefrightFoundationTest.java
29 */
30
31static jboolean android_security_cts_StagefrightFoundation_test_aMessageFromParcel(
32 JNIEnv* env __unused, jobject thiz __unused)
33{
34 const int kMaxNumItems = 64;
35 const int kNumItems = kMaxNumItems + 1 + 1000;
36 char name[128];
37
38 Parcel data;
39 data.writeInt32(0); // what
40 data.writeInt32(kNumItems); // numItems
41 for (int i = 0; i < kMaxNumItems; ++i) {
42 snprintf(name, sizeof(name), "item-%d", i);
43 data.writeCString(name); // name
44 data.writeInt32(0); // kTypeInt32
45 data.writeInt32(i); // value
46 }
47 data.writeCString("evil"); // name
48 data.writeInt32(0); // kTypeInt32
49 data.writeInt32(0); // value
50 // NOTE: This could overwrite mNumItems!
51
52 for (int i = 0; i < 1000; ++i) {
53 snprintf(name, sizeof(name), "evil-%d", i);
54 data.writeCString(name); // name
55 data.writeInt32(0); // kTypeInt32
56 data.writeInt32(0); // value
57 }
58
59 data.setDataPosition(0);
60 sp<AMessage> msg = AMessage::FromParcel(data);
61
62 for (int i = 0; i < kMaxNumItems; ++i) {
63 snprintf(name, sizeof(name), "item-%d", i);
64 int32_t value;
65 if (!msg->findInt32(name, &value)) {
66 ALOGE("cannot find value for %s", name);
67 return JNI_FALSE;
68 }
69 if (value != i) {
70 ALOGE("value is changed: expected %d actual %d", i, value);
71 return JNI_FALSE;
72 }
73 }
74 return JNI_TRUE;
75}
76
77int register_android_security_cts_StagefrightFoundationTest(JNIEnv *env)
78{
79 static JNINativeMethod methods[] = {
80 { "native_test_aMessageFromParcel", "()Z",
81 (void *) android_security_cts_StagefrightFoundation_test_aMessageFromParcel},
82 };
83
84 jclass clazz = env->FindClass("android/security/cts/StagefrightFoundationTest");
85 return env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
86}