Fix a test that assumed the system encoding was utf8

Also fixes a few resource leaks reported in this test file (unclosed file handles)