My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2006-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;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.microedition.io.StreamConnection;

import org.bluecove.tester.log.Logger;
import org.bluecove.tester.util.RuntimeDetect;
import org.bluecove.tester.util.TimeUtils;

import junit.framework.Assert;
import net.sf.bluecove.tests.RfTrafficGenerator;
import net.sf.bluecove.tests.TwoThreadsPerConnection;
import net.sf.bluecove.tests.RfTrafficGenerator.Config;
import net.sf.bluecove.util.ValueHolder;

public class CommunicationTester extends CommunicationData {

public static boolean dataOutputStreamFlush = true;

public static int clientConnectionOpenRetry = 3;

static void sendString(OutputStream os) throws IOException {
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(stringData);
if (dataOutputStreamFlush) {
dos.flush();
}
}

static void readString(InputStream is) throws IOException {
DataInputStream dis = new DataInputStream(is);
String got = dis.readUTF();
Assert.assertEquals("ReadString", stringData, got);
}

static void sendUTFString(OutputStream os) throws IOException {
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(stringUTFData);
if (dataOutputStreamFlush) {
dos.flush();
}
}

static void readUTFString(InputStream is) throws IOException {
DataInputStream dis = new DataInputStream(is);
String got = dis.readUTF();
Assert.assertEquals("ReadString", stringUTFData, got);
}

static void sendByte(OutputStream os) throws IOException {
os.write(aKnowndPositiveByte);
os.write(aKnowndNegativeByte);

// Test int conversions
int bp = aKnowndPositiveByte;
os.write(bp);
int bn = aKnowndNegativeByte;
os.write(bn);

for (int i = 1; i < byteCount; i++) {
os.write((byte) i);
}
for (int i = 0; i < byteAray.length; i++) {
os.write(byteAray[i]);
}
// The byte to be written is the eight low-order bits of the argument b.
os.write(0xABC);
os.write(aKnowndPositiveByte);
}

static void readByte(InputStream is) throws IOException {
Assert.assertEquals("positiveByte", aKnowndPositiveByte, (byte) is.read());
Assert.assertEquals("negativeByte", aKnowndNegativeByte, (byte) is.read());
Assert.assertEquals("positiveByte written(int)", aKnowndPositiveByte, (byte) is.read());
Assert.assertEquals("negativeByte written(int)", aKnowndNegativeByte, (byte) is.read());
for (int i = 1; i < byteCount; i++) {
byte got = (byte) is.read();
Assert.assertEquals("t1, byte [" + i + "]", (byte) i, got);
}
for (int i = 0; i < byteAray.length; i++) {
byte got = (byte) is.read();
Assert.assertEquals("t2, byte [" + i + "]", byteAray[i], got);
}
int abc = is.read();
Assert.assertEquals("written(0xABC)", 0xBC, abc);
Assert.assertEquals("positiveByte", aKnowndPositiveByte, (byte) is.read());
}

static void sendBytes256(OutputStream os) throws IOException {
// Write all 256 bytes
int cnt = 0;
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
try {
os.write((byte) i);
cnt++;
} catch (IOException e) {
Logger.debug("Sent only " + cnt + " bytes");
throw e;
}
}
// Send one more byte to see is 0xFF is not EOF
os.write(aKnowndPositiveByte);
}

static void readBytes256(InputStream is) throws IOException {
// Read all 256 bytes
int cnt = 0;
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
byte got;
try {
got = (byte) is.read();
cnt++;
} catch (IOException e) {
Logger.debug("Received only " + cnt + " bytes");
throw e;
}
Assert.assertEquals("all bytes [" + i + "]", (byte) i, got);
}
Assert.assertEquals("conformation that 0xFF is not EOF", aKnowndPositiveByte, (byte) is.read());
}

static void sendByteAray(OutputStream os) throws IOException {
os.write(byteAray);
}

static void readByteAray(InputStream is) throws IOException {
byte[] byteArayGot = new byte[byteAray.length];
int got = is.read(byteArayGot);
Assert.assertEquals("byteAray.len", byteAray.length, got);
for (int i = 0; i < byteAray.length; i++) {
Assert.assertEquals("byte", byteAray[i], byteArayGot[i]);
}
}

