add a test for LOAD_EFFECT_LIBRARY Binder call

Added a test verifying that it is not possible to
load an effect library via any IAudioFlinger binder
transaction.

Bug: 9557593.

Cherry-picked from 6fe72da.

Change-Id: Ieb00362a8dcbe5fd8920fe985f9bd9e33e1a01e5
diff --git a/tests/tests/security/jni/Android.mk b/tests/tests/security/jni/Android.mk
index 06172c5..9e45a54 100644
--- a/tests/tests/security/jni/Android.mk
+++ b/tests/tests/security/jni/Android.mk
@@ -26,9 +26,10 @@
 		android_security_cts_CharDeviceTest.cpp \
 		android_security_cts_LinuxRngTest.cpp \
 		android_security_cts_NativeCodeTest.cpp \
+		android_security_cts_LoadEffectLibraryTest.cpp
 
 LOCAL_C_INCLUDES := $(JNI_H_INCLUDE)
 
-LOCAL_SHARED_LIBRARIES := libnativehelper liblog
+LOCAL_SHARED_LIBRARIES := libnativehelper liblog libbinder libutils libmedia
 
 include $(BUILD_SHARED_LIBRARY)
diff --git a/tests/tests/security/jni/CtsSecurityJniOnLoad.cpp b/tests/tests/security/jni/CtsSecurityJniOnLoad.cpp
index 7577eef..b9aeaf5 100644
--- a/tests/tests/security/jni/CtsSecurityJniOnLoad.cpp
+++ b/tests/tests/security/jni/CtsSecurityJniOnLoad.cpp
@@ -20,6 +20,7 @@
 extern int register_android_security_cts_CharDeviceTest(JNIEnv*);
 extern int register_android_security_cts_LinuxRngTest(JNIEnv*);
 extern int register_android_security_cts_NativeCodeTest(JNIEnv*);
+extern int register_android_security_cts_LoadEffectLibraryTest(JNIEnv*);
 
 jint JNI_OnLoad(JavaVM *vm, void *reserved) {
     JNIEnv *env = NULL;
@@ -40,5 +41,9 @@
         return JNI_ERR;
     }
 
+    if (register_android_security_cts_LoadEffectLibraryTest(env)) {
+        return JNI_ERR;
+    }
+
     return JNI_VERSION_1_4;
 }
diff --git a/tests/tests/security/jni/android_security_cts_LoadEffectLibraryTest.cpp b/tests/tests/security/jni/android_security_cts_LoadEffectLibraryTest.cpp
new file mode 100644
index 0000000..6e0f6e1
--- /dev/null
+++ b/tests/tests/security/jni/android_security_cts_LoadEffectLibraryTest.cpp
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <jni.h>
+#include <binder/IServiceManager.h>
+#include <media/IAudioFlinger.h>
+#include <media/AudioEffect.h>
+
+
+using namespace android;
+
+
+/*
+ * Native method used by
+ * cts/tests/tests/security/src/android/security/cts/LoadEffectLibraryTest.java
+ *
+ * Checks that no IAudioFlinger binder transaction manages to load an effect library
+ * as LOAD_EFFECT_LIBRARY did in gingerbread.
+ */
+
+jboolean android_security_cts_LoadEffectLibraryTest_doLoadLibraryTest(JNIEnv* env, jobject thiz)
+{
+    sp<IServiceManager> sm = defaultServiceManager();
+    if (sm == 0) {
+        return false;
+    }
+
+    sp<IBinder> binder = sm->getService(String16("media.audio_flinger"));
+    if (binder == 0) {
+        return false;
+    }
+
+    Parcel data, reply;
+    sp<IAudioFlinger> af = interface_cast<IAudioFlinger>(binder);
+
+    data.writeInterfaceToken(af->getInterfaceDescriptor());
+    // test library path defined in cts/tests/tests/security/testeffect/Android.mk
+    data.writeCString("/system/lib/soundfx/libctstesteffect.so");
+
+    // test 100 IAudioFlinger binder transaction values and check that none corresponds
+    // to LOAD_EFFECT_LIBRARY and successfully loads our test library
+    for (uint32_t i = IBinder::FIRST_CALL_TRANSACTION;
+            i < IBinder::FIRST_CALL_TRANSACTION + 100;
+            i++) {
+        status_t status = binder->transact(i, data, &reply);
+        if (status != NO_ERROR) {
+            continue;
+        }
+        status = reply.readInt32();
+        if (status != NO_ERROR) {
+            continue;
+        }
+
+        // Effect UUID defined in cts/tests/tests/security/testeffect/CTSTestEffect.cpp
+        effect_uuid_t uuid =
+                    {0xff93e360, 0x0c3c, 0x11e3, 0x8a97, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}};
+        effect_descriptor_t desc;
+
+        status = AudioEffect::getEffectDescriptor(&uuid, &desc);
+        if (status == NO_ERROR) {
+            return false;
+        }
+    }
+    return true;
+}
+
+static JNINativeMethod gMethods[] = {
+    {  "doLoadLibraryTest", "()Z",
+            (void *) android_security_cts_LoadEffectLibraryTest_doLoadLibraryTest },
+};
+
+int register_android_security_cts_LoadEffectLibraryTest(JNIEnv* env)
+{
+    jclass clazz = env->FindClass("android/security/cts/LoadEffectLibraryTest");
+    return env->RegisterNatives(clazz, gMethods,
+            sizeof(gMethods) / sizeof(JNINativeMethod));
+}
diff --git a/tests/tests/security/src/android/security/cts/LoadEffectLibraryTest.java b/tests/tests/security/src/android/security/cts/LoadEffectLibraryTest.java
new file mode 100644
index 0000000..900ac7f
--- /dev/null
+++ b/tests/tests/security/src/android/security/cts/LoadEffectLibraryTest.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.security.cts;
+
+import junit.framework.TestCase;
+
+public class LoadEffectLibraryTest extends TestCase {
+
+    static {
+        System.loadLibrary("ctssecurity_jni");
+    }
+
+    /**
+     * Checks that no binder calls to IAudioFlinger manages to load an effect library.
+     */
+    public void testLoadLibrary() throws Exception {
+        assertTrue(doLoadLibraryTest());
+    }
+
+    private static native boolean doLoadLibraryTest();
+
+}
diff --git a/tests/tests/security/testeffect/Android.mk b/tests/tests/security/testeffect/Android.mk
new file mode 100644
index 0000000..3b23bc6
--- /dev/null
+++ b/tests/tests/security/testeffect/Android.mk
@@ -0,0 +1,34 @@
+# Copyright (C) 2013 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH:= $(call my-dir)
+
+# Test effect library
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := tests
+
+LOCAL_SRC_FILES:= \
+  CTSTestEffect.cpp
+
+LOCAL_CFLAGS+= -O2 -fvisibility=hidden
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/soundfx
+LOCAL_MODULE:= libctstesteffect
+
+LOCAL_C_INCLUDES := \
+  $(call include-path-for, audio-effects)
+
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/tests/tests/security/testeffect/CTSTestEffect.cpp b/tests/tests/security/testeffect/CTSTestEffect.cpp
new file mode 100644
index 0000000..5b4b795
--- /dev/null
+++ b/tests/tests/security/testeffect/CTSTestEffect.cpp
@@ -0,0 +1,222 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string.h>
+#include <hardware/audio_effect.h>
+
+
+extern "C" {
+
+extern const struct effect_interface_s gCTSEffectInterface;
+
+const effect_descriptor_t gCTSEffectsDescriptor = {
+        {0xf2a4bb20, 0x0c3c, 0x11e3, 0x8b07, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // type
+        {0xff93e360, 0x0c3c, 0x11e3, 0x8a97, {0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b}}, // uuid
+        EFFECT_CONTROL_API_VERSION,
+        0,
+        0,
+        1,
+        "CTS test Effect",
+        "The Android Open Source Project",
+};
+
+struct CTSEffectsContext {
+    const struct effect_interface_s *mItfe;
+    effect_config_t mConfig;
+};
+
+//
+//--- Effect Library Interface Implementation
+//
+
+int CTSEffectsLib_QueryNumberEffects(uint32_t *pNumEffects) {
+    *pNumEffects = 1;
+    return 0;
+}
+
+int CTSEffectsLib_QueryEffect(uint32_t index,
+                              effect_descriptor_t *pDescriptor) {
+    if (pDescriptor == NULL) {
+        return -EINVAL;
+    }
+    if (index > 0) {
+        return -EINVAL;
+    }
+    memcpy(pDescriptor, &gCTSEffectsDescriptor, sizeof(effect_descriptor_t));
+    return 0;
+}
+
+int CTSEffectsLib_Create(const effect_uuid_t *uuid,
+                         int32_t sessionId,
+                         int32_t ioId,
+                         effect_handle_t *pHandle) {
+    if (pHandle == NULL || uuid == NULL) {
+        return -EINVAL;
+    }
+
+    if (memcmp(uuid, &gCTSEffectsDescriptor.uuid, sizeof(effect_uuid_t)) != 0) {
+        return -EINVAL;
+    }
+
+    CTSEffectsContext *pContext = new CTSEffectsContext;
+
+    pContext->mItfe = &gCTSEffectInterface;
+
+    *pHandle = (effect_handle_t)pContext;
+
+    return 0;
+
+}
+
+int CTSEffectsLib_Release(effect_handle_t handle) {
+    CTSEffectsContext * pContext = (CTSEffectsContext *)handle;
+
+    if (pContext == NULL) {
+        return -EINVAL;
+    }
+    delete pContext;
+
+    return 0;
+}
+
+int CTSEffectsLib_GetDescriptor(const effect_uuid_t *uuid,
+                                effect_descriptor_t *pDescriptor) {
+
+    if (pDescriptor == NULL || uuid == NULL){
+        return -EINVAL;
+    }
+
+    if (memcmp(uuid, &gCTSEffectsDescriptor.uuid, sizeof(effect_uuid_t)) == 0) {
+        *pDescriptor = gCTSEffectsDescriptor;
+        return 0;
+    }
+
+    return  -EINVAL;
+} /* end CTSEffectsLib_GetDescriptor */
+
+//
+//--- Effect Control Interface Implementation
+//
+
+int CTSEffects_process(
+        effect_handle_t self,audio_buffer_t *inBuffer, audio_buffer_t *outBuffer)
+{
+    return 0;
+}   // end CTSEffects_process
+
+int CTSEffects_command(effect_handle_t self, uint32_t cmdCode, uint32_t cmdSize,
+        void *pCmdData, uint32_t *replySize, void *pReplyData) {
+
+    CTSEffectsContext * pContext = (CTSEffectsContext *)self;
+
+    if (pContext == NULL) {
+        return -EINVAL;
+    }
+
+    switch (cmdCode) {
+    case EFFECT_CMD_INIT:
+        if (pReplyData == NULL || *replySize != sizeof(int)) {
+            return -EINVAL;
+        }
+        *(int *) pReplyData = 0;
+        break;
+    case EFFECT_CMD_SET_CONFIG:
+        if (pCmdData == NULL || cmdSize != sizeof(effect_config_t)
+                || pReplyData == NULL || *replySize != sizeof(int)) {
+            return -EINVAL;
+        }
+        memcpy(&pContext->mConfig, pCmdData, cmdSize);
+        *(int *) pReplyData = 0;
+        break;
+    case EFFECT_CMD_GET_CONFIG:
+        if (pReplyData == NULL ||
+            *replySize != sizeof(effect_config_t)) {
+            return -EINVAL;
+        }
+        memcpy(pReplyData, &pContext->mConfig, *replySize);
+        break;
+    case EFFECT_CMD_RESET:
+        break;
+    case EFFECT_CMD_ENABLE:
+    case EFFECT_CMD_DISABLE:
+        if (pReplyData == NULL || *replySize != sizeof(int)) {
+            return -EINVAL;
+        }
+        *(int *)pReplyData = 0;
+        break;
+    case EFFECT_CMD_GET_PARAM: {
+        if (pCmdData == NULL ||
+            cmdSize != (int)(sizeof(effect_param_t)) ||
+            pReplyData == NULL ||
+            *replySize < (int)(sizeof(effect_param_t))) {
+            return -EINVAL;
+        }
+        effect_param_t *p = (effect_param_t *)pReplyData;
+        p->status = 0;
+        } break;
+    case EFFECT_CMD_SET_PARAM: {
+        if (pCmdData == NULL ||
+            cmdSize != (int)(sizeof(effect_param_t)) ||
+            pReplyData == NULL || *replySize != sizeof(int32_t)) {
+            return -EINVAL;
+        }
+        *(int32_t *)pReplyData = 0;
+        } break;
+    default:
+        break;
+    }
+
+    return 0;
+}
+
+/* Effect Control Interface Implementation: get_descriptor */
+int CTSEffects_getDescriptor(effect_handle_t   self,
+                                    effect_descriptor_t *pDescriptor)
+{
+    CTSEffectsContext * pContext = (CTSEffectsContext *) self;
+
+    if (pContext == NULL || pDescriptor == NULL) {
+        return -EINVAL;
+    }
+
+    *pDescriptor = gCTSEffectsDescriptor;
+
+    return 0;
+}   /* end CTSEffects_getDescriptor */
+
+// effect_handle_t interface implementation for test effect
+const struct effect_interface_s gCTSEffectInterface = {
+        CTSEffects_process,
+        CTSEffects_command,
+        CTSEffects_getDescriptor,
+        NULL,
+};
+
+// This is the only symbol that needs to be exported
+__attribute__ ((visibility ("default")))
+audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = {
+    tag : AUDIO_EFFECT_LIBRARY_TAG,
+    version : EFFECT_LIBRARY_API_VERSION,
+    name : "CTS Effects Library",
+    implementor : "The Android Open Source Project",
+    query_num_effects : CTSEffectsLib_QueryNumberEffects,
+    query_effect : CTSEffectsLib_QueryEffect,
+    create_effect : CTSEffectsLib_Create,
+    release_effect : CTSEffectsLib_Release,
+    get_descriptor : CTSEffectsLib_GetDescriptor,
+};
+
+}; // extern "C"