2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 Change History (most recent first):
27 $Log: DNSSDUnitTest.java,v $
28 Revision 1.3 2004/05/26 01:41:58 cheshire
29 Pass proper flags to DNSSD.enumerateDomains
31 Revision 1.2 2004/04/30 21:53:34 rpantos
32 Change line endings for CVS.
34 Revision 1.1 2004/04/30 16:29:35 rpantos
37 DNSSDUnitTest is a simple program that exercises parts of the DNSSD API.
40 import com
.apple
.dnssd
.*;
48 public static final String TEST_TYPE
= "_unittest._udp";
49 public static final String WIRE_CHAR_SET
= "ISO-8859-1";
51 public DNSSDUnitTest fInstance
= null;
53 public DNSSDUnitTest() throws Exception
58 Enumeration en
= NetworkInterface
.getNetworkInterfaces();
59 while ( en
.hasMoreElements())
60 System
.out
.println( ((NetworkInterface
) en
.nextElement()).getName());
63 public void testTxtRecord()
65 byte[] src
= { 6, 'a', 't', '=', 'X', 'Y', 'Z' };
66 TXTRecord txtRecord
= new TXTRecord( src
);
69 txtRecord
.set( "path", "~/names");
70 txtRecord
.set( "ttl", "4");
72 byte[] rawBytes
= txtRecord
.getRawBytes();
73 System
.out
.println( ( new String( rawBytes
, 0, rawBytes
.length
)) + " has count " +
74 String
.valueOf( txtRecord
.size()));
76 boolean ttlPresent
= txtRecord
.contains( "ttl");
77 System
.out
.println( "ttl is present: " + ( ttlPresent ?
"true" : "false"));
78 boolean timeoutPresent
= txtRecord
.contains( "timeout");
79 System
.out
.println( "timeout is present: " + ( timeoutPresent ?
"true" : "false"));
81 for ( int i
=0; null != ( a
= txtRecord
.getKey( i
)); i
++)
82 System
.out
.println( "attr/val " + String
.valueOf( i
) + ": " + a
+ "," + txtRecord
.getValueAsString( i
));
85 public void run() throws DNSSDException
87 System
.out
.println( "Running DNSSD unit test for " + System
.getProperty( "user.name"));
91 fRegTest
= new RegTest();
99 protected RegTest fRegTest
;
101 public synchronized void bumpStage()
107 protected synchronized void waitForEnd()
110 while ( stage
== fStage
)
114 } catch (InterruptedException e
) {}
118 public static void main(String s
[])
121 new DNSSDUnitTest().run();
123 catch ( Exception e
) { terminateWithException( e
); }
126 protected static void terminateWithException( Exception e
)
133 class TermReporter
implements BaseListener
135 public void operationFailed( DNSSDService service
, int errorCode
)
137 System
.out
.println( this.getClass().getName() + " encountered error " + String
.valueOf( errorCode
));
140 protected void finalize() throws Throwable
142 System
.out
.println( "Instance of " + this.getClass().getName() + " has been destroyed");
146 class RegTest
extends TermReporter
implements RegisterListener
148 public static final int TEST_PORT
= 5678;
150 public RegTest() throws DNSSDException
152 fReg
= DNSSD
.register( 0, 0, "Test service", DNSSDUnitTest
.TEST_TYPE
, "", "", TEST_PORT
, null, this);
155 public void serviceRegistered( DNSSDRegistration registration
, int flags
, String serviceName
,
156 String regType
, String domain
)
158 String s
= "RegTest result flags:" + String
.valueOf( flags
) +
159 " serviceName:" + serviceName
+ " regType:" + regType
+ " domain:" + domain
;
160 System
.out
.println( s
);
165 byte[] kResponsiblePerson
= { 'c','o','o','k','i','e',' ','m','o','n','s','t','e','r' };
166 fReg
.addRecord( 0, 17 /*ns_t_rp*/, kResponsiblePerson
, 3600);
167 new QueryTest( 0, 0, "Test service", 17 /*ns_t_rp*/, 1);
168 } catch( Exception e
) { e
.printStackTrace(); }
171 protected DNSSDRegistration fReg
;
174 class DupRegTest
extends TermReporter
implements RegisterListener
176 public static final int TEST_PORT
= 5678;
178 public DupRegTest() throws DNSSDException
180 DNSSD
.register( DNSSD
.NO_AUTO_RENAME
| DNSSD
.UNIQUE
, 0, "Test service", DNSSDUnitTest
.TEST_TYPE
, "", "", TEST_PORT
+ 1, null, this);
183 public void serviceRegistered( DNSSDRegistration registration
, int flags
, String serviceName
,
184 String regType
, String domain
)
186 System
.out
.println( "Oik - registered a duplicate!");
187 String s
= "DupRegTest result flags:" + String
.valueOf( flags
) +
188 " serviceName:" + serviceName
+ " regType:" + regType
+ " domain:" + domain
;
189 System
.out
.println( s
);
193 class BrowseTest
extends TermReporter
implements BrowseListener
198 DNSSD
.browse( 0, 0, DNSSDUnitTest
.TEST_TYPE
, "", this);
199 } catch( Exception e
) { e
.printStackTrace(); }
202 public void serviceFound( DNSSDService browser
, int flags
, int ifIndex
,
203 String serviceName
, String regType
, String domain
)
205 String s
= "BrowseTest found flags:" + String
.valueOf( flags
) +
206 " ifIndex:" + String
.valueOf( ifIndex
) +
207 " serviceName:" + serviceName
+ " regType:" + regType
+ " domain:" + domain
;
208 System
.out
.println( s
);
210 System
.out
.println( "Resolving " + serviceName
);
211 new ResolveTest( 0, ifIndex
, serviceName
, regType
, domain
);
214 public void serviceLost( DNSSDService browser
, int flags
, int ifIndex
,
215 String serviceName
, String regType
, String domain
)
217 String s
= "BrowseTest lost flags:" + String
.valueOf( flags
) +
218 " ifIndex:" + String
.valueOf( ifIndex
) +
219 " serviceName:" + serviceName
+ " regType:" + regType
+ " domain:" + domain
;
220 System
.out
.println( s
);
223 public void operationFailed( DNSSDService service
, int errorCode
)
225 System
.out
.println( "Browse failed " + String
.valueOf( errorCode
));
229 class DomainTest
extends TermReporter
implements DomainListener
234 DNSSD
.enumerateDomains( DNSSD
.BROWSE_DOMAINS
, 0, this);
235 } catch( Exception e
) { e
.printStackTrace(); }
238 public void domainFound( DNSSDService enumerator
, int flags
, int ifIndex
, String domain
)
240 String s
= "Domain found flags:" + String
.valueOf( flags
) +
241 " ifIndex:" + String
.valueOf( ifIndex
) +
243 System
.out
.println( s
);
246 public void domainLost( DNSSDService enumerator
, int flags
, int ifIndex
, String domain
)
248 String s
= "Domain lost flags:" + String
.valueOf( flags
) +
249 " ifIndex:" + String
.valueOf( ifIndex
) +
251 System
.out
.println( s
);
254 public void operationFailed( DNSSDService service
, int errorCode
)
256 System
.out
.println( "Domain enum op failed " + String
.valueOf( errorCode
));
260 class ResolveTest
extends TermReporter
implements ResolveListener
262 public ResolveTest( int flags
, int ifIndex
, String serviceName
, String regType
,
266 DNSSD
.resolve( flags
, ifIndex
, serviceName
, regType
, domain
, this);
267 } catch( Exception e
) { e
.printStackTrace(); }
270 public void serviceResolved( DNSSDService resolver
, int flags
, int ifIndex
, String fullName
,
271 String hostName
, int port
, TXTRecord txtRecord
)
274 String s
= "ResolveTest result flags:" + String
.valueOf( flags
) +
275 " ifIndex:" + String
.valueOf( ifIndex
) +
276 " fullName:" + fullName
+ " hostName:" + hostName
+ " port:" + String
.valueOf( port
);
277 for ( int i
=0; null != ( a
= txtRecord
.getKey( i
)); i
++)
278 s
+= " attr/val " + String
.valueOf( i
) + ": " + a
+ "," + txtRecord
.getValueAsString( i
);
280 System
.out
.println( s
);
282 System
.out
.println( "Querying " + hostName
);
283 new QueryTest( 0, ifIndex
, hostName
, 1 /* ns_t_a */, 1 /* ns_c_in */);
287 class QueryTest
extends TermReporter
implements QueryListener
289 public QueryTest( int flags
, int ifIndex
, String serviceName
, int rrtype
, int rrclass
)
292 DNSSD
.queryRecord( flags
, ifIndex
, serviceName
, rrtype
, rrclass
, this);
293 } catch( Exception e
) { e
.printStackTrace(); }
296 public void queryAnswered( DNSSDService query
, int flags
, int ifIndex
, String fullName
,
297 int rrtype
, int rrclass
, byte[] rdata
, int ttl
)
299 String s
= "QueryTest result flags:" + String
.valueOf( flags
) +
300 " ifIndex:" + String
.valueOf( ifIndex
) +
301 " fullName:" + fullName
+ " rrtype:" + String
.valueOf( rrtype
) +
302 " rrclass:" + String
.valueOf( rrclass
) + " ttl:" + String
.valueOf( ttl
);
303 System
.out
.println( s
);
306 String dataTxt
= new String( rdata
, 0, rdata
.length
, DNSSDUnitTest
.WIRE_CHAR_SET
);
307 System
.out
.println( "Query data is:" + dataTxt
);
308 } catch( Exception e
) { e
.printStackTrace(); }