Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a read error

Tags:

unix

testing

It's simple to generate a write error in a test suite by writing to /dev/full. Is there a good technique to generate a read error? I'm currently using LD_PRELOAD to override read but that seems too complicated and non-portable (not that /dev/full is portable...).

like image 334
William Pursell Avatar asked Jun 27 '12 14:06

William Pursell


3 Answers

Besides reading from a directory (as mentioned in a previous answer) you can try to read /proc/self/mem to get an error (this should get you an EIO on Linux). For an explanation, please see: https://unix.stackexchange.com/a/6302

like image 78
rubasov Avatar answered Oct 26 '22 06:10

rubasov


An approach that works on all major unices would be to implement a small FUSE filesystem. EIO is the default error code when your userspace filesystem driver does something wrong, so it's easy to achieve. Both the Perl and Python bindings come with examples to get started, you can quickly write a filesystem that mostly mirrors existing files but injects an EIO in carefully chosen places.

There's an existing such filesystem: petardfs (article), I don't know how well it works out of the box.

like image 24
Gilles 'SO- stop being evil' Avatar answered Oct 26 '22 04:10

Gilles 'SO- stop being evil'


According to the (OS X) read(2) manpage, read(2) will generate an error if "[a]n attempt is made to read a directory." You could therefore open(2) a directory (make sure the prot doesn't permit writing, or this'll throw an error) and then try to read from it. That looks like the only error listed there which could happen in 'normal' circumstances (ie without doing something like deliberately breaking a FILE* struct).

I'm presuming you're talking about read(2) errors in C or something like it, but even in a higher-level language, you might be able to open a directory and try to read from it (though I just tried it with Python, and it's too smart to let you open the directory...)

like image 2
Norman Gray Avatar answered Oct 26 '22 05:10

Norman Gray