1 /* -*- Mode: Java; tab-width: 4 -*-
3 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 Change History (most recent first):
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
23 Revision 1.5 2006/06/20 23:01:58 rpantos
24 <rdar://problem/3839132> Java needs to implement DNSServiceRegisterRecord equivalent
26 Revision 1.4 2004/08/04 01:07:43 rpantos
27 Update unit test for <rdar://problems/3731579&3731582>.
29 Revision 1.3 2004/05/26 01:41:58 cheshire
30 Pass proper flags to DNSSD.enumerateDomains
32 Revision 1.2 2004/04/30 21:53:34 rpantos
33 Change line endings for CVS.
35 Revision 1.1 2004/04/30 16:29:35 rpantos
38 DNSSDUnitTest is a simple program that exercises parts of the DNSSD API.
41 import com
.apple
.dnssd
.*;
49 public static final String TEST_TYPE
= "_unittest._udp";
50 public static final String WIRE_CHAR_SET
= "ISO-8859-1";
52 public DNSSDUnitTest fInstance
= null;
54 public DNSSDUnitTest() throws Exception
59 Enumeration en
= NetworkInterface
.getNetworkInterfaces();
60 while ( en
.hasMoreElements())
61 System
.out
.println( ((NetworkInterface
) en
.nextElement()).getName());
64 public void testTxtRecord()
66 byte[] src
= { 6, 'a', 't', '=', 'X', 'Y', 'Z' };
67 TXTRecord txtRecord
= new TXTRecord( src
);
70 txtRecord
.set( "path", "~/names");
71 txtRecord
.set( "rw", (String
) null);
72 txtRecord
.set( "empty", "");
73 txtRecord
.set( "ttl", "4");
75 byte[] rawBytes
= txtRecord
.getRawBytes();
76 System
.out
.println( ( new String( rawBytes
, 0, rawBytes
.length
)) + " has count " +
77 String
.valueOf( txtRecord
.size()));
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"));
85 txtRecord
.set( "path", "~/numbers");
86 System
.out
.println( txtRecord
);
88 txtRecord
.remove( "ttl");
89 System
.out
.println( txtRecord
);
91 txtRecord
.remove( "path");
92 System
.out
.println( txtRecord
);
94 txtRecord
.remove( "at");
95 System
.out
.println( txtRecord
);
97 txtRecord
.set( "rw", "1");
98 System
.out
.println( txtRecord
);
101 public void run() throws DNSSDException
103 System
.out
.println( "Running DNSSD unit test for " + System
.getProperty( "user.name"));
105 this.testTxtRecord();
107 fRegTest
= new RegTest();
115 protected int fStage
;
116 protected RegTest fRegTest
;
118 public synchronized void bumpStage()
124 protected synchronized void waitForEnd()
127 while ( stage
== fStage
)
131 } catch (InterruptedException e
) {}
135 public static void main(String s
[])
138 new DNSSDUnitTest().run();
140 catch ( Exception e
) { terminateWithException( e
); }
143 protected static void terminateWithException( Exception e
)
150 class TermReporter
implements BaseListener
152 public void operationFailed( DNSSDService service
, int errorCode
)
154 System
.out
.println( this.getClass().getName() + " encountered error " + String
.valueOf( errorCode
));
157 protected void finalize() throws Throwable
159 System
.out
.println( "Instance of " + this.getClass().getName() + " has been destroyed");
163 class RegTest
extends TermReporter
implements RegisterListener
165 public static final int TEST_PORT
= 5678;
167 public RegTest() throws DNSSDException
169 fReg
= DNSSD
.register( 0, 0, "Test service", DNSSDUnitTest
.TEST_TYPE
, "", "", TEST_PORT
, null, this);
172 public void serviceRegistered( DNSSDRegistration registration
, int flags
, String serviceName
,
173 String regType
, String domain
)
175 String s
= "RegTest result flags:" + String
.valueOf( flags
) +
176 " serviceName:" + serviceName
+ " regType:" + regType
+ " domain:" + domain
;
177 System
.out
.println( s
);
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(); }
188 protected DNSSDRegistration fReg
;
191 class DupRegTest
extends TermReporter
implements RegisterListener
193 public static final int TEST_PORT
= 5678;
195 public DupRegTest() throws DNSSDException
197 DNSSD
.register( DNSSD
.NO_AUTO_RENAME
| DNSSD
.UNIQUE
, 0, "Test service", DNSSDUnitTest
.TEST_TYPE
, "", "", TEST_PORT
+ 1, null, this);
200 public void serviceRegistered( DNSSDRegistration registration
, int flags
, String serviceName
,
201 String regType
, String domain
)
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
);
210 class BrowseTest
extends TermReporter
implements BrowseListener
215 DNSSD
.browse( 0, 0, DNSSDUnitTest
.TEST_TYPE
, "", this);
216 } catch( Exception e
) { e
.printStackTrace(); }
219 public void serviceFound( DNSSDService browser
, int flags
, int ifIndex
,
220 String serviceName
, String regType
, String domain
)
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
);
227 System
.out
.println( "Resolving " + serviceName
);
228 new ResolveTest( 0, ifIndex
, serviceName
, regType
, domain
);
231 public void serviceLost( DNSSDService browser
, int flags
, int ifIndex
,
232 String serviceName
, String regType
, String domain
)
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
);
240 public void operationFailed( DNSSDService service
, int errorCode
)
242 System
.out
.println( "Browse failed " + String
.valueOf( errorCode
));
246 class DomainTest
extends TermReporter
implements DomainListener
251 DNSSD
.enumerateDomains( DNSSD
.BROWSE_DOMAINS
, 0, this);
252 } catch( Exception e
) { e
.printStackTrace(); }
255 public void domainFound( DNSSDService enumerator
, int flags
, int ifIndex
, String domain
)
257 String s
= "Domain found flags:" + String
.valueOf( flags
) +
258 " ifIndex:" + String
.valueOf( ifIndex
) +
260 System
.out
.println( s
);
263 public void domainLost( DNSSDService enumerator
, int flags
, int ifIndex
, String domain
)
265 String s
= "Domain lost flags:" + String
.valueOf( flags
) +
266 " ifIndex:" + String
.valueOf( ifIndex
) +
268 System
.out
.println( s
);
271 public void operationFailed( DNSSDService service
, int errorCode
)
273 System
.out
.println( "Domain enum op failed " + String
.valueOf( errorCode
));
277 class ResolveTest
extends TermReporter
implements ResolveListener
279 public ResolveTest( int flags
, int ifIndex
, String serviceName
, String regType
,
283 DNSSD
.resolve( flags
, ifIndex
, serviceName
, regType
, domain
, this);
284 } catch( Exception e
) { e
.printStackTrace(); }
287 public void serviceResolved( DNSSDService resolver
, int flags
, int ifIndex
, String fullName
,
288 String hostName
, int port
, TXTRecord txtRecord
)
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
);
297 System
.out
.println( s
);
299 System
.out
.println( "Querying " + hostName
);
300 new QueryTest( 0, ifIndex
, hostName
, 1 /* ns_t_a */, 1 /* ns_c_in */);
304 class QueryTest
extends TermReporter
implements QueryListener
306 public QueryTest( int flags
, int ifIndex
, String serviceName
, int rrtype
, int rrclass
)
309 DNSSD
.queryRecord( flags
, ifIndex
, serviceName
, rrtype
, rrclass
, this);
310 } catch( Exception e
) { e
.printStackTrace(); }
313 public void queryAnswered( DNSSDService query
, int flags
, int ifIndex
, String fullName
,
314 int rrtype
, int rrclass
, byte[] rdata
, int ttl
)
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
);
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(); }
329 class RegistrarTest
extends TermReporter
implements RegisterRecordListener
331 public RegistrarTest()
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(); }
341 public void recordRegistered( DNSRecord record
, int flags
)
343 String s
= "RegistrarTest result flags:" + String
.valueOf( flags
);
344 System
.out
.println( s
);
347 byte[] kResponsiblePerson
= { 'e','l','m','o' };
348 record
.update( 0, kResponsiblePerson
, 3600);
350 } catch( Exception e
) { e
.printStackTrace(); }
353 protected DNSSDRecordRegistrar fRegistrar
;