File: InetAddressTest.java

package info (click to toggle)
mauve 20120103-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 28,504 kB
  • sloc: java: 250,155; sh: 2,834; xml: 208; makefile: 66
file content (220 lines) | stat: -rw-r--r-- 6,311 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Tags: JDK1.0

/*
   Copyright (C) 1999 Hewlett-Packard Company

   This file is part of Mauve.

   Mauve is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   Mauve is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Mauve; see the file COPYING.  If not, write to
   the Free Software Foundation, 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

package gnu.testlet.java.net.InetAddress;
import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;
import java.net.*;


public class InetAddressTest implements Testlet
{
  protected static TestHarness harness;
  public void test_Basics()
  {
    harness.checkPoint("Basics");
    InetAddress addr=null;
    try {
      addr = InetAddress.getLocalHost();
    }
    catch ( UnknownHostException e ){
      e.printStackTrace();
      harness.fail("Error : test_Basics failed - 0 " +
			 " Should not throw UnknownHostException here " );
    }

    harness.check ( !(addr.getHostName() == null),
      "Error : test_Basics failed - 1" +
			 " Should not return null as the host name " );


    harness.check ( !(addr.getHostAddress() == null),
      "Error : test_Basics failed - 2" +
			 " Should not return null as the host address " );

    harness.check ( !(addr.hashCode() == 0),
      "Error : test_Basics failed - 3" +
			 " Should not return 0 as the hashcode " );


    InetAddress addr1 = null;
    try {
      addr1 = InetAddress.getByName(addr.getHostName());
      harness.check(true);
    }
    catch ( UnknownHostException e ){
      e.printStackTrace();
      harness.fail("Error : test_Basics failed - 4 " +
			 " Should not throw UnknownHostException here " );
    }



    harness.check ( addr, addr1, "Error : test_Basics failed - 5" +
			 " Both the addresses should be the same" );


    harness.check ( addr1.getHostAddress(), addr.getHostAddress(),
      "Error : test_Basics failed - 6" +
			 " Should return the host addresses the same" );
		

    InetAddress addr2[] = null;
    try {
      addr2 = InetAddress.getAllByName(addr.getHostName());
      harness.check(true);
    }
    catch ( UnknownHostException e ){
      e.printStackTrace();
      harness.fail("Error : test_Basics failed - 7 " +
			 " Should not throw UnknownHostException here " );
    }
    catch ( Exception e ){
      e.printStackTrace();
      harness.fail("Error : test_Basics failed - 7 " +
			 " Should not throw Exception here " );
    }

		
    if ( addr2.length < 1 ) {
      harness.fail("Error : test_Basics failed - 8 " +
			 "the address array should be of length 1 or larger" );
    } else
      harness.check(true);


    harness.check ( addr2[0], addr1, "Error : test_Basics failed - 9" +
			 " Both the addresses should be the same" );

    InetAddress addr3 = null;
    try {
      addr3 = InetAddress.getByName("savannah.gnu.org");
      harness.check(true);
    }
    catch ( UnknownHostException e ){
      e.printStackTrace();
      harness.fail("Error : test_Basics failed - 10 " +
			 " Should not throw UnknownHostException here " );
    }
 

    try {
      harness.check((addr3.getHostName().equals("savannah.gnu.org")
		    || addr3.getHostName().equals("savannah")),
		    "test_Basics failed - 11 " +
		    " the hostname returned is not correct." );
    } catch (NullPointerException npe) {
       harness.check(false, "test_Basics failed - 11 - NullPointerException");
    }

    try {
      String toStr = addr3.toString();
      String toStr1 = addr3.getHostAddress();
      if (toStr.indexOf(toStr1) == -1)
        harness.fail("Error : test_Basics failed - 12 " +
			 " the host address returned is not correct." );
    } catch (NullPointerException npe) {
       harness.check(false, "test_Basics failed - 12 - NullPointerException");
    }

      //multicast test

      InetAddress addr4 = null;
      try {
	addr4 = InetAddress.getByName("176.1.1.1");
      }
      catch ( UnknownHostException e ){
	harness.fail("Error : test_Basics failed - 13 " +
			   " Should not throw UnknownHostException here " );
      }

      if ( addr4.isMulticastAddress())
	harness.fail("Error : test_Basics failed - 14 " +
			   " Should have returned false here " );

      InetAddress addr5 = null;
      try {
	addr5 = InetAddress.getByName("238.255.255.255");
      }
      catch ( UnknownHostException e ){
	harness.fail("Error : test_Basics failed - 15 " +
			   " Should not throw UnknownHostException here " );
      }

      if ( !addr5.isMulticastAddress())
	harness.fail("Error : test_Basics failed - 16 " +
			   " Should have returned true here " );


      InetAddress addr6 = null;
      try {
	addr6 = InetAddress.getByName("224.0.0.1");
      }
      catch ( UnknownHostException e ){
	harness.fail("Error : test_Basics failed - 17 " +
			   " Should not throw UnknownHostException here " );
      }

      if ( !addr6.isMulticastAddress())
	harness.fail("Error : test_Basics failed - 18 " +
			   " Should have returned true here " );

      InetAddress addr7 = null;
      try {
	addr7 = InetAddress.getByName("229.35.35.1");
      }
      catch ( UnknownHostException e ){
	harness.fail("Error : test_Basics failed - 19 " +
			   " Should not throw UnknownHostException here " );
      }

      if ( !addr7.isMulticastAddress())
	harness.fail("Error : test_Basics failed - 20 " +
			   " Should have returned true here " );

    InetAddress addr8 = null;
    try {
      addr8 = InetAddress.getByName("127.0.0.1");
    }
    catch (UnknownHostException e) {
      harness.fail("Error : test_Basics failed - 21 " +
		   " Should not throw UnknownHostException here " );
    }

    if (! (addr8 instanceof Inet4Address))
      harness.fail("Error : test_Basics failed - 22 " +
		   " Should have returned true here " );
  }

  public void testall()
  {
    test_Basics();
  }

  public void test (TestHarness the_harness)
  {
    harness = the_harness;
    testall ();
  }

}