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

Side by Side Diff: components/cdm/browser/media_drm_storage_impl_unittest.cc

Issue 2791903004: media: Implement MediaDrmStorageImpl with tests (Closed)
Patch Set: fix rebase errors Created 3 years, 8 months 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
(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 SavePersistentSession(bool success = true) {
68 SavePersistentSession("session_id", {1}, "mime/type", success);
69 }
70
71 void LoadPersistentSession(const std::string& session_id,
72 const std::vector<uint8_t>& expected_key_set_id,
73 const std::string& expected_mime_type) {
74 media_drm_storage_->LoadPersistentSession(
75 session_id, ExpectResult(base::MakeUnique<SessionData>(
76 expected_key_set_id, expected_mime_type)));
77 }
78
79 void LoadPersistentSessionAndExpectFailure(const std::string& session_id) {
80 media_drm_storage_->LoadPersistentSession(
81 session_id, ExpectResult(std::unique_ptr<SessionData>()));
82 }
83
84 void RemovePersistentSession(const std::string& session_id,
85 bool success = true) {
86 media_drm_storage_->RemovePersistentSession(session_id,
87 ExpectResult(success));
88 }
89
90 void SaveAndLoadPersistentSession(const std::string& session_id,
91 const std::vector<uint8_t>& key_set_id,
92 const std::string& mime_type) {
93 SavePersistentSession(session_id, key_set_id, mime_type);
94 LoadPersistentSession(session_id, key_set_id, mime_type);
95 }
96
97 media::MediaDrmStorage::ResultCB ExpectResult(bool expected_result) {
98 return base::BindOnce(&MediaDrmStorageImplTest::CheckResult,
99 base::Unretained(this), expected_result);
100 }
101
102 media::MediaDrmStorage::LoadPersistentSessionCB ExpectResult(
103 std::unique_ptr<SessionData> expected_session_data) {
104 return base::BindOnce(&MediaDrmStorageImplTest::CheckLoadedSession,
105 base::Unretained(this),
106 base::Passed(&expected_session_data));
107 }
108
109 void CheckResult(bool expected_result, bool result) {
110 EXPECT_EQ(expected_result, result);
111 }
112
113 void CheckLoadedSession(std::unique_ptr<SessionData> expected_session_data,
114 std::unique_ptr<SessionData> session_data) {
115 if (!expected_session_data) {
116 EXPECT_FALSE(session_data);
117 return;
118 }
119
120 EXPECT_EQ(expected_session_data->key_set_id, session_data->key_set_id);
121 EXPECT_EQ(expected_session_data->mime_type, session_data->mime_type);
122 }
123
124 base::TestMessageLoop message_loop_;
125 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
126 std::unique_ptr<media::MediaDrmStorage> media_drm_storage_;
127 };
128
129 TEST_F(MediaDrmStorageImplTest, OnProvisioned) {
130 OnProvisioned();
131 base::RunLoop().RunUntilIdle();
132
133 // Verify the origin dictionary is created.
134 const base::DictionaryValue* storage_dict =
135 pref_service_->GetDictionary(kMediaDrmStorage);
136 EXPECT_TRUE(
137 storage_dict->GetDictionaryWithoutPathExpansion(kTestOrigin, nullptr));
138 }
139
140 TEST_F(MediaDrmStorageImplTest, SaveSession_Unprovisioned) {
141 SavePersistentSession(false);
142 base::RunLoop().RunUntilIdle();
143 }
144
145 TEST_F(MediaDrmStorageImplTest, SaveSession_SaveTwice) {
146 OnProvisioned();
147 SavePersistentSession();
148 SavePersistentSession(false);
149 base::RunLoop().RunUntilIdle();
150 }
151
152 TEST_F(MediaDrmStorageImplTest, SaveAndLoadSession_LoadTwice) {
153 OnProvisioned();
154 SaveAndLoadPersistentSession("session_id", {1, 0}, "mime/type");
155 LoadPersistentSession("session_id", {1, 0}, "mime/type");
156 base::RunLoop().RunUntilIdle();
157 }
158
159 TEST_F(MediaDrmStorageImplTest, SaveAndLoadSession_SpecialCharacters) {
160 OnProvisioned();
161 SaveAndLoadPersistentSession("session.id", {1, 0}, "mime.type");
162 SaveAndLoadPersistentSession("session/id", {1, 0}, "mime/type");
163 SaveAndLoadPersistentSession("session-id", {1, 0}, "mime-type");
164 SaveAndLoadPersistentSession("session_id", {1, 0}, "mime_type");
165 SaveAndLoadPersistentSession("session,id", {1, 0}, "mime,type");
166 base::RunLoop().RunUntilIdle();
167 }
168
169 TEST_F(MediaDrmStorageImplTest, LoadSession_Unprovisioned) {
170 LoadPersistentSessionAndExpectFailure("session_id");
171 base::RunLoop().RunUntilIdle();
172 }
173
174 TEST_F(MediaDrmStorageImplTest, RemoveSession_Success) {
175 OnProvisioned();
176 SaveAndLoadPersistentSession("session_id", {1, 0}, "mime/type");
177 RemovePersistentSession("session_id", true);
178 LoadPersistentSessionAndExpectFailure("session_id");
179 base::RunLoop().RunUntilIdle();
180 }
181
182 TEST_F(MediaDrmStorageImplTest, RemoveSession_InvalidSession) {
183 OnProvisioned();
184 SaveAndLoadPersistentSession("session_id", {1, 0}, "mime/type");
185 RemovePersistentSession("invalid_session_id", false);
186 // Can still load "session_id" session.
187 LoadPersistentSession("session_id", {1, 0}, "mime/type");
188 base::RunLoop().RunUntilIdle();
189 }
190
191 } // namespace cdm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698