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 { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 18 | public: |
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. |
| 21 | bool DoTest(OmahaRequestParams* out); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 22 | static const string kTestDir; |
| 23 | }; |
| 24 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 25 | const string OmahaRequestDeviceParamsTest::kTestDir = |
| 26 | "omaha_request_device_params-test"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 27 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 28 | bool OmahaRequestDeviceParamsTest::DoTest(OmahaRequestParams* out) { |
| 29 | OmahaRequestDeviceParams params; |
| 30 | params.set_root(string("./") + kTestDir); |
| 31 | bool success = params.Init(); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 32 | if (out) |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 33 | *out = params; |
| 34 | return success; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | namespace { |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 38 | bool IsHexDigit(char c) { |
| 39 | return ((c >= '0') && (c <= '9')) || |
| 40 | ((c >= 'a') && (c <= 'f')) || |
| 41 | ((c >= 'A') && (c <= 'F')); |
| 42 | } |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 43 | |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 44 | // Returns true iff str is formatted as a GUID. Example GUID: |
| 45 | // "{2251FFAD-DBAB-4E53-8B3A-18F98BB4EB80}" |
| 46 | bool IsValidGuid(const string& str) { |
| 47 | TEST_AND_RETURN_FALSE(str.size() == 38); |
| 48 | TEST_AND_RETURN_FALSE((*str.begin() == '{') && (*str.rbegin() == '}')); |
| 49 | for (string::size_type i = 1; i < (str.size() - 1); ++i) { |
| 50 | if ((i == 9) || (i == 14) || (i == 19) || (i == 24)) { |
| 51 | TEST_AND_RETURN_FALSE(str[i] == '-'); |
| 52 | } else { |
| 53 | TEST_AND_RETURN_FALSE(IsHexDigit(str[i])); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | return true; |
| 57 | } |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 58 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 59 | string GetMachineType() { |
| 60 | FILE* fp = popen("uname -m", "r"); |
| 61 | if (!fp) |
| 62 | return ""; |
| 63 | string ret; |
| 64 | for (;;) { |
| 65 | char buffer[10]; |
| 66 | size_t r = fread(buffer, 1, sizeof(buffer), fp); |
| 67 | if (r == 0) |
| 68 | break; |
| 69 | ret.insert(ret.begin(), buffer, buffer + r); |
| 70 | } |
| 71 | // strip trailing '\n' if it exists |
| 72 | if ((*ret.rbegin()) == '\n') |
| 73 | ret.resize(ret.size() - 1); |
| 74 | fclose(fp); |
| 75 | return ret; |
| 76 | } |
| 77 | } // namespace {} |
| 78 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 79 | TEST_F(OmahaRequestDeviceParamsTest, SimpleTest) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 80 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 81 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + |
| 82 | utils::kStatefulPartition + "/etc")); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 83 | { |
| 84 | ASSERT_TRUE(WriteFileString( |
| 85 | kTestDir + "/etc/lsb-release", |
Andrew de los Reyes | 37c2032 | 2010-06-30 13:27:19 -0700 | [diff] [blame] | 86 | "CHROMEOS_RELEASE_BOARD=arm-generic\n" |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 87 | "CHROMEOS_RELEASE_FOO=bar\n" |
| 88 | "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" |
| 89 | "CHROMEOS_RELEASE_TRACK=footrack")); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 90 | OmahaRequestParams out; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 91 | EXPECT_TRUE(DoTest(&out)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 92 | EXPECT_TRUE(IsValidGuid(out.machine_id)) << "id: " << out.machine_id; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 93 | // for now we're just using the machine id here |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 94 | EXPECT_TRUE(IsValidGuid(out.user_id)) << "id: " << out.user_id; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 95 | EXPECT_EQ("Chrome OS", out.os_platform); |
| 96 | EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); |
Andrew de los Reyes | 37c2032 | 2010-06-30 13:27:19 -0700 | [diff] [blame] | 97 | EXPECT_EQ("arm-generic", out.os_board); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 98 | EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); |
| 99 | EXPECT_EQ("0.2.2.3", out.app_version); |
| 100 | EXPECT_EQ("en-US", out.app_lang); |
| 101 | EXPECT_EQ("footrack", out.app_track); |
| 102 | } |
| 103 | EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); |
| 104 | } |
| 105 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 106 | TEST_F(OmahaRequestDeviceParamsTest, MissingTrackTest) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 107 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 108 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + |
| 109 | utils::kStatefulPartition + "/etc")); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 110 | { |
| 111 | ASSERT_TRUE(WriteFileString( |
| 112 | kTestDir + "/etc/lsb-release", |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 113 | "CHROMEOS_RELEASE_FOO=bar\n" |
| 114 | "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" |
| 115 | "CHROMEOS_RELEASE_TRXCK=footrack")); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 116 | OmahaRequestParams out; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 117 | EXPECT_TRUE(DoTest(&out)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 118 | EXPECT_TRUE(IsValidGuid(out.machine_id)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 119 | // for now we're just using the machine id here |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 120 | EXPECT_TRUE(IsValidGuid(out.user_id)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 121 | EXPECT_EQ("Chrome OS", out.os_platform); |
| 122 | EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); |
| 123 | EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); |
| 124 | EXPECT_EQ("0.2.2.3", out.app_version); |
| 125 | EXPECT_EQ("en-US", out.app_lang); |
| 126 | EXPECT_EQ("", out.app_track); |
| 127 | } |
| 128 | EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); |
| 129 | } |
| 130 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 131 | TEST_F(OmahaRequestDeviceParamsTest, ConfusingReleaseTest) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 132 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 133 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + |
| 134 | utils::kStatefulPartition + "/etc")); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 135 | { |
| 136 | ASSERT_TRUE(WriteFileString( |
| 137 | kTestDir + "/etc/lsb-release", |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 138 | "CHROMEOS_RELEASE_FOO=CHROMEOS_RELEASE_VERSION=1.2.3.4\n" |
| 139 | "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" |
| 140 | "CHROMEOS_RELEASE_TRXCK=footrack")); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 141 | OmahaRequestParams out; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 142 | EXPECT_TRUE(DoTest(&out)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 143 | EXPECT_TRUE(IsValidGuid(out.machine_id)) << out.machine_id; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 144 | // for now we're just using the machine id here |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 145 | EXPECT_TRUE(IsValidGuid(out.user_id)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 146 | EXPECT_EQ("Chrome OS", out.os_platform); |
| 147 | EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); |
| 148 | EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); |
| 149 | EXPECT_EQ("0.2.2.3", out.app_version); |
| 150 | EXPECT_EQ("en-US", out.app_lang); |
| 151 | EXPECT_EQ("", out.app_track); |
| 152 | } |
| 153 | EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); |
| 154 | } |
| 155 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 156 | TEST_F(OmahaRequestDeviceParamsTest, MachineIdPersistsTest) { |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 157 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); |
| 158 | ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + |
| 159 | utils::kStatefulPartition + "/etc")); |
| 160 | ASSERT_TRUE(WriteFileString( |
| 161 | kTestDir + "/etc/lsb-release", |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 162 | "CHROMEOS_RELEASE_FOO=CHROMEOS_RELEASE_VERSION=1.2.3.4\n" |
| 163 | "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" |
| 164 | "CHROMEOS_RELEASE_TRXCK=footrack")); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 165 | OmahaRequestParams out1; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 166 | EXPECT_TRUE(DoTest(&out1)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 167 | string machine_id; |
| 168 | EXPECT_TRUE(utils::ReadFileToString( |
| 169 | kTestDir + |
| 170 | utils::kStatefulPartition + "/etc/omaha_id", |
| 171 | &machine_id)); |
| 172 | EXPECT_EQ(machine_id, out1.machine_id); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 173 | OmahaRequestParams out2; |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame^] | 174 | EXPECT_TRUE(DoTest(&out2)); |
Andrew de los Reyes | 970bb28 | 2009-12-09 16:34:04 -0800 | [diff] [blame] | 175 | EXPECT_EQ(machine_id, out2.machine_id); |
| 176 | EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); |
| 177 | } |
| 178 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 179 | } // namespace chromeos_update_engine |