Definition
A negative control is a case in a test or experiment whose correct result is known in advance to be negative: the effect under study must not appear. In a laboratory it is a sample that cannot contain what the test detects, such as plain water; a detection there reveals contamination or a faulty instrument. In software testing it is usually an input that the program must refuse, such as a request with a wrong authentication §1.6 tag, the short value that proves the sender holds a secret key.
The idea
A protection is a check that stops some effect. Take a device that changes a setting only when the request carries a correct authentication tag. A test of that protection sends a request with a wrong tag and asserts that the device refused it. When the test passes, the tester wants to conclude that the protection works. That conclusion is sound only if the test would have failed had the protection been missing. A test that passes with and without the protection says nothing about the protection.
Laboratory science gave the idea its name. An experiment that looks for an effect also runs a case in which the effect cannot be present. A report of the effect there exposes false positives §1.22, reports of an effect that is not there. Software uses the name in two ways. For a bug detector, a negative control is a program known to be free of the bug, on which the detector must report nothing. For a protection, it is an invalid input whose forbidden effect must stay absent.
How it works
A negative control for a protection is built and checked in five steps.
Name the forbidden effect and every place it could show. For a request that changes a setting, the effect is a changed value, which could show in the reply, in memory, in persistent state §8.1 (data that survives a restart) and in later behavior.
Start from a valid input. Build an input that passes every check and has the effect. Running it is the positive control §10.30: it shows that the effect happens and that the test can see it.
Change exactly one factor. Alter only the property the protection checks, for example one bit of the tag. Everything else stays valid, so the input passes the other checks and reaches the protection.
Assert that the effect is absent. Record the protected state, send the input, and check that the reply reports a refusal and that the state is unchanged everywhere named in step 1. Checking only that the program still runs, or that some error came back, is too weak.
Validate the control. Run the same control against a build in which only the protection is disabled; the control must now fail. Also confirm that the refusal in the normal build came from the protection under test, for example from a reason code in the reply.
Step 5 is a positive control for the test oracle §10.5, the rule that decides whether a test passes: the forbidden effect is made to happen on purpose, and the oracle must report it. Mutation testing §10.45 applies the same idea on a large scale: it plants many small deliberate faults in a program and counts how many of them the tests detect.
Teams often keep negative controls as stored inputs together with their expected outcomes. Such inputs are called golden examples or golden fixtures, a fixture being the fixed data a test runs on. Golden-invalid fixtures are negative controls and golden-valid fixtures are positive controls. Kept in a regression test §10.4 suite, they run again after every change to the program.
Worked example
A device holds a setting whose current value is 5. A request to change the setting carries the new value, a length field that gives the size of the request, and an 8-byte authentication tag: a value computed from the request and a secret key with a message authentication code §5.7, so that only a key holder can produce the correct tag. A valid request to set the value to 9 has a tag whose last byte is 0x3c. Three negative controls are built from it.
N1: the last tag byte is changed to
0x3d. In binary,0x3cis00111100and0x3dis00111101, so exactly one bit differs. The oracle checks only that the device did not crash.N2: the same wrong tag, and the length field is also wrong. The oracle checks that the reply is a refusal and that the value is still 5.
N3: the same wrong tag and a correct length field. The oracle checks that the reply is a refusal and that the value is still 5.
Each control runs against two builds: the normal build, and a test build in which only the tag check is disabled.
| Control | Normal build | Tag check disabled | Evidence? |
|---|---|---|---|
| N1 | passes | passes (value becomes 9) | no |
| N2 | passes | passes (length check refuses) | no |
| N3 | passes | fails (value becomes 9) | yes |
N1 passes in both builds because a device that wrongly accepts the request does not crash either: the oracle checks survival, not the effect. N2 passes in both builds because the length check refuses the request before the tag check runs, so N2 tests the length check, not the tag check. Only N3 gives different results in the two builds, so only N3 is evidence that the tag check works.
The valid request is the positive control: in the normal build it must change the value from 5 to 9. A build that refused every request would pass N3 but fail this positive control, which is why the two belong together.
Stated precisely
Let be the program with the protection, and let be the same program with only the protection disabled. Let be the negative-control input and a valid input that must have the effect. Let be the oracle, which returns pass or fail for a program and an input; for it passes when the effect is absent, for when the effect is present. The negative control is evidence that the protection works when all three of the following hold.
: with the protection, is refused and the effect is absent.
: without the protection, the effect occurs and the oracle reports it.
: with the protection, the valid input has its effect, so does not refuse everything.
Condition 2 rests on three assumptions. First, differs from only in the protection, so a different result has no other cause; a disabled build compiled with other options can fail for unrelated reasons. Second, the oracle observes every place where the effect could show, including persistent state and later behavior. Third, the runs are repeatable, so a difference between and is not chance. In this entry, a test passes when its assertion holds; for a negative control that means the program refused.
Limits and common mistakes
One negative control tests one way of being invalid. A correct refusal of a tag with one flipped bit says nothing about a shortened tag, a missing tag field, or a valid request that was recorded earlier and is sent again, which is a replay attack §1.26. Each way of being invalid needs its own control.
The absence of the effect must be checked where the effect would show. A program can send a refusal and still have written the new value to storage, where the value takes effect after the next restart. An oracle that reads only the reply misses that.
The words are used inconsistently. Some testers call a test with an invalid input a negative test and keep negative control for the laboratory meaning, and “the negative control passed” can be misread as “the invalid input got through”. A report should state the input, the expected observation and what pass means.
Questions answered
Would disabling the protection make the negative control pass incorrectly?
A well-built negative control does not pass when the protection is disabled: it fails, and seeing it fail is how one knows that the control tests the protection. A negative control is a test input whose correct outcome is known to be negative, such as a request with a wrong authentication tag, which the program must refuse while the protected state stays unchanged. To find out whether a given control passes incorrectly, run it against a build in which only the protection is disabled. If the control now fails because the forbidden effect occurs and the test sees it, the control is evidence. If it still passes, it says nothing about the protection, usually for one of two reasons.
The test oracle, the rule that decides pass or fail, checks something that holds either way, such as the program not crashing or some error being returned, instead of checking that the protected state is unchanged.
The input is refused by an earlier, unrelated check, such as a format or length check, and never reaches the protection.
Repair such a control by changing only the property the protection checks and by asserting that the effect is absent everywhere it could show, including stored state. Pair it with a positive control, a valid input that must be accepted, so that a program that refuses everything cannot pass.
Related entries: Positive control (§10.30), Test oracle (§10.5), Mutation testing (§10.45), Authorization (§1.7), Fault injection (§10.28).