Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: media/mojo/clients/mojo_cdm_unittest.cc

Issue 2561263002: [eme] Reject CDM calls after connection error (Closed)
Patch Set: nit (+rebase for MediaKeys rename) Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/mojo/clients/mojo_cdm.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/test/test_message_loop.h" 11 #include "base/test/test_message_loop.h"
12 #include "media/base/cdm_config.h" 12 #include "media/base/cdm_config.h"
13 #include "media/base/content_decryption_module.h" 13 #include "media/base/content_decryption_module.h"
14 #include "media/base/mock_filters.h" 14 #include "media/base/mock_filters.h"
15 #include "media/cdm/default_cdm_factory.h" 15 #include "media/cdm/default_cdm_factory.h"
16 #include "media/mojo/clients/mojo_cdm.h" 16 #include "media/mojo/clients/mojo_cdm.h"
17 #include "media/mojo/interfaces/content_decryption_module.mojom.h" 17 #include "media/mojo/interfaces/content_decryption_module.mojom.h"
18 #include "media/mojo/services/mojo_cdm_service.h" 18 #include "media/mojo/services/mojo_cdm_service.h"
19 #include "media/mojo/services/mojo_cdm_service_context.h" 19 #include "media/mojo/services/mojo_cdm_service_context.h"
20 #include "mojo/public/cpp/bindings/interface_request.h" 20 #include "mojo/public/cpp/bindings/interface_request.h"
21 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
22 22
23 using ::testing::_;
23 using ::testing::StrictMock; 24 using ::testing::StrictMock;
24 25
26 MATCHER(NotEmpty, "") {
27 return !arg.empty();
28 }
29
25 namespace media { 30 namespace media {
26 31
27 namespace { 32 namespace {
28 33
29 const char kClearKeyKeySystem[] = "org.w3.clearkey"; 34 const char kClearKeyKeySystem[] = "org.w3.clearkey";
30 const char kTestSecurityOrigin[] = "https://www.test.com"; 35 const char kTestSecurityOrigin[] = "https://www.test.com";
31 36
37 // Random key ID used to create a session.
38 const uint8_t kKeyId[] = {
39 // base64 equivalent is AQIDBAUGBwgJCgsMDQ4PEA
40 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
41 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
42 };
43
32 } // namespace 44 } // namespace
33 45
34 class MojoCdmTest : public ::testing::Test { 46 class MojoCdmTest : public ::testing::Test {
35 public: 47 public:
36 enum ExpectedResult { SUCCESS, CONNECTION_ERROR }; 48 enum ExpectedResult { SUCCESS, CONNECTION_ERROR, FAILURE };
37 49
38 MojoCdmTest() 50 MojoCdmTest()
39 : mojo_cdm_service_(base::MakeUnique<MojoCdmService>( 51 : mojo_cdm_service_(base::MakeUnique<MojoCdmService>(
40 mojo_cdm_service_context_.GetWeakPtr(), 52 mojo_cdm_service_context_.GetWeakPtr(),
41 &cdm_factory_)), 53 &cdm_factory_)),
42 cdm_binding_(mojo_cdm_service_.get()) {} 54 cdm_binding_(mojo_cdm_service_.get()) {}
43 55
44 virtual ~MojoCdmTest() {} 56 virtual ~MojoCdmTest() {}
45 57
46 void Initialize(ExpectedResult expected_result) { 58 void Initialize(const std::string& key_system,
59 ExpectedResult expected_result) {
47 mojom::ContentDecryptionModulePtr remote_cdm; 60 mojom::ContentDecryptionModulePtr remote_cdm;
48 auto cdm_request = mojo::GetProxy(&remote_cdm); 61 auto cdm_request = mojo::GetProxy(&remote_cdm);
49 62
50 switch (expected_result) { 63 switch (expected_result) {
51 case SUCCESS: 64 case SUCCESS:
65 case FAILURE:
52 cdm_binding_.Bind(std::move(cdm_request)); 66 cdm_binding_.Bind(std::move(cdm_request));
53 break; 67 break;
54 case CONNECTION_ERROR: 68 case CONNECTION_ERROR:
55 cdm_request.ResetWithReason(0, "Request dropped for testing."); 69 cdm_request.ResetWithReason(0, "Request dropped for testing.");
56 break; 70 break;
57 } 71 }
58 72
59 MojoCdm::Create(kClearKeyKeySystem, GURL(kTestSecurityOrigin), CdmConfig(), 73 MojoCdm::Create(key_system, GURL(kTestSecurityOrigin), CdmConfig(),
60 std::move(remote_cdm), 74 std::move(remote_cdm),
61 base::Bind(&MockCdmClient::OnSessionMessage, 75 base::Bind(&MockCdmClient::OnSessionMessage,
62 base::Unretained(&cdm_client_)), 76 base::Unretained(&cdm_client_)),
63 base::Bind(&MockCdmClient::OnSessionClosed, 77 base::Bind(&MockCdmClient::OnSessionClosed,
64 base::Unretained(&cdm_client_)), 78 base::Unretained(&cdm_client_)),
65 base::Bind(&MockCdmClient::OnSessionKeysChange, 79 base::Bind(&MockCdmClient::OnSessionKeysChange,
66 base::Unretained(&cdm_client_)), 80 base::Unretained(&cdm_client_)),
67 base::Bind(&MockCdmClient::OnSessionExpirationUpdate, 81 base::Bind(&MockCdmClient::OnSessionExpirationUpdate,
68 base::Unretained(&cdm_client_)), 82 base::Unretained(&cdm_client_)),
69 base::Bind(&MojoCdmTest::OnCdmCreated, 83 base::Bind(&MojoCdmTest::OnCdmCreated,
70 base::Unretained(this), expected_result)); 84 base::Unretained(this), expected_result));
71 85
72 base::RunLoop().RunUntilIdle(); 86 base::RunLoop().RunUntilIdle();
73
74 if (expected_result == SUCCESS) {
75 EXPECT_TRUE(mojo_cdm_);
76 } else {
77 EXPECT_FALSE(mojo_cdm_);
78 }
79 } 87 }
80 88
81 void OnCdmCreated(ExpectedResult expected_result, 89 void OnCdmCreated(ExpectedResult expected_result,
82 const scoped_refptr<ContentDecryptionModule>& cdm, 90 const scoped_refptr<ContentDecryptionModule>& cdm,
83 const std::string& error_message) { 91 const std::string& error_message) {
84 if (!cdm) { 92 if (!cdm) {
93 EXPECT_NE(SUCCESS, expected_result);
85 DVLOG(1) << error_message; 94 DVLOG(1) << error_message;
86 return; 95 return;
87 } 96 }
88 97
89 EXPECT_EQ(SUCCESS, expected_result); 98 EXPECT_EQ(SUCCESS, expected_result);
90 mojo_cdm_ = cdm; 99 mojo_cdm_ = cdm;
91 } 100 }
92 101
102 void ForceConnectionError() {
103 // If there is an existing session it will get closed when the connection
104 // is broken.
105 if (!session_id_.empty()) {
106 EXPECT_CALL(cdm_client_, OnSessionClosed(session_id_));
107 }
108
109 cdm_binding_.CloseWithReason(2, "Test closed connection.");
110
111 base::RunLoop().RunUntilIdle();
112 }
113
114 void SetServerCertificateAndExpect(const std::vector<uint8_t>& certificate,
115 ExpectedResult expected_result) {
116 mojo_cdm_->SetServerCertificate(
117 certificate,
118 base::MakeUnique<MockCdmPromise>(expected_result == SUCCESS));
119
120 base::RunLoop().RunUntilIdle();
121 }
122
123 void CreateSessionAndExpect(EmeInitDataType data_type,
124 const std::vector<uint8_t>& key_id,
125 ExpectedResult expected_result) {
126 if (expected_result == SUCCESS) {
127 EXPECT_CALL(cdm_client_, OnSessionMessage(NotEmpty(), _, _));
128 }
129
130 mojo_cdm_->CreateSessionAndGenerateRequest(
131 ContentDecryptionModule::SessionType::TEMPORARY_SESSION, data_type,
132 key_id, base::MakeUnique<MockCdmSessionPromise>(
133 expected_result == SUCCESS, &session_id_));
134
135 base::RunLoop().RunUntilIdle();
136 }
137
138 void CloseSessionAndExpect(ExpectedResult expected_result) {
139 DCHECK(!session_id_.empty()) << "CloseSessionAndExpect() must be called "
140 "after a successful "
141 "CreateSessionAndExpect()";
142
143 if (expected_result == SUCCESS) {
144 EXPECT_CALL(cdm_client_, OnSessionClosed(session_id_));
145 }
146
147 mojo_cdm_->CloseSession(session_id_, base::MakeUnique<MockCdmPromise>(
148 expected_result == SUCCESS));
149
150 base::RunLoop().RunUntilIdle();
151 }
152
93 // Fixture members. 153 // Fixture members.
94 base::TestMessageLoop message_loop_; 154 base::TestMessageLoop message_loop_;
95 155
96 MojoCdmServiceContext mojo_cdm_service_context_; 156 MojoCdmServiceContext mojo_cdm_service_context_;
97 StrictMock<MockCdmClient> cdm_client_; 157 StrictMock<MockCdmClient> cdm_client_;
98 158
99 // TODO(jrummell): Use a MockCdmFactory to create a MockCdm here for more test 159 // TODO(jrummell): Use a MockCdmFactory to create a MockCdm here for more test
100 // coverage. 160 // coverage.
101 DefaultCdmFactory cdm_factory_; 161 DefaultCdmFactory cdm_factory_;
102 162
103 std::unique_ptr<MojoCdmService> mojo_cdm_service_; 163 std::unique_ptr<MojoCdmService> mojo_cdm_service_;
104 mojo::Binding<mojom::ContentDecryptionModule> cdm_binding_; 164 mojo::Binding<mojom::ContentDecryptionModule> cdm_binding_;
105 scoped_refptr<ContentDecryptionModule> mojo_cdm_; 165 scoped_refptr<ContentDecryptionModule> mojo_cdm_;
106 166
167 // |session_id_| is the latest successful result of calling CreateSession().
168 std::string session_id_;
169
107 private: 170 private:
108 DISALLOW_COPY_AND_ASSIGN(MojoCdmTest); 171 DISALLOW_COPY_AND_ASSIGN(MojoCdmTest);
109 }; 172 };
110 173
111 TEST_F(MojoCdmTest, Create_Success) { 174 TEST_F(MojoCdmTest, Create_Success) {
112 Initialize(SUCCESS); 175 Initialize(kClearKeyKeySystem, SUCCESS);
113 } 176 }
114 177
115 TEST_F(MojoCdmTest, Create_ConnectionError) { 178 TEST_F(MojoCdmTest, Create_ConnectionError) {
116 Initialize(CONNECTION_ERROR); 179 Initialize(kClearKeyKeySystem, CONNECTION_ERROR);
117 } 180 }
118 181
119 // TODO(xhwang): Add more test cases! 182 TEST_F(MojoCdmTest, Create_Failure) {
183 // This fails as DefaultCdmFactory only supports Clear Key.
184 Initialize("org.random.cdm", FAILURE);
185 }
186
187 TEST_F(MojoCdmTest, SetServerCertificate_AfterConnectionError) {
188 Initialize(kClearKeyKeySystem, SUCCESS);
189 ForceConnectionError();
190 SetServerCertificateAndExpect({0, 1, 2}, FAILURE);
191 }
192
193 TEST_F(MojoCdmTest, CreateSessionAndGenerateRequest_AfterConnectionError) {
194 std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId));
195
196 Initialize(kClearKeyKeySystem, SUCCESS);
197 ForceConnectionError();
198 CreateSessionAndExpect(EmeInitDataType::WEBM, key_id, FAILURE);
199 }
200
201 TEST_F(MojoCdmTest, CloseSession_Success) {
202 std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId));
203
204 Initialize(kClearKeyKeySystem, SUCCESS);
205 CreateSessionAndExpect(EmeInitDataType::WEBM, key_id, SUCCESS);
206 CloseSessionAndExpect(SUCCESS);
207 }
208
209 TEST_F(MojoCdmTest, CloseSession_AfterConnectionError) {
210 std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId));
211
212 Initialize(kClearKeyKeySystem, SUCCESS);
213 CreateSessionAndExpect(EmeInitDataType::WEBM, key_id, SUCCESS);
214 ForceConnectionError();
215 CloseSessionAndExpect(FAILURE);
216 }
120 217
121 } // namespace media 218 } // namespace media
OLDNEW
« no previous file with comments | « media/mojo/clients/mojo_cdm.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698