forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BlimpEngineConfig to verify command line switches and store config
Initially this has the shared secret between the client and the engine. BUG=575421 Review URL: https://codereview.chromium.org/1489413005 Cr-Commit-Position: refs/heads/master@{#369861}
- Loading branch information
marcinjb
authored and
Commit bot
committed
Jan 15, 2016
1 parent
0918e57
commit 446acf6
Showing
12 changed files
with
275 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2015 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 "blimp/engine/browser/blimp_engine_config.h" | ||
|
||
#include <string> | ||
|
||
#include "base/command_line.h" | ||
#include "base/files/file_path.h" | ||
#include "base/files/file_util.h" | ||
#include "base/memory/scoped_ptr.h" | ||
#include "base/strings/string_util.h" | ||
#include "blimp/engine/browser/switches.h" | ||
|
||
namespace blimp { | ||
namespace engine { | ||
|
||
namespace { | ||
// Gets the client token from the file provided by the command line. If a read | ||
// does not succeed, or the switch is malformed, an empty string is returned. | ||
std::string GetClientToken(const base::CommandLine& cmd_line) { | ||
std::string file_contents; | ||
const base::FilePath path = cmd_line.GetSwitchValuePath(kClientTokenPath); | ||
if (!base::ReadFileToString(path, &file_contents)) { | ||
LOG(ERROR) << "Could not read client token file at " | ||
<< (path.empty() ? "(not provided)" : path.AsUTF8Unsafe()); | ||
} | ||
return base::CollapseWhitespaceASCII(file_contents, true); | ||
} | ||
} // namespace | ||
|
||
BlimpEngineConfig::~BlimpEngineConfig() {} | ||
|
||
// static | ||
scoped_ptr<BlimpEngineConfig> BlimpEngineConfig::Create( | ||
const base::CommandLine& cmd_line) { | ||
const std::string client_token = GetClientToken(cmd_line); | ||
if (!client_token.empty()) { | ||
return make_scoped_ptr(new BlimpEngineConfig(client_token)); | ||
} | ||
return nullptr; | ||
} | ||
|
||
// static | ||
scoped_ptr<BlimpEngineConfig> BlimpEngineConfig::CreateForTest( | ||
const std::string& client_token) { | ||
return make_scoped_ptr(new BlimpEngineConfig(client_token)); | ||
} | ||
|
||
const std::string& BlimpEngineConfig::client_token() const { | ||
return client_token_; | ||
} | ||
|
||
BlimpEngineConfig::BlimpEngineConfig(const std::string& client_token) | ||
: client_token_(client_token) {} | ||
|
||
} // namespace engine | ||
} // namespace blimp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2015 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. | ||
|
||
#ifndef BLIMP_ENGINE_BROWSER_BLIMP_ENGINE_CONFIG_H_ | ||
#define BLIMP_ENGINE_BROWSER_BLIMP_ENGINE_CONFIG_H_ | ||
|
||
#include <string> | ||
|
||
#include "base/macros.h" | ||
#include "base/memory/scoped_ptr.h" | ||
|
||
namespace base { | ||
class CommandLine; | ||
} // namespace base | ||
|
||
namespace blimp { | ||
namespace engine { | ||
|
||
// Class to hold all of the configuration bits necessary for the Blimp engine. | ||
// | ||
// The BlimpEngineConfig class is initialized from parameters provided on the | ||
// command line. For the switches to pass verification: | ||
// * A client token filepath must be provided and the file must have a | ||
// non-empty token. | ||
// | ||
// The BlimpEngineConfig object is intended to live as long as the engine is | ||
// running. It should also be one of the first things to be set up. | ||
class BlimpEngineConfig { | ||
public: | ||
~BlimpEngineConfig(); | ||
|
||
// Attempts to create a BlimpEngineConfig based on the parameters in the | ||
// specified CommandLine. This validates all of the command line switches | ||
// and parses all files specified. Returns a non-null scoped_ptr on success. | ||
static scoped_ptr<BlimpEngineConfig> Create( | ||
const base::CommandLine& cmd_line); | ||
|
||
// Creates a BlimpEngineConfig based on individual components. Should only | ||
// be used for testing. | ||
static scoped_ptr<BlimpEngineConfig> CreateForTest( | ||
const std::string& client_token); | ||
|
||
// Returns the client token. | ||
const std::string& client_token() const; | ||
|
||
private: | ||
explicit BlimpEngineConfig(const std::string& client_token); | ||
|
||
const std::string client_token_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(BlimpEngineConfig); | ||
}; | ||
|
||
} // namespace engine | ||
} // namespace blimp | ||
|
||
#endif // BLIMP_ENGINE_BROWSER_BLIMP_ENGINE_CONFIG_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2015 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 "blimp/engine/browser/blimp_engine_config.h" | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "base/command_line.h" | ||
#include "base/files/file_util.h" | ||
#include "base/files/scoped_temp_dir.h" | ||
#include "base/memory/scoped_ptr.h" | ||
#include "base/strings/stringprintf.h" | ||
#include "blimp/engine/browser/switches.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace blimp { | ||
namespace engine { | ||
namespace { | ||
|
||
// Reference client token. | ||
static const char kTestClientToken[] = "Reference client token"; | ||
|
||
class BlimpEngineConfigTest : public testing::Test { | ||
protected: | ||
void SetUp() override { | ||
// Create a temporary directory and populate it with all of the switches | ||
// If a test requires a switch's file to be missing, call | ||
// RemoveFileForSwitch(). | ||
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | ||
CreateFileForSwitch(kClientTokenPath, kTestClientToken); | ||
} | ||
|
||
// Creates a file in the temp directory for a given filepath switch. | ||
void CreateFileForSwitch(const std::string& filepath_switch, | ||
const std::string& data) const { | ||
ASSERT_TRUE(base::WriteFile(GetFilepathForSwitch(filepath_switch), | ||
data.c_str(), data.size())); | ||
} | ||
|
||
// Removes the associated file for a given filepath switch. | ||
void RemoveFileForSwitch(const std::string& filepath_switch) const { | ||
base::DeleteFile(GetFilepathForSwitch(filepath_switch), false); | ||
} | ||
|
||
// Creates and returns a CommandLine object with specified filepath switches. | ||
base::CommandLine CreateCommandLine( | ||
const std::vector<std::string>& filepath_switches) { | ||
base::CommandLine::StringVector cmd_vec = {"dummy_program"}; | ||
for (const std::string& filepath_switch : filepath_switches) { | ||
cmd_vec.push_back(base::StringPrintf( | ||
"--%s=%s", filepath_switch.c_str(), | ||
GetFilepathForSwitch(filepath_switch).AsUTF8Unsafe().c_str())); | ||
} | ||
return base::CommandLine(cmd_vec); | ||
} | ||
|
||
base::FilePath GetFilepathForSwitch( | ||
const std::string& filepath_switch) const { | ||
return temp_dir_.path().Append(filepath_switch); | ||
} | ||
|
||
const std::vector<std::string> all_filepath_switches_ = {kClientTokenPath}; | ||
|
||
base::ScopedTempDir temp_dir_; | ||
}; | ||
|
||
TEST_F(BlimpEngineConfigTest, ClientTokenCorrect) { | ||
auto cmd_line = CreateCommandLine(all_filepath_switches_); | ||
auto engine_config = BlimpEngineConfig::Create(cmd_line); | ||
EXPECT_NE(nullptr, engine_config); | ||
EXPECT_EQ(kTestClientToken, engine_config->client_token()); | ||
} | ||
|
||
TEST_F(BlimpEngineConfigTest, ClientTokenEmpty) { | ||
RemoveFileForSwitch(kClientTokenPath); | ||
CreateFileForSwitch(kClientTokenPath, " "); | ||
auto cmd_line = CreateCommandLine(all_filepath_switches_); | ||
auto engine_config = BlimpEngineConfig::Create(cmd_line); | ||
EXPECT_EQ(nullptr, engine_config); | ||
} | ||
|
||
} // namespace | ||
} // namespace engine | ||
} // namespace blimp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2015 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 "blimp/engine/browser/switches.h" | ||
|
||
namespace blimp { | ||
namespace engine { | ||
|
||
const char kClientTokenPath[] = "blimp-client-token-path"; | ||
|
||
} // namespace engine | ||
} // namespace blimp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2015 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. | ||
|
||
#ifndef BLIMP_ENGINE_BROWSER_SWITCHES_H_ | ||
#define BLIMP_ENGINE_BROWSER_SWITCHES_H_ | ||
|
||
namespace blimp { | ||
namespace engine { | ||
|
||
// Path to the client token/shared secret between the engine and the client. | ||
extern const char kClientTokenPath[]; | ||
|
||
} // namespace engine | ||
} // namespace blimp | ||
|
||
#endif // BLIMP_ENGINE_BROWSER_SWITCHES_H_ |