kcott has asked for the wisdom of the Perl Monks concerning the following question:
I'm extending an application for $work and am seeking some advice on testing unexpected I/O failures.
The application receives a lot of parameters and I perform sanity checks on these. There are three parameters of interest with respect to this SoPW, in brief:
- directory: check it exists; is a directory; has appropriate permissions.
- input file: check it exists; is a normal file; isn't empty; can be read.
- output file: this really only needed a name format check; it doesn't need to exist and it's overwritten if it does (which can occur often and would generally be the normal case).
The application works fine if good parameters are supplied. There's a total of nine (file-related) sanity checks; I've successfully tested all of these with bad parameters.
The actual I/O is very straightforward; e.g.
open my $in_fh, '<', $input_file_path ... open my $out_fh, '>', $output_file_path ...
The error messages on failure are similarly standard:
Can't open $input_file_path for reading: $! Can't open $output_file_path for writing: $!
Given the sanity checks, I/O failure would be unexpected but still possible; for instance, between the sanity checks and an open call, a file could be deleted or renamed, it's permissions changed, a hardware failure could occur, and so on.
I managed to test the write failure by running normally; manually removing the write permissions of the output file;
and then running again with the same parameters.
Yes, I remembered to put the write permissions back after this test. :-)
So that just leaves me with testing the read failure; unfortunately, I can't think of a way to do that. Any ideas would be greatly appreciated.
While I do like to test everything, if this last test can't be done it's not a huge problem. The code is very straightforward (I've probably written similar code thousands of times in the past); the syntax is fine (perl -c); and, I know it works with good parameters.
— Ken