Mocking and Stubbing in TDD for Security Testing Quiz

Assess your understanding of mocking and stubbing techniques used in test-driven development (TDD) with a focus on their applications in security testing. This quiz explores best practices, scenarios, and key differences crucial for writing robust and secure automated tests.

  1. Distinguishing Mocking and Stubbing

    In the context of test-driven development for security testing, which statement best defines the difference between mocking and stubbing?

    1. Stubbing provides predefined responses, while mocking verifies how dependencies are used.
    2. Mocking replaces all code logic, while stubbing creates real network traffic.
    3. Stubbing records the sequence of function calls, while mocking allows data modification.
    4. Mocking is only used for performance testing, while stubbing is only used for security testing.

    Explanation: Stubbing is primarily used to supply fixed responses for dependencies, isolating the component under test. Mocking, on the other hand, goes further by also checking interactions, such as whether certain methods were called, and with what parameters. The second option is incorrect because mocking does not replace all logic, nor does stubbing generate real network traffic. The third option confuses roles; stubbing does not record function calls, and mocking is not mainly for modification of data. The last choice wrongly restricts mocking and stubbing to specific testing types, which is inaccurate.

  2. Application of Stubs in Security Testing

    Which scenario best illustrates the use of a stub in a security-focused test-driven development practice?

    1. Replacing a database call with a fixed return value to simulate user authentication responses.
    2. Verifying that a logging function is called after an authentication failure.
    3. Measuring the time it takes for a security alert to be triggered under heavy load.
    4. Intercepting and modifying incoming HTTP requests to test security headers.

    Explanation: Stubs are typically used to isolate code by providing predetermined outputs, such as simulating a database's response for authentication. The correct option represents this use. The second option describes mocking, where function calls are verified. The third option relates to performance rather than stubbing. The fourth option is closer to intercepting or modifying, which is outside the typical scope of simple stubbing.

  3. Benefit of Using Mocks in Security TDD

    Why is mocking an external dependency particularly important during security regression testing in TDD?

    1. It ensures the security tests do not require external services and can validate interaction patterns.
    2. Mocks are used to increase test execution speed by using actual production data.
    3. Mocking enables unencrypted communication for easier debugging.
    4. It allows tests to randomly change network addresses for threat simulations.

    Explanation: Mocking lets you simulate external services, avoiding possible side effects and ensuring tests are repeatable and isolated, which is essential for reliable security regression tests. The second option is incorrect because using actual production data can be insecure, and mocks do not necessarily improve speed that way. The third option mentions unencrypted communication, which is unrelated and potentially risky. The fourth option confuses the purpose of mocking with network simulation tools.

  4. Incorrect Use of Stubs in Security TDD

    Which situation would be an incorrect use of a stub when developing security-focused unit tests?

    1. Using a stub to simulate harmless exceptions that never occur in production.
    2. Stub responses are programmed to return fixed error codes for access denial tests.
    3. A stub returns a specific user role for privilege checking logic tests.
    4. Stubs are used to avoid making real file writes during data sanitization tests.

    Explanation: Stubs should mimic realistic interactions; simulating exceptions that cannot happen in production introduces irrelevant cases that don't improve test value. The second, third, and fourth options represent correct uses, since returning fixed errors, user roles, or avoiding real file system changes supports realistic, reliable, and secure testing practices.

  5. Choosing Between Stubs and Mocks

    During TDD for a security-sensitive API endpoint, if you need to check whether the security logging service was called after a denial of access, which approach is most appropriate?

    1. Mocking the logging service to verify it received the correct method call.
    2. Stubbing the user validation service to always grant access.
    3. Using a stub to return random data from the API endpoint.
    4. Mocking the data store to return HTTP 500 errors.

    Explanation: Mocking allows you to verify if and how a dependency (like a logging service) was called, which is essential for ensuring critical actions like logging are not skipped during security events. Stubbing the validation service as in the second option would not help verify logging interactions. Returning random data as in option three can make tests unreliable and not focused on the logging aspect. Mocking the data store to return errors may test error handling, but not whether the logging service was triggered as required.