OLD | NEW |
---|---|
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_callback_promise.h" | |
12 #include "media/base/cdm_config.h" | 13 #include "media/base/cdm_config.h" |
13 #include "media/base/mock_filters.h" | 14 #include "media/base/mock_filters.h" |
14 #include "media/cdm/default_cdm_factory.h" | 15 #include "media/cdm/default_cdm_factory.h" |
15 #include "media/mojo/clients/mojo_cdm.h" | 16 #include "media/mojo/clients/mojo_cdm.h" |
16 #include "media/mojo/interfaces/content_decryption_module.mojom.h" | 17 #include "media/mojo/interfaces/content_decryption_module.mojom.h" |
17 #include "media/mojo/services/mojo_cdm_service.h" | 18 #include "media/mojo/services/mojo_cdm_service.h" |
18 #include "media/mojo/services/mojo_cdm_service_context.h" | 19 #include "media/mojo/services/mojo_cdm_service_context.h" |
19 #include "mojo/public/cpp/bindings/interface_request.h" | 20 #include "mojo/public/cpp/bindings/interface_request.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
21 | 22 |
23 using ::testing::_; | |
24 using ::testing::SaveArg; | |
22 using ::testing::StrictMock; | 25 using ::testing::StrictMock; |
23 | 26 |
27 MATCHER(IsNotEmpty, "") { | |
xhwang
2016/12/09 06:13:30
s/IsNotEmpty/NotEmpty
jrummell
2016/12/13 20:57:34
Done.
| |
28 return !arg.empty(); | |
29 } | |
30 | |
24 namespace media { | 31 namespace media { |
25 | 32 |
26 namespace { | 33 namespace { |
27 | 34 |
28 const char kClearKeyKeySystem[] = "org.w3.clearkey"; | 35 const char kClearKeyKeySystem[] = "org.w3.clearkey"; |
29 const char kTestSecurityOrigin[] = "https://www.test.com"; | 36 const char kTestSecurityOrigin[] = "https://www.test.com"; |
30 | 37 |
38 // Random key ID used to create a session. | |
39 const uint8_t kKeyId[] = { | |
40 // base64 equivalent is AQIDBAUGBwgJCgsMDQ4PEA | |
41 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, | |
42 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, | |
43 }; | |
44 | |
31 } // namespace | 45 } // namespace |
32 | 46 |
33 class MojoCdmTest : public ::testing::Test { | 47 class MojoCdmTest : public ::testing::Test { |
34 public: | 48 public: |
35 enum ExpectedResult { SUCCESS, CONNECTION_ERROR }; | 49 enum ExpectedResult { SUCCESS, CONNECTION_ERROR, FAILURE }; |
36 | 50 |
37 MojoCdmTest() | 51 MojoCdmTest() |
38 : mojo_cdm_service_(base::MakeUnique<MojoCdmService>( | 52 : mojo_cdm_service_(base::MakeUnique<MojoCdmService>( |
39 mojo_cdm_service_context_.GetWeakPtr(), | 53 mojo_cdm_service_context_.GetWeakPtr(), |
40 &cdm_factory_)), | 54 &cdm_factory_)), |
41 cdm_binding_(mojo_cdm_service_.get()) {} | 55 cdm_binding_(mojo_cdm_service_.get()) {} |
42 | 56 |
43 virtual ~MojoCdmTest() {} | 57 virtual ~MojoCdmTest() {} |
44 | 58 |
45 void Initialize(ExpectedResult expected_result) { | 59 void Initialize(ExpectedResult expected_result) { |
46 mojom::ContentDecryptionModulePtr remote_cdm; | 60 mojom::ContentDecryptionModulePtr remote_cdm; |
47 auto cdm_request = mojo::GetProxy(&remote_cdm); | 61 auto cdm_request = mojo::GetProxy(&remote_cdm); |
48 | 62 |
49 switch (expected_result) { | 63 switch (expected_result) { |
50 case SUCCESS: | 64 case SUCCESS: |
51 cdm_binding_.Bind(std::move(cdm_request)); | 65 cdm_binding_.Bind(std::move(cdm_request)); |
52 break; | 66 break; |
53 case CONNECTION_ERROR: | 67 case CONNECTION_ERROR: |
54 cdm_request.ResetWithReason(0, "Request dropped for testing."); | 68 cdm_request.ResetWithReason(0, "Request dropped for testing."); |
55 break; | 69 break; |
70 case FAILURE: | |
71 FAIL(); | |
xhwang
2016/12/09 06:13:30
Can you actually add a test to cover initializatio
jrummell
2016/12/13 20:57:34
Done.
| |
72 break; | |
56 } | 73 } |
57 | 74 |
58 MojoCdm::Create(kClearKeyKeySystem, GURL(kTestSecurityOrigin), CdmConfig(), | 75 MojoCdm::Create(kClearKeyKeySystem, GURL(kTestSecurityOrigin), CdmConfig(), |
59 std::move(remote_cdm), | 76 std::move(remote_cdm), |
60 base::Bind(&MockCdmClient::OnSessionMessage, | 77 base::Bind(&MockCdmClient::OnSessionMessage, |
61 base::Unretained(&cdm_client_)), | 78 base::Unretained(&cdm_client_)), |
62 base::Bind(&MockCdmClient::OnSessionClosed, | 79 base::Bind(&MockCdmClient::OnSessionClosed, |
63 base::Unretained(&cdm_client_)), | 80 base::Unretained(&cdm_client_)), |
64 base::Bind(&MockCdmClient::OnSessionKeysChange, | 81 base::Bind(&MockCdmClient::OnSessionKeysChange, |
65 base::Unretained(&cdm_client_)), | 82 base::Unretained(&cdm_client_)), |
66 base::Bind(&MockCdmClient::OnSessionExpirationUpdate, | 83 base::Bind(&MockCdmClient::OnSessionExpirationUpdate, |
67 base::Unretained(&cdm_client_)), | 84 base::Unretained(&cdm_client_)), |
68 base::Bind(&MojoCdmTest::OnCdmCreated, | 85 base::Bind(&MojoCdmTest::OnCdmCreated, |
69 base::Unretained(this), expected_result)); | 86 base::Unretained(this), expected_result)); |
70 | 87 |
71 base::RunLoop().RunUntilIdle(); | 88 base::RunLoop().RunUntilIdle(); |
72 | |
73 if (expected_result == SUCCESS) { | |
74 EXPECT_TRUE(mojo_cdm_); | |
75 } else { | |
76 EXPECT_FALSE(mojo_cdm_); | |
77 } | |
78 } | 89 } |
79 | 90 |
80 void OnCdmCreated(ExpectedResult expected_result, | 91 void OnCdmCreated(ExpectedResult expected_result, |
81 const scoped_refptr<MediaKeys>& cdm, | 92 const scoped_refptr<MediaKeys>& cdm, |
82 const std::string& error_message) { | 93 const std::string& error_message) { |
83 if (!cdm) { | 94 if (!cdm) { |
95 EXPECT_EQ(CONNECTION_ERROR, expected_result); | |
xhwang
2016/12/09 06:13:30
If you actually have a init failure test, this wil
jrummell
2016/12/13 20:57:35
Fixed.
| |
84 DVLOG(1) << error_message; | 96 DVLOG(1) << error_message; |
85 return; | 97 return; |
86 } | 98 } |
87 | 99 |
88 EXPECT_EQ(SUCCESS, expected_result); | 100 EXPECT_EQ(SUCCESS, expected_result); |
89 mojo_cdm_ = cdm; | 101 mojo_cdm_ = cdm; |
90 } | 102 } |
91 | 103 |
104 void ForceConnectionError() { | |
105 // If there is an existing session it will get closed when the connection | |
106 // is broken. | |
107 if (!session_id_.empty()) { | |
108 EXPECT_CALL(cdm_client_, OnSessionClosed(session_id_)); | |
109 } | |
110 | |
111 static_cast<MojoCdm*>(mojo_cdm_.get()) | |
112 ->OnConnectionError(1, "Dropping connection for testing."); | |
113 } | |
xhwang
2016/12/09 06:13:30
You can reset the cdm_binding_ to trigger a connec
jrummell
2016/12/13 20:57:35
Done.
| |
114 | |
115 void SetServerCertificateAndExpect(const std::vector<uint8_t>& certificate, | |
116 ExpectedResult expected_result) { | |
117 mojo_cdm_->SetServerCertificate(certificate, | |
118 CreatePromise(expected_result)); | |
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(IsNotEmpty(), _, _)); | |
128 } | |
129 | |
130 mojo_cdm_->CreateSessionAndGenerateRequest( | |
131 MediaKeys::SessionType::TEMPORARY_SESSION, data_type, key_id, | |
132 CreateSessionPromise(expected_result)); | |
133 | |
134 base::RunLoop().RunUntilIdle(); | |
135 } | |
136 | |
137 void CloseSessionAndExpect(ExpectedResult expected_result) { | |
138 DCHECK(!session_id_.empty()) << "CloseSessionAndExpect() must be called " | |
139 "after a successful " | |
140 "CreateSessionAndExpect()"; | |
141 | |
142 if (expected_result == SUCCESS) { | |
143 EXPECT_CALL(cdm_client_, OnSessionClosed(session_id_)); | |
144 } | |
145 | |
146 mojo_cdm_->CloseSession(session_id_, CreatePromise(expected_result)); | |
147 | |
148 base::RunLoop().RunUntilIdle(); | |
149 } | |
xhwang
2016/12/09 06:13:30
This really is testing AesDecryptor, not MojoCdm..
jrummell
2016/12/13 20:57:34
In the failure case MojoCdm will fail the promise.
| |
150 | |
151 // Create a promise. |expected_result| is used to indicate how the promise | |
152 // should be fulfilled. | |
153 std::unique_ptr<SimpleCdmPromise> CreatePromise( | |
154 ExpectedResult expected_result) { | |
155 if (expected_result == SUCCESS) { | |
156 EXPECT_CALL(*this, OnResolve()); | |
157 } else { | |
158 EXPECT_CALL(*this, OnReject(_, _, IsNotEmpty())); | |
159 } | |
160 | |
161 std::unique_ptr<SimpleCdmPromise> promise(new CdmCallbackPromise<>( | |
162 base::Bind(&MojoCdmTest::OnResolve, base::Unretained(this)), | |
163 base::Bind(&MojoCdmTest::OnReject, base::Unretained(this)))); | |
164 return promise; | |
165 } | |
166 | |
167 // Create a promise to be used when a new session is created. | |
168 // |expected_result| is used to indicate how the promise should be fulfilled. | |
169 std::unique_ptr<NewSessionCdmPromise> CreateSessionPromise( | |
170 ExpectedResult expected_result) { | |
171 if (expected_result == SUCCESS) { | |
172 EXPECT_CALL(*this, OnResolveWithSession(_)) | |
173 .WillOnce(SaveArg<0>(&session_id_)); | |
174 } else { | |
175 EXPECT_CALL(*this, OnReject(_, _, IsNotEmpty())); | |
176 } | |
177 | |
178 std::unique_ptr<NewSessionCdmPromise> promise( | |
179 new CdmCallbackPromise<std::string>( | |
180 base::Bind(&MojoCdmTest::OnResolveWithSession, | |
181 base::Unretained(this)), | |
182 base::Bind(&MojoCdmTest::OnReject, base::Unretained(this)))); | |
183 return promise; | |
184 } | |
185 | |
186 // Methods used for promise resolved/rejected. | |
187 MOCK_METHOD0(OnResolve, void()); | |
188 MOCK_METHOD1(OnResolveWithSession, void(const std::string& session_id)); | |
189 MOCK_METHOD3(OnReject, | |
190 void(CdmPromise::Exception exception_code, | |
191 uint32_t system_code, | |
192 const std::string& error_message)); | |
xhwang
2016/12/09 06:13:30
We have this in multiple tests. Maybe we should ha
jrummell
2016/12/13 20:57:34
Done. I'll update the other places in another CL.
| |
193 | |
92 // Fixture members. | 194 // Fixture members. |
93 base::TestMessageLoop message_loop_; | 195 base::TestMessageLoop message_loop_; |
94 | 196 |
95 MojoCdmServiceContext mojo_cdm_service_context_; | 197 MojoCdmServiceContext mojo_cdm_service_context_; |
96 StrictMock<MockCdmClient> cdm_client_; | 198 StrictMock<MockCdmClient> cdm_client_; |
97 | 199 |
98 // TODO(jrummell): Use a MockCdmFactory to create a MockCdm here for more test | 200 // TODO(jrummell): Use a MockCdmFactory to create a MockCdm here for more test |
99 // coverage. | 201 // coverage. |
100 DefaultCdmFactory cdm_factory_; | 202 DefaultCdmFactory cdm_factory_; |
101 | 203 |
102 std::unique_ptr<MojoCdmService> mojo_cdm_service_; | 204 std::unique_ptr<MojoCdmService> mojo_cdm_service_; |
103 mojo::Binding<mojom::ContentDecryptionModule> cdm_binding_; | 205 mojo::Binding<mojom::ContentDecryptionModule> cdm_binding_; |
104 scoped_refptr<MediaKeys> mojo_cdm_; | 206 scoped_refptr<MediaKeys> mojo_cdm_; |
105 | 207 |
208 // |session_id_| is the latest successful result of calling CreateSession(). | |
209 std::string session_id_; | |
210 | |
106 private: | 211 private: |
107 DISALLOW_COPY_AND_ASSIGN(MojoCdmTest); | 212 DISALLOW_COPY_AND_ASSIGN(MojoCdmTest); |
108 }; | 213 }; |
109 | 214 |
110 TEST_F(MojoCdmTest, Create_Success) { | 215 TEST_F(MojoCdmTest, Create_Success) { |
111 Initialize(SUCCESS); | 216 Initialize(SUCCESS); |
112 } | 217 } |
113 | 218 |
114 TEST_F(MojoCdmTest, Create_ConnectionError) { | 219 TEST_F(MojoCdmTest, Create_ConnectionError) { |
115 Initialize(CONNECTION_ERROR); | 220 Initialize(CONNECTION_ERROR); |
116 } | 221 } |
117 | 222 |
118 // TODO(xhwang): Add more test cases! | 223 TEST_F(MojoCdmTest, SetServerCertificate_AfterConnectionError) { |
224 Initialize(SUCCESS); | |
225 ForceConnectionError(); | |
226 SetServerCertificateAndExpect({0, 1, 2}, FAILURE); | |
227 } | |
228 | |
229 TEST_F(MojoCdmTest, CreateSessionAndGenerateRequest_AfterConnectionError) { | |
230 std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId)); | |
231 | |
232 Initialize(SUCCESS); | |
233 ForceConnectionError(); | |
234 CreateSessionAndExpect(EmeInitDataType::WEBM, key_id, FAILURE); | |
235 } | |
236 | |
237 TEST_F(MojoCdmTest, CloseSession_Success) { | |
238 std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId)); | |
239 | |
240 Initialize(SUCCESS); | |
241 CreateSessionAndExpect(EmeInitDataType::WEBM, key_id, SUCCESS); | |
242 CloseSessionAndExpect(SUCCESS); | |
243 } | |
244 | |
245 TEST_F(MojoCdmTest, CloseSession_AfterConnectionError) { | |
246 std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId)); | |
247 | |
248 Initialize(SUCCESS); | |
249 CreateSessionAndExpect(EmeInitDataType::WEBM, key_id, SUCCESS); | |
250 ForceConnectionError(); | |
251 CloseSessionAndExpect(FAILURE); | |
252 } | |
xhwang
2016/12/09 06:13:30
If you add a MockCdm first and use it in this test
jrummell
2016/12/13 20:57:34
Agreed. Later :)
| |
119 | 253 |
120 } // namespace media | 254 } // namespace media |
OLD | NEW |