1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151

media / mojo / services / fuchsia_cdm_provisioning_fetcher_impl_unittest.cc [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/mojo/services/fuchsia_cdm_provisioning_fetcher_impl.h"

#include <fuchsia/media/drm/cpp/fidl.h>
#include <memory>

#include "base/fuchsia/mem_buffer_util.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {
namespace {

namespace drm = ::fuchsia::media::drm;

using ::testing::_;
using ::testing::Eq;
using ::testing::Invoke;
using ::testing::WithArgs;

// This is a mock for the Chromium media::ProvisionFetcher (and not Fuchsia's
// similarly named ProvisioningFetcher protocol).
class MockProvisionFetcher : public ProvisionFetcher {
 public:
  MockProvisionFetcher() = default;
  ~MockProvisionFetcher() override = default;

  MOCK_METHOD(void,
              Retrieve,
              (const GURL& default_url,
               const std::string& request_data,
               ResponseCB response_cb),
              (override));
};

constexpr char kTestDefaultUrl[] = "http://test_default_url.com/";
constexpr char kTestRequest[] = "test_request_message";
constexpr char kTestResponse[] = "test_response_message";

drm::ProvisioningRequest CreateProvisioningRequest(
    fidl::StringPtr default_provisioning_server_url,
    const std::string& message) {
  drm::ProvisioningRequest request;
  request.default_provisioning_server_url =
      std::move(default_provisioning_server_url);
  request.message = base::MemBufferFromString(message, "provisioning_request");
  return request;
}

class FuchsiaCdmProvisioningFetcherImplTest : public ::testing::Test {
 public:
  FuchsiaCdmProvisioningFetcherImplTest() = default;

 protected:
  base::test::SingleThreadTaskEnvironment task_environment_{
      base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
};

TEST_F(FuchsiaCdmProvisioningFetcherImplTest, Fetch) {
  FuchsiaCdmProvisioningFetcherImpl fetcher(base::BindLambdaForTesting([]() {
    auto mock_provision_fetcher = std::make_unique<MockProvisionFetcher>();
    EXPECT_CALL(*mock_provision_fetcher,
                Retrieve(Eq(kTestDefaultUrl), Eq(kTestRequest), _))
        .WillOnce(WithArgs<2>(
            Invoke([](ProvisionFetcher::ResponseCB response_callback) {
              std::move(response_callback).Run(true, kTestResponse);
            })));

    return std::unique_ptr<ProvisionFetcher>(std::move(mock_provision_fetcher));
  }));

  fetcher.Bind(base::MakeExpectedNotRunClosure(FROM_HERE));

  std::optional<std::string> response_message;
  fetcher.Fetch(CreateProvisioningRequest(kTestDefaultUrl, kTestRequest),
                [&](drm::ProvisioningResponse response) {
                  response_message =
                      base::StringFromMemBuffer(response.message);
                });
  ASSERT_TRUE(response_message.has_value());
  EXPECT_EQ(*response_message, kTestResponse);
}

TEST_F(FuchsiaCdmProvisioningFetcherImplTest, RetrieveFails) {
  FuchsiaCdmProvisioningFetcherImpl fetcher(base::BindLambdaForTesting([]() {
    auto mock_provision_fetcher = std::make_unique<MockProvisionFetcher>();
    EXPECT_CALL(*mock_provision_fetcher, Retrieve(_, _, _))
        .WillOnce(WithArgs<2>(
            Invoke([](ProvisionFetcher::ResponseCB response_callback) {
              std::move(response_callback).Run(false, "");
            })));

    return std::unique_ptr<ProvisionFetcher>(std::move(mock_provision_fetcher));
  }));

  fetcher.Bind(base::MakeExpectedNotRunClosure(FROM_HERE));

  std::optional<std::string> response_message;
  fetcher.Fetch(CreateProvisioningRequest(kTestDefaultUrl, kTestRequest),
                [&](drm::ProvisioningResponse response) {
                  response_message =
                      base::StringFromMemBuffer(response.message);
                });
  ASSERT_TRUE(response_message.has_value());
  EXPECT_TRUE(response_message->empty());
}

TEST_F(FuchsiaCdmProvisioningFetcherImplTest, NoDefaultProvisioningUrl) {
  FuchsiaCdmProvisioningFetcherImpl fetcher(base::BindLambdaForTesting([]() {
    auto mock_provision_fetcher = std::make_unique<MockProvisionFetcher>();
    EXPECT_CALL(*mock_provision_fetcher, Retrieve(_, _, _))
        .WillOnce(WithArgs<2>(
            Invoke([](ProvisionFetcher::ResponseCB response_callback) {
              std::move(response_callback).Run(true, kTestResponse);
            })));

    return std::unique_ptr<ProvisionFetcher>(std::move(mock_provision_fetcher));
  }));

  fetcher.Bind(base::MakeExpectedRunClosure(FROM_HERE));

  fetcher.Fetch(CreateProvisioningRequest({}, kTestRequest),
                [](drm::ProvisioningResponse response) { FAIL(); });
}

TEST_F(FuchsiaCdmProvisioningFetcherImplTest, MultipleRequestsFails) {
  FuchsiaCdmProvisioningFetcherImpl fetcher(base::BindLambdaForTesting([]() {
    auto mock_provision_fetcher = std::make_unique<MockProvisionFetcher>();
    EXPECT_CALL(*mock_provision_fetcher, Retrieve(_, _, _)).Times(1);

    return std::unique_ptr<ProvisionFetcher>(std::move(mock_provision_fetcher));
  }));

  fetcher.Bind(base::MakeExpectedRunClosure(FROM_HERE));

  fetcher.Fetch(CreateProvisioningRequest(kTestDefaultUrl, kTestRequest),
                [](drm::ProvisioningResponse) { FAIL(); });
  fetcher.Fetch(CreateProvisioningRequest(kTestDefaultUrl, kTestRequest),
                [](drm::ProvisioningResponse) { FAIL(); });
}

}  // namespace

}  // namespace media