File: ssl_protocol_test.py

package info (click to toggle)
python-httplib2 0.11.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 924 kB
  • sloc: python: 5,701; makefile: 4
file content (57 lines) | stat: -rwxr-xr-x 1,880 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Tests for SSL handling in httplib2."""

import httplib2
import os
import ssl
import sys
import unittest


class TestSslProtocol(unittest.TestCase):

  def testSslCertValidationWithInvalidCaCert(self):
    if sys.version_info >= (2, 6):
      http = httplib2.Http(ca_certs='/nosuchfile')
      if sys.version_info >= (2, 7):
        with self.assertRaises(IOError):
          http.request('https://www.google.com/', 'GET')
      else:
        self.assertRaises(
            ssl.SSLError, http.request, 'https://www.google.com/', 'GET')

  def testSslCertValidationWithSelfSignedCaCert(self):
    if sys.version_info >= (2, 7):
      other_ca_certs = os.path.join(
          os.path.dirname(os.path.abspath(httplib2.__file__ )), 'test',
          'other_cacerts.txt')
      http = httplib2.Http(ca_certs=other_ca_certs)
      if sys.platform != 'darwin':
        with self.assertRaises(httplib2.SSLHandshakeError):
          http.request('https://www.google.com/', 'GET')

  def testSslProtocolTlsV1AndShouldPass(self):
    http = httplib2.Http(ssl_version=ssl.PROTOCOL_TLSv1)
    urls = ['https://www.amazon.com',
            'https://www.apple.com',
            'https://www.twitter.com']
    for url in urls:
      if sys.version_info >= (2, 7):
        self.assertIsNotNone(http.request(uri=url))

  def testSslProtocolV3AndShouldFailDueToPoodle(self):
    http = httplib2.Http(ssl_version=ssl.PROTOCOL_SSLv3)
    urls = ['https://www.amazon.com',
            'https://www.apple.com',
            'https://www.twitter.com']
    for url in urls:
      if sys.version_info >= (2, 7):
        with self.assertRaises(httplib2.SSLHandshakeError):
          http.request(url)
        try:
          http.request(url)
        except httplib2.SSLHandshakeError as e:
          self.assertTrue('sslv3 alert handshake failure' in str(e))


if __name__ == '__main__':
  unittest.main()