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