1 /* 2 * Copyright (C) 2009 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 17 package android.net.cts; 18 19 import android.net.DhcpInfo; 20 import android.test.AndroidTestCase; 21 import dalvik.annotation.TestLevel; 22 import dalvik.annotation.TestTargetClass; 23 import dalvik.annotation.TestTargetNew; 24 25 @TestTargetClass(DhcpInfo.class) 26 public class DhcpInfoTest extends AndroidTestCase { 27 28 @TestTargetNew( 29 level = TestLevel.COMPLETE, 30 notes = "Test DhcpInfo's constructor.", 31 method = "DhcpInfo", 32 args = {} 33 ) 34 public void testConstructor() { 35 new DhcpInfo(); 36 } 37 38 @TestTargetNew( 39 level = TestLevel.COMPLETE, 40 notes = "Test toString function.", 41 method = "toString", 42 args = {} 43 ) 44 public void testToString() { 45 String expectedDefault = "ipaddr 0.0.0.0 gateway 0.0.0.0 netmask 0.0.0.0 dns1 0.0.0.0 " 46 + "dns2 0.0.0.0 DHCP server 0.0.0.0 lease 0 seconds"; 47 String STR_ADDR1 = "255.255.255.255"; 48 String STR_ADDR2 = "127.0.0.1"; 49 String STR_ADDR3 = "192.168.1.1"; 50 String STR_ADDR4 = "192.168.1.0"; 51 int leaseTime = 9999; 52 String expected = "ipaddr " + STR_ADDR1 + " gateway " + STR_ADDR2 + " netmask " 53 + STR_ADDR3 + " dns1 " + STR_ADDR4 + " dns2 " + STR_ADDR4 + " DHCP server " 54 + STR_ADDR2 + " lease " + leaseTime + " seconds"; 55 56 DhcpInfo dhcpInfo = new DhcpInfo(); 57 58 // Test default string. 59 assertEquals(expectedDefault, dhcpInfo.toString()); 60 61 dhcpInfo.ipAddress = ipToInteger(STR_ADDR1); 62 dhcpInfo.gateway = ipToInteger(STR_ADDR2); 63 dhcpInfo.netmask = ipToInteger(STR_ADDR3); 64 dhcpInfo.dns1 = ipToInteger(STR_ADDR4); 65 dhcpInfo.dns2 = ipToInteger(STR_ADDR4); 66 dhcpInfo.serverAddress = ipToInteger(STR_ADDR2); 67 dhcpInfo.leaseDuration = leaseTime; 68 69 // Test with new values 70 assertEquals(expected, dhcpInfo.toString()); 71 } 72 73 private int ipToInteger(String ipString) { 74 String ipSegs[] = ipString.split("[.]"); 75 int tmp = Integer.parseInt(ipSegs[3]) << 24 | Integer.parseInt(ipSegs[2]) << 16 | 76 Integer.parseInt(ipSegs[1]) << 8 | Integer.parseInt(ipSegs[0]); 77 return tmp; 78 } 79 } 80