Add a test to cover the behavior described in https://github.com/bitcoin/bitcoin/issues/2039, in preparation for fixing it.
The functional test was massage to be slightly less bad code: fewer levels of `try-except` wrapping, deduplication of the platform specific code (only the command name changes based on the OS)
This is a backport of core#28545, core#27850 and core#28660
Depends on D18969