OLD | NEW |
| (Empty) |
1 // Copyright 2011 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 <io.h> | |
17 #include <stdio.h> | |
18 #include <atlstr.h> | |
19 #include <atlsimpstr.h> | |
20 #include <windows.h> | |
21 #include "base/scoped_ptr.h" | |
22 #include "omaha/base/app_util.h" | |
23 #include "omaha/base/file.h" | |
24 #include "omaha/base/reg_key.h" | |
25 #include "omaha/base/scope_guard.h" | |
26 #include "omaha/base/scoped_any.h" | |
27 #include "omaha/base/scoped_ptr_address.h" | |
28 #include "omaha/base/utils.h" | |
29 #include "omaha/common/config_manager.h" | |
30 #include "omaha/common/const_goopdate.h" | |
31 #include "omaha/goopdate/app_command.h" | |
32 #include "omaha/goopdate/app_unittest_base.h" | |
33 #include "omaha/testing/unit_test.h" | |
34 | |
35 namespace omaha { | |
36 | |
37 namespace { | |
38 | |
39 const TCHAR* const kAppGuid1 = _T("{3B1A3CCA-0525-4418-93E6-A0DB3398EC9B}"); | |
40 | |
41 const TCHAR* const kCmdLineExit0 = _T("cmd.exe /c \"exit 0\""); | |
42 | |
43 const TCHAR* const kCmdId1 = _T("command one"); | |
44 const TCHAR* const kCmdId2 = _T("command two"); | |
45 | |
46 const TCHAR* const kSessionId = _T("unittest_session_id"); | |
47 | |
48 const bool kTrue = true; | |
49 const bool kFalse = false; | |
50 | |
51 const DWORD kOne = 1; | |
52 const DWORD kTwo = 2; | |
53 | |
54 } // namespace | |
55 | |
56 CString GetEchoCommandLine(CString string, CString output_file) { | |
57 CString command_line; | |
58 _sntprintf_s(CStrBuf(command_line, MAX_PATH), | |
59 MAX_PATH, | |
60 _TRUNCATE, | |
61 _T("cmd.exe /c \"echo %s > \"%s\"\""), | |
62 static_cast<const TCHAR*>(string), | |
63 static_cast<const TCHAR*>(output_file)); | |
64 return command_line; | |
65 } | |
66 | |
67 class AppCommandTest : public AppTestBaseWithRegistryOverride { | |
68 protected: | |
69 // false == is_machine | |
70 AppCommandTest() : AppTestBaseWithRegistryOverride(false, true) {} | |
71 | |
72 static void CreateAppClientKey(const CString& guid, bool is_machine) { | |
73 CString client_key_name = AppendRegKeyPath( | |
74 ConfigManager::Instance()->registry_clients(is_machine), guid); | |
75 | |
76 RegKey client_key; | |
77 | |
78 ASSERT_SUCCEEDED(client_key.Create(client_key_name)); | |
79 ASSERT_SUCCEEDED(client_key.SetValue(kRegValueProductVersion, | |
80 _T("1.1.1.3"))); | |
81 ASSERT_SUCCEEDED(client_key.SetValue(kRegValueAppName, | |
82 _T("Dispay Name of ") + guid)); | |
83 } | |
84 | |
85 static void CreateLegacyCommand(const CString& guid, | |
86 bool is_machine, | |
87 const CString& cmd_id, | |
88 const CString& cmd_line) { | |
89 CString client_key_name = AppendRegKeyPath( | |
90 ConfigManager::Instance()->registry_clients(is_machine), guid); | |
91 | |
92 RegKey client_key; | |
93 | |
94 ASSERT_SUCCEEDED(client_key.Create(client_key_name)); | |
95 ASSERT_SUCCEEDED(client_key.SetValue(cmd_id, cmd_line)); | |
96 } | |
97 | |
98 static void CreateCommand(const CString& guid, | |
99 bool is_machine, | |
100 const CString& cmd_id, | |
101 const CString& cmd_line, | |
102 const bool* sends_pings, | |
103 const bool* is_web_accessible, | |
104 const DWORD* reporting_id) { | |
105 CString client_key_name = AppendRegKeyPath( | |
106 ConfigManager::Instance()->registry_clients(is_machine), guid); | |
107 | |
108 CString command_key_name = AppendRegKeyPath(client_key_name, | |
109 kCommandsRegKeyName, | |
110 cmd_id); | |
111 | |
112 RegKey command_key; | |
113 | |
114 ASSERT_SUCCEEDED(command_key.Create(command_key_name)); | |
115 ASSERT_SUCCEEDED(command_key.SetValue(kRegValueCommandLine, cmd_line)); | |
116 if (sends_pings != NULL) { | |
117 ASSERT_SUCCEEDED(command_key.SetValue( | |
118 kRegValueSendsPings, static_cast<DWORD>(*sends_pings ? 1 : 0))); | |
119 } | |
120 if (is_web_accessible != NULL) { | |
121 ASSERT_SUCCEEDED(command_key.SetValue( | |
122 kRegValueWebAccessible, | |
123 static_cast<DWORD>(*is_web_accessible ? 1 : 0))); | |
124 } | |
125 if (reporting_id != NULL) { | |
126 ASSERT_SUCCEEDED(command_key.SetValue(kRegValueReportingId, | |
127 *reporting_id)); | |
128 } | |
129 } | |
130 }; // class AppCommandTest | |
131 | |
132 TEST_F(AppCommandTest, NoApp) { | |
133 scoped_ptr<AppCommand> app_command; | |
134 ASSERT_FAILED(AppCommand::Load( | |
135 kAppGuid1, false, kCmdId1, kSessionId, address(app_command))); | |
136 } | |
137 | |
138 TEST_F(AppCommandTest, NoCmd) { | |
139 scoped_ptr<AppCommand> app_command; | |
140 CreateAppClientKey(kAppGuid1, false); | |
141 CreateCommand( | |
142 kAppGuid1, false, kCmdId1, kCmdLineExit0, &kTrue, &kFalse, &kOne); | |
143 | |
144 ASSERT_FAILED(AppCommand::Load( | |
145 kAppGuid1, false, kCmdId2, kSessionId, address(app_command))); | |
146 } | |
147 | |
148 TEST_F(AppCommandTest, WrongLevel) { | |
149 scoped_ptr<AppCommand> app_command; | |
150 CreateAppClientKey(kAppGuid1, true); | |
151 CreateCommand( | |
152 kAppGuid1, true, kCmdId1, kCmdLineExit0, &kTrue, &kFalse, &kOne); | |
153 | |
154 ASSERT_FAILED(AppCommand::Load( | |
155 kAppGuid1, false, kCmdId1, kSessionId, address(app_command))); | |
156 } | |
157 | |
158 TEST_F(AppCommandTest, LoadCommand) { | |
159 scoped_ptr<AppCommand> app_command; | |
160 CreateAppClientKey(kAppGuid1, true); | |
161 CreateCommand( | |
162 kAppGuid1, true, kCmdId1, kCmdLineExit0, &kTrue, &kFalse, &kOne); | |
163 | |
164 ASSERT_SUCCEEDED(AppCommand::Load( | |
165 kAppGuid1, true, kCmdId1, kSessionId, address(app_command))); | |
166 ASSERT_FALSE(app_command->is_web_accessible()); | |
167 } | |
168 | |
169 TEST_F(AppCommandTest, LoadCommandDefaultValues) { | |
170 scoped_ptr<AppCommand> app_command; | |
171 CreateAppClientKey(kAppGuid1, true); | |
172 CreateCommand( | |
173 kAppGuid1, true, kCmdId1, kCmdLineExit0, NULL, NULL, NULL); | |
174 | |
175 ASSERT_SUCCEEDED(AppCommand::Load( | |
176 kAppGuid1, true, kCmdId1, kSessionId, address(app_command))); | |
177 ASSERT_FALSE(app_command->is_web_accessible()); | |
178 } | |
179 | |
180 TEST_F(AppCommandTest, LoadWebAccessibleCommand) { | |
181 scoped_ptr<AppCommand> app_command; | |
182 CreateAppClientKey(kAppGuid1, true); | |
183 CreateCommand( | |
184 kAppGuid1, true, kCmdId1, kCmdLineExit0, NULL, &kTrue, NULL); | |
185 | |
186 ASSERT_SUCCEEDED(AppCommand::Load( | |
187 kAppGuid1, true, kCmdId1, kSessionId, address(app_command))); | |
188 ASSERT_TRUE(app_command->is_web_accessible()); | |
189 } | |
190 | |
191 TEST_F(AppCommandTest, Execute) { | |
192 CString temp_file; | |
193 ASSERT_TRUE(::GetTempFileName(app_util::GetTempDir(), | |
194 _T("omaha"), | |
195 0, | |
196 CStrBuf(temp_file, MAX_PATH))); | |
197 | |
198 // GetTempFileName created an empty file. Delete it. | |
199 ASSERT_EQ(0, _tunlink(temp_file)); | |
200 | |
201 // Hopefully we will cause the file to be created. Cause its deletion at exit. | |
202 ON_SCOPE_EXIT(_tunlink, temp_file); | |
203 | |
204 CString command_line = GetEchoCommandLine(_T("hello world!"), temp_file); | |
205 | |
206 scoped_ptr<AppCommand> app_command; | |
207 CreateAppClientKey(kAppGuid1, true); | |
208 CreateCommand( | |
209 kAppGuid1, true, kCmdId1, command_line, &kTrue, &kTrue, NULL); | |
210 | |
211 ASSERT_SUCCEEDED(AppCommand::Load( | |
212 kAppGuid1, true, kCmdId1, kSessionId, address(app_command))); | |
213 | |
214 scoped_process process; | |
215 ASSERT_SUCCEEDED(app_command->Execute(address(process))); | |
216 ASSERT_EQ(WAIT_OBJECT_0, WaitForSingleObject(get(process), 16 * kMsPerSec)); | |
217 | |
218 ASSERT_TRUE(File::Exists(temp_file)); | |
219 } | |
220 | |
221 } // namespace omaha | |
OLD | NEW |