test: move common functions into test lib

Moved duplicated function definitions to the test-lib.sh file, so that maintenance is lessened.