Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: base/test/test_file_util_win.cc

Issue 10914109: Refactoring and tests for the highly undertested file_util::CreateOrUpdateShortcutLink() method. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/test/test_file_util.h" 5 #include "base/test/test_file_util.h"
6 6
7 #include <aclapi.h> 7 #include <aclapi.h>
8 #include <shlwapi.h> 8 #include <shlwapi.h>
9 #include <windows.h> 9 #include <windows.h>
10 #include <shlobj.h>
11 #include <propkey.h>
12 #include <propvarutil.h>
10 13
11 #include <vector> 14 #include <vector>
12 15
13 #include "base/file_path.h" 16 #include "base/file_path.h"
14 #include "base/file_util.h" 17 #include "base/file_util.h"
15 #include "base/logging.h" 18 #include "base/logging.h"
16 #include "base/string_split.h" 19 #include "base/string_split.h"
20 #include "base/win/scoped_comptr.h"
17 #include "base/win/scoped_handle.h" 21 #include "base/win/scoped_handle.h"
22 #include "base/win/windows_version.h"
18 #include "base/threading/platform_thread.h" 23 #include "base/threading/platform_thread.h"
19 24
25 // propsys.lib is required for PropvariantTo*().
26 #pragma comment(lib, "propsys.lib")
27
20 namespace file_util { 28 namespace file_util {
21 29
22 static const ptrdiff_t kOneMB = 1024 * 1024; 30 static const ptrdiff_t kOneMB = 1024 * 1024;
23 31
24 namespace { 32 namespace {
25 33
26 struct PermissionInfo { 34 struct PermissionInfo {
27 PSECURITY_DESCRIPTOR security_descriptor; 35 PSECURITY_DESCRIPTOR security_descriptor;
28 ACL dacl; 36 ACL dacl;
29 }; 37 };
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 // This will prevent us from evicting from the cache, but these don't 269 // This will prevent us from evicting from the cache, but these don't
262 // matter anyway. 270 // matter anyway.
263 EvictFileFromSystemCache(cur_dest_path); 271 EvictFileFromSystemCache(cur_dest_path);
264 } 272 }
265 } while (FindNextFile(fh, &fd)); 273 } while (FindNextFile(fh, &fd));
266 274
267 FindClose(fh); 275 FindClose(fh);
268 return true; 276 return true;
269 } 277 }
270 278
279 VerifyShortcutStatus VerifyShortcut(const string16& shortcut_path,
280 const ShortcutProperties& properties) {
281 base::win::ScopedComPtr<IShellLink> i_shell_link;
282 base::win::ScopedComPtr<IPersistFile> i_persist_file;
283 wchar_t temp_path[MAX_PATH] = {0};
284
285 wchar_t read_target[MAX_PATH] = {0};
286 wchar_t read_working_dir[MAX_PATH] = {0};
287 wchar_t read_arguments[MAX_PATH] = {0};
288 wchar_t read_description[MAX_PATH] = {0};
289 wchar_t read_icon[MAX_PATH] = {0};
290 int read_icon_index = 0;
291
292 // Initialize the shell interfaces.
293 if (FAILED(i_shell_link.CreateInstance(CLSID_ShellLink, NULL,
294 CLSCTX_INPROC_SERVER)) ||
295 FAILED(i_persist_file.QueryFrom(i_shell_link))) {
296 return VERIFY_SHORTCUT_FAILURE_UNEXPECTED;
297 }
298
299 // Load the shortcut.
300 if (FAILED(i_persist_file->Load(shortcut_path.c_str(), 0)))
301 return VERIFY_SHORTCUT_FAILURE_FILE_NOT_FOUND;
302
303 if ((properties.bitfield & ShortcutProperties::PROPERTIES_TARGET) != 0 &&
robertshield 2012/09/06 02:37:34 None of the != 0s are needed here, are you using t
gab 2012/09/06 04:20:02 Right, done in file_util_win.cc as well.
304 (FAILED(i_shell_link->GetPath(read_target, MAX_PATH, NULL,
305 SLGP_SHORTPATH)) ||
306 ::GetShortPathName(properties.target.c_str(), temp_path,
307 MAX_PATH) == 0 ||
308 string16(read_target) != string16(temp_path))) {
309 return VERIFY_SHORTCUT_FAILURE_TARGET;
310 }
311
312 if ((properties.bitfield & ShortcutProperties::PROPERTIES_WORKING_DIR) != 0 &&
313 (FAILED(i_shell_link->GetWorkingDirectory(read_working_dir, MAX_PATH)) ||
314 string16(read_working_dir) != properties.working_dir)) {
315 return VERIFY_SHORTCUT_FAILURE_WORKING_DIR;
316 }
317
318 if ((properties.bitfield & ShortcutProperties::PROPERTIES_ARGUMENTS) != 0 &&
319 (FAILED(i_shell_link->GetArguments(read_arguments, MAX_PATH)) ||
320 string16(read_arguments) != properties.arguments)) {
321 return VERIFY_SHORTCUT_FAILURE_ARGUMENTS;
322 }
323
324 if ((properties.bitfield & ShortcutProperties::PROPERTIES_DESCRIPTION) != 0 &&
325 (FAILED(i_shell_link->GetDescription(read_description, MAX_PATH)) ||
326 string16(read_description) != properties.description)) {
327 return VERIFY_SHORTCUT_FAILURE_DESCRIPTION;
328 }
329
330 if ((properties.bitfield & ShortcutProperties::PROPERTIES_ICON) != 0 &&
331 (FAILED(i_shell_link->GetIconLocation(read_icon, MAX_PATH,
332 &read_icon_index)) ||
333 ::GetLongPathName(properties.icon.c_str(), temp_path, MAX_PATH) == 0 ||
334 string16(read_icon) != string16(temp_path) ||
335 read_icon_index != properties.icon_index)) {
336 return VERIFY_SHORTCUT_FAILURE_ICON;
337 }
338
339 if(base::win::GetVersion() >= base::win::VERSION_WIN7) {
340 base::win::ScopedComPtr<IPropertyStore> property_store;
341 // Note that, as mentioned on MSDN at http://goo.gl/M8h9g, if a property is
342 // not set, GetValue will return S_OK and the PROPVARIANT will be set to
343 // VT_EMPTY.
344 PROPVARIANT pv_app_id, pv_dual_mode;
345 if (FAILED(property_store.QueryFrom(i_shell_link)) ||
346 property_store->GetValue(PKEY_AppUserModel_ID, &pv_app_id) != S_OK ||
347 property_store->GetValue(PKEY_AppUserModel_IsDualMode,
348 &pv_dual_mode) != S_OK) {
349 return VERIFY_SHORTCUT_FAILURE_UNEXPECTED;
350 }
351
352 // Note, as mentioned on MSDN at http://goo.gl/hZ3sO, if |pv_app_id| is a
353 // VT_EMPTY it is successfully converted to the empty string.
354 wchar_t read_app_id[MAX_PATH] = {0};
355 PropVariantToString(pv_app_id, read_app_id, MAX_PATH);
356 if((properties.bitfield & ShortcutProperties::PROPERTIES_APP_ID) != 0 &&
357 string16(read_app_id) != properties.app_id) {
358 return VERIFY_SHORTCUT_FAILURE_APP_ID;
359 }
360
361 // Note, as mentioned on MSDN at http://goo.gl/9mBHB, if |pv_dual_mode| is a
362 // VT_EMPTY it is successfully converted to false.
363 BOOL read_dual_mode;
364 PropVariantToBoolean(pv_dual_mode, &read_dual_mode);
365 if((properties.bitfield & ShortcutProperties::PROPERTIES_DUAL_MODE) != 0 &&
366 static_cast<bool>(read_dual_mode) != properties.dual_mode) {
367 return VERIFY_SHORTCUT_FAILURE_DUAL_MODE;
368 }
369 }
370
371 return VERIFY_SHORTCUT_SUCCESS;
372 }
373
271 // Checks if the volume supports Alternate Data Streams. This is required for 374 // Checks if the volume supports Alternate Data Streams. This is required for
272 // the Zone Identifier implementation. 375 // the Zone Identifier implementation.
273 bool VolumeSupportsADS(const FilePath& path) { 376 bool VolumeSupportsADS(const FilePath& path) {
274 wchar_t drive[MAX_PATH] = {0}; 377 wchar_t drive[MAX_PATH] = {0};
275 wcscpy_s(drive, MAX_PATH, path.value().c_str()); 378 wcscpy_s(drive, MAX_PATH, path.value().c_str());
276 379
277 if (!PathStripToRootW(drive)) 380 if (!PathStripToRootW(drive))
278 return false; 381 return false;
279 382
280 DWORD fs_flags = 0; 383 DWORD fs_flags = 0;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 DCHECK(info_ != NULL); 437 DCHECK(info_ != NULL);
335 DCHECK_NE(0u, length_); 438 DCHECK_NE(0u, length_);
336 } 439 }
337 440
338 PermissionRestorer::~PermissionRestorer() { 441 PermissionRestorer::~PermissionRestorer() {
339 if (!RestorePermissionInfo(path_, info_, length_)) 442 if (!RestorePermissionInfo(path_, info_, length_))
340 NOTREACHED(); 443 NOTREACHED();
341 } 444 }
342 445
343 } // namespace file_util 446 } // namespace file_util
OLDNEW
« base/file_util_win.cc ('K') | « base/test/test_file_util.h ('k') | base/win/win_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698