134486800a022857b45c3882624449980c5cb6907Jeff Tinker/*
234486800a022857b45c3882624449980c5cb6907Jeff Tinker * Copyright (C) 2013 The Android Open Source Project
334486800a022857b45c3882624449980c5cb6907Jeff Tinker *
434486800a022857b45c3882624449980c5cb6907Jeff Tinker * Licensed under the Apache License, Version 2.0 (the "License");
534486800a022857b45c3882624449980c5cb6907Jeff Tinker * you may not use this file except in compliance with the License.
634486800a022857b45c3882624449980c5cb6907Jeff Tinker * You may obtain a copy of the License at
734486800a022857b45c3882624449980c5cb6907Jeff Tinker *
834486800a022857b45c3882624449980c5cb6907Jeff Tinker *      http://www.apache.org/licenses/LICENSE-2.0
934486800a022857b45c3882624449980c5cb6907Jeff Tinker *
1034486800a022857b45c3882624449980c5cb6907Jeff Tinker * Unless required by applicable law or agreed to in writing, software
1134486800a022857b45c3882624449980c5cb6907Jeff Tinker * distributed under the License is distributed on an "AS IS" BASIS,
1234486800a022857b45c3882624449980c5cb6907Jeff Tinker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1334486800a022857b45c3882624449980c5cb6907Jeff Tinker * See the License for the specific language governing permissions and
1434486800a022857b45c3882624449980c5cb6907Jeff Tinker * limitations under the License.
1534486800a022857b45c3882624449980c5cb6907Jeff Tinker */
1634486800a022857b45c3882624449980c5cb6907Jeff Tinker
1734486800a022857b45c3882624449980c5cb6907Jeff Tinker//#define LOG_NDEBUG 0
1834486800a022857b45c3882624449980c5cb6907Jeff Tinker#define LOG_TAG "MockDrmCryptoPlugin"
1934486800a022857b45c3882624449980c5cb6907Jeff Tinker#include <utils/Log.h>
2034486800a022857b45c3882624449980c5cb6907Jeff Tinker
2134486800a022857b45c3882624449980c5cb6907Jeff Tinker
2234486800a022857b45c3882624449980c5cb6907Jeff Tinker#include "drm/DrmAPI.h"
2334486800a022857b45c3882624449980c5cb6907Jeff Tinker#include "MockDrmCryptoPlugin.h"
2434486800a022857b45c3882624449980c5cb6907Jeff Tinker#include "media/stagefright/MediaErrors.h"
2534486800a022857b45c3882624449980c5cb6907Jeff Tinker
2634486800a022857b45c3882624449980c5cb6907Jeff Tinkerusing namespace android;
2734486800a022857b45c3882624449980c5cb6907Jeff Tinker
2834486800a022857b45c3882624449980c5cb6907Jeff Tinker// Shared library entry point
2934486800a022857b45c3882624449980c5cb6907Jeff TinkerDrmFactory *createDrmFactory()
3034486800a022857b45c3882624449980c5cb6907Jeff Tinker{
3134486800a022857b45c3882624449980c5cb6907Jeff Tinker    return new MockDrmFactory();
3234486800a022857b45c3882624449980c5cb6907Jeff Tinker}
3334486800a022857b45c3882624449980c5cb6907Jeff Tinker
3434486800a022857b45c3882624449980c5cb6907Jeff Tinker// Shared library entry point
3534486800a022857b45c3882624449980c5cb6907Jeff TinkerCryptoFactory *createCryptoFactory()
3634486800a022857b45c3882624449980c5cb6907Jeff Tinker{
3734486800a022857b45c3882624449980c5cb6907Jeff Tinker    return new MockCryptoFactory();
3834486800a022857b45c3882624449980c5cb6907Jeff Tinker}
3934486800a022857b45c3882624449980c5cb6907Jeff Tinker
4034486800a022857b45c3882624449980c5cb6907Jeff Tinkerconst uint8_t mock_uuid[16] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
4134486800a022857b45c3882624449980c5cb6907Jeff Tinker                               0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10};
4234486800a022857b45c3882624449980c5cb6907Jeff Tinker
4334486800a022857b45c3882624449980c5cb6907Jeff Tinkernamespace android {
4434486800a022857b45c3882624449980c5cb6907Jeff Tinker
4534486800a022857b45c3882624449980c5cb6907Jeff Tinker    // MockDrmFactory
4634486800a022857b45c3882624449980c5cb6907Jeff Tinker    bool MockDrmFactory::isCryptoSchemeSupported(const uint8_t uuid[16])
4734486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
4868d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker        return (!memcmp(uuid, mock_uuid, sizeof(mock_uuid)));
4934486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
5034486800a022857b45c3882624449980c5cb6907Jeff Tinker
5134486800a022857b45c3882624449980c5cb6907Jeff Tinker    bool MockDrmFactory::isContentTypeSupported(const String8 &mimeType)
5234486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
5334486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (mimeType != "video/mp4") {
5434486800a022857b45c3882624449980c5cb6907Jeff Tinker            return false;
5534486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
5634486800a022857b45c3882624449980c5cb6907Jeff Tinker        return true;
5734486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
5834486800a022857b45c3882624449980c5cb6907Jeff Tinker
5968b3d9f49e68a11af5225175dc9e60ce88819e84Edwin Wong    status_t MockDrmFactory::createDrmPlugin(const uint8_t /* uuid */[16],
6068b3d9f49e68a11af5225175dc9e60ce88819e84Edwin Wong                                             DrmPlugin **plugin)
6134486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
6234486800a022857b45c3882624449980c5cb6907Jeff Tinker        *plugin = new MockDrmPlugin();
6334486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
6434486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
6534486800a022857b45c3882624449980c5cb6907Jeff Tinker
6634486800a022857b45c3882624449980c5cb6907Jeff Tinker    // MockCryptoFactory
6734486800a022857b45c3882624449980c5cb6907Jeff Tinker    bool MockCryptoFactory::isCryptoSchemeSupported(const uint8_t uuid[16]) const
6834486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
6968d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker        return (!memcmp(uuid, mock_uuid, sizeof(mock_uuid)));
7034486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
7134486800a022857b45c3882624449980c5cb6907Jeff Tinker
722fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker    status_t MockCryptoFactory::createPlugin(const uint8_t /* uuid */[16],
732fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker                                             const void * /* data */,
742fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker                                             size_t /* size */, CryptoPlugin **plugin)
7534486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
7634486800a022857b45c3882624449980c5cb6907Jeff Tinker        *plugin = new MockCryptoPlugin();
7734486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
7834486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
7934486800a022857b45c3882624449980c5cb6907Jeff Tinker
8034486800a022857b45c3882624449980c5cb6907Jeff Tinker
8134486800a022857b45c3882624449980c5cb6907Jeff Tinker    // MockDrmPlugin methods
8234486800a022857b45c3882624449980c5cb6907Jeff Tinker
8334486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::openSession(Vector<uint8_t> &sessionId)
8434486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
8534486800a022857b45c3882624449980c5cb6907Jeff Tinker        const size_t kSessionIdSize = 8;
8634486800a022857b45c3882624449980c5cb6907Jeff Tinker
8734486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
8834486800a022857b45c3882624449980c5cb6907Jeff Tinker        for (size_t i = 0; i < kSessionIdSize / sizeof(long); i++) {
8934486800a022857b45c3882624449980c5cb6907Jeff Tinker            long r = random();
9034486800a022857b45c3882624449980c5cb6907Jeff Tinker            sessionId.appendArray((uint8_t *)&r, sizeof(long));
9134486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
9234486800a022857b45c3882624449980c5cb6907Jeff Tinker        mSessions.add(sessionId);
9334486800a022857b45c3882624449980c5cb6907Jeff Tinker
9434486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::openSession() -> %s", vectorToString(sessionId).string());
9534486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
9634486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
9734486800a022857b45c3882624449980c5cb6907Jeff Tinker
9834486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::closeSession(Vector<uint8_t> const &sessionId)
9934486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
10034486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
10134486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::closeSession(%s)", vectorToString(sessionId).string());
10234486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = findSession(sessionId);
10334486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index == kNotFound) {
10434486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Invalid sessionId");
10534486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
10634486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
10734486800a022857b45c3882624449980c5cb6907Jeff Tinker        mSessions.removeAt(index);
10834486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
10934486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
11034486800a022857b45c3882624449980c5cb6907Jeff Tinker
11134486800a022857b45c3882624449980c5cb6907Jeff Tinker
11234486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::getKeyRequest(Vector<uint8_t> const &sessionId,
11334486800a022857b45c3882624449980c5cb6907Jeff Tinker                                          Vector<uint8_t> const &initData,
11434486800a022857b45c3882624449980c5cb6907Jeff Tinker                                          String8 const &mimeType, KeyType keyType,
11534486800a022857b45c3882624449980c5cb6907Jeff Tinker                                          KeyedVector<String8, String8> const &optionalParameters,
116d072c909d87f8150433860f6de4c11df6e09e2f9Jeff Tinker                                          Vector<uint8_t> &request, String8 &defaultUrl,
117d072c909d87f8150433860f6de4c11df6e09e2f9Jeff Tinker                                          KeyRequestType *keyRequestType)
11834486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
11934486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
12034486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::getKeyRequest(sessionId=%s, initData=%s, mimeType=%s"
12134486800a022857b45c3882624449980c5cb6907Jeff Tinker              ", keyType=%d, optionalParameters=%s))",
12234486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(sessionId).string(), vectorToString(initData).string(), mimeType.string(),
12334486800a022857b45c3882624449980c5cb6907Jeff Tinker              keyType, stringMapToString(optionalParameters).string());
12434486800a022857b45c3882624449980c5cb6907Jeff Tinker
12534486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = findSession(sessionId);
12634486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index == kNotFound) {
12734486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Invalid sessionId");
12834486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
12934486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
13034486800a022857b45c3882624449980c5cb6907Jeff Tinker
13134486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by mock plugin and verifed cts test app
13234486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] initData           -> mock-initdata
13334486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   string mimeType           -> mock-mimetype
13434486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   string keyType            -> mock-keytype
13534486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   string optionalParameters -> mock-optparams formatted as {key1,value1},{key2,value2}
13634486800a022857b45c3882624449980c5cb6907Jeff Tinker
13734486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-initdata"), initData);
13834486800a022857b45c3882624449980c5cb6907Jeff Tinker        mStringProperties.add(String8("mock-mimetype"), mimeType);
13934486800a022857b45c3882624449980c5cb6907Jeff Tinker
14034486800a022857b45c3882624449980c5cb6907Jeff Tinker        String8 keyTypeStr;
14134486800a022857b45c3882624449980c5cb6907Jeff Tinker        keyTypeStr.appendFormat("%d", (int)keyType);
14234486800a022857b45c3882624449980c5cb6907Jeff Tinker        mStringProperties.add(String8("mock-keytype"), keyTypeStr);
14334486800a022857b45c3882624449980c5cb6907Jeff Tinker
14434486800a022857b45c3882624449980c5cb6907Jeff Tinker        String8 params;
14534486800a022857b45c3882624449980c5cb6907Jeff Tinker        for (size_t i = 0; i < optionalParameters.size(); i++) {
14634486800a022857b45c3882624449980c5cb6907Jeff Tinker            params.appendFormat("%s{%s,%s}", i ? "," : "",
14734486800a022857b45c3882624449980c5cb6907Jeff Tinker                                optionalParameters.keyAt(i).string(),
14834486800a022857b45c3882624449980c5cb6907Jeff Tinker                                optionalParameters.valueAt(i).string());
14934486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
15034486800a022857b45c3882624449980c5cb6907Jeff Tinker        mStringProperties.add(String8("mock-optparams"), params);
15134486800a022857b45c3882624449980c5cb6907Jeff Tinker
15234486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by cts test app returned from mock plugin
15334486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] mock-request       -> request
15434486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   string mock-default-url   -> defaultUrl
1552fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker        //   string mock-keyRequestType -> keyRequestType
15634486800a022857b45c3882624449980c5cb6907Jeff Tinker
15734486800a022857b45c3882624449980c5cb6907Jeff Tinker        index = mByteArrayProperties.indexOfKey(String8("mock-request"));
15834486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index < 0) {
15934486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Missing 'mock-request' parameter for mock");
16034486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
16134486800a022857b45c3882624449980c5cb6907Jeff Tinker        } else {
16234486800a022857b45c3882624449980c5cb6907Jeff Tinker            request = mByteArrayProperties.valueAt(index);
16334486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
16434486800a022857b45c3882624449980c5cb6907Jeff Tinker
16534486800a022857b45c3882624449980c5cb6907Jeff Tinker        index = mStringProperties.indexOfKey(String8("mock-defaultUrl"));
16634486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index < 0) {
16734486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Missing 'mock-defaultUrl' parameter for mock");
16834486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
16934486800a022857b45c3882624449980c5cb6907Jeff Tinker        } else {
17034486800a022857b45c3882624449980c5cb6907Jeff Tinker            defaultUrl = mStringProperties.valueAt(index);
17134486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
172d072c909d87f8150433860f6de4c11df6e09e2f9Jeff Tinker
173d072c909d87f8150433860f6de4c11df6e09e2f9Jeff Tinker        index = mStringProperties.indexOfKey(String8("mock-keyRequestType"));
174d072c909d87f8150433860f6de4c11df6e09e2f9Jeff Tinker        if (index < 0) {
175d072c909d87f8150433860f6de4c11df6e09e2f9Jeff Tinker            ALOGD("Missing 'mock-keyRequestType' parameter for mock");
176d072c909d87f8150433860f6de4c11df6e09e2f9Jeff Tinker            return BAD_VALUE;
177d072c909d87f8150433860f6de4c11df6e09e2f9Jeff Tinker        } else {
178d072c909d87f8150433860f6de4c11df6e09e2f9Jeff Tinker            *keyRequestType = static_cast<KeyRequestType>(
179d072c909d87f8150433860f6de4c11df6e09e2f9Jeff Tinker                atoi(mStringProperties.valueAt(index).string()));
180d072c909d87f8150433860f6de4c11df6e09e2f9Jeff Tinker        }
181d072c909d87f8150433860f6de4c11df6e09e2f9Jeff Tinker
18234486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
18334486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
18434486800a022857b45c3882624449980c5cb6907Jeff Tinker
18534486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::provideKeyResponse(Vector<uint8_t> const &sessionId,
18634486800a022857b45c3882624449980c5cb6907Jeff Tinker                                               Vector<uint8_t> const &response,
18734486800a022857b45c3882624449980c5cb6907Jeff Tinker                                               Vector<uint8_t> &keySetId)
18834486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
18934486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
19034486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::provideKeyResponse(sessionId=%s, response=%s)",
19134486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(sessionId).string(), vectorToString(response).string());
19234486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = findSession(sessionId);
19334486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index == kNotFound) {
19434486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Invalid sessionId");
19534486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
19634486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
19734486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (response.size() == 0) {
19834486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
19934486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
20034486800a022857b45c3882624449980c5cb6907Jeff Tinker
20134486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by mock plugin and verifed cts test app
20234486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] response            -> mock-response
20334486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-response"), response);
20434486800a022857b45c3882624449980c5cb6907Jeff Tinker
20534486800a022857b45c3882624449980c5cb6907Jeff Tinker        const size_t kKeySetIdSize = 8;
20634486800a022857b45c3882624449980c5cb6907Jeff Tinker
20734486800a022857b45c3882624449980c5cb6907Jeff Tinker        for (size_t i = 0; i < kKeySetIdSize / sizeof(long); i++) {
20834486800a022857b45c3882624449980c5cb6907Jeff Tinker            long r = random();
20934486800a022857b45c3882624449980c5cb6907Jeff Tinker            keySetId.appendArray((uint8_t *)&r, sizeof(long));
21034486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
21134486800a022857b45c3882624449980c5cb6907Jeff Tinker        mKeySets.add(keySetId);
21234486800a022857b45c3882624449980c5cb6907Jeff Tinker
21334486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
21434486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
21534486800a022857b45c3882624449980c5cb6907Jeff Tinker
21634486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::removeKeys(Vector<uint8_t> const &keySetId)
21734486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
21834486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
21934486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::removeKeys(keySetId=%s)",
22034486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(keySetId).string());
22134486800a022857b45c3882624449980c5cb6907Jeff Tinker
22234486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = findKeySet(keySetId);
22334486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index == kNotFound) {
22434486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Invalid keySetId");
22534486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
22634486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
22734486800a022857b45c3882624449980c5cb6907Jeff Tinker        mKeySets.removeAt(index);
22834486800a022857b45c3882624449980c5cb6907Jeff Tinker
22934486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
23034486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
23134486800a022857b45c3882624449980c5cb6907Jeff Tinker
23234486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::restoreKeys(Vector<uint8_t> const &sessionId,
23334486800a022857b45c3882624449980c5cb6907Jeff Tinker                                        Vector<uint8_t> const &keySetId)
23434486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
23534486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
23634486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::restoreKeys(sessionId=%s, keySetId=%s)",
23734486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(sessionId).string(),
23834486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(keySetId).string());
23934486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = findSession(sessionId);
24034486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index == kNotFound) {
24134486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Invalid sessionId");
24234486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
24334486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
24434486800a022857b45c3882624449980c5cb6907Jeff Tinker
24534486800a022857b45c3882624449980c5cb6907Jeff Tinker        index = findKeySet(keySetId);
24634486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index == kNotFound) {
24734486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Invalid keySetId");
24834486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
24934486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
25034486800a022857b45c3882624449980c5cb6907Jeff Tinker
25134486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
25234486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
25334486800a022857b45c3882624449980c5cb6907Jeff Tinker
25434486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::queryKeyStatus(Vector<uint8_t> const &sessionId,
25534486800a022857b45c3882624449980c5cb6907Jeff Tinker                                               KeyedVector<String8, String8> &infoMap) const
25634486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
25734486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::queryKeyStatus(sessionId=%s)",
25834486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(sessionId).string());
25934486800a022857b45c3882624449980c5cb6907Jeff Tinker
26034486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = findSession(sessionId);
26134486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index == kNotFound) {
26234486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Invalid sessionId");
26334486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
26434486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
26534486800a022857b45c3882624449980c5cb6907Jeff Tinker
26634486800a022857b45c3882624449980c5cb6907Jeff Tinker        infoMap.add(String8("purchaseDuration"), String8("1000"));
26734486800a022857b45c3882624449980c5cb6907Jeff Tinker        infoMap.add(String8("licenseDuration"), String8("100"));
26834486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
26934486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
27034486800a022857b45c3882624449980c5cb6907Jeff Tinker
2712fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker    status_t MockDrmPlugin::getProvisionRequest(String8 const & /* certType */,
2722fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker                                                String8 const & /* certAuthority */,
27368d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker                                                Vector<uint8_t> &request,
27434486800a022857b45c3882624449980c5cb6907Jeff Tinker                                                String8 &defaultUrl)
27534486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
27634486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
27734486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::getProvisionRequest()");
27834486800a022857b45c3882624449980c5cb6907Jeff Tinker
27934486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by cts test app returned from mock plugin
28034486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] mock-request       -> request
28134486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   string mock-default-url   -> defaultUrl
28234486800a022857b45c3882624449980c5cb6907Jeff Tinker
28334486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = mByteArrayProperties.indexOfKey(String8("mock-request"));
28434486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index < 0) {
28534486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Missing 'mock-request' parameter for mock");
28634486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
28734486800a022857b45c3882624449980c5cb6907Jeff Tinker        } else {
28834486800a022857b45c3882624449980c5cb6907Jeff Tinker            request = mByteArrayProperties.valueAt(index);
28934486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
29034486800a022857b45c3882624449980c5cb6907Jeff Tinker
29134486800a022857b45c3882624449980c5cb6907Jeff Tinker        index = mStringProperties.indexOfKey(String8("mock-defaultUrl"));
29234486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index < 0) {
29334486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Missing 'mock-defaultUrl' parameter for mock");
29434486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
29534486800a022857b45c3882624449980c5cb6907Jeff Tinker        } else {
29634486800a022857b45c3882624449980c5cb6907Jeff Tinker            defaultUrl = mStringProperties.valueAt(index);
29734486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
29834486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
29934486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
30034486800a022857b45c3882624449980c5cb6907Jeff Tinker
30168d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker    status_t MockDrmPlugin::provideProvisionResponse(Vector<uint8_t> const &response,
3022fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker                                                     Vector<uint8_t> & /* certificate */,
3032fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker                                                     Vector<uint8_t> & /* wrappedKey */)
30434486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
30534486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
30634486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::provideProvisionResponse(%s)",
30734486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(response).string());
30834486800a022857b45c3882624449980c5cb6907Jeff Tinker
30934486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by mock plugin and verifed cts test app
31034486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] response            -> mock-response
31134486800a022857b45c3882624449980c5cb6907Jeff Tinker
31234486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-response"), response);
31334486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
31434486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
31534486800a022857b45c3882624449980c5cb6907Jeff Tinker
3162fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker    status_t MockDrmPlugin::getSecureStop(Vector<uint8_t> const & /* ssid */,
3172fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker                                          Vector<uint8_t> & secureStop)
31885c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker    {
31985c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker        Mutex::Autolock lock(mLock);
32085c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker        ALOGD("MockDrmPlugin::getSecureStop()");
32185c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker
32285c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker        // Properties used in mock test, set by cts test app returned from mock plugin
32385c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker        //   byte[] mock-secure-stop  -> first secure stop in list
32485c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker
32585c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker        ssize_t index = mByteArrayProperties.indexOfKey(String8("mock-secure-stop"));
32685c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker        if (index < 0) {
32785c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker            ALOGD("Missing 'mock-secure-stop' parameter for mock");
32885c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker            return BAD_VALUE;
32985c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker        } else {
33085c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker            secureStop = mByteArrayProperties.valueAt(index);
33185c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker        }
33285c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker        return OK;
33385c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker    }
33485c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker
33534486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::getSecureStops(List<Vector<uint8_t> > &secureStops)
33634486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
33734486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
33834486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::getSecureStops()");
33934486800a022857b45c3882624449980c5cb6907Jeff Tinker
34034486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by cts test app returned from mock plugin
34134486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] mock-secure-stop1  -> first secure stop in list
34234486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] mock-secure-stop2  -> second secure stop in list
34334486800a022857b45c3882624449980c5cb6907Jeff Tinker
34434486800a022857b45c3882624449980c5cb6907Jeff Tinker        Vector<uint8_t> ss1, ss2;
34534486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = mByteArrayProperties.indexOfKey(String8("mock-secure-stop1"));
34634486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index < 0) {
34734486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Missing 'mock-secure-stop1' parameter for mock");
34834486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
34934486800a022857b45c3882624449980c5cb6907Jeff Tinker        } else {
35034486800a022857b45c3882624449980c5cb6907Jeff Tinker            ss1 = mByteArrayProperties.valueAt(index);
35134486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
35234486800a022857b45c3882624449980c5cb6907Jeff Tinker
35334486800a022857b45c3882624449980c5cb6907Jeff Tinker        index = mByteArrayProperties.indexOfKey(String8("mock-secure-stop2"));
35434486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index < 0) {
35534486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Missing 'mock-secure-stop2' parameter for mock");
35634486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
35734486800a022857b45c3882624449980c5cb6907Jeff Tinker        } else {
35834486800a022857b45c3882624449980c5cb6907Jeff Tinker            ss2 = mByteArrayProperties.valueAt(index);
35934486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
36034486800a022857b45c3882624449980c5cb6907Jeff Tinker
36134486800a022857b45c3882624449980c5cb6907Jeff Tinker        secureStops.push_back(ss1);
36234486800a022857b45c3882624449980c5cb6907Jeff Tinker        secureStops.push_back(ss2);
36334486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
36434486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
36534486800a022857b45c3882624449980c5cb6907Jeff Tinker
36634486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::releaseSecureStops(Vector<uint8_t> const &ssRelease)
36734486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
36834486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
36934486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::releaseSecureStops(%s)",
37034486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(ssRelease).string());
37134486800a022857b45c3882624449980c5cb6907Jeff Tinker
37234486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by mock plugin and verifed cts test app
37334486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] secure-stop-release  -> mock-ssrelease
37434486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-ssrelease"), ssRelease);
37534486800a022857b45c3882624449980c5cb6907Jeff Tinker
37634486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
37785c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker    }
37885c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker
37985c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker    status_t MockDrmPlugin::releaseAllSecureStops()
38085c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker    {
38185c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker        Mutex::Autolock lock(mLock);
38285c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker        ALOGD("MockDrmPlugin::releaseAllSecureStops()");
38385c737a67e1b679251fce7f456ae79eefdc15e07Jeff Tinker        return OK;
38434486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
38534486800a022857b45c3882624449980c5cb6907Jeff Tinker
38634486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::getPropertyString(String8 const &name, String8 &value) const
38734486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
38834486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::getPropertyString(name=%s)", name.string());
38934486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = mStringProperties.indexOfKey(name);
39034486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index < 0) {
39134486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("no property for '%s'", name.string());
39234486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
39334486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
39434486800a022857b45c3882624449980c5cb6907Jeff Tinker        value = mStringProperties.valueAt(index);
39534486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
39634486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
39734486800a022857b45c3882624449980c5cb6907Jeff Tinker
39834486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::getPropertyByteArray(String8 const &name,
39934486800a022857b45c3882624449980c5cb6907Jeff Tinker                                                 Vector<uint8_t> &value) const
40034486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
40134486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::getPropertyByteArray(name=%s)", name.string());
40234486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = mByteArrayProperties.indexOfKey(name);
40334486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index < 0) {
40434486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("no property for '%s'", name.string());
40534486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
40634486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
40734486800a022857b45c3882624449980c5cb6907Jeff Tinker        value = mByteArrayProperties.valueAt(index);
40834486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
40934486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
41034486800a022857b45c3882624449980c5cb6907Jeff Tinker
41134486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::setPropertyString(String8 const &name,
41234486800a022857b45c3882624449980c5cb6907Jeff Tinker                                              String8 const &value)
41334486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
41434486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
41534486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::setPropertyString(name=%s, value=%s)",
41634486800a022857b45c3882624449980c5cb6907Jeff Tinker              name.string(), value.string());
41734486800a022857b45c3882624449980c5cb6907Jeff Tinker
41834486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (name == "mock-send-event") {
41934486800a022857b45c3882624449980c5cb6907Jeff Tinker            unsigned code, extra;
42034486800a022857b45c3882624449980c5cb6907Jeff Tinker            sscanf(value.string(), "%d %d", &code, &extra);
42134486800a022857b45c3882624449980c5cb6907Jeff Tinker            DrmPlugin::EventType eventType = (DrmPlugin::EventType)code;
42234486800a022857b45c3882624449980c5cb6907Jeff Tinker
42334486800a022857b45c3882624449980c5cb6907Jeff Tinker            Vector<uint8_t> const *pSessionId = NULL;
42434486800a022857b45c3882624449980c5cb6907Jeff Tinker            ssize_t index = mByteArrayProperties.indexOfKey(String8("mock-event-session-id"));
42534486800a022857b45c3882624449980c5cb6907Jeff Tinker            if (index >= 0) {
42634486800a022857b45c3882624449980c5cb6907Jeff Tinker                pSessionId = &mByteArrayProperties[index];
42734486800a022857b45c3882624449980c5cb6907Jeff Tinker            }
42834486800a022857b45c3882624449980c5cb6907Jeff Tinker
42934486800a022857b45c3882624449980c5cb6907Jeff Tinker            Vector<uint8_t> const *pData = NULL;
43034486800a022857b45c3882624449980c5cb6907Jeff Tinker            index = mByteArrayProperties.indexOfKey(String8("mock-event-data"));
43134486800a022857b45c3882624449980c5cb6907Jeff Tinker            if (index >= 0) {
43234486800a022857b45c3882624449980c5cb6907Jeff Tinker                pData = &mByteArrayProperties[index];
43334486800a022857b45c3882624449980c5cb6907Jeff Tinker            }
43434486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("sending event from mock drm plugin: %d %d %s %s",
43534486800a022857b45c3882624449980c5cb6907Jeff Tinker                  (int)code, extra, pSessionId ? vectorToString(*pSessionId) : "{}",
43634486800a022857b45c3882624449980c5cb6907Jeff Tinker                  pData ? vectorToString(*pData) : "{}");
43734486800a022857b45c3882624449980c5cb6907Jeff Tinker
43834486800a022857b45c3882624449980c5cb6907Jeff Tinker            sendEvent(eventType, extra, pSessionId, pData);
4392fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker        } else if (name == "mock-send-expiration-update") {
4402fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            int64_t expiryTimeMS;
4412fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            sscanf(value.string(), "%jd", &expiryTimeMS);
4422fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker
4432fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            Vector<uint8_t> const *pSessionId = NULL;
4442fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            ssize_t index = mByteArrayProperties.indexOfKey(String8("mock-event-session-id"));
4452fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            if (index >= 0) {
4462fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker                pSessionId = &mByteArrayProperties[index];
4472fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            }
4482fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker
4492fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            ALOGD("sending expiration-update from mock drm plugin: %jd %s",
4502fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker                  expiryTimeMS, pSessionId ? vectorToString(*pSessionId) : "{}");
4512fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker
4522fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            sendExpirationUpdate(pSessionId, expiryTimeMS);
4532fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker        } else if (name == "mock-send-keys-change") {
4542fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            Vector<uint8_t> const *pSessionId = NULL;
4552fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            ssize_t index = mByteArrayProperties.indexOfKey(String8("mock-event-session-id"));
4562fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            if (index >= 0) {
4572fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker                pSessionId = &mByteArrayProperties[index];
4582fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            }
4592fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker
4602fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            ALOGD("sending keys-change from mock drm plugin: %s",
4612fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker                  pSessionId ? vectorToString(*pSessionId) : "{}");
4622fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker
4632fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            Vector<DrmPlugin::KeyStatus> keyStatusList;
4642fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            DrmPlugin::KeyStatus keyStatus;
4652fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            uint8_t keyId1[] = {'k', 'e', 'y', '1'};
4662fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mKeyId.clear();
4672fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mKeyId.appendArray(keyId1, sizeof(keyId1));
4682fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mType = DrmPlugin::kKeyStatusType_Usable;
4692fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatusList.add(keyStatus);
4702fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker
4712fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            uint8_t keyId2[] = {'k', 'e', 'y', '2'};
4722fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mKeyId.clear();
4732fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mKeyId.appendArray(keyId2, sizeof(keyId2));
4742fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mType = DrmPlugin::kKeyStatusType_Expired;
4752fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatusList.add(keyStatus);
4762fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker
4772fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            uint8_t keyId3[] = {'k', 'e', 'y', '3'};
4782fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mKeyId.clear();
4792fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mKeyId.appendArray(keyId3, sizeof(keyId3));
4802fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mType = DrmPlugin::kKeyStatusType_OutputNotAllowed;
4812fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatusList.add(keyStatus);
4822fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker
4832fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            uint8_t keyId4[] = {'k', 'e', 'y', '4'};
4842fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mKeyId.clear();
4852fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mKeyId.appendArray(keyId4, sizeof(keyId4));
4862fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mType = DrmPlugin::kKeyStatusType_StatusPending;
4872fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatusList.add(keyStatus);
4882fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker
4892fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            uint8_t keyId5[] = {'k', 'e', 'y', '5'};
4902fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mKeyId.clear();
4912fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mKeyId.appendArray(keyId5, sizeof(keyId5));
4922fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatus.mType = DrmPlugin::kKeyStatusType_InternalError;
4932fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            keyStatusList.add(keyStatus);
4942fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker
4952fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            sendKeysChange(pSessionId, &keyStatusList, true);
49634486800a022857b45c3882624449980c5cb6907Jeff Tinker        } else {
49734486800a022857b45c3882624449980c5cb6907Jeff Tinker            mStringProperties.add(name, value);
49834486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
49934486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
50034486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
50134486800a022857b45c3882624449980c5cb6907Jeff Tinker
50234486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::setPropertyByteArray(String8 const &name,
50334486800a022857b45c3882624449980c5cb6907Jeff Tinker                                                 Vector<uint8_t> const &value)
50434486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
50534486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
50634486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::setPropertyByteArray(name=%s, value=%s)",
50734486800a022857b45c3882624449980c5cb6907Jeff Tinker              name.string(), vectorToString(value).string());
50834486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(name, value);
50934486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
51034486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
51134486800a022857b45c3882624449980c5cb6907Jeff Tinker
51234486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::setCipherAlgorithm(Vector<uint8_t> const &sessionId,
51334486800a022857b45c3882624449980c5cb6907Jeff Tinker                                               String8 const &algorithm)
51434486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
51534486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
51634486800a022857b45c3882624449980c5cb6907Jeff Tinker
51734486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::setCipherAlgorithm(sessionId=%s, algorithm=%s)",
51834486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(sessionId).string(), algorithm.string());
51934486800a022857b45c3882624449980c5cb6907Jeff Tinker
52034486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = findSession(sessionId);
52134486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index == kNotFound) {
52234486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Invalid sessionId");
52334486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
52434486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
52534486800a022857b45c3882624449980c5cb6907Jeff Tinker
52634486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (algorithm == "AES/CBC/NoPadding") {
52734486800a022857b45c3882624449980c5cb6907Jeff Tinker            return OK;
52834486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
52934486800a022857b45c3882624449980c5cb6907Jeff Tinker        return BAD_VALUE;
53034486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
53134486800a022857b45c3882624449980c5cb6907Jeff Tinker
53234486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::setMacAlgorithm(Vector<uint8_t> const &sessionId,
53334486800a022857b45c3882624449980c5cb6907Jeff Tinker                                            String8 const &algorithm)
53434486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
53534486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
53634486800a022857b45c3882624449980c5cb6907Jeff Tinker
53734486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::setMacAlgorithm(sessionId=%s, algorithm=%s)",
53834486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(sessionId).string(), algorithm.string());
53934486800a022857b45c3882624449980c5cb6907Jeff Tinker
54034486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = findSession(sessionId);
54134486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index == kNotFound) {
54234486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Invalid sessionId");
54334486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
54434486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
54534486800a022857b45c3882624449980c5cb6907Jeff Tinker
54634486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (algorithm == "HmacSHA256") {
54734486800a022857b45c3882624449980c5cb6907Jeff Tinker            return OK;
54834486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
54934486800a022857b45c3882624449980c5cb6907Jeff Tinker        return BAD_VALUE;
55034486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
55134486800a022857b45c3882624449980c5cb6907Jeff Tinker
55234486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::encrypt(Vector<uint8_t> const &sessionId,
55334486800a022857b45c3882624449980c5cb6907Jeff Tinker                                    Vector<uint8_t> const &keyId,
55434486800a022857b45c3882624449980c5cb6907Jeff Tinker                                    Vector<uint8_t> const &input,
55534486800a022857b45c3882624449980c5cb6907Jeff Tinker                                    Vector<uint8_t> const &iv,
55634486800a022857b45c3882624449980c5cb6907Jeff Tinker                                    Vector<uint8_t> &output)
55734486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
55834486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
55934486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::encrypt(sessionId=%s, keyId=%s, input=%s, iv=%s)",
56034486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(sessionId).string(),
56134486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(keyId).string(),
56234486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(input).string(),
56334486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(iv).string());
56434486800a022857b45c3882624449980c5cb6907Jeff Tinker
56534486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = findSession(sessionId);
56634486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index == kNotFound) {
56734486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Invalid sessionId");
56834486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
56934486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
57034486800a022857b45c3882624449980c5cb6907Jeff Tinker
57134486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by mock plugin and verifed cts test app
57234486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] keyId              -> mock-keyid
57334486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] input              -> mock-input
57434486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] iv                 -> mock-iv
57534486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-keyid"), keyId);
57634486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-input"), input);
57734486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-iv"), iv);
57834486800a022857b45c3882624449980c5cb6907Jeff Tinker
57934486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by cts test app returned from mock plugin
58034486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] mock-output        -> output
58134486800a022857b45c3882624449980c5cb6907Jeff Tinker        index = mByteArrayProperties.indexOfKey(String8("mock-output"));
58234486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index < 0) {
58334486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Missing 'mock-request' parameter for mock");
58434486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
58534486800a022857b45c3882624449980c5cb6907Jeff Tinker        } else {
58634486800a022857b45c3882624449980c5cb6907Jeff Tinker            output = mByteArrayProperties.valueAt(index);
58734486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
58834486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
58934486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
59034486800a022857b45c3882624449980c5cb6907Jeff Tinker
59134486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::decrypt(Vector<uint8_t> const &sessionId,
59234486800a022857b45c3882624449980c5cb6907Jeff Tinker                                    Vector<uint8_t> const &keyId,
59334486800a022857b45c3882624449980c5cb6907Jeff Tinker                                    Vector<uint8_t> const &input,
59434486800a022857b45c3882624449980c5cb6907Jeff Tinker                                    Vector<uint8_t> const &iv,
59534486800a022857b45c3882624449980c5cb6907Jeff Tinker                                    Vector<uint8_t> &output)
59634486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
59734486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
59834486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::decrypt(sessionId=%s, keyId=%s, input=%s, iv=%s)",
59934486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(sessionId).string(),
60034486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(keyId).string(),
60134486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(input).string(),
60234486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(iv).string());
60334486800a022857b45c3882624449980c5cb6907Jeff Tinker
60434486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = findSession(sessionId);
60534486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index == kNotFound) {
60634486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Invalid sessionId");
60734486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
60834486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
60934486800a022857b45c3882624449980c5cb6907Jeff Tinker
61034486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by mock plugin and verifed cts test app
61134486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] keyId              -> mock-keyid
61234486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] input              -> mock-input
61334486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] iv                 -> mock-iv
61434486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-keyid"), keyId);
61534486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-input"), input);
61634486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-iv"), iv);
61734486800a022857b45c3882624449980c5cb6907Jeff Tinker
61834486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by cts test app returned from mock plugin
61934486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] mock-output        -> output
62034486800a022857b45c3882624449980c5cb6907Jeff Tinker        index = mByteArrayProperties.indexOfKey(String8("mock-output"));
62134486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index < 0) {
62234486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Missing 'mock-request' parameter for mock");
62334486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
62434486800a022857b45c3882624449980c5cb6907Jeff Tinker        } else {
62534486800a022857b45c3882624449980c5cb6907Jeff Tinker            output = mByteArrayProperties.valueAt(index);
62634486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
62734486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
62834486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
62934486800a022857b45c3882624449980c5cb6907Jeff Tinker
63034486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::sign(Vector<uint8_t> const &sessionId,
63134486800a022857b45c3882624449980c5cb6907Jeff Tinker                                 Vector<uint8_t> const &keyId,
63234486800a022857b45c3882624449980c5cb6907Jeff Tinker                                 Vector<uint8_t> const &message,
63334486800a022857b45c3882624449980c5cb6907Jeff Tinker                                 Vector<uint8_t> &signature)
63434486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
63534486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
63634486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::sign(sessionId=%s, keyId=%s, message=%s)",
63734486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(sessionId).string(),
63834486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(keyId).string(),
63934486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(message).string());
64034486800a022857b45c3882624449980c5cb6907Jeff Tinker
64134486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = findSession(sessionId);
64234486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index == kNotFound) {
64334486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Invalid sessionId");
64434486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
64534486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
64634486800a022857b45c3882624449980c5cb6907Jeff Tinker
64734486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by mock plugin and verifed cts test app
64834486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] keyId              -> mock-keyid
64934486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] message            -> mock-message
65034486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-keyid"), keyId);
65134486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-message"), message);
65234486800a022857b45c3882624449980c5cb6907Jeff Tinker
65334486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by cts test app returned from mock plugin
65434486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] mock-signature        -> signature
65534486800a022857b45c3882624449980c5cb6907Jeff Tinker        index = mByteArrayProperties.indexOfKey(String8("mock-signature"));
65634486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index < 0) {
65734486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Missing 'mock-request' parameter for mock");
65834486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
65934486800a022857b45c3882624449980c5cb6907Jeff Tinker        } else {
66034486800a022857b45c3882624449980c5cb6907Jeff Tinker            signature = mByteArrayProperties.valueAt(index);
66134486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
66234486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
66334486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
66434486800a022857b45c3882624449980c5cb6907Jeff Tinker
66534486800a022857b45c3882624449980c5cb6907Jeff Tinker    status_t MockDrmPlugin::verify(Vector<uint8_t> const &sessionId,
66634486800a022857b45c3882624449980c5cb6907Jeff Tinker                                   Vector<uint8_t> const &keyId,
66734486800a022857b45c3882624449980c5cb6907Jeff Tinker                                   Vector<uint8_t> const &message,
66834486800a022857b45c3882624449980c5cb6907Jeff Tinker                                   Vector<uint8_t> const &signature,
66934486800a022857b45c3882624449980c5cb6907Jeff Tinker                                   bool &match)
67034486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
67134486800a022857b45c3882624449980c5cb6907Jeff Tinker        Mutex::Autolock lock(mLock);
67234486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockDrmPlugin::verify(sessionId=%s, keyId=%s, message=%s, signature=%s)",
67334486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(sessionId).string(),
67434486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(keyId).string(),
67534486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(message).string(),
67634486800a022857b45c3882624449980c5cb6907Jeff Tinker              vectorToString(signature).string());
67734486800a022857b45c3882624449980c5cb6907Jeff Tinker
67834486800a022857b45c3882624449980c5cb6907Jeff Tinker        ssize_t index = findSession(sessionId);
67934486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index == kNotFound) {
68034486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Invalid sessionId");
68134486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
68234486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
68334486800a022857b45c3882624449980c5cb6907Jeff Tinker
68434486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by mock plugin and verifed cts test app
68534486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] keyId              -> mock-keyid
68634486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] message            -> mock-message
68734486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   byte[] signature          -> mock-signature
68834486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-keyid"), keyId);
68934486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-message"), message);
69034486800a022857b45c3882624449980c5cb6907Jeff Tinker        mByteArrayProperties.add(String8("mock-signature"), signature);
69134486800a022857b45c3882624449980c5cb6907Jeff Tinker
69234486800a022857b45c3882624449980c5cb6907Jeff Tinker        // Properties used in mock test, set by cts test app returned from mock plugin
69334486800a022857b45c3882624449980c5cb6907Jeff Tinker        //   String mock-match "1" or "0"         -> match
69434486800a022857b45c3882624449980c5cb6907Jeff Tinker        index = mStringProperties.indexOfKey(String8("mock-match"));
69534486800a022857b45c3882624449980c5cb6907Jeff Tinker        if (index < 0) {
69634486800a022857b45c3882624449980c5cb6907Jeff Tinker            ALOGD("Missing 'mock-request' parameter for mock");
69734486800a022857b45c3882624449980c5cb6907Jeff Tinker            return BAD_VALUE;
69834486800a022857b45c3882624449980c5cb6907Jeff Tinker        } else {
69934486800a022857b45c3882624449980c5cb6907Jeff Tinker            match = atol(mStringProperties.valueAt(index).string());
70034486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
70134486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
70234486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
70334486800a022857b45c3882624449980c5cb6907Jeff Tinker
70468d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker    status_t MockDrmPlugin::signRSA(Vector<uint8_t> const &sessionId,
70568d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker                                    String8 const &algorithm,
70668d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker                                    Vector<uint8_t> const &message,
70768d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker                                    Vector<uint8_t> const &wrappedKey,
70868d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker                                    Vector<uint8_t> &signature)
70968d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker    {
71068d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker        Mutex::Autolock lock(mLock);
71168d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker        ALOGD("MockDrmPlugin::signRSA(sessionId=%s, algorithm=%s, keyId=%s, "
71268d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker              "message=%s, signature=%s)",
71368d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker              vectorToString(sessionId).string(),
71468d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker              algorithm.string(),
71568d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker              vectorToString(message).string(),
71668d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker              vectorToString(wrappedKey).string(),
71768d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker              vectorToString(signature).string());
71868d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker
71968d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker        // Properties used in mock test, set by mock plugin and verifed cts test app
72068d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker        //   byte[] wrappedKey         -> mock-wrappedkey
72168d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker        //   byte[] message            -> mock-message
72268d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker        //   byte[] signature          -> mock-signature
72368d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker        mByteArrayProperties.add(String8("mock-sessionid"), sessionId);
72468d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker        mStringProperties.add(String8("mock-algorithm"), algorithm);
72568d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker        mByteArrayProperties.add(String8("mock-message"), message);
72668d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker        mByteArrayProperties.add(String8("mock-wrappedkey"), wrappedKey);
72768d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker        mByteArrayProperties.add(String8("mock-signature"), signature);
72868d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker        return OK;
72968d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker    }
73068d9d71a792deed75d32fe13febc07c9c12c8449Jeff Tinker
73134486800a022857b45c3882624449980c5cb6907Jeff Tinker    ssize_t MockDrmPlugin::findSession(Vector<uint8_t> const &sessionId) const
73234486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
73368b3d9f49e68a11af5225175dc9e60ce88819e84Edwin Wong        ALOGD("findSession: nsessions=%u, size=%u", mSessions.size(), sessionId.size());
73434486800a022857b45c3882624449980c5cb6907Jeff Tinker        for (size_t i = 0; i < mSessions.size(); ++i) {
73534486800a022857b45c3882624449980c5cb6907Jeff Tinker            if (memcmp(mSessions[i].array(), sessionId.array(), sessionId.size()) == 0) {
73634486800a022857b45c3882624449980c5cb6907Jeff Tinker                return i;
73734486800a022857b45c3882624449980c5cb6907Jeff Tinker            }
73834486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
73934486800a022857b45c3882624449980c5cb6907Jeff Tinker        return kNotFound;
74034486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
74134486800a022857b45c3882624449980c5cb6907Jeff Tinker
74234486800a022857b45c3882624449980c5cb6907Jeff Tinker    ssize_t MockDrmPlugin::findKeySet(Vector<uint8_t> const &keySetId) const
74334486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
74468b3d9f49e68a11af5225175dc9e60ce88819e84Edwin Wong        ALOGD("findKeySet: nkeySets=%u, size=%u", mKeySets.size(), keySetId.size());
74534486800a022857b45c3882624449980c5cb6907Jeff Tinker        for (size_t i = 0; i < mKeySets.size(); ++i) {
74634486800a022857b45c3882624449980c5cb6907Jeff Tinker            if (memcmp(mKeySets[i].array(), keySetId.array(), keySetId.size()) == 0) {
74734486800a022857b45c3882624449980c5cb6907Jeff Tinker                return i;
74834486800a022857b45c3882624449980c5cb6907Jeff Tinker            }
74934486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
75034486800a022857b45c3882624449980c5cb6907Jeff Tinker        return kNotFound;
75134486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
75234486800a022857b45c3882624449980c5cb6907Jeff Tinker
75334486800a022857b45c3882624449980c5cb6907Jeff Tinker
75434486800a022857b45c3882624449980c5cb6907Jeff Tinker    // Conversion utilities
75534486800a022857b45c3882624449980c5cb6907Jeff Tinker    String8 MockDrmPlugin::vectorToString(Vector<uint8_t> const &vector) const
75634486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
75734486800a022857b45c3882624449980c5cb6907Jeff Tinker        return arrayToString(vector.array(), vector.size());
75834486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
75934486800a022857b45c3882624449980c5cb6907Jeff Tinker
76034486800a022857b45c3882624449980c5cb6907Jeff Tinker    String8 MockDrmPlugin::arrayToString(uint8_t const *array, size_t len) const
76134486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
76234486800a022857b45c3882624449980c5cb6907Jeff Tinker        String8 result("{ ");
76334486800a022857b45c3882624449980c5cb6907Jeff Tinker        for (size_t i = 0; i < len; i++) {
76434486800a022857b45c3882624449980c5cb6907Jeff Tinker            result.appendFormat("0x%02x ", array[i]);
76534486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
76634486800a022857b45c3882624449980c5cb6907Jeff Tinker        result += "}";
76734486800a022857b45c3882624449980c5cb6907Jeff Tinker        return result;
76834486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
76934486800a022857b45c3882624449980c5cb6907Jeff Tinker
7708c0164ca98bcc76f3656fb1078033bffd4a387deChih-Hung Hsieh    String8 MockDrmPlugin::stringMapToString(const KeyedVector<String8, String8>& map) const
77134486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
77234486800a022857b45c3882624449980c5cb6907Jeff Tinker        String8 result("{ ");
77334486800a022857b45c3882624449980c5cb6907Jeff Tinker        for (size_t i = 0; i < map.size(); i++) {
77434486800a022857b45c3882624449980c5cb6907Jeff Tinker            result.appendFormat("%s{name=%s, value=%s}", i > 0 ? ", " : "",
77534486800a022857b45c3882624449980c5cb6907Jeff Tinker                                map.keyAt(i).string(), map.valueAt(i).string());
77634486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
77734486800a022857b45c3882624449980c5cb6907Jeff Tinker        return result + " }";
77834486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
77934486800a022857b45c3882624449980c5cb6907Jeff Tinker
78034486800a022857b45c3882624449980c5cb6907Jeff Tinker    bool operator<(Vector<uint8_t> const &lhs, Vector<uint8_t> const &rhs) {
78134486800a022857b45c3882624449980c5cb6907Jeff Tinker        return lhs.size() < rhs.size() || (memcmp(lhs.array(), rhs.array(), lhs.size()) < 0);
78234486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
78334486800a022857b45c3882624449980c5cb6907Jeff Tinker
78434486800a022857b45c3882624449980c5cb6907Jeff Tinker    //
78534486800a022857b45c3882624449980c5cb6907Jeff Tinker    // Crypto Plugin
78634486800a022857b45c3882624449980c5cb6907Jeff Tinker    //
78734486800a022857b45c3882624449980c5cb6907Jeff Tinker
78834486800a022857b45c3882624449980c5cb6907Jeff Tinker    bool MockCryptoPlugin::requiresSecureDecoderComponent(const char *mime) const
78934486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
79034486800a022857b45c3882624449980c5cb6907Jeff Tinker        ALOGD("MockCryptoPlugin::requiresSecureDecoderComponent(mime=%s)", mime);
79134486800a022857b45c3882624449980c5cb6907Jeff Tinker        return false;
79234486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
79334486800a022857b45c3882624449980c5cb6907Jeff Tinker
79434486800a022857b45c3882624449980c5cb6907Jeff Tinker    ssize_t
79534486800a022857b45c3882624449980c5cb6907Jeff Tinker    MockCryptoPlugin::decrypt(bool secure, const uint8_t key[16], const uint8_t iv[16],
79618cb1eca504817b5b144a023ae2792d90e74c9a5Jeff Tinker            Mode mode, const Pattern &pattern, const void *srcPtr,
79718cb1eca504817b5b144a023ae2792d90e74c9a5Jeff Tinker            const SubSample *subSamples, size_t numSubSamples,
79818cb1eca504817b5b144a023ae2792d90e74c9a5Jeff Tinker            void *dstPtr, AString * /* errorDetailMsg */)
79934486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
80018cb1eca504817b5b144a023ae2792d90e74c9a5Jeff Tinker        ALOGD("MockCryptoPlugin::decrypt(secure=%d, key=%s, iv=%s, mode=%d, "
80118cb1eca504817b5b144a023ae2792d90e74c9a5Jeff Tinker              "pattern:{encryptBlocks=%d, skipBlocks=%d} src=%p, "
80234486800a022857b45c3882624449980c5cb6907Jeff Tinker              "subSamples=%s, dst=%p)",
80334486800a022857b45c3882624449980c5cb6907Jeff Tinker              (int)secure,
80434486800a022857b45c3882624449980c5cb6907Jeff Tinker              arrayToString(key, sizeof(key)).string(),
80534486800a022857b45c3882624449980c5cb6907Jeff Tinker              arrayToString(iv, sizeof(iv)).string(),
80618cb1eca504817b5b144a023ae2792d90e74c9a5Jeff Tinker              (int)mode, pattern.mEncryptBlocks, pattern.mSkipBlocks, srcPtr,
80734486800a022857b45c3882624449980c5cb6907Jeff Tinker              subSamplesToString(subSamples, numSubSamples).string(),
80834486800a022857b45c3882624449980c5cb6907Jeff Tinker              dstPtr);
80934486800a022857b45c3882624449980c5cb6907Jeff Tinker        return OK;
81034486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
81134486800a022857b45c3882624449980c5cb6907Jeff Tinker
81234486800a022857b45c3882624449980c5cb6907Jeff Tinker    // Conversion utilities
81334486800a022857b45c3882624449980c5cb6907Jeff Tinker    String8 MockCryptoPlugin::arrayToString(uint8_t const *array, size_t len) const
81434486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
81534486800a022857b45c3882624449980c5cb6907Jeff Tinker        String8 result("{ ");
81634486800a022857b45c3882624449980c5cb6907Jeff Tinker        for (size_t i = 0; i < len; i++) {
81734486800a022857b45c3882624449980c5cb6907Jeff Tinker            result.appendFormat("0x%02x ", array[i]);
81834486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
81934486800a022857b45c3882624449980c5cb6907Jeff Tinker        result += "}";
82034486800a022857b45c3882624449980c5cb6907Jeff Tinker        return result;
82134486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
82234486800a022857b45c3882624449980c5cb6907Jeff Tinker
82334486800a022857b45c3882624449980c5cb6907Jeff Tinker    String8 MockCryptoPlugin::subSamplesToString(SubSample const *subSamples,
82434486800a022857b45c3882624449980c5cb6907Jeff Tinker                                                 size_t numSubSamples) const
82534486800a022857b45c3882624449980c5cb6907Jeff Tinker    {
82634486800a022857b45c3882624449980c5cb6907Jeff Tinker        String8 result;
82734486800a022857b45c3882624449980c5cb6907Jeff Tinker        for (size_t i = 0; i < numSubSamples; i++) {
8282fb25c873e1a7f346bd8c7c072a85ca6a74b0e95Jeff Tinker            result.appendFormat("[%zu] {clear:%u, encrypted:%u} ", i,
82934486800a022857b45c3882624449980c5cb6907Jeff Tinker                                subSamples[i].mNumBytesOfClearData,
83034486800a022857b45c3882624449980c5cb6907Jeff Tinker                                subSamples[i].mNumBytesOfEncryptedData);
83134486800a022857b45c3882624449980c5cb6907Jeff Tinker        }
83234486800a022857b45c3882624449980c5cb6907Jeff Tinker        return result;
83334486800a022857b45c3882624449980c5cb6907Jeff Tinker    }
83434486800a022857b45c3882624449980c5cb6907Jeff Tinker
83534486800a022857b45c3882624449980c5cb6907Jeff Tinker};
836