如何在 CRT 中模拟文件读取错误
使用VS2008,我们想模拟一个大小为X的文件,但在XY字节处读取失败,以便我们得到错误指示。
有人知道如何在 Windows 上执行此操作吗?看起来linux有一个解决方案,但我真的想不出在windows上做到这一点的方法。我们有多个开发人员、多台机器和 cppunit 测试框架,所以我想要一个纯软件设计。
我正在尝试模拟实际的 CRT 故障,以便我可以测试处理故障的代码。
Using VS2008, we would like to simulate a file that has a size of X, but that has a read failure at X-Y bytes, so that we get an error indication.
Anyone have an idea of how to do this on windows? Looks like there is a solution for linux, but I can't really come up with a way to do this on windows. We have multiple developers, multiple machines, and cppunit testing framework, so I want a software only design.
I'm trying to simulate the actual CRT failing, so I can test the code that is dealing with the failure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将文件 I/O 函数包装在一个类中;覆盖测试派生类中的那些;用假的或模拟的来模拟失败。
Wrap the file I/O functions in a class; override those in a testing derived class; simulate failure with a fake or mock.
我不确定这是否可行,但您可以尝试创建一个文件,然后在 xy 字节处截断它,在该位置创建一个带有标头的新文件。但我不知道你会怎么做。另一个想法是打开文件,查找之前的字节,然后更改权限,这样您就无法再读取该文件 - 但如果有文件锁,您就必须绕过它。
不过,我对 Windows 文件系统不是很熟悉,所以这些只是猜测。
I'm not sure if this would work, but you could try creating a file and then truncating it at x-y bytes, creating a new file w/header at that location. I don't know how you would do that, though. Another idea is to open the file, seek to the byte before, then change the permissions so you can no longer read the file - though if there's a file lock in place you'd have to get around that.
I'm not super familiar with the Windows File systems though, so these are just guesses.