Use wrappers around read/write

This way it is safe to assume that all data was read/written.
Fix infinite loop when test crashes