summaryrefslogtreecommitdiffstats
path: root/mDNSResponder/Clients/Java/DNSSDUnitTest.java
blob: 2b1839a03fc83cc791366cdd5931951af49b1752 (plain) (blame)
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
/* -*- Mode: Java; tab-width: 4 -*-
 *
 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * DNSSDUnitTest is a simple program that exercises parts of the DNSSD API.
 */

import com.apple.dnssd.*;

import java.net.*;
import java.util.*;


class	DNSSDUnitTest
{
	public static final String	TEST_TYPE = "_unittest._udp";
	public static final String	WIRE_CHAR_SET = "ISO-8859-1";

	public DNSSDUnitTest	fInstance = null;

	public		DNSSDUnitTest() throws Exception
	{
		fStage = 0;
		fInstance = this;
		
		Enumeration	en = NetworkInterface.getNetworkInterfaces();
		while ( en.hasMoreElements())
			System.out.println( ((NetworkInterface) en.nextElement()).getName());
	}

	public void	testTxtRecord()
	{
		byte[]		src = { 6, 'a', 't', '=', 'X', 'Y', 'Z' };
		TXTRecord	txtRecord = new TXTRecord( src);
		String		a;
		
		txtRecord.set( "path", "~/names");
		txtRecord.set( "rw", (String) null);
		txtRecord.set( "empty", "");
		txtRecord.set( "ttl", "4");

		byte[]	rawBytes = txtRecord.getRawBytes();
		System.out.println( ( new String( rawBytes, 0, rawBytes.length)) + " has count " + 
								String.valueOf( txtRecord.size()));

		System.out.println( txtRecord);
		boolean		ttlPresent = txtRecord.contains( "ttl");
		System.out.println( "ttl is present: " + ( ttlPresent ? "true" : "false"));
		boolean		timeoutPresent = txtRecord.contains( "timeout");
		System.out.println( "timeout is present: " + ( timeoutPresent ? "true" : "false"));

		txtRecord.set( "path", "~/numbers");
		System.out.println( txtRecord);

		txtRecord.remove( "ttl");
		System.out.println( txtRecord);

		txtRecord.remove( "path");
		System.out.println( txtRecord);

		txtRecord.remove( "at");
		System.out.println( txtRecord);

		txtRecord.set( "rw", "1");
		System.out.println( txtRecord);
	}

	public void	run() throws DNSSDException
	{
		System.out.println( "Running DNSSD unit test for " + System.getProperty( "user.name"));

		this.testTxtRecord();

		fRegTest = new RegTest();
		new BrowseTest();
		new DomainTest();
		new RegistrarTest();
		
		this.waitForEnd();
	}

	protected int		fStage;
	protected RegTest	fRegTest;

	public synchronized void bumpStage()
	{
		fStage++;
		this.notifyAll();
	}

	protected synchronized void waitForEnd()
	{
		int stage = fStage;
		while ( stage == fStage)
		{
			try {
				wait();
			} catch (InterruptedException e) {}
		}
	}

    public static void main(String s[]) 
    {
    	try {
			new DNSSDUnitTest().run();
		}
		catch ( Exception e) { terminateWithException( e); }
    }

	protected static void	terminateWithException( Exception e)
	{
		e.printStackTrace();
		System.exit( -1);
	}
}

class	TermReporter implements BaseListener
{
	public void	operationFailed( DNSSDService service, int errorCode)
	{
		System.out.println( this.getClass().getName() + " encountered error " + String.valueOf( errorCode));
	}

	protected void finalize() throws Throwable
	{
		System.out.println( "Instance of " + this.getClass().getName() + " has been destroyed");
	}
}

class	RegTest extends TermReporter implements RegisterListener
{
	public static final int		TEST_PORT = 5678;

	public		RegTest() throws DNSSDException
	{
		fReg = DNSSD.register( 0, 0, "Test service", DNSSDUnitTest.TEST_TYPE, "", "", TEST_PORT, null, this);
	}

	public void	serviceRegistered( DNSSDRegistration registration, int flags, String serviceName, 
								String regType, String domain)
	{
		String s = "RegTest result flags:" + String.valueOf( flags) + 
					" serviceName:" + serviceName + " regType:" + regType + " domain:" + domain;
		System.out.println( s);

		try {
			new DupRegTest();
			
			byte[]	kResponsiblePerson = { 'c','o','o','k','i','e',' ','m','o','n','s','t','e','r' };
			fReg.addRecord( 0, 17 /*ns_t_rp*/, kResponsiblePerson, 3600);
			new QueryTest( 0, 0, "Test service", 17 /*ns_t_rp*/, 1);
		} catch( Exception e) { e.printStackTrace(); }
	}

	protected DNSSDRegistration	fReg;
}

class	DupRegTest extends TermReporter implements RegisterListener
{
	public static final int		TEST_PORT = 5678;

	public		DupRegTest() throws DNSSDException
	{
		DNSSD.register( DNSSD.NO_AUTO_RENAME | DNSSD.UNIQUE, 0, "Test service", DNSSDUnitTest.TEST_TYPE, "", "", TEST_PORT + 1, null, this);
	}

	public void	serviceRegistered( DNSSDRegistration registration, int flags, String serviceName, 
								String regType, String domain)
	{
		System.out.println( "Oik - registered a duplicate!");
		String s = "DupRegTest result flags:" + String.valueOf( flags) + 
					" serviceName:" + serviceName + " regType:" + regType + " domain:" + domain;
		System.out.println( s);
	}
}

