Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 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" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 9 | #include "update_engine/omaha_request_params.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 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 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 17 | class OmahaRequestDeviceParamsTest : public ::testing::Test { |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame^] | 18 | protected: |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 19 | // Return true iff the OmahaRequestDeviceParams::Init succeeded. If |
| 20 | // out is non-NULL, it's set w/ the generated data. |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame^] | 21 | bool DoTest(OmahaRequestParams* out, const string& app_version, |
| 22 | const string& omaha_url); |
| 23 | |
| 24 | virtual void SetUp() { |
| 25 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); |
| 26 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + |
| 27 | utils::kStatefulPartition + "/etc")); |
| 28 | } |
| 29 | |
| 30 | virtual void TearDown() { |
| 31 | EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); |
| 32 | } |
| 33 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 34 | static const string kTestDir; |
| 35 | }; |
| 36 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 37 | const string OmahaRequestDeviceParamsTest::kTestDir = |
| 38 | "omaha_request_device_params-test"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 39 | |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame^] | 40 | bool OmahaRequestDeviceParamsTest::DoTest(OmahaRequestParams* out, |
| 41 | const string& app_version, |
| 42 | const string& omaha_url) { |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 43 | OmahaRequestDeviceParams params; |
| 44 | params.set_root(string("./") + kTestDir); |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame^] | 45 | bool success = params.Init(app_version, omaha_url); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 46 | if (out) |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 47 | *out = params; |
| 48 | return success; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | namespace { |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 52 | bool IsHexDigit(char c) { |
| 53 | return ((c >= '0') && (c <= '9')) || |
| 54 | ((c >= 'a') && (c <= 'f')) || |
| 55 | ((c >= 'A') && (c <= 'F')); |
| 56 | } |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 57 | |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 58 | // Returns true iff str is formatted as a GUID. Example GUID: |
| 59 | // "{2251FFAD-DBAB-4E53-8B3A-18F98BB4EB80}" |
| 60 | bool IsValidGuid(const string& str) { |
| 61 | TEST_AND_RETURN_FALSE(str.size() == 38); |
| 62 | TEST_AND_RETURN_FALSE((*str.begin() == '{') && (*str.rbegin() == '}')); |
| 63 | for (string::size_type i = 1; i < (str.size() - 1); ++i) { |
| 64 | if ((i == 9) || (i == 14) || (i == 19) || (i == 24)) { |
| 65 | TEST_AND_RETURN_FALSE(str[i] == '-'); |
| 66 | } else { |
| 67 | TEST_AND_RETURN_FALSE(IsHexDigit(str[i])); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | return true; |
| 71 | } |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 72 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 73 | string GetMachineType() { |
| 74 | FILE* fp = popen("uname -m", "r"); |
| 75 | if (!fp) |
| 76 | return ""; |
| 77 | string ret; |
| 78 | for (;;) { |
| 79 | char buffer[10]; |
| 80 | size_t r = fread(buffer, 1, sizeof(buffer), fp); |
| 81 | if (r == 0) |
| 82 | break; |
| 83 | ret.insert(ret.begin(), buffer, buffer + r); |
| 84 | } |
| 85 | // strip trailing '\n' if it exists |
| 86 | if ((*ret.rbegin()) == '\n') |
| 87 | ret.resize(ret.size() - 1); |
| 88 | fclose(fp); |
| 89 | return ret; |
| 90 | } |
| 91 | } // namespace {} |
| 92 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 93 | TEST_F(OmahaRequestDeviceParamsTest, SimpleTest) { |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame^] | 94 | ASSERT_TRUE(WriteFileString( |
| 95 | kTestDir + "/etc/lsb-release", |
| 96 | "CHROMEOS_RELEASE_BOARD=arm-generic\n" |
| 97 | "CHROMEOS_RELEASE_FOO=bar\n" |
| 98 | "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" |
| 99 | "CHROMEOS_RELEASE_TRACK=footrack\n" |
| 100 | "CHROMEOS_AUSERVER=http://www.google.com")); |
| 101 | OmahaRequestParams out; |
| 102 | EXPECT_TRUE(DoTest(&out, "", "")); |
| 103 | EXPECT_TRUE(IsValidGuid(out.machine_id)) << "id: " << out.machine_id; |
| 104 | // for now we're just using the machine id here |
| 105 | EXPECT_TRUE(IsValidGuid(out.user_id)) << "id: " << out.user_id; |
| 106 | EXPECT_EQ("Chrome OS", out.os_platform); |
| 107 | EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); |
| 108 | EXPECT_EQ("arm-generic", out.os_board); |
| 109 | EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); |
| 110 | EXPECT_EQ("0.2.2.3", out.app_version); |
| 111 | EXPECT_EQ("en-US", out.app_lang); |
| 112 | EXPECT_TRUE(out.delta_okay); |
| 113 | EXPECT_EQ("footrack", out.app_track); |
| 114 | EXPECT_EQ("http://www.google.com", out.update_url); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 117 | TEST_F(OmahaRequestDeviceParamsTest, MissingTrackTest) { |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame^] | 118 | ASSERT_TRUE(WriteFileString( |
| 119 | kTestDir + "/etc/lsb-release", |
| 120 | "CHROMEOS_RELEASE_FOO=bar\n" |
| 121 | "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" |
| 122 | "CHROMEOS_RELEASE_TRXCK=footrack")); |
| 123 | OmahaRequestParams out; |
| 124 | EXPECT_TRUE(DoTest(&out, "", "")); |
| 125 | EXPECT_TRUE(IsValidGuid(out.machine_id)); |
| 126 | // for now we're just using the machine id here |
| 127 | EXPECT_TRUE(IsValidGuid(out.user_id)); |
| 128 | EXPECT_EQ("Chrome OS", out.os_platform); |
| 129 | EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); |
| 130 | EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); |
| 131 | EXPECT_EQ("0.2.2.3", out.app_version); |
| 132 | EXPECT_EQ("en-US", out.app_lang); |
| 133 | EXPECT_EQ("", out.app_track); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 136 | TEST_F(OmahaRequestDeviceParamsTest, ConfusingReleaseTest) { |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame^] | 137 | ASSERT_TRUE(WriteFileString( |
| 138 | kTestDir + "/etc/lsb-release", |
| 139 | "CHROMEOS_RELEASE_FOO=CHROMEOS_RELEASE_VERSION=1.2.3.4\n" |
| 140 | "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" |
| 141 | "CHROMEOS_RELEASE_TRXCK=footrack")); |
| 142 | OmahaRequestParams out; |
| 143 | EXPECT_TRUE(DoTest(&out, "", "")); |
| 144 | EXPECT_TRUE(IsValidGuid(out.machine_id)) << out.machine_id; |
| 145 | // for now we're just using the machine id here |
| 146 | EXPECT_TRUE(IsValidGuid(out.user_id)); |
| 147 | EXPECT_EQ("Chrome OS", out.os_platform); |
| 148 | EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); |
| 149 | EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); |
| 150 | EXPECT_EQ("0.2.2.3", out.app_version); |
| 151 | EXPECT_EQ("en-US", out.app_lang); |
| 152 | EXPECT_EQ("", out.app_track); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 155 | TEST_F(OmahaRequestDeviceParamsTest, MachineIdPersistsTest) { |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 156 | ASSERT_TRUE(WriteFileString( |
| 157 | kTestDir + "/etc/lsb-release", |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 158 | "CHROMEOS_RELEASE_FOO=CHROMEOS_RELEASE_VERSION=1.2.3.4\n" |
| 159 | "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" |
| 160 | "CHROMEOS_RELEASE_TRXCK=footrack")); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 161 | OmahaRequestParams out1; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame^] | 162 | EXPECT_TRUE(DoTest(&out1, "", "")); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 163 | string machine_id; |
| 164 | EXPECT_TRUE(utils::ReadFileToString( |
| 165 | kTestDir + |
| 166 | utils::kStatefulPartition + "/etc/omaha_id", |
| 167 | &machine_id)); |
| 168 | EXPECT_EQ(machine_id, out1.machine_id); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 169 | OmahaRequestParams out2; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame^] | 170 | EXPECT_TRUE(DoTest(&out2, "", "")); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 171 | EXPECT_EQ(machine_id, out2.machine_id); |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame^] | 172 | } |
| 173 | |
| 174 | TEST_F(OmahaRequestDeviceParamsTest, MissingVersionTest) { |
| 175 | ASSERT_TRUE(WriteFileString( |
| 176 | kTestDir + "/etc/lsb-release", |
| 177 | "CHROMEOS_RELEASE_BOARD=arm-generic\n" |
| 178 | "CHROMEOS_RELEASE_FOO=bar\n" |
| 179 | "CHROMEOS_RELEASE_TRACK=footrack")); |
| 180 | OmahaRequestParams out; |
| 181 | EXPECT_TRUE(DoTest(&out, "", "")); |
| 182 | EXPECT_TRUE(IsValidGuid(out.machine_id)) << "id: " << out.machine_id; |
| 183 | // for now we're just using the machine id here |
| 184 | EXPECT_TRUE(IsValidGuid(out.user_id)) << "id: " << out.user_id; |
| 185 | EXPECT_EQ("Chrome OS", out.os_platform); |
| 186 | EXPECT_EQ(string("_") + GetMachineType(), out.os_sp); |
| 187 | EXPECT_EQ("arm-generic", out.os_board); |
| 188 | EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); |
| 189 | EXPECT_EQ("", out.app_version); |
| 190 | EXPECT_EQ("en-US", out.app_lang); |
| 191 | EXPECT_TRUE(out.delta_okay); |
| 192 | EXPECT_EQ("footrack", out.app_track); |
| 193 | } |
| 194 | |
| 195 | TEST_F(OmahaRequestDeviceParamsTest, ForceVersionTest) { |
| 196 | ASSERT_TRUE(WriteFileString( |
| 197 | kTestDir + "/etc/lsb-release", |
| 198 | "CHROMEOS_RELEASE_BOARD=arm-generic\n" |
| 199 | "CHROMEOS_RELEASE_FOO=bar\n" |
| 200 | "CHROMEOS_RELEASE_TRACK=footrack")); |
| 201 | OmahaRequestParams out; |
| 202 | EXPECT_TRUE(DoTest(&out, "ForcedVersion", "")); |
| 203 | EXPECT_TRUE(IsValidGuid(out.machine_id)) << "id: " << out.machine_id; |
| 204 | // for now we're just using the machine id here |
| 205 | EXPECT_TRUE(IsValidGuid(out.user_id)) << "id: " << out.user_id; |
| 206 | EXPECT_EQ("Chrome OS", out.os_platform); |
| 207 | EXPECT_EQ(string("ForcedVersion_") + GetMachineType(), out.os_sp); |
| 208 | EXPECT_EQ("arm-generic", out.os_board); |
| 209 | EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); |
| 210 | EXPECT_EQ("ForcedVersion", out.app_version); |
| 211 | EXPECT_EQ("en-US", out.app_lang); |
| 212 | EXPECT_TRUE(out.delta_okay); |
| 213 | EXPECT_EQ("footrack", out.app_track); |
| 214 | } |
| 215 | |
| 216 | TEST_F(OmahaRequestDeviceParamsTest, ForcedURLTest) { |
| 217 | ASSERT_TRUE(WriteFileString( |
| 218 | kTestDir + "/etc/lsb-release", |
| 219 | "CHROMEOS_RELEASE_BOARD=arm-generic\n" |
| 220 | "CHROMEOS_RELEASE_FOO=bar\n" |
| 221 | "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" |
| 222 | "CHROMEOS_RELEASE_TRACK=footrack")); |
| 223 | OmahaRequestParams out; |
| 224 | EXPECT_TRUE(DoTest(&out, "", "http://forced.google.com")); |
| 225 | EXPECT_TRUE(IsValidGuid(out.machine_id)) << "id: " << out.machine_id; |
| 226 | // for now we're just using the machine id here |
| 227 | EXPECT_TRUE(IsValidGuid(out.user_id)) << "id: " << out.user_id; |
| 228 | EXPECT_EQ("Chrome OS", out.os_platform); |
| 229 | EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); |
| 230 | EXPECT_EQ("arm-generic", out.os_board); |
| 231 | EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); |
| 232 | EXPECT_EQ("0.2.2.3", out.app_version); |
| 233 | EXPECT_EQ("en-US", out.app_lang); |
| 234 | EXPECT_TRUE(out.delta_okay); |
| 235 | EXPECT_EQ("footrack", out.app_track); |
| 236 | EXPECT_EQ("http://forced.google.com", out.update_url); |
| 237 | } |
| 238 | |
| 239 | TEST_F(OmahaRequestDeviceParamsTest, MissingURLTest) { |
| 240 | ASSERT_TRUE(WriteFileString( |
| 241 | kTestDir + "/etc/lsb-release", |
| 242 | "CHROMEOS_RELEASE_BOARD=arm-generic\n" |
| 243 | "CHROMEOS_RELEASE_FOO=bar\n" |
| 244 | "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" |
| 245 | "CHROMEOS_RELEASE_TRACK=footrack")); |
| 246 | OmahaRequestParams out; |
| 247 | EXPECT_TRUE(DoTest(&out, "", "")); |
| 248 | EXPECT_TRUE(IsValidGuid(out.machine_id)) << "id: " << out.machine_id; |
| 249 | // for now we're just using the machine id here |
| 250 | EXPECT_TRUE(IsValidGuid(out.user_id)) << "id: " << out.user_id; |
| 251 | EXPECT_EQ("Chrome OS", out.os_platform); |
| 252 | EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); |
| 253 | EXPECT_EQ("arm-generic", out.os_board); |
| 254 | EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); |
| 255 | EXPECT_EQ("0.2.2.3", out.app_version); |
| 256 | EXPECT_EQ("en-US", out.app_lang); |
| 257 | EXPECT_TRUE(out.delta_okay); |
| 258 | EXPECT_EQ("footrack", out.app_track); |
| 259 | EXPECT_EQ(OmahaRequestParams::kUpdateUrl, out.update_url); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 262 | TEST_F(OmahaRequestDeviceParamsTest, NoDeltasTest) { |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 263 | ASSERT_TRUE(WriteFileString( |
| 264 | kTestDir + "/etc/lsb-release", |
| 265 | "CHROMEOS_RELEASE_FOO=CHROMEOS_RELEASE_VERSION=1.2.3.4\n" |
| 266 | "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" |
| 267 | "CHROMEOS_RELEASE_TRXCK=footrack")); |
| 268 | ASSERT_TRUE(WriteFileString(kTestDir + "/.nodelta", "")); |
| 269 | OmahaRequestParams out; |
Darin Petkov | 5a7f565 | 2010-07-22 21:40:09 -0700 | [diff] [blame^] | 270 | EXPECT_TRUE(DoTest(&out, "", "")); |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 271 | EXPECT_FALSE(out.delta_okay); |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 272 | } |
| 273 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 274 | } // namespace chromeos_update_engine |