PageRenderTime 327ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/Languages/IronPython/Tests/modules/network_related/_ssl_test.py

http://github.com/IronLanguages/main
Python | 385 lines | 340 code | 22 blank | 23 comment | 1 complexity | bbb7f37e0029e8305058e95cafe7bb48 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Apache License, Version 2.0, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Apache License, Version 2.0.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. '''
  16. Tests for the _ssl module. See http://docs.python.org/library/ssl.html
  17. '''
  18. #--IMPORTS---------------------------------------------------------------------
  19. from iptest.assert_util import *
  20. skiptest("silverlight")
  21. #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=24266
  22. import _ssl as real_ssl
  23. import socket
  24. #--GLOBALS---------------------------------------------------------------------
  25. SSL_URL = "www.microsoft.com"
  26. SSL_ISSUER = "CN=Symantec Class 3 Secure Server CA - G4, OU=Symantec Trust Network, O=Symantec Corporation, C=US"
  27. SSL_SERVER = "www.microsoft.com"
  28. SSL_PORT = 443
  29. SSL_REQUEST = "GET / HTTP/1.0\r\nHost: www.microsoft.com\r\n\r\n"
  30. SSL_RESPONSE = "Microsoft Corporation"
  31. #--HELPERS---------------------------------------------------------------------
  32. #--TEST CASES------------------------------------------------------------------
  33. @skip("silverlight") #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21411
  34. def test_CERT_NONE():
  35. AreEqual(real_ssl.CERT_NONE,
  36. 0)
  37. @skip("silverlight") #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21411
  38. def test_CERT_OPTIONAL():
  39. AreEqual(real_ssl.CERT_OPTIONAL,
  40. 1)
  41. @skip("silverlight") #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21411
  42. def test_CERT_REQUIRED():
  43. AreEqual(real_ssl.CERT_REQUIRED,
  44. 2)
  45. @skip("silverlight") #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21411
  46. def test_PROTOCOL_SSLv2():
  47. AreEqual(real_ssl.PROTOCOL_SSLv2,
  48. 0)
  49. @skip("silverlight") #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21411
  50. def test_PROTOCOL_SSLv23():
  51. AreEqual(real_ssl.PROTOCOL_SSLv23,
  52. 2)
  53. @skip("silverlight") #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21411
  54. def test_PROTOCOL_SSLv3():
  55. AreEqual(real_ssl.PROTOCOL_SSLv3,
  56. 1)
  57. @skip("silverlight") #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21411
  58. def test_PROTOCOL_TLSv1():
  59. AreEqual(real_ssl.PROTOCOL_TLSv1,
  60. 3)
  61. @skip("silverlight")
  62. def test_PROTOCOL_TLSv1_1():
  63. AreEqual(real_ssl.PROTOCOL_TLSv1_1,
  64. 4)
  65. @skip("silverlight")
  66. def test_PROTOCOL_TLSv1_2():
  67. AreEqual(real_ssl.PROTOCOL_TLSv1_2,
  68. 5)
  69. @skip("silverlight")
  70. def test_OP_NO_SSLv2():
  71. AreEqual(real_ssl.OP_NO_SSLv2,
  72. 0x0000000)
  73. @skip("silverlight")
  74. def test_OP_NO_SSLv3():
  75. AreEqual(real_ssl.OP_NO_SSLv3,
  76. 0x2000000)
  77. @skip("silverlight")
  78. def test_OP_NO_TLSv1():
  79. AreEqual(real_ssl.OP_NO_TLSv1,
  80. 0x4000000)
  81. @skip("silverlight")
  82. def test_OP_NO_TLSv1_1():
  83. AreEqual(real_ssl.OP_NO_TLSv1_1,
  84. 0x10000000)
  85. @skip("silverlight")
  86. def test_OP_NO_TLSv1_2():
  87. AreEqual(real_ssl.OP_NO_TLSv1_2,
  88. 0x8000000)
  89. def test_RAND_add():
  90. #--Positive
  91. AreEqual(real_ssl.RAND_add("", 3.14),
  92. None)
  93. AreEqual(real_ssl.RAND_add(u"", 3.14),
  94. None)
  95. AreEqual(real_ssl.RAND_add("", 3),
  96. None)
  97. #--Negative
  98. for g1, g2 in [ (None, None),
  99. ("", None),
  100. (None, 3.14), #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=24276
  101. ]:
  102. AssertError(TypeError, real_ssl.RAND_add, g1, g2)
  103. AssertError(TypeError, real_ssl.RAND_add)
  104. AssertError(TypeError, real_ssl.RAND_add, "")
  105. AssertError(TypeError, real_ssl.RAND_add, 3.14)
  106. AssertError(TypeError, real_ssl.RAND_add, "", 3.14, "")
  107. def test_RAND_status():
  108. #--Positive
  109. AreEqual(real_ssl.RAND_status(),
  110. 1)
  111. #--Negative
  112. AssertError(TypeError, real_ssl.RAND_status, None)
  113. AssertError(TypeError, real_ssl.RAND_status, "")
  114. AssertError(TypeError, real_ssl.RAND_status, 1)
  115. AssertError(TypeError, real_ssl.RAND_status, None, None)
  116. def test_SSLError():
  117. AreEqual(real_ssl.SSLError.__bases__, (socket.error, ))
  118. def test_SSL_ERROR_EOF():
  119. AreEqual(real_ssl.SSL_ERROR_EOF, 8)
  120. def test_SSL_ERROR_INVALID_ERROR_CODE():
  121. AreEqual(real_ssl.SSL_ERROR_INVALID_ERROR_CODE, 9)
  122. def test_SSL_ERROR_SSL():
  123. AreEqual(real_ssl.SSL_ERROR_SSL, 1)
  124. def test_SSL_ERROR_SYSCALL():
  125. AreEqual(real_ssl.SSL_ERROR_SYSCALL, 5)
  126. def test_SSL_ERROR_WANT_CONNECT():
  127. AreEqual(real_ssl.SSL_ERROR_WANT_CONNECT, 7)
  128. def test_SSL_ERROR_WANT_READ():
  129. AreEqual(real_ssl.SSL_ERROR_WANT_READ, 2)
  130. def test_SSL_ERROR_WANT_WRITE():
  131. AreEqual(real_ssl.SSL_ERROR_WANT_WRITE, 3)
  132. def test_SSL_ERROR_WANT_X509_LOOKUP():
  133. AreEqual(real_ssl.SSL_ERROR_WANT_X509_LOOKUP, 4)
  134. def test_SSL_ERROR_ZERO_RETURN():
  135. AreEqual(real_ssl.SSL_ERROR_ZERO_RETURN, 6)
  136. @skip("silverlight") #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21411
  137. def test___doc__():
  138. expected_doc = """Implementation module for SSL socket operations. See the socket module
  139. for documentation."""
  140. AreEqual(real_ssl.__doc__, expected_doc)
  141. def test__test_decode_cert():
  142. if not is_cpython and hasattr(real_ssl, "decode_cert"):
  143. raise Exception("Please add a test for _ssl.decode_cert")
  144. print 'TODO: no implementation to test yet.'
  145. def test_sslwrap():
  146. print 'TODO: no implementation to test yet.'
  147. def test_SSLType():
  148. #--Positive
  149. if is_cpython:
  150. AreEqual(str(real_ssl.SSLType),
  151. "<type 'ssl.SSLContext'>")
  152. else:
  153. #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=24266
  154. AreEqual(str(real_ssl.SSLType),
  155. "<type 'socket.ssl'>")
  156. #--Negative
  157. if is_cpython:
  158. AssertErrorWithMessage(TypeError, "cannot create 'ssl.SSLContext' instances",
  159. real_ssl.SSLType)
  160. else:
  161. #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=24277
  162. AssertErrorWithMessage(TypeError, "ssl() takes at least 1 argument (0 given)",
  163. real_ssl.SSLType)
  164. #--TEST CASES for _ssl.sll-----------------------------------------------------
  165. '''
  166. TODO: once we have a proper implementation of _ssl.sslwrap the tests below need
  167. to be rewritten.
  168. '''
  169. @retry_on_failure
  170. def test_SSLType_ssl():
  171. '''
  172. Should be essentially the same as _ssl.sslwrap. It's not though and will
  173. simply be tested as implemented for the time being.
  174. ssl(PythonSocket.socket sock,
  175. [DefaultParameterValue(null)] string keyfile,
  176. [DefaultParameterValue(null)] string certfile)
  177. '''
  178. #--Positive
  179. #sock
  180. s = socket.socket(socket.AF_INET)
  181. s.connect((SSL_URL, SSL_PORT))
  182. ssl_s = real_ssl.sslwrap(s._sock, False)
  183. if is_cpython:
  184. pass #ssl_s.shutdown() #Too slow
  185. s.close()
  186. #sock, keyfile, certfile
  187. #TODO!
  188. @disabled
  189. @retry_on_failure
  190. def test_SSLType_ssl_neg():
  191. '''
  192. See comments on test_SSLType_ssl. Basically this needs to be revisited
  193. entirely (TODO) after we're more compatible with CPython.
  194. '''
  195. s = socket.socket(socket.AF_INET)
  196. s.connect((SSL_URL, SSL_PORT))
  197. #--Negative
  198. #Empty
  199. AssertError(TypeError, real_ssl.sslwrap)
  200. AssertError(TypeError, real_ssl.sslwrap, False)
  201. #None
  202. AssertError(TypeError, real_ssl.sslwrap, None, False)
  203. #s, bad keyfile
  204. #Should throw _ssl.SSLError because both keyfile and certificate weren't specified
  205. AssertError(real_ssl.SSLError, real_ssl.sslwrap, s._sock, False, "bad keyfile")
  206. #s, bad certfile
  207. #Should throw _ssl.SSLError because both keyfile and certificate weren't specified
  208. #s, bad keyfile, bad certfile
  209. #Should throw ssl.SSLError
  210. AssertError(real_ssl.SSLError, real_ssl.sslwrap, s._sock, False, "bad keyfile", "bad certfile")
  211. #Cleanup
  212. s.close()
  213. @retry_on_failure
  214. def test_SSLType_issuer():
  215. #--Positive
  216. s = socket.socket(socket.AF_INET)
  217. s.connect((SSL_URL, SSL_PORT))
  218. ssl_s = real_ssl.sslwrap(s._sock, False)
  219. AreEqual(ssl_s.issuer(), '') #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=24281
  220. ssl_s.do_handshake()
  221. if is_cpython:
  222. AreEqual(ssl_s.issuer.__doc__, None)
  223. else:
  224. #Incompat, but a good one at that
  225. Assert("Returns a string that describes the issuer of the server's certificate" in ssl_s.issuer.__doc__)
  226. issuer = ssl_s.issuer()
  227. #If we can get the issuer once, we should be able to do it again
  228. AreEqual(issuer, ssl_s.issuer())
  229. Assert(SSL_ISSUER in issuer)
  230. #--Negative
  231. AssertErrorWithMessage(TypeError, "issuer() takes no arguments (1 given)",
  232. ssl_s.issuer, None)
  233. AssertErrorWithMessage(TypeError, "issuer() takes no arguments (1 given)",
  234. ssl_s.issuer, 1)
  235. AssertErrorWithMessage(TypeError, "issuer() takes no arguments (2 given)",
  236. ssl_s.issuer, 3.14, "abc")
  237. #Cleanup
  238. if is_cpython:
  239. pass #ssl_s.shutdown() #Too slow
  240. s.close()
  241. @retry_on_failure
  242. def test_SSLType_server():
  243. #--Positive
  244. s = socket.socket(socket.AF_INET)
  245. s.connect((SSL_URL, SSL_PORT))
  246. ssl_s = real_ssl.sslwrap(s._sock, False)
  247. AreEqual(ssl_s.server(), '') #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=24281
  248. ssl_s.do_handshake()
  249. if is_cpython:
  250. AreEqual(ssl_s.server.__doc__, None)
  251. else:
  252. #Incompat, but a good one at that
  253. Assert("Returns a string that describes the issuer of the server's certificate" in ssl_s.issuer.__doc__)
  254. server = ssl_s.server()
  255. #If we can get the server once, we should be able to do it again
  256. AreEqual(server, ssl_s.server())
  257. Assert(SSL_SERVER in server)
  258. #--Negative
  259. AssertErrorWithMessage(TypeError, "server() takes no arguments (1 given)",
  260. ssl_s.server, None)
  261. AssertErrorWithMessage(TypeError, "server() takes no arguments (1 given)",
  262. ssl_s.server, 1)
  263. AssertErrorWithMessage(TypeError, "server() takes no arguments (2 given)",
  264. ssl_s.server, 3.14, "abc")
  265. #Cleanup
  266. if is_cpython:
  267. pass #ssl_s.shutdown() #Too slow
  268. s.close()
  269. @retry_on_failure
  270. def test_SSLType_read_and_write():
  271. #--Positive
  272. s = socket.socket(socket.AF_INET)
  273. s.connect((SSL_URL, SSL_PORT))
  274. ssl_s = real_ssl.sslwrap(s._sock, False)
  275. ssl_s.do_handshake()
  276. if is_cpython:
  277. Assert("Writes the string s into the SSL object" in ssl_s.write.__doc__)
  278. Assert("Read up to len bytes from the SSL socket" in ssl_s.read.__doc__)
  279. else:
  280. #Incompat, but we can live with this
  281. Assert("Writes the string s through the SSL connection" in ssl_s.write.__doc__)
  282. Assert("If n is present, reads up to n bytes from the SSL connection" in ssl_s.read.__doc__)
  283. #Write
  284. AreEqual(ssl_s.write(SSL_REQUEST),
  285. len(SSL_REQUEST))
  286. #Read
  287. AreEqual(ssl_s.read(4).lower(), "http")
  288. response = ssl_s.read(5000)
  289. Assert(SSL_RESPONSE in response)
  290. #Cleanup
  291. if is_cpython:
  292. pass #ssl_s.shutdown() #Too slow
  293. s.close()
  294. #--MAIN------------------------------------------------------------------------
  295. run_test(__name__)