ó
ŒGUc           @   sæ   d  Z  d Z d Z d Z d Z d Z d Z d d l Z e j d	 e j	 ƒ e j
 e ƒ Z d d l Z d d l Z d d
 l m Z d d l m Z d d l m Z d d l m Z d e j f d „  ƒ  YZ e d k râ e j ƒ  n  d S(   s•   unit tests module for ndg.httpsclient.https.HTTPSconnection class

PyOpenSSL utility to make a httplib-like interface suitable for use with 
urllib2
s   P J Kershaw (STFC)s   06/01/12s2   (C) 2012 Science and Technology Facilities Councils-   BSD - see LICENSE file in top-level directorys   Philip.Kershaw@stfc.ac.uks   $Id$iÿÿÿÿNt   level(   t   SSL(   t	   Constants(   t   HTTPSConnection(   t   ServerSSLCertVerificationt   TestHTTPSConnectionc           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s+   Test ndg HTTPS client HTTPSConnection classc         C   s[   t  t j d t j ƒ} | j ƒ  | j d d ƒ | j ƒ  } d | j ƒ  GH| j ƒ  d  S(   Nt   portt   GETt   /s   Response = %s(	   R   R   t   HOSTNAMEt   PORTt   connectt   requestt   getresponset   readt   close(   t   selft   connt   resp(    (    sC   /usr/lib/python2.7/dist-packages/ndg/httpsclient/test/test_https.pyt   test01_open   s    
c         C   s2   t  t j d t j ƒ} |  j t j | j ƒ d  S(   NR   (   R   R   R	   t   PORT2t   assertRaisest   sockett   errorR   (   R   R   (    (    sC   /usr/lib/python2.7/dist-packages/ndg/httpsclient/test/test_https.pyt   test02_open_fails$   s    c         C   s–   t  j t  j ƒ } d „  } | j t  j | ƒ | j d ƒ | j d  t j	 ƒ t
 t j d t j d | ƒ} | j ƒ  |  j t  j | j d d ƒ d  S(   Nc         S   s   t  j d | j ƒ  ƒ | S(   Ns/   SSL peer certificate verification failed for %r(   t   logt   debugt   get_subject(   R   t   x509t   errnumt   errdeptht   preverify_ok(    (    sC   /usr/lib/python2.7/dist-packages/ndg/httpsclient/test/test_https.pyt   verify_callback+   s    	i	   R   t   ssl_contextR   R   (   R   t   Contextt   TLSv1_METHODt
   set_verifyt   VERIFY_PEERt   set_verify_deptht   load_verify_locationst   NoneR   t   UNITTEST_DIRR   R	   R
   R   R   t   ErrorR   (   R   t   ctxR    R   (    (    sC   /usr/lib/python2.7/dist-packages/ndg/httpsclient/test/test_https.pyt%   test03_ssl_verification_of_peer_fails(   s    		
c         C   s¥   t  j t  j ƒ } d „  } | j t  j | ƒ | j d ƒ | j d  t j	 ƒ t
 t j d t j d | ƒ} | j ƒ  | j d d ƒ | j ƒ  } d | j ƒ  GHd  S(   Nc         S   s   | S(   N(    (   R   R   R   R   R   (    (    sC   /usr/lib/python2.7/dist-packages/ndg/httpsclient/test/test_https.pyt   <lambda>>   s    i	   R   R!   R   R   s   Response = %s(   R   R"   R#   R$   R%   R&   R'   R(   R   t
   CACERT_DIRR   R	   R
   R   R   R   R   (   R   R+   R    R   R   (    (    sC   /usr/lib/python2.7/dist-packages/ndg/httpsclient/test/test_https.pyt(   test03_ssl_verification_of_peer_succeeds;   s    		
c         C   s·   t  j t  j ƒ } t d d ƒ } | j ƒ  } | j t  j | ƒ | j d ƒ | j d  t
 j ƒ t t
 j d t
 j d | ƒ} | j ƒ  | j d d ƒ | j ƒ  } d | j ƒ  GHd  S(	   Nt   hostnamet	   localhosti	   R   R!   R   R   s   Response = %s(   R   R"   R#   R   t   get_verify_server_cert_funcR$   R%   R&   R'   R(   R   R.   R   R	   R
   R   R   R   R   (   R   R+   t   verificationR    R   R   (    (    sC   /usr/lib/python2.7/dist-packages/ndg/httpsclient/test/test_https.pyt*   test04_ssl_verification_with_subj_alt_nameN   s    	
c         C   s½   t  j t  j ƒ } t d d d t ƒ } | j ƒ  } | j t  j | ƒ | j d ƒ | j	 d  t j ƒ t t j d t j d | ƒ} | j ƒ  | j d d ƒ | j ƒ  } d	 | j ƒ  GHd  S(
   NR0   R1   t   subj_alt_name_matchi	   R   R!   R   R   s   Response = %s(   R   R"   R#   R   t   FalseR2   R$   R%   R&   R'   R(   R   R.   R   R	   R
   R   R   R   R   (   R   R+   R3   R    R   R   (    (    sC   /usr/lib/python2.7/dist-packages/ndg/httpsclient/test/test_https.pyt-   test04_ssl_verification_with_subj_common_namea   s    		
(	   t   __name__t
   __module__t   __doc__R   R   R,   R/   R4   R7   (    (    (    sC   /usr/lib/python2.7/dist-packages/ndg/httpsclient/test/test_https.pyR      s   					t   __main__(   R:   t
   __author__t   __date__t   __copyright__t   __license__t   __contact__t   __revision__t   loggingt   basicConfigt   DEBUGt	   getLoggerR8   R   t   unittestR   t   OpenSSLR   t   ndg.httpsclient.testR   t   ndg.httpsclient.httpsR   t%   ndg.httpsclient.ssl_peer_verificationR   t   TestCaseR   t   main(    (    (    sC   /usr/lib/python2.7/dist-packages/ndg/httpsclient/test/test_https.pyt   <module>   s$   `