File: MulticastSocketTest.java

package info (click to toggle)
mauve 20161030-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 44,628 kB
  • ctags: 35,425
  • sloc: java: 336,555; sh: 2,834; xml: 208; makefile: 72
file content (250 lines) | stat: -rw-r--r-- 6,806 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
// Tags: JDK1.1
// Uses: MulticastServer MulticastClient

/*
   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.MulticastSocket;
import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;
import java.net.*;
import java.io.*;


public class MulticastSocketTest implements Testlet
{
  protected static TestHarness harness;
    public void test_Basics()
    {
	MulticastSocket     socket;
	int                 nPort = 0;
	
	// Test for incorrect ipaddress, port and a port in use.

	try {
	    socket = new MulticastSocket(4441);
	    InetAddress address = InetAddress.getByName("15.0.0.1");
	    socket.joinGroup(address);
	    harness.fail("Wrong ipaddress arg. - 1");
	} catch (IOException e) {
	    harness.check(true);
	}


	try {
	    socket = new MulticastSocket(-1);
	    harness.fail("Wrong port arg. - 2");
	    InetAddress address = InetAddress.getByName("230.0.0.1");
	    socket.joinGroup(address);

	} catch (Exception e) {
	    harness.check(true);
	}
	

	try {
	    socket = new MulticastSocket(0);
	    InetAddress address = InetAddress.getByName("230.0.0.1");
	    socket.joinGroup(address);
	    socket.leaveGroup(address);
	    socket.close();
	    harness.check(true);
	} catch (Exception e) {   
	    harness.fail("Correct args. - 3"); 
	    harness.debug(e);
	}
	

	try {
	    socket = new MulticastSocket();
	    InetAddress address = InetAddress.getByName("230.0.0.1");
	    nPort = socket.getLocalPort();
	    socket.joinGroup(address);
	    socket.leaveGroup(address);
	    socket.close();
	    harness.check(true);
	} catch (Exception e) { 
	    harness.fail("Correct args. different constructor. - 4");
	    harness.debug(e);
	}
	
	
	try {
	    socket = new MulticastSocket(nPort);
	    InetAddress address = InetAddress.getByName("230.0.0.1");
	    socket.joinGroup(address);
	    socket.joinGroup(address);
	    harness.fail("joinGroup() twice.");
	} catch (Exception e) {
	    harness.check(true);
	}

	try {
	    socket = new MulticastSocket(++nPort);
	    InetAddress address = InetAddress.getByName("230.0.0.1");
	    socket.joinGroup(null);
	    harness.fail("joinGroup() with incorrect params. - 5");
	} catch (NullPointerException e) {
	    harness.check(true);
	}
	catch(Exception e) {
	    harness.fail("joinGroup() with incorrect params. should have " +
		"thrown a NullPointerException - 5a");
	    harness.debug(e);
	}
	

	try {
	    socket = new MulticastSocket(++nPort);
	    InetAddress address = InetAddress.getByName("230.0.0.1");
	    socket.joinGroup(address);
	    socket.leaveGroup(address);
	    harness.check(true);
	    socket.leaveGroup(address);
	    harness.fail("leaveGroup() twice. - 6");
	    socket.close();
	} catch (Exception e) {
	    harness.check(true);
	}


	try {
	    socket = new MulticastSocket(++nPort);
	    InetAddress address = InetAddress.getByName("230.0.0.1");
	    socket.joinGroup(address);
	    socket.leaveGroup(null);
	    harness.fail("leaveGroup() with incorrect params - 7");
	    socket.close();
	} catch (NullPointerException e) {
	    harness.check(true);
	} catch (Exception e) {
	    harness.fail("leaveGroup() with incorrect params. should have " +
		"thrown a NullPointerException - 7a");
	    harness.debug(e);
	}
	

	try {
	    socket = new MulticastSocket(++nPort);
	    InetAddress address = InetAddress.getByName("230.0.0.1");
	    socket.joinGroup(address);
	    socket.leaveGroup(address);
	    socket.close();
	    harness.check(true);
	} catch (Exception e) { 
	    harness.fail("Correct args. - 8");
	    harness.debug(e);
	}
	
	
	try {
	    //	    System.out.println("getTTL() and setTTL().");
	    socket = new MulticastSocket(++nPort);
	    InetAddress address = InetAddress.getByName("230.0.0.1");
	    socket.joinGroup(address);
	    byte bTTL = socket.getTTL();
	    harness.check(bTTL != 0, "getTTL() should never return zero - 9");
	    
	    //System.out.println("Default TTL = " + bTTL);
	    byte newbTTL = (byte)127;
	    socket.setTTL(newbTTL);
	    bTTL = socket.getTTL();
	    //System.out.println("New TTL = " + bTTL);
	    harness.check(bTTL, newbTTL,
	      "getTTL() should return same value (127) used for setTTL() - 10");
	    
	    bTTL = (byte)-56;
	    socket.setTTL(bTTL);
	    bTTL = socket.getTTL();
	    //System.out.println("Newer TTL = " + bTTL);
	    // FIXME: if unsigned byte is used -56 will roll to a +ve value.
	    // Developer should verify if this is a failure case or not.
	    //if(bTTL == -56)
	    //System.out.println("FAIL : TTL cannot be negative");
	    
	    socket.setTTL((byte)1);
	    socket.leaveGroup(address);
	    socket.close();
	    harness.check(true);
	} catch (Exception e) {
	    harness.fail("Should not have thrown any exception - 11");
	    harness.debug(e);
	}
	
	    
    }

    public void test_MultipleBind() {
	
	final int sharedMcastPort = 1234;
	
        // First Socket
	
        MulticastSocket firstMcastSock;
        try {
	    firstMcastSock = new MulticastSocket(sharedMcastPort);
	    harness.check(true);
        }
        catch (Exception e) {
	    harness.fail("could not create FIRST multicast socket on shared port " + sharedMcastPort);
	    harness.debug(e);
        }
	
        // Second Socket
	
        MulticastSocket secondMcastSock;
        try {
	    secondMcastSock = new MulticastSocket(sharedMcastPort);
	    harness.check(true);
        }
        catch (Exception e) {
            harness.fail("could not create SECOND multicast socket on shared port " + sharedMcastPort);
            harness.debug(e);
        }
    }
	

    public void test_Comm(){
	try {
                        MulticastClient client = new MulticastClient();
                        client.start();
			MulticastServer server = new MulticastServer(4446);
			server.start();
			harness.check(true);
	}catch(Exception e){
			harness.fail("test_Comm failed");
			harness.debug(e);
	}
    }
	
    public void testall()
    {
	test_Basics();
       test_MultipleBind();
	test_Comm();
    }

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