1 package com.atlassian.security.auth.trustedapps;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.Set;
7
8 import junit.framework.TestCase;
9
10 public class TestDefaultIPMatcher extends TestCase
11 {
12 public void testIpMatcherFailsOutOfRangeNumber() throws Exception
13 {
14 assertIllegalMatcherInvalidIP("299.299.299.299");
15 assertIllegalMatcherInvalidIP("299.1.1.1");
16 assertIllegalMatcherInvalidIP("1.299.1.1");
17 assertIllegalMatcherInvalidIP("1.1.299.1");
18 assertIllegalMatcherInvalidIP("1.1.1.299");
19 }
20
21 public void testIpMatcherFailsOutOfRangeNegativeNumber() throws Exception
22 {
23 assertIllegalMatcherInvalidIP("-2.-2.-2.-2");
24 assertIllegalMatcherInvalidIP("-2.1.1.1");
25 assertIllegalMatcherInvalidIP("1.-2.1.1");
26 assertIllegalMatcherInvalidIP("1.1.-2.1");
27 assertIllegalMatcherInvalidIP("1.1.1.-2");
28 }
29
30 public void testIpMatcherFailsIllegalWildcard() throws Exception
31 {
32 assertIllegalMatcherInvalidIP("?.?.?.?");
33 assertIllegalMatcherInvalidIP("?.1.1.1");
34 assertIllegalMatcherInvalidIP("1.?.1.1");
35 assertIllegalMatcherInvalidIP("1.1.?.1");
36 assertIllegalMatcherInvalidIP("1.1.1.?");
37 }
38
39 private void assertIllegalMatcherInvalidIP(final String dodgyIPAddress)
40 {
41 try
42 {
43 final Set<String> patterns = new HashSet<String>(Arrays.asList(dodgyIPAddress));
44 new DefaultIPMatcher(patterns);
45 fail("Should have thrown InvalidIPAddress: " + dodgyIPAddress);
46 }
47 catch (final IPAddressFormatException yay)
48 {
49 assertEquals(dodgyIPAddress, yay.getBadIPAddress());
50 }
51 }
52
53 public void testDefIPMatcher() throws Exception
54 {
55 DefaultIPMatcher matcher = new DefaultIPMatcher(new HashSet<String>(Arrays.asList("192.168.*.23", "123.132.*.*", "123.45.67.*",
56 "255.255.255.255", "255.255.255.*")));
57
58 assertIPMatch(true, matcher, "192.168.1.23");
59 assertIPMatch(true, matcher, "192.168.2.23");
60 assertIPMatch(false, matcher, "192.168.1.24");
61
62 assertIPMatch(true, matcher, "123.132.1.24");
63 assertIPMatch(true, matcher, "123.132.2.27");
64 assertIPMatch(false, matcher, "124.132.1.24");
65 assertIPMatch(false, matcher, "123.131.1.24");
66
67 assertIPMatch(true, matcher, "123.45.67.24");
68 assertIPMatch(true, matcher, "123.45.67.27");
69 assertIPMatch(false, matcher, "124.45.1.24");
70 assertIPMatch(false, matcher, "123.45.32.24");
71
72 assertIPMatch(true, matcher, "255.255.255.255");
73 assertIPMatch(true, matcher, "255.255.255.251");
74 assertIPMatch(false, matcher, "255.255.254.255");
75
76 try
77 {
78 matcher.match("192.168.1.");
79 fail("invalid pattern must fail");
80 }
81 catch (final IPAddressFormatException e)
82 {
83
84 }
85
86 try
87 {
88 matcher = new DefaultIPMatcher(Collections.singleton("192.168.*."));
89 fail("invalid pattern must fail");
90 }
91 catch (final IPAddressFormatException e)
92 {
93
94 }
95 }
96
97 public void testEmptyIpMatcherMatchesEverything() throws Exception
98 {
99 final DefaultIPMatcher matcher = new DefaultIPMatcher(Collections.<String> emptySet());
100 assertIPMatch(true, matcher, "192.168.0.5");
101 }
102
103 private void assertIPMatch(final boolean expected, final IPMatcher matcher, final String pattern) throws Exception
104 {
105 assertEquals(expected, matcher.match(pattern));
106 }
107 }