1/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.conscrypt;
18
19import junit.framework.TestCase;
20
21/**
22 * Test for AddressUtils
23 */
24public class AddressUtilsTest extends TestCase {
25    public void test_isValidSniHostname_Success() throws Exception {
26        assertTrue(AddressUtils.isValidSniHostname("www.google.com"));
27    }
28
29    public void test_isValidSniHostname_NotFQDN_Failure() throws Exception {
30        assertFalse(AddressUtils.isValidSniHostname("www"));
31    }
32
33    public void test_isValidSniHostname_IPv4_Failure() throws Exception {
34        assertFalse(AddressUtils.isValidSniHostname("192.168.0.1"));
35    }
36
37    public void test_isValidSniHostname_IPv6_Failure() throws Exception {
38        assertFalse(AddressUtils.isValidSniHostname("2001:db8::1"));
39    }
40
41    public void test_isLiteralIpAddress_IPv4_Success() throws Exception {
42        assertTrue(AddressUtils.isLiteralIpAddress("127.0.0.1"));
43        assertTrue(AddressUtils.isLiteralIpAddress("255.255.255.255"));
44        assertTrue(AddressUtils.isLiteralIpAddress("0.0.00.000"));
45        assertTrue(AddressUtils.isLiteralIpAddress("192.009.010.19"));
46        assertTrue(AddressUtils.isLiteralIpAddress("254.249.190.094"));
47    }
48
49    public void test_isLiteralIpAddress_IPv4_ExtraCharacters_Failure() throws Exception {
50        assertFalse(AddressUtils.isLiteralIpAddress("127.0.0.1a"));
51        assertFalse(AddressUtils.isLiteralIpAddress(" 255.255.255.255"));
52        assertFalse(AddressUtils.isLiteralIpAddress("0.0.00.0009"));
53        assertFalse(AddressUtils.isLiteralIpAddress("192.009z.010.19"));
54        assertFalse(AddressUtils.isLiteralIpAddress("254.249..094"));
55    }
56
57    public void test_isLiteralIpAddress_IPv4_NumbersTooLarge_Failure() throws Exception {
58        assertFalse(AddressUtils.isLiteralIpAddress("256.255.255.255"));
59        assertFalse(AddressUtils.isLiteralIpAddress("255.255.255.256"));
60        assertFalse(AddressUtils.isLiteralIpAddress("192.168.1.260"));
61    }
62
63    public void test_isLiteralIpAddress_IPv6_Success() throws Exception {
64        assertTrue(AddressUtils.isLiteralIpAddress("::1"));
65        assertTrue(AddressUtils.isLiteralIpAddress("2001:Db8::1"));
66        assertTrue(AddressUtils.isLiteralIpAddress("2001:cdbA:0000:0000:0000:0000:3257:9652"));
67        assertTrue(AddressUtils.isLiteralIpAddress("2001:cdba:0:0:0:0:3257:9652"));
68        assertTrue(AddressUtils.isLiteralIpAddress("2001:cdBA::3257:9652"));
69    }
70
71    public void test_isLiteralIpAddress_IPv6_Failure() throws Exception {
72        assertFalse(AddressUtils.isLiteralIpAddress(":::1"));
73        assertFalse(AddressUtils.isLiteralIpAddress("::11111"));
74        assertFalse(AddressUtils.isLiteralIpAddress("20011::1111"));
75        assertFalse(AddressUtils.isLiteralIpAddress("2001:db8:::1"));
76        assertFalse(AddressUtils.isLiteralIpAddress("2001:cdba:0000:00000:0000:0000:3257:9652"));
77        assertFalse(AddressUtils.isLiteralIpAddress("2001:cdbA:0000:0000:0000:0000:0000:3257:9652"));
78        assertFalse(AddressUtils.isLiteralIpAddress("2001:cdba:0::0:0:0:3257:9652"));
79        assertFalse(AddressUtils.isLiteralIpAddress("02001:cdba::3257:9652"));
80        assertFalse(AddressUtils.isLiteralIpAddress("2001:cdba::3257:96521"));
81    }
82}
83