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

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

Issue 2561263002: [eme] Reject CDM calls after connection error (Closed)
Patch Set: changes 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
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_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::_;
22 using ::testing::StrictMock; 24 using ::testing::StrictMock;
23 25
26 MATCHER(NotEmpty, "") {
27 return !arg.empty();
28 }
29
24 namespace media { 30 namespace media {
25 31
26 namespace { 32 namespace {
27 33
28 const char kClearKeyKeySystem[] = "org.w3.clearkey"; 34 const char kClearKeyKeySystem[] = "org.w3.clearkey";
29 const char kTestSecurityOrigin[] = "https://www.test.com"; 35 const char kTestSecurityOrigin[] = "https://www.test.com";
30 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
31 } // namespace 44 } // namespace
32 45
33 class MojoCdmTest : public ::testing::Test { 46 class MojoCdmTest : public ::testing::Test {
34 public: 47 public:
35 enum ExpectedResult { SUCCESS, CONNECTION_ERROR }; 48 enum ExpectedResult { SUCCESS, CONNECTION_ERROR, FAILURE };
36 49
37 MojoCdmTest() 50 MojoCdmTest()
38 : mojo_cdm_service_(base::MakeUnique<MojoCdmService>( 51 : mojo_cdm_service_(base::MakeUnique<MojoCdmService>(
39 mojo_cdm_service_context_.GetWeakPtr(), 52 mojo_cdm_service_context_.GetWeakPtr(),
40 &cdm_factory_)), 53 &cdm_factory_)),
41 cdm_binding_(mojo_cdm_service_.get()) {} 54 cdm_binding_(mojo_cdm_service_.get()) {}
42 55
43 virtual ~MojoCdmTest() {} 56 virtual ~MojoCdmTest() {}
44 57
45 void Initialize(ExpectedResult expected_result) { 58 void Initialize(const std::string& key_system,
59 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:
xhwang 2016/12/14 18:43:54 I think this is equivalent to CONNECTION_ERROR. I
jrummell 2016/12/15 21:53:56 Fixed. Now I get "ERROR: org.random.cdm is not a k
71 break;
56 } 72 }
57 73
58 MojoCdm::Create(kClearKeyKeySystem, GURL(kTestSecurityOrigin), CdmConfig(), 74 MojoCdm::Create(key_system, GURL(kTestSecurityOrigin), CdmConfig(),
59 std::move(remote_cdm), 75 std::move(remote_cdm),
60 base::Bind(&MockCdmClient::OnSessionMessage, 76 base::Bind(&MockCdmClient::OnSessionMessage,
61 base::Unretained(&cdm_client_)), 77 base::Unretained(&cdm_client_)),
62 base::Bind(&MockCdmClient::OnSessionClosed, 78 base::Bind(&MockCdmClient::OnSessionClosed,
63 base::Unretained(&cdm_client_)), 79 base::Unretained(&cdm_client_)),
64 base::Bind(&MockCdmClient::OnSessionKeysChange, 80 base::Bind(&MockCdmClient::OnSessionKeysChange,
65 base::Unretained(&cdm_client_)), 81 base::Unretained(&cdm_client_)),
66 base::Bind(&MockCdmClient::OnSessionExpirationUpdate, 82 base::Bind(&MockCdmClient::OnSessionExpirationUpdate,
67 base::Unretained(&cdm_client_)), 83 base::Unretained(&cdm_client_)),
68 base::Bind(&MojoCdmTest::OnCdmCreated, 84 base::Bind(&MojoCdmTest::OnCdmCreated,
69 base::Unretained(this), expected_result)); 85 base::Unretained(this), expected_result));
70 86
71 base::RunLoop().RunUntilIdle(); 87 base::RunLoop().RunUntilIdle();
72
73 if (expected_result == SUCCESS) {
74 EXPECT_TRUE(mojo_cdm_);
75 } else {
76 EXPECT_FALSE(mojo_cdm_);
77 }
78 } 88 }
79 89
80 void OnCdmCreated(ExpectedResult expected_result, 90 void OnCdmCreated(ExpectedResult expected_result,
81 const scoped_refptr<MediaKeys>& cdm, 91 const scoped_refptr<MediaKeys>& cdm,
82 const std::string& error_message) { 92 const std::string& error_message) {
83 if (!cdm) { 93 if (!cdm) {
94 EXPECT_NE(SUCCESS, expected_result);
84 DVLOG(1) << error_message; 95 DVLOG(1) << error_message;
85 return; 96 return;
86 } 97 }
87 98
88 EXPECT_EQ(SUCCESS, expected_result); 99 EXPECT_EQ(SUCCESS, expected_result);
89 mojo_cdm_ = cdm; 100 mojo_cdm_ = cdm;
90 } 101 }
91 102
103 void ForceConnectionError() {
104 // If there is an existing session it will get closed when the connection
105 // is broken.
106 if (!session_id_.empty()) {
107 EXPECT_CALL(cdm_client_, OnSessionClosed(session_id_));
108 }
109
110 cdm_binding_.CloseWithReason(2, "Test closed connection.");
111
112 base::RunLoop().RunUntilIdle();
113 }
114
115 void SetServerCertificateAndExpect(const std::vector<uint8_t>& certificate,
116 ExpectedResult expected_result) {
117 mojo_cdm_->SetServerCertificate(
118 certificate,
119 base::MakeUnique<MockCdmPromise>(expected_result == SUCCESS));
120
121 base::RunLoop().RunUntilIdle();
122 }
123
124 void CreateSessionAndExpect(EmeInitDataType data_type,
125 const std::vector<uint8_t>& key_id,
126 ExpectedResult expected_result) {
127 if (expected_result == SUCCESS) {
128 EXPECT_CALL(cdm_client_, OnSessionMessage(NotEmpty(), _, _));
129 }
130
131 mojo_cdm_->CreateSessionAndGenerateRequest(
132 MediaKeys::SessionType::TEMPORARY_SESSION, data_type, key_id,
133 base::MakeUnique<MockCdmSessionPromise>(expected_result == SUCCESS,
134 &session_id_));
135
136 base::RunLoop().RunUntilIdle();
137 }
138
139 void CloseSessionAndExpect(ExpectedResult expected_result) {
140 DCHECK(!session_id_.empty()) << "CloseSessionAndExpect() must be called "
141 "after a successful "
142 "CreateSessionAndExpect()";
143
144 if (expected_result == SUCCESS) {
145 EXPECT_CALL(cdm_client_, OnSessionClosed(session_id_));
146 }
147
148 mojo_cdm_->CloseSession(session_id_, base::MakeUnique<MockCdmPromise>(
149 expected_result == SUCCESS));
150
151 base::RunLoop().RunUntilIdle();
152 }
153
92 // Fixture members. 154 // Fixture members.
93 base::TestMessageLoop message_loop_; 155 base::TestMessageLoop message_loop_;
94 156
95 MojoCdmServiceContext mojo_cdm_service_context_; 157 MojoCdmServiceContext mojo_cdm_service_context_;
96 StrictMock<MockCdmClient> cdm_client_; 158 StrictMock<MockCdmClient> cdm_client_;
97 159
98 // TODO(jrummell): Use a MockCdmFactory to create a MockCdm here for more test 160 // TODO(jrummell): Use a MockCdmFactory to create a MockCdm here for more test
99 // coverage. 161 // coverage.
100 DefaultCdmFactory cdm_factory_; 162 DefaultCdmFactory cdm_factory_;
101 163
102 std::unique_ptr<MojoCdmService> mojo_cdm_service_; 164 std::unique_ptr<MojoCdmService> mojo_cdm_service_;
103 mojo::Binding<mojom::ContentDecryptionModule> cdm_binding_; 165 mojo::Binding<mojom::ContentDecryptionModule> cdm_binding_;
104 scoped_refptr<MediaKeys> mojo_cdm_; 166 scoped_refptr<MediaKeys> mojo_cdm_;
105 167
168 // |session_id_| is the latest successful result of calling CreateSession().
169 std::string session_id_;
170
106 private: 171 private:
107 DISALLOW_COPY_AND_ASSIGN(MojoCdmTest); 172 DISALLOW_COPY_AND_ASSIGN(MojoCdmTest);
108 }; 173 };
109 174
110 TEST_F(MojoCdmTest, Create_Success) { 175 TEST_F(MojoCdmTest, Create_Success) {
111 Initialize(SUCCESS); 176 Initialize(kClearKeyKeySystem, SUCCESS);
112 } 177 }
113 178
114 TEST_F(MojoCdmTest, Create_ConnectionError) { 179 TEST_F(MojoCdmTest, Create_ConnectionError) {
115 Initialize(CONNECTION_ERROR); 180 Initialize(kClearKeyKeySystem, CONNECTION_ERROR);
116 } 181 }
117 182
118 // TODO(xhwang): Add more test cases! 183 TEST_F(MojoCdmTest, Create_Failure) {
184 Initialize("org.random.cdm", FAILURE);
xhwang 2016/12/14 18:43:54 See comment above. I think this is failing because
jrummell 2016/12/15 21:53:56 Fixed.
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 }
119 217
120 } // namespace media 218 } // namespace media
OLDNEW
« media/mojo/clients/mojo_cdm.cc ('K') | « media/mojo/clients/mojo_cdm.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698