BucketNameValidator implements ConstraintValidator<BucketName, String> { @Override public boolean isValid(String jarBucketName, final ConstraintValidatorContext context) { if (jarBucketName == null) { return true; } if (!isValidStartAndEnd(jarBucketName) || jarBucketName.startsWith("goog") || !jarBucketName.matches("^[a-z0-9\\.-]+$")) { return false; } if (jarBucketName.indexOf(".") == -1) { if (!isValidLength(jarBucketName)) { return false; } } else { if (jarBucketName.length() > MAX_TOTAL_LENGTH || InetAddresses.isInetAddress(jarBucketName)) { return false; } String[] parts = jarBucketName.split("\\."); for (String part : parts) { if (part.length() > MAX_SEGMENT_LENGTH) { return false; } } } return true; }  @Override void initialize(final BucketName target); @Override boolean isValid(String jarBucketName, final ConstraintValidatorContext context);  }
@Test public void testEmptyBucketName() { assertTrue(validator.isValid(null, null)); }
@Test public void testStartAndEnd() { List<String> validBucketNameList = ImmutableList.of( "abc1", "1abc", "123"); for (String validBucketName : validBucketNameList) { assertTrue(validator.isValid(validBucketName, null)); } List<String> invalidBucketNameList = ImmutableList.of( ".abc1", "$abc", "-123."); for (String invalidBucketName : invalidBucketNameList) { assertFalse(validator.isValid(invalidBucketName, null)); } }
@Test public void testPrefix() { assertTrue(validator.isValid("gooabc11", null)); assertFalse(validator.isValid("googabc11", null)); }
@Test public void testBucketNameAllowedCharSet() { List<String> validBucketNameList = ImmutableList.of( "abc123", "ab.c", "a-c", "a--.1.bc"); for (String validBucketName : validBucketNameList) { assertTrue(validator.isValid(validBucketName, null)); } List<String> invalidBucketNameList = ImmutableList.of( "%%^", "aBc", "1@(2.3"); for (String invalidBucketName : invalidBucketNameList) { assertFalse(validator.isValid(invalidBucketName, null)); } }
@Test public void testDottedIpAddress() { assertTrue(validator.isValid("192.168.5.com", null)); assertFalse(validator.isValid("192.168.5.4", null)); }
@Test public void testLength() { assertTrue(validator.isValid("abcabc", null)); assertFalse(validator.isValid("a2", null)); StringBuilder bucketNameTooLong = new StringBuilder(64); for (int i = 0; i < 64; i++) { bucketNameTooLong.append('a'); } assertFalse(validator.isValid(bucketNameTooLong.toString(), null)); }