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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// 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 "net/base/net_errors.h"
#include "net/socket/tcp_server_socket.h"
#include "net/test/python_utils.h"
const uint16 kMinPort = 17000;
const uint16 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");
WVTestLicenseServerConfig::WVTestLicenseServerConfig() {
}
WVTestLicenseServerConfig::~WVTestLicenseServerConfig() {
}
bool WVTestLicenseServerConfig::GetServerCommandLine(
base::CommandLine* command_line) {
if (!GetPythonCommand(command_line)) {
LOG(ERROR) << "Could not get Python runtime command.";
return false;
}
// Add the Python protocol buffers files directory to Python path.
base::FilePath pyproto_dir;
if (!GetPyProtoPath(&pyproto_dir)) {
DVLOG(0) << "Cannot find pyproto directory required by license server.";
return false;
}
AppendToPythonPath(pyproto_dir);
base::FilePath license_server_path;
GetLicenseServerPath(&license_server_path);
if (!base::PathExists(license_server_path)) {
DVLOG(0) << "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))) {
DVLOG(0) << "Missing license server configuration files.";
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 defined(OS_LINUX)
scoped_ptr<base::Environment> env(base::Environment::Create());
const char kLibraryPathEnvVarName[] = "LD_LIBRARY_PATH";
std::string library_paths(license_server_path.DirName().value());
std::string old_path;
if (env->GetVar(kLibraryPathEnvVarName, &old_path))
library_paths.append(":").append(old_path);
env->SetVar(kLibraryPathEnvVarName, library_paths);
#endif // defined(OS_LINUX)
// 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;
}
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.
net::IPAddressNumber address;
net::ParseIPLiteralToNumber("127.0.0.1", &address);
uint16 start_seed = base::RandInt(0, kPortRangeSize);
uint16 try_port = 0;
for (uint16 i = 0; i < kPortRangeSize; ++i) {
try_port = kMinPort + (start_seed + i) % kPortRangeSize;
net::NetLog::Source source;
net::TCPServerSocket sock(NULL, source);
if (sock.Listen(net::IPEndPoint(address, try_port), 1) == net::OK) {
port_ = try_port;
return true;
}
}
DVLOG(0) << "Could not find an open port in the range of " << kMinPort <<
" to " << kMinPort + kPortRangeSize;
return false;
}
bool WVTestLicenseServerConfig::IsPlatformSupported() {
#if defined(OS_LINUX) && defined(ARCH_CPU_X86_64)
return true;
#else
return false;
#endif // defined(OS_LINUX)
}
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 defined(OS_LINUX)
server_root.Append(FILE_PATH_LITERAL("linux"))
.Append(FILE_PATH_LITERAL("license_server.py"));
#else
server_root.Append(FILE_PATH_LITERAL("unsupported_platform"));
#endif // defined(OS_LINUX)
}
void WVTestLicenseServerConfig::GetLicenseServerRootPath(
base::FilePath* path) {
base::FilePath source_root;
PathService::Get(base::DIR_SOURCE_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"));
}
|