2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 Change History (most recent first):
25 $Log: DNSSDUnitTest.java,v $
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();
114 protected int fStage
;
115 protected RegTest fRegTest
;
117 public synchronized void bumpStage()
123 protected synchronized void waitForEnd()
126 while ( stage
== fStage
)
130 } catch (InterruptedException e
) {}
134 public static void main(String s
[])
137 new DNSSDUnitTest().run();
139 catch ( Exception e
) { terminateWithException( e
); }
142 protected static void terminateWithException( Exception e
)
149 class TermReporter
implements BaseListener
151 public void operationFailed( DNSSDService service
, int errorCode
)
153 System
.out
.println( this.getClass().getName() + " encountered error " + String
.valueOf( errorCode
));
156 protected void finalize() throws Throwable
158 System
.out
.println( "Instance of " + this.getClass().getName() + " has been destroyed");
162 class RegTest
extends TermReporter
implements RegisterListener
164 public static final int TEST_PORT
= 5678;
166 public RegTest() throws DNSSDException
168 fReg
= DNSSD
.register( 0, 0, "Test service", DNSSDUnitTest
.TEST_TYPE
, "", "", TEST_PORT
, null, this);
171 public void serviceRegistered( DNSSDRegistration registration
, int flags
, String serviceName
,
172 String regType
, String domain
)
174 String s
= "RegTest result flags:" + String
.valueOf( flags
) +
175 " serviceName:" + serviceName
+ " regType:" + regType
+ " domain:" + domain
;
176 System
.out
.println( s
);
181 byte[] kResponsiblePerson
= { 'c','o','o','k','i','e',' ','m','o','n','s','t','e','r' };
182 fReg
.addRecord( 0, 17 /*ns_t_rp*/, kResponsiblePerson
, 3600);
183 new QueryTest( 0, 0, "Test service", 17 /*ns_t_rp*/, 1);
184 } catch( Exception e
) { e
.printStackTrace(); }
187 protected DNSSDRegistration fReg
;
190 class DupRegTest
extends TermReporter
implements RegisterListener
192 public static final int TEST_PORT
= 5678;
194 public DupRegTest() throws DNSSDException
196 DNSSD
.register( DNSSD
.NO_AUTO_RENAME
| DNSSD
.UNIQUE
, 0, "Test service", DNSSDUnitTest
.TEST_TYPE
, "", "", TEST_PORT
+ 1, null, this);
199 public void serviceRegistered( DNSSDRegistration registration
, int flags
, String serviceName
,
200 String regType
, String domain
)
202 System
.out
.println( "Oik - registered a duplicate!");
203 String s
= "DupRegTest result flags:" + String
.valueOf( flags
) +
204 " serviceName:" + serviceName
+ " regType:" + regType
+ " domain:" + domain
;
205 System
.out
.println( s
);
209 class BrowseTest
extends TermReporter
implements BrowseListener
214 DNSSD
.browse( 0, 0, DNSSDUnitTest
.TEST_TYPE
, "", this);
215 } catch( Exception e
) { e
.printStackTrace(); }
218 public void serviceFound( DNSSDService browser
, int flags
, int ifIndex
,
219 String serviceName
, String regType
, String domain
)
221 String s
= "BrowseTest found flags:" + String
.valueOf( flags
) +
222 " ifIndex:" + String
.valueOf( ifIndex
) +
223 " serviceName:" + serviceName
+ " regType:" + regType
+ " domain:" + domain
;
224 System
.out
.println( s
);
226 System
.out
.println( "Resolving " + serviceName
);
227 new ResolveTest( 0, ifIndex
, serviceName
, regType
, domain
);
230 public void serviceLost( DNSSDService browser
, int flags
, int ifIndex
,
231 String serviceName
, String regType
, String domain
)
233 String s
= "BrowseTest lost flags:" + String
.valueOf( flags
) +
234 " ifIndex:" + String
.valueOf( ifIndex
) +
235 " serviceName:" + serviceName
+ " regType:" + regType
+ " domain:" + domain
;
236 System
.out
.println( s
);
239 public void operationFailed( DNSSDService service
, int errorCode
)
241 System
.out
.println( "Browse failed " + String
.valueOf( errorCode
));
245 class DomainTest
extends TermReporter
implements DomainListener
250 DNSSD
.enumerateDomains( DNSSD
.BROWSE_DOMAINS
, 0, this);
251 } catch( Exception e
) { e
.printStackTrace(); }
254 public void domainFound( DNSSDService enumerator
, int flags
, int ifIndex
, String domain
)
256 String s
= "Domain found flags:" + String
.valueOf( flags
) +
257 " ifIndex:" + String
.valueOf( ifIndex
) +
259 System
.out
.println( s
);
262 public void domainLost( DNSSDService enumerator
, int flags
, int ifIndex
, String domain
)
264 String s
= "Domain lost flags:" + String
.valueOf( flags
) +
265 " ifIndex:" + String
.valueOf( ifIndex
) +
267 System
.out
.println( s
);
270 public void operationFailed( DNSSDService service
, int errorCode
)
272 System
.out
.println( "Domain enum op failed " + String
.valueOf( errorCode
));
276 class ResolveTest
extends TermReporter
implements ResolveListener
278 public ResolveTest( int flags
, int ifIndex
, String serviceName
, String regType
,
282 DNSSD
.resolve( flags
, ifIndex
, serviceName
, regType
, domain
, this);
283 } catch( Exception e
) { e
.printStackTrace(); }
286 public void serviceResolved( DNSSDService resolver
, int flags
, int ifIndex
, String fullName
,
287 String hostName
, int port
, TXTRecord txtRecord
)
290 String s
= "ResolveTest result flags:" + String
.valueOf( flags
) +
291 " ifIndex:" + String
.valueOf( ifIndex
) +
292 " fullName:" + fullName
+ " hostName:" + hostName
+ " port:" + String
.valueOf( port
);
293 for ( int i
=0; null != ( a
= txtRecord
.getKey( i
)); i
++)
294 s
+= " attr/val " + String
.valueOf( i
) + ": " + a
+ "," + txtRecord
.getValueAsString( i
);
296 System
.out
.println( s
);
298 System
.out
.println( "Querying " + hostName
);
299 new QueryTest( 0, ifIndex
, hostName
, 1 /* ns_t_a */, 1 /* ns_c_in */);
303 class QueryTest
extends TermReporter
implements QueryListener
305 public QueryTest( int flags
, int ifIndex
, String serviceName
, int rrtype
, int rrclass
)
308 DNSSD
.queryRecord( flags
, ifIndex
, serviceName
, rrtype
, rrclass
, this);
309 } catch( Exception e
) { e
.printStackTrace(); }
312 public void queryAnswered( DNSSDService query
, int flags
, int ifIndex
, String fullName
,
313 int rrtype
, int rrclass
, byte[] rdata
, int ttl
)
315 String s
= "QueryTest result flags:" + String
.valueOf( flags
) +
316 " ifIndex:" + String
.valueOf( ifIndex
) +
317 " fullName:" + fullName
+ " rrtype:" + String
.valueOf( rrtype
) +
318 " rrclass:" + String
.valueOf( rrclass
) + " ttl:" + String
.valueOf( ttl
);
319 System
.out
.println( s
);
322 String dataTxt
= new String( rdata
, 0, rdata
.length
, DNSSDUnitTest
.WIRE_CHAR_SET
);
323 System
.out
.println( "Query data is:" + dataTxt
);
324 } catch( Exception e
) { e
.printStackTrace(); }