static void sendDataStream(OutputStream os) throws IOException {
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(stringData);
dos.writeInt(1025);
dos.writeLong(567890025);
dos.writeBoolean(true);
dos.writeBoolean(false);
dos.writeChar('O');
dos.writeShort(541);
// CLDC_1_0 dos.writeFloat((float)3.14159);
// CLDC_1_0 dos.writeDouble(Math.E);
dos.writeByte(aKnowndPositiveByte);
dos.writeByte(aKnowndNegativeByte);
if (dataOutputStreamFlush) {
dos.flush();
}
}

static void readDataStream(InputStream is) throws IOException {
DataInputStream dis = new DataInputStream(is);
String got = dis.readUTF();
Assert.assertEquals("ReadString", stringData, got);
Assert.assertEquals("ReadInt", 1025, dis.readInt());
Assert.assertEquals("ReadLong", 567890025, dis.readLong());
Assert.assertEquals("ReadBoolean", true, dis.readBoolean());
Assert.assertEquals("ReadBoolean2", false, dis.readBoolean());
Assert.assertEquals("ReadChar", 'O', dis.readChar());
Assert.assertEquals("readShort", 541, dis.readShort());
// CLDC_1_0 Assert.assertEquals("readFloat", (float)3.14159,
// dis.readFloat(), (float)0.0000001);
// CLDC_1_0 Assert.assertEquals("readDouble", Math.E, dis.readDouble(),
// 0.0000000000000001);
Assert.assertEquals("positiveByte", aKnowndPositiveByte, dis.readByte());
Assert.assertEquals("negativeByte", aKnowndNegativeByte, dis.readByte());
}

private static void sendStreamAvailable(InputStream is, OutputStream os) throws IOException {
for (int i = 1; i < streamAvailableByteCount; i++) {
os.write(i);
if (i % 10 == 0) {
os.flush();
}
}
// Long test need confirmation
os.flush();
byte got = (byte) is.read();
Assert.assertEquals("Confirmation byte", streamAvailableByteCount, got);
}

private static void readStreamAvailable(InputStream is, OutputStream os) throws IOException {
int available = 0;
for (int i = 1; i < streamAvailableByteCount; i++) {
boolean hasData = (available > 0);
int tryCount = 0;
while (!hasData) {
// This blocks on Nokia(Srv) on second call connected to
// Widcomm(Client)
try {
available = is.available();
} catch (IOException e) {
Logger.debug("(m1) Received only " + i + " bytes");
throw e;
}
if (available > 0) {
hasData = true;
} else if (available < 0) {
Assert.fail("negative available");
}
tryCount++;
try {
Thread.sleep(300);
} catch (InterruptedException e) {
Assert.fail("Test Interrupted");
}
if (tryCount > 70) {
Assert.fail("Test Available took too long, got " + i + " bytes");
}
}

byte got;
try {
got = (byte) is.read();
} catch (IOException e) {
Logger.debug("(m2) Received only " + i + " bytes");
throw e;
}
Assert.assertEquals("byte[" + i + "]", i, got);
available--;
}
os.write(streamAvailableByteCount);
os.flush();
}

private static void sendEOF(ConnectionHolderStream c, TestStatus testStatus) throws IOException {
c.os.write(aKnowndPositiveByte);
c.os.flush();
Assert.assertEquals("byte received", aKnowndNegativeByte, (byte) c.is.read());
c.disconnected();
c.os.close();
c.is.close();
testStatus.streamClosed = true;
}

private static void readEOF(InputStream is, OutputStream os, TestStatus testStatus) throws IOException {
Assert.assertEquals("byte", aKnowndPositiveByte, (byte) is.read());
os.write(aKnowndNegativeByte);
os.flush();
long startWait = System.currentTimeMillis();
Assert.assertEquals("EOF expected", -1, is.read());
testStatus.streamClosed = true;
Assert.assertFalse("Took too long to close", TimeUtils.since(startWait) > 7 * 1000);
testStatus.isSuccess = true;
}

