PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_2_6/Src/Tests/modules/network_related/_ssl_test.py

#
Python | 364 lines | 325 code | 16 blank | 23 comment | 1 complexity | 35370512f8814e6079597ff13a45e9f3 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Microsoft Public License. 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 Microsoft Public License, 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 Microsoft Public License.
  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 = "Microsoft Secure Server Authority"
  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("cli", "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("cli", "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("cli", "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("cli", "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("cli", "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("cli", "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("cli", "silverlight") #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21411
  58. def test_PROTOCOL_TLSv1():
  59. AreEqual(real_ssl.PROTOCOL_TLSv1,
  60. 3)
  61. def test_RAND_add():
  62. #--Positive
  63. AreEqual(real_ssl.RAND_add("", 3.14),
  64. None)
  65. AreEqual(real_ssl.RAND_add(u"", 3.14),
  66. None)
  67. AreEqual(real_ssl.RAND_add("", 3),
  68. None)
  69. #--Negative
  70. for g1, g2 in [ (None, None),
  71. ("", None),
  72. #(None, 3.14), #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=24276
  73. ]:
  74. AssertError(TypeError, real_ssl.RAND_add, g1, g2)
  75. AssertError(TypeError, real_ssl.RAND_add)
  76. AssertError(TypeError, real_ssl.RAND_add, "")
  77. AssertError(TypeError, real_ssl.RAND_add, 3.14)
  78. AssertError(TypeError, real_ssl.RAND_add, "", 3.14, "")
  79. def test_RAND_egd():
  80. #--Positive
  81. if not is_cpython:
  82. AreEqual(real_ssl.RAND_egd(""),
  83. None)
  84. #--Negative
  85. AssertError(TypeError, real_ssl.RAND_egd)
  86. AssertError(TypeError, real_ssl.RAND_add, None)
  87. AssertError(TypeError, real_ssl.RAND_add, 1)
  88. AssertError(TypeError, real_ssl.RAND_add, 3.14)
  89. AssertError(TypeError, real_ssl.RAND_add, "", "")
  90. def test_RAND_status():
  91. #--Positive
  92. AreEqual(real_ssl.RAND_status(),
  93. 1)
  94. #--Negative
  95. AssertError(TypeError, real_ssl.RAND_status, None)
  96. AssertError(TypeError, real_ssl.RAND_status, "")
  97. AssertError(TypeError, real_ssl.RAND_status, 1)
  98. AssertError(TypeError, real_ssl.RAND_status, None, None)
  99. def test_SSLError():
  100. AreEqual(real_ssl.SSLError.__bases__, (socket.error, ))
  101. def test_SSL_ERROR_EOF():
  102. AreEqual(real_ssl.SSL_ERROR_EOF, 8)
  103. def test_SSL_ERROR_INVALID_ERROR_CODE():
  104. AreEqual(real_ssl.SSL_ERROR_INVALID_ERROR_CODE, 9)
  105. def test_SSL_ERROR_SSL():
  106. AreEqual(real_ssl.SSL_ERROR_SSL, 1)
  107. def test_SSL_ERROR_SYSCALL():
  108. AreEqual(real_ssl.SSL_ERROR_SYSCALL, 5)
  109. def test_SSL_ERROR_WANT_CONNECT():
  110. AreEqual(real_ssl.SSL_ERROR_WANT_CONNECT, 7)
  111. def test_SSL_ERROR_WANT_READ():
  112. AreEqual(real_ssl.SSL_ERROR_WANT_READ, 2)
  113. def test_SSL_ERROR_WANT_WRITE():
  114. AreEqual(real_ssl.SSL_ERROR_WANT_WRITE, 3)
  115. def test_SSL_ERROR_WANT_X509_LOOKUP():
  116. AreEqual(real_ssl.SSL_ERROR_WANT_X509_LOOKUP, 4)
  117. def test_SSL_ERROR_ZERO_RETURN():
  118. AreEqual(real_ssl.SSL_ERROR_ZERO_RETURN, 6)
  119. @skip("cli", "silverlight") #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21411
  120. def test___doc__():
  121. expected_doc = """Implementation module for SSL socket operations. See the socket module
  122. for documentation."""
  123. AreEqual(real_ssl.__doc__, expected_doc)
  124. def test__test_decode_cert():
  125. if not is_cpython and hasattr(real_ssl, "decode_cert"):
  126. raise Exception("Please add a test for _ssl.decode_cert")
  127. print 'TODO: no implementation to test yet.'
  128. def test_sslwrap():
  129. print 'TODO: no implementation to test yet.'
  130. def test_SSLType():
  131. #--Positive
  132. if is_cpython:
  133. AreEqual(str(real_ssl.SSLType),
  134. "<type 'ssl.SSLContext'>")
  135. else:
  136. #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=24266
  137. AreEqual(str(real_ssl.SSLType),
  138. "<type 'socket.ssl'>")
  139. #--Negative
  140. if is_cpython:
  141. AssertErrorWithMessage(TypeError, "cannot create 'ssl.SSLContext' instances",
  142. real_ssl.SSLType)
  143. else:
  144. #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=24277
  145. AssertErrorWithMessage(TypeError, "ssl() takes at least 1 argument (0 given)",
  146. real_ssl.SSLType)
  147. #--TEST CASES for _ssl.sll-----------------------------------------------------
  148. '''
  149. TODO: once we have a proper implementation of _ssl.sslwrap the tests below need
  150. to be rewritten.
  151. '''
  152. @retry_on_failure
  153. def test_SSLType_ssl():
  154. '''
  155. Should be essentially the same as _ssl.sslwrap. It's not though and will
  156. simply be tested as implemented for the time being.
  157. ssl(PythonSocket.socket sock,
  158. [DefaultParameterValue(null)] string keyfile,
  159. [DefaultParameterValue(null)] string certfile)
  160. '''
  161. #--Positive
  162. #sock
  163. s = socket.socket(socket.AF_INET)
  164. s.connect((SSL_URL, SSL_PORT))
  165. ssl_s = real_ssl.sslwrap(s._sock, False)
  166. if is_cpython:
  167. pass #ssl_s.shutdown() #Too slow
  168. s.close()
  169. #sock, keyfile, certfile
  170. #TODO!
  171. @retry_on_failure
  172. def test_SSLType_ssl_neg():
  173. '''
  174. See comments on test_SSLType_ssl. Basically this needs to be revisited
  175. entirely (TODO) after we're more compatible with CPython.
  176. '''
  177. s = socket.socket(socket.AF_INET)
  178. s.connect((SSL_URL, SSL_PORT))
  179. #--Negative
  180. #Empty
  181. AssertError(TypeError, real_ssl.sslwrap)
  182. AssertError(TypeError, real_ssl.sslwrap, False)
  183. #None
  184. AssertError(TypeError, real_ssl.sslwrap, None, False)
  185. #s, bad keyfile
  186. #Should throw _ssl.SSLError because both keyfile and certificate weren't specified
  187. AssertError(real_ssl.SSLError, real_ssl.sslwrap, s._sock, False, "bad keyfile")
  188. #s, bad certfile
  189. #Should throw _ssl.SSLError because both keyfile and certificate weren't specified
  190. #s, bad keyfile, bad certfile
  191. #Should throw ssl.SSLError
  192. AssertError(real_ssl.SSLError, real_ssl.sslwrap, s._sock, False, "bad keyfile", "bad certfile")
  193. #Cleanup
  194. s.close()
  195. @retry_on_failure
  196. def test_SSLType_issuer():
  197. #--Positive
  198. s = socket.socket(socket.AF_INET)
  199. s.connect((SSL_URL, SSL_PORT))
  200. ssl_s = real_ssl.sslwrap(s._sock, False)
  201. AreEqual(ssl_s.issuer(), '') #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=24281
  202. ssl_s.do_handshake()
  203. if is_cpython:
  204. AreEqual(ssl_s.issuer.__doc__, None)
  205. else:
  206. #Incompat, but a good one at that
  207. Assert("Returns a string that describes the issuer of the server's certificate" in ssl_s.issuer.__doc__)
  208. issuer = ssl_s.issuer()
  209. #If we can get the issuer once, we should be able to do it again
  210. AreEqual(issuer, ssl_s.issuer())
  211. Assert(SSL_ISSUER in issuer)
  212. #--Negative
  213. AssertErrorWithMessage(TypeError, "issuer() takes no arguments (1 given)",
  214. ssl_s.issuer, None)
  215. AssertErrorWithMessage(TypeError, "issuer() takes no arguments (1 given)",
  216. ssl_s.issuer, 1)
  217. AssertErrorWithMessage(TypeError, "issuer() takes no arguments (2 given)",
  218. ssl_s.issuer, 3.14, "abc")
  219. #Cleanup
  220. if is_cpython:
  221. pass #ssl_s.shutdown() #Too slow
  222. s.close()
  223. @retry_on_failure
  224. def test_SSLType_server():
  225. #--Positive
  226. s = socket.socket(socket.AF_INET)
  227. s.connect((SSL_URL, SSL_PORT))
  228. ssl_s = real_ssl.sslwrap(s._sock, False)
  229. AreEqual(ssl_s.server(), '') #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=24281
  230. ssl_s.do_handshake()
  231. if is_cpython:
  232. AreEqual(ssl_s.server.__doc__, None)
  233. else:
  234. #Incompat, but a good one at that
  235. Assert("Returns a string that describes the issuer of the server's certificate" in ssl_s.issuer.__doc__)
  236. server = ssl_s.server()
  237. #If we can get the server once, we should be able to do it again
  238. AreEqual(server, ssl_s.server())
  239. Assert(SSL_SERVER in server)
  240. #--Negative
  241. AssertErrorWithMessage(TypeError, "server() takes no arguments (1 given)",
  242. ssl_s.server, None)
  243. AssertErrorWithMessage(TypeError, "server() takes no arguments (1 given)",
  244. ssl_s.server, 1)
  245. AssertErrorWithMessage(TypeError, "server() takes no arguments (2 given)",
  246. ssl_s.server, 3.14, "abc")
  247. #Cleanup
  248. if is_cpython:
  249. pass #ssl_s.shutdown() #Too slow
  250. s.close()
  251. @retry_on_failure
  252. def test_SSLType_read_and_write():
  253. #--Positive
  254. s = socket.socket(socket.AF_INET)
  255. s.connect((SSL_URL, SSL_PORT))
  256. ssl_s = real_ssl.sslwrap(s._sock, False)
  257. ssl_s.do_handshake()
  258. if is_cpython:
  259. Assert("Writes the string s into the SSL object" in ssl_s.write.__doc__)
  260. Assert("Read up to len bytes from the SSL socket" in ssl_s.read.__doc__)
  261. else:
  262. #Incompat, but we can live with this
  263. Assert("Writes the string s through the SSL connection" in ssl_s.write.__doc__)
  264. Assert("If n is present, reads up to n bytes from the SSL connection" in ssl_s.read.__doc__)
  265. #Write
  266. AreEqual(ssl_s.write(SSL_REQUEST),
  267. len(SSL_REQUEST))
  268. #Read
  269. AreEqual(ssl_s.read(4).lower(), "http")
  270. response = ssl_s.read(5000)
  271. Assert(SSL_RESPONSE in response)
  272. #Cleanup
  273. if is_cpython:
  274. pass #ssl_s.shutdown() #Too slow
  275. s.close()
  276. #--MAIN------------------------------------------------------------------------
  277. run_test(__name__)