How to simulate file I/O errors for testing purposes

There are few techniques which can help testing various cases related to file I/O. This how to assumes that you can inject test file (or directory) path into your tests.

Empty file
Reading from dev/null always results in end of file: http://man7.org/linux/man-pages/man4/null.4.html

No space left on device
Writing to /dev/full always results in ENOSPC error: http://man7.org/linux/man-pages/man4/full.4.html

Unwritable file/directory
As a non-root user: chmod -w <path>
As a root: chattr +i <path>

Unreadable file/directory
As a non-root user: chmod -r <path>
As a root: use su to become a regular user. For example to run Gradle tests with Script step:

useradd -m bitrise
chown -R bitrise .
chown -R bitrise ${ANDROID_HOME}
su -c './gradlew check --scan' bitrise
2 Likes

Perfect, thank you for the awesome #how-to @koral! :slight_smile: