1 // Copyright 2014 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.
5 #include "chrome/browser/media/wv_test_license_server_config.h"
7 #include "base/command_line.h"
8 #include "base/environment.h"
9 #include "base/files/file_util.h"
10 #include "base/path_service.h"
11 #include "base/rand_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "net/base/net_errors.h"
14 #include "net/socket/tcp_server_socket.h"
15 #include "net/test/python_utils.h"
18 const uint16 kMinPort
= 17000;
19 const uint16 kPortRangeSize
= 1000;
21 // Widevine license server configuration files.
22 const base::FilePath::CharType kKeysFileName
[] =
23 FILE_PATH_LITERAL("keys.dat");
24 const base::FilePath::CharType kPoliciesFileName
[] =
25 FILE_PATH_LITERAL("policies.dat");
26 const base::FilePath::CharType kProfilesFileName
[] =
27 FILE_PATH_LITERAL("profiles.dat");
29 // License server configuration files directory name relative to root.
30 const base::FilePath::CharType kLicenseServerConfigDirName
[] =
31 FILE_PATH_LITERAL("config");
33 WVTestLicenseServerConfig::WVTestLicenseServerConfig() {
36 WVTestLicenseServerConfig::~WVTestLicenseServerConfig() {
39 bool WVTestLicenseServerConfig::GetServerCommandLine(
40 base::CommandLine
* command_line
) {
41 if (!GetPythonCommand(command_line
)) {
42 LOG(ERROR
) << "Could not get Python runtime command.";
46 // Add the Python protocol buffers files directory to Python path.
47 base::FilePath pyproto_dir
;
48 if (!GetPyProtoPath(&pyproto_dir
)) {
49 DVLOG(0) << "Cannot find pyproto directory required by license server.";
52 AppendToPythonPath(pyproto_dir
);
54 base::FilePath license_server_path
;
55 GetLicenseServerPath(&license_server_path
);
56 if (!base::PathExists(license_server_path
)) {
57 DVLOG(0) << "Missing license server file at "
58 << license_server_path
.value();
62 base::FilePath server_root
;
63 GetLicenseServerRootPath(&server_root
);
64 base::FilePath config_path
= server_root
.Append(kLicenseServerConfigDirName
);
66 if (!base::PathExists(config_path
.Append(kKeysFileName
)) ||
67 !base::PathExists(config_path
.Append(kPoliciesFileName
)) ||
68 !base::PathExists(config_path
.Append(kProfilesFileName
))) {
69 DVLOG(0) << "Missing license server configuration files.";
73 if (!SelectServerPort())
76 // Needed to dynamically load .so libraries used by license server.
77 // TODO(shadi): Remove need to set env variable once b/12932983 is fixed.
79 scoped_ptr
<base::Environment
> env(base::Environment::Create());
80 const char kLibraryPathEnvVarName
[] = "LD_LIBRARY_PATH";
81 std::string
library_paths(license_server_path
.DirName().value());
83 if (env
->GetVar(kLibraryPathEnvVarName
, &old_path
))
84 library_paths
.append(":").append(old_path
);
85 env
->SetVar(kLibraryPathEnvVarName
, library_paths
);
86 #endif // defined(OS_LINUX)
88 // Since it is a Python command line, we need to AppendArg instead of
89 // AppendSwitch so that the arguments are passed to the Python server instead
91 command_line
->AppendArgPath(license_server_path
);
92 command_line
->AppendArg("-k");
93 command_line
->AppendArgPath(config_path
.Append(kKeysFileName
));
94 command_line
->AppendArg("-o");
95 command_line
->AppendArgPath(config_path
.Append(kPoliciesFileName
));
96 command_line
->AppendArg("-r");
97 command_line
->AppendArgPath(config_path
.Append(kProfilesFileName
));
98 command_line
->AppendArg(base::StringPrintf("--port=%u", port_
));
102 bool WVTestLicenseServerConfig::SelectServerPort() {
103 // Try all ports within the range of kMinPort to (kMinPort + kPortRangeSize)
104 // Instead of starting from kMinPort, use a random port within that range.
105 net::IPAddressNumber address
;
106 net::ParseIPLiteralToNumber("127.0.0.1", &address
);
107 uint16 start_seed
= base::RandInt(0, kPortRangeSize
);
109 for (uint16 i
= 0; i
< kPortRangeSize
; ++i
) {
110 try_port
= kMinPort
+ (start_seed
+ i
) % kPortRangeSize
;
111 net::NetLog::Source source
;
112 net::TCPServerSocket
sock(NULL
, source
);
113 if (sock
.Listen(net::IPEndPoint(address
, try_port
), 1) == net::OK
) {
118 DVLOG(0) << "Could not find an open port in the range of " << kMinPort
<<
119 " to " << kMinPort
+ kPortRangeSize
;
123 bool WVTestLicenseServerConfig::IsPlatformSupported() {
124 #if defined(OS_LINUX) && defined(ARCH_CPU_X86_64)
128 #endif // defined(OS_LINUX)
131 std::string
WVTestLicenseServerConfig::GetServerURL() {
132 return base::StringPrintf("http://localhost:%u/license_server", port_
);
135 void WVTestLicenseServerConfig::GetLicenseServerPath(base::FilePath
*path
) {
136 base::FilePath server_root
;
137 GetLicenseServerRootPath(&server_root
);
138 // Platform-specific license server binary path relative to root.
140 #if defined(OS_LINUX)
141 server_root
.Append(FILE_PATH_LITERAL("linux"))
142 .Append(FILE_PATH_LITERAL("license_server.py"));
144 server_root
.Append(FILE_PATH_LITERAL("unsupported_platform"));
145 #endif // defined(OS_LINUX)
148 void WVTestLicenseServerConfig::GetLicenseServerRootPath(
149 base::FilePath
* path
) {
150 base::FilePath source_root
;
151 PathService::Get(base::DIR_SOURCE_ROOT
, &source_root
);
152 *path
= source_root
.Append(FILE_PATH_LITERAL("third_party"))
153 .Append(FILE_PATH_LITERAL("widevine"))
154 .Append(FILE_PATH_LITERAL("test"))
155 .Append(FILE_PATH_LITERAL("license_server"));