private static void sendArayEOF(ConnectionHolderStream c, TestStatus testStatus) throws IOException {
c.os.write(aKnowndPositiveByte);
c.os.write(aKnowndNegativeByte);
c.os.flush();
// Let the server read the message
try {
Thread.sleep(1200);
} catch (InterruptedException e) {
Assert.fail("Test Interrupted");
}
c.disconnected();
c.os.close();
c.is.close();
testStatus.streamClosed = true;
}

private static void readArayEOF(InputStream is, OutputStream os, TestStatus testStatus) throws IOException {
byte[] byteArayGot = new byte[3];
int got = is.read(byteArayGot);
if (got == 1) {
int size = is.read(byteArayGot, 1, 2);
if (size != -1) {
got += size;
}
}
Assert.assertEquals("byteAray.len", 2, got);
Assert.assertEquals("byte1", aKnowndPositiveByte, byteArayGot[0]);
Assert.assertEquals("byte2", aKnowndNegativeByte, byteArayGot[1]);
int got2 = is.read(byteArayGot);
Assert.assertEquals("EOF expected", -1, got2);
testStatus.streamClosed = true;
testStatus.isSuccess = true;
}

private static void sendClosedConnection(ConnectionHolderStream c, TestStatus testStatus) throws IOException {
c.os.write(aKnowndPositiveByte);
c.os.write(aKnowndNegativeByte);
c.os.flush();
// Let the server read the message
try {
Thread.sleep(1200);
} catch (InterruptedException e) {
Assert.fail("Test Interrupted");
}
c.disconnected();
c.os.close();
c.is.close();
testStatus.streamClosed = true;

try {
c.os.write(byteAray);
c.os.flush();
Assert.fail("Can write to closed OutputStream");
} catch (IOException ok) {
testStatus.isSuccess = true;
}
}

private static void readClosedConnection(ConnectionHolderStream c, TestStatus testStatus) throws IOException {
Assert.assertEquals("byte1", aKnowndPositiveByte, (byte) c.is.read());
Assert.assertEquals("byte2", aKnowndNegativeByte, (byte) c.is.read());
testStatus.streamClosed = true;
try {
Assert.assertEquals("EOF expected", -1, c.is.read());
} catch (IOException e) {
if (RuntimeDetect.isBlueCove) {
Logger.error("EOF IOException not expected", e);
Assert.fail("EOF IOException not expected [" + e.toString() + "]");
}
}
c.disconnected();
try {
c.os.write(byteAray);
c.os.flush();
Assert.fail("Can write to closed BT Connection");
} catch (IOException ok) {
testStatus.isSuccess = true;
}
}

private static void serverRemoteDevice(StreamConnection conn, InputStream is, OutputStream os, TestStatus testStatus)
throws IOException {
RemoteDevice device = RemoteDevice.getRemoteDevice(conn);
Logger.debug("is connected to BTAddress " + device.getBluetoothAddress());
DataInputStream dis = new DataInputStream(is);
DataOutputStream dos = new DataOutputStream(os);
String gotBluetoothAddress = dis.readUTF();
Assert.assertEquals("PairBTAddress", gotBluetoothAddress.toUpperCase(), device.getBluetoothAddress()
.toUpperCase());

boolean remoreIsAuthenticated = dis.readBoolean();
boolean remoreIsEncrypted = dis.readBoolean();

boolean isAuthenticated = device.isAuthenticated();
boolean isEncrypted = device.isEncrypted();

dos.writeBoolean(isAuthenticated);
dos.writeBoolean(isEncrypted);
if (dataOutputStreamFlush) {
dos.flush();
}

if (Configuration.authenticate.booleanValue()) {
if (!isAuthenticated) {
Logger.error("wrong isAuthenticated " + isAuthenticated);
testStatus.isError = true;
} else {
Logger.debug("isAuthenticated OK " + isAuthenticated);
}
} else {
Logger.debug("isAuthenticated " + isAuthenticated);
}
if (remoreIsAuthenticated != isAuthenticated) {
Logger.error("this Authenticated " + isAuthenticated + " != remote " + remoreIsAuthenticated);
testStatus.isError = true;
}

if (Configuration.encrypt.booleanValue()) {
if (!isEncrypted) {
Logger.error("wrong isEncrypted " + isEncrypted);
testStatus.isError = true;
} else {
Logger.debug("isEncrypted OK " + isEncrypted);
}
} else if (isEncrypted && (remoreIsEncrypted == isEncrypted)) {
Logger.debug("isEncrypted OK " + isEncrypted);
} else {
Logger.debug("isEncrypted " + isEncrypted);
}
if (remoreIsEncrypted != isEncrypted) {
Logger.error("this Encrypted " + isEncrypted + " != remote " + remoreIsEncrypted);
testStatus.isError = true;
}
}

