CA-142595: Fix, unit tests and refactoring

This pull request includes:
-The fix from @wangyanbn (https://github.com/xapi-project/sm/pull/203)
-The testing framework from @matelakat (https://github.com/xapi-project/sm/pull/208)
-The unit tests for the fix, which are built on top of the testing framework
-Some additional refactoring, to prevent side effects when applying the fix

Thanks,
Flavien