OLD | NEW |
| (Empty) |
1 // Copyright 2007-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 #include "omaha/test/step_test.h" | |
17 | |
18 const TCHAR *StepTestBase::RESUME_EVENT_TEMPLATE = _T("R %s"); | |
19 const TCHAR *StepTestBase::STEP_REACHED_EVENT_TEMPLATE = _T("SR %s"); | |
20 const TCHAR *StepTestBase::STATE_REG_KEY_TEMPLATE = _T("SOFTWARE\\GAT %s"); | |
21 const TCHAR *StepTestBase::STATE_REG_VALUE_NAME = _T("state"); | |
22 | |
23 StepTestBase::StepTestBase(const TCHAR*) : step_reached_event_(NULL), | |
24 resume_event_(NULL), state_key_(NULL) { | |
25 } | |
26 | |
27 void StepTestBase::Terminate() { | |
28 CloseHandle(step_reached_event_); | |
29 CloseHandle(resume_event_); | |
30 RegCloseKey(state_key_); | |
31 } | |
32 | |
33 StepTestBase::~StepTestBase() { | |
34 Terminate(); | |
35 } | |
36 | |
37 StepTestStepper::StepTestStepper(const TCHAR* name) : | |
38 StepTestBase(name), | |
39 is_someone_listening_(false) { | |
40 CString namebuf; | |
41 | |
42 // A "broadcaster" is a program that notifies the "listener" program that | |
43 // the broadcaster has reached an interesting point in its execution, and | |
44 // the listener should check state. | |
45 // | |
46 // Results will be unpredictable if two broadcasters are running at the | |
47 // same time. However, since a broadcaster will only open the resume event | |
48 // and not create it, we shouldn't see any problems where two broadcasters | |
49 // deadlock each other. | |
50 namebuf.Format(RESUME_EVENT_TEMPLATE, name); | |
51 resume_event_ = OpenEvent(EVENT_ALL_ACCESS, FALSE, namebuf); | |
52 if (resume_event_ != NULL) { | |
53 is_someone_listening_ = true; | |
54 | |
55 namebuf.Format(STEP_REACHED_EVENT_TEMPLATE, name); | |
56 step_reached_event_ = OpenEvent(EVENT_ALL_ACCESS, FALSE, namebuf); | |
57 | |
58 namebuf.Format(STATE_REG_KEY_TEMPLATE, name); | |
59 RegCreateKeyEx(HKEY_CURRENT_USER, namebuf, 0, NULL, REG_OPTION_VOLATILE, | |
60 KEY_READ | KEY_WRITE, NULL, &state_key_, NULL); | |
61 } else { | |
62 Terminate(); | |
63 } | |
64 } | |
65 | |
66 StepTestStepper::~StepTestStepper() { | |
67 Terminate(); | |
68 } | |
69 | |
70 void StepTestStepper::Step(DWORD step) { | |
71 if (!is_someone_listening_) { | |
72 return; | |
73 } | |
74 RegSetValueEx(state_key_, STATE_REG_VALUE_NAME, 0, REG_DWORD, | |
75 reinterpret_cast<BYTE*>(&step), sizeof(step)); | |
76 SetEvent(step_reached_event_); | |
77 WaitForSingleObject(resume_event_, INFINITE); | |
78 } | |
79 | |
80 StepTestWatcher::StepTestWatcher(const TCHAR* name) : StepTestBase(name), | |
81 name_(name) { | |
82 } | |
83 | |
84 StepTestWatcher::~StepTestWatcher() { | |
85 Terminate(); | |
86 } | |
87 | |
88 void StepTestWatcher::Go() { | |
89 DWORD step = 0; | |
90 bool done = false; | |
91 | |
92 PrintIntroduction(); | |
93 | |
94 printf("Verifying preconditions...\r\n"); | |
95 bool passed = VerifyStep(0, &done); | |
96 if (passed) { | |
97 _tprintf(_T("Preconditions OK.\r\n")); | |
98 } else { | |
99 _tprintf(_T("*** Preconditions FAILED. ***\r\n")); | |
100 } | |
101 | |
102 CString namebuf; | |
103 | |
104 namebuf.Format(RESUME_EVENT_TEMPLATE, name_); | |
105 resume_event_ = CreateEvent(NULL, FALSE, FALSE, namebuf); | |
106 namebuf.Format(STEP_REACHED_EVENT_TEMPLATE, name_); | |
107 step_reached_event_ = CreateEvent(NULL, FALSE, FALSE, namebuf); | |
108 | |
109 namebuf.Format(STATE_REG_KEY_TEMPLATE, name_); | |
110 RegCreateKeyEx(HKEY_CURRENT_USER, namebuf, 0, NULL, REG_OPTION_VOLATILE, | |
111 KEY_READ, NULL, &state_key_, NULL); | |
112 | |
113 printf("Begin test now.\r\n"); | |
114 | |
115 done = false; | |
116 while (!done) { | |
117 WaitForSingleObject(step_reached_event_, INFINITE); | |
118 DWORD step_size = sizeof(step); | |
119 RegQueryValueEx(state_key_, STATE_REG_VALUE_NAME, NULL, NULL, | |
120 reinterpret_cast<BYTE*>(&step), &step_size); | |
121 printf("Testing step %d.\r\n", step); | |
122 passed = VerifyStep(step, &done); | |
123 if (!passed) { | |
124 _tprintf(_T("\r\n\r\n*** TEST FAILURE: Step %d. ***\r\nr\n"), step); | |
125 } | |
126 SetEvent(resume_event_); | |
127 } | |
128 | |
129 PrintConclusion(); | |
130 } | |
131 | |
132 bool StepTestWatcher::RegistryKeyExists(HKEY root, const TCHAR* name) { | |
133 HKEY key; | |
134 LONG result = RegOpenKeyEx(root, name, 0, KEY_READ, &key); | |
135 if (ERROR_SUCCESS == result) { | |
136 RegCloseKey(key); | |
137 return true; | |
138 } | |
139 return false; | |
140 } | |
141 | |
142 bool StepTestWatcher::RegistryValueExists(HKEY root, const TCHAR* key_name, | |
143 const TCHAR* value_name) { | |
144 bool result = false; | |
145 HKEY key; | |
146 LONG reg_result = RegOpenKeyEx(root, key_name, 0, KEY_READ, &key); | |
147 if (ERROR_SUCCESS == reg_result) { | |
148 DWORD dummy_size = 0; | |
149 reg_result = RegQueryValueEx(key, value_name, NULL, NULL, NULL, | |
150 &dummy_size); | |
151 if (reg_result == ERROR_SUCCESS && dummy_size > 0) { | |
152 result = true; | |
153 } | |
154 RegCloseKey(key); | |
155 } | |
156 return result; | |
157 } | |
OLD | NEW |