GUACAMOLE-760: Add validation and tests for the TimeZoneGuacamoleProperty

This commit is contained in:
Virtually Nick
2020-10-24 14:53:17 -04:00
parent 0ec9bec4c8
commit 3630e7800c
2 changed files with 252 additions and 1 deletions

View File

@@ -20,7 +20,9 @@
package org.apache.guacamole.properties;
import java.util.TimeZone;
import java.util.regex.Pattern;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleServerException;
/**
* A GuacamoleProperty whose value is a TimeZone.
@@ -28,6 +30,12 @@ import org.apache.guacamole.GuacamoleException;
public abstract class TimeZoneGuacamoleProperty
implements GuacamoleProperty<TimeZone> {
/**
* A regex that matches valid variants of GMT timezones.
*/
public static final Pattern GMT_REGEX =
Pattern.compile("^GMT([+-](0|00)((:)?00)?)?$");
@Override
public TimeZone parseValue(String value) throws GuacamoleException {
@@ -36,7 +44,16 @@ public abstract class TimeZoneGuacamoleProperty
return null;
// Attempt to return the TimeZone of the provided string value.
return TimeZone.getTimeZone(value);
TimeZone tz = TimeZone.getTimeZone(value);
// If the input is not GMT, but the output is GMT, the TimeZone is not
// valid and we throw an exception.
if (!GMT_REGEX.matcher(value).matches()
&& GMT_REGEX.matcher(tz.getID()).matches())
throw new GuacamoleServerException("Property \"" + getName()
+ "\" does not specify a valid time zone.");
return tz;
}