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 * DNSSDUnitTest is a simple program that exercises parts of the DNSSD API.
20 import com
.apple
.dnssd
.*;
28 public static final String TEST_TYPE
= "_unittest._udp";
29 public static final String WIRE_CHAR_SET
= "ISO-8859-1";
31 public DNSSDUnitTest fInstance
= null;
33 public DNSSDUnitTest() throws Exception
38 Enumeration en
= NetworkInterface
.getNetworkInterfaces();
39 while ( en
.hasMoreElements())
40 System
.out
.println( ((NetworkInterface
) en
.nextElement()).getName());
43 public void testTxtRecord()
45 byte[] src
= { 6, 'a', 't', '=', 'X', 'Y', 'Z' };
46 TXTRecord txtRecord
= new TXTRecord( src
);
49 txtRecord
.set( "path", "~/names");
50 txtRecord
.set( "rw", (String
) null);
51 txtRecord
.set( "empty", "");
52 txtRecord
.set( "ttl", "4");
54 byte[] rawBytes
= txtRecord
.getRawBytes();
55 System
.out
.println( ( new String( rawBytes
, 0, rawBytes
.length
)) + " has count " +
56 String
.valueOf( txtRecord
.size()));
58 System
.out
.println( txtRecord
);
59 boolean ttlPresent
= txtRecord
.contains( "ttl");
60 System
.out
.println( "ttl is present: " + ( ttlPresent ?
"true" : "false"));
61 boolean timeoutPresent
= txtRecord
.contains( "timeout");
62 System
.out
.println( "timeout is present: " + ( timeoutPresent ?
"true" : "false"));
64 txtRecord
.set( "path", "~/numbers");
65 System
.out
.println( txtRecord
);
67 txtRecord
.remove( "ttl");
68 System
.out
.println( txtRecord
);
70 txtRecord
.remove( "path");
71 System
.out
.println( txtRecord
);
73 txtRecord
.remove( "at");
74 System
.out
.println( txtRecord
);
76 txtRecord
.set( "rw", "1");
77 System
.out
.println( txtRecord
);
80 public void run() throws DNSSDException
82 System
.out
.println( "Running DNSSD unit test for " + System
.getProperty( "user.name"));
86 fRegTest
= new RegTest();
95 protected RegTest fRegTest
;
97 public synchronized void bumpStage()
103 protected synchronized void waitForEnd()
106 while ( stage
== fStage
)
110 } catch (InterruptedException e
) {}
114 public static void main(String s
[])
117 new DNSSDUnitTest().run();
119 catch ( Exception e
) { terminateWithException( e
); }
122 protected static void terminateWithException( Exception e
)
129 class TermReporter
implements BaseListener
131 public void operationFailed( DNSSDService service
, int errorCode
)
133 System
.out
.println( this.getClass().getName() + " encountered error " + String
.valueOf( errorCode
));
136 protected void finalize() throws Throwable
138 System
.out
.println( "Instance of " + this.getClass().getName() + " has been destroyed");
142 class RegTest
extends TermReporter
implements RegisterListener
144 public static final int TEST_PORT
= 5678;
146 public RegTest() throws DNSSDException
148 fReg
= DNSSD
.register( 0, 0, "Test service", DNSSDUnitTest
.TEST_TYPE
, "", "", TEST_PORT
, null, this);
151 public void serviceRegistered( DNSSDRegistration registration
, int flags
, String serviceName
,
152 String regType
, String domain
)
154 String s
= "RegTest result flags:" + String
.valueOf( flags
) +
155 " serviceName:" + serviceName
+ " regType:" + regType
+ " domain:" + domain
;
156 System
.out
.println( s
);
161 byte[] kResponsiblePerson
= { 'c','o','o','k','i','e',' ','m','o','n','s','t','e','r' };
162 fReg
.addRecord( 0, 17 /*ns_t_rp*/, kResponsiblePerson
, 3600);
163 new QueryTest( 0, 0, "Test service", 17 /*ns_t_rp*/, 1);
164 } catch( Exception e
) { e
.printStackTrace(); }
167 protected DNSSDRegistration fReg
;
170 class DupRegTest
extends TermReporter
implements RegisterListener
172 public static final int TEST_PORT
= 5678;
174 public DupRegTest() throws DNSSDException
176 DNSSD
.register( DNSSD
.NO_AUTO_RENAME
| DNSSD
.UNIQUE
, 0, "Test service", DNSSDUnitTest
.TEST_TYPE
, "", "", TEST_PORT
+ 1, null, this);
179 public void serviceRegistered( DNSSDRegistration registration
, int flags
, String serviceName
,
180 String regType
, String domain
)
182 System
.out
.println( "Oik - registered a duplicate!");
183 String s
= "DupRegTest result flags:" + String
.valueOf( flags
) +
184 " serviceName:" + serviceName
+ " regType:" + regType
+ " domain:" + domain
;
185 System
.out
.println( s
);
189 class BrowseTest
extends TermReporter
implements BrowseListener
194 DNSSD
.browse( 0, 0, DNSSDUnitTest
.TEST_TYPE
, "", this);
195 } catch( Exception e
) { e
.printStackTrace(); }
198 public void serviceFound( DNSSDService browser
, int flags
, int ifIndex
,
199 String serviceName
, String regType
, String domain
)
201 String s
= "BrowseTest found flags:" + String
.valueOf( flags
) +
202 " ifIndex:" + String
.valueOf( ifIndex
) +
203 " serviceName:" + serviceName
+ " regType:" + regType
+ " domain:" + domain
;
204 System
.out
.println( s
);
206 System
.out
.println( "Resolving " + serviceName
);
207 new ResolveTest( 0, ifIndex
, serviceName
, regType
, domain
);
210 public void serviceLost( DNSSDService browser
, int flags
, int ifIndex
,
211 String serviceName
, String regType
, String domain
)
213 String s
= "BrowseTest lost flags:" + String
.valueOf( flags
) +
214 " ifIndex:" + String
.valueOf( ifIndex
) +
215 " serviceName:" + serviceName
+ " regType:" + regType
+ " domain:" + domain
;
216 System
.out
.println( s
);
219 public void operationFailed( DNSSDService service
, int errorCode
)
221 System
.out
.println( "Browse failed " + String
.valueOf( errorCode
));
225 class DomainTest
extends TermReporter
implements DomainListener
230 DNSSD
.enumerateDomains( DNSSD
.BROWSE_DOMAINS
, 0, this);
231 } catch( Exception e
) { e
.printStackTrace(); }
234 public void domainFound( DNSSDService enumerator
, int flags
, int ifIndex
, String domain
)
236 String s
= "Domain found flags:" + String
.valueOf( flags
) +
237 " ifIndex:" + String
.valueOf( ifIndex
) +
239 System
.out
.println( s
);
242 public void domainLost( DNSSDService enumerator
, int flags
, int ifIndex
, String domain
)
244 String s
= "Domain lost flags:" + String
.valueOf( flags
) +
245 " ifIndex:" + String
.valueOf( ifIndex
) +
247 System
.out
.println( s
);
250 public void operationFailed( DNSSDService service
, int errorCode
)
252 System
.out
.println( "Domain enum op failed " + String
.valueOf( errorCode
));
256 class ResolveTest
extends TermReporter
implements ResolveListener
258 public ResolveTest( int flags
, int ifIndex
, String serviceName
, String regType
,
262 DNSSD
.resolve( flags
, ifIndex
, serviceName
, regType
, domain
, this);
263 } catch( Exception e
) { e
.printStackTrace(); }
266 public void serviceResolved( DNSSDService resolver
, int flags
, int ifIndex
, String fullName
,
267 String hostName
, int port
, TXTRecord txtRecord
)
270 String s
= "ResolveTest result flags:" + String
.valueOf( flags
) +
271 " ifIndex:" + String
.valueOf( ifIndex
) +
272 " fullName:" + fullName
+ " hostName:" + hostName
+ " port:" + String
.valueOf( port
);
273 for ( int i
=0; null != ( a
= txtRecord
.getKey( i
)); i
++)
274 s
+= " attr/val " + String
.valueOf( i
) + ": " + a
+ "," + txtRecord
.getValueAsString( i
);
276 System
.out
.println( s
);
278 System
.out
.println( "Querying " + hostName
);
279 new QueryTest( 0, ifIndex
, hostName
, 1 /* ns_t_a */, 1 /* ns_c_in */);
283 class QueryTest
extends TermReporter
implements QueryListener
285 public QueryTest( int flags
, int ifIndex
, String serviceName
, int rrtype
, int rrclass
)
288 DNSSD
.queryRecord( flags
, ifIndex
, serviceName
, rrtype
, rrclass
, this);
289 } catch( Exception e
) { e
.printStackTrace(); }
292 public void queryAnswered( DNSSDService query
, int flags
, int ifIndex
, String fullName
,
293 int rrtype
, int rrclass
, byte[] rdata
, int ttl
)
295 String s
= "QueryTest result flags:" + String
.valueOf( flags
) +
296 " ifIndex:" + String
.valueOf( ifIndex
) +
297 " fullName:" + fullName
+ " rrtype:" + String
.valueOf( rrtype
) +
298 " rrclass:" + String
.valueOf( rrclass
) + " ttl:" + String
.valueOf( ttl
);
299 System
.out
.println( s
);
302 String dataTxt
= new String( rdata
, 0, rdata
.length
, DNSSDUnitTest
.WIRE_CHAR_SET
);
303 System
.out
.println( "Query data is:" + dataTxt
);
304 } catch( Exception e
) { e
.printStackTrace(); }
308 class RegistrarTest
extends TermReporter
implements RegisterRecordListener
310 public RegistrarTest()
313 byte[] kResponsiblePerson
= { 'g','r','o','v','e','r' };
314 fRegistrar
= DNSSD
.createRecordRegistrar( this);
315 fRegistrar
.registerRecord( DNSSD
.UNIQUE
, 0,
316 "test.registrartest.local", 17 /*ns_t_rp*/, 1, kResponsiblePerson
, 3600);
317 } catch( Exception e
) { e
.printStackTrace(); }
320 public void recordRegistered( DNSRecord record
, int flags
)
322 String s
= "RegistrarTest result flags:" + String
.valueOf( flags
);
323 System
.out
.println( s
);
326 byte[] kResponsiblePerson
= { 'e','l','m','o' };
327 record
.update( 0, kResponsiblePerson
, 3600);
329 } catch( Exception e
) { e
.printStackTrace(); }
332 protected DNSSDRecordRegistrar fRegistrar
;