PR #4161 is stalled. Tried to cherry pick the code from there, but that
led to conflicts, manually copying over while taking into account the
comments on that PR. Use that code and extend the error checking, don't
modify existing tests and make the badwriter test simpler.
Closes: #4161
Signed-off-by: Miek Gieben <miek@miek.nl>
add tests
Signed-off-by: Miek Gieben <miek@miek.nl>