Validators { public static void validateVersion(int candidate, int maxExpected) throws GeneralSecurityException { if (candidate < 0 || candidate > maxExpected) { throw new GeneralSecurityException( String.format( "key has version %d; only keys with version in range [0..%d] are supported", candidate, maxExpected)); } } private  Validators(); static void validateTypeUrl(String typeUrl); static void validateAesKeySize(int sizeInBytes); static void validateVersion(int candidate, int maxExpected); static void validateSignatureHash(HashType hash); static void validateRsaModulusSize(int modulusSize); static void validateRsaPublicExponent(
      BigInteger publicExponent); static void validateNotExists(File f); static void validateExists(File f); static String validateKmsKeyUriAndRemovePrefix(String expectedPrefix, String kmsKeyUri); static void validateCryptoKeyUri(String kmsKeyUri);  }
@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) { 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) { count++; TestUtil.assertExceptionContains(e, "version"); } } } } } assertEquals(maxVersion * (maxVersion + 1) / 2, count); assertEquals(maxVersion * (3 * maxVersion + 2), countNegative); }