665 cs130_ryutaro 1.1 TranslateMessage( &msg );
666 DispatchMessage( &msg );
667
668 }
669 }
670
671 /**************** Clients ***************/
672
673 /*
674 * simple_client: A very basic client doing synchronous IO.
675 */
676 static VOID WINAPI simple_client ( client_params *par )
677 {
678 test_params *gen = par->general;
679 client_memory *mem;
680 int n_sent, n_recvd, n_expected = gen->n_chunks * gen->chunk_size, id;
681 char *p;
682
683 id = GetCurrentThreadId();
684 trace ( "simple_client (%x): starting\n", id );
685 /* wait here because we want to call set_so_opentype before creating a socket */
686 cs130_ryutaro 1.1 WaitForSingleObject ( server_ready, INFINITE );
687 trace ( "simple_client (%x): server ready\n", id );
688
689 check_so_opentype ();
690 set_so_opentype ( FALSE ); /* non-overlapped */
691 client_start ( par );
692 mem = TlsGetValue ( tls );
693
694 /* Connect */
695 wsa_ok ( connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) ),
696 0 ==, "simple_client (%lx): connect error: %d\n" );
697 ok ( set_blocking ( mem->s, TRUE ) == 0,
698 "simple_client (%x): failed to set blocking mode\n", id );
699 trace ( "simple_client (%x) connected\n", id );
700
701 /* send data to server */
702 n_sent = do_synchronous_send ( mem->s, mem->send_buf, n_expected, par->buflen );
703 ok ( n_sent == n_expected,
704 "simple_client (%x): sent less data than expected: %d of %d\n", id, n_sent, n_expected );
705
706 /* shutdown send direction */
707 cs130_ryutaro 1.1 wsa_ok ( shutdown ( mem->s, SD_SEND ), 0 ==, "simple_client (%lx): shutdown failed: %d\n" );
708
709 /* Receive data echoed back & check it */
710 n_recvd = do_synchronous_recv ( mem->s, mem->recv_buf, n_expected, par->buflen );
711 ok ( n_recvd == n_expected,
712 "simple_client (%x): received less data than expected: %d of %d\n", id, n_recvd, n_expected );
713
714 /* check data */
715 p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
716 ok ( p == NULL, "simple_client (%x): test pattern error: %d\n", id, p - mem->recv_buf);
717
718 /* cleanup */
719 read_zero_bytes ( mem->s );
720 trace ( "simple_client (%x) exiting\n", id );
721 client_stop ();
722 }
723
724 /*
725 * event_client: An event-driven client
726 */
727 static void WINAPI event_client ( client_params *par )
728 cs130_ryutaro 1.1 {
729 test_params *gen = par->general;
730 client_memory *mem;
731 int id = GetCurrentThreadId(), n_expected = gen->n_chunks * gen->chunk_size,
732 tmp, err, n;
733 HANDLE event;
734 WSANETWORKEVENTS wsa_events;
735 char *send_last, *recv_last, *send_p, *recv_p;
736 long mask = FD_READ | FD_WRITE | FD_CLOSE;
737
738 trace ( "event_client (%x): starting\n", id );
739 client_start ( par );
740 trace ( "event_client (%x): server ready\n", id );
741
742 mem = TlsGetValue ( tls );
743
744 /* Prepare event notification for connect, makes socket nonblocking */
745 event = WSACreateEvent ();
746 WSAEventSelect ( mem->s, event, FD_CONNECT );
747 tmp = connect ( mem->s, (struct sockaddr*) &mem->addr, sizeof ( mem->addr ) );
748 if ( tmp != 0 && ( err = WSAGetLastError () ) != WSAEWOULDBLOCK )
749 cs130_ryutaro 1.1 ok ( 0, "event_client (%x): connect error: %d\n", id, err );
750
751 tmp = WaitForSingleObject ( event, INFINITE );
752 ok ( tmp == WAIT_OBJECT_0, "event_client (%x): wait for connect event failed: %d\n", id, tmp );
753 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
754 wsa_ok ( err, 0 ==, "event_client (%lx): WSAEnumNetworkEvents error: %d\n" );
755
756 err = wsa_events.iErrorCode[ FD_CONNECT_BIT ];
757 ok ( err == 0, "event_client (%x): connect error: %d\n", id, err );
758 if ( err ) goto out;
759
760 trace ( "event_client (%x) connected\n", id );
761
762 WSAEventSelect ( mem->s, event, mask );
763
764 recv_p = mem->recv_buf;
765 recv_last = mem->recv_buf + n_expected;
766 send_p = mem->send_buf;
767 send_last = mem->send_buf + n_expected;
768
769 while ( TRUE )
770 cs130_ryutaro 1.1 {
771 err = WaitForSingleObject ( event, INFINITE );
772 ok ( err == WAIT_OBJECT_0, "event_client (%x): wait failed\n", id );
773
774 err = WSAEnumNetworkEvents ( mem->s, event, &wsa_events );
775 wsa_ok ( err, 0 ==, "event_client (%lx): WSAEnumNetworkEvents error: %d\n" );
776
777 if ( wsa_events.lNetworkEvents & FD_WRITE )
778 {
779 err = wsa_events.iErrorCode[ FD_WRITE_BIT ];
780 ok ( err == 0, "event_client (%x): FD_WRITE error code: %d\n", id, err );
781
782 if ( err== 0 )
783 do
784 {
785 n = send ( mem->s, send_p, min ( send_last - send_p, par->buflen ), 0 );
786 if ( n < 0 )
787 {
788 err = WSAGetLastError ();
789 ok ( err == WSAEWOULDBLOCK, "event_client (%x): send error: %d\n", id, err );
790 }
791 cs130_ryutaro 1.1 else
792 send_p += n;
793 }
794 while ( n >= 0 && send_p < send_last );
795
796 if ( send_p == send_last )
797 {
798 trace ( "event_client (%x): all data sent - shutdown\n", id );
799 shutdown ( mem->s, SD_SEND );
800 mask &= ~FD_WRITE;
801 WSAEventSelect ( mem->s, event, mask );
802 }
803 }
804 if ( wsa_events.lNetworkEvents & FD_READ )
805 {
806 err = wsa_events.iErrorCode[ FD_READ_BIT ];
807 ok ( err == 0, "event_client (%x): FD_READ error code: %d\n", id, err );
808 if ( err != 0 ) break;
809
810 /* First read must succeed */
811 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
812 cs130_ryutaro 1.1 wsa_ok ( n, 0 <=, "event_client (%lx): recv error: %d\n" );
813
814 while ( n >= 0 ) {
815 recv_p += n;
816 if ( recv_p == recv_last )
817 {
818 mask &= ~FD_READ;
819 trace ( "event_client (%x): all data received\n", id );
820 WSAEventSelect ( mem->s, event, mask );
821 break;
822 }
823 n = recv ( mem->s, recv_p, min ( recv_last - recv_p, par->buflen ), 0 );
824 if ( n < 0 && ( err = WSAGetLastError()) != WSAEWOULDBLOCK )
825 ok ( 0, "event_client (%x): read error: %d\n", id, err );
826
827 }
828 }
829 if ( wsa_events.lNetworkEvents & FD_CLOSE )
830 {
831 trace ( "event_client (%x): close event\n", id );
832 err = wsa_events.iErrorCode[ FD_CLOSE_BIT ];
833 cs130_ryutaro 1.1 ok ( err == 0, "event_client (%x): FD_CLOSE error code: %d\n", id, err );
834 break;
835 }
836 }
837
838 ok ( send_p == send_last,
839 "simple_client (%x): sent less data than expected: %d of %d\n",
840 id, send_p - mem->send_buf, n_expected );
841 ok ( recv_p == recv_last,
842 "simple_client (%x): received less data than expected: %d of %d\n",
843 id, recv_p - mem->recv_buf, n_expected );
844 recv_p = test_buffer ( mem->recv_buf, gen->chunk_size, gen->n_chunks );
845 ok ( recv_p == NULL, "event_client (%x): test pattern error: %d\n", id, recv_p - mem->recv_buf);
846
847 out:
848 WSACloseEvent ( event );
849 trace ( "event_client (%x) exiting\n", id );
850 client_stop ();
851 }
852
853 /**************** Main program utility functions ***************/
854 cs130_ryutaro 1.1
855 static void Init (void)
856 {
857 WORD ver = MAKEWORD (2, 2);
858 WSADATA data;
859
860 ok ( WSAStartup ( ver, &data ) == 0, "WSAStartup failed\n" );
861 tls = TlsAlloc();
862 }
863
864 static void Exit (void)
865 {
866 TlsFree ( tls );
867 ok ( WSACleanup() == 0, "WSACleanup failed\n" );
868 }
869
870 static void StartServer (LPTHREAD_START_ROUTINE routine,
871 test_params *general, server_params *par)
872 {
873 par->general = general;
874 thread[0] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[0] );
875 cs130_ryutaro 1.1 ok ( thread[0] != NULL, "Failed to create server thread\n" );
876 }
877
878 static void StartClients (LPTHREAD_START_ROUTINE routine,
879 test_params *general, client_params *par)
880 {
881 int i;
882 par->general = general;
883 for ( i = 1; i <= min ( general->n_clients, MAX_CLIENTS ); i++ )
884 {
885 client_id = i - 1;
886 thread[i] = CreateThread ( NULL, 0, routine, par, 0, &thread_id[i] );
887 ok ( thread[i] != NULL, "Failed to create client thread\n" );
888 /* Make sure the client is up and running */
889 WaitForSingleObject ( client_ready[client_id], INFINITE );
890 };
891 }
892
893 static void do_test( test_setup *test )
894 {
895 DWORD i, n = min (test->general.n_clients, MAX_CLIENTS);
896 cs130_ryutaro 1.1 DWORD wait;
897
898 server_ready = CreateEventW ( NULL, TRUE, FALSE, NULL );
899 for (i = 0; i <= n; i++)
900 client_ready[i] = CreateEventW ( NULL, TRUE, FALSE, NULL );
901
902 StartServer ( test->srv, &test->general, &test->srv_params );
903 StartClients ( test->clt, &test->general, &test->clt_params );
904 WaitForSingleObject ( server_ready, INFINITE );
905
906 wait = WaitForMultipleObjects ( 1 + n, thread, TRUE, 1000 * TEST_TIMEOUT );
907 ok ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n ,
908 "some threads have not completed: %lx\n", wait );
909
910 if ( ! ( wait >= WAIT_OBJECT_0 && wait <= WAIT_OBJECT_0 + n ) )
911 {
912 for (i = 0; i <= n; i++)
913 {
914 trace ("terminating thread %08lx\n", thread_id[i]);
915 if ( WaitForSingleObject ( thread[i], 0 ) != WAIT_OBJECT_0 )
916 TerminateThread ( thread [i], 0 );
917 cs130_ryutaro 1.1 }
918 }
919 CloseHandle ( server_ready );
920 for (i = 0; i <= n; i++)
921 CloseHandle ( client_ready[i] );
922 }
923
924 /********* some tests for getsockopt(setsockopt(X)) == X ***********/
925 /* optname = SO_LINGER */
926 LINGER linger_testvals[] = {
927 {0,0},
928 {0,73},
929 {1,0},
930 {5,189}
931 };
932
933 /* optname = SO_RCVTIMEO, SOSNDTIMEO */
934 #define SOCKTIMEOUT1 63000 /* 63 seconds. Do not test fractional part because of a
935 bug in the linux kernel (fixed in 2.6.8) */
936 #define SOCKTIMEOUT2 997000 /* 997 seconds */
937
938 cs130_ryutaro 1.1 static void test_set_getsockopt()
939 {
940 SOCKET s;
941 int i, err;
942 int timeout;
943 LINGER lingval;
944 int size;
945
946 s = socket(AF_INET, SOCK_STREAM, 0);
947 ok(s!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
948 if( s == INVALID_SOCKET) return;
949 /* SO_RCVTIMEO */
950 timeout = SOCKTIMEOUT1;
951 size = sizeof(timeout);
952 err = setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, size);
953 if( !err)
954 err = getsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout, &size);
955 ok( !err, "get/setsockopt(SO_RCVTIMEO) failed error: %d\n", WSAGetLastError());
956 ok( timeout == SOCKTIMEOUT1, "getsockopt(SO_RCVTIMEO) returned wrong value %d\n", timeout);
957 /* SO_SNDTIMEO */
958 timeout = SOCKTIMEOUT2; /* 54 seconds. See remark above */
959 cs130_ryutaro 1.1 size = sizeof(timeout);
960 err = setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, size);
961 if( !err)
962 err = getsockopt(s, SOL_SOCKET, SO_SNDTIMEO, (char *) &timeout, &size);
963 ok( !err, "get/setsockopt(SO_SNDTIMEO) failed error: %d\n", WSAGetLastError());
964 ok( timeout == SOCKTIMEOUT2, "getsockopt(SO_SNDTIMEO) returned wrong value %d\n", timeout);
965 /* SO_LINGER */
966 for( i = 0; i < sizeof(linger_testvals)/sizeof(LINGER);i++) {
967 size = sizeof(lingval);
968 lingval = linger_testvals[i];
969 err = setsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, size);
970 if( !err)
971 err = getsockopt(s, SOL_SOCKET, SO_LINGER, (char *) &lingval, &size);
972 ok( !err, "get/setsockopt(SO_LINGER) failed error: %d\n", WSAGetLastError());
973 ok( !lingval.l_onoff == !linger_testvals[i].l_onoff &&
974 (lingval.l_linger == linger_testvals[i].l_linger ||
975 (!lingval.l_linger && !linger_testvals[i].l_onoff))
976 , "getsockopt(SO_LINGER #%d) returned wrong value %d,%d not %d,%d \n", i,
977 lingval.l_onoff, lingval.l_linger,
978 linger_testvals[i].l_onoff, linger_testvals[i].l_linger);
979 }
980 cs130_ryutaro 1.1 closesocket(s);
981 }
982
983 static void test_so_reuseaddr()
984 {
985 struct sockaddr_in saddr;
986 SOCKET s1,s2;
987 unsigned int rc,reuse,size;
988
989 saddr.sin_family = AF_INET;
990 saddr.sin_port = htons(9375);
991 saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
992
993 s1=socket(AF_INET, SOCK_STREAM, 0);
994 ok(s1!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
995 rc = bind(s1, (struct sockaddr*)&saddr, sizeof(saddr));
996 ok(rc!=SOCKET_ERROR, "bind(s1) failed error: %d\n", WSAGetLastError());
997
998 s2=socket(AF_INET, SOCK_STREAM, 0);
999 ok(s2!=INVALID_SOCKET, "socket() failed error: %d\n", WSAGetLastError());
1000
1001 cs130_ryutaro 1.1 reuse=0x1234;
1002 size=sizeof(reuse);
1003 rc=getsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, &size );
1004 ok(rc==0 && reuse==0,"wrong result in getsockopt(SO_REUSEADDR): rc=%d reuse=%d\n",rc,reuse);
1005
1006 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1007 ok(rc==SOCKET_ERROR, "bind() succeeded\n");
1008
1009 reuse = 1;
1010 rc = setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse));
1011 ok(rc==0, "setsockopt() failed error: %d\n", WSAGetLastError());
1012
1013 todo_wine {
1014 rc = bind(s2, (struct sockaddr*)&saddr, sizeof(saddr));
1015 ok(rc==0, "bind() failed error: %d\n", WSAGetLastError());
1016 }
1017
1018 closesocket(s2);
1019 closesocket(s1);
1020 }
1021
1022 cs130_ryutaro 1.1 /************* Array containing the tests to run **********/
1023
1024 #define STD_STREAM_SOCKET \
1025 SOCK_STREAM, \
1026 0, \
1027 "127.0.0.1", \
1028 9374
1029
1030 static test_setup tests [NUM_TESTS] =
1031 {
1032 /* Test 0: synchronous client and server */
1033 {
1034 {
1035 STD_STREAM_SOCKET,
1036 2048,
1037 16,
1038 2
1039 },
1040 simple_server,
1041 {
1042 NULL,
1043 cs130_ryutaro 1.1 0,
1044 64
1045 },
1046 simple_client,
1047 {
1048 NULL,
1049 0,
1050 128
1051 }
1052 },
1053 /* Test 1: event-driven client, synchronous server */
1054 {
1055 {
1056 STD_STREAM_SOCKET,
1057 2048,
1058 16,
1059 2
1060 },
1061 simple_server,
1062 {
1063 NULL,
1064 cs130_ryutaro 1.1 0,
1065 64
1066 },
1067 event_client,
1068 {
1069 NULL,
1070 WSA_FLAG_OVERLAPPED,
1071 128
1072 }
1073 },
1074
1075 /* Test 2: synchronous client and asynchronous server */
1076 {
1077 {
1078 STD_STREAM_SOCKET,
1079 2048,
1080 16,
1081 2
1082 },
1083 async_server,
1084 {
1085 cs130_ryutaro 1.1 NULL,
1086 0,
1087 64
1088 },
1089 simple_client,
1090 {
1091 NULL,
1092 0,
1093 128
1094 }
1095 }
1096 };
1097
1098 static void test_WSAAddressToStringA()
1099 {
1100 INT ret;
1101 DWORD len;
1102 SOCKADDR_IN sockaddr;
1103 CHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1104
1105 CHAR expect1[] = "0.0.0.0";
1106 cs130_ryutaro 1.1 CHAR expect2[] = "255.255.255.255";
1107 CHAR expect3[] = "0.0.0.0:65535";
1108 CHAR expect4[] = "255.255.255.255:65535";
1109
1110 len = 0;
1111
1112 sockaddr.sin_family = AF_INET;
1113 sockaddr.sin_port = 0;
1114 sockaddr.sin_addr.s_addr = 0;
1115
1116 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1117 ok( ret == SOCKET_ERROR, "WSAAddressToStringA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1118
1119 len = sizeof(address);
1120
1121 sockaddr.sin_family = AF_INET;
1122 sockaddr.sin_port = 0;
1123 sockaddr.sin_addr.s_addr = 0;
1124
1125 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1126 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1127 cs130_ryutaro 1.1
1128 ok( !strcmp( address, expect1 ), "Expected: %s, got: %s\n", expect1, address );
1129
1130 len = sizeof(address);
1131
1132 sockaddr.sin_family = AF_INET;
1133 sockaddr.sin_port = 0;
1134 sockaddr.sin_addr.s_addr = 0xffffffff;
1135
1136 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1137 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1138
1139 ok( !strcmp( address, expect2 ), "Expected: %s, got: %s\n", expect2, address );
1140
1141 len = sizeof(address);
1142
1143 sockaddr.sin_family = AF_INET;
1144 sockaddr.sin_port = 0xffff;
1145 sockaddr.sin_addr.s_addr = 0;
1146
1147 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1148 cs130_ryutaro 1.1 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1149
1150 ok( !strcmp( address, expect3 ), "Expected: %s, got: %s\n", expect3, address );
1151
1152 len = sizeof(address);
1153
1154 sockaddr.sin_family = AF_INET;
1155 sockaddr.sin_port = 0xffff;
1156 sockaddr.sin_addr.s_addr = 0xffffffff;
1157
1158 ret = WSAAddressToStringA( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1159 ok( !ret, "WSAAddressToStringA() failed unexpectedly: %d\n", WSAGetLastError() );
1160
1161 ok( !strcmp( address, expect4 ), "Expected: %s, got: %s\n", expect4, address );
1162 }
1163
1164 static void test_WSAAddressToStringW()
1165 {
1166 INT ret;
1167 DWORD len;
1168 SOCKADDR_IN sockaddr;
1169 cs130_ryutaro 1.1 WCHAR address[22]; /* 12 digits + 3 dots + ':' + 5 digits + '\0' */
1170
1171 WCHAR expect1[] = { '0','.','0','.','0','.','0', 0 };
1172 WCHAR expect2[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1173 WCHAR expect3[] = { '0','.','0','.','0','.','0', ':', '6', '5', '5', '3', '5', 0 };
1174 WCHAR expect4[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1175 '6', '5', '5', '3', '5', 0 };
1176
1177 len = 0;
1178
1179 sockaddr.sin_family = AF_INET;
1180 sockaddr.sin_port = 0;
1181 sockaddr.sin_addr.s_addr = 0;
1182
1183 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1184 ok( ret == SOCKET_ERROR, "WSAAddressToStringW() succeeded unexpectedly: %x\n", WSAGetLastError() );
1185
1186 len = sizeof(address);
1187
1188 sockaddr.sin_family = AF_INET;
1189 sockaddr.sin_port = 0;
1190 cs130_ryutaro 1.1 sockaddr.sin_addr.s_addr = 0;
1191
1192 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1193 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1194
1195 ok( !lstrcmpW( address, expect1 ), "Expected different address string\n" );
1196
1197 len = sizeof(address);
1198
1199 sockaddr.sin_family = AF_INET;
1200 sockaddr.sin_port = 0;
1201 sockaddr.sin_addr.s_addr = 0xffffffff;
1202
1203 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1204 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1205
1206 ok( !lstrcmpW( address, expect2 ), "Expected different address string\n" );
1207
1208 len = sizeof(address);
1209
1210 sockaddr.sin_family = AF_INET;
1211 cs130_ryutaro 1.1 sockaddr.sin_port = 0xffff;
1212 sockaddr.sin_addr.s_addr = 0;
1213
1214 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1215 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1216
1217 ok( !lstrcmpW( address, expect3 ), "Expected different address string\n" );
1218
1219 len = sizeof(address);
1220
1221 sockaddr.sin_family = AF_INET;
1222 sockaddr.sin_port = 0xffff;
1223 sockaddr.sin_addr.s_addr = 0xffffffff;
1224
1225 ret = WSAAddressToStringW( (SOCKADDR*)&sockaddr, sizeof(sockaddr), NULL, address, &len );
1226 ok( !ret, "WSAAddressToStringW() failed unexpectedly: %x\n", WSAGetLastError() );
1227
1228 ok( !lstrcmpW( address, expect4 ), "Expected different address string\n" );
1229 }
1230
1231 static void test_WSAStringToAddressA()
1232 cs130_ryutaro 1.1 {
1233 INT ret, len;
1234 SOCKADDR_IN sockaddr;
1235
1236 CHAR address1[] = "0.0.0.0";
1237 CHAR address2[] = "127.127.127.127";
1238 CHAR address3[] = "255.255.255.255";
1239 CHAR address4[] = "127.127.127.127:65535";
1240 CHAR address5[] = "255.255.255.255:65535";
1241
1242 len = 0;
1243 sockaddr.sin_family = AF_INET;
1244
1245 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1246 ok( ret == SOCKET_ERROR, "WSAStringToAddressA() succeeded unexpectedly: %x\n",
1247 WSAGetLastError() );
1248
1249 len = sizeof(sockaddr);
1250 sockaddr.sin_port = 0;
1251 sockaddr.sin_addr.s_addr = 0;
1252
1253 cs130_ryutaro 1.1 ret = WSAStringToAddressA( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1254 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1255 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1256
1257 len = sizeof(sockaddr);
1258 sockaddr.sin_port = 0;
1259 sockaddr.sin_addr.s_addr = 0;
1260
1261 ret = WSAStringToAddressA( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1262 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1263 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1264
1265 len = sizeof(sockaddr);
1266
1267 ret = WSAStringToAddressA( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1268 ok( ret, "WSAStringToAddressA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1269
1270 len = sizeof(sockaddr);
1271 sockaddr.sin_port = 0;
1272 sockaddr.sin_addr.s_addr = 0;
1273
1274 cs130_ryutaro 1.1 ret = WSAStringToAddressA( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1275 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1276 "WSAStringToAddressA() failed unexpectedly: %d\n", WSAGetLastError() );
1277
1278 len = sizeof(sockaddr);
1279
1280 ret = WSAStringToAddressA( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1281 ok( ret, "WSAStringToAddressA() succeeded unexpectedly: %d\n", WSAGetLastError() );
1282 }
1283
1284 static void test_WSAStringToAddressW()
1285 {
1286 INT ret, len;
1287 SOCKADDR_IN sockaddr;
1288
1289 WCHAR address1[] = { '0','.','0','.','0','.','0', 0 };
1290 WCHAR address2[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7', 0 };
1291 WCHAR address3[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', 0 };
1292 WCHAR address4[] = { '1','2','7','.','1','2','7','.','1','2','7','.','1','2','7',
1293 ':', '6', '5', '5', '3', '5', 0 };
1294 WCHAR address5[] = { '2','5','5','.','2','5','5','.','2','5','5','.','2','5','5', ':',
1295 cs130_ryutaro 1.1 '6', '5', '5', '3', '5', 0 };
1296
1297 len = 0;
1298 sockaddr.sin_family = AF_INET;
1299
1300 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1301 ok( ret == SOCKET_ERROR, "WSAStringToAddressW() failed unexpectedly: %d\n",
1302 WSAGetLastError() );
1303
1304 len = sizeof(sockaddr);
1305 sockaddr.sin_port = 0;
1306 sockaddr.sin_addr.s_addr = 0;
1307
1308 ret = WSAStringToAddressW( address1, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1309 ok( !ret && sockaddr.sin_addr.s_addr == 0,
1310 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1311
1312 len = sizeof(sockaddr);
1313
1314 sockaddr.sin_port = 0;
1315 sockaddr.sin_addr.s_addr = 0;
1316 cs130_ryutaro 1.1
1317 ret = WSAStringToAddressW( address2, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1318 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f,
1319 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1320
1321 len = sizeof(sockaddr);
1322
1323 ret = WSAStringToAddressW( address3, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1324 ok( ret, "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1325
1326 len = sizeof(sockaddr);
1327 sockaddr.sin_port = 0;
1328 sockaddr.sin_addr.s_addr = 0;
1329
1330 ret = WSAStringToAddressW( address4, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1331 ok( !ret && sockaddr.sin_addr.s_addr == 0x7f7f7f7f && sockaddr.sin_port == 0xffff,
1332 "WSAStringToAddressW() failed unexpectedly: %d\n", WSAGetLastError() );
1333
1334 len = sizeof(sockaddr);
1335
1336 ret = WSAStringToAddressW( address5, AF_INET, NULL, (SOCKADDR*)&sockaddr, &len );
1337 cs130_ryutaro 1.1 ok( ret, "WSAStringToAddressW() succeeded unexpectedly: %d\n", WSAGetLastError() );
1338 }
1339
1340 /**************** Main program ***************/
1341
1342 START_TEST( sock )
1343 {
1344 int i;
1345 Init();
1346
1347 test_set_getsockopt();
1348 test_so_reuseaddr();
1349
1350 for (i = 0; i < NUM_TESTS; i++)
1351 {
1352 trace ( " **** STARTING TEST %d **** \n", i );
1353 do_test ( &tests[i] );
1354 trace ( " **** TEST %d COMPLETE **** \n", i );
1355 }
1356
1357 test_WSAAddressToStringA();
1358 cs130_ryutaro 1.1 test_WSAAddressToStringW();
1359
1360 test_WSAStringToAddressA();
1361 test_WSAStringToAddressW();
1362
1363 Exit();
1364 }
|