Check entire set of value_meta key/value pairs for length

Change-Id: Iba7b96d603ac218d53ba1bb3827ed6f23b4be75b
Closes-Bug: #1501929
This commit is contained in:
bklei 2015-10-13 08:46:00 -06:00
parent f4d1ce149d
commit e7f5ca3a80
2 changed files with 10 additions and 11 deletions

View File

@ -90,23 +90,21 @@ public final class ValueMetaValidation {
throw Exceptions.unprocessableEntity("valueMeta name %s must be %d characters or less",
name, VALUE_META_NAME_MAX_LENGTH);
}
verifyValueMetaStringLength(name, value);
}
verifyValueMetaStringLength(valueMetas);
}
private static void verifyValueMetaStringLength(String name, String value) {
private static void verifyValueMetaStringLength(Map<String, String> valueMetas) {
Map<String, String> tmpMap = new HashMap<String, String>();
tmpMap.put(name, value);
try {
String valueMetaString = objectMapper.writeValueAsString(tmpMap);
String valueMetaString = objectMapper.writeValueAsString(valueMetas);
if (valueMetaString.length() > VALUE_META_VALUE_MAX_LENGTH) {
throw Exceptions.unprocessableEntity("valueMeta name value combination %s must be %d characters or less",
throw Exceptions.unprocessableEntity("valueMeta name value combinations %s must be %d characters or less",
valueMetaString, VALUE_META_VALUE_MAX_LENGTH);
}
} catch (JsonProcessingException e) {
throw Exceptions.unprocessableEntity("Failed to serialize valueMeta %s", tmpMap);
throw Exceptions.unprocessableEntity("Failed to serialize valueMeta combinations %s", valueMetas);
}
}
}

View File

@ -54,12 +54,13 @@ public class ValueMetaValidationTest {
final Map<String, String> valueMeta = new HashMap<String, String>();
for (int i = 0; i < 16; i++) {
//
// Test with max value less length of name and 7 extra chars:
// All 16 name/value pairs (converted to json) must fit in 2048
// chars. Test that we can fit 1/16th of 2048 in each pair (128 chars):
//
// {"name":"value"}
// ^^ ^^^ ^^ <-- extra chars
// {"name":"value"}, ...
// ^^ ^^^ ^^^ <-- extra chars (8 per pair)
//
valueMeta.put(makeString(i, 255), makeString(i, (2048 - (255+7))));
valueMeta.put(makeString(i, 10), makeString(i, (128 - (10+8))));
}
ValueMetaValidation.validate(valueMeta);
}