[Title] Fixed version validation 91/11191/3
authorchanghyun1.lee <changhyun1.lee@samsung.com>
Tue, 22 Oct 2013 10:12:18 +0000 (19:12 +0900)
committerchanghyun1.lee <changhyun1.lee@samsung.com>
Thu, 24 Oct 2013 10:41:34 +0000 (19:41 +0900)
[Desc.]
[Issue]

Change-Id: I1607a05192c81d584652552c433e80c187d7ae0a
Signed-off-by: changhyun1.lee <changhyun1.lee@samsung.com>
org.tizen.common/src/org/tizen/common/util/ValidationUtil.java
org.tizen.common/test/src/org/tizen/common/util/ValidationUtilTest.java

index b1e0e4b..88ac5ed 100644 (file)
@@ -58,7 +58,7 @@ public class ValidationUtil {
 
     private static final String REGEX_EMAIL = "[A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])??"; //$NON-NLS-1$
     private static final String REGEX_URL = "\\b(https?)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; //$NON-NLS-1$
-    private static final String REGEX_VERSION = "[0-9]{1,2}\\.[0-9]{1,2}(\\.[0-9]{1,4})?"; //$NON-NLS-1$
+    private static final String REGEX_VERSION = "[0-9]{1,3}\\.[0-9]{1,3}(\\.[0-9]{1,5})?"; //$NON-NLS-1$
     private static final String REGEX_APPWIDGETID = "[0-9a-zA-Z]{10}\\.[0-9a-zA-Z]{1,52}\\.[0-9a-zA-Z]{1,}"; //$NON-NLS-1$
 
     public static final String WIDGET_CONTENT_FILE_EXTENSIONS[] = { ".html", ".htm", ".xhtml", ".xht", ".svg" }; // http://www.w3.org/TR/widgets/#default-start-files
@@ -141,18 +141,39 @@ public class ValidationUtil {
 
     /**
      * Checking for version.
-     * 
+     * The application version format has the following constraints: {xxx.yyy.zzzz} "0<={x,y}<=255, 0<=z<=65535"
      * @param version
      * @return {@code true} if version is valid version
      */
     public static boolean checkForVersion( String version ) {
-        if ( version == null || StringUtil.isEmpty( version ) || version.equals( "0.0" ) ) {
+        if ( version == null || StringUtil.isEmpty( version ) ) {
             return false;
         }
 
         Pattern pattern = Pattern.compile( REGEX_VERSION );
         Matcher matcher = pattern.matcher( version );
-        return matcher.matches();
+        if ( matcher.matches() ) {
+            String[] versions = version.split("\\.");
+            // check version length: x.y(.z)
+            if ( versions.length < 3 ) {
+                return false;
+            }
+            // check x,y value: 0<=(x,y)<=255
+            for ( int index=0; index<2; index++ ) {
+                int xy = Integer.parseInt(versions[index]);
+                if ( xy < 0  || 255 < xy ) {
+                    return false;
+                }
+            }
+            // check z value: 0<=z<=65535
+            int z = Integer.parseInt(versions[2]);
+            if ( z < 0  || 65535 < z ) {
+                return false;
+            }
+            return true;
+        } else {
+            return false;
+        }
     }
 
     /**
index 8b544e8..be1124f 100755 (executable)
@@ -183,12 +183,12 @@ public class ValidationUtilTest {
      */
     @Test
     public void test_checkForVersion() throws Exception {
-        final String[] validedVersions = { "1.0.1", "1.0.111", "1.10.111", "35.35.123", "0.0.1", "0.1", "1.0", "0.0.0" };
+        final String[] validedVersions = { "0.0.0", "0.0.1", "0.1.0", "1.0.0", "1.0.1", "1.1.1", "0.0.00001", "000.000.00001", "000.001.00000", "001.000.00000", "001.001.00001", "255.0.0", "0.255.0", "0.0.65535", "255.255.65535" };
         for ( String version : validedVersions ) {
             assertTrue( "Failed for \"" + version + "\"", ValidationUtil.checkForVersion( version ) );
         }
 
-        final String[] invalidedVersions = { "", "111.111.111111", "1", "1.0.", "0" };
+        final String[] invalidedVersions = { "", "256.256.65536", "0", "0.0.", "1", "256.0.0", "0.256.0", "0.0.65536", "0000.000.00001", "000.0000.00001", "000.000.000001" };
         for ( String version : invalidedVersions ) {
             assertFalse( "Failed for \"" + version + "\"", ValidationUtil.checkForVersion( version ) );
         }