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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sal.hxx"
/** test coder preface:
1. the BSD socket function will meet "unresolved external symbol error" on Windows platform
if you are not including ws2_32.lib in makefile.mk, the including format will be like this:
.IF "$(GUI)" == "WNT"
SHL1STDLIBS += $(SOLARLIBDIR)$/cppunit.lib
SHL1STDLIBS += ws2_32.lib
.ENDIF
likewise on Solaris platform.
.IF "$(GUI)" == "UNX"
SHL1STDLIBS+=$(SOLARLIBDIR)$/libcppunit$(DLLPOSTFIX).a
SHL1STDLIBS += -lsocket -ldl -lnsl
.ENDIF
2. since the Socket implementation of osl is only IPv4 oriented, our test are mainly focus on IPv4
category.
3. some fragment of Socket source implementation are lack of comment so it is hard for testers
guess what the exact functionality or usage of a member. Hope the Socket section's comment
will be added.
4. following functions are declared but not implemented:
inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
*/
#include <testshl/simpleheader.hxx>
#include "sockethelper.hxx"
#include <osl/conditn.hxx>
using namespace osl;
using ::rtl::OUString;
using ::rtl::OString;
#define IP_PORT_MYPORT9 8897
#define IP_PORT_MYPORT10 18900
const char * pTestString1 = "test socket";
const char * pTestString2 = " Passed#OK";
//------------------------------------------------------------------------
// helper functions
//------------------------------------------------------------------------
// just used to test socket::close() when accepting
class AcceptorThread : public Thread
{
::osl::AcceptorSocket asAcceptorSocket;
::rtl::OUString aHostIP;
sal_Bool bOK;
protected:
void SAL_CALL run( )
{
::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
::osl::StreamSocket ssStreamConnection;
asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
if ( sal_True != bOK1 )
{
t_print("# AcceptorSocket bind address failed.\n" ) ;
return;
}
sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
if ( sal_True != bOK2 )
{
t_print("# AcceptorSocket listen address failed.\n" ) ;
return;
}
asAcceptorSocket.enableNonBlockingMode( sal_False );
oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
if (eResult != osl_Socket_Ok )
{
bOK = sal_True;
t_print("AcceptorThread: acceptConnection failed! \n");
}
}
public:
AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP )
: asAcceptorSocket( asSocket ), aHostIP( aBindIP )
{
bOK = sal_False;
}
sal_Bool isOK() { return bOK; }
~AcceptorThread( )
{
if ( isRunning( ) )
{
asAcceptorSocket.shutdown();
t_print("# error: Acceptor thread not terminated.\n" );
}
}
};
/** Server Socket Thread, served as a temp little server to communicate with client.
*/
class ServerSocketThread : public Thread
{
osl::Condition &m_aCondition;
protected:
oslThreadIdentifier m_id;
void SAL_CALL run( )
{
::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
::osl::SocketAddr saLocalSocketAddr( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("127.0.0.1")), IP_PORT_MYPORT9 );
::osl::StreamSocket ssStreamConnection;
//if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
while ( schedule( ) == sal_True )
{
sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
if ( sal_True != bOK1 )
{
t_print("# ServerSocketThread: AcceptorSocket bind address failed.\n" ) ;
break;
}
sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
if ( sal_True != bOK2 )
{
t_print("# ServerSocketThread: AcceptorSocket listen address failed.\n" ) ;
break;
}
asAcceptorSocket.enableNonBlockingMode( sal_False );
m_aCondition.set();
oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
if (eResult != osl_Socket_Ok )
{
t_print("ServerSocketThread: acceptConnection failed! \n");
break;
}
sal_Int32 nReadNumber1 = ssStreamConnection.recv( pReadBuffer, 11 );
sal_Int32 nReadNumber2 = ssStreamConnection.recv( pReadBuffer + nReadNumber1, 11 );
pReadBuffer[nReadNumber1 + nReadNumber2] = '\0';
//t_print("# read buffer content: %s\n", pReadBuffer );
break;
}
ssStreamConnection.close();
asAcceptorSocket.close();
}
void SAL_CALL onTerminated( )
{
//t_print("# normally terminate this server thread %d!\n", m_id );
}
public:
// public to check if data transmition is OK
sal_Char pReadBuffer[30];
ServerSocketThread( osl::Condition &_aCond ):m_aCondition(_aCond)
{
m_aCondition.reset();
t_print("#init ServerSocketThread\n");
m_id = getIdentifier( );
//t_print("# successfully creat this ServerSocketThread %d!\n", m_id );
}
~ServerSocketThread( )
{
if ( isRunning( ) )
t_print("# error: ServerSocketThread has not terminated.\n" );
}
};
/** Client Socket Thread, served as a temp little client to communicate with server.
*/
class ClientSocketThread : public Thread
{
protected:
osl::Condition &m_aCondition;
oslThreadIdentifier m_id;
::osl::SocketAddr m_saTargetSocketAddr;
::osl::ConnectorSocket m_csConnectorSocket;
void SAL_CALL run( )
{
TimeValue *pTimeout;
pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
pTimeout->Seconds = 5;
pTimeout->Nanosec = 0;
/// if the thread should terminate, schedule return false
//while ( schedule( ) == sal_True )
//{
if ( osl::Condition::result_ok != m_aCondition.wait( pTimeout ) )
{
free( pTimeout );
return;
}
if ( osl_Socket_Ok == m_csConnectorSocket.connect( m_saTargetSocketAddr, pTimeout ))
{
m_csConnectorSocket.send( pTestString1, 11 ); // "test socket"
m_csConnectorSocket.send( pTestString2, 10);
}
else
t_print("# ClientSocketThread: connect failed! \n");
// terminate();
//}
m_csConnectorSocket.close();
free( pTimeout );
}
void SAL_CALL onTerminated( )
{
//t_print("# normally terminate this thread %d!\n", m_id );
}
public:
ClientSocketThread( osl::Condition &_aCond ):
m_aCondition(_aCond),
m_saTargetSocketAddr( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("127.0.0.1")), IP_PORT_MYPORT9 ),
m_csConnectorSocket( )
{
m_id = getIdentifier( );
//t_print("# successfully creat this client thread %d!\n", m_id );
}
~ClientSocketThread( )
{
if ( isRunning( ) )
t_print("# error: client thread not terminated.\n" );
}
};
// -----------------------------------------------------------------------------
// Helper functions, to create buffers, check buffers
class ValueCheckProvider
{
bool m_bFoundFailure;
char *m_pBuffer;
sal_Int32 m_nBufferSize;
public:
ValueCheckProvider()
:m_bFoundFailure(false),
m_pBuffer(NULL),
m_nBufferSize(0)
{
}
bool isFailure() {return m_bFoundFailure;}
const char* getBuffer() {return m_pBuffer;}
char* getWriteBuffer() {return m_pBuffer;}
sal_Int32 getBufferSize() {return m_nBufferSize;}
bool checkValues(sal_Int32 _nLength, int _nValue)
{
m_bFoundFailure = false;
for(sal_Int32 i=0;i<_nLength;i++)
{
if (m_pBuffer[i] != _nValue)
{
m_bFoundFailure = true;
}
}
return m_bFoundFailure;
}
void createBuffer(sal_Int32 _nLength, int _nValue)
{
m_nBufferSize = _nLength;
m_pBuffer = (char*) malloc(m_nBufferSize);
if (m_pBuffer)
{
memset(m_pBuffer, _nValue, m_nBufferSize);
}
}
void freeBuffer()
{
if (m_pBuffer) free(m_pBuffer);
}
};
// -----------------------------------------------------------------------------
/** Client Socket Thread, served as a temp little client to communicate with server.
*/
class ReadSocketThread : public Thread
{
ValueCheckProvider m_aValues;
int m_nValue;
osl::Condition &m_aCondition;
protected:
oslThreadIdentifier m_id;
void SAL_CALL run( )
{
::osl::SocketAddr m_aTargetSocketAddr( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("127.0.0.1")), IP_PORT_MYPORT10 );
::osl::ConnectorSocket m_aConnectorSocket;
if (! m_aTargetSocketAddr.is())
{
t_print("# SocketAddr was NOT created successfully!\n");
}
else
{
t_print("start ReadSocketThread\n");
TimeValue *pTimeout;
pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
pTimeout->Seconds = 5;
pTimeout->Nanosec = 0;
m_aCondition.wait();
t_print("connect()\n");
oslSocketResult eResult = m_aConnectorSocket.connect( m_aTargetSocketAddr, pTimeout );
if ( osl_Socket_Ok == eResult )
{
sal_Int32 nReadCount = m_aConnectorSocket.read( m_aValues.getWriteBuffer(), m_aValues.getBufferSize() );
m_aValues.checkValues(nReadCount, m_nValue);
}
else
{
t_print("# ReadSocketThread: connect failed! \n");
printSocketResult(eResult);
}
//remove this line for deadlock on solaris( margritte.germany )
m_aConnectorSocket.close();
free( pTimeout );
}
}
void SAL_CALL onTerminated( )
{
//t_print("# normally terminate this thread %d!\n", m_id );
}
public:
sal_Int32 getCount() {return m_aValues.getBufferSize();}
bool isOk() {return m_aValues.isFailure() == true ? false : true;}
ReadSocketThread(sal_Int32 _nBufferSize, int _nValue, osl::Condition &_aCond )
: m_nValue( _nValue ),
m_aCondition(_aCond)
{
t_print("#init ReadSocketThread\n");
m_id = getIdentifier( );
//t_print("# successfully creat this client thread %d!\n", m_id );
m_aValues.createBuffer(_nBufferSize, 0);
}
~ReadSocketThread( )
{
if ( isRunning( ) )
t_print("# error: client thread not terminated.\n" );
m_aValues.freeBuffer();
}
};
/** Server Socket Thread, write a file which is large
*/
class WriteSocketThread : public Thread
{
ValueCheckProvider m_aValues;
osl::Condition &m_aCondition;
protected:
oslThreadIdentifier m_id;
void SAL_CALL run( )
{
t_print("start WriteSocketThread\n");
::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
::osl::SocketAddr saLocalSocketAddr( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("127.0.0.1")), IP_PORT_MYPORT10 );
if (! saLocalSocketAddr.is())
{
t_print("LocalSocketAddr was NOT created successfully!\n");
}
::osl::StreamSocket ssStreamConnection;
//if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
/// if the thread should terminate, schedule return false
// while ( schedule( ) == sal_True )
// {
t_print("bind()\n");
sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
if ( sal_True != bOK1 )
{
t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ;
}
else
{
t_print("listen()\n");
sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
if ( sal_True != bOK2 )
{
t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ;
}
else
{
// blocking mode, if read/recv failed, block until success
asAcceptorSocket.enableNonBlockingMode( sal_False);
t_print("acceptConnection()\n");
m_aCondition.set();
oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
if (eResult != osl_Socket_Ok )
{
t_print("WriteSocketThread: acceptConnection failed! \n");
}
else
{
t_print("write()\n");
ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() );
t_print("done written.\n");
}
}
}
ssStreamConnection.close();
asAcceptorSocket.close();
}
void SAL_CALL onTerminated( )
{
//t_print("# normally terminate this server thread %d!\n", m_id );
}
public:
// public to check if data transmition is OK
WriteSocketThread(sal_Int32 _nBufferSize, int _nValue, osl::Condition &_aCond )
: m_aCondition(_aCond)
{
m_aCondition.reset();
t_print("#init WriteSocketThread\n");
m_id = getIdentifier( );
//t_print("# successfully creat this server thread %d!\n", m_id );
m_aValues.createBuffer(_nBufferSize, _nValue);
}
~WriteSocketThread( )
{
if ( isRunning( ) )
t_print("# error: server thread not terminated.\n" );
m_aValues.freeBuffer();
}
};
// -----------------------------------------------------------------------------
namespace osl_StreamSocket
{
/** testing the methods:
inline StreamSocket(oslAddrFamily Family = osl_Socket_FamilyInet,
oslProtocol Protocol = osl_Socket_ProtocolIp,
oslSocketType Type = osl_Socket_TypeStream);
inline StreamSocket( const StreamSocket & );
inline StreamSocket( oslSocket Socket , __sal_NoAcquire noacquire );
inline StreamSocket( oslSocket Socket );
*/
class ctors : public CppUnit::TestFixture
{
public:
oslSocket sHandle;
// initialization
void setUp( )
{
sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
}
void tearDown( )
{
sHandle = NULL;
}
void ctors_none()
{
/// Socket constructor.
::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the stream socket was created successfully.",
osl_Socket_TypeStream == ssSocket.getType( ) );
}
void ctors_acquire()
{
/// Socket constructor.
::osl::StreamSocket ssSocket( sHandle );
CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully",
osl_Socket_TypeStream == ssSocket.getType( ) );
}
void ctors_no_acquire()
{
/// Socket constructor.
::osl::StreamSocket ssSocket( sHandle, SAL_NO_ACQUIRE );
CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully",
osl_Socket_TypeStream == ssSocket.getType( ) );
}
void ctors_copy_ctor()
{
/// Socket constructor.
::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
/// Socket copy constructor.
::osl::StreamSocket copySocket( ssSocket );
CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor",
osl_Socket_TypeStream == copySocket.getType( ) );
}
CPPUNIT_TEST_SUITE( ctors );
CPPUNIT_TEST( ctors_none );
CPPUNIT_TEST( ctors_acquire );
CPPUNIT_TEST( ctors_no_acquire );
CPPUNIT_TEST( ctors_copy_ctor );
CPPUNIT_TEST_SUITE_END();
}; // class ctors
class send_recv: public CppUnit::TestFixture
{
public:
// initialization
void setUp( )
{
}
void tearDown( )
{
}
void send_recv1()
{
osl::Condition aCondition;
//client sent two strings, and server received, check the order and value
ServerSocketThread myServerThread( aCondition );
ClientSocketThread myClientThread( aCondition );
myServerThread.create( );
myClientThread.create( );
//wait until the thread terminate
myClientThread.join( );
myServerThread.join( );
sal_Char myStr[30] = "";
strcat( myStr, pTestString1 );
strcat( myStr, pTestString2 );
sal_Int32 nRes = strcmp( myServerThread.pReadBuffer, myStr );
CPPUNIT_ASSERT_MESSAGE(" test for send/recv with two threads: launch Server/Client threads, send data from client, check received data in Server thread.",
nRes == 0 );
}
// error when recv
void send_recv2()
{
::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
::osl::SocketAddr saLocalSocketAddr( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("127.0.0.1")), IP_PORT_MYPORT9 );
::osl::StreamSocket ssStreamConnection;
sal_Char pReadBuffer[30] = "";
osl::Condition aCondition;
aCondition.reset();
ClientSocketThread myClientThread( aCondition );
myClientThread.create( );
asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
asAcceptorSocket.bind( saLocalSocketAddr );
asAcceptorSocket.listen( 1 );
asAcceptorSocket.enableNonBlockingMode( sal_True );
aCondition.set();
asAcceptorSocket.acceptConnection( ssStreamConnection );
sal_Int32 nReadNumber = ssStreamConnection.recv( pReadBuffer, 11 );
myClientThread.join( ) ;
ssStreamConnection.close();
asAcceptorSocket.close();
CPPUNIT_ASSERT_MESSAGE(" test for send/recv, recv error!", nReadNumber == -1 );
}
// LLA: This is a helper function, which create 2 threads, a server and a client.
// the server writes the buffersize to the client.
void write_read(sal_Int32 _nBufferSize, int _nValue)
{
//client sent two strings, and server received, check the order and value
osl::Condition aCondition;
WriteSocketThread myServerThread(_nBufferSize, _nValue, aCondition);
ReadSocketThread myClientThread(_nBufferSize, _nValue, aCondition);
myServerThread.create( );
// thread_sleep( 1 );
myClientThread.create( );
//wait until the thread terminate
myClientThread.join( );
myServerThread.join( );
//Maximum Packet Size is ( ARPANET, MILNET = 1007 Ethernet (10Mb) = 1500
// Proteon PRONET = 2046), so here test read 4000 bytes
sal_Int32 nLength = myClientThread.getCount();
bool bIsOk = myClientThread.isOk(); // check if the values are right.
t_print("Length:=%d\n", nLength);
t_print(" bIsOk:=%d\n", bIsOk);
CPPUNIT_ASSERT_MESSAGE(" test for write/read values with two threads: send data from server, check readed data in client.",
nLength == _nBufferSize && bIsOk == true);
}
// Tests with different values and sizes
void write_read_001()
{
write_read(50, 10);
}
void write_read_002()
{
write_read(1024, 20);
}
void write_read_003()
{
write_read(4000, 1);
}
void write_read_004()
{
write_read(8192, 3);
}
void write_read_005()
{
write_read(32768, 3);
}
CPPUNIT_TEST_SUITE( send_recv );
CPPUNIT_TEST( write_read_001 );
CPPUNIT_TEST( write_read_002 );
CPPUNIT_TEST( write_read_003 );
CPPUNIT_TEST( write_read_004 );
CPPUNIT_TEST( write_read_005 );
CPPUNIT_TEST( send_recv1 );
CPPUNIT_TEST( send_recv2 );
// CPPUNIT_TEST( write_read );
CPPUNIT_TEST_SUITE_END();
}; // class send_recv
// -----------------------------------------------------------------------------
class SendClientThread : public Thread
{
protected:
::osl::SocketAddr m_saTargetSocketAddr;
::osl::ConnectorSocket m_csConnectorSocket;
void SAL_CALL run( )
{
TimeValue *pTimeout;
pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
pTimeout->Seconds = 5;
pTimeout->Nanosec = 0;
if ( osl_Socket_Ok == m_csConnectorSocket.connect( m_saTargetSocketAddr, pTimeout ))
{
sal_Int32 nWrite1 = m_csConnectorSocket.write( pTestString1, 11 ); // "test socket"
sal_Int32 nWrite2 = m_csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
thread_sleep( 2 );
m_csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
t_print("nWrite1 is %d, nWrite2 is %d\n", nWrite1, nWrite2 );
//thread_sleep( 1 );
}
else
t_print("# SendClientThread: connect failed! \n");
m_csConnectorSocket.close();
free( pTimeout );
}
public:
SendClientThread( ):
m_saTargetSocketAddr( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("127.0.0.1")), IP_PORT_MYPORT9 ),
m_csConnectorSocket( )
{
//t_print("# successfully creat this SendClientThread %d!\n", m_id );
}
~SendClientThread( )
{
if ( isRunning( ) )
t_print("# error: SendClientThread has not terminated.\n" );
}
};
class shutdown: public CppUnit::TestFixture
{
public:
// initialization
void setUp( )
{
}
void tearDown( )
{
}
// similar to close_002
void shutdown_001()
{
#if defined(LINUX)
::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
AcceptorThread myAcceptorThread( asSocket, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("127.0.0.1")) );
myAcceptorThread.create();
thread_sleep( 1 );
//when accepting, shutdown the socket, the thread will not block for accepting
asSocket.shutdown();
myAcceptorThread.join();
CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
myAcceptorThread.isOK( ) == sal_True );
#endif
}
void shutdown_002()
{
::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
::osl::SocketAddr saLocalSocketAddr( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("127.0.0.1")), IP_PORT_MYPORT9);
asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True);
CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True );
sal_Char pReadBuffer[40];
// osl::Condition aCondition;
SendClientThread mySendThread;
mySendThread.create();
asSocket.enableNonBlockingMode( sal_False );
::osl::StreamSocket ssConnectionSocket;
oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok );
/* set socket option SO_LINGER 0, so close immediatly */
linger aLingerSet;
sal_Int32 nBufferLen = sizeof( struct linger );
aLingerSet.l_onoff = 0;
aLingerSet.l_linger = 0;
ssConnectionSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen );
thread_sleep( 1 );
//sal_uInt32 nRecv1 = 0;
sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
//shutdown read after client the first send complete
ssConnectionSocket.shutdown( osl_Socket_DirRead );
sal_Int32 nRead2 = ssConnectionSocket.read( pReadBuffer + nRead1, 12 );
sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 + nRead2, 12 );
t_print("after read 2, nRead1 is %d, nRead2 is %d, nRead3 is %d \n", nRead1, nRead2, nRead3 );
mySendThread.join();
ssConnectionSocket.close();
asSocket.close();
/* on Linux, if send is before shutdown(DirRead), can read, nRecv2 still > 0,
http://dbforums.com/arch/186/2002/12/586417
While on Solaris, after shutdown(DirRead), all read will return 0
*/
#ifdef LINUX
CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).",
nRead1 > 0 && nRead3 == 0 );
#else
CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).",
nRead1 > 0 && nRead2 == 0 && nRead3 == 0 );
#endif
}
void shutdown_003()
{
::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
::osl::SocketAddr saLocalSocketAddr( rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("127.0.0.1")), IP_PORT_MYPORT9);
asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True);
CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True );
sal_Char pReadBuffer[40];
osl::Condition aCondition;
SendClientThread mySendThread;
mySendThread.create();
asSocket.enableNonBlockingMode( sal_False );
::osl::StreamSocket ssConnectionSocket;
oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok );
thread_sleep( 1 );
//shutdown write after client the first send complete
ssConnectionSocket.shutdown( osl_Socket_DirWrite );
// recv should not shutdown
sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
sal_Int32 nWrite = ssConnectionSocket.write( pReadBuffer, 11 );
// still can read
sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 , 12 );
t_print("after read 2, nRead1 is %d, nWrite is %d, nRead3 is %d\n", nRead1, nWrite, nRead3 );
mySendThread.join();
ssConnectionSocket.close();
asSocket.close();
CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not send(write).",
nRead1 > 0 && nWrite == 0 && nRead3 > 0);
}
CPPUNIT_TEST_SUITE( shutdown );
CPPUNIT_TEST( shutdown_001 );
CPPUNIT_TEST( shutdown_002 );
CPPUNIT_TEST( shutdown_003 );
CPPUNIT_TEST_SUITE_END();
}; // class shutdown
class isExceptionPending: public CppUnit::TestFixture
{
public:
void isExPending_001()
{
::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
TimeValue *pTimeout;
pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
pTimeout->Seconds = 3;
pTimeout->Nanosec = 0;
sal_Bool bOk = asSocket.isExceptionPending( pTimeout );
free( pTimeout );
CPPUNIT_ASSERT_MESSAGE( "test for isExceptionPending.",
bOk == sal_False );
}
/**tester's comments: lack of a case that return sal_True, do not know when it will return sal_True*/
CPPUNIT_TEST_SUITE( isExceptionPending );
CPPUNIT_TEST( isExPending_001 );
CPPUNIT_TEST_SUITE_END();
}; // class isExceptionPending
// -----------------------------------------------------------------------------
/** Client Socket Thread, served as a temp little client to communicate with server.
*/
#define IP_PORT_TEST 8900
class ReadSocket2Thread : public Thread
{
osl::Condition &m_aCondition;
char* m_pBuffer;
sal_Int32 m_nBufferSize;
sal_Int32 m_nReadCount;
rtl::OString m_sAddr;
bool m_bOk;
void setFailed()
{
m_bOk = false;
}
protected:
oslThreadIdentifier m_id;
void read()
{
if (m_sAddr.getLength() == 0)
{
setFailed();
return;
}
// 10.16.66.252
::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(m_sAddr.getStr()), IP_PORT_TEST );
::osl::ConnectorSocket aSocket; // ( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
m_aCondition.wait();
t_print("wait done\n");
TimeValue *pTimeout;
pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
pTimeout->Seconds = 20;
pTimeout->Nanosec = 0;
// blocking mode, if read/recv failed, block until success
t_print("enableNonBlockingMode(false)\n");
aSocket.enableNonBlockingMode( sal_False );
t_print("connect()\n");
oslSocketResult eResult = aSocket.connect( aSocketAddr, pTimeout );
if ( osl_Socket_Ok == eResult)
{
if (m_pBuffer)
{
t_print("read()\n");
m_nReadCount = aSocket.read( m_pBuffer, m_nBufferSize );
t_print("%d bytes recived.\n", m_nReadCount);
}
}
else
{
t_print("# ReadSocket2Thread: connect failed! \n");
printSocketResult(eResult);
setFailed();
}
//remove this line for deadlock on solaris( margritte.germany )
aSocket.close();
free( pTimeout );
}
void SAL_CALL run( )
{
read();
}
void SAL_CALL onTerminated( )
{
//t_print("# normally terminate this thread %d!\n", m_id );
}
public:
sal_Int32 getCount() {return m_nReadCount;}
bool isOk() {return m_nReadCount == 0 ? false : true;}
bool getFailed() {return m_bOk == false ? true : false;}
ReadSocket2Thread(osl::Condition &_aCondition)
:m_aCondition(_aCondition),
m_nReadCount(0),
m_bOk( true )
{
m_aCondition.reset();
m_pBuffer = (char*) malloc(1024);
if (m_pBuffer)
{
m_nBufferSize = 1024;
}
m_id = getIdentifier( );
//t_print("# successfully creat this client thread %d!\n", m_id );
}
void setAddr(rtl::OString const& _sAddr)
{
m_sAddr = _sAddr;
}
~ReadSocket2Thread( )
{
if ( isRunning( ) )
t_print("# error: client thread not terminated.\n" );
free(m_pBuffer);
}
};
// -----------------------------------------------------------------------------
class justtest : public CppUnit::TestFixture
{
void send_Acceptor(rtl::OString const& _sAddr, osl::Condition &)
{
::osl::AcceptorSocket aSocket; // ( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
::osl::SocketAddr aSocketAddr;
if (! aSocketAddr.setPort(IP_PORT_TEST))
{
t_print("# cant set port\n" );
}
if (! aSocketAddr.setHostname(rtl::OUString::createFromAscii(_sAddr.getStr())))
{
t_print("# cant set hostname/ip\n" );
}
rtl::OUString aHostname = aSocketAddr.getHostname();
aSocketAddr.getPort();
//if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
/// if the thread should terminate, schedule return false
// while ( schedule( ) == sal_True )
// {
if (! aSocket.bind( aSocketAddr ))
{
t_print("# can't bind.\n" );
}
if (! aSocket.listen( ))
{
t_print("# can't listen. \n" );
}
// blocking mode, if read/recv failed, block until success
aSocket.enableNonBlockingMode( sal_False);
::osl::StreamSocket ssStreamConnection;
oslSocketResult eResult = aSocket.acceptConnection( ssStreamConnection );
if (eResult != osl_Socket_Ok )
{
t_print("WriteSocketThread: acceptConnection failed! \n");
// break;
}
char const * pBuffer = "Test String\n";
sal_Int32 nBufferSize = strlen(pBuffer);
ssStreamConnection.write( pBuffer, nBufferSize );
// break;
// }
// ssStreamConnection.close();
aSocket.close();
}
// -----------------------------------------------------------------------------
void send_Connector(rtl::OString const& _sAddr, osl::Condition &/*_aCondition*/ )
{
::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(_sAddr.getStr()), IP_PORT_TEST );
if (! aSocketAddr.is())
{
t_print("is failed.\n");
return;
}
//if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
oslSocketResult aResult = aSocket.connect( aSocketAddr );
if ( aResult != osl_Socket_Ok )
{
t_print("# send_Connector: connect failed. \n" );
}
else
{
// blocking mode, if read/recv failed, block until success
// aSocket.enableNonBlockingMode( sal_False );
// _aCondition.set();
::osl::StreamSocket ssStreamConnection(aSocket);
char const * pBuffer = "GET / HTTP/1.0\015\012\015\012";
sal_Int32 nBufferSize = strlen(pBuffer);
ssStreamConnection.write( pBuffer, nBufferSize );
char *pBufferPeek = (char*) malloc(1024);
sal_Int32 nReadNumber = ssStreamConnection.recv( pBufferPeek, 1024, osl_Socket_MsgPeek);
free(pBufferPeek);
char *pBuffer2 = (char*) malloc(nReadNumber + 1);
sal_Int32 nReadNumberReal = ssStreamConnection.read( pBuffer2, nReadNumber );
pBuffer2[nReadNumberReal] = '\0';
t_print("received: %s\n", pBuffer2);
// char * pBuffer3 = "quit\n";
// nBufferSize = strlen(pBuffer3);
// nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize );
rtl::OUString suError = ssStreamConnection.getErrorAsString();
free(pBuffer2);
// ssStreamConnection.close();
// ssStreamConnection.close();
}
aSocket.shutdown(osl_Socket_DirReadWrite);
aSocket.close();
}
public:
// LLA: orig void send_recv()
// LLA: orig {
// LLA: orig if ( ifAvailable(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("margritte.germany"))) == sal_True )
// LLA: orig t_print("margritte is alive ! \n");
// LLA: orig if ( ifAvailable(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("10.16.66.252"))) == sal_False )
// LLA: orig {
// LLA: orig t_print("ip 10.16.66.252 is not alive! \n");
// LLA: orig return;
// LLA: orig }
// LLA: orig ReadSocket2Thread myReadThread;
// LLA: orig myReadThread.create();
// LLA: orig
// LLA: orig thread_sleep( 2 );
// LLA: orig // send_Acceptor();
// LLA: orig send_Connector();
// LLA: orig
// LLA: orig myReadThread.join();
// LLA: orig
// LLA: orig // statistics
// LLA: orig sal_uInt32 nLength = myReadThread.getCount();
// LLA: orig bool bIsOk = myReadThread.isOk(); // check if the values are right.
// LLA: orig
// LLA: orig t_print("Length:=%d\n", nLength);
// LLA: orig t_print(" bIsOk:=%d\n", bIsOk);
// LLA: orig }
// -----------------------------------------------------------------------------
// LLA: send_Connector_2_margritte works, it send strings to echo server on margritte
// but can not receive anything
void send_Connector_2_margritte(rtl::OString const& _sAddr)
{
::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(_sAddr.getStr()), IP_PORT_TEST );
//if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
oslSocketResult aResult = aSocket.connect( aSocketAddr );
if ( aResult != osl_Socket_Ok )
{
t_print("# connect failed. \n" );
}
else
{
// blocking mode, if read/recv failed, block until success
aSocket.enableNonBlockingMode( sal_False );
::osl::StreamSocket ssStreamConnection(aSocket);
char const * pBuffer = "Test String\n";
sal_Int32 nBufferSize = strlen(pBuffer);
sal_Int32 nWriteNumber = ssStreamConnection.write( pBuffer, nBufferSize );
// char * pBuffer2 = " ";
// sal_Int32 nReadNumber = ssStreamConnection.read( pBuffer2, strlen(pBuffer2) );
char const * pBuffer3 = "quit\n";
nBufferSize = strlen(pBuffer3);
nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize );
ssStreamConnection.close();
}
aSocket.close();
}
void send_recv_2_margritte()
{
rtl::OString sAddr;
sAddr = "margritte.germany.sun.com";
if ( ifAvailable(rtl::OUString::createFromAscii(sAddr.getStr())) == sal_True )
{
t_print("found %s!\n", sAddr.getStr());
}
send_Connector_2_margritte(sAddr);
}
// -----------------------------------------------------------------------------
void send_recv()
{
rtl::OString sAddr;
// if ( ifAvailable(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("margritte.germany"))) == sal_True )
// {
// t_print("margritte is alive ! \n");
// sAddr = "margritte.germany";
// }
sAddr = "margritte.germany.sun.com";
if ( ifAvailable(rtl::OUString::createFromAscii(sAddr.getStr())) == sal_True )
{
t_print("found %s!\n", sAddr.getStr());
}
// else
// {
// if ( ifAvailable(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("192.168.7.2"))) == sal_True )
// {
// sAddr = "192.168.7.2";
// t_print("moon found ! \n");
// }
// else
// {
// if ( ifAvailable(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("moon.linux.bogus"))) == sal_True )
// {
// sAddr = "moon.linux.bogus";
// t_print("moon found ! \n");
// }
// else
// {
// if ( ifAvailable(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("moon"))) == sal_True )
// {
// sAddr = "moon";
// t_print("moon found ! \n");
// }
// }
// }
// }
// if ( ifAvailable(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("10.16.64.196"))) == sal_False )
// {
// t_print("ip 10.16.64.196 is not alive! \n");
// return;
// }
osl::Condition aCondition;
ReadSocket2Thread myReadThread(aCondition);
myReadThread.setAddr(sAddr);
// myReadThread.create();
thread_sleep( 2 );
if (! myReadThread.getFailed())
{
// send_Acceptor(sAddr, aCondition);
send_Connector(sAddr, aCondition);
thread_sleep( 2 );
if (myReadThread.isRunning())
{
myReadThread.join();
}
// termAndJoinThread(&myReadThread);
// statistics
sal_uInt32 nLength = myReadThread.getCount();
bool bIsOk = myReadThread.isOk(); // check if the values are right.
t_print("Length:=%d\n", nLength);
t_print(" bIsOk:=%d\n", bIsOk);
}
else
{
t_print("ERROR: No echo Server on %s found.\n", sAddr.getStr());
}
}
void getPage(rtl::OString const& _sAddr);
void test_getPage()
{
// rtl::OString sPage("lla-1.germany.sun.com");
// getPage(sPage);
rtl::OString sPage("lla-1");
getPage(sPage);
}
CPPUNIT_TEST_SUITE( justtest );
CPPUNIT_TEST( send_recv );
CPPUNIT_TEST( test_getPage );
CPPUNIT_TEST_SUITE_END();
}; // class isExceptionPending
void justtest::getPage(rtl::OString const& _sAddr)
{
rtl::OUString suAddr = rtl::OUString::createFromAscii(_sAddr.getStr());
::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
::osl::SocketAddr aSocketAddr( suAddr, 80 );
{
// some checks
aSocketAddr.getPort();
oslSocketResult aResult;
rtl::ByteSequence aSeq = aSocketAddr.getAddr(&aResult);
if (aResult != osl_Socket_Ok)
{
t_print("problem with getAddr: ");
printSocketResult(aResult);
}
rtl::OUString sStr = aSocketAddr.getHostname(&aResult);
if (aResult != osl_Socket_Ok)
{
t_print("problem with hostname: ");
printSocketResult(aResult);
}
}
oslSocketResult aResult;
// SocketAddr::resolveHostname(suAddr, aSocketAddr);
// if (! aSocketAddr.is())
// {
// t_print("Can't resolve Hostname.\n");
// return;
// }
// rtl::OUString sStr = aSocketAddr.getHostname(&aResult);
// if (aResult != osl_Socket_Ok)
// {
// t_print("problem with hostname: ");
// printSocketResult(aResult);
//
// }
if (! aSocketAddr.is())
{
t_print("SocketAddr::is() failed.\n");
return;
}
//if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
aResult = aSocket.connect( aSocketAddr );
if ( aResult != osl_Socket_Ok )
{
t_print("# send_Connector: connect failed. \n" );
}
else
{
// blocking mode, if read/recv failed, block until success
// aSocket.enableNonBlockingMode( sal_False );
// _aCondition.set();
::osl::StreamSocket ssStreamConnection(aSocket);
char const * pBuffer = "GET / HTTP/1.0\015\012\015\012";
sal_Int32 nBufferSize = strlen(pBuffer);
ssStreamConnection.write( pBuffer, nBufferSize );
char *pBufferPeek = (char*) malloc(1024);
sal_Int32 nReadNumber = 1;
while ( nReadNumber != 0)
{
nReadNumber = ssStreamConnection.recv( pBufferPeek, 1024, osl_Socket_MsgPeek);
if (nReadNumber > 0)
{
char *pBuffer2 = (char*) malloc(nReadNumber + 1);
sal_Int32 nReadNumberReal = ssStreamConnection.read( pBuffer2, nReadNumber );
pBuffer2[nReadNumberReal] = '\0';
t_print("%s", pBuffer2);
free(pBuffer2);
}
}
free(pBufferPeek);
// char * pBuffer3 = "quit\n";
// nBufferSize = strlen(pBuffer3);
// nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize );
rtl::OUString suError = ssStreamConnection.getErrorAsString();
}
aSocket.shutdown(osl_Socket_DirReadWrite);
aSocket.close();
}
// -----------------------------------------------------------------------------
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::ctors, "osl_StreamSocket");
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::send_recv, "osl_StreamSocket");
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::shutdown, "osl_StreamSocket");
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::isExceptionPending, "osl_StreamSocket");
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::justtest, "osl_StreamSocket");
} // namespace osl_StreamSocket
// -----------------------------------------------------------------------------
// this macro creates an empty function, which will called by the RegisterAllFunctions()
// to let the user the possibility to also register some functions by hand.
NOADDITIONAL;
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|