Definition
Boundary-value testing, also called boundary-value analysis, is a test-design method that places test inputs at the limits where a program’s specified behavior changes. A limit is the largest or smallest value that still gets one behavior, for example the largest length that is accepted. The tests use the value at each limit and the values immediately below and above it. The method targets off-by-one errors, in which a comparison or a count is wrong by exactly one, and errors at the smallest and largest values a variable can hold.
The idea
A specification says that a message may carry at most 16 bytes of data. A programmer writes this rule as a comparison of the length, here named len, with 16, and a comparison is easy to get wrong by one. The check len < 16 instead of len <= 16 refuses the legal length 16. The check len <= 17 accepts the illegal length 17, and the copy that follows writes one byte past the end of a 16-byte buffer. A test with length 5 or length 100 passes for all three checks, because the three agree everywhere except at 16 and 17. Errors of this kind sit exactly at the limits, so that is where the tests go.
The limits come from the specification and from the way values are stored, not from round numbers: limits on one value, limits that depend on another value, and the edges of each integer type, where arithmetic wraps around.
How it works
List every limit. Go through the specification for each input and each piece of stored state: lengths, counts, offsets, value ranges, table sizes, numbers of retries and timeouts §9.34. Add the edges of each field’s representation: 0, 1, the largest value and the largest value minus 1.
Write down the specified behavior on each side. Work out each expected result from the specification, not from the program’s own output, which would make the test check nothing.
Choose the values. For an upper limit on whole numbers, two-value testing uses and the closest value on the other side, . Three-value testing uses , and . A lower limit is handled the same way, mirrored.
Compute limits that depend on other inputs. When the rule says that offset plus length must not exceed a size, the largest legal length is the size minus the offset. Compute that limit for several offsets, including 0 and the size itself, and test around each.
Build each value directly. Write the over-limit value into its field as a literal. Computing it with arithmetic in the same integer type can wrap: in a 16-bit field, is stored as 0, so the test would send 0 instead of an over-limit value.
Check the whole outcome. For a refused value, check that nothing changed: no partial copy, no updated counter. For the value at the limit, check that it is accepted, so that a repair that became too strict is caught.
Boundary-value testing complements equivalence partitioning §10.32, which divides the inputs into classes that the specification treats alike. The limits are the edges between those classes.
Worked example
A buffer holds 16 bytes, and the specification accepts the lengths 0 to 16. Four versions of the check are run on the boundary values 15, 16 and 17 and on the middle value 5. Bold marks a result that differs from the specification.
| Check | 5 | 15 | 16 | 17 |
|---|---|---|---|---|
| specification | accept | accept | accept | refuse |
len <= 16 |
accept | accept | accept | refuse |
len < 16 |
accept | accept | refuse | refuse |
len <= 17 |
accept | accept | accept | accept |
len == 16 |
refuse | refuse | accept | refuse |
The value 16 exposes len < 16, and the value 17 exposes len <= 17; these two values form the two-value test of the upper limit. The middle value 5 exposes neither. The fourth check, len == 16, is a mistyped operator. Among the boundary values only 15, the third value, exposes it. The lower limit is tested with 0 and 1, since an unsigned length has no value below 0. If the length field is one byte wide, its largest value, 255, is tested too.
A limit that depends on another input. An object is 16 bytes long. A request asks to read bytes starting at offset , and the specification accepts it when in ordinary arithmetic. At offset 14, bytes remain, so the tests use the lengths 1, 2 and 3: 1 and 2 must be accepted and 3 refused, although 3 would pass any check on the length alone.
The edge of the integer type. Suppose the program computes in 32-bit unsigned arithmetic, which keeps only the remainder after division by , and then compares the result with 16. A boundary test with the largest 32-bit length, , and gives , which is stored as . Since , the program accepts a request to read about 4 GiB from a 16-byte object. This is unsigned wraparound §4.16. The overflow-safe range check §4.17 first tests and then ; it refuses the request because .
Stated precisely
Let the input be a whole number , and let the specification assign every value of one behavior from a finite set, such as accept and refuse. A boundary is a pair of neighboring values and to which the specification assigns different behaviors. For an upper limit of the accepted values, . Two-value boundary testing runs and for every limit; three-value boundary testing also runs .
The guarantee for one comparison: suppose the specification accepts exactly when , and the program accepts exactly when for some constant . A strict comparison is the same as for whole numbers, so it is covered too. If , the program refuses , which must be accepted. If , the program accepts , which must be refused. So the two values and expose every wrong constant , however far it is from . The guarantee assumes that the decision really is one comparison of against a constant. A decision of another shape, such as an equality test or a special case inside the range, can pass both values; the third value exposes some of those shapes, such as the equality test above, but not all.
When the limit depends on other inputs, as in for an object of bytes, the limit on is for each offset from 0 to . The tests sample several offsets and include the largest values that the types of and can hold.
Limits and common mistakes
Boundary tests check values near the limits. They do not show that the values between the limits behave correctly. A special case inside a range, such as a different code path for lengths above 8, is invisible to them.
The tests find only the limits that someone listed, and limits inside an implementation are easy to miss. The hash function SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output) processes its input in 64-byte blocks and appends at least 9 bytes of padding, so a 55-byte message fits in one block and a 56-byte message needs two. An implementation can fail at 56 bytes although no specified limit is near.
Testing each limit on its own does not test combinations. A length at its largest value together with an offset at its largest value can fail even when each passes alone.
Inputs without a natural order, such as message types or option names, have no value just below or just above. They need equivalence partitioning instead.
Limits in time count as well. An event that arrives exactly at a timeout, one clock tick before it and one tick after it are three boundary tests.
Questions answered
Which values lie just below, at, and above every admitted limit?
For an admitted limit , meaning the largest value that the specification still accepts, the values are , and , counted in the smallest step the input’s type allows: and must be accepted and refused. For a lower limit , the smallest value still accepted, and must be accepted and refused, if the type can hold . For a maximum length of 16 bytes the values are 15, 16 and 17.
Find the limits in the specification and in the way values are stored: every length, count, offset, range and timeout; the value 0; and the largest value each field can hold, such as 4,294,967,295 for a 32-bit unsigned field, where a sum wraps around to a small number. Where a limit depends on another value, compute it: in a 16-byte object at offset 14, the largest legal length is , so the values are the lengths 1, 2 and 3. For each refused value, check that nothing changed, such as a partial copy. Keep the test that the value at the limit is accepted, so that a repair that became too strict is caught.
Related entries: Equivalence partitioning (§10.32), Overflow-safe range check (§4.17), Integer promotion and conversion (§4.18), Deadline versus timeout (§9.34), Unsigned modular arithmetic (§4.16).