Turn OmahaRequestPrepAction into OmahaRequestDeviceParams.
Pass the params to OmahaRequestAction's ctor. This simplifies a bit
executing as well as testing of OmahaRequestAction and testing of
OmahaRequestDeviceParams. It also allows us to initialize the params
once per update attempt and use them for all OmahaRequestActions.
BUG=560
TEST=unit tests, gmerged on device and forced an update through dev server,
inspected logs.
Review URL: http://codereview.chromium.org/2836053
diff --git a/SConstruct b/SConstruct
index df29f86..c3960bd 100644
--- a/SConstruct
+++ b/SConstruct
@@ -210,7 +210,7 @@
marshal.glibmarshal.c
omaha_hash_calculator.cc
omaha_request_action.cc
- omaha_request_prep_action.cc
+ omaha_request_params.cc
omaha_response_handler_action.cc
postinstall_runner_action.cc
set_bootable_flag_action.cc
@@ -243,7 +243,7 @@
mock_http_fetcher.cc
omaha_hash_calculator_unittest.cc
omaha_request_action_unittest.cc
- omaha_request_prep_action_unittest.cc
+ omaha_request_params_unittest.cc
omaha_response_handler_action_unittest.cc
postinstall_runner_action_unittest.cc
set_bootable_flag_action_unittest.cc
diff --git a/main.cc b/main.cc
index ed2fcf5..52ace9a 100644
--- a/main.cc
+++ b/main.cc
@@ -37,7 +37,7 @@
gboolean PeriodicallyUpdate(void* arg) {
PeriodicallyUpdateArgs* args = reinterpret_cast<PeriodicallyUpdateArgs*>(arg);
- args->update_attempter->Update(false);
+ args->update_attempter->Update();
return args->should_repeat;
}
@@ -98,7 +98,7 @@
PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
LOG(INFO) << "Chrome OS Update Engine starting";
-
+
// Create the single GMainLoop
GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
diff --git a/omaha_request_action.cc b/omaha_request_action.cc
index 9d14a6d..927c7d3 100644
--- a/omaha_request_action.cc
+++ b/omaha_request_action.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -13,6 +13,7 @@
#include "base/string_util.h"
#include "chromeos/obsolete_logging.h"
#include "update_engine/action_pipe.h"
+#include "update_engine/omaha_request_params.h"
#include "update_engine/utils.h"
using std::string;
@@ -109,16 +110,16 @@
return string(reinterpret_cast<const char *>(str.get()));
}
-OmahaRequestAction::OmahaRequestAction(OmahaEvent* event,
+OmahaRequestAction::OmahaRequestAction(const OmahaRequestParams& params,
+ OmahaEvent* event,
HttpFetcher* http_fetcher)
- : event_(event),
+ : params_(params),
+ event_(event),
http_fetcher_(http_fetcher) {}
OmahaRequestAction::~OmahaRequestAction() {}
void OmahaRequestAction::PerformAction() {
- CHECK(HasInputObject());
- params_ = GetInputObject();
http_fetcher_->set_delegate(this);
string request_post(FormatRequest(event_.get(), params_));
http_fetcher_->SetPostData(request_post.data(), request_post.size());
diff --git a/omaha_request_action.h b/omaha_request_action.h
index 582fe5f..0f066e3 100644
--- a/omaha_request_action.h
+++ b/omaha_request_action.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -26,54 +26,6 @@
// UTF-8 formatted. Output will be UTF-8 formatted.
std::string XmlEncode(const std::string& input);
-// This struct encapsulates the data Omaha gets for the request.
-// These strings in this struct should not be XML escaped.
-struct OmahaRequestParams {
- OmahaRequestParams()
- : os_platform(kOsPlatform), os_version(kOsVersion), app_id(kAppId) {}
- OmahaRequestParams(const std::string& in_machine_id,
- const std::string& in_user_id,
- const std::string& in_os_platform,
- const std::string& in_os_version,
- const std::string& in_os_sp,
- const std::string& in_os_board,
- const std::string& in_app_id,
- const std::string& in_app_version,
- const std::string& in_app_lang,
- const std::string& in_app_track,
- const std::string& in_update_url)
- : machine_id(in_machine_id),
- user_id(in_user_id),
- os_platform(in_os_platform),
- os_version(in_os_version),
- os_sp(in_os_sp),
- os_board(in_os_board),
- app_id(in_app_id),
- app_version(in_app_version),
- app_lang(in_app_lang),
- app_track(in_app_track),
- update_url(in_update_url) {}
-
- std::string machine_id;
- std::string user_id;
- std::string os_platform;
- std::string os_version;
- std::string os_sp;
- std::string os_board;
- std::string app_id;
- std::string app_version;
- std::string app_lang;
- std::string app_track;
-
- std::string update_url;
-
- // Suggested defaults
- static const char* const kAppId;
- static const char* const kOsPlatform;
- static const char* const kOsVersion;
- static const char* const kUpdateUrl;
-};
-
// This struct encapsulates the data Omaha's response for the request.
// These strings in this struct are not XML escaped.
struct OmahaResponse {
@@ -123,14 +75,15 @@
int error_code;
};
-class OmahaRequestAction;
class NoneType;
+class OmahaRequestAction;
+struct OmahaRequestParams;
template<>
class ActionTraits<OmahaRequestAction> {
public:
// Takes parameters on the input pipe.
- typedef OmahaRequestParams InputObjectType;
+ typedef NoneType InputObjectType;
// On UpdateCheck success, puts the Omaha response on output. Event
// requests do not have an output pipe.
typedef OmahaResponse OutputObjectType;
@@ -150,10 +103,11 @@
// Event requests always succeed.
//
// A good calling pattern is:
- // OmahaRequestAction(new OmahaEvent(...), new WhateverHttpFetcher);
+ // OmahaRequestAction(..., new OmahaEvent(...), new WhateverHttpFetcher);
// or
- // OmahaRequestAction(NULL, new WhateverHttpFetcher);
- OmahaRequestAction(OmahaEvent* event,
+ // OmahaRequestAction(..., NULL, new WhateverHttpFetcher);
+ OmahaRequestAction(const OmahaRequestParams& params,
+ OmahaEvent* event,
HttpFetcher* http_fetcher);
virtual ~OmahaRequestAction();
typedef ActionTraits<OmahaRequestAction>::InputObjectType InputObjectType;
@@ -174,8 +128,8 @@
bool IsEvent() const { return event_.get() != NULL; }
private:
- // These are data that are passed in the request to the Omaha server
- OmahaRequestParams params_;
+ // These are data that are passed in the request to the Omaha server.
+ const OmahaRequestParams& params_;
// Pointer to the OmahaEvent info. This is an UpdateCheck request if NULL.
scoped_ptr<OmahaEvent> event_;
diff --git a/omaha_request_action_unittest.cc b/omaha_request_action_unittest.cc
index 0e06439..cd91f68 100644
--- a/omaha_request_action_unittest.cc
+++ b/omaha_request_action_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -13,6 +13,7 @@
#include "update_engine/mock_http_fetcher.h"
#include "update_engine/omaha_hash_calculator.h"
#include "update_engine/omaha_request_action.h"
+#include "update_engine/omaha_request_params.h"
#include "update_engine/test_utils.h"
using std::string;
@@ -129,20 +130,16 @@
GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
http_response.size());
- ObjectFeederAction<OmahaRequestParams> feeder_action;
- OmahaRequestAction action(NULL, fetcher); // takes ownership of fetcher
+ OmahaRequestAction action(params, NULL, fetcher);
OmahaRequestActionTestProcessorDelegate delegate;
delegate.loop_ = loop;
delegate.expected_success_ = expected_success;
+
ActionProcessor processor;
- feeder_action.set_obj(params);
processor.set_delegate(&delegate);
- processor.EnqueueAction(&feeder_action);
processor.EnqueueAction(&action);
OutputObjectCollectorAction collector_action;
-
- BondActions(&feeder_action, &action);
BondActions(&action, &collector_action);
processor.EnqueueAction(&collector_action);
@@ -166,18 +163,13 @@
GMainLoop* loop = g_main_loop_new(g_main_context_default(), FALSE);
MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
http_response.size());
- ObjectFeederAction<OmahaRequestParams> feeder_action;
- OmahaRequestAction action(event, fetcher); // takes ownership of fetcher
+ OmahaRequestAction action(params, event, fetcher);
OmahaRequestActionTestProcessorDelegate delegate;
delegate.loop_ = loop;
ActionProcessor processor;
- feeder_action.set_obj(params);
processor.set_delegate(&delegate);
- processor.EnqueueAction(&feeder_action);
processor.EnqueueAction(&action);
- BondActions(&feeder_action, &action);
-
g_timeout_add(0, &StartProcessorInRunLoop, &processor);
g_main_loop_run(loop);
g_main_loop_unref(loop);
@@ -259,18 +251,14 @@
GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
- ObjectFeederAction<OmahaRequestParams> feeder_action;
- feeder_action.set_obj(params);
- OmahaRequestAction action(NULL,
+ OmahaRequestAction action(params, NULL,
new MockHttpFetcher(http_response.data(),
http_response.size()));
OmahaRequestActionTestProcessorDelegate delegate;
delegate.loop_ = loop;
ActionProcessor processor;
processor.set_delegate(&delegate);
- processor.EnqueueAction(&feeder_action);
processor.EnqueueAction(&action);
- BondActions(&feeder_action, &action);
g_timeout_add(0, &StartProcessorInRunLoop, &processor);
g_main_loop_run(loop);
@@ -450,18 +438,14 @@
string http_response("doesn't matter");
GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
- ObjectFeederAction<OmahaRequestParams> feeder_action;
- feeder_action.set_obj(params);
- OmahaRequestAction action(NULL,
+ OmahaRequestAction action(params, NULL,
new MockHttpFetcher(http_response.data(),
http_response.size()));
TerminateEarlyTestProcessorDelegate delegate;
delegate.loop_ = loop;
ActionProcessor processor;
processor.set_delegate(&delegate);
- processor.EnqueueAction(&feeder_action);
processor.EnqueueAction(&action);
- BondActions(&feeder_action, &action);
g_timeout_add(0, &TerminateTransferTestStarter, &processor);
g_main_loop_run(loop);
@@ -629,14 +613,27 @@
TEST(OmahaRequestActionTest, IsEventTest) {
string http_response("doesn't matter");
+ OmahaRequestParams params("machine_id",
+ "user_id",
+ OmahaRequestParams::kOsPlatform,
+ OmahaRequestParams::kOsVersion,
+ "service_pack",
+ "x86-generic",
+ OmahaRequestParams::kAppId,
+ "0.1.0.0",
+ "en-US",
+ "unittest_track",
+ "http://url");
OmahaRequestAction update_check_action(
+ params,
NULL,
new MockHttpFetcher(http_response.data(),
http_response.size()));
EXPECT_FALSE(update_check_action.IsEvent());
OmahaRequestAction event_action(
+ params,
new OmahaEvent(OmahaEvent::kTypeInstallComplete,
OmahaEvent::kResultError,
0),
diff --git a/omaha_request_params.cc b/omaha_request_params.cc
new file mode 100644
index 0000000..502f7b0
--- /dev/null
+++ b/omaha_request_params.cc
@@ -0,0 +1,115 @@
+// Copyright (c) 2010 The Chromium OS 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 "update_engine/omaha_request_params.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/utsname.h>
+
+#include <map>
+#include <string>
+
+#include "base/string_util.h"
+#include "update_engine/simple_key_value_store.h"
+#include "update_engine/utils.h"
+
+using std::map;
+using std::string;
+
+namespace {
+const string OmahaIdPath() {
+ return string(chromeos_update_engine::utils::kStatefulPartition) +
+ "/etc/omaha_id";
+}
+} // namespace {}
+
+namespace chromeos_update_engine {
+
+bool OmahaRequestDeviceParams::Init() {
+ TEST_AND_RETURN_FALSE(GetMachineId(&machine_id));
+ user_id = machine_id;
+ os_platform = OmahaRequestParams::kOsPlatform;
+ os_version = OmahaRequestParams::kOsVersion;
+ app_version = GetLsbValue("CHROMEOS_RELEASE_VERSION", "");
+ os_sp = app_version + "_" + GetMachineType();
+ os_board = GetLsbValue("CHROMEOS_RELEASE_BOARD", "");
+ app_id = OmahaRequestParams::kAppId;
+ app_lang = "en-US";
+ app_track = GetLsbValue("CHROMEOS_RELEASE_TRACK", "");
+ update_url = GetLsbValue("CHROMEOS_AUSERVER",
+ OmahaRequestParams::kUpdateUrl);
+ return true;
+}
+
+namespace {
+const size_t kGuidDataByteLength = 128 / 8;
+const string::size_type kGuidStringLength = 38;
+// Formats 16 bytes (128 bits) of data as a GUID:
+// "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" where X is a hex digit
+string GuidFromData(const unsigned char data[kGuidDataByteLength]) {
+ return StringPrintf(
+ "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
+ data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
+ data[8], data[9], data[10], data[11], data[12], data[13], data[14],
+ data[15]);
+}
+}
+
+// Returns true on success.
+bool OmahaRequestDeviceParams::GetMachineId(std::string* out_id) const {
+ // Checks if we have an existing Machine ID.
+ const string omaha_id_path = root_ + OmahaIdPath();
+
+ if (utils::ReadFileToString(omaha_id_path, out_id) &&
+ out_id->size() == kGuidStringLength) {
+ return true;
+ }
+
+ // Creates a new ID.
+ int rand_fd = open("/dev/urandom", O_RDONLY, 0);
+ TEST_AND_RETURN_FALSE_ERRNO(rand_fd >= 0);
+ ScopedFdCloser rand_fd_closer(&rand_fd);
+ unsigned char buf[kGuidDataByteLength];
+ size_t bytes_read = 0;
+ while (bytes_read < sizeof(buf)) {
+ ssize_t rc = read(rand_fd, buf + bytes_read, sizeof(buf) - bytes_read);
+ TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
+ bytes_read += rc;
+ }
+ string guid = GuidFromData(buf);
+ TEST_AND_RETURN_FALSE(
+ utils::WriteFile(omaha_id_path.c_str(), guid.data(), guid.size()));
+ *out_id = guid;
+ return true;
+}
+
+string OmahaRequestDeviceParams::GetLsbValue(
+ const string& key, const string& default_value) const {
+ string files[] = {string(utils::kStatefulPartition) + "/etc/lsb-release",
+ "/etc/lsb-release"};
+ for (unsigned int i = 0; i < arraysize(files); ++i) {
+ // TODO(adlr): make sure files checked are owned as root (and all
+ // their parents are recursively, too).
+ string file_data;
+ if (!utils::ReadFileToString(root_ + files[i], &file_data))
+ continue;
+
+ map<string, string> data = simple_key_value_store::ParseString(file_data);
+ if (utils::MapContainsKey(data, key))
+ return data[key];
+ }
+ // not found
+ return default_value;
+}
+
+string OmahaRequestDeviceParams::GetMachineType() const {
+ struct utsname buf;
+ string ret;
+ if (uname(&buf) == 0)
+ ret = buf.machine;
+ return ret;
+}
+
+} // namespace chromeos_update_engine
diff --git a/omaha_request_params.h b/omaha_request_params.h
new file mode 100644
index 0000000..136c60a
--- /dev/null
+++ b/omaha_request_params.h
@@ -0,0 +1,97 @@
+// Copyright (c) 2010 The Chromium OS 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 CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H__
+#define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H__
+
+#include <string>
+
+#include "base/basictypes.h"
+
+// This gathers local system information and prepares info used by the
+// Omaha request action.
+
+namespace chromeos_update_engine {
+
+// This struct encapsulates the data Omaha gets for the request.
+// These strings in this struct should not be XML escaped.
+struct OmahaRequestParams {
+ OmahaRequestParams()
+ : os_platform(kOsPlatform), os_version(kOsVersion), app_id(kAppId) {}
+ OmahaRequestParams(const std::string& in_machine_id,
+ const std::string& in_user_id,
+ const std::string& in_os_platform,
+ const std::string& in_os_version,
+ const std::string& in_os_sp,
+ const std::string& in_os_board,
+ const std::string& in_app_id,
+ const std::string& in_app_version,
+ const std::string& in_app_lang,
+ const std::string& in_app_track,
+ const std::string& in_update_url)
+ : machine_id(in_machine_id),
+ user_id(in_user_id),
+ os_platform(in_os_platform),
+ os_version(in_os_version),
+ os_sp(in_os_sp),
+ os_board(in_os_board),
+ app_id(in_app_id),
+ app_version(in_app_version),
+ app_lang(in_app_lang),
+ app_track(in_app_track),
+ update_url(in_update_url) {}
+
+ std::string machine_id;
+ std::string user_id;
+ std::string os_platform;
+ std::string os_version;
+ std::string os_sp;
+ std::string os_board;
+ std::string app_id;
+ std::string app_version;
+ std::string app_lang;
+ std::string app_track;
+
+ std::string update_url;
+
+ // Suggested defaults
+ static const char* const kAppId;
+ static const char* const kOsPlatform;
+ static const char* const kOsVersion;
+ static const char* const kUpdateUrl;
+};
+
+class OmahaRequestDeviceParams : public OmahaRequestParams {
+ public:
+ explicit OmahaRequestDeviceParams() {}
+
+ // Initializes all the data in the object. Returns true on success,
+ // false otherwise.
+ bool Init();
+
+ // For unit-tests.
+ void set_root(const std::string& root) { root_ = root; }
+
+ private:
+ // Gets a machine-local ID (for now, first MAC address we find).
+ bool GetMachineId(std::string* out_id) const;
+
+ // Fetches the value for a given key from
+ // /mnt/stateful_partition/etc/lsb-release if possible. Failing that,
+ // it looks for the key in /etc/lsb-release.
+ std::string GetLsbValue(const std::string& key,
+ const std::string& default_value) const;
+
+ // Gets the machine type (e.g. "i686").
+ std::string GetMachineType() const;
+
+ // When reading files, prepend root_ to the paths. Useful for testing.
+ std::string root_;
+
+ DISALLOW_COPY_AND_ASSIGN(OmahaRequestDeviceParams);
+};
+
+} // namespace chromeos_update_engine
+
+#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_REQUEST_PARAMS_H__
diff --git a/omaha_request_prep_action_unittest.cc b/omaha_request_params_unittest.cc
similarity index 70%
rename from omaha_request_prep_action_unittest.cc
rename to omaha_request_params_unittest.cc
index 0813964..54d8dbd 100644
--- a/omaha_request_prep_action_unittest.cc
+++ b/omaha_request_params_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -6,7 +6,7 @@
#include <string>
#include <gtest/gtest.h>
#include "update_engine/install_plan.h"
-#include "update_engine/omaha_request_prep_action.h"
+#include "update_engine/omaha_request_params.h"
#include "update_engine/test_utils.h"
#include "update_engine/utils.h"
@@ -14,53 +14,24 @@
namespace chromeos_update_engine {
-class OmahaRequestPrepActionTest : public ::testing::Test {
+class OmahaRequestDeviceParamsTest : public ::testing::Test {
public:
- // Return true iff the OmahaResponseHandlerAction succeeded.
- // if out is non-NULL, it's set w/ the response from the action.
- bool DoTest(bool force_full_update, OmahaRequestParams* out);
+ // Return true iff the OmahaRequestDeviceParams::Init succeeded. If
+ // out is non-NULL, it's set w/ the generated data.
+ bool DoTest(OmahaRequestParams* out);
static const string kTestDir;
};
-const string OmahaRequestPrepActionTest::kTestDir = "request_prep_action-test";
+const string OmahaRequestDeviceParamsTest::kTestDir =
+ "omaha_request_device_params-test";
-class OmahaRequestPrepActionProcessorDelegate
- : public ActionProcessorDelegate {
- public:
- OmahaRequestPrepActionProcessorDelegate()
- : success_(false),
- success_set_(false) {}
- void ActionCompleted(ActionProcessor* processor,
- AbstractAction* action,
- bool success) {
- if (action->Type() == OmahaRequestPrepAction::StaticType()) {
- success_ = success;
- success_set_ = true;
- }
- }
- bool success_;
- bool success_set_;
-};
-
-bool OmahaRequestPrepActionTest::DoTest(bool force_full_update,
- OmahaRequestParams* out) {
- ActionProcessor processor;
- OmahaRequestPrepActionProcessorDelegate delegate;
- processor.set_delegate(&delegate);
-
- OmahaRequestPrepAction request_prep_action(force_full_update);
- request_prep_action.set_root(string("./") + kTestDir);
- ObjectCollectorAction<OmahaRequestParams> collector_action;
- BondActions(&request_prep_action, &collector_action);
- processor.EnqueueAction(&request_prep_action);
- processor.EnqueueAction(&collector_action);
- processor.StartProcessing();
- EXPECT_TRUE(!processor.IsRunning())
- << "Update test to handle non-asynch actions";
+bool OmahaRequestDeviceParamsTest::DoTest(OmahaRequestParams* out) {
+ OmahaRequestDeviceParams params;
+ params.set_root(string("./") + kTestDir);
+ bool success = params.Init();
if (out)
- *out = collector_action.object();
- EXPECT_TRUE(delegate.success_set_);
- return delegate.success_;
+ *out = params;
+ return success;
}
namespace {
@@ -84,6 +55,7 @@
}
return true;
}
+
string GetMachineType() {
FILE* fp = popen("uname -m", "r");
if (!fp)
@@ -104,7 +76,7 @@
}
} // namespace {}
-TEST_F(OmahaRequestPrepActionTest, SimpleTest) {
+TEST_F(OmahaRequestDeviceParamsTest, SimpleTest) {
ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc"));
ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir +
utils::kStatefulPartition + "/etc"));
@@ -116,7 +88,7 @@
"CHROMEOS_RELEASE_VERSION=0.2.2.3\n"
"CHROMEOS_RELEASE_TRACK=footrack"));
OmahaRequestParams out;
- EXPECT_TRUE(DoTest(false, &out));
+ EXPECT_TRUE(DoTest(&out));
EXPECT_TRUE(IsValidGuid(out.machine_id)) << "id: " << out.machine_id;
// for now we're just using the machine id here
EXPECT_TRUE(IsValidGuid(out.user_id)) << "id: " << out.user_id;
@@ -131,7 +103,7 @@
EXPECT_EQ(0, System(string("rm -rf ") + kTestDir));
}
-TEST_F(OmahaRequestPrepActionTest, MissingTrackTest) {
+TEST_F(OmahaRequestDeviceParamsTest, MissingTrackTest) {
ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc"));
ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir +
utils::kStatefulPartition + "/etc"));
@@ -142,7 +114,7 @@
"CHROMEOS_RELEASE_VERSION=0.2.2.3\n"
"CHROMEOS_RELEASE_TRXCK=footrack"));
OmahaRequestParams out;
- EXPECT_TRUE(DoTest(false, &out));
+ EXPECT_TRUE(DoTest(&out));
EXPECT_TRUE(IsValidGuid(out.machine_id));
// for now we're just using the machine id here
EXPECT_TRUE(IsValidGuid(out.user_id));
@@ -156,7 +128,7 @@
EXPECT_EQ(0, System(string("rm -rf ") + kTestDir));
}
-TEST_F(OmahaRequestPrepActionTest, ConfusingReleaseTest) {
+TEST_F(OmahaRequestDeviceParamsTest, ConfusingReleaseTest) {
ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc"));
ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir +
utils::kStatefulPartition + "/etc"));
@@ -167,7 +139,7 @@
"CHROMEOS_RELEASE_VERSION=0.2.2.3\n"
"CHROMEOS_RELEASE_TRXCK=footrack"));
OmahaRequestParams out;
- EXPECT_TRUE(DoTest(false, &out));
+ EXPECT_TRUE(DoTest(&out));
EXPECT_TRUE(IsValidGuid(out.machine_id)) << out.machine_id;
// for now we're just using the machine id here
EXPECT_TRUE(IsValidGuid(out.user_id));
@@ -181,7 +153,7 @@
EXPECT_EQ(0, System(string("rm -rf ") + kTestDir));
}
-TEST_F(OmahaRequestPrepActionTest, MachineIdPersistsTest) {
+TEST_F(OmahaRequestDeviceParamsTest, MachineIdPersistsTest) {
ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc"));
ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir +
utils::kStatefulPartition + "/etc"));
@@ -191,7 +163,7 @@
"CHROMEOS_RELEASE_VERSION=0.2.2.3\n"
"CHROMEOS_RELEASE_TRXCK=footrack"));
OmahaRequestParams out1;
- EXPECT_TRUE(DoTest(false, &out1));
+ EXPECT_TRUE(DoTest(&out1));
string machine_id;
EXPECT_TRUE(utils::ReadFileToString(
kTestDir +
@@ -199,7 +171,7 @@
&machine_id));
EXPECT_EQ(machine_id, out1.machine_id);
OmahaRequestParams out2;
- EXPECT_TRUE(DoTest(false, &out2));
+ EXPECT_TRUE(DoTest(&out2));
EXPECT_EQ(machine_id, out2.machine_id);
EXPECT_EQ(0, System(string("rm -rf ") + kTestDir));
}
diff --git a/omaha_request_prep_action.cc b/omaha_request_prep_action.cc
deleted file mode 100644
index 09ae3bd..0000000
--- a/omaha_request_prep_action.cc
+++ /dev/null
@@ -1,127 +0,0 @@
-// Copyright (c) 2009 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 "update_engine/omaha_request_prep_action.h"
-#include <sys/utsname.h>
-#include <errno.h>
-#include <map>
-#include <string>
-#include "base/string_util.h"
-#include "update_engine/simple_key_value_store.h"
-#include "update_engine/utils.h"
-
-using std::map;
-using std::string;
-
-// This gathers local system information and prepares info used by the
-// update check action.
-
-namespace {
-const string OmahaIdPath() {
- return string(chromeos_update_engine::utils::kStatefulPartition) +
- "/etc/omaha_id";
-}
-} // namespace {}
-
-namespace chromeos_update_engine {
-
-void OmahaRequestPrepAction::PerformAction() {
- // TODO(adlr): honor force_full_update_
- ScopedActionCompleter completer(processor_, this);
- string machine_id;
- TEST_AND_RETURN(GetMachineId(&machine_id));
- const string version(GetLsbValue("CHROMEOS_RELEASE_VERSION", ""));
- const string sp(version + "_" + GetMachineType());
- const string track(GetLsbValue("CHROMEOS_RELEASE_TRACK", ""));
- const string update_url(GetLsbValue("CHROMEOS_AUSERVER",
- OmahaRequestParams::kUpdateUrl));
- const string board(GetLsbValue("CHROMEOS_RELEASE_BOARD", ""));
-
- OmahaRequestParams out(machine_id, // machine_id
- machine_id, // user_id (use machine_id)
- OmahaRequestParams::kOsPlatform,
- OmahaRequestParams::kOsVersion,
- sp, // e.g. 0.2.3.3_i686
- board, // e.g. x86-generic
- OmahaRequestParams::kAppId,
- version, // app version (from lsb-release)
- "en-US", // lang
- track, // track
- update_url);
-
- CHECK(HasOutputPipe());
- SetOutputObject(out);
- completer.set_success(true);
-}
-
-namespace {
-const size_t kGuidDataByteLength = 128 / 8;
-const string::size_type kGuidStringLength = 38;
-// Formats 16 bytes (128 bits) of data as a GUID:
-// "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" where X is a hex digit
-string GuidFromData(const unsigned char data[kGuidDataByteLength]) {
- return StringPrintf(
- "{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
- data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
- data[8], data[9], data[10], data[11], data[12], data[13], data[14],
- data[15]);
-}
-}
-
-// Returns true on success.
-bool OmahaRequestPrepAction::GetMachineId(std::string* out_id) const {
- // See if we have an existing Machine ID
- const string omaha_id_path = root_ + OmahaIdPath();
-
- if (utils::ReadFileToString(omaha_id_path, out_id) &&
- out_id->size() == kGuidStringLength) {
- return true;
- }
-
- // Create a new ID
- int rand_fd = open("/dev/urandom", O_RDONLY, 0);
- TEST_AND_RETURN_FALSE_ERRNO(rand_fd >= 0);
- ScopedFdCloser rand_fd_closer(&rand_fd);
- unsigned char buf[kGuidDataByteLength];
- size_t bytes_read = 0;
- while (bytes_read < sizeof(buf)) {
- ssize_t rc = read(rand_fd, buf + bytes_read, sizeof(buf) - bytes_read);
- TEST_AND_RETURN_FALSE_ERRNO(rc > 0);
- bytes_read += rc;
- }
- string guid = GuidFromData(buf);
- TEST_AND_RETURN_FALSE(
- utils::WriteFile(omaha_id_path.c_str(), guid.data(), guid.size()));
- *out_id = guid;
- return true;
-}
-
-string OmahaRequestPrepAction::GetLsbValue(
- const string& key, const string& default_value) const {
- string files[] = {string(utils::kStatefulPartition) + "/etc/lsb-release",
- "/etc/lsb-release"};
- for (unsigned int i = 0; i < arraysize(files); i++) {
- // TODO(adlr): make sure files checked are owned as root (and all
- // their parents are recursively, too).
- string file_data;
- if (!utils::ReadFileToString(root_ + files[i], &file_data))
- continue;
-
- map<string, string> data = simple_key_value_store::ParseString(file_data);
- if (utils::MapContainsKey(data, key))
- return data[key];
- }
- // not found
- return default_value;
-}
-
-string OmahaRequestPrepAction::GetMachineType() const {
- struct utsname buf;
- string ret;
- if (uname(&buf) == 0)
- ret = buf.machine;
- return ret;
-}
-
-} // namespace chromeos_update_engine
diff --git a/omaha_request_prep_action.h b/omaha_request_prep_action.h
deleted file mode 100644
index 794cfb7..0000000
--- a/omaha_request_prep_action.h
+++ /dev/null
@@ -1,75 +0,0 @@
-// Copyright (c) 2009 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 CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_RESQUEST_PREP_ACTION_H__
-#define CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_RESQUEST_PREP_ACTION_H__
-
-#include <string>
-#include "update_engine/action.h"
-#include "update_engine/omaha_request_action.h"
-
-// This gathers local system information and prepares info used by the
-// update check action.
-
-namespace chromeos_update_engine {
-
-class OmahaRequestPrepAction;
-class NoneType;
-
-template<>
-class ActionTraits<OmahaRequestPrepAction> {
- public:
- typedef NoneType InputObjectType;
- typedef OmahaRequestParams OutputObjectType;
-};
-
-class OmahaRequestPrepAction : public Action<OmahaRequestPrepAction> {
- public:
- explicit OmahaRequestPrepAction(bool force_full_update)
- : force_full_update_(force_full_update) {}
- typedef ActionTraits<OmahaRequestPrepAction>::InputObjectType
- InputObjectType;
- typedef ActionTraits<OmahaRequestPrepAction>::OutputObjectType
- OutputObjectType;
- void PerformAction();
-
- // This is a synchronous action, and thus TerminateProcessing() should
- // never be called
- void TerminateProcessing() { CHECK(false); }
-
- // Debugging/logging
- static std::string StaticType() { return "OmahaRequestPrepAction"; }
- std::string Type() const { return StaticType(); }
-
- // For unit-tests.
- void set_root(const std::string& root) {
- root_ = root;
- }
-
- private:
- // Gets a machine-local ID (for now, first MAC address we find)
- bool GetMachineId(std::string* out_id) const;
-
- // Fetches the value for a given key from
- // /mnt/stateful_partition/etc/lsb-release if possible. Failing that,
- // it looks for the key in /etc/lsb-release .
- std::string GetLsbValue(const std::string& key,
- const std::string& default_value) const;
-
- // Gets the machine type (e.g. "i686")
- std::string GetMachineType() const;
-
- // Set to true if this should set up the Update Check Action to do
- // a full update
- bool force_full_update_;
-
- // When reading files, prepend root_ to the paths. Useful for testing.
- std::string root_;
-
- DISALLOW_COPY_AND_ASSIGN(OmahaRequestPrepAction);
-};
-
-} // namespace chromeos_update_engine
-
-#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_OMAHA_RESQUEST_PREP_ACTION_H__
diff --git a/update_attempter.cc b/update_attempter.cc
index f034b0b..1c32ff4 100644
--- a/update_attempter.cc
+++ b/update_attempter.cc
@@ -19,7 +19,7 @@
#include "update_engine/filesystem_copier_action.h"
#include "update_engine/libcurl_http_fetcher.h"
#include "update_engine/omaha_request_action.h"
-#include "update_engine/omaha_request_prep_action.h"
+#include "update_engine/omaha_request_params.h"
#include "update_engine/omaha_response_handler_action.h"
#include "update_engine/postinstall_runner_action.h"
#include "update_engine/set_bootable_flag_action.h"
@@ -85,7 +85,7 @@
}
}
-void UpdateAttempter::Update(bool force_full_update) {
+void UpdateAttempter::Update() {
if (status_ == UPDATE_STATUS_UPDATED_NEED_REBOOT) {
LOG(INFO) << "Not updating b/c we already updated and we're waiting for "
<< "reboot";
@@ -95,15 +95,18 @@
// Update in progress. Do nothing
return;
}
- full_update_ = force_full_update;
+ if (!omaha_request_params_.Init()) {
+ LOG(ERROR) << "Unable to initialize Omaha request device params.";
+ return;
+ }
CHECK(!processor_.IsRunning());
processor_.set_delegate(this);
// Actions:
- shared_ptr<OmahaRequestPrepAction> update_check_prep_action(
- new OmahaRequestPrepAction(force_full_update));
shared_ptr<OmahaRequestAction> update_check_action(
- new OmahaRequestAction(NULL, new LibcurlHttpFetcher));
+ new OmahaRequestAction(omaha_request_params_,
+ NULL,
+ new LibcurlHttpFetcher));
shared_ptr<OmahaResponseHandlerAction> response_handler_action(
new OmahaResponseHandlerAction);
shared_ptr<FilesystemCopierAction> filesystem_copier_action(
@@ -118,10 +121,9 @@
new SetBootableFlagAction);
shared_ptr<PostinstallRunnerAction> postinstall_runner_action_postcommit(
new PostinstallRunnerAction(false));
- shared_ptr<OmahaRequestPrepAction> install_success_prep_action(
- new OmahaRequestPrepAction(false));
shared_ptr<OmahaRequestAction> install_success_action(
- new OmahaRequestAction(new OmahaEvent(OmahaEvent::kTypeInstallComplete,
+ new OmahaRequestAction(omaha_request_params_,
+ new OmahaEvent(OmahaEvent::kTypeInstallComplete,
OmahaEvent::kResultSuccess,
0),
new LibcurlHttpFetcher));
@@ -129,7 +131,6 @@
download_action->set_delegate(this);
response_handler_action_ = response_handler_action;
- actions_.push_back(shared_ptr<AbstractAction>(update_check_prep_action));
actions_.push_back(shared_ptr<AbstractAction>(update_check_action));
actions_.push_back(shared_ptr<AbstractAction>(response_handler_action));
actions_.push_back(shared_ptr<AbstractAction>(filesystem_copier_action));
@@ -141,7 +142,6 @@
actions_.push_back(shared_ptr<AbstractAction>(set_bootable_flag_action));
actions_.push_back(shared_ptr<AbstractAction>(
postinstall_runner_action_postcommit));
- actions_.push_back(shared_ptr<AbstractAction>(install_success_prep_action));
actions_.push_back(shared_ptr<AbstractAction>(install_success_action));
// Enqueue the actions
@@ -152,8 +152,6 @@
// Bond them together. We have to use the leaf-types when calling
// BondActions().
- BondActions(update_check_prep_action.get(),
- update_check_action.get());
BondActions(update_check_action.get(),
response_handler_action.get());
BondActions(response_handler_action.get(),
@@ -168,8 +166,6 @@
set_bootable_flag_action.get());
BondActions(set_bootable_flag_action.get(),
postinstall_runner_action_postcommit.get());
- BondActions(install_success_prep_action.get(),
- install_success_action.get());
SetStatusAndNotify(UPDATE_STATUS_CHECKING_FOR_UPDATE);
processor_.StartProcessing();
@@ -181,7 +177,7 @@
<< UpdateStatusToString(status_) << ", so not checking.";
return;
}
- Update(false);
+ Update();
}
// Delegate methods:
diff --git a/update_attempter.h b/update_attempter.h
index 9d69403..753b409 100644
--- a/update_attempter.h
+++ b/update_attempter.h
@@ -12,6 +12,7 @@
#include <glib.h>
#include "update_engine/action_processor.h"
#include "update_engine/download_action.h"
+#include "update_engine/omaha_request_params.h"
#include "update_engine/omaha_response_handler_action.h"
struct UpdateEngineService;
@@ -35,8 +36,7 @@
class UpdateAttempter : public ActionProcessorDelegate,
public DownloadActionDelegate {
public:
- UpdateAttempter() : full_update_(false),
- dbus_service_(NULL),
+ UpdateAttempter() : dbus_service_(NULL),
status_(UPDATE_STATUS_IDLE),
download_progress_(0.0),
last_checked_time_(0),
@@ -47,22 +47,22 @@
if (utils::FileExists(kUpdateCompletedMarker))
status_ = UPDATE_STATUS_UPDATED_NEED_REBOOT;
}
- void Update(bool force_full_update);
-
+ void Update();
+
// ActionProcessorDelegate methods:
void ProcessingDone(const ActionProcessor* processor, bool success);
void ProcessingStopped(const ActionProcessor* processor);
void ActionCompleted(ActionProcessor* processor,
AbstractAction* action,
bool success);
-
+
// Stop updating. An attempt will be made to record status to the disk
// so that updates can be resumed later.
void Terminate();
-
+
// Try to resume from a previously Terminate()d update.
void ResumeUpdating();
-
+
// Returns the current status in the out params. Returns true on success.
bool GetStatus(int64_t* last_checked_time,
double* progress,
@@ -83,13 +83,12 @@
// Sets the status to the given status and notifies a status update
// over dbus.
void SetStatusAndNotify(UpdateStatus status);
-
+
struct timespec last_notify_time_;
- bool full_update_;
std::vector<std::tr1::shared_ptr<AbstractAction> > actions_;
ActionProcessor processor_;
-
+
// If non-null, this UpdateAttempter will send status updates over this
// dbus service.
UpdateEngineService* dbus_service_;
@@ -104,6 +103,9 @@
std::string new_version_;
int64_t new_size_;
+ // Device paramaters common to all Omaha requests.
+ OmahaRequestDeviceParams omaha_request_params_;
+
DISALLOW_COPY_AND_ASSIGN(UpdateAttempter);
};