InetAddressTest.java revision 5d3f5562c167120b5ec00e509af0f0ab9308bff5
1/* 2 * Copyright (C) 2011 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 libcore.java.net; 18 19import java.net.InetAddress; 20 21public class InetAddressTest extends junit.framework.TestCase { 22 public void test_parseNumericAddress() throws Exception { 23 // Regular IPv4. 24 assertEquals("/1.2.3.4", InetAddress.parseNumericAddress("1.2.3.4").toString()); 25 // Regular IPv6. 26 assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("2001:4860:800d::68").toString()); 27 // Weird IPv4 special cases. 28 assertEquals("/1.2.0.3", InetAddress.parseNumericAddress("1.2.3").toString()); 29 assertEquals("/1.0.0.2", InetAddress.parseNumericAddress("1.2").toString()); 30 assertEquals("/0.0.0.1", InetAddress.parseNumericAddress("1").toString()); 31 try { 32 // Almost numeric but invalid... 33 InetAddress.parseNumericAddress("1."); 34 fail(); 35 } catch (IllegalArgumentException expected) { 36 } 37 try { 38 // Not even close to numeric... 39 InetAddress.parseNumericAddress("www.google.com"); 40 fail(); 41 } catch (IllegalArgumentException expected) { 42 } 43 // Strange special cases, for compatibility with InetAddress.getByName. 44 assertTrue(InetAddress.parseNumericAddress(null).isLoopbackAddress()); 45 assertTrue(InetAddress.parseNumericAddress("").isLoopbackAddress()); 46 } 47 48 public void test_getLoopbackAddress() throws Exception { 49 assertTrue(InetAddress.getLoopbackAddress().isLoopbackAddress()); 50 } 51} 52