@Test
public void testValidateVersion() throws Exception {

  int maxVersion = 1;
  int count = 0;
  int countNegative = 0;
  for (int maxExpected = -maxVersion; maxExpected <= maxVersion; maxExpected++) {
    for (int candidate = -maxVersion; candidate <= maxVersion; candidate++) {
      if (candidate < 0 || maxExpected < 0) {
        try {
          Validators.validateVersion(candidate, maxExpected);
          fail("Negative version parameters, should have thrown exception.");
        } catch (GeneralSecurityException e) {
          // Expected.
          countNegative++;
          TestUtil.assertExceptionContains(e, "version");
        }
      } else {
        if (candidate <= maxExpected) {
          Validators.validateVersion(candidate, maxExpected);
        } else {
          try {
            Validators.validateVersion(candidate, maxExpected);
            fail("Invalid key version, should have thrown exception.");
          } catch (GeneralSecurityException e) {
            // Expected.
            count++;
            TestUtil.assertExceptionContains(e, "version");
          }
        }
      }
    }
  }
  assertEquals(maxVersion * (maxVersion + 1) / 2, count);
  // countNegative == (2*maxVersion + 1)^2 - (maxVersion+1^2)
  assertEquals(maxVersion * (3 * maxVersion + 2), countNegative);
}