private static void clientRemoteDevice(StreamConnection conn, InputStream is, OutputStream os, TestStatus testStatus)
throws IOException {
RemoteDevice device = RemoteDevice.getRemoteDevice(conn);
Logger.debug("is connected toBTAddress " + device.getBluetoothAddress());
DataOutputStream dos = new DataOutputStream(os);
DataInputStream dis = new DataInputStream(is);
dos.writeUTF(LocalDevice.getLocalDevice().getBluetoothAddress());
if (dataOutputStreamFlush) {
dos.flush();
}
Assert.assertEquals("PairBTAddress", testStatus.pairBTAddress.toUpperCase(), device.getBluetoothAddress()
.toUpperCase());

boolean isAuthenticated = device.isAuthenticated();
boolean isEncrypted = device.isEncrypted();

dos.writeBoolean(isAuthenticated);
dos.writeBoolean(isEncrypted);
if (dataOutputStreamFlush) {
dos.flush();
}
boolean remoreIsAuthenticated = dis.readBoolean();
boolean remoreIsEncrypted = dis.readBoolean();

if (Configuration.authenticate.booleanValue() == isAuthenticated) {
Logger.debug("isAuthenticated OK " + Configuration.authenticate);
} else if (Configuration.authenticate.booleanValue() && !isAuthenticated) {
Logger.error("wrong isAuthenticated " + isAuthenticated);
} else {
Logger.debug("isAuthenticated OK " + isAuthenticated);
}
if (remoreIsAuthenticated != isAuthenticated) {
Logger.error("this Authenticated " + isAuthenticated + " != remote " + remoreIsAuthenticated);
testStatus.isError = true;
}

if (Configuration.encrypt.booleanValue() == isEncrypted) {
Logger.debug("isEncrypted OK " + Configuration.encrypt);
} else if (Configuration.encrypt.booleanValue() && !isEncrypted) {
Logger.error("wrong isEncrypted " + isEncrypted);
} else {
Logger.debug("isEncrypted OK " + isEncrypted);
}
if (remoreIsEncrypted != isEncrypted) {
Logger.error("this Encrypted " + isEncrypted + " != remote " + remoreIsEncrypted);
testStatus.isError = true;
}
}

static void sendByte4clientToClose(OutputStream os, InputStream is, TestStatus testStatus) throws IOException {
os.write(aKnowndPositiveByte);
os.flush();

TimeUtils.sleep(1 * 1000);

// Do not send any reply to client.
testStatus.streamClosed = true;

long startWait = System.currentTimeMillis();
// wait for client to close connection, This has been tested in
// TEST_EOF_READ
int eof = 0;
try {
eof = is.read();
Assert.assertEquals("EOF expected", -1, eof);
} catch (IOException e) {
Logger.debug("OK conn.closed");
}
Assert.assertFalse("Took too long to close", TimeUtils.since(startWait) > 7 * 1000);
testStatus.isSuccess = true;
}

