Home | History | Annotate | Download | only in cts
      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 
     22 public class DhcpInfoTest extends AndroidTestCase {
     23 
     24     public void testConstructor() {
     25         new DhcpInfo();
     26     }
     27 
     28     public void testToString() {
     29         String expectedDefault = "ipaddr 0.0.0.0 gateway 0.0.0.0 netmask 0.0.0.0 dns1 0.0.0.0 "
     30                 + "dns2 0.0.0.0 DHCP server 0.0.0.0 lease 0 seconds";
     31         String STR_ADDR1 = "255.255.255.255";
     32         String STR_ADDR2 = "127.0.0.1";
     33         String STR_ADDR3 = "192.168.1.1";
     34         String STR_ADDR4 = "192.168.1.0";
     35         int leaseTime = 9999;
     36         String expected = "ipaddr " + STR_ADDR1 + " gateway " + STR_ADDR2 + " netmask "
     37                 + STR_ADDR3 + " dns1 " + STR_ADDR4 + " dns2 " + STR_ADDR4 + " DHCP server "
     38                 + STR_ADDR2 + " lease " + leaseTime + " seconds";
     39 
     40         DhcpInfo dhcpInfo = new DhcpInfo();
     41 
     42         // Test default string.
     43         assertEquals(expectedDefault, dhcpInfo.toString());
     44 
     45         dhcpInfo.ipAddress = ipToInteger(STR_ADDR1);
     46         dhcpInfo.gateway = ipToInteger(STR_ADDR2);
     47         dhcpInfo.netmask = ipToInteger(STR_ADDR3);
     48         dhcpInfo.dns1 = ipToInteger(STR_ADDR4);
     49         dhcpInfo.dns2 = ipToInteger(STR_ADDR4);
     50         dhcpInfo.serverAddress = ipToInteger(STR_ADDR2);
     51         dhcpInfo.leaseDuration = leaseTime;
     52 
     53         // Test with new values
     54         assertEquals(expected, dhcpInfo.toString());
     55     }
     56 
     57     private int ipToInteger(String ipString) {
     58         String ipSegs[] = ipString.split("[.]");
     59         int tmp = Integer.parseInt(ipSegs[3]) << 24 | Integer.parseInt(ipSegs[2]) << 16 |
     60             Integer.parseInt(ipSegs[1]) << 8 | Integer.parseInt(ipSegs[0]);
     61         return tmp;
     62     }
     63 }
     64