My favorites | Sign in
Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2008-2009 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @author vlads
* @version $Id$
*/
package net.sf.bluecove.obex;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.obex.Authenticator;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.Operation;
import javax.obex.PasswordAuthentication;
import javax.obex.ResponseCodes;
import javax.obex.ServerRequestHandler;

import com.intel.bluetooth.DebugLog;
import com.intel.bluetooth.obex.BlueCoveOBEX;

public class OBEXAuthenticatorTest extends OBEXAuthenticationBaseTestCase {

private byte[] serverData;

private volatile ChallengeData serverChallenge;

@Override
protected void setUp() throws Exception {
super.setUp();
serverData = null;
serverChallenge = new ChallengeData("SrvTest", true, true);
}

private class RequestHandler extends ServerRequestHandler {

private boolean authenticationRequested = false;

@Override
public int onConnect(HeaderSet request, HeaderSet reply) {
try {
DebugLog.debug("==TEST== Server onConnect");
Long when = (Long) request.getHeader(testHeaderID);
if (!authenticationRequested && When.onConnect.ordinal() == when) {
reply.createAuthenticationChallenge(serverChallenge.realm, serverChallenge.userID,
serverChallenge.access);
authenticationRequested = true;
return ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
}
} catch (IOException e) {
DebugLog.error("==TEST== Server", e);
return ResponseCodes.OBEX_HTTP_INTERNAL_ERROR;
}
return ResponseCodes.OBEX_HTTP_OK;
}

@Override
public int onPut(Operation op) {
try {
serverRequestHandlerInvocations++;
DebugLog
.debug("==TEST== Server onPut serverRequestHandlerInvocations", serverRequestHandlerInvocations);
serverHeaders = op.getReceivedHeaders();

Long when = (Long) serverHeaders.getHeader(testHeaderID);
if (!authenticationRequested && When.onPut.ordinal() == when) {
HeaderSet reply = createHeaderSet();
reply.createAuthenticationChallenge(serverChallenge.realm, serverChallenge.userID,
serverChallenge.access);
op.sendHeaders(reply);
authenticationRequested = true;
return ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
}

InputStream is = op.openInputStream();
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int data;
while ((data = is.read()) != -1) {
buf.write(data);
}
serverData = buf.toByteArray();
DebugLog.debug("==TEST== Server close Operation");
op.close();
op = null;
serverResponseCode = ResponseCodes.OBEX_HTTP_OK;
DebugLog.debug("==TEST== Server returns " + BlueCoveOBEX.obexResponseCodes(serverResponseCode));
return serverResponseCode;
} catch (IOException e) {
DebugLog.error("==TEST== Server", e);
return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
} finally {
try {
if (op != null) {
op.close();
}
} catch (IOException e) {
DebugLog.error("==TEST== Server close", e);
return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
}
}
}

@Override
public int onGet(Operation op) {
try {
serverRequestHandlerInvocations++;
DebugLog
.debug("==TEST== Server onGet serverRequestHandlerInvocations", serverRequestHandlerInvocations);
serverHeaders = op.getReceivedHeaders();
Long when = (Long) serverHeaders.getHeader(testHeaderID);
if (!authenticationRequested && When.onGet.ordinal() == when) {
HeaderSet reply = createHeaderSet();
reply.createAuthenticationChallenge(serverChallenge.realm, serverChallenge.userID,
serverChallenge.access);
op.sendHeaders(reply);
authenticationRequested = true;
return ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
}
serverResponseCode = ResponseCodes.OBEX_HTTP_OK;
DebugLog.debug("==TEST== Server returns " + BlueCoveOBEX.obexResponseCodes(serverResponseCode));
return serverResponseCode;
} catch (IOException e) {
DebugLog.error("==TEST== Server", e);
return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
} finally {
try {
op.close();
} catch (IOException e) {
DebugLog.error("==TEST== Server close", e);
return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
}
}
}

public int onSetPath(HeaderSet request, HeaderSet reply, boolean backup, boolean create) {
serverRequestHandlerInvocations++;
serverHeaders = request;
Long when;
try {
when = (Long) request.getHeader(testHeaderID);
} catch (IOException e) {
DebugLog.error("==TEST== Server", e);
return ResponseCodes.OBEX_HTTP_UNAVAILABLE;
}
if (!authenticationRequested && When.onSetPath.ordinal() == when) {
reply.createAuthenticationChallenge(serverChallenge.realm, serverChallenge.userID,
serverChallenge.access);
authenticationRequested = true;
return ResponseCodes.OBEX_HTTP_UNAUTHORIZED;
}
serverResponseCode = ResponseCodes.OBEX_HTTP_OK;
DebugLog.debug("==TEST== Server returns " + BlueCoveOBEX.obexResponseCodes(serverResponseCode));
return serverResponseCode;
}

public void onAuthenticationFailure(byte[] userName) {
DebugLog.debug("==TEST== Server onAuthenticationFailure");
authenticationRequested = false;
}
}

private class ServerRequestAuthenticator implements Authenticator {

ServerRequestAuthenticator(ServerRequestHandler handler) {

}

public PasswordAuthentication onAuthenticationChallenge(String description, boolean isUserIdRequired,
boolean isFullAccess) {
serverOnAuthenticationChallengeCalled++;
DebugLog.debug("==TEST== Server challenge " + (isUserIdRequired ? "U" : "") + (isFullAccess ? "A" : "")
+ " " + description);
return new PasswordAuthentication(userName.getBytes(), getUserPassword(userName));
}

public byte[] onAuthenticationResponse(byte[] userName) {
serverOnAuthenticationResponseCalled++;
DebugLog.debug("==TEST== Server authenticate user", (userName != null) ? new String(userName) : "{null}");
return getUserPassword(userName);
}

}

@Override
protected Authenticator getServerAuthenticator(ServerRequestHandler handler) {
return new ServerRequestAuthenticator(handler);
}

@Override
protected ServerRequestHandler createRequestHandler() {
return new RequestHandler();
}

private void runPUTOperation(final When whenChallenge, boolean empty) throws IOException {

now = When.Never;

authenticatorCalled = When.Never;

final ChallengeData clientChallenge = new ChallengeData();

Authenticator auth = new Authenticator() {

public PasswordAuthentication onAuthenticationChallenge(String description, boolean isUserIdRequired,
boolean isFullAccess) {
authenticatorCalled = now;
clientOnAuthenticationChallengeCalled ++;
clientChallenge.realm = description;
clientChallenge.userID = isUserIdRequired;
clientChallenge.access = isFullAccess;
DebugLog.debug("==TEST== Client challenge " + (isUserIdRequired ? "U" : "") + (isFullAccess ? "A" : "")
+ " " + description);
byte[] password;
if (isUserIdRequired) {
password = getUserPassword(userName);
} else {
password = getUserPassword((String) null);
}
return new PasswordAuthentication(userName.getBytes(), password);
}

public byte[] onAuthenticationResponse(byte[] userName) {
clientOnAuthenticationResponseCalled++;
DebugLog.debug("==TEST== Client onAuthenticationResponse");
return null;
}
};

ClientSession clientSession = (ClientSession) Connector.open(selectService(serverUUID));

clientSession.setAuthenticator(auth);
HeaderSet hsConnect = clientSession.createHeaderSet();
hsConnect.setHeader(testHeaderID, new Long(whenChallenge.ordinal()));
now = When.onConnect;
HeaderSet hsConnectReply = clientSession.connect(hsConnect);
int connectCode = hsConnectReply.getResponseCode();
DebugLog.debug0x("==TEST== Client connect ResponseCode " + BlueCoveOBEX.obexResponseCodes(connectCode) + " = ",
connectCode);
assertEquals("connect", ResponseCodes.OBEX_HTTP_OK, connectCode);

HeaderSet hsOperation = clientSession.createHeaderSet();
String name = "Hello.txt";
hsOperation.setHeader(HeaderSet.NAME, name);
hsOperation.setHeader(testHeaderID, new Long(whenChallenge.ordinal()));

int responseCode;
if (whenChallenge == When.onGet) {
now = When.onGet;
Operation getOperation = clientSession.get(hsOperation);

DebugLog.debug("==TEST== Client getResponseCode");
responseCode = getOperation.getResponseCode();
DebugLog.debug0x(
"==TEST== Client GET ResponseCode " + BlueCoveOBEX.obexResponseCodes(responseCode) + " = ",
responseCode);

getOperation.close();

} else if (whenChallenge == When.onSetPath) {
now = When.onSetPath;
HeaderSet reply = clientSession.setPath(hsOperation, true, true);
responseCode = reply.getResponseCode();
} else {
now = When.onPut;
// Create PUT Operation
Operation putOperation = clientSession.put(hsOperation);

// Send some text to server
byte data[] = new byte[0];
OutputStream os = putOperation.openOutputStream();
if (!empty) {
data = simpleData;
os.write(data);
}
os.close();
DebugLog.debug("==TEST== Client getResponseCode");
responseCode = putOperation.getResponseCode();
DebugLog.debug0x(
"==TEST== Client PUT ResponseCode " + BlueCoveOBEX.obexResponseCodes(responseCode) + " = ",
responseCode);

putOperation.close();

assertEquals("data", data, serverData);
}

now = When.Never;

clientSession.disconnect(null);

clientSession.close();

assertEquals("NAME", name, serverHeaders.getHeader(HeaderSet.NAME));

int invocationsExpected = 1;
if ((When.onPut == whenChallenge) || (When.onGet == whenChallenge) || (When.onSetPath == whenChallenge)) {
invocationsExpected++;
}
assertEquals("invocations", invocationsExpected, serverRequestHandlerInvocations);

assertEquals("onAuthenticationChallenge", whenChallenge.name(), authenticatorCalled.name());

if (When.Never != whenChallenge) {
assertEquals("Challenge.realm", serverChallenge.realm, clientChallenge.realm);
assertEquals("Challenge.userID", serverChallenge.userID, clientChallenge.userID);
assertEquals("Challenge.access", serverChallenge.access, clientChallenge.access);
}

int challengeCalledExpected = 1;
if (When.Never == whenChallenge) {
challengeCalledExpected = 0;
}

assertEquals("serverOnAuthenticationResponseCalled", challengeCalledExpected,
serverOnAuthenticationResponseCalled);

assertEquals("clientOnAuthenticationChallengeCalled", challengeCalledExpected, clientOnAuthenticationChallengeCalled);

assertEquals("serverOnAuthenticationChallengeCalled", 0, serverOnAuthenticationChallengeCalled);
assertEquals("clientOnAuthenticationResponseCalled", 0, clientOnAuthenticationResponseCalled);

assertEquals("ResponseCodes." + BlueCoveOBEX.obexResponseCodes(serverResponseCode), serverResponseCode,
responseCode);
assertServerErrors();

}

public void testNoChallenge() throws IOException {
runPUTOperation(When.Never, false);
}

public void testChallengeOnConnect() throws IOException {
runPUTOperation(When.onConnect, false);
}

public void testChallengeOnConnectFalseFalse() throws IOException {
serverChallenge = new ChallengeData("RealmFalseFalse", false, false);
runPUTOperation(When.onConnect, false);
}

public void testChallengeOnPut() throws IOException {
runPUTOperation(When.onPut, false);
}

public void testChallengeOnPutEmpty() throws IOException {
runPUTOperation(When.onPut, true);
}

public void testChallengeOnGet() throws IOException {
runPUTOperation(When.onGet, false);
}

public void testChallengeOnSetPath() throws IOException {
runPUTOperation(When.onSetPath, false);
}
}
Show details Hide details

Change log

r2917 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
Go to: 
Project members, sign in to write a code review

Older revisions

r2644 by skarzhevskyy on Dec 22, 2008   Diff
fixed handling of Authentication
Responses
r2641 by skarzhevskyy on Dec 22, 2008   Diff
Fixed client side handling of
Authentication Challenge, will retry
sending headers
r2618 by skarzhevskyy on Dec 18, 2008   Diff
testing OBEX
All revisions of this file

File info

Size: 12950 bytes, 378 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Hosted by Google Code