static void reciveByteAndCloseStream(boolean testArray, final ConnectionHolderStream c, TestStatus testStatus)
throws IOException {
Assert.assertEquals("byte", aKnowndPositiveByte, (byte) c.is.read());
final ValueHolder whenClose = new ValueHolder();
final ValueHolder alreadyClose = new ValueHolder(false);
final ValueHolder whoClose = new ValueHolder();
final boolean debug = true;
Runnable r = new Runnable() {
public void run() {
TimeUtils.sleep(500);
c.disconnected();
if (debug) {
Logger.debug("try to closed");
}
whenClose.valueLong = System.currentTimeMillis();
whoClose.valueInt = 1;
try {
// No effect on Nokia
c.conn.close();
} catch (IOException e) {
Logger.debug("error in conn close", e);
}
if (debug) {
Logger.debug("conn.closed");
}
whenClose.valueLong = System.currentTimeMillis();
TimeUtils.sleep(100);
if (!alreadyClose.valueBoolean) {
TimeUtils.sleep(600);
whenClose.valueLong = System.currentTimeMillis();
whoClose.valueInt = 2;
try {
c.is.close();
} catch (IOException e) {
Logger.debug("error in is close", e);
}
if (debug) {
Logger.debug("is.closed");
}
whenClose.valueLong = System.currentTimeMillis();
TimeUtils.sleep(100);
if (!alreadyClose.valueBoolean) {
TimeUtils.sleep(600);
whenClose.valueLong = System.currentTimeMillis();
whoClose.valueInt = 3;
try {
c.os.close();
} catch (IOException e) {
Logger.debug("error in os close", e);
}
if (debug) {
Logger.debug("os.closed");
}
whenClose.valueLong = System.currentTimeMillis();
}
}
if (debug) {
Logger.debug("close thread finished");
}
}
};
Thread t = RuntimeDetect.cldcStub.createNamedThread(r, "ReciveStreamCloser");
t.start();

testStatus.streamClosed = true;
// This will stuck since server is not sending any more data.
// conn.close() should force read() to throw exception or return -1.
int eof = 0;
try {
if (debug) {
Logger.debug("try to read EOF");
}
// This is function under test
if (testArray) {
byte[] buf = new byte[2];
eof = c.is.read(buf, 0, buf.length);
} else {
eof = c.is.read();
}
Assert.assertEquals("EOF expected", -1, eof);
Logger.debug("OK read on conn.closed GOT EOF");
} catch (IOException e) {
Logger.debug("OK read on conn.closed throws Exception", e);
} finally {
if (debug) {
Logger.debug("read EOF ends");
}
}
alreadyClose.valueBoolean = true;
long returenedDelay = System.currentTimeMillis() - whenClose.valueLong;
if ((returenedDelay < 2 * 1000) || (returenedDelay > -2 * 1000)) {
testStatus.isSuccess = true;
} else {
Assert.fail("Took too long " + (returenedDelay) + " to return");
}
switch (whoClose.valueInt) {
case 1:
Logger.debug("Closed by StreamConnection.close()");
break;
case 2:
Logger.debug("Closed by InputStream.close()");
break;
case 3:
Logger.debug("Closed by OutputStream.close()");
break;
default:
Assert.fail("Closed by unknown source");
}
}

static void sendByteArayLarge(ConnectionHolderStream c, InputStream is, OutputStream os, int araySize)
throws IOException {
long start = System.currentTimeMillis();
c.setTestTimeOutSec(araySize / 1000);
byte[] byteArayLarge = new byte[araySize];
for (int i = 0; i < araySize; i++) {
byteArayLarge[i] = (byte) (i & 0xF);
}
os.write(byteArayLarge);
c.active();
os.flush();
int ok = is.read();
c.active();
Assert.assertEquals("confirmation expected", 1, ok);
Logger.debug("send speed " + TimeUtils.bps(araySize, start));
}

