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

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

Issue 2561263002: [eme] Reject CDM calls after connection error (Closed)
Patch Set: improved mock promises 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/mock_filters.h" 13 #include "media/base/mock_filters.h"
14 #include "media/cdm/default_cdm_factory.h" 14 #include "media/cdm/default_cdm_factory.h"
15 #include "media/mojo/clients/mojo_cdm.h" 15 #include "media/mojo/clients/mojo_cdm.h"
16 #include "media/mojo/interfaces/content_decryption_module.mojom.h" 16 #include "media/mojo/interfaces/content_decryption_module.mojom.h"
17 #include "media/mojo/services/mojo_cdm_service.h" 17 #include "media/mojo/services/mojo_cdm_service.h"
18 #include "media/mojo/services/mojo_cdm_service_context.h" 18 #include "media/mojo/services/mojo_cdm_service_context.h"
19 #include "mojo/public/cpp/bindings/interface_request.h" 19 #include "mojo/public/cpp/bindings/interface_request.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 using ::testing::_;
22 using ::testing::StrictMock; 23 using ::testing::StrictMock;
23 24
25 MATCHER(NotEmpty, "") {
26 return !arg.empty();
27 }
28
24 namespace media { 29 namespace media {
25 30
26 namespace { 31 namespace {
27 32
28 const char kClearKeyKeySystem[] = "org.w3.clearkey"; 33 const char kClearKeyKeySystem[] = "org.w3.clearkey";
29 const char kTestSecurityOrigin[] = "https://www.test.com"; 34 const char kTestSecurityOrigin[] = "https://www.test.com";
30 35
36 // Random key ID used to create a session.
37 const uint8_t kKeyId[] = {
38 // base64 equivalent is AQIDBAUGBwgJCgsMDQ4PEA
39 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
40 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
41 };
42
31 } // namespace 43 } // namespace
32 44
33 class MojoCdmTest : public ::testing::Test { 45 class MojoCdmTest : public ::testing::Test {
34 public: 46 public:
35 enum ExpectedResult { SUCCESS, CONNECTION_ERROR }; 47 enum ExpectedResult { SUCCESS, CONNECTION_ERROR, FAILURE };
36 48
37 MojoCdmTest() 49 MojoCdmTest()
38 : mojo_cdm_service_(base::MakeUnique<MojoCdmService>( 50 : mojo_cdm_service_(base::MakeUnique<MojoCdmService>(
39 mojo_cdm_service_context_.GetWeakPtr(), 51 mojo_cdm_service_context_.GetWeakPtr(),
40 &cdm_factory_)), 52 &cdm_factory_)),
41 cdm_binding_(mojo_cdm_service_.get()) {} 53 cdm_binding_(mojo_cdm_service_.get()) {}
42 54
43 virtual ~MojoCdmTest() {} 55 virtual ~MojoCdmTest() {}
44 56
45 void Initialize(ExpectedResult expected_result) { 57 void Initialize(const std::string& key_system,
58 ExpectedResult expected_result) {
46 mojom::ContentDecryptionModulePtr remote_cdm; 59 mojom::ContentDecryptionModulePtr remote_cdm;
47 auto cdm_request = mojo::GetProxy(&remote_cdm); 60 auto cdm_request = mojo::GetProxy(&remote_cdm);
48 61
49 switch (expected_result) { 62 switch (expected_result) {
50 case SUCCESS: 63 case SUCCESS:
64 case FAILURE:
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;
56 } 70 }
57 71
58 MojoCdm::Create(kClearKeyKeySystem, GURL(kTestSecurityOrigin), CdmConfig(), 72 MojoCdm::Create(key_system, GURL(kTestSecurityOrigin), CdmConfig(),
59 std::move(remote_cdm), 73 std::move(remote_cdm),
60 base::Bind(&MockCdmClient::OnSessionMessage, 74 base::Bind(&MockCdmClient::OnSessionMessage,
61 base::Unretained(&cdm_client_)), 75 base::Unretained(&cdm_client_)),
62 base::Bind(&MockCdmClient::OnSessionClosed, 76 base::Bind(&MockCdmClient::OnSessionClosed,
63 base::Unretained(&cdm_client_)), 77 base::Unretained(&cdm_client_)),
64 base::Bind(&MockCdmClient::OnSessionKeysChange, 78 base::Bind(&MockCdmClient::OnSessionKeysChange,
65 base::Unretained(&cdm_client_)), 79 base::Unretained(&cdm_client_)),
66 base::Bind(&MockCdmClient::OnSessionExpirationUpdate, 80 base::Bind(&MockCdmClient::OnSessionExpirationUpdate,
67 base::Unretained(&cdm_client_)), 81 base::Unretained(&cdm_client_)),
68 base::Bind(&MojoCdmTest::OnCdmCreated, 82 base::Bind(&MojoCdmTest::OnCdmCreated,
69 base::Unretained(this), expected_result)); 83 base::Unretained(this), expected_result));
70 84
71 base::RunLoop().RunUntilIdle(); 85 base::RunLoop().RunUntilIdle();
72
73 if (expected_result == SUCCESS) {
74 EXPECT_TRUE(mojo_cdm_);
75 } else {
76 EXPECT_FALSE(mojo_cdm_);
77 }
78 } 86 }
79 87
80 void OnCdmCreated(ExpectedResult expected_result, 88 void OnCdmCreated(ExpectedResult expected_result,
81 const scoped_refptr<MediaKeys>& cdm, 89 const scoped_refptr<MediaKeys>& cdm,
82 const std::string& error_message) { 90 const std::string& error_message) {
83 if (!cdm) { 91 if (!cdm) {
92 EXPECT_NE(SUCCESS, expected_result);
84 DVLOG(1) << error_message; 93 DVLOG(1) << error_message;
85 return; 94 return;
86 } 95 }
87 96
88 EXPECT_EQ(SUCCESS, expected_result); 97 EXPECT_EQ(SUCCESS, expected_result);
89 mojo_cdm_ = cdm; 98 mojo_cdm_ = cdm;
90 } 99 }
91 100
101 void ForceConnectionError() {
102 // If there is an existing session it will get closed when the connection
103 // is broken.
104 if (!session_id_.empty()) {
105 EXPECT_CALL(cdm_client_, OnSessionClosed(session_id_));
106 }
107
108 cdm_binding_.CloseWithReason(2, "Test closed connection.");
109
110 base::RunLoop().RunUntilIdle();
111 }
112
113 void SetServerCertificateAndExpect(const std::vector<uint8_t>& certificate,
114 ExpectedResult expected_result) {
115 mojo_cdm_->SetServerCertificate(
116 certificate,
117 base::MakeUnique<MockCdmPromise>(expected_result == SUCCESS));
118
119 base::RunLoop().RunUntilIdle();
120 }
121
122 void CreateSessionAndExpect(EmeInitDataType data_type,
123 const std::vector<uint8_t>& key_id,
124 ExpectedResult expected_result) {
125 if (expected_result == SUCCESS) {
126 EXPECT_CALL(cdm_client_, OnSessionMessage(NotEmpty(), _, _));
127 }
128
129 mojo_cdm_->CreateSessionAndGenerateRequest(
130 MediaKeys::SessionType::TEMPORARY_SESSION, data_type, key_id,
131 base::MakeUnique<MockCdmSessionPromise>(expected_result == SUCCESS,
132 &session_id_));
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_, base::MakeUnique<MockCdmPromise>(
147 expected_result == SUCCESS));
148
149 base::RunLoop().RunUntilIdle();
150 }
151
92 // Fixture members. 152 // Fixture members.
93 base::TestMessageLoop message_loop_; 153 base::TestMessageLoop message_loop_;
94 154
95 MojoCdmServiceContext mojo_cdm_service_context_; 155 MojoCdmServiceContext mojo_cdm_service_context_;
96 StrictMock<MockCdmClient> cdm_client_; 156 StrictMock<MockCdmClient> cdm_client_;
97 157
98 // TODO(jrummell): Use a MockCdmFactory to create a MockCdm here for more test 158 // TODO(jrummell): Use a MockCdmFactory to create a MockCdm here for more test
99 // coverage. 159 // coverage.
100 DefaultCdmFactory cdm_factory_; 160 DefaultCdmFactory cdm_factory_;
101 161
102 std::unique_ptr<MojoCdmService> mojo_cdm_service_; 162 std::unique_ptr<MojoCdmService> mojo_cdm_service_;
103 mojo::Binding<mojom::ContentDecryptionModule> cdm_binding_; 163 mojo::Binding<mojom::ContentDecryptionModule> cdm_binding_;
104 scoped_refptr<MediaKeys> mojo_cdm_; 164 scoped_refptr<MediaKeys> mojo_cdm_;
105 165
166 // |session_id_| is the latest successful result of calling CreateSession().
167 std::string session_id_;
168
106 private: 169 private:
107 DISALLOW_COPY_AND_ASSIGN(MojoCdmTest); 170 DISALLOW_COPY_AND_ASSIGN(MojoCdmTest);
108 }; 171 };
109 172
110 TEST_F(MojoCdmTest, Create_Success) { 173 TEST_F(MojoCdmTest, Create_Success) {
111 Initialize(SUCCESS); 174 Initialize(kClearKeyKeySystem, SUCCESS);
112 } 175 }
113 176
114 TEST_F(MojoCdmTest, Create_ConnectionError) { 177 TEST_F(MojoCdmTest, Create_ConnectionError) {
115 Initialize(CONNECTION_ERROR); 178 Initialize(kClearKeyKeySystem, CONNECTION_ERROR);
116 } 179 }
117 180
118 // TODO(xhwang): Add more test cases! 181 TEST_F(MojoCdmTest, Create_Failure) {
182 Initialize("org.random.cdm", FAILURE);
xhwang 2016/12/16 01:21:13 Please add a comment that this is failing because
jrummell 2016/12/16 02:32:34 Done.
183 }
184
185 TEST_F(MojoCdmTest, SetServerCertificate_AfterConnectionError) {
186 Initialize(kClearKeyKeySystem, SUCCESS);
187 ForceConnectionError();
188 SetServerCertificateAndExpect({0, 1, 2}, FAILURE);
189 }
190
191 TEST_F(MojoCdmTest, CreateSessionAndGenerateRequest_AfterConnectionError) {
192 std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId));
193
194 Initialize(kClearKeyKeySystem, SUCCESS);
195 ForceConnectionError();
196 CreateSessionAndExpect(EmeInitDataType::WEBM, key_id, FAILURE);
197 }
198
199 TEST_F(MojoCdmTest, CloseSession_Success) {
200 std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId));
201
202 Initialize(kClearKeyKeySystem, SUCCESS);
203 CreateSessionAndExpect(EmeInitDataType::WEBM, key_id, SUCCESS);
204 CloseSessionAndExpect(SUCCESS);
205 }
206
207 TEST_F(MojoCdmTest, CloseSession_AfterConnectionError) {
208 std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId));
209
210 Initialize(kClearKeyKeySystem, SUCCESS);
211 CreateSessionAndExpect(EmeInitDataType::WEBM, key_id, SUCCESS);
212 ForceConnectionError();
213 CloseSessionAndExpect(FAILURE);
214 }
119 215
120 } // namespace media 216 } // 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