OLD | NEW |
| (Empty) |
1 // Copyright 2008-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 // | |
17 // System level tests for On Demand. Unlike omaha_unittest.cpp, this test | |
18 // should be run one test at a time view --gtest_filter. The tests all assume | |
19 // that there is an update available for the app (specified via guid as the | |
20 // first argument). | |
21 | |
22 #include "base/basictypes.h" | |
23 #include "omaha/testing/unit_test.h" | |
24 #include "omaha/third_party/gtest/include/gtest/gtest.h" | |
25 #include "omaha/tools/performondemand/performondemand.h" | |
26 #include "omaha/base/utils.h" | |
27 #include <windows.h> | |
28 #include <atltime.h> | |
29 | |
30 namespace omaha { | |
31 | |
32 // Lazily define the guid and is_machine (passed in at the command line) global. | |
33 CString guid; | |
34 bool is_machine; | |
35 const int UPDATE_TIMEOUT = 60; | |
36 | |
37 class OnDemandTest : public testing::Test { | |
38 protected: | |
39 | |
40 virtual void SetUp() { | |
41 wprintf(_T("Initializing\n")); | |
42 HRESULT hr = CComObject<JobObserver>::CreateInstance(&job_observer); | |
43 ASSERT_EQ(S_OK, hr); | |
44 job_holder = job_observer; | |
45 ASSERT_EQ(Reconnect(), S_OK); | |
46 } | |
47 | |
48 virtual HRESULT Reconnect() { | |
49 on_demand = NULL; | |
50 if (is_machine) { | |
51 return on_demand.CoCreateInstance( | |
52 L"GoogleUpdate.OnDemandCOMClassMachine"); | |
53 } else { | |
54 return on_demand.CoCreateInstance( | |
55 L"GoogleUpdate.OnDemandCOMClassUser"); | |
56 } | |
57 } | |
58 | |
59 virtual void TearDown() { | |
60 job_holder = NULL; | |
61 on_demand = NULL; | |
62 } | |
63 | |
64 void WaitForUpdateCompletion(int timeout) { | |
65 MSG msg; | |
66 SYSTEMTIME start_system_time = {0}; | |
67 SYSTEMTIME current_system_time = {0}; | |
68 ::GetSystemTime(&start_system_time); | |
69 CTime start_time(start_system_time); | |
70 CTimeSpan timeout_period(0, 0, 0, timeout); | |
71 | |
72 while (::GetMessage(&msg, NULL, 0, 0)) | |
73 { | |
74 ::TranslateMessage(&msg); | |
75 ::DispatchMessage(&msg); | |
76 ::GetSystemTime(¤t_system_time); | |
77 CTime current_time(current_system_time); | |
78 CTimeSpan elapsed_time = current_time - start_time; | |
79 if (timeout_period < elapsed_time) { | |
80 wprintf(_T("Timed out.\n")); | |
81 break; | |
82 } | |
83 } | |
84 | |
85 PrintSummary(); | |
86 } | |
87 | |
88 void PrintSummary() { | |
89 wprintf(_T("Observed: [0x%x]\n"), job_observer->observed); | |
90 } | |
91 | |
92 void ExpectSuccessful() { | |
93 // Make sure we got a succesfful install. | |
94 char* err_msg = "Did not observe a complete, successful install!"; | |
95 EXPECT_TRUE(job_observer->observed & ( | |
96 // Complete codes we don't expect are intentially commented out. | |
97 ON_COMPLETE_SUCCESS | | |
98 //ON_COMPLETE_SUCCESS_CLOSE_UI | | |
99 //ON_COMPLETE_RESTART_ALL_BROWSERS | | |
100 //ON_COMPLETE_REBOOT | | |
101 //ON_COMPLETE_RESTART_BROWSER | | |
102 ON_COMPLETE_RESTART_ALL_BROWSERS_NOTICE_ONLY | |
103 //ON_COMPLETE_REBOOT_NOTICE_ONLY | | |
104 //ON_COMPLETE_RESTART_BROWSER_NOTICE_ONLY | | |
105 //ON_COMPLETE_RUN_COMMAND | |
106 )) << err_msg; | |
107 } | |
108 | |
109 CComObject<JobObserver>* job_observer; | |
110 CComPtr<IJobObserver> job_holder; | |
111 CComPtr<IGoogleUpdate> on_demand; | |
112 }; | |
113 | |
114 TEST_F(OnDemandTest, BasicUpdate) { | |
115 wprintf(_T("Starting Update\n")); | |
116 HRESULT hr = on_demand->Update(guid, job_observer); | |
117 ASSERT_EQ(S_OK, hr); | |
118 WaitForUpdateCompletion(UPDATE_TIMEOUT); | |
119 | |
120 ExpectSuccessful(); | |
121 char* err_msg = "Did not observe a complete, successful install!"; | |
122 EXPECT_TRUE(job_observer->observed & ON_INSTALLING) << err_msg; | |
123 } | |
124 | |
125 TEST_F(OnDemandTest, OnDemandDuringAutoUpdate) { | |
126 wprintf(_T("Starting Update\n")); | |
127 HRESULT hr = on_demand->Update(guid, job_observer); | |
128 ASSERT_EQ(S_OK, hr); | |
129 WaitForUpdateCompletion(UPDATE_TIMEOUT); | |
130 | |
131 ExpectSuccessful(); | |
132 char* err_msg = "Did not observe a complete, successful install!"; | |
133 EXPECT_TRUE(job_observer->observed & ON_INSTALLING) << err_msg; | |
134 } | |
135 | |
136 | |
137 TEST_F(OnDemandTest, UpdateThenNoUpdate) { | |
138 wprintf(_T("Starting Update\n")); | |
139 HRESULT hr = on_demand->Update(guid, job_observer); | |
140 ASSERT_EQ(S_OK, hr); | |
141 WaitForUpdateCompletion(UPDATE_TIMEOUT); | |
142 | |
143 ExpectSuccessful(); | |
144 char* err_msg = "Did not observe a complete, successful install!"; | |
145 EXPECT_TRUE(job_observer->observed & ON_INSTALLING) << err_msg; | |
146 | |
147 | |
148 wprintf(_T("Starting Second Update Request\n")); | |
149 // Reset the memory of observed actions. | |
150 job_observer->Reset(); | |
151 hr = on_demand->Update(guid, job_observer); | |
152 ASSERT_EQ(S_OK, hr); | |
153 WaitForUpdateCompletion(UPDATE_TIMEOUT); | |
154 | |
155 ExpectSuccessful(); | |
156 EXPECT_FALSE(job_observer->observed & ON_INSTALLING) << err_msg; | |
157 } | |
158 | |
159 | |
160 TEST_F(OnDemandTest, CloseDuringCheckingForUpdates) { | |
161 wprintf(_T("Starting Update\n")); | |
162 | |
163 job_observer->AddCloseMode(ON_CHECKING_FOR_UPDATES); | |
164 | |
165 HRESULT hr = on_demand->Update(guid, job_observer); | |
166 ASSERT_EQ(S_OK, hr); | |
167 WaitForUpdateCompletion(UPDATE_TIMEOUT); | |
168 | |
169 char* err_msg = "Observed a complete install when should have closed!"; | |
170 EXPECT_TRUE(job_observer->observed & ON_COMPLETE_ERROR) << err_msg; | |
171 EXPECT_FALSE(job_observer->observed & ON_INSTALLING) << err_msg; | |
172 } | |
173 | |
174 | |
175 TEST_F(OnDemandTest, CloseDuringDownload) { | |
176 wprintf(_T("Starting Update\n")); | |
177 | |
178 job_observer->AddCloseMode(ON_DOWNLOADING); | |
179 | |
180 HRESULT hr = on_demand->Update(guid, job_observer); | |
181 ASSERT_EQ(S_OK, hr); | |
182 WaitForUpdateCompletion(UPDATE_TIMEOUT); | |
183 | |
184 char* err_msg = "Observed an install when should have closed!"; | |
185 EXPECT_FALSE(job_observer->observed & ON_INSTALLING) << err_msg; | |
186 } | |
187 | |
188 | |
189 TEST_F(OnDemandTest, CloseDuringDownloadAndTryAgain) { | |
190 wprintf(_T("Starting Update\n")); | |
191 | |
192 job_observer->AddCloseMode(ON_DOWNLOADING); | |
193 | |
194 HRESULT hr = on_demand->Update(guid, job_observer); | |
195 ASSERT_EQ(S_OK, hr); | |
196 WaitForUpdateCompletion(UPDATE_TIMEOUT); | |
197 | |
198 char* err_msg = "Observed a complete, install when should have closed!"; | |
199 EXPECT_FALSE(job_observer->observed & ON_INSTALLING) << err_msg; | |
200 | |
201 wprintf(_T("Requesting update.\n")); | |
202 // Try a second time, but this time don't interfere. An update should ensue. | |
203 job_observer->Reset(); | |
204 hr = on_demand->Update(guid, job_observer); | |
205 ASSERT_EQ(S_OK, hr); | |
206 WaitForUpdateCompletion(UPDATE_TIMEOUT); | |
207 | |
208 ExpectSuccessful(); | |
209 EXPECT_TRUE(job_observer->observed & ON_INSTALLING) << err_msg; | |
210 } | |
211 | |
212 | |
213 TEST_F(OnDemandTest, UpdateWithOmahaUpdateAvailable) { | |
214 wprintf(_T("Starting Update\n")); | |
215 HRESULT hr = on_demand->Update(guid, job_observer); | |
216 ASSERT_EQ(S_OK, hr); | |
217 WaitForUpdateCompletion(UPDATE_TIMEOUT); | |
218 | |
219 ExpectSuccessful(); | |
220 char* err_msg = "Did not observe a complete, successful install!"; | |
221 EXPECT_TRUE(job_observer->observed & ON_INSTALLING) << err_msg; | |
222 } | |
223 | |
224 | |
225 TEST_F(OnDemandTest, UpdateAfterOmahaUpdate) { | |
226 // Test shutdown code by first connecting the the com instance, waiting around | |
227 // for Omaha to update (done by OnDemandTestFactory.py), and then updating. | |
228 wprintf(_T("Waiting 60 seconds for omaha to update itself.\n")); | |
229 ::SleepEx(60000, true); | |
230 | |
231 wprintf(_T("Attempting update on shut-down server.\n")); | |
232 HRESULT hr = on_demand->Update(guid, job_observer); | |
233 ASSERT_EQ(hr, HRESULT_FROM_WIN32(RPC_S_SERVER_UNAVAILABLE)) << | |
234 "Goopdate should have shutdown."; | |
235 | |
236 wprintf(_T("Reconnecting to COM\n")); | |
237 ASSERT_EQ(Reconnect(), S_OK); | |
238 | |
239 wprintf(_T("Starting Update\n")); | |
240 hr = on_demand->Update(guid, job_observer); | |
241 ASSERT_EQ(S_OK, hr); | |
242 | |
243 WaitForUpdateCompletion(UPDATE_TIMEOUT); | |
244 | |
245 ExpectSuccessful(); | |
246 char* err_msg = "Did not observe a complete, successful install!"; | |
247 EXPECT_TRUE(job_observer->observed & ON_INSTALLING) << err_msg; | |
248 } | |
249 | |
250 | |
251 | |
252 bool ParseParams(int argc, TCHAR* argv[], CString* guid, bool* is_machine) { | |
253 ASSERT1(argv); | |
254 ASSERT1(guid); | |
255 if (argc < 3) { | |
256 return false; | |
257 } | |
258 *guid = argv[1]; | |
259 // NOTE(cnygaard): I tried static casting from int to bool but it gave me a | |
260 // nasty warning about losing efficiency. | |
261 if (0 == _ttoi(argv[2])) { | |
262 *is_machine = false; | |
263 } else { | |
264 *is_machine = true; | |
265 } | |
266 | |
267 // Verify that the guid is valid. | |
268 GUID parsed = StringToGuid(*guid); | |
269 if (parsed == GUID_NULL) { | |
270 return false; | |
271 } | |
272 | |
273 argc -= 2; | |
274 testing::InitGoogleTest(&argc, argv+2); | |
275 return true; | |
276 } | |
277 | |
278 } // namespace omaha | |
279 | |
280 int _tmain(int argc, TCHAR** argv) { | |
281 | |
282 if (!omaha::ParseParams(argc, argv, &omaha::guid, &omaha::is_machine)) { | |
283 wprintf(_T("Usage: ondemandsystem_unittest.exe \n")); | |
284 wprintf(_T(" [{GUID}] [is_machine (0|1)] --gtest_filter=<testname>\n")); | |
285 return 0; | |
286 } | |
287 omaha::FailOnAssert fail_on_assert; | |
288 CComModule module; | |
289 scoped_co_init com_apt; | |
290 | |
291 int result = RUN_ALL_TESTS(); | |
292 return result; | |
293 } | |
294 | |
OLD | NEW |