static void readByteArayLarge(ConnectionHolderStream c, InputStream is, OutputStream os, int araySize)
throws IOException {
long start = System.currentTimeMillis();
byte[] byteArayGot = new byte[araySize];
int got = 0;
c.setTestTimeOutSec(araySize / 1000);
boolean readInterrupted = true;
try {
while (got < araySize) {
int read = is.read(byteArayGot, got, araySize - got);
if (read == -1) {
break;
}
got += read;
c.active();
}
readInterrupted = false;
} finally {
if (readInterrupted) {
Logger.debug("Received only " + got);
}
}

Assert.assertEquals("byteArayLarge.len", araySize, got);
for (int i = 0; i < araySize; i++) {
Assert.assertEquals("byte", (i & 0xF), byteArayGot[i]);
}
os.write(1);
c.active();
os.flush();
Logger.debug("read speed " + TimeUtils.bps(araySize, start));
}

public static void runTest(int testType, boolean server, ConnectionHolderStream c, TestStatus testStatus)
throws IOException {
InputStream is = c.is;
OutputStream os = c.os;
switch (testType) {
case TEST_STRING:
testStatus.setName("STRING");
if (server) {
CommunicationTester.readString(is);
} else {
CommunicationTester.sendString(os);
}
break;
case TEST_STRING_BACK:
testStatus.setName("STRING_BACK");
if (!server) {
CommunicationTester.readString(is);
} else {
CommunicationTester.sendString(os);
}
break;
case TEST_BYTE:
testStatus.setName("BYTE");
if (server) {
CommunicationTester.readByte(is);
} else {
CommunicationTester.sendByte(os);
}
break;
case TEST_BYTE_BACK:
testStatus.setName("BYTE_BACK");
if (!server) {
CommunicationTester.readByte(is);
} else {
CommunicationTester.sendByte(os);
}
break;
case TEST_STRING_UTF:
testStatus.setName("STRING_UTF");
if (server) {
CommunicationTester.readUTFString(is);
} else {
CommunicationTester.sendUTFString(os);
}
break;
case TEST_STRING_UTF_BACK:
testStatus.setName("STRING_UTF_BACK");
if (!server) {
CommunicationTester.readUTFString(is);
} else {
CommunicationTester.sendUTFString(os);
}
break;
case TEST_BYTE_ARRAY:
testStatus.setName("BYTE_ARRAY");
if (server) {
CommunicationTester.readByteAray(is);
} else {
CommunicationTester.sendByteAray(os);
}
break;
case TEST_BYTE_ARRAY_BACK:
testStatus.setName("BYTE_ARRAY_BACK");
if (!server) {
CommunicationTester.readByteAray(is);
} else {
CommunicationTester.sendByteAray(os);
}
break;
case TEST_DataStream:
testStatus.setName("DataStream");
if (server) {
CommunicationTester.readDataStream(is);
} else {
CommunicationTester.sendDataStream(os);
}
break;
case TEST_DataStream_BACK:
testStatus.setName("DataStream_BACK");
if (!server) {
CommunicationTester.readDataStream(is);
} else {
CommunicationTester.sendDataStream(os);
}
break;
case TEST_StreamAvailable:
testStatus.setName("StreamAvailable");
if (server) {
CommunicationTester.readStreamAvailable(is, os);
} else {
CommunicationTester.sendStreamAvailable(is, os);
}
break;
case TEST_StreamAvailable_BACK:
testStatus.setName("StreamAvailable_BACK");
if (!server) {
CommunicationTester.readStreamAvailable(is, os);
} else {
CommunicationTester.sendStreamAvailable(is, os);
}
break;
case TEST_EOF_READ:
testStatus.setName("EOF_READ");
if (server) {
CommunicationTester.readEOF(is, os, testStatus);
} else {
CommunicationTester.sendEOF(c, testStatus);
}
break;
case TEST_EOF_READ_BACK:
testStatus.setName("EOF_READ_BACK");
if (!server) {
CommunicationTester.readEOF(is, os, testStatus);
} else {
CommunicationTester.sendEOF(c, testStatus);
}
break;
case TEST_EOF_READ_ARRAY:
testStatus.setName("EOF_READ_ARRAY");
if (server) {
CommunicationTester.readArayEOF(is, os, testStatus);
} else {
CommunicationTester.sendArayEOF(c, testStatus);
}
break;
case TEST_EOF_READ_ARRAY_BACK:
testStatus.setName("EOF_READ_ARRAY_BACK");
if (!server) {
CommunicationTester.readArayEOF(is, os, testStatus);
} else {
CommunicationTester.sendArayEOF(c, testStatus);
}
break;
case TEST_CONNECTION_INFO:
testStatus.setName("TEST_CONNECTION_INFO");
if (server) {
CommunicationTester.serverRemoteDevice(c.conn, is, os, testStatus);
} else {
CommunicationTester.clientRemoteDevice(c.conn, is, os, testStatus);
}
break;
case TEST_CLOSED_CONNECTION:
testStatus.setName("CLOSED_CONNECTION");
if (server) {
CommunicationTester.readClosedConnection(c, testStatus);
} else {
CommunicationTester.sendClosedConnection(c, testStatus);
}
break;
case TEST_CLOSED_CONNECTION_BACK:
testStatus.setName("CLOSED_CONNECTION_BACK");
if (!server) {
CommunicationTester.readClosedConnection(c, testStatus);
} else {
CommunicationTester.sendClosedConnection(c, testStatus);
}
break;
case TEST_BYTES_256:
testStatus.setName("BYTES256");
if (server) {
CommunicationTester.readBytes256(is);
} else {
CommunicationTester.sendBytes256(os);
}
break;
case TEST_BYTES_256_BACK:
testStatus.setName("BYTES256_BACK");
if (!server) {
CommunicationTester.readBytes256(is);
} else {
CommunicationTester.sendBytes256(os);
}
break;
case TEST_CAN_CLOSE_READ_ON_CLIENT:
testStatus.setName("CAN_CLOSE_READ_ON_CLIENT");
if (server) {
CommunicationTester.sendByte4clientToClose(os, is, testStatus);
} else {
CommunicationTester.reciveByteAndCloseStream(false, c, testStatus);
}
break;
case TEST_CAN_CLOSE_READ_ON_SERVER:
testStatus.setName("CAN_CLOSE_READ_ON_SERVER");
if (!server) {
CommunicationTester.sendByte4clientToClose(os, is, testStatus);
} else {
CommunicationTester.reciveByteAndCloseStream(false, c, testStatus);
}
break;
case TEST_CAN_CLOSE_READ_ARRAY_ON_CLIENT:
testStatus.setName("CAN_CLOSE_READ_ARRAY_ON_CLIENT");
if (server) {
CommunicationTester.sendByte4clientToClose(os, is, testStatus);
} else {
CommunicationTester.reciveByteAndCloseStream(true, c, testStatus);
}
break;
case TEST_CAN_CLOSE_READ_ARRAY_ON_SERVER:
testStatus.setName("CAN_CLOSE_READ_ARRAY_ON_SERVER");
if (!server) {
CommunicationTester.sendByte4clientToClose(os, is, testStatus);
} else {
CommunicationTester.reciveByteAndCloseStream(true, c, testStatus);
}
break;
case TEST_TWO_THREADS_SYNC_BYTES:
testStatus.setName("TWO_THREADS_SYNC_BYTES");
TwoThreadsPerConnection.start(c, 1, true);
break;
case TEST_TWO_THREADS_SYNC_ARRAYS:
testStatus.setName("TWO_THREADS_SYNC_ARRAYS");
TwoThreadsPerConnection.start(c, 64, true);
break;
case TEST_TWO_THREADS_BYTES:
testStatus.setName("TWO_THREADS_BYTES");
TwoThreadsPerConnection.start(c, 1, false);
break;
case TEST_TWO_THREADS_ARRAYS:
testStatus.setName("TWO_THREADS_ARRAYS");
TwoThreadsPerConnection.start(c, 64, false);
break;
case TEST_8K_PLUS_BYTE_ARRAY:
testStatus.setName("8K_PLUS_BYTE_ARRAY");
if (server) {
CommunicationTester.readByteArayLarge(c, is, os, byteAray8KPlusSize);
} else {
CommunicationTester.sendByteArayLarge(c, is, os, byteAray8KPlusSize);
}
break;
case TEST_8K_PLUS_BYTE_ARRAY_BACK:
testStatus.setName("8K_PLUS_BYTE_ARRAY_BACK");
if (!server) {
CommunicationTester.readByteArayLarge(c, is, os, byteAray8KPlusSize);
} else {
CommunicationTester.sendByteArayLarge(c, is, os, byteAray8KPlusSize);
}
break;
case TEST_64K_PLUS_BYTE_ARRAY:
testStatus.setName("64K_PLUS_BYTE_ARRAY");
if (server) {
CommunicationTester.readByteArayLarge(c, is, os, byteAray64KPlusSize);
} else {
CommunicationTester.sendByteArayLarge(c, is, os, byteAray64KPlusSize);
}
break;
case TEST_64K_PLUS_BYTE_ARRAY_BACK:
testStatus.setName("64K_PLUS_BYTE_ARRAY_BACK");
if (!server) {
CommunicationTester.readByteArayLarge(c, is, os, byteAray64KPlusSize);
} else {
CommunicationTester.sendByteArayLarge(c, is, os, byteAray64KPlusSize);
}
break;

case TEST_128K_BYTE_ARRAY_X_10:
testStatus.setName("128K_BYTE_ARRAY_X_10");
for (int i = 0; i < 10; i++) {
if (server) {
CommunicationTester.readByteArayLarge(c, is, os, byteAray128KSize);
} else {
CommunicationTester.sendByteArayLarge(c, is, os, byteAray128KSize);
}
}
break;
case TEST_128K_BYTE_ARRAY_X_10_BACK:
testStatus.setName("128K_BYTE_ARRAY_X_10_BACK");
for (int i = 0; i < 10; i++) {
if (!server) {
CommunicationTester.readByteArayLarge(c, is, os, byteAray128KSize);
} else {
CommunicationTester.sendByteArayLarge(c, is, os, byteAray128KSize);
}
}
break;

// ---- TRAFFIC GENERATORS
case TRAFFIC_GENERATOR_WRITE:
testStatus.setName("RFgenW");
if (server) {
Config cfg = RfTrafficGenerator.getConfig(c, server, "RF write");
if (cfg != null) {
RfTrafficGenerator.trafficGeneratorStatusReadStart(c, testStatus);
RfTrafficGenerator.trafficGeneratorWrite(c, cfg, testStatus);
}
} else {
RfTrafficGenerator.trafficGeneratorClientInit(c);
RfTrafficGenerator.trafficGeneratorRead(c, server, testStatus);
}
break;
case TRAFFIC_GENERATOR_READ:
testStatus.setName("RFgenR");
if (server) {
RfTrafficGenerator.trafficGeneratorRead(c, server, testStatus);
} else {
RfTrafficGenerator.trafficGeneratorClientInit(c);
Config cfg = RfTrafficGenerator.getConfig(c, server, "RF write");
if (cfg != null) {
RfTrafficGenerator.trafficGeneratorStatusReadStart(c, testStatus);
RfTrafficGenerator.trafficGeneratorWrite(c, cfg, testStatus);
}
}
break;
case TRAFFIC_GENERATOR_READ_WRITE:
testStatus.setName("RFgenRW");
if (!server) {
RfTrafficGenerator.trafficGeneratorClientInit(c);
}
Config cfg = RfTrafficGenerator.getConfig(c, server, "RF write");
if (cfg != null) {
RfTrafficGenerator.trafficGeneratorReadStart(c, server, testStatus);
RfTrafficGenerator.trafficGeneratorWrite(c, cfg, testStatus);
}
break;
case TEST_SERVER_TERMINATE:
return;
default:
Assert.fail("Invalid test#" + testType);
}
}
}

Change log

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

Older revisions

r2607 by skarzhevskyy on Dec 17, 2008   Diff
OBEX tests in J2ME
r2508 by skarzhevskyy on Dec 5, 2008   Diff
Fixed TRAFFIC GENERATOR
initializations
r2471 by skarzhevskyy on Nov 30, 2008   Diff
Change license to Apache License,
Version 2.0, Update headers
All revisions of this file

File info

Size: 29980 bytes, 966 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting