1791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang/*
2791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * Copyright (C) 2017 The Android Open Source Project
3791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang *
4791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * Licensed under the Apache License, Version 2.0 (the "License");
5791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * you may not use this file except in compliance with the License.
6791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * You may obtain a copy of the License at
7791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang *
8791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang *      http://www.apache.org/licenses/LICENSE-2.0
9791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang *
10791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * Unless required by applicable law or agreed to in writing, software
11791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * distributed under the License is distributed on an "AS IS" BASIS,
12791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * See the License for the specific language governing permissions and
14791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang * limitations under the License.
15791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang */
16791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang
17791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang//#define LOG_NDEBUG 0
18791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#define LOG_TAG "ClearKeyFetcherTest"
19791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#include <utils/Log.h>
20791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#include <gtest/gtest.h>
21791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#include <stddef.h>
22791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#include <algorithm>
23791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#include <string>
24791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang
25791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#include "ClearKeyFetcher.h"
26791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#include "ClearKeyLicenseFetcher.h"
27791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang#include "protos/license_protos.pb.h"
28791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang
29791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangnamespace android {
30791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangnamespace clearkeycas {
31791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang
32791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangconst char *kTestAssetInJson =
33791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        "{                                                   "
34791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        "  \"id\": 21140844,                                 "
35791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        "  \"name\": \"Test Title\",                         "
36791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        "  \"lowercase_organization_name\": \"Android\",     "
37791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        "  \"asset_key\": {                                  "
38791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        "  \"encryption_key\": \"nezAr3CHFrmBR9R8Tedotw==\"  "
39791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        "  },                                                "
40791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        "  \"cas_type\": 1,                                  "
41791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        "  \"track_types\": [ ]                              "
42791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        "}                                                   " ;
43791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang
44791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangconst uint8_t kTestEcmContainer[] = {
45791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
46791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        0x00, 0x00, 0x27, 0x10, 0x02, 0x00, 0x01, 0x77,
47791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        0x01, 0x42, 0x95, 0x6c, 0x0e, 0xe3, 0x91, 0xbc,
48791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        0xfd, 0x05, 0xb1, 0x60, 0x4f, 0x17, 0x82, 0xa4,
49791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        0x86, 0x9b, 0x23, 0x56, 0x00, 0x01, 0x00, 0x00,
50791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        0x00, 0x01, 0x00, 0x00, 0x27, 0x10, 0x02, 0x00,
51791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        0x01, 0x77, 0x01, 0x42, 0x95, 0x6c, 0xd7, 0x43,
52791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        0x62, 0xf8, 0x1c, 0x62, 0x19, 0x05, 0xc7, 0x3a,
53791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        0x42, 0xcd, 0xfd, 0xd9, 0x13, 0x48,
54791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang};
55791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang
56791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangconst uint8_t kTestContentKey0[] = {
57791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        0x0e, 0xe3, 0x91, 0xbc, 0xfd, 0x05, 0xb1, 0x60,
58791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        0x4f, 0x17, 0x82, 0xa4, 0x86, 0x9b, 0x23, 0x56};
59791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang
60791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangconst uint8_t kTestContentKey1[] = {
61791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        0xd7, 0x43, 0x62, 0xf8, 0x1c, 0x62, 0x19, 0x05,
62791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang        0xc7, 0x3a, 0x42, 0xcd, 0xfd, 0xd9, 0x13, 0x48};
63791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang
64791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangconstexpr uint32_t kTestEcmCount = 2;
65791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang
66791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangclass ClearKeyFetcherTest : public testing::Test {
67791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangprotected:
68791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    virtual void SetUp();
69791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang
70791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangprotected:
71791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    std::unique_ptr<ClearKeyLicenseFetcher> license_fetcher_;
72791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    sp<ABuffer> ecm_;
73791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    sp<ABuffer> content_key_[kTestEcmCount];
74791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang};
75791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang
76791a1a206b56be8601a6fffd2614926e67d64790Chong Zhangvoid ClearKeyFetcherTest::SetUp() {
77791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    license_fetcher_.reset(new ClearKeyLicenseFetcher());
78791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    EXPECT_EQ(OK, license_fetcher_->Init(kTestAssetInJson));
79791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    ecm_ = new ABuffer((void*) (kTestEcmContainer), sizeof(kTestEcmContainer));
80791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    content_key_[0] = new ABuffer(
81791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang            (void*)kTestContentKey0, sizeof(kTestContentKey0));
82791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    content_key_[1] = new ABuffer(
83791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang            (void*)kTestContentKey1, sizeof(kTestContentKey1));
84791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang}
85791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang
86791a1a206b56be8601a6fffd2614926e67d64790Chong ZhangTEST_F(ClearKeyFetcherTest, Ctor) {
87791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    ClearKeyFetcher fetcher(std::move(license_fetcher_));
88791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang}
89791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang
90791a1a206b56be8601a6fffd2614926e67d64790Chong ZhangTEST_F(ClearKeyFetcherTest, Success) {
91791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    ClearKeyFetcher fetcher(std::move(license_fetcher_));
92791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    EXPECT_EQ(OK, fetcher.Init());
93791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    uint64_t asset_id;
94791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    std::vector<KeyFetcher::KeyInfo> keys;
95791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    EXPECT_EQ(OK, fetcher.ObtainKey(ecm_, &asset_id, &keys));
96791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    EXPECT_EQ(2, keys.size());
97791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    EXPECT_EQ(0, keys[0].key_id);
98791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    EXPECT_EQ(content_key_[0]->size(), keys[0].key_bytes->size());
99791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    EXPECT_EQ(0, memcmp(content_key_[0]->data(),
100791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang            keys[0].key_bytes->data(), content_key_[0]->size()));
101791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    EXPECT_EQ(1, keys[1].key_id);
102791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    EXPECT_EQ(content_key_[1]->size(), keys[1].key_bytes->size());
103791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang    EXPECT_EQ(0, memcmp(content_key_[1]->data(),
104791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang            keys[1].key_bytes->data(), content_key_[1]->size()));
105791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang}
106791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang
107791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang} // namespace clearkeycas
108791a1a206b56be8601a6fffd2614926e67d64790Chong Zhang} // namespace android
109