]> git.saurik.com Git - apple/mdnsresponder.git/blob - Clients/Java/DNSSDUnitTest.java
e4523e1778caa9a77b95b92a15bf7fcdbacb3f7f
[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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24
25 Change History (most recent first):
26
27 $Log: DNSSDUnitTest.java,v $
28 Revision 1.3 2004/05/26 01:41:58 cheshire
29 Pass proper flags to DNSSD.enumerateDomains
30
31 Revision 1.2 2004/04/30 21:53:34 rpantos
32 Change line endings for CVS.
33
34 Revision 1.1 2004/04/30 16:29:35 rpantos
35 First checked in.
36
37 DNSSDUnitTest is a simple program that exercises parts of the DNSSD API.
38 */
39
40 import com.apple.dnssd.*;
41
42 import java.net.*;
43 import java.util.*;
44
45
46 class DNSSDUnitTest
47 {
48 public static final String TEST_TYPE = "_unittest._udp";
49 public static final String WIRE_CHAR_SET = "ISO-8859-1";
50
51 public DNSSDUnitTest fInstance = null;
52
53 public DNSSDUnitTest() throws Exception
54 {
55 fStage = 0;
56 fInstance = this;
57
58 Enumeration en = NetworkInterface.getNetworkInterfaces();
59 while ( en.hasMoreElements())
60 System.out.println( ((NetworkInterface) en.nextElement()).getName());
61 }
62
63 public void testTxtRecord()
64 {
65 byte[] src = { 6, 'a', 't', '=', 'X', 'Y', 'Z' };
66 TXTRecord txtRecord = new TXTRecord( src);
67 String a;
68
69 txtRecord.set( "path", "~/names");
70 txtRecord.set( "ttl", "4");
71
72 byte[] rawBytes = txtRecord.getRawBytes();
73 System.out.println( ( new String( rawBytes, 0, rawBytes.length)) + " has count " +
74 String.valueOf( txtRecord.size()));
75
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"));
80
81 for ( int i=0; null != ( a = txtRecord.getKey( i)); i++)
82 System.out.println( "attr/val " + String.valueOf( i) + ": " + a + "," + txtRecord.getValueAsString( i));
83 }
84
85 public void run() throws DNSSDException
86 {
87 System.out.println( "Running DNSSD unit test for " + System.getProperty( "user.name"));
88
89 this.testTxtRecord();
90
91 fRegTest = new RegTest();
92 new BrowseTest();
93 new DomainTest();
94
95 this.waitForEnd();
96 }
97
98 protected int fStage;
99 protected RegTest fRegTest;
100
101 public synchronized void bumpStage()
102 {
103 fStage++;
104 this.notifyAll();
105 }
106
107 protected synchronized void waitForEnd()
108 {
109 int stage = fStage;
110 while ( stage == fStage)
111 {
112 try {
113 wait();
114 } catch (InterruptedException e) {}
115 }
116 }
117
118 public static void main(String s[])
119 {
120 try {
121 new DNSSDUnitTest().run();
122 }
123 catch ( Exception e) { terminateWithException( e); }
124 }
125
126 protected static void terminateWithException( Exception e)
127 {
128 e.printStackTrace();
129 System.exit( -1);
130 }
131 }
132
133 class TermReporter implements BaseListener
134 {
135 public void operationFailed( DNSSDService service, int errorCode)
136 {
137 System.out.println( this.getClass().getName() + " encountered error " + String.valueOf( errorCode));
138 }
139
140 protected void finalize() throws Throwable
141 {
142 System.out.println( "Instance of " + this.getClass().getName() + " has been destroyed");
143 }
144 }
145
146 class RegTest extends TermReporter implements RegisterListener
147 {
148 public static final int TEST_PORT = 5678;
149
150 public RegTest() throws DNSSDException
151 {
152 fReg = DNSSD.register( 0, 0, "Test service", DNSSDUnitTest.TEST_TYPE, "", "", TEST_PORT, null, this);
153 }
154
155 public void serviceRegistered( DNSSDRegistration registration, int flags, String serviceName,
156 String regType, String domain)
157 {
158 String s = "RegTest result flags:" + String.valueOf( flags) +
159 " serviceName:" + serviceName + " regType:" + regType + " domain:" + domain;
160 System.out.println( s);
161
162 try {
163 new DupRegTest();
164
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(); }
169 }
170
171 protected DNSSDRegistration fReg;
172 }
173
174 class DupRegTest extends TermReporter implements RegisterListener
175 {
176 public static final int TEST_PORT = 5678;
177
178 public DupRegTest() throws DNSSDException
179 {
180 DNSSD.register( DNSSD.NO_AUTO_RENAME | DNSSD.UNIQUE, 0, "Test service", DNSSDUnitTest.TEST_TYPE, "", "", TEST_PORT + 1, null, this);
181 }
182
183 public void serviceRegistered( DNSSDRegistration registration, int flags, String serviceName,
184 String regType, String domain)
185 {
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);
190 }
191 }
192
193 class BrowseTest extends TermReporter implements BrowseListener
194 {
195 public BrowseTest()
196 {
197 try {
198 DNSSD.browse( 0, 0, DNSSDUnitTest.TEST_TYPE, "", this);
199 } catch( Exception e) { e.printStackTrace(); }
200 }
201
202 public void serviceFound( DNSSDService browser, int flags, int ifIndex,
203 String serviceName, String regType, String domain)
204 {
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);
209
210 System.out.println( "Resolving " + serviceName);
211 new ResolveTest( 0, ifIndex, serviceName, regType, domain);
212 }
213
214 public void serviceLost( DNSSDService browser, int flags, int ifIndex,
215 String serviceName, String regType, String domain)
216 {
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);
221 }
222
223 public void operationFailed( DNSSDService service, int errorCode)
224 {
225 System.out.println( "Browse failed " + String.valueOf( errorCode));
226 }
227 }
228
229 class DomainTest extends TermReporter implements DomainListener
230 {
231 public DomainTest()
232 {
233 try {
234 DNSSD.enumerateDomains( DNSSD.BROWSE_DOMAINS, 0, this);
235 } catch( Exception e) { e.printStackTrace(); }
236 }
237
238 public void domainFound( DNSSDService enumerator, int flags, int ifIndex, String domain)
239 {
240 String s = "Domain found flags:" + String.valueOf( flags) +
241 " ifIndex:" + String.valueOf( ifIndex) +
242 " domain:" + domain;
243 System.out.println( s);
244 }
245
246 public void domainLost( DNSSDService enumerator, int flags, int ifIndex, String domain)
247 {
248 String s = "Domain lost flags:" + String.valueOf( flags) +
249 " ifIndex:" + String.valueOf( ifIndex) +
250 " domain:" + domain;
251 System.out.println( s);
252 }
253
254 public void operationFailed( DNSSDService service, int errorCode)
255 {
256 System.out.println( "Domain enum op failed " + String.valueOf( errorCode));
257 }
258 }
259
260 class ResolveTest extends TermReporter implements ResolveListener
261 {
262 public ResolveTest( int flags, int ifIndex, String serviceName, String regType,
263 String domain)
264 {
265 try {
266 DNSSD.resolve( flags, ifIndex, serviceName, regType, domain, this);
267 } catch( Exception e) { e.printStackTrace(); }
268 }
269
270 public void serviceResolved( DNSSDService resolver, int flags, int ifIndex, String fullName,
271 String hostName, int port, TXTRecord txtRecord)
272 {
273 String a;
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);
279
280 System.out.println( s);
281
282 System.out.println( "Querying " + hostName);
283 new QueryTest( 0, ifIndex, hostName, 1 /* ns_t_a */, 1 /* ns_c_in */);
284 }
285 }
286
287 class QueryTest extends TermReporter implements QueryListener
288 {
289 public QueryTest( int flags, int ifIndex, String serviceName, int rrtype, int rrclass)
290 {
291 try {
292 DNSSD.queryRecord( flags, ifIndex, serviceName, rrtype, rrclass, this);
293 } catch( Exception e) { e.printStackTrace(); }
294 }
295
296 public void queryAnswered( DNSSDService query, int flags, int ifIndex, String fullName,
297 int rrtype, int rrclass, byte[] rdata, int ttl)
298 {
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);
304
305 try {
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(); }
309 }
310 }
311