2

How do you unit/integration test code that requires a different privilege level than exists in your continuous integration environment?

In my non-root, CCRB-driven build environment, I've got some utility functions that assume privileges that don't hold in my automated build environment: either root privileges or special accounts and groups. (For example, one function changes UID/GID and supplementary groups to a specified account, changes root and current working directory, and divorces from any controlling terminal.)

We could run the tests by hand, of course, but then we might forget to run them.

How have others tackled this issue?

pilcrow
  • 56,591
  • 13
  • 94
  • 135

1 Answers1

0

I would try to factor out the security management code behind a mockable interface, so that in unit tests I can provide fake privileges however I want.

This way it would be possible to test both that barring the required privileges the function fails, and that with the privileges granted it does what it is supposed to do.

Without more concrete details it is hard to say more.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • Can you clarify, for example, how you'd test a function, intended to be called as root, that changes UID/GID, working directory, and filesystem root? – pilcrow Dec 13 '10 at 20:10
  • @pilcrow, as I said, I would try to hide all these functionalitites behind mockable interface(s), with two implementations: a real one to be used in production, and a mock implementation intended for unit testing, which can be preconfigured to return the needed value(s) and allows me to sense method calls and parameter values. Thus in my unit test environment I could "verify" whatever privileges I need to, and I could sense that the proper methods are called with the right parameters, without the side effect of actually changing system state. – Péter Török Dec 14 '10 at 14:18
  • éter, it is the "security management code" itself that I wish to test. I don't want to mock verify that I have shed mock privileges, I want to verify that I do shed actual privileges. – pilcrow Dec 14 '10 at 18:04