OLD | NEW |
| (Empty) |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/cdm/browser/media_drm_storage_impl.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/run_loop.h" | |
10 #include "base/test/test_message_loop.h" | |
11 #include "components/prefs/testing_pref_service.h" | |
12 #include "media/mojo/services/mojo_media_drm_storage.h" | |
13 #include "mojo/public/cpp/bindings/interface_request.h" | |
14 #include "mojo/public/cpp/bindings/strong_binding.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 #include "url/gurl.h" | |
17 #include "url/origin.h" | |
18 | |
19 namespace cdm { | |
20 | |
21 const char kMediaDrmStorage[] = "media.media_drm_storage"; | |
22 const char kTestOrigin[] = "https://www.testorigin.com:80"; | |
23 | |
24 class MediaDrmStorageImplTest : public ::testing::Test { | |
25 public: | |
26 MediaDrmStorageImplTest() {} | |
27 | |
28 void SetUp() override { | |
29 pref_service_.reset(new TestingPrefServiceSimple()); | |
30 PrefRegistrySimple* registry = pref_service_->registry(); | |
31 MediaDrmStorageImpl::RegisterProfilePrefs(registry); | |
32 | |
33 media::mojom::MediaDrmStoragePtr media_drm_storage_ptr; | |
34 auto request = mojo::MakeRequest(&media_drm_storage_ptr); | |
35 | |
36 media_drm_storage_.reset( | |
37 new media::MojoMediaDrmStorage(std::move(media_drm_storage_ptr))); | |
38 | |
39 // The created object will be destroyed on connection error. | |
40 new MediaDrmStorageImpl(nullptr, // Use null RenderFrameHost for testing. | |
41 pref_service_.get(), url::Origin(GURL(kTestOrigin)), | |
42 std::move(request)); | |
43 | |
44 media_drm_storage_->Initialize(url::Origin(GURL(kTestOrigin))); | |
45 } | |
46 | |
47 void TearDown() override { | |
48 media_drm_storage_.reset(); | |
49 base::RunLoop().RunUntilIdle(); | |
50 } | |
51 | |
52 protected: | |
53 using SessionData = media::MediaDrmStorage::SessionData; | |
54 | |
55 void OnProvisioned() { | |
56 media_drm_storage_->OnProvisioned(ExpectResult(true)); | |
57 } | |
58 | |
59 void SavePersistentSession(const std::string& session_id, | |
60 const std::vector<uint8_t>& key_set_id, | |
61 const std::string& mime_type, | |
62 bool success = true) { | |
63 media_drm_storage_->SavePersistentSession( | |
64 session_id, SessionData(key_set_id, mime_type), ExpectResult(success)); | |
65 } | |
66 | |
67 void LoadPersistentSession(const std::string& session_id, | |
68 const std::vector<uint8_t>& expected_key_set_id, | |
69 const std::string& expected_mime_type) { | |
70 media_drm_storage_->LoadPersistentSession( | |
71 session_id, ExpectResult(base::MakeUnique<SessionData>( | |
72 expected_key_set_id, expected_mime_type))); | |
73 } | |
74 | |
75 void LoadPersistentSessionAndExpectFailure(const std::string& session_id) { | |
76 media_drm_storage_->LoadPersistentSession( | |
77 session_id, ExpectResult(std::unique_ptr<SessionData>())); | |
78 } | |
79 | |
80 void RemovePersistentSession(const std::string& session_id, | |
81 bool success = true) { | |
82 media_drm_storage_->RemovePersistentSession(session_id, | |
83 ExpectResult(success)); | |
84 } | |
85 | |
86 void SaveAndLoadPersistentSession(const std::string& session_id, | |
87 const std::vector<uint8_t>& key_set_id, | |
88 const std::string& mime_type) { | |
89 SavePersistentSession(session_id, key_set_id, mime_type); | |
90 LoadPersistentSession(session_id, key_set_id, mime_type); | |
91 } | |
92 | |
93 media::MediaDrmStorage::ResultCB ExpectResult(bool expected_result) { | |
94 return base::BindOnce(&MediaDrmStorageImplTest::CheckResult, | |
95 base::Unretained(this), expected_result); | |
96 } | |
97 | |
98 media::MediaDrmStorage::LoadPersistentSessionCB ExpectResult( | |
99 std::unique_ptr<SessionData> expected_session_data) { | |
100 return base::BindOnce(&MediaDrmStorageImplTest::CheckLoadedSession, | |
101 base::Unretained(this), | |
102 base::Passed(&expected_session_data)); | |
103 } | |
104 | |
105 void CheckResult(bool expected_result, bool result) { | |
106 EXPECT_EQ(expected_result, result); | |
107 } | |
108 | |
109 void CheckLoadedSession(std::unique_ptr<SessionData> expected_session_data, | |
110 std::unique_ptr<SessionData> session_data) { | |
111 if (!expected_session_data) { | |
112 EXPECT_FALSE(session_data); | |
113 return; | |
114 } | |
115 | |
116 EXPECT_EQ(expected_session_data->key_set_id, session_data->key_set_id); | |
117 EXPECT_EQ(expected_session_data->mime_type, session_data->mime_type); | |
118 } | |
119 | |
120 base::TestMessageLoop message_loop_; | |
121 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | |
122 std::unique_ptr<media::MediaDrmStorage> media_drm_storage_; | |
123 }; | |
124 | |
125 TEST_F(MediaDrmStorageImplTest, OnProvisioned) { | |
126 OnProvisioned(); | |
127 base::RunLoop().RunUntilIdle(); | |
128 | |
129 // Verify the origin dictionary is created. | |
130 const base::DictionaryValue* storage_dict = | |
131 pref_service_->GetDictionary(kMediaDrmStorage); | |
132 EXPECT_TRUE( | |
133 storage_dict->GetDictionaryWithoutPathExpansion(kTestOrigin, nullptr)); | |
134 } | |
135 | |
136 TEST_F(MediaDrmStorageImplTest, OnProvisioned_Twice) { | |
137 OnProvisioned(); | |
138 SaveAndLoadPersistentSession("session_id", {1, 0}, "mime/type1"); | |
139 // Provisioning again will clear everything associated with the origin. | |
140 OnProvisioned(); | |
141 LoadPersistentSessionAndExpectFailure("session_id"); | |
142 base::RunLoop().RunUntilIdle(); | |
143 } | |
144 | |
145 TEST_F(MediaDrmStorageImplTest, SaveSession_Unprovisioned) { | |
146 SavePersistentSession("session_id", {1, 0}, "mime/type", false); | |
147 base::RunLoop().RunUntilIdle(); | |
148 } | |
149 | |
150 TEST_F(MediaDrmStorageImplTest, SaveSession_SaveTwice) { | |
151 OnProvisioned(); | |
152 SaveAndLoadPersistentSession("session_id", {1, 0}, "mime/type1"); | |
153 SaveAndLoadPersistentSession("session_id", {2, 3}, "mime/type2"); | |
154 base::RunLoop().RunUntilIdle(); | |
155 } | |
156 | |
157 TEST_F(MediaDrmStorageImplTest, SaveAndLoadSession_LoadTwice) { | |
158 OnProvisioned(); | |
159 SaveAndLoadPersistentSession("session_id", {1, 0}, "mime/type"); | |
160 LoadPersistentSession("session_id", {1, 0}, "mime/type"); | |
161 base::RunLoop().RunUntilIdle(); | |
162 } | |
163 | |
164 TEST_F(MediaDrmStorageImplTest, SaveAndLoadSession_SpecialCharacters) { | |
165 OnProvisioned(); | |
166 SaveAndLoadPersistentSession("session.id", {1, 0}, "mime.type"); | |
167 SaveAndLoadPersistentSession("session/id", {1, 0}, "mime/type"); | |
168 SaveAndLoadPersistentSession("session-id", {1, 0}, "mime-type"); | |
169 SaveAndLoadPersistentSession("session_id", {1, 0}, "mime_type"); | |
170 SaveAndLoadPersistentSession("session,id", {1, 0}, "mime,type"); | |
171 base::RunLoop().RunUntilIdle(); | |
172 } | |
173 | |
174 TEST_F(MediaDrmStorageImplTest, LoadSession_Unprovisioned) { | |
175 LoadPersistentSessionAndExpectFailure("session_id"); | |
176 base::RunLoop().RunUntilIdle(); | |
177 } | |
178 | |
179 TEST_F(MediaDrmStorageImplTest, RemoveSession_Success) { | |
180 OnProvisioned(); | |
181 SaveAndLoadPersistentSession("session_id", {1, 0}, "mime/type"); | |
182 RemovePersistentSession("session_id", true); | |
183 LoadPersistentSessionAndExpectFailure("session_id"); | |
184 base::RunLoop().RunUntilIdle(); | |
185 } | |
186 | |
187 TEST_F(MediaDrmStorageImplTest, RemoveSession_InvalidSession) { | |
188 OnProvisioned(); | |
189 SaveAndLoadPersistentSession("session_id", {1, 0}, "mime/type"); | |
190 RemovePersistentSession("invalid_session_id", true); | |
191 // Can still load "session_id" session. | |
192 LoadPersistentSession("session_id", {1, 0}, "mime/type"); | |
193 base::RunLoop().RunUntilIdle(); | |
194 } | |
195 | |
196 } // namespace cdm | |
OLD | NEW |