adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 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. |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <string> |
| 7 | #include <gtest/gtest.h> |
| 8 | #include "update_engine/install_plan.h" |
| 9 | #include "update_engine/omaha_request_prep_action.h" |
| 10 | #include "update_engine/test_utils.h" |
| 11 | #include "update_engine/utils.h" |
| 12 | |
| 13 | using std::string; |
| 14 | |
| 15 | namespace chromeos_update_engine { |
| 16 | |
| 17 | class OmahaRequestPrepActionTest : public ::testing::Test { |
| 18 | public: |
| 19 | // Return true iff the OmahaResponseHandlerAction succeeded. |
| 20 | // if out is non-NULL, it's set w/ the response from the action. |
| 21 | bool DoTest(bool force_full_update, UpdateCheckParams* out); |
| 22 | static const string kTestDir; |
| 23 | }; |
| 24 | |
| 25 | const string OmahaRequestPrepActionTest::kTestDir = "request_prep_action-test"; |
| 26 | |
| 27 | class OmahaRequestPrepActionProcessorDelegate |
| 28 | : public ActionProcessorDelegate { |
| 29 | public: |
| 30 | OmahaRequestPrepActionProcessorDelegate() |
| 31 | : success_(false), |
| 32 | success_set_(false) {} |
| 33 | void ActionCompleted(ActionProcessor* processor, |
| 34 | AbstractAction* action, |
| 35 | bool success) { |
| 36 | if (action->Type() == OmahaRequestPrepAction::StaticType()) { |
| 37 | success_ = success; |
| 38 | success_set_ = true; |
| 39 | } |
| 40 | } |
| 41 | bool success_; |
| 42 | bool success_set_; |
| 43 | }; |
| 44 | |
| 45 | bool OmahaRequestPrepActionTest::DoTest(bool force_full_update, |
| 46 | UpdateCheckParams* out) { |
| 47 | ActionProcessor processor; |
| 48 | OmahaRequestPrepActionProcessorDelegate delegate; |
| 49 | processor.set_delegate(&delegate); |
| 50 | |
| 51 | OmahaRequestPrepAction request_prep_action(force_full_update); |
| 52 | request_prep_action.set_root(string("./") + kTestDir); |
| 53 | ObjectCollectorAction<UpdateCheckParams> collector_action; |
| 54 | BondActions(&request_prep_action, &collector_action); |
| 55 | processor.EnqueueAction(&request_prep_action); |
| 56 | processor.EnqueueAction(&collector_action); |
| 57 | processor.StartProcessing(); |
| 58 | EXPECT_TRUE(!processor.IsRunning()) |
| 59 | << "Update test to handle non-asynch actions"; |
| 60 | if (out) |
| 61 | *out = collector_action.object(); |
| 62 | EXPECT_TRUE(delegate.success_set_); |
| 63 | return delegate.success_; |
| 64 | } |
| 65 | |
| 66 | namespace { |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame^] | 67 | bool IsHexDigit(char c) { |
| 68 | return ((c >= '0') && (c <= '9')) || |
| 69 | ((c >= 'a') && (c <= 'f')) || |
| 70 | ((c >= 'A') && (c <= 'F')); |
| 71 | } |
| 72 | |
| 73 | // Returns true iff str is formatted as a GUID. Example GUID: |
| 74 | // "{2251FFAD-DBAB-4E53-8B3A-18F98BB4EB80}" |
| 75 | bool IsValidGuid(const string& str) { |
| 76 | TEST_AND_RETURN_FALSE(str.size() == 38); |
| 77 | TEST_AND_RETURN_FALSE((*str.begin() == '{') && (*str.rbegin() == '}')); |
| 78 | for (string::size_type i = 1; i < (str.size() - 1); ++i) { |
| 79 | if ((i == 9) || (i == 14) || (i == 19) || (i == 24)) { |
| 80 | TEST_AND_RETURN_FALSE(str[i] == '-'); |
| 81 | } else { |
| 82 | TEST_AND_RETURN_FALSE(IsHexDigit(str[i])); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | string GetMachineType() { |
| 88 | FILE* fp = popen("uname -m", "r"); |
| 89 | if (!fp) |
| 90 | return ""; |
| 91 | string ret; |
| 92 | for (;;) { |
| 93 | char buffer[10]; |
| 94 | size_t r = fread(buffer, 1, sizeof(buffer), fp); |
| 95 | if (r == 0) |
| 96 | break; |
| 97 | ret.insert(ret.begin(), buffer, buffer + r); |
| 98 | } |
| 99 | // strip trailing '\n' if it exists |
| 100 | if ((*ret.rbegin()) == '\n') |
| 101 | ret.resize(ret.size() - 1); |
| 102 | fclose(fp); |
| 103 | return ret; |
| 104 | } |
| 105 | } // namespace {} |
| 106 | |
| 107 | TEST_F(OmahaRequestPrepActionTest, SimpleTest) { |
| 108 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame^] | 109 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + |
| 110 | utils::kStatefulPartition + "/etc")); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 111 | { |
| 112 | ASSERT_TRUE(WriteFileString( |
| 113 | kTestDir + "/etc/lsb-release", |
| 114 | "GOOGLE_FOO=bar\nGOOGLE_RELEASE=0.2.2.3\nGOOGLE_TRACK=footrack")); |
| 115 | UpdateCheckParams out; |
| 116 | EXPECT_TRUE(DoTest(false, &out)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame^] | 117 | EXPECT_TRUE(IsValidGuid(out.machine_id)) << "id: " << out.machine_id; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 118 | // for now we're just using the machine id here |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame^] | 119 | EXPECT_TRUE(IsValidGuid(out.user_id)) << "id: " << out.user_id; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 120 | EXPECT_EQ("Chrome OS", out.os_platform); |
| 121 | EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); |
| 122 | EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); |
| 123 | EXPECT_EQ("0.2.2.3", out.app_version); |
| 124 | EXPECT_EQ("en-US", out.app_lang); |
| 125 | EXPECT_EQ("footrack", out.app_track); |
| 126 | } |
| 127 | EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); |
| 128 | } |
| 129 | |
| 130 | TEST_F(OmahaRequestPrepActionTest, MissingTrackTest) { |
| 131 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame^] | 132 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + |
| 133 | utils::kStatefulPartition + "/etc")); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 134 | { |
| 135 | ASSERT_TRUE(WriteFileString( |
| 136 | kTestDir + "/etc/lsb-release", |
| 137 | "GOOGLE_FOO=bar\nGOOGLE_RELEASE=0.2.2.3\nGOOGLE_TRXCK=footrack")); |
| 138 | UpdateCheckParams out; |
| 139 | EXPECT_TRUE(DoTest(false, &out)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame^] | 140 | EXPECT_TRUE(IsValidGuid(out.machine_id)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 141 | // for now we're just using the machine id here |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame^] | 142 | EXPECT_TRUE(IsValidGuid(out.user_id)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 143 | EXPECT_EQ("Chrome OS", out.os_platform); |
| 144 | EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); |
| 145 | EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); |
| 146 | EXPECT_EQ("0.2.2.3", out.app_version); |
| 147 | EXPECT_EQ("en-US", out.app_lang); |
| 148 | EXPECT_EQ("", out.app_track); |
| 149 | } |
| 150 | EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); |
| 151 | } |
| 152 | |
| 153 | TEST_F(OmahaRequestPrepActionTest, ConfusingReleaseTest) { |
| 154 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame^] | 155 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + |
| 156 | utils::kStatefulPartition + "/etc")); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 157 | { |
| 158 | ASSERT_TRUE(WriteFileString( |
| 159 | kTestDir + "/etc/lsb-release", |
| 160 | "GOOGLE_FOO=GOOGLE_RELEASE=1.2.3.4\n" |
| 161 | "GOOGLE_RELEASE=0.2.2.3\nGOOGLE_TRXCK=footrack")); |
| 162 | UpdateCheckParams out; |
| 163 | EXPECT_TRUE(DoTest(false, &out)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame^] | 164 | EXPECT_TRUE(IsValidGuid(out.machine_id)) << out.machine_id; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 165 | // for now we're just using the machine id here |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame^] | 166 | EXPECT_TRUE(IsValidGuid(out.user_id)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 167 | EXPECT_EQ("Chrome OS", out.os_platform); |
| 168 | EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); |
| 169 | EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); |
| 170 | EXPECT_EQ("0.2.2.3", out.app_version); |
| 171 | EXPECT_EQ("en-US", out.app_lang); |
| 172 | EXPECT_EQ("", out.app_track); |
| 173 | } |
| 174 | EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); |
| 175 | } |
| 176 | |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame^] | 177 | TEST_F(OmahaRequestPrepActionTest, MachineIdPersistsTest) { |
| 178 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); |
| 179 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + |
| 180 | utils::kStatefulPartition + "/etc")); |
| 181 | ASSERT_TRUE(WriteFileString( |
| 182 | kTestDir + "/etc/lsb-release", |
| 183 | "GOOGLE_FOO=GOOGLE_RELEASE=1.2.3.4\n" |
| 184 | "GOOGLE_RELEASE=0.2.2.3\nGOOGLE_TRXCK=footrack")); |
| 185 | UpdateCheckParams out1; |
| 186 | EXPECT_TRUE(DoTest(false, &out1)); |
| 187 | string machine_id; |
| 188 | EXPECT_TRUE(utils::ReadFileToString( |
| 189 | kTestDir + |
| 190 | utils::kStatefulPartition + "/etc/omaha_id", |
| 191 | &machine_id)); |
| 192 | EXPECT_EQ(machine_id, out1.machine_id); |
| 193 | UpdateCheckParams out2; |
| 194 | EXPECT_TRUE(DoTest(false, &out2)); |
| 195 | EXPECT_EQ(machine_id, out2.machine_id); |
| 196 | EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); |
| 197 | } |
| 198 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 199 | } // namespace chromeos_update_engine |