Fix minor issues and add tests for key handling

There are still issues with this code (starting with the name), but this is a start.

Tests are not built automatically, at least for now.
If you add `CONFIG += testcase`, I think this will also allow `make check` to work.