@@ -781,6 +781,9 @@ def testNULLcert(self):
781781 def testMalformedCert (self ):
782782 badCertTest (os .path .join (os .path .dirname (__file__ ) or os .curdir ,
783783 "badcert.pem" ))
784+ def testWrongCert (self ):
785+ badCertTest (os .path .join (os .path .dirname (__file__ ) or os .curdir ,
786+ "wrongcert.pem" ))
784787 def testMalformedKey (self ):
785788 badCertTest (os .path .join (os .path .dirname (__file__ ) or os .curdir ,
786789 "badkey.pem" ))
@@ -1033,6 +1036,129 @@ def testAsyncoreServer(self):
10331036 server .stop ()
10341037 server .join ()
10351038
1039+ def testAllRecvAndSendMethods (self ):
1040+
1041+ if support .verbose :
1042+ sys .stdout .write ("\n " )
1043+
1044+ server = ThreadedEchoServer (CERTFILE ,
1045+ certreqs = ssl .CERT_NONE ,
1046+ ssl_version = ssl .PROTOCOL_TLSv1 ,
1047+ cacerts = CERTFILE ,
1048+ chatty = True ,
1049+ connectionchatty = False )
1050+ flag = threading .Event ()
1051+ server .start (flag )
1052+ # wait for it to start
1053+ flag .wait ()
1054+ # try to connect
1055+ try :
1056+ s = ssl .wrap_socket (socket .socket (),
1057+ server_side = False ,
1058+ certfile = CERTFILE ,
1059+ ca_certs = CERTFILE ,
1060+ cert_reqs = ssl .CERT_NONE ,
1061+ ssl_version = ssl .PROTOCOL_TLSv1 )
1062+ s .connect ((HOST , server .port ))
1063+ except ssl .SSLError as x :
1064+ raise support .TestFailed ("Unexpected SSL error: " + str (x ))
1065+ except Exception as x :
1066+ raise support .TestFailed ("Unexpected exception: " + str (x ))
1067+ else :
1068+ # helper methods for standardising recv* method signatures
1069+ def _recv_into ():
1070+ b = bytearray (b"\0 " * 100 )
1071+ count = s .recv_into (b )
1072+ return b [:count ]
1073+
1074+ def _recvfrom_into ():
1075+ b = bytearray (b"\0 " * 100 )
1076+ count , addr = s .recvfrom_into (b )
1077+ return b [:count ]
1078+
1079+ # (name, method, whether to expect success, *args)
1080+ send_methods = [
1081+ ('send' , s .send , True , []),
1082+ ('sendto' , s .sendto , False , ["some.address" ]),
1083+ ('sendall' , s .sendall , True , []),
1084+ ]
1085+ recv_methods = [
1086+ ('recv' , s .recv , True , []),
1087+ ('recvfrom' , s .recvfrom , False , ["some.address" ]),
1088+ ('recv_into' , _recv_into , True , []),
1089+ ('recvfrom_into' , _recvfrom_into , False , []),
1090+ ]
1091+ data_prefix = "PREFIX_"
1092+
1093+ for meth_name , send_meth , expect_success , args in send_methods :
1094+ indata = data_prefix + meth_name
1095+ try :
1096+ send_meth (indata .encode ('ASCII' , 'strict' ), * args )
1097+ outdata = s .read ()
1098+ outdata = str (outdata , 'ASCII' , 'strict' )
1099+ if outdata != indata .lower ():
1100+ raise support .TestFailed (
1101+ "While sending with <<{name:s}>> bad data "
1102+ "<<{outdata:s}>> ({nout:d}) received; "
1103+ "expected <<{indata:s}>> ({nin:d})\n " .format (
1104+ name = meth_name , outdata = repr (outdata [:20 ]),
1105+ nout = len (outdata ),
1106+ indata = repr (indata [:20 ]), nin = len (indata )
1107+ )
1108+ )
1109+ except ValueError as e :
1110+ if expect_success :
1111+ raise support .TestFailed (
1112+ "Failed to send with method <<{name:s}>>; "
1113+ "expected to succeed.\n " .format (name = meth_name )
1114+ )
1115+ if not str (e ).startswith (meth_name ):
1116+ raise support .TestFailed (
1117+ "Method <<{name:s}>> failed with unexpected "
1118+ "exception message: {exp:s}\n " .format (
1119+ name = meth_name , exp = e
1120+ )
1121+ )
1122+
1123+ for meth_name , recv_meth , expect_success , args in recv_methods :
1124+ indata = data_prefix + meth_name
1125+ try :
1126+ s .send (indata .encode ('ASCII' , 'strict' ))
1127+ outdata = recv_meth (* args )
1128+ outdata = str (outdata , 'ASCII' , 'strict' )
1129+ if outdata != indata .lower ():
1130+ raise support .TestFailed (
1131+ "While receiving with <<{name:s}>> bad data "
1132+ "<<{outdata:s}>> ({nout:d}) received; "
1133+ "expected <<{indata:s}>> ({nin:d})\n " .format (
1134+ name = meth_name , outdata = repr (outdata [:20 ]),
1135+ nout = len (outdata ),
1136+ indata = repr (indata [:20 ]), nin = len (indata )
1137+ )
1138+ )
1139+ except ValueError as e :
1140+ if expect_success :
1141+ raise support .TestFailed (
1142+ "Failed to receive with method <<{name:s}>>; "
1143+ "expected to succeed.\n " .format (name = meth_name )
1144+ )
1145+ if not str (e ).startswith (meth_name ):
1146+ raise support .TestFailed (
1147+ "Method <<{name:s}>> failed with unexpected "
1148+ "exception message: {exp:s}\n " .format (
1149+ name = meth_name , exp = e
1150+ )
1151+ )
1152+ # consume data
1153+ s .read ()
1154+
1155+ s .write ("over\n " .encode ("ASCII" , "strict" ))
1156+ s .close ()
1157+ finally :
1158+ server .stop ()
1159+ server .join ()
1160+
1161+
10361162def test_main (verbose = False ):
10371163 if skip_expected :
10381164 raise support .TestSkipped ("No SSL support" )
0 commit comments