1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.jspf.core;
21  
22  import org.apache.james.jspf.core.IPAddr;
23  import org.apache.james.jspf.core.exceptions.PermErrorException;
24  
25  import junit.framework.TestCase;
26  
27  public class IPAddrTest extends TestCase {
28  
29      public void testValidIp4Address() throws PermErrorException {
30          assertEquals("in-addr", IPAddr.getInAddress("123.212.255.213"));
31          assertEquals("in-addr", IPAddr.getInAddress("0.0.0.0"));
32          assertEquals("in-addr", IPAddr.getInAddress("255.255.255.255"));
33      }
34  
35      public void testValidIp4OverIpv6Address() throws PermErrorException {
36          assertEquals("ip6", IPAddr.getInAddress("0:0:0:0:0:0:13.1.68.3"));
37          assertEquals("ip6", IPAddr
38                  .getInAddress("0:0:0:0:0:FFFF:129.144.52.38"));
39          assertEquals("ip6", IPAddr.getInAddress("::13.1.68.3"));
40          assertEquals("ip6", IPAddr.getInAddress("::FFFF:129.144.52.38"));
41      }
42  
43      public void testValidIp6Address() throws PermErrorException {
44          assertEquals("ip6", IPAddr
45                  .getInAddress("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210"));
46          assertEquals("ip6", IPAddr.getInAddress("1080:0:0:0:8:800:200C:417A"));
47          assertEquals("ip6", IPAddr.getInAddress("FF01:0:0:0:0:0:0:101"));
48          assertEquals("ip6", IPAddr.getInAddress("0:0:0:0:0:0:0:1"));
49          assertEquals("ip6", IPAddr.getInAddress("0:0:0:0:0:0:0:0"));
50          assertEquals("ip6", IPAddr.getInAddress("1080::8:800:200C:417A"));
51          assertEquals("ip6", IPAddr.getInAddress("FF01::101"));
52          assertEquals("ip6", IPAddr.getInAddress("::1"));
53          assertEquals("ip6", IPAddr.getInAddress("::"));
54      }
55  
56      public void testInvalidIp6Address() throws PermErrorException {
57          try {
58              assertEquals("ip6", IPAddr.getInAddress("12AB:0:0:CD3"));
59              fail();
60          } catch (PermErrorException e) {
61          }
62          try {
63              assertEquals("ip6", IPAddr
64                      .getInAddress("1080:0:0:0:8::800:200C:417A"));
65              fail();
66          } catch (PermErrorException e) {
67          }
68          try {
69              assertEquals("ip6", IPAddr.getInAddress("FF01:0:0:0:0:0:0:00000"));
70              fail();
71          } catch (PermErrorException e) {
72          }
73          try {
74              assertEquals("ip6", IPAddr.getInAddress("0:0:0:0:0:0:0:0:1"));
75              fail();
76          } catch (PermErrorException e) {
77          }
78          try {
79              assertEquals("ip6", IPAddr.getInAddress("0:0:0:0:0:0:0:O"));
80              fail();
81          } catch (PermErrorException e) {
82          }
83          try {
84              assertEquals("ip6", IPAddr.getInAddress("1080::8:800::200C:417A"));
85              fail();
86          } catch (PermErrorException e) {
87          }
88          try {
89              assertEquals("ip6", IPAddr.getInAddress("FF01:::101"));
90              fail();
91          } catch (PermErrorException e) {
92          }
93          try {
94              assertEquals("ip6", IPAddr.getInAddress(":1:"));
95              fail();
96          } catch (PermErrorException e) {
97          }
98          try {
99              assertEquals("ip6", IPAddr.getInAddress(":"));
100             fail();
101         } catch (PermErrorException e) {
102         }
103     }
104 
105     public void testInvalidIp4AddressGreatThan255() {
106         try {
107             assertEquals("in-addr", IPAddr.getInAddress("333.212.255.213"));
108             fail();
109         } catch (PermErrorException e) {
110         }
111         try {
112             assertEquals("in-addr", IPAddr.getInAddress("1.2.3."));
113             fail();
114         } catch (PermErrorException e) {
115         }
116         try {
117             assertEquals("in-addr", IPAddr.getInAddress("1.2.3.a"));
118             fail();
119         } catch (PermErrorException e) {
120         }
121         try {
122             assertEquals("in-addr", IPAddr.getInAddress("1.1.1.1111"));
123             fail();
124         } catch (PermErrorException e) {
125         }
126     }
127 
128 }