瀏覽代碼

Improve server name validation. Unit test things.

Change-Id: Ic8df21726d438117222c09f617ea9b66dfc7df88
Reviewed-on: http://gerrit.dmdirc.com/2437
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.7rc1
Greg Holmes 12 年之前
父節點
當前提交
f16e4ab36b

+ 11
- 7
src/com/dmdirc/util/validators/ServerNameValidator.java 查看文件

@@ -35,17 +35,21 @@ public class ServerNameValidator implements Validator<String> {
35 35
     @Override
36 36
     public ValidationResponse validate(final String object) {
37 37
         try {
38
-            URI uri = new URI(object);
39
-            if (uri.getHost() == null || uri.getHost().isEmpty()) {
40
-                uri = new URI("irc://" + object);
38
+            if (object == null || object.isEmpty()) {
39
+                return new ValidationResponse("Server name is required.");
41 40
             }
42
-            if (uri.getHost() == null || uri.getHost().isEmpty()) {
43
-                return new ValidationResponse("Address requires a hostname.");
41
+            if (!object.matches(".*://")) {
42
+                new URI("irc://" + object);
44 43
             } else {
45
-                return new ValidationResponse();
44
+                new URI(object);
46 45
             }
46
+            return new ValidationResponse();
47 47
         } catch (URISyntaxException ex) {
48
-            return new ValidationResponse(ex.getReason());
48
+            if ("Expected authority".equals(ex.getReason())) {
49
+                return new ValidationResponse("Address requires a hostname.");
50
+            } else {
51
+                return new ValidationResponse(ex.getReason());
52
+            }
49 53
         }
50 54
     }
51 55
 }

+ 48
- 0
test/com/dmdirc/util/validators/DisabledOptionValidatorTest.java 查看文件

@@ -0,0 +1,48 @@
1
+/*
2
+ * Copyright (c) 2006-2012 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ * THE SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util.validators;
24
+
25
+import org.junit.Test;
26
+
27
+import static org.junit.Assert.*;
28
+
29
+public class DisabledOptionValidatorTest {
30
+
31
+    @Test
32
+    public void testValidateNull() {
33
+        DisabledOptionValidator instance = new DisabledOptionValidator();
34
+        assertFalse(instance.validate(null).isFailure());
35
+    }
36
+
37
+    @Test
38
+    public void testValidateFalse() {
39
+        DisabledOptionValidator instance = new DisabledOptionValidator();
40
+        assertTrue(instance.validate("false:moo").isFailure());
41
+    }
42
+
43
+    @Test
44
+    public void testValidateTrue() {
45
+        DisabledOptionValidator instance = new DisabledOptionValidator();
46
+        assertFalse(instance.validate("true:moo").isFailure());
47
+    }
48
+}

+ 12
- 8
test/com/dmdirc/util/validators/NumericalValidatorTest.java 查看文件

@@ -21,13 +21,17 @@
21 21
  */
22 22
 package com.dmdirc.util.validators;
23 23
 
24
-import com.dmdirc.util.validators.ValidationResponse;
25
-import com.dmdirc.util.validators.NumericalValidator;
26 24
 import org.junit.Test;
25
+
27 26
 import static org.junit.Assert.*;
28 27
 
29 28
 public class NumericalValidatorTest {
30 29
 
30
+    @Test(expected=IllegalArgumentException.class)
31
+    public void testConstructorThrowsExceptionForInvalidArguments() {
32
+        new NumericalValidator(2, 1);
33
+    }
34
+
31 35
     @Test
32 36
     public void testGetMax() {
33 37
         assertEquals(10, new NumericalValidator(1, 10).getMax());
@@ -43,34 +47,34 @@ public class NumericalValidatorTest {
43 47
     @Test
44 48
     public void testNAN() {
45 49
         final ValidationResponse vr = new NumericalValidator(3, 5).validate("foo");
46
-        
50
+
47 51
         assertTrue(vr.isFailure());
48 52
         assertTrue(vr.getFailureReason().indexOf("number") > -1);
49 53
     }
50
-    
54
+
51 55
     @Test
52 56
     public void testMin() {
53 57
         final NumericalValidator nv1 = new NumericalValidator(-1, -1);
54 58
         final NumericalValidator nv2 = new NumericalValidator(-5, -1);
55
-        
59
+
56 60
         assertFalse(nv1.validate("-5").isFailure());
57 61
         assertFalse(nv2.validate("-5").isFailure());
58 62
         assertFalse(nv2.validate("-4").isFailure());
59 63
         assertFalse(nv2.validate("10").isFailure());
60 64
         assertTrue(nv2.validate("-6").isFailure());
61 65
     }
62
-    
66
+
63 67
     @Test
64 68
     public void testMax() {
65 69
         final NumericalValidator nv1 = new NumericalValidator(-1, -1);
66 70
         final NumericalValidator nv2 = new NumericalValidator(-1, 10);
67
-        
71
+
68 72
         assertFalse(nv1.validate("-5").isFailure());
69 73
         assertFalse(nv1.validate("50").isFailure());
70 74
         assertFalse(nv2.validate("-5").isFailure());
71 75
         assertFalse(nv2.validate("-4").isFailure());
72 76
         assertFalse(nv2.validate("10").isFailure());
73 77
         assertTrue(nv2.validate("11").isFailure());
74
-    }    
78
+    }
75 79
 
76 80
 }

+ 8
- 3
test/com/dmdirc/util/validators/OptionalValidatorTest.java 查看文件

@@ -22,14 +22,19 @@
22 22
 
23 23
 package com.dmdirc.util.validators;
24 24
 
25
-import com.dmdirc.util.validators.OptionalValidator;
26
-import com.dmdirc.util.validators.ValidationResponse;
27
-import com.dmdirc.util.validators.StringLengthValidator;
28 25
 import org.junit.Test;
26
+
29 27
 import static org.junit.Assert.*;
30 28
 
31 29
 public class OptionalValidatorTest {
32 30
 
31
+    @Test
32
+    public void testGetValidator() {
33
+        final Validator<String> validator = new PermissiveValidator<String>();
34
+        final OptionalValidator instance = new OptionalValidator(validator);
35
+        assertEquals(validator, instance.getValidator());
36
+    }
37
+
33 38
     @Test
34 39
     public void testNoSeparator() {
35 40
         final ValidationResponse res = new OptionalValidator(new StringLengthValidator(0, 3))

+ 68
- 0
test/com/dmdirc/util/validators/ServerNameValidatorTest.java 查看文件

@@ -0,0 +1,68 @@
1
+/*
2
+ * Copyright (c) 2006-2012 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ * THE SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util.validators;
24
+
25
+import org.junit.Test;
26
+
27
+import static org.junit.Assert.*;
28
+
29
+public class ServerNameValidatorTest {
30
+
31
+
32
+    @Test
33
+    public void testValidateNull() {
34
+        ServerNameValidator instance = new ServerNameValidator();
35
+        assertTrue(instance.validate(null).isFailure());
36
+    }
37
+
38
+    @Test
39
+    public void testValidateEmpty() {
40
+        ServerNameValidator instance = new ServerNameValidator();
41
+        assertTrue(instance.validate("").isFailure());
42
+    }
43
+
44
+    @Test
45
+    public void testValidateNoProtocol() {
46
+        ServerNameValidator instance = new ServerNameValidator();
47
+        assertFalse(instance.validate("test.com").isFailure());
48
+    }
49
+
50
+    @Test
51
+    public void testValidateNoHostname() {
52
+        ServerNameValidator instance = new ServerNameValidator();
53
+        assertEquals("Address requires a hostname.",
54
+                instance.validate("irc://").getFailureReason());
55
+    }
56
+
57
+    @Test
58
+    public void testValidateInvalidURI() {
59
+        ServerNameValidator instance = new ServerNameValidator();
60
+        assertTrue(instance.validate("irc://^").isFailure());
61
+    }
62
+
63
+    @Test
64
+    public void testValidateValid() {
65
+        ServerNameValidator instance = new ServerNameValidator();
66
+        assertFalse(instance.validate("irc://test.com").isFailure());
67
+    }
68
+}

+ 48
- 0
test/com/dmdirc/util/validators/URIValidatorTest.java 查看文件

@@ -0,0 +1,48 @@
1
+/*
2
+ * Copyright (c) 2006-2012 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ * THE SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util.validators;
24
+
25
+import org.junit.Test;
26
+
27
+import static org.junit.Assert.*;
28
+
29
+public class URIValidatorTest {
30
+
31
+    @Test
32
+    public void testValidateNoHostname() {
33
+        URIValidator instance = new URIValidator();
34
+        assertTrue(instance.validate("http://").isFailure());
35
+    }
36
+
37
+    @Test
38
+    public void testValidateInvalidURI() {
39
+        URIValidator instance = new URIValidator();
40
+        assertTrue(instance.validate("http://^").isFailure());
41
+    }
42
+
43
+    @Test
44
+    public void testValidateValid() {
45
+        URIValidator instance = new URIValidator();
46
+        assertFalse(instance.validate("http://google.com:80").isFailure());
47
+    }
48
+}

Loading…
取消
儲存