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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/media/wv_test_license_server_config.h"
#include "base/command_line.h"
#include "base/environment.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/rand_util.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "net/base/ip_address.h"
#include "net/base/net_errors.h"
#include "net/log/net_log_source.h"
#include "net/socket/tcp_server_socket.h"
#include "net/test/python_utils.h"
#if BUILDFLAG(IS_APPLE)
#include "base/apple/foundation_util.h"
#endif
namespace {
const uint16_t kMinPort = 17000;
const uint16_t kPortRangeSize = 1000;
// Widevine license server configuration files.
const base::FilePath::CharType kKeysFileName[] = FILE_PATH_LITERAL("keys.dat");
const base::FilePath::CharType kPoliciesFileName[] =
FILE_PATH_LITERAL("policies.dat");
const base::FilePath::CharType kProfilesFileName[] =
FILE_PATH_LITERAL("profiles.dat");
// License server configuration files directory name relative to root.
const base::FilePath::CharType kLicenseServerConfigDirName[] =
FILE_PATH_LITERAL("config");
bool GetPyProtoPath(base::FilePath* dir) {
// Locate the Python code generated by the protocol buffers compiler.
base::FilePath generated_code_dir;
if (!base::PathService::Get(base::DIR_EXE, &generated_code_dir)) {
LOG(ERROR) << "Can't find " << generated_code_dir.value();
return false;
}
#if BUILDFLAG(IS_APPLE)
if (base::apple::AmIBundled()) {
generated_code_dir = generated_code_dir.DirName().DirName().DirName();
}
#endif
const base::FilePath kPyProto(FILE_PATH_LITERAL("pyproto"));
if (base::DirectoryExists(generated_code_dir.Append(kPyProto))) {
*dir = generated_code_dir.Append(kPyProto);
return true;
}
return false;
}
} // namespace
WVTestLicenseServerConfig::WVTestLicenseServerConfig() = default;
WVTestLicenseServerConfig::~WVTestLicenseServerConfig() = default;
bool WVTestLicenseServerConfig::GetServerCommandLine(
base::CommandLine* command_line) {
if (!GetPython3Command(command_line)) {
LOG(ERROR) << "Could not get Python runtime command.";
return false;
}
base::FilePath license_server_path;
GetLicenseServerPath(&license_server_path);
if (!base::PathExists(license_server_path)) {
LOG(WARNING) << "Missing license server file at "
<< license_server_path.value();
return false;
}
base::FilePath server_root;
GetLicenseServerRootPath(&server_root);
base::FilePath config_path = server_root.Append(kLicenseServerConfigDirName);
if (!base::PathExists(config_path.Append(kKeysFileName)) ||
!base::PathExists(config_path.Append(kPoliciesFileName)) ||
!base::PathExists(config_path.Append(kProfilesFileName))) {
LOG(WARNING) << "Missing license server configuration files at "
<< config_path;
return false;
}
if (!SelectServerPort())
return false;
// Needed to dynamically load .so libraries used by license server.
// TODO(shadi): Remove need to set env variable once b/12932983 is fixed.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
std::unique_ptr<base::Environment> env(base::Environment::Create());
const char kLibraryPathEnvVarName[] = "LD_LIBRARY_PATH";
std::string library_paths(license_server_path.DirName().value());
std::optional<std::string> old_path = env->GetVar(kLibraryPathEnvVarName);
if (old_path.has_value()) {
library_paths.append(":").append(*old_path);
}
env->SetVar(kLibraryPathEnvVarName, library_paths);
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// Since it is a Python command line, we need to AppendArg instead of
// AppendSwitch so that the arguments are passed to the Python server instead
// of Python engine.
command_line->AppendArgPath(license_server_path);
command_line->AppendArg("-k");
command_line->AppendArgPath(config_path.Append(kKeysFileName));
command_line->AppendArg("-o");
command_line->AppendArgPath(config_path.Append(kPoliciesFileName));
command_line->AppendArg("-r");
command_line->AppendArgPath(config_path.Append(kProfilesFileName));
command_line->AppendArg(base::StringPrintf("--port=%u", port_));
return true;
}
std::optional<base::EnvironmentMap>
WVTestLicenseServerConfig::GetServerEnvironment() {
// Add the Python protocol buffers files directory to Python path.
base::FilePath pyproto_dir;
if (!GetPyProtoPath(&pyproto_dir)) {
LOG(WARNING) << "Cannot find pyproto directory required by license server.";
return std::nullopt;
}
base::EnvironmentMap map;
SetPythonPathInEnvironment({pyproto_dir}, &map);
return map;
}
bool WVTestLicenseServerConfig::SelectServerPort() {
// Try all ports within the range of kMinPort to (kMinPort + kPortRangeSize)
// Instead of starting from kMinPort, use a random port within that range.
uint16_t start_seed = base::RandInt(0, kPortRangeSize);
uint16_t try_port = 0;
for (uint16_t i = 0; i < kPortRangeSize; ++i) {
try_port = kMinPort + (start_seed + i) % kPortRangeSize;
net::NetLogSource source;
net::TCPServerSocket sock(nullptr, source);
if (sock.Listen(net::IPEndPoint(net::IPAddress::IPv4Localhost(), try_port),
1, /*ipv6_only=*/std::nullopt) == net::OK) {
port_ = try_port;
return true;
}
}
LOG(WARNING) << "Could not find an open port in the range of " << kMinPort
<< " to " << kMinPort + kPortRangeSize;
return false;
}
bool WVTestLicenseServerConfig::IsPlatformSupported() {
// TODO(crbug.com/40167923): Reenable OS_LINUX once license server
// (or Widevine CDM) updated.
#if BUILDFLAG(IS_CHROMEOS) && defined(ARCH_CPU_X86_64)
return true;
#else
return false;
#endif // BUILDFLAG(IS_CHROMEOS) && defined(ARCH_CPU_X86_64)
}
std::string WVTestLicenseServerConfig::GetServerURL() {
return base::StringPrintf("http://localhost:%u/license_server", port_);
}
void WVTestLicenseServerConfig::GetLicenseServerPath(base::FilePath *path) {
base::FilePath server_root;
GetLicenseServerRootPath(&server_root);
// Platform-specific license server binary path relative to root.
*path =
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
server_root.Append(FILE_PATH_LITERAL("linux"))
.Append(FILE_PATH_LITERAL("license_server.py"));
#else
server_root.Append(FILE_PATH_LITERAL("unsupported_platform"));
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
}
void WVTestLicenseServerConfig::GetLicenseServerRootPath(
base::FilePath* path) {
base::FilePath source_root;
base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, &source_root);
*path = source_root.Append(FILE_PATH_LITERAL("third_party"))
.Append(FILE_PATH_LITERAL("widevine"))
.Append(FILE_PATH_LITERAL("test"))
.Append(FILE_PATH_LITERAL("license_server"));
}
|