]> git.saurik.com Git - apple/mdnsresponder.git/blob - Clients/Java/DNSSDUnitTest.java
mDNSResponder-108.6.tar.gz
[apple/mdnsresponder.git] / Clients / Java / DNSSDUnitTest.java
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22
23 Change History (most recent first):
24
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>.
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
111 this.waitForEnd();
112 }
113
114 protected int fStage;
115 protected RegTest fRegTest;
116
117 public synchronized void bumpStage()
118 {
119 fStage++;
120 this.notifyAll();
121 }
122
123 protected synchronized void waitForEnd()
124 {
125 int stage = fStage;
126 while ( stage == fStage)
127 {
128 try {
129 wait();
130 } catch (InterruptedException e) {}
131 }
132 }
133
134 public static void main(String s[])
135 {
136 try {
137 new DNSSDUnitTest().run();
138 }
139 catch ( Exception e) { terminateWithException( e); }
140 }
141
142 protected static void terminateWithException( Exception e)
143 {
144 e.printStackTrace();
145 System.exit( -1);
146 }
147 }
148
149 class TermReporter implements BaseListener
150 {
151 public void operationFailed( DNSSDService service, int errorCode)
152 {
153 System.out.println( this.getClass().getName() + " encountered error " + String.valueOf( errorCode));
154 }
155
156 protected void finalize() throws Throwable
157 {
158 System.out.println( "Instance of " + this.getClass().getName() + " has been destroyed");
159 }
160 }
161
162 class RegTest extends TermReporter implements RegisterListener
163 {
164 public static final int TEST_PORT = 5678;
165
166 public RegTest() throws DNSSDException
167 {
168 fReg = DNSSD.register( 0, 0, "Test service", DNSSDUnitTest.TEST_TYPE, "", "", TEST_PORT, null, this);
169 }
170
171 public void serviceRegistered( DNSSDRegistration registration, int flags, String serviceName,
172 String regType, String domain)
173 {
174 String s = "RegTest result flags:" + String.valueOf( flags) +
175 " serviceName:" + serviceName + " regType:" + regType + " domain:" + domain;
176 System.out.println( s);
177
178 try {
179 new DupRegTest();
180
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(); }
185 }
186
187 protected DNSSDRegistration fReg;
188 }
189
190 class DupRegTest extends TermReporter implements RegisterListener
191 {
192 public static final int TEST_PORT = 5678;
193
194 public DupRegTest() throws DNSSDException
195 {
196 DNSSD.register( DNSSD.NO_AUTO_RENAME | DNSSD.UNIQUE, 0, "Test service", DNSSDUnitTest.TEST_TYPE, "", "", TEST_PORT + 1, null, this);
197 }
198
199 public void serviceRegistered( DNSSDRegistration registration, int flags, String serviceName,
200 String regType, String domain)
201 {
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);
206 }
207 }
208
209 class BrowseTest extends TermReporter implements BrowseListener
210 {
211 public BrowseTest()
212 {
213 try {
214 DNSSD.browse( 0, 0, DNSSDUnitTest.TEST_TYPE, "", this);
215 } catch( Exception e) { e.printStackTrace(); }
216 }
217
218 public void serviceFound( DNSSDService browser, int flags, int ifIndex,
219 String serviceName, String regType, String domain)
220 {
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);
225
226 System.out.println( "Resolving " + serviceName);
227 new ResolveTest( 0, ifIndex, serviceName, regType, domain);
228 }
229
230 public void serviceLost( DNSSDService browser, int flags, int ifIndex,
231 String serviceName, String regType, String domain)
232 {
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);
237 }
238
239 public void operationFailed( DNSSDService service, int errorCode)
240 {
241 System.out.println( "Browse failed " + String.valueOf( errorCode));
242 }
243 }
244
245 class DomainTest extends TermReporter implements DomainListener
246 {
247 public DomainTest()
248 {
249 try {
250 DNSSD.enumerateDomains( DNSSD.BROWSE_DOMAINS, 0, this);
251 } catch( Exception e) { e.printStackTrace(); }
252 }
253
254 public void domainFound( DNSSDService enumerator, int flags, int ifIndex, String domain)
255 {
256 String s = "Domain found flags:" + String.valueOf( flags) +
257 " ifIndex:" + String.valueOf( ifIndex) +
258 " domain:" + domain;
259 System.out.println( s);
260 }
261
262 public void domainLost( DNSSDService enumerator, int flags, int ifIndex, String domain)
263 {
264 String s = "Domain lost flags:" + String.valueOf( flags) +
265 " ifIndex:" + String.valueOf( ifIndex) +
266 " domain:" + domain;
267 System.out.println( s);
268 }
269
270 public void operationFailed( DNSSDService service, int errorCode)
271 {
272 System.out.println( "Domain enum op failed " + String.valueOf( errorCode));
273 }
274 }
275
276 class ResolveTest extends TermReporter implements ResolveListener
277 {
278 public ResolveTest( int flags, int ifIndex, String serviceName, String regType,
279 String domain)
280 {
281 try {
282 DNSSD.resolve( flags, ifIndex, serviceName, regType, domain, this);
283 } catch( Exception e) { e.printStackTrace(); }
284 }
285
286 public void serviceResolved( DNSSDService resolver, int flags, int ifIndex, String fullName,
287 String hostName, int port, TXTRecord txtRecord)
288 {
289 String a;
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);
295
296 System.out.println( s);
297
298 System.out.println( "Querying " + hostName);
299 new QueryTest( 0, ifIndex, hostName, 1 /* ns_t_a */, 1 /* ns_c_in */);
300 }
301 }
302
303 class QueryTest extends TermReporter implements QueryListener
304 {
305 public QueryTest( int flags, int ifIndex, String serviceName, int rrtype, int rrclass)
306 {
307 try {
308 DNSSD.queryRecord( flags, ifIndex, serviceName, rrtype, rrclass, this);
309 } catch( Exception e) { e.printStackTrace(); }
310 }
311
312 public void queryAnswered( DNSSDService query, int flags, int ifIndex, String fullName,
313 int rrtype, int rrclass, byte[] rdata, int ttl)
314 {
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);
320
321 try {
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(); }
325 }
326 }
327