update_engine: Remove usage of non-literal format string.
FakeP2PManagerConfiguration used a private variable to store a
format string that unittests can change. This implies that we use a
non-literal format string with printf.
This patch instead uses {file_id} and {minsize} as markers on that
string that get replaced later to the passed values, avoiding the
non-literal format string. This also drops the exception flag from
the gyp file.
BUG=chromium:394166
TEST=USE="clang asan" FEATURES="test" cros_workon_make update_engine
Change-Id: I071a743e756c6214f4915b094520ef73ae902362
Reviewed-on: https://chromium-review.googlesource.com/208205
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/fake_p2p_manager_configuration.h b/fake_p2p_manager_configuration.h
index e71baae..8756630 100644
--- a/fake_p2p_manager_configuration.h
+++ b/fake_p2p_manager_configuration.h
@@ -14,6 +14,7 @@
#include <glib.h>
#include <base/logging.h>
+#include <base/strings/string_util.h>
#include <base/strings/stringprintf.h>
namespace chromeos_update_engine {
@@ -23,7 +24,8 @@
class FakeP2PManagerConfiguration : public P2PManager::Configuration {
public:
FakeP2PManagerConfiguration()
- : p2p_client_cmdline_format_("p2p-client --get-url=%s --minimum-size=%zu") {
+ : p2p_client_cmdline_format_(
+ "p2p-client --get-url={file_id} --minimum-size={minsize}") {
EXPECT_TRUE(utils::MakeTempDirectory("/tmp/p2p-tc.XXXXXX", &p2p_dir_));
SetInitctlStartCommandLine("initctl start p2p");
SetInitctlStopCommandLine("initctl stop p2p");
@@ -48,9 +50,14 @@
// P2PManager::Configuration override
virtual std::vector<std::string> GetP2PClientArgs(const std::string &file_id,
size_t minimum_size) {
- std::string formatted_command_line =
- base::StringPrintf(p2p_client_cmdline_format_.c_str(),
- file_id.c_str(), minimum_size);
+ std::string formatted_command_line = p2p_client_cmdline_format_;
+ // Replace {variable} on the passed string.
+ ReplaceSubstringsAfterOffset(
+ &formatted_command_line, 0, "{file_id}", file_id);
+ ReplaceSubstringsAfterOffset(
+ &formatted_command_line, 0,
+ "{minsize}", base::StringPrintf("%zu", minimum_size));
+
return ParseCommandLine(formatted_command_line);
}
@@ -66,14 +73,13 @@
initctl_stop_args_ = ParseCommandLine(command_line);
}
- // Use |command_line_format| instead of "p2p-client --get-url=%s
- // --minimum-size=%zu" when attempting to look up a file using
+ // Use |command_line_format| instead of "p2p-client --get-url={file_id}
+ // --minimum-size={minsize}" when attempting to look up a file using
// p2p-client(1).
//
- // The passed |command_line_format| argument should be a
- // printf()-style format string taking two arguments, the first
- // being the a C string for the p2p file id (e.g. %s) and the second
- // being a size_t with the minimum_size.
+ // The passed |command_line_format| argument can have "{file_id}" and
+ // "{minsize}" as substrings, that will be replaced by the corresponding
+ // values passed to GetP2PClientArgs().
void SetP2PClientCommandLine(const std::string &command_line_format) {
p2p_client_cmdline_format_ = command_line_format;
}
@@ -111,8 +117,8 @@
// Argument vector for stopping p2p.
std::vector<std::string> initctl_stop_args_;
- // A printf()-style format string for generating the p2p-client format.
- // See the SetP2PClientCommandLine() for details.
+ // A string for generating the p2p-client command. See the
+ // SetP2PClientCommandLine() for details.
std::string p2p_client_cmdline_format_;
DISALLOW_COPY_AND_ASSIGN(FakeP2PManagerConfiguration);
diff --git a/p2p_manager_unittest.cc b/p2p_manager_unittest.cc
index 262031f..af83d4a 100644
--- a/p2p_manager_unittest.cc
+++ b/p2p_manager_unittest.cc
@@ -437,7 +437,8 @@
// Emulate p2p-client returning valid URL with "fooX", 42 and "cros_au"
// being propagated in the right places.
- test_conf_->SetP2PClientCommandLine("echo 'http://1.2.3.4/%s_%zu'");
+ test_conf_->SetP2PClientCommandLine(
+ "echo 'http://1.2.3.4/{file_id}_{minsize}'");
manager->LookupUrlForFile("fooX", 42, TimeDelta(),
base::Bind(ExpectUrl,
"http://1.2.3.4/fooX.cros_au_42", loop));