]> git.saurik.com Git - apple/mdnsresponder.git/blob - Clients/Java/DNSSDUnitTest.java
mDNSResponder-107.6.tar.gz
[apple/mdnsresponder.git] / Clients / Java / DNSSDUnitTest.java
1 /* -*- Mode: Java; tab-width: 4 -*-
2 *
3 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16
17 Change History (most recent first):
18
19 $Log: DNSSDUnitTest.java,v $
20 Revision 1.6 2006/08/14 23:24:07 cheshire
21 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
22
23 Revision 1.5 2006/06/20 23:01:58 rpantos
24 <rdar://problem/3839132> Java needs to implement DNSServiceRegisterRecord equivalent
25
26 Revision 1.4 2004/08/04 01:07:43 rpantos
27 Update unit test for <rdar://problems/3731579&3731582>.
28
29 Revision 1.3 2004/05/26 01:41:58 cheshire
30 Pass proper flags to DNSSD.enumerateDomains
31
32 Revision 1.2 2004/04/30 21:53:34 rpantos
33 Change line endings for CVS.
34
35 Revision 1.1 2004/04/30 16:29:35 rpantos
36 First checked in.
37
38 DNSSDUnitTest is a simple program that exercises parts of the DNSSD API.
39 */
40
41 import com.apple.dnssd.*;
42
43 import java.net.*;
44 import java.util.*;
45
46
47 class DNSSDUnitTest
48 {
49 public static final String TEST_TYPE = "_unittest._udp";
50 public static final String WIRE_CHAR_SET = "ISO-8859-1";
51
52 public DNSSDUnitTest fInstance = null;
53
54 public DNSSDUnitTest() throws Exception
55 {
56 fStage = 0;
57 fInstance = this;
58
59 Enumeration en = NetworkInterface.getNetworkInterfaces();
60 while ( en.hasMoreElements())
61 System.out.println( ((NetworkInterface) en.nextElement()).getName());
62 }
63
64 public void testTxtRecord()
65 {
66 byte[] src = { 6, 'a', 't', '=', 'X', 'Y', 'Z' };
67 TXTRecord txtRecord = new TXTRecord( src);
68 String a;
69
70 txtRecord.set( "path", "~/names");
71 txtRecord.set( "rw", (String) null);
72 txtRecord.set( "empty", "");
73 txtRecord.set( "ttl", "4");
74
75 byte[] rawBytes = txtRecord.getRawBytes();
76 System.out.println( ( new String( rawBytes, 0, rawBytes.length)) + " has count " +
77 String.valueOf( txtRecord.size()));
78
79 System.out.println( txtRecord);
80 boolean ttlPresent = txtRecord.contains( "ttl");
81 System.out.println( "ttl is present: " + ( ttlPresent ? "true" : "false"));
82 boolean timeoutPresent = txtRecord.contains( "timeout");
83 System.out.println( "timeout is present: " + ( timeoutPresent ? "true" : "false"));
84
85 txtRecord.set( "path", "~/numbers");
86 System.out.println( txtRecord);
87
88 txtRecord.remove( "ttl");
89 System.out.println( txtRecord);
90
91 txtRecord.remove( "path");
92 System.out.println( txtRecord);
93
94 txtRecord.remove( "at");
95 System.out.println( txtRecord);
96
97 txtRecord.set( "rw", "1");
98 System.out.println( txtRecord);
99 }
100
101 public void run() throws DNSSDException
102 {
103 System.out.println( "Running DNSSD unit test for " + System.getProperty( "user.name"));
104
105 this.testTxtRecord();
106
107 fRegTest = new RegTest();
108 new BrowseTest();
109 new DomainTest();
110 new RegistrarTest();
111
112 this.waitForEnd();
113 }
114
115 protected int fStage;
116 protected RegTest fRegTest;
117
118 public synchronized void bumpStage()
119 {
120 fStage++;
121 this.notifyAll();
122 }
123
124 protected synchronized void waitForEnd()
125 {
126 int stage = fStage;
127 while ( stage == fStage)
128 {
129 try {
130 wait();
131 } catch (InterruptedException e) {}
132 }
133 }
134
135 public static void main(String s[])
136 {
137 try {
138 new DNSSDUnitTest().run();
139 }
140 catch ( Exception e) { terminateWithException( e); }
141 }
142
143 protected static void terminateWithException( Exception e)
144 {
145 e.printStackTrace();
146 System.exit( -1);
147 }
148 }
149
150 class TermReporter implements BaseListener
151 {
152 public void operationFailed( DNSSDService service, int errorCode)
153 {
154 System.out.println( this.getClass().getName() + " encountered error " + String.valueOf( errorCode));
155 }
156
157 protected void finalize() throws Throwable
158 {
159 System.out.println( "Instance of " + this.getClass().getName() + " has been destroyed");
160 }
161 }
162
163 class RegTest extends TermReporter implements RegisterListener
164 {
165 public static final int TEST_PORT = 5678;
166
167 public RegTest() throws DNSSDException
168 {
169 fReg = DNSSD.register( 0, 0, "Test service", DNSSDUnitTest.TEST_TYPE, "", "", TEST_PORT, null, this);
170 }
171
172 public void serviceRegistered( DNSSDRegistration registration, int flags, String serviceName,
173 String regType, String domain)
174 {
175 String s = "RegTest result flags:" + String.valueOf( flags) +
176 " serviceName:" + serviceName + " regType:" + regType + " domain:" + domain;
177 System.out.println( s);
178
179 try {
180 new DupRegTest();
181
182 byte[] kResponsiblePerson = { 'c','o','o','k','i','e',' ','m','o','n','s','t','e','r' };
183 fReg.addRecord( 0, 17 /*ns_t_rp*/, kResponsiblePerson, 3600);
184 new QueryTest( 0, 0, "Test service", 17 /*ns_t_rp*/, 1);
185 } catch( Exception e) { e.printStackTrace(); }
186 }
187
188 protected DNSSDRegistration fReg;
189 }
190
191 class DupRegTest extends TermReporter implements RegisterListener
192 {
193 public static final int TEST_PORT = 5678;
194
195 public DupRegTest() throws DNSSDException
196 {
197 DNSSD.register( DNSSD.NO_AUTO_RENAME | DNSSD.UNIQUE, 0, "Test service", DNSSDUnitTest.TEST_TYPE, "", "", TEST_PORT + 1, null, this);
198 }
199
200 public void serviceRegistered( DNSSDRegistration registration, int flags, String serviceName,
201 String regType, String domain)
202 {
203 System.out.println( "Oik - registered a duplicate!");
204 String s = "DupRegTest result flags:" + String.valueOf( flags) +
205 " serviceName:" + serviceName + " regType:" + regType + " domain:" + domain;
206 System.out.println( s);
207 }
208 }
209
210 class BrowseTest extends TermReporter implements BrowseListener
211 {
212 public BrowseTest()
213 {
214 try {
215 DNSSD.browse( 0, 0, DNSSDUnitTest.TEST_TYPE, "", this);
216 } catch( Exception e) { e.printStackTrace(); }
217 }
218
219 public void serviceFound( DNSSDService browser, int flags, int ifIndex,
220 String serviceName, String regType, String domain)
221 {
222 String s = "BrowseTest found flags:" + String.valueOf( flags) +
223 " ifIndex:" + String.valueOf( ifIndex) +
224 " serviceName:" + serviceName + " regType:" + regType + " domain:" + domain;
225 System.out.println( s);
226
227 System.out.println( "Resolving " + serviceName);
228 new ResolveTest( 0, ifIndex, serviceName, regType, domain);
229 }
230
231 public void serviceLost( DNSSDService browser, int flags, int ifIndex,
232 String serviceName, String regType, String domain)
233 {
234 String s = "BrowseTest lost flags:" + String.valueOf( flags) +
235 " ifIndex:" + String.valueOf( ifIndex) +
236 " serviceName:" + serviceName + " regType:" + regType + " domain:" + domain;
237 System.out.println( s);
238 }
239
240 public void operationFailed( DNSSDService service, int errorCode)
241 {
242 System.out.println( "Browse failed " + String.valueOf( errorCode));
243 }
244 }
245
246 class DomainTest extends TermReporter implements DomainListener
247 {
248 public DomainTest()
249 {
250 try {
251 DNSSD.enumerateDomains( DNSSD.BROWSE_DOMAINS, 0, this);
252 } catch( Exception e) { e.printStackTrace(); }
253 }
254
255 public void domainFound( DNSSDService enumerator, int flags, int ifIndex, String domain)
256 {
257 String s = "Domain found flags:" + String.valueOf( flags) +
258 " ifIndex:" + String.valueOf( ifIndex) +
259 " domain:" + domain;
260 System.out.println( s);
261 }
262
263 public void domainLost( DNSSDService enumerator, int flags, int ifIndex, String domain)
264 {
265 String s = "Domain lost flags:" + String.valueOf( flags) +
266 " ifIndex:" + String.valueOf( ifIndex) +
267 " domain:" + domain;
268 System.out.println( s);
269 }
270
271 public void operationFailed( DNSSDService service, int errorCode)
272 {
273 System.out.println( "Domain enum op failed " + String.valueOf( errorCode));
274 }
275 }
276
277 class ResolveTest extends TermReporter implements ResolveListener
278 {
279 public ResolveTest( int flags, int ifIndex, String serviceName, String regType,
280 String domain)
281 {
282 try {
283 DNSSD.resolve( flags, ifIndex, serviceName, regType, domain, this);
284 } catch( Exception e) { e.printStackTrace(); }
285 }
286
287 public void serviceResolved( DNSSDService resolver, int flags, int ifIndex, String fullName,
288 String hostName, int port, TXTRecord txtRecord)
289 {
290 String a;
291 String s = "ResolveTest result flags:" + String.valueOf( flags) +
292 " ifIndex:" + String.valueOf( ifIndex) +
293 " fullName:" + fullName + " hostName:" + hostName + " port:" + String.valueOf( port);
294 for ( int i=0; null != ( a = txtRecord.getKey( i)); i++)
295 s += " attr/val " + String.valueOf( i) + ": " + a + "," + txtRecord.getValueAsString( i);
296
297 System.out.println( s);
298
299 System.out.println( "Querying " + hostName);
300 new QueryTest( 0, ifIndex, hostName, 1 /* ns_t_a */, 1 /* ns_c_in */);
301 }
302 }
303
304 class QueryTest extends TermReporter implements QueryListener
305 {
306 public QueryTest( int flags, int ifIndex, String serviceName, int rrtype, int rrclass)
307 {
308 try {
309 DNSSD.queryRecord( flags, ifIndex, serviceName, rrtype, rrclass, this);
310 } catch( Exception e) { e.printStackTrace(); }
311 }
312
313 public void queryAnswered( DNSSDService query, int flags, int ifIndex, String fullName,
314 int rrtype, int rrclass, byte[] rdata, int ttl)
315 {
316 String s = "QueryTest result flags:" + String.valueOf( flags) +
317 " ifIndex:" + String.valueOf( ifIndex) +
318 " fullName:" + fullName + " rrtype:" + String.valueOf( rrtype) +
319 " rrclass:" + String.valueOf( rrclass) + " ttl:" + String.valueOf( ttl);
320 System.out.println( s);
321
322 try {
323 String dataTxt = new String( rdata, 0, rdata.length, DNSSDUnitTest.WIRE_CHAR_SET);
324 System.out.println( "Query data is:" + dataTxt);
325 } catch( Exception e) { e.printStackTrace(); }
326 }
327 }
328
329 class RegistrarTest extends TermReporter implements RegisterRecordListener
330 {
331 public RegistrarTest()
332 {
333 try {
334 byte[] kResponsiblePerson = { 'g','r','o','v','e','r' };
335 fRegistrar = DNSSD.createRecordRegistrar( this);
336 fRegistrar.registerRecord( DNSSD.UNIQUE, 0,
337 "test.registrartest.local", 17 /*ns_t_rp*/, 1, kResponsiblePerson, 3600);
338 } catch( Exception e) { e.printStackTrace(); }
339 }
340
341 public void recordRegistered( DNSRecord record, int flags)
342 {
343 String s = "RegistrarTest result flags:" + String.valueOf( flags);
344 System.out.println( s);
345
346 try {
347 byte[] kResponsiblePerson = { 'e','l','m','o' };
348 record.update( 0, kResponsiblePerson, 3600);
349 record.remove();
350 } catch( Exception e) { e.printStackTrace(); }
351 }
352
353 protected DNSSDRecordRegistrar fRegistrar;
354 }
355