1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "ClearKeyFetcherTest"
19 #include <utils/Log.h>
20 #include <gtest/gtest.h>
21 #include <stddef.h>
22 #include <algorithm>
23 #include <string>
24
25 #include "ClearKeyFetcher.h"
26 #include "ClearKeyLicenseFetcher.h"
27 #include "protos/license_protos.pb.h"
28
29 namespace android {
30 namespace clearkeycas {
31
32 const char *kTestAssetInJson =
33 "{ "
34 " \"id\": 21140844, "
35 " \"name\": \"Test Title\", "
36 " \"lowercase_organization_name\": \"Android\", "
37 " \"asset_key\": { "
38 " \"encryption_key\": \"nezAr3CHFrmBR9R8Tedotw==\" "
39 " }, "
40 " \"cas_type\": 1, "
41 " \"track_types\": [ ] "
42 "} " ;
43
44 const uint8_t kTestEcmContainer[] = {
45 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
46 0x00, 0x00, 0x27, 0x10, 0x02, 0x00, 0x01, 0x77,
47 0x01, 0x42, 0x95, 0x6c, 0x0e, 0xe3, 0x91, 0xbc,
48 0xfd, 0x05, 0xb1, 0x60, 0x4f, 0x17, 0x82, 0xa4,
49 0x86, 0x9b, 0x23, 0x56, 0x00, 0x01, 0x00, 0x00,
50 0x00, 0x01, 0x00, 0x00, 0x27, 0x10, 0x02, 0x00,
51 0x01, 0x77, 0x01, 0x42, 0x95, 0x6c, 0xd7, 0x43,
52 0x62, 0xf8, 0x1c, 0x62, 0x19, 0x05, 0xc7, 0x3a,
53 0x42, 0xcd, 0xfd, 0xd9, 0x13, 0x48,
54 };
55
56 const uint8_t kTestContentKey0[] = {
57 0x0e, 0xe3, 0x91, 0xbc, 0xfd, 0x05, 0xb1, 0x60,
58 0x4f, 0x17, 0x82, 0xa4, 0x86, 0x9b, 0x23, 0x56};
59
60 const uint8_t kTestContentKey1[] = {
61 0xd7, 0x43, 0x62, 0xf8, 0x1c, 0x62, 0x19, 0x05,
62 0xc7, 0x3a, 0x42, 0xcd, 0xfd, 0xd9, 0x13, 0x48};
63
64 constexpr uint32_t kTestEcmCount = 2;
65
66 class ClearKeyFetcherTest : public testing::Test {
67 protected:
68 virtual void SetUp();
69
70 protected:
71 std::unique_ptr<ClearKeyLicenseFetcher> license_fetcher_;
72 sp<ABuffer> ecm_;
73 sp<ABuffer> content_key_[kTestEcmCount];
74 };
75
SetUp()76 void ClearKeyFetcherTest::SetUp() {
77 license_fetcher_.reset(new ClearKeyLicenseFetcher());
78 EXPECT_EQ(OK, license_fetcher_->Init(kTestAssetInJson));
79 ecm_ = new ABuffer((void*) (kTestEcmContainer), sizeof(kTestEcmContainer));
80 content_key_[0] = new ABuffer(
81 (void*)kTestContentKey0, sizeof(kTestContentKey0));
82 content_key_[1] = new ABuffer(
83 (void*)kTestContentKey1, sizeof(kTestContentKey1));
84 }
85
TEST_F(ClearKeyFetcherTest,Ctor)86 TEST_F(ClearKeyFetcherTest, Ctor) {
87 ClearKeyFetcher fetcher(std::move(license_fetcher_));
88 }
89
TEST_F(ClearKeyFetcherTest,Success)90 TEST_F(ClearKeyFetcherTest, Success) {
91 ClearKeyFetcher fetcher(std::move(license_fetcher_));
92 EXPECT_EQ(OK, fetcher.Init());
93 uint64_t asset_id;
94 std::vector<KeyFetcher::KeyInfo> keys;
95 EXPECT_EQ(OK, fetcher.ObtainKey(ecm_, &asset_id, &keys));
96 EXPECT_EQ(2U, keys.size());
97 EXPECT_EQ(0, keys[0].key_id);
98 EXPECT_EQ(content_key_[0]->size(), keys[0].key_bytes->size());
99 EXPECT_EQ(0, memcmp(content_key_[0]->data(),
100 keys[0].key_bytes->data(), content_key_[0]->size()));
101 EXPECT_EQ(1, keys[1].key_id);
102 EXPECT_EQ(content_key_[1]->size(), keys[1].key_bytes->size());
103 EXPECT_EQ(0, memcmp(content_key_[1]->data(),
104 keys[1].key_bytes->data(), content_key_[1]->size()));
105 }
106
107 } // namespace clearkeycas
108 } // namespace android
109