Section 10.31

Boundary-value testing

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

  1. 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.

  2. 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.

  3. Choose the values. For an upper limit LL on whole numbers, two-value testing uses LL and the closest value on the other side, L+1L + 1. Three-value testing uses L−1L - 1, LL and L+1L + 1. A lower limit is handled the same way, mirrored.

  4. 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.

  5. 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, 65535+165535 + 1 is stored as 0, so the test would send 0 instead of an over-limit value.

  6. 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 nn bytes starting at offset oo, and the specification accepts it when o+n≤16o + n \le 16 in ordinary arithmetic. At offset 14, 16−14=216 - 14 = 2 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 o+no + n in 32-bit unsigned arithmetic, which keeps only the remainder after division by 232=4,294,967,2962^{32} = 4{,}294{,}967{,}296, and then compares the result with 16. A boundary test with the largest 32-bit length, n=4,294,967,295n = 4{,}294{,}967{,}295, and o=14o = 14 gives 14+4,294,967,295=4,294,967,30914 + 4{,}294{,}967{,}295 = 4{,}294{,}967{,}309, which is stored as 4,294,967,309−4,294,967,296=134{,}294{,}967{,}309 - 4{,}294{,}967{,}296 = 13. Since 13≤1613 \le 16, 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 o≤16o \le 16 and then n≤16−on \le 16 - o; it refuses the request because 4,294,967,295>24{,}294{,}967{,}295 > 2.

Stated precisely

Let the input be a whole number xx, and let the specification assign every value of xx one behavior from a finite set, such as accept and refuse. A boundary is a pair of neighboring values bb and b+1b + 1 to which the specification assigns different behaviors. For an upper limit LL of the accepted values, b=Lb = L. Two-value boundary testing runs LL and L+1L + 1 for every limit; three-value boundary testing also runs L−1L - 1.

The guarantee for one comparison: suppose the specification accepts exactly when x≤Lx \le L, and the program accepts exactly when x≤cx \le c for some constant cc. A strict comparison x<cx < c is the same as x≤c−1x \le c - 1 for whole numbers, so it is covered too. If c<Lc < L, the program refuses LL, which must be accepted. If c>Lc > L, the program accepts L+1L + 1, which must be refused. So the two values LL and L+1L + 1 expose every wrong constant cc, however far it is from LL. The guarantee assumes that the decision really is one comparison of xx 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 L−1L - 1 exposes some of those shapes, such as the equality test above, but not all.

When the limit depends on other inputs, as in o+n≤So + n \le S for an object of SS bytes, the limit on nn is S−oS - o for each offset oo from 0 to SS. The tests sample several offsets and include the largest values that the types of oo and nn 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 LL, meaning the largest value that the specification still accepts, the values are L−1L - 1, LL and L+1L + 1, counted in the smallest step the input’s type allows: L−1L - 1 and LL must be accepted and L+1L + 1 refused. For a lower limit MM, the smallest value still accepted, MM and M+1M + 1 must be accepted and M−1M - 1 refused, if the type can hold M−1M - 1. 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 16−14=216 - 14 = 2, 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).