简体   繁体   中英

Simulating file errors (e.g. ERROR_ACCESS_DENIED) on Windows

For testing and development purposes, it would be nice to somehow simulate (spurious) file access errors to local files. For example, even if an application has correctly opened a file with the appropriate restrictive sharing flags, it still can happen that an attempt to access the file (through any of the Win32 API functions or your favourite framework, which internally will just call any of the Win32 API functions) can fail.

The only example I ever was able to track down was the virus scanner on a machine, but I guess there could be other reasons. (In this question's comment, Luke mentions something about "File system filter drivers".)

FWIW, I know of a few possibilities to "simulate" file problems, that I do not consider good solutions, either because they require to much manual work or because they don't fit for every app/file:

  • Place a file on a network drive or removable storage device - that way you can just mess up the device (unplug, disk-full, ...).
  • Open the application process in Process Explorer and close the handle of the file you want to test.

So the question really is if there are any ((semi)automated) tools that can mess up file access (on an NTFS drive) even though an application has already opened a file with appropriate (for the app) sharing flags.

Holodeck purports to allow Win32 API hooking, which would enable you to manipulate return codes as needed for Fault Injection.

If your API set of interest is well-defined, you could probably do this yourself using the Import Address Table approach described here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM