File: SocketTest.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 (520 lines) | stat: -rw-r--r-- 13,760 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// Tags: JDK1.0
// Uses: SocketBServer SocketServer

/*
  Copyright (C) 1999 Hewlett-Packard Company
  Copyright (C) 2005 Mark J. Wielaard <mark@klomp.org>
  
  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.Socket;
import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;
import java.net.*;
import java.io.*;


public class SocketTest implements Testlet
{
  protected static TestHarness harness;
  
  public void test_BasicServer()
  {
    harness.checkPoint("BasicServer");
    try {
      SocketServer srv = new SocketServer();
      SocketServer.harness = harness;
      srv.init();
      srv.start();
      Thread.yield();
      harness.check(true, "BasicServer");
    }
    catch (Exception e) {
      harness.fail("Error : test_BasicServer failed - 0 " +
		   "exception was thrown.");
      harness.debug(e);
    }
    
    Socket sock = null;
    try {
      sock = new Socket("127.0.0.1", 23000);
      DataInputStream dis = new DataInputStream(sock.getInputStream());
      String str = dis.readLine();
      
      harness.check(str.equals("hello buddy"),
		    "Error : test_BasicServer failed - 1 " +
		    "string returned is not correct.");
    }
    catch (Exception e) {
      harness.fail("Error : test_BasicServer failed - 2 " +
		   "exception was thrown.");
      harness.debug(e);
    }
    finally {
      try {
	if (sock != null)
	  sock.close();
      } catch(IOException ignored) {}
    }
    
    // second iteration
    try {
      sock = new Socket("127.0.0.1", 23000);
      DataInputStream dis = new DataInputStream(sock.getInputStream());
      String str = dis.readLine();
      
      harness.check(str.equals("hello buddy"),
		    "Error : test_BasicServer failed - 3 " +
		    "string returned is not correct.");
      sock.close();
      harness.check(true);
    }
    catch (Exception e) {
      harness.fail("Error : test_BasicServer failed - 4 " +
		   "exception was thrown.");
      harness.debug(e);
    }
    finally {
      try {
	if (sock != null)
	  sock.close();
      } catch(IOException ignored) {}
    }
    
    // second iteration
    try {
      sock = new Socket("127.0.0.1", 23000);
      DataInputStream dis = new DataInputStream(sock.getInputStream());
   
      byte data[] = new byte[5];
      int len;

      len = dis.read(data);
      String str = new String(data, 0, 0, 5);

      harness.check(str.equals("hello"),
		    "Error : test_BasicServer failed - 5 " +
		    "string returned is not correct.");
      dis.close();
      sock.close();
      harness.check(true);
    }
    catch (Exception e) {
      harness.fail("Error : test_BasicServer failed - 6 " +
		   "exception was thrown.");
      harness.debug(e);
    }
    finally {
      try {
	if (sock != null)
	  sock.close();
      } catch(IOException ignored) {}
    }

    // second iteration
    try {
      sock = new Socket("127.0.0.1", 23000);
      InputStream is = sock.getInputStream();
      byte data[] = new byte[5];

      int len;
      len = is.read(data, 0, 5);
      String str= new String(data, 0, 0, 5);

      harness.check(str.equals("hello"),
		    "Error : test_BasicServer failed - 8 " +
		    "string returned is not correct.");
      is.close();
      harness.check(true);
      
    }
    catch (Exception e) {
      harness.fail("Error : test_BasicServer failed - 9 " +
		   "exception was thrown.");
      harness.debug(e);
    }
    finally {
      try {
	if (sock != null)
	  sock.close();
      } catch(IOException ignored) {}
    }

    // second iteration
    try {
      sock = new Socket("127.0.0.1", 23000);
      InputStream is = sock.getInputStream();
      byte data[] = new byte[5];
      is.skip(2);

      int len = is.available();  // deterministic after blocking for skip
      harness.check(len > 0,
		    "Error : test_BasicServer failed - 7 " +
		     "no more data available");

      is.read(data, 0, 3);

      String str = new String(data, 0, 0, 3);

      harness.check(str.equals("llo"),
		    "Error : test_BasicServer failed - 10 " +
		    "string returned is not correct.");
      is.close();
      harness.check(true);
    }
    catch (Exception e) {
      harness.fail("Error : test_BasicServer failed - 11 " +
		   "exception was thrown.");
      harness.debug(e);
    }
    finally {
      try {
	if (sock != null)
	  sock.close();
      } catch(IOException ignored) {}
    }

  }

  public void test_params()
  {
    String host = harness.getMailHost();
    int port = 25;

    harness.checkPoint("params");
    Socket sock = null;

    try {
      sock = new Socket(host, port);
      
      harness.check(sock.getLocalPort() > 0,
		    "Error : test_params failed - 1 " +
		    "get port did not return proper values");
      
      try {
	sock.setSoTimeout(100);
	harness.check(sock.getSoTimeout() == 100,
		      "Error : test_params failed - 2 " +
		      "get /set timeout did not return proper values");
	harness.check(true);
      } 
      catch (Exception e) {
	harness.check(false, "Error : setSoTimeout fails since some OSes do not support the feature");
	harness.debug(e);
      }

      harness.debug ("getTcpNoDelay() default: " + sock.getTcpNoDelay ());
      harness.check ((sock.getTcpNoDelay () == false), "default getTcpNoDelay() should be false");
      sock.setTcpNoDelay(true);
      harness.check(sock.getTcpNoDelay(),
		    "Error : test_params failed - 3 " +
		    "get /set tcp delay did not return proper values");
      
      harness.debug ("getSoLinger() default: " + sock.getSoLinger());
      harness.check (sock.getSoLinger(), -1, "default getSoLinger() should be -1");
      sock.setSoLinger(true, 10);
      harness.check(sock.getSoLinger() == 10, 
		    "Error : test_params failed - 4");

      sock.setSoLinger(false, 20);
      harness.check(sock.getSoLinger() == -1,
		    "Error : test_params failed - 5");
      
      harness.check(sock.getPort() == port,
		    "Error : test_params failed - 6");
      
      harness.debug("sock.getInetAddress().toString(): " +
		    sock.getInetAddress().toString());
      harness.check(sock.getInetAddress().toString().indexOf(host) != -1,
		    "getInetAddress().toString() should contain host " + host);
      harness.debug("sock.toString(): " + sock.toString());
      harness.check(sock.toString().indexOf(host) != -1,
		    "toString() should contain host " + host);

    }
    catch (Exception e) {
      harness.fail("Error : test_params failed - 10 exception was thrown.");
      harness.debug(e);
    }
    finally {
      try {
	if (sock != null)
	  sock.close();
      } catch(IOException ignored) {}
    }
  }

  public void test_Basics()
  {
    harness.checkPoint("Basics");
    Socket s = null;
    // host name given
    try {
      s = new Socket ("babuspdjflks.gnu.org.", 200);
      harness.fail("Error : test_Basics failed - 1 " +
		   "exception should have been thrown here");
    }
    catch (UnknownHostException e) {
      harness.check(true);
    }
    catch (IOException e) {
      harness.fail("Error : test_Basics failed - 2 " +
		   "Unknown host exception should have been thrown here.");
      harness.debug(e);
    }
    finally {
      try {
	if (s != null)
	  s.close();
      } catch(IOException ignored) {}
    }

    try {
      s = new Socket("127.0.0.1", 30001);
      harness.fail("Error : test_Basics failed - 3 " +
		   "exception should have been thrown here");
    }
    catch (UnknownHostException e) {
      harness.fail("Error : test_Basics failed - 4 " +
		   "Unknown host exception should not have been thrown here");
      harness.debug(e);
    }
    catch (ConnectException e) {
      harness.check(true);      
    }
    catch (IOException e) {
      harness.fail("Error : test_Basics failed - 4 " +
                   "ConnectException should have been thrown here");
      harness.debug(e);
    }
    finally {
      try {
	if (s != null)
	  s.close();
      } catch(IOException ignored) {}
    }

    try {
      s = new Socket("127.0.0.1", 30001, true);
      harness.fail("Error : test_Basics failed - 5 " +
		   "exception should have been thrown here");

    }
    catch (UnknownHostException e) {
      harness.fail("Error : test_Basics failed - 6 " +
		   "Unknown host exception should not have been thrown here");
      harness.debug(e);
    }
    catch (ConnectException e) {
      harness.check(true);
    }
    catch (IOException e) {
      harness.fail("Error : test_Basics failed - 6 " +
                   "ConnectException should have been thrown here");
      harness.debug(e);
    }
    finally {
      try {
	if (s != null)
	  s.close();
      } catch(IOException ignored) {}
    }

    // host inet given
    try {
      // This is host / port that is unlikely to be blocked.  (Outgoing
      // port 80 connections are often blocked.)
      s = new Socket (harness.getMailHost(), 25);
      harness.check(true);
    }
    catch (Exception e) {
      harness.fail("Error : test_Basics failed - 7 " +
		   "exception should not have been thrown.");
      harness.debug(e);
    }
    finally {
      try {
	if (s != null)
	  s.close();
      } catch(IOException ignored) {}
    }

    try {
      s = new Socket(InetAddress.getLocalHost(), 30002);
      harness.fail("Error : test_Basics failed - 8 " +
		   "exception should have been thrown here");
    }
    catch (ConnectException e) {
      harness.check(true);
    }
    catch (IOException e) {
      harness.fail("Error : test_Basics failed - 8 " +
                   "ConnectException should have been thrown here");
      harness.debug(e);
    }
    finally {
      try {
	if (s != null)
	  s.close();
      } catch(IOException ignored) {}
    }

    if (true) { // 1.1 features not implemented
      
      // src socket target socket given(as hostname).
      try {
	s = new Socket ("babuspdjflks.gnu.org.", 200,
			InetAddress.getLocalHost() ,20006);
	harness.fail("Error : test_Basics failed - 9 " +
		     " exception should have been thrown here");
      }
      catch (UnknownHostException e) {
	harness.check(true);
      }
      catch (IOException e) {
	harness.fail("Error : test_Basics failed - 10 " +
		     "UnknownHostException should have been thrown here");
	harness.debug(e);
      }
      finally {
	try {
	  if (s != null)
	    s.close();
	} catch(IOException ignored) {}
      }
      
      try {
	s = new Socket("127.0.0.1", 30003,
		       InetAddress.getLocalHost(), 20007);
	harness.fail("Error : test_Basics failed - 11 " +
		     " exception should have been thrown here");
      }
      catch (UnknownHostException e) {
	harness.fail("Error : test_Basics failed - 12 " +
		     "UnknownHostException should not have been thrown");
	harness.debug(e);
      }
      catch (IOException e) {
	harness.check(true);
      }
      finally {
	try {
	  if (s != null)
	    s.close();
	} catch(IOException ignored) {}
      }
      
      // src socket target socket given (as ip address).
      try {
	s = new Socket(InetAddress.getLocalHost(), 30004,
		       InetAddress.getLocalHost(), 20008);
	harness.fail("Error : test_Basics failed - 13 " +
		     " exception should have been thrown here");
      }
      catch (UnknownHostException e) {
	harness.fail("Error : test_Basics failed - 14 " +
		     "Unknown host exception should not have been thrown");
	harness.debug(e);
      }
      catch (IOException e) {
	harness.check(true);
      }
      finally {
	try {
	  if (s != null)
	    s.close();
	} catch(IOException ignored) {}
      }
    }
  }

  public void test_BasicBServer()
  {
    harness.checkPoint("BasicBServer");
    SocketBServer srv = new SocketBServer();
    SocketBServer.harness = harness;
    srv.init();
    srv.start();
    Thread.yield();

    Socket sock = null;
    try {
      sock = new Socket("127.0.0.1", 20002);
      InputStream is = sock.getInputStream();

      DataInputStream dis = new DataInputStream(is);

      String str = dis.readLine();

      harness.check(str.equals("hello buddy"),
		    "Error : test_BasicServer failed - 1 " +
		    "string returned is not correct.");
      harness.check(true);
    }
    catch (Exception e) {
      harness.fail("Error : test_BasicServer failed - 2 exception was thrown");
      harness.debug(e);
    }
    finally {
      try {
	if (sock != null)
	  sock.close();
      } catch (IOException ignored) {}
    }
  }

  public void test_closed()
  {
    harness.checkPoint("closed");
    Socket sock = null;
    
    try
      {
	sock = new Socket();
	sock.close();
	sock.setSoTimeout(1000);
	harness.fail("exception expected");
      }
    catch (SocketException e)
      {
	harness.check(e.getMessage().compareToIgnoreCase("socket is closed") == 0,
		      "wrong SocketException error message: " + e.getMessage());
      }
    catch (Exception e)
      {
	harness.fail("wrong exception thrown");
      }
  }


  public void testall()
  {
    test_Basics();
    test_params();
    test_BasicServer();
    test_BasicBServer();
    test_closed();
  }

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