class	BrowseTest extends TermReporter implements BrowseListener
{
	public		BrowseTest()
	{
		try {
			DNSSD.browse( 0, 0, DNSSDUnitTest.TEST_TYPE, "", this);
		} catch( Exception e) { e.printStackTrace(); }
	}

	public void	serviceFound( DNSSDService browser, int flags, int ifIndex, 
							String serviceName, String regType, String domain)
	{
		String s = "BrowseTest found flags:" + String.valueOf( flags) + 
					" ifIndex:" + String.valueOf( ifIndex) + 
					" serviceName:" + serviceName + " regType:" + regType + " domain:" + domain;
		System.out.println( s);
		
		System.out.println( "Resolving " + serviceName);
		new ResolveTest( 0, ifIndex, serviceName, regType, domain);
	}

	public void	serviceLost( DNSSDService browser, int flags, int ifIndex,
							String serviceName, String regType, String domain)
	{
		String s = "BrowseTest lost flags:" + String.valueOf( flags) + 
					" ifIndex:" + String.valueOf( ifIndex) + 
					" serviceName:" + serviceName + " regType:" + regType + " domain:" + domain;
		System.out.println( s);
	}

	public void	operationFailed( DNSSDService service, int errorCode)
	{
		System.out.println( "Browse failed " + String.valueOf( errorCode));
	}
}

class	DomainTest extends TermReporter implements DomainListener
{
	public		DomainTest()
	{
		try {
			DNSSD.enumerateDomains( DNSSD.BROWSE_DOMAINS, 0, this);
		} catch( Exception e) { e.printStackTrace(); }
	}

	public void	domainFound( DNSSDService enumerator, int flags, int ifIndex, String domain)
	{
		String s = "Domain found flags:" + String.valueOf( flags) + 
					" ifIndex:" + String.valueOf( ifIndex) + 
					" domain:" + domain;
		System.out.println( s);
	}

	public void	domainLost( DNSSDService enumerator, int flags, int ifIndex, String domain)
	{
		String s = "Domain lost flags:" + String.valueOf( flags) + 
					" ifIndex:" + String.valueOf( ifIndex) + 
					" domain:" + domain;
		System.out.println( s);
	}

	public void	operationFailed( DNSSDService service, int errorCode)
	{
		System.out.println( "Domain enum op failed " + String.valueOf( errorCode));
	}
}

class	ResolveTest extends TermReporter implements ResolveListener
{
	public		ResolveTest( int flags, int ifIndex, String serviceName, String regType, 
							String domain)
	{
		try {
			DNSSD.resolve( flags, ifIndex, serviceName, regType, domain, this);
		} catch( Exception e) { e.printStackTrace(); }
	}

	public void	serviceResolved( DNSSDService resolver, int flags, int ifIndex, String fullName, 
								String hostName, int port, TXTRecord txtRecord)
	{
		String a;
		String s = "ResolveTest result flags:" + String.valueOf( flags) + 
					" ifIndex:" + String.valueOf( ifIndex) + 
					" fullName:" + fullName + " hostName:" + hostName + " port:" + String.valueOf( port);
		for ( int i=0; null != ( a = txtRecord.getKey( i)); i++)
			s += " attr/val " + String.valueOf( i) + ": " + a + "," + txtRecord.getValueAsString( i);

		System.out.println( s);

		System.out.println( "Querying " + hostName);
		new QueryTest( 0, ifIndex, hostName, 1 /* ns_t_a */, 1 /* ns_c_in */);
	}
}

class	QueryTest extends TermReporter implements QueryListener
{
	public		QueryTest( int flags, int ifIndex, String serviceName, int rrtype, int rrclass)
	{
		try {
			DNSSD.queryRecord( flags, ifIndex, serviceName, rrtype, rrclass, this);
		} catch( Exception e) { e.printStackTrace(); }
	}

	public void	queryAnswered( DNSSDService query, int flags, int ifIndex, String fullName, 
								int rrtype, int rrclass, byte[] rdata, int ttl)
	{
		String s = "QueryTest result flags:" + String.valueOf( flags) + 
					" ifIndex:" + String.valueOf( ifIndex) + 
					" fullName:" + fullName + " rrtype:" + String.valueOf( rrtype) + 
					" rrclass:" + String.valueOf( rrclass) + " ttl:" + String.valueOf( ttl);
		System.out.println( s);

		try {
			String	dataTxt = new String( rdata, 0, rdata.length, DNSSDUnitTest.WIRE_CHAR_SET);
			System.out.println( "Query data is:" + dataTxt);
		} catch( Exception e) { e.printStackTrace(); }
	}
}

class	RegistrarTest extends TermReporter implements RegisterRecordListener
{
	public		RegistrarTest()
	{
		try {
			byte[]	kResponsiblePerson = { 'g','r','o','v','e','r' };
			fRegistrar = DNSSD.createRecordRegistrar( this);
			fRegistrar.registerRecord( DNSSD.UNIQUE, 0,
					"test.registrartest.local", 17 /*ns_t_rp*/, 1, kResponsiblePerson, 3600);
		} catch( Exception e) { e.printStackTrace(); }
	}

	public void	recordRegistered( DNSRecord record, int flags)
	{
		String s = "RegistrarTest result flags:" + String.valueOf( flags);
		System.out.println( s);

		try {
			byte[]	kResponsiblePerson = { 'e','l','m','o' };
			record.update( 0, kResponsiblePerson, 3600);
			record.remove();
		} catch( Exception e) { e.printStackTrace(); }
	}

	protected DNSSDRecordRegistrar	fRegistrar;
}