1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/mojo/services/fuchsia_cdm_provisioning_fetcher_impl.h"
7 #include <fuchsia/media/drm/cpp/fidl.h>
10 #include "base/fuchsia/mem_buffer_util.h"
11 #include "base/functional/bind.h"
12 #include "base/location.h"
13 #include "base/test/bind.h"
14 #include "base/test/task_environment.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
21 namespace drm = ::fuchsia::media::drm;
25 using ::testing::Invoke;
26 using ::testing::WithArgs;
28 // This is a mock for the Chromium media::ProvisionFetcher (and not Fuchsia's
29 // similarly named ProvisioningFetcher protocol).
30 class MockProvisionFetcher : public ProvisionFetcher {
32 MockProvisionFetcher() = default;
33 ~MockProvisionFetcher() override = default;
37 (const GURL& default_url,
38 const std::string& request_data,
39 ResponseCB response_cb),
43 constexpr char kTestDefaultUrl[] = "http://test_default_url.com/";
44 constexpr char kTestRequest[] = "test_request_message";
45 constexpr char kTestResponse[] = "test_response_message";
47 drm::ProvisioningRequest CreateProvisioningRequest(
48 fidl::StringPtr default_provisioning_server_url,
49 const std::string& message) {
50 drm::ProvisioningRequest request;
51 request.default_provisioning_server_url =
52 std::move(default_provisioning_server_url);
53 request.message = base::MemBufferFromString(message, "provisioning_request");
57 class FuchsiaCdmProvisioningFetcherImplTest : public ::testing::Test {
59 FuchsiaCdmProvisioningFetcherImplTest() = default;
62 base::test::SingleThreadTaskEnvironment task_environment_{
63 base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
66 TEST_F(FuchsiaCdmProvisioningFetcherImplTest, Fetch) {
67 FuchsiaCdmProvisioningFetcherImpl fetcher(base::BindLambdaForTesting([]() {
68 auto mock_provision_fetcher = std::make_unique<MockProvisionFetcher>();
69 EXPECT_CALL(*mock_provision_fetcher,
70 Retrieve(Eq(kTestDefaultUrl), Eq(kTestRequest), _))
71 .WillOnce(WithArgs<2>(
72 Invoke([](ProvisionFetcher::ResponseCB response_callback) {
73 std::move(response_callback).Run(true, kTestResponse);
76 return std::unique_ptr<ProvisionFetcher>(std::move(mock_provision_fetcher));
79 fetcher.Bind(base::MakeExpectedNotRunClosure(FROM_HERE));
81 absl::optional<std::string> response_message;
82 fetcher.Fetch(CreateProvisioningRequest(kTestDefaultUrl, kTestRequest),
83 [&](drm::ProvisioningResponse response) {
85 base::StringFromMemBuffer(response.message);
87 ASSERT_TRUE(response_message.has_value());
88 EXPECT_EQ(*response_message, kTestResponse);
91 TEST_F(FuchsiaCdmProvisioningFetcherImplTest, RetrieveFails) {
92 FuchsiaCdmProvisioningFetcherImpl fetcher(base::BindLambdaForTesting([]() {
93 auto mock_provision_fetcher = std::make_unique<MockProvisionFetcher>();
94 EXPECT_CALL(*mock_provision_fetcher, Retrieve(_, _, _))
95 .WillOnce(WithArgs<2>(
96 Invoke([](ProvisionFetcher::ResponseCB response_callback) {
97 std::move(response_callback).Run(false, "");
100 return std::unique_ptr<ProvisionFetcher>(std::move(mock_provision_fetcher));
103 fetcher.Bind(base::MakeExpectedNotRunClosure(FROM_HERE));
105 absl::optional<std::string> response_message;
106 fetcher.Fetch(CreateProvisioningRequest(kTestDefaultUrl, kTestRequest),
107 [&](drm::ProvisioningResponse response) {
109 base::StringFromMemBuffer(response.message);
111 ASSERT_TRUE(response_message.has_value());
112 EXPECT_TRUE(response_message->empty());
115 TEST_F(FuchsiaCdmProvisioningFetcherImplTest, NoDefaultProvisioningUrl) {
116 FuchsiaCdmProvisioningFetcherImpl fetcher(base::BindLambdaForTesting([]() {
117 auto mock_provision_fetcher = std::make_unique<MockProvisionFetcher>();
118 EXPECT_CALL(*mock_provision_fetcher, Retrieve(_, _, _))
119 .WillOnce(WithArgs<2>(
120 Invoke([](ProvisionFetcher::ResponseCB response_callback) {
121 std::move(response_callback).Run(true, kTestResponse);
124 return std::unique_ptr<ProvisionFetcher>(std::move(mock_provision_fetcher));
127 fetcher.Bind(base::MakeExpectedRunClosure(FROM_HERE));
129 fetcher.Fetch(CreateProvisioningRequest({}, kTestRequest),
130 [](drm::ProvisioningResponse response) { FAIL(); });
133 TEST_F(FuchsiaCdmProvisioningFetcherImplTest, MultipleRequestsFails) {
134 FuchsiaCdmProvisioningFetcherImpl fetcher(base::BindLambdaForTesting([]() {
135 auto mock_provision_fetcher = std::make_unique<MockProvisionFetcher>();
136 EXPECT_CALL(*mock_provision_fetcher, Retrieve(_, _, _)).Times(1);
138 return std::unique_ptr<ProvisionFetcher>(std::move(mock_provision_fetcher));
141 fetcher.Bind(base::MakeExpectedRunClosure(FROM_HERE));
143 fetcher.Fetch(CreateProvisioningRequest(kTestDefaultUrl, kTestRequest),
144 [](drm::ProvisioningResponse) { FAIL(); });
145 fetcher.Fetch(CreateProvisioningRequest(kTestDefaultUrl, kTestRequest),
146 [](drm::ProvisioningResponse) { FAIL(); });