| OLD | NEW | 
|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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_prep_action.h" | 
| 10 #include "update_engine/test_utils.h" | 10 #include "update_engine/test_utils.h" | 
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 57   processor.StartProcessing(); | 57   processor.StartProcessing(); | 
| 58   EXPECT_TRUE(!processor.IsRunning()) | 58   EXPECT_TRUE(!processor.IsRunning()) | 
| 59       << "Update test to handle non-asynch actions"; | 59       << "Update test to handle non-asynch actions"; | 
| 60   if (out) | 60   if (out) | 
| 61     *out = collector_action.object(); | 61     *out = collector_action.object(); | 
| 62   EXPECT_TRUE(delegate.success_set_); | 62   EXPECT_TRUE(delegate.success_set_); | 
| 63   return delegate.success_; | 63   return delegate.success_; | 
| 64 } | 64 } | 
| 65 | 65 | 
| 66 namespace { | 66 namespace { | 
| 67 // Returns true iff str is formatted as a mac address | 67 bool IsHexDigit(char c) { | 
| 68 bool IsValidMac(const string& str) { | 68   return ((c >= '0') && (c <= '9')) || | 
| 69   if (str.size() != (3 * 6 - 1)) | 69       ((c >= 'a') && (c <= 'f')) || | 
| 70     return false; | 70       ((c >= 'A') && (c <= 'F')); | 
| 71   for (unsigned int i = 0; i < str.size(); i++) { | 71 } | 
| 72     char c = str[i]; | 72 | 
| 73     switch (i % 3) { | 73 // Returns true iff str is formatted as a GUID. Example GUID: | 
| 74       case 0:  // fall through | 74 // "{2251FFAD-DBAB-4E53-8B3A-18F98BB4EB80}" | 
| 75       case 1: | 75 bool IsValidGuid(const string& str) { | 
| 76         if ((c >= '0') && (c <= '9')) | 76   TEST_AND_RETURN_FALSE(str.size() == 38); | 
| 77           break; | 77   TEST_AND_RETURN_FALSE((*str.begin() == '{') && (*str.rbegin() == '}')); | 
| 78         if ((c >= 'a') && (c <= 'f')) | 78   for (string::size_type i = 1; i < (str.size() - 1); ++i) { | 
| 79           break; | 79     if ((i == 9) || (i == 14) || (i == 19) || (i == 24)) { | 
| 80         if ((c >= 'A') && (c <= 'F')) | 80       TEST_AND_RETURN_FALSE(str[i] == '-'); | 
| 81           break; | 81     } else { | 
| 82         return false; | 82       TEST_AND_RETURN_FALSE(IsHexDigit(str[i])); | 
| 83       case 2: |  | 
| 84         if (c == ':') |  | 
| 85           break; |  | 
| 86         return false; |  | 
| 87     } | 83     } | 
| 88   } | 84   } | 
| 89   return true; | 85   return true; | 
| 90 } | 86 } | 
| 91 string GetMachineType() { | 87 string GetMachineType() { | 
| 92   FILE* fp = popen("uname -m", "r"); | 88   FILE* fp = popen("uname -m", "r"); | 
| 93   if (!fp) | 89   if (!fp) | 
| 94     return ""; | 90     return ""; | 
| 95   string ret; | 91   string ret; | 
| 96   for (;;) { | 92   for (;;) { | 
| 97     char buffer[10]; | 93     char buffer[10]; | 
| 98     size_t r = fread(buffer, 1, sizeof(buffer), fp); | 94     size_t r = fread(buffer, 1, sizeof(buffer), fp); | 
| 99     if (r == 0) | 95     if (r == 0) | 
| 100       break; | 96       break; | 
| 101     ret.insert(ret.begin(), buffer, buffer + r); | 97     ret.insert(ret.begin(), buffer, buffer + r); | 
| 102   } | 98   } | 
| 103   // strip trailing '\n' if it exists | 99   // strip trailing '\n' if it exists | 
| 104   if ((*ret.rbegin()) == '\n') | 100   if ((*ret.rbegin()) == '\n') | 
| 105     ret.resize(ret.size() - 1); | 101     ret.resize(ret.size() - 1); | 
| 106   fclose(fp); | 102   fclose(fp); | 
| 107   return ret; | 103   return ret; | 
| 108 } | 104 } | 
| 109 }  // namespace {} | 105 }  // namespace {} | 
| 110 | 106 | 
| 111 TEST_F(OmahaRequestPrepActionTest, SimpleTest) { | 107 TEST_F(OmahaRequestPrepActionTest, SimpleTest) { | 
| 112   ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); | 108   ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); | 
|  | 109   ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + | 
|  | 110                       utils::kStatefulPartition + "/etc")); | 
| 113   { | 111   { | 
| 114     ASSERT_TRUE(WriteFileString( | 112     ASSERT_TRUE(WriteFileString( | 
| 115         kTestDir + "/etc/lsb-release", | 113         kTestDir + "/etc/lsb-release", | 
| 116         "GOOGLE_FOO=bar\nGOOGLE_RELEASE=0.2.2.3\nGOOGLE_TRACK=footrack")); | 114         "GOOGLE_FOO=bar\nGOOGLE_RELEASE=0.2.2.3\nGOOGLE_TRACK=footrack")); | 
| 117     UpdateCheckParams out; | 115     UpdateCheckParams out; | 
| 118     EXPECT_TRUE(DoTest(false, &out)); | 116     EXPECT_TRUE(DoTest(false, &out)); | 
| 119     EXPECT_TRUE(IsValidMac(out.machine_id)); | 117     EXPECT_TRUE(IsValidGuid(out.machine_id)) << "id: " << out.machine_id; | 
| 120     // for now we're just using the machine id here | 118     // for now we're just using the machine id here | 
| 121     EXPECT_TRUE(IsValidMac(out.user_id)); | 119     EXPECT_TRUE(IsValidGuid(out.user_id)) << "id: " << out.user_id; | 
| 122     EXPECT_EQ("Chrome OS", out.os_platform); | 120     EXPECT_EQ("Chrome OS", out.os_platform); | 
| 123     EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); | 121     EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); | 
| 124     EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); | 122     EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); | 
| 125     EXPECT_EQ("0.2.2.3", out.app_version); | 123     EXPECT_EQ("0.2.2.3", out.app_version); | 
| 126     EXPECT_EQ("en-US", out.app_lang); | 124     EXPECT_EQ("en-US", out.app_lang); | 
| 127     EXPECT_EQ("footrack", out.app_track); | 125     EXPECT_EQ("footrack", out.app_track); | 
| 128   } | 126   } | 
| 129   EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); | 127   EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); | 
| 130 } | 128 } | 
| 131 | 129 | 
| 132 TEST_F(OmahaRequestPrepActionTest, MissingTrackTest) { | 130 TEST_F(OmahaRequestPrepActionTest, MissingTrackTest) { | 
| 133   ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); | 131   ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); | 
|  | 132   ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + | 
|  | 133                       utils::kStatefulPartition + "/etc")); | 
| 134   { | 134   { | 
| 135     ASSERT_TRUE(WriteFileString( | 135     ASSERT_TRUE(WriteFileString( | 
| 136         kTestDir + "/etc/lsb-release", | 136         kTestDir + "/etc/lsb-release", | 
| 137         "GOOGLE_FOO=bar\nGOOGLE_RELEASE=0.2.2.3\nGOOGLE_TRXCK=footrack")); | 137         "GOOGLE_FOO=bar\nGOOGLE_RELEASE=0.2.2.3\nGOOGLE_TRXCK=footrack")); | 
| 138     UpdateCheckParams out; | 138     UpdateCheckParams out; | 
| 139     EXPECT_TRUE(DoTest(false, &out)); | 139     EXPECT_TRUE(DoTest(false, &out)); | 
| 140     EXPECT_TRUE(IsValidMac(out.machine_id)); | 140     EXPECT_TRUE(IsValidGuid(out.machine_id)); | 
| 141     // for now we're just using the machine id here | 141     // for now we're just using the machine id here | 
| 142     EXPECT_TRUE(IsValidMac(out.user_id)); | 142     EXPECT_TRUE(IsValidGuid(out.user_id)); | 
| 143     EXPECT_EQ("Chrome OS", out.os_platform); | 143     EXPECT_EQ("Chrome OS", out.os_platform); | 
| 144     EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); | 144     EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); | 
| 145     EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); | 145     EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); | 
| 146     EXPECT_EQ("0.2.2.3", out.app_version); | 146     EXPECT_EQ("0.2.2.3", out.app_version); | 
| 147     EXPECT_EQ("en-US", out.app_lang); | 147     EXPECT_EQ("en-US", out.app_lang); | 
| 148     EXPECT_EQ("", out.app_track); | 148     EXPECT_EQ("", out.app_track); | 
| 149   } | 149   } | 
| 150   EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); | 150   EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); | 
| 151 } | 151 } | 
| 152 | 152 | 
| 153 TEST_F(OmahaRequestPrepActionTest, ConfusingReleaseTest) { | 153 TEST_F(OmahaRequestPrepActionTest, ConfusingReleaseTest) { | 
| 154   ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); | 154   ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc")); | 
|  | 155   ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + | 
|  | 156                       utils::kStatefulPartition + "/etc")); | 
| 155   { | 157   { | 
| 156     ASSERT_TRUE(WriteFileString( | 158     ASSERT_TRUE(WriteFileString( | 
| 157         kTestDir + "/etc/lsb-release", | 159         kTestDir + "/etc/lsb-release", | 
| 158         "GOOGLE_FOO=GOOGLE_RELEASE=1.2.3.4\n" | 160         "GOOGLE_FOO=GOOGLE_RELEASE=1.2.3.4\n" | 
| 159         "GOOGLE_RELEASE=0.2.2.3\nGOOGLE_TRXCK=footrack")); | 161         "GOOGLE_RELEASE=0.2.2.3\nGOOGLE_TRXCK=footrack")); | 
| 160     UpdateCheckParams out; | 162     UpdateCheckParams out; | 
| 161     EXPECT_TRUE(DoTest(false, &out)); | 163     EXPECT_TRUE(DoTest(false, &out)); | 
| 162     EXPECT_TRUE(IsValidMac(out.machine_id)); | 164     EXPECT_TRUE(IsValidGuid(out.machine_id)) << out.machine_id; | 
| 163     // for now we're just using the machine id here | 165     // for now we're just using the machine id here | 
| 164     EXPECT_TRUE(IsValidMac(out.user_id)); | 166     EXPECT_TRUE(IsValidGuid(out.user_id)); | 
| 165     EXPECT_EQ("Chrome OS", out.os_platform); | 167     EXPECT_EQ("Chrome OS", out.os_platform); | 
| 166     EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); | 168     EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp); | 
| 167     EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); | 169     EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id); | 
| 168     EXPECT_EQ("0.2.2.3", out.app_version); | 170     EXPECT_EQ("0.2.2.3", out.app_version); | 
| 169     EXPECT_EQ("en-US", out.app_lang); | 171     EXPECT_EQ("en-US", out.app_lang); | 
| 170     EXPECT_EQ("", out.app_track); | 172     EXPECT_EQ("", out.app_track); | 
| 171   } | 173   } | 
| 172   EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); | 174   EXPECT_EQ(0, System(string("rm -rf ") + kTestDir)); | 
| 173 } | 175 } | 
| 174 | 176 | 
|  | 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 | 
| 175 }  // namespace chromeos_update_engine | 199 }  // namespace chromeos_update_engine | 
| OLD | NEW | 
|---|