class SimpleHTTPResponseTest(unittest.TestCase):

    def setUp(self):
        from acme.challenges import SimpleHTTPResponse
        self.msg = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA')
        self.jmsg = {
            'type': 'simpleHttp',
            'path': '6tbIMBC5Anhl5bOlWT5ZFA',
        }

    def test_uri(self):
        self.assertEqual('https://example.com/.well-known/acme-challenge/'
                         '6tbIMBC5Anhl5bOlWT5ZFA', self.msg.uri('example.com'))

    def test_to_partial_json(self):
        self.assertEqual(self.jmsg, self.msg.to_partial_json())

    def test_from_json(self):
        from acme.challenges import SimpleHTTPResponse
        self.assertEqual(
            self.msg, SimpleHTTPResponse.from_json(self.jmsg))

    def test_from_json_hashable(self):
        from acme.challenges import SimpleHTTPResponse
        hash(SimpleHTTPResponse.from_json(self.jmsg))
Exemple #2
0
 def setUp(self):
     from acme.challenges import SimpleHTTPResponse
     self.msg = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA')
     self.jmsg = {
         'type': 'simpleHttp',
         'path': '6tbIMBC5Anhl5bOlWT5ZFA',
     }
Exemple #3
0
class SimpleHTTPResponseTest(unittest.TestCase):
    def setUp(self):
        from acme.challenges import SimpleHTTPResponse
        self.msg_http = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA',
                                           tls=False)
        self.msg_https = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA')
        self.jmsg_http = {
            'type': 'simpleHttp',
            'path': '6tbIMBC5Anhl5bOlWT5ZFA',
            'tls': False,
        }
        self.jmsg_https = {
            'type': 'simpleHttp',
            'path': '6tbIMBC5Anhl5bOlWT5ZFA',
            'tls': True,
        }

    def test_good_path(self):
        self.assertTrue(self.msg_http.good_path)
        self.assertTrue(self.msg_https.good_path)
        self.assertFalse(
            self.msg_http.update(path=(self.msg_http.path * 10)).good_path)

    def test_scheme(self):
        self.assertEqual('http', self.msg_http.scheme)
        self.assertEqual('https', self.msg_https.scheme)

    def test_uri(self):
        self.assertEqual(
            'http://example.com/.well-known/acme-challenge/'
            '6tbIMBC5Anhl5bOlWT5ZFA', self.msg_http.uri('example.com'))
        self.assertEqual(
            'https://example.com/.well-known/acme-challenge/'
            '6tbIMBC5Anhl5bOlWT5ZFA', self.msg_https.uri('example.com'))

    def test_to_partial_json(self):
        self.assertEqual(self.jmsg_http, self.msg_http.to_partial_json())
        self.assertEqual(self.jmsg_https, self.msg_https.to_partial_json())

    def test_from_json(self):
        from acme.challenges import SimpleHTTPResponse
        self.assertEqual(self.msg_http,
                         SimpleHTTPResponse.from_json(self.jmsg_http))
        self.assertEqual(self.msg_https,
                         SimpleHTTPResponse.from_json(self.jmsg_https))

    def test_from_json_hashable(self):
        from acme.challenges import SimpleHTTPResponse
        hash(SimpleHTTPResponse.from_json(self.jmsg_http))
        hash(SimpleHTTPResponse.from_json(self.jmsg_https))
class SimpleHTTPResponseTest(unittest.TestCase):

    def setUp(self):
        from acme.challenges import SimpleHTTPResponse
        self.msg_http = SimpleHTTPResponse(
            path='6tbIMBC5Anhl5bOlWT5ZFA', tls=False)
        self.msg_https = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA')
        self.jmsg_http = {
            'type': 'simpleHttp',
            'path': '6tbIMBC5Anhl5bOlWT5ZFA',
            'tls': False,
        }
        self.jmsg_https = {
            'type': 'simpleHttp',
            'path': '6tbIMBC5Anhl5bOlWT5ZFA',
            'tls': True,
        }

    def test_good_path(self):
        self.assertTrue(self.msg_http.good_path)
        self.assertTrue(self.msg_https.good_path)
        self.assertFalse(
            self.msg_http.update(path=(self.msg_http.path * 10)).good_path)

    def test_scheme(self):
        self.assertEqual('http', self.msg_http.scheme)
        self.assertEqual('https', self.msg_https.scheme)

    def test_uri(self):
        self.assertEqual('http://example.com/.well-known/acme-challenge/'
                         '6tbIMBC5Anhl5bOlWT5ZFA', self.msg_http.uri('example.com'))
        self.assertEqual('https://example.com/.well-known/acme-challenge/'
                         '6tbIMBC5Anhl5bOlWT5ZFA', self.msg_https.uri('example.com'))

    def test_to_partial_json(self):
        self.assertEqual(self.jmsg_http, self.msg_http.to_partial_json())
        self.assertEqual(self.jmsg_https, self.msg_https.to_partial_json())

    def test_from_json(self):
        from acme.challenges import SimpleHTTPResponse
        self.assertEqual(
            self.msg_http, SimpleHTTPResponse.from_json(self.jmsg_http))
        self.assertEqual(
            self.msg_https, SimpleHTTPResponse.from_json(self.jmsg_https))

    def test_from_json_hashable(self):
        from acme.challenges import SimpleHTTPResponse
        hash(SimpleHTTPResponse.from_json(self.jmsg_http))
        hash(SimpleHTTPResponse.from_json(self.jmsg_https))
Exemple #5
0
 def setUp(self):
     from acme.challenges import SimpleHTTPResponse
     self.msg_http = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA',
                                        tls=False)
     self.msg_https = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA')
     self.jmsg_http = {
         'type': 'simpleHttp',
         'path': '6tbIMBC5Anhl5bOlWT5ZFA',
         'tls': False,
     }
     self.jmsg_https = {
         'type': 'simpleHttp',
         'path': '6tbIMBC5Anhl5bOlWT5ZFA',
         'tls': True,
     }
 def setUp(self):
     from acme.challenges import SimpleHTTPResponse
     self.msg_http = SimpleHTTPResponse(
         path='6tbIMBC5Anhl5bOlWT5ZFA', tls=False)
     self.msg_https = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA')
     self.jmsg_http = {
         'type': 'simpleHttp',
         'path': '6tbIMBC5Anhl5bOlWT5ZFA',
         'tls': False,
     }
     self.jmsg_https = {
         'type': 'simpleHttp',
         'path': '6tbIMBC5Anhl5bOlWT5ZFA',
         'tls': True,
     }
 def setUp(self):
     from acme.challenges import SimpleHTTPResponse
     self.msg = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA')
     self.jmsg = {
         'type': 'simpleHttp',
         'path': '6tbIMBC5Anhl5bOlWT5ZFA',
     }
    def setUp(self):
        from acme.challenges import SimpleHTTPResponse
        self.msg_http = SimpleHTTPResponse(tls=False)
        self.msg_https = SimpleHTTPResponse(tls=True)
        self.jmsg_http = {
            'resource': 'challenge',
            'type': 'simpleHttp',
            'tls': False,
        }
        self.jmsg_https = {
            'resource': 'challenge',
            'type': 'simpleHttp',
            'tls': True,
        }

        from acme.challenges import SimpleHTTP
        self.chall = SimpleHTTP(token=(b"x" * 16))
        self.resp_http = SimpleHTTPResponse(tls=False)
        self.resp_https = SimpleHTTPResponse(tls=True)
        self.good_headers = {'Content-Type': SimpleHTTPResponse.CONTENT_TYPE}
    def setUp(self):
        from acme.challenges import SimpleHTTPResponse
        self.msg_http = SimpleHTTPResponse(
            path='6tbIMBC5Anhl5bOlWT5ZFA', tls=False)
        self.msg_https = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA')
        self.jmsg_http = {
            'type': 'simpleHttp',
            'path': '6tbIMBC5Anhl5bOlWT5ZFA',
            'tls': False,
        }
        self.jmsg_https = {
            'type': 'simpleHttp',
            'path': '6tbIMBC5Anhl5bOlWT5ZFA',
            'tls': True,
        }

        from acme.challenges import SimpleHTTP
        self.chall = SimpleHTTP(token="foo")
        self.resp_http = SimpleHTTPResponse(path="bar", tls=False)
        self.resp_https = SimpleHTTPResponse(path="bar", tls=True)
        self.good_headers = {'Content-Type': SimpleHTTPResponse.CONTENT_TYPE}
Exemple #10
0
class SimpleHTTPResponseTest(unittest.TestCase):
    def setUp(self):
        from acme.challenges import SimpleHTTPResponse
        self.msg = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA')
        self.jmsg = {
            'type': 'simpleHttp',
            'path': '6tbIMBC5Anhl5bOlWT5ZFA',
        }

    def test_uri(self):
        self.assertEqual(
            'https://example.com/.well-known/acme-challenge/'
            '6tbIMBC5Anhl5bOlWT5ZFA', self.msg.uri('example.com'))

    def test_to_partial_json(self):
        self.assertEqual(self.jmsg, self.msg.to_partial_json())

    def test_from_json(self):
        from acme.challenges import SimpleHTTPResponse
        self.assertEqual(self.msg, SimpleHTTPResponse.from_json(self.jmsg))

    def test_from_json_hashable(self):
        from acme.challenges import SimpleHTTPResponse
        hash(SimpleHTTPResponse.from_json(self.jmsg))
Exemple #11
0
    def setUp(self):
        from acme.challenges import SimpleHTTPResponse
        self.msg_http = SimpleHTTPResponse(tls=False)
        self.msg_https = SimpleHTTPResponse(tls=True)
        self.jmsg_http = {
            'resource': 'challenge',
            'type': 'simpleHttp',
            'tls': False,
        }
        self.jmsg_https = {
            'resource': 'challenge',
            'type': 'simpleHttp',
            'tls': True,
        }

        from acme.challenges import SimpleHTTP
        self.chall = SimpleHTTP(token=(b"x" * 16))
        self.resp_http = SimpleHTTPResponse(tls=False)
        self.resp_https = SimpleHTTPResponse(tls=True)
        self.good_headers = {'Content-Type': SimpleHTTPResponse.CONTENT_TYPE}
Exemple #12
0
    def setUp(self):
        from acme.challenges import SimpleHTTPResponse
        self.msg_http = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA',
                                           tls=False)
        self.msg_https = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA')
        self.jmsg_http = {
            'type': 'simpleHttp',
            'path': '6tbIMBC5Anhl5bOlWT5ZFA',
            'tls': False,
        }
        self.jmsg_https = {
            'type': 'simpleHttp',
            'path': '6tbIMBC5Anhl5bOlWT5ZFA',
            'tls': True,
        }

        from acme.challenges import SimpleHTTP
        self.chall = SimpleHTTP(token="foo")
        self.resp_http = SimpleHTTPResponse(path="bar", tls=False)
        self.resp_https = SimpleHTTPResponse(path="bar", tls=True)
        self.good_headers = {'Content-Type': SimpleHTTPResponse.CONTENT_TYPE}
class SimpleHTTPResponseTest(unittest.TestCase):
    # pylint: disable=too-many-instance-attributes

    def setUp(self):
        from acme.challenges import SimpleHTTPResponse
        self.msg_http = SimpleHTTPResponse(
            path='6tbIMBC5Anhl5bOlWT5ZFA', tls=False)
        self.msg_https = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA')
        self.jmsg_http = {
            'type': 'simpleHttp',
            'path': '6tbIMBC5Anhl5bOlWT5ZFA',
            'tls': False,
        }
        self.jmsg_https = {
            'type': 'simpleHttp',
            'path': '6tbIMBC5Anhl5bOlWT5ZFA',
            'tls': True,
        }

        from acme.challenges import SimpleHTTP
        self.chall = SimpleHTTP(token="foo")
        self.resp_http = SimpleHTTPResponse(path="bar", tls=False)
        self.resp_https = SimpleHTTPResponse(path="bar", tls=True)
        self.good_headers = {'Content-Type': SimpleHTTPResponse.CONTENT_TYPE}

    def test_good_path(self):
        self.assertTrue(self.msg_http.good_path)
        self.assertTrue(self.msg_https.good_path)
        self.assertFalse(
            self.msg_http.update(path=(self.msg_http.path * 10)).good_path)

    def test_scheme(self):
        self.assertEqual('http', self.msg_http.scheme)
        self.assertEqual('https', self.msg_https.scheme)

    def test_port(self):
        self.assertEqual(80, self.msg_http.port)
        self.assertEqual(443, self.msg_https.port)

    def test_uri(self):
        self.assertEqual(
            'http://example.com/.well-known/acme-challenge/'
            '6tbIMBC5Anhl5bOlWT5ZFA', self.msg_http.uri('example.com'))
        self.assertEqual(
            'https://example.com/.well-known/acme-challenge/'
            '6tbIMBC5Anhl5bOlWT5ZFA', self.msg_https.uri('example.com'))

    def test_to_partial_json(self):
        self.assertEqual(self.jmsg_http, self.msg_http.to_partial_json())
        self.assertEqual(self.jmsg_https, self.msg_https.to_partial_json())

    def test_from_json(self):
        from acme.challenges import SimpleHTTPResponse
        self.assertEqual(
            self.msg_http, SimpleHTTPResponse.from_json(self.jmsg_http))
        self.assertEqual(
            self.msg_https, SimpleHTTPResponse.from_json(self.jmsg_https))

    def test_from_json_hashable(self):
        from acme.challenges import SimpleHTTPResponse
        hash(SimpleHTTPResponse.from_json(self.jmsg_http))
        hash(SimpleHTTPResponse.from_json(self.jmsg_https))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_good_token(self, mock_get):
        for resp in self.resp_http, self.resp_https:
            mock_get.reset_mock()
            mock_get.return_value = mock.MagicMock(
                text=self.chall.token, headers=self.good_headers)
            self.assertTrue(resp.simple_verify(self.chall, "local"))
            mock_get.assert_called_once_with(resp.uri("local"), verify=False)

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_bad_token(self, mock_get):
        mock_get.return_value = mock.MagicMock(
            text=self.chall.token + "!", headers=self.good_headers)
        self.assertFalse(self.resp_http.simple_verify(self.chall, "local"))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_bad_content_type(self, mock_get):
        mock_get().text = self.chall.token
        self.assertFalse(self.resp_http.simple_verify(self.chall, "local"))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_connection_error(self, mock_get):
        mock_get.side_effect = requests.exceptions.RequestException
        self.assertFalse(self.resp_http.simple_verify(self.chall, "local"))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_port(self, mock_get):
        self.resp_http.simple_verify(self.chall, "local", 4430)
        self.assertEqual("local:4430", urlparse.urlparse(
            mock_get.mock_calls[0][1][0]).netloc)
 def test_from_json_hashable(self):
     from acme.challenges import SimpleHTTPResponse
     hash(SimpleHTTPResponse.from_json(self.jmsg_http))
     hash(SimpleHTTPResponse.from_json(self.jmsg_https))
 def test_from_json(self):
     from acme.challenges import SimpleHTTPResponse
     self.assertEqual(
         self.msg_http, SimpleHTTPResponse.from_json(self.jmsg_http))
     self.assertEqual(
         self.msg_https, SimpleHTTPResponse.from_json(self.jmsg_https))
 def test_from_json(self):
     from acme.challenges import SimpleHTTPResponse
     self.assertEqual(self.msg_http,
                      SimpleHTTPResponse.from_json(self.jmsg_http))
     self.assertEqual(self.msg_https,
                      SimpleHTTPResponse.from_json(self.jmsg_https))
class SimpleHTTPResponseTest(unittest.TestCase):
    # pylint: disable=too-many-instance-attributes

    def setUp(self):
        from acme.challenges import SimpleHTTPResponse
        self.msg_http = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA',
                                           tls=False)
        self.msg_https = SimpleHTTPResponse(path='6tbIMBC5Anhl5bOlWT5ZFA')
        self.jmsg_http = {
            'resource': 'challenge',
            'type': 'simpleHttp',
            'path': '6tbIMBC5Anhl5bOlWT5ZFA',
            'tls': False,
        }
        self.jmsg_https = {
            'resource': 'challenge',
            'type': 'simpleHttp',
            'path': '6tbIMBC5Anhl5bOlWT5ZFA',
            'tls': True,
        }

        from acme.challenges import SimpleHTTP
        self.chall = SimpleHTTP(token="foo")
        self.resp_http = SimpleHTTPResponse(path="bar", tls=False)
        self.resp_https = SimpleHTTPResponse(path="bar", tls=True)
        self.good_headers = {'Content-Type': SimpleHTTPResponse.CONTENT_TYPE}

    def test_good_path(self):
        self.assertTrue(self.msg_http.good_path)
        self.assertTrue(self.msg_https.good_path)
        self.assertFalse(
            self.msg_http.update(path=(self.msg_http.path * 10)).good_path)

    def test_scheme(self):
        self.assertEqual('http', self.msg_http.scheme)
        self.assertEqual('https', self.msg_https.scheme)

    def test_port(self):
        self.assertEqual(80, self.msg_http.port)
        self.assertEqual(443, self.msg_https.port)

    def test_uri(self):
        self.assertEqual(
            'http://example.com/.well-known/acme-challenge/'
            '6tbIMBC5Anhl5bOlWT5ZFA', self.msg_http.uri('example.com'))
        self.assertEqual(
            'https://example.com/.well-known/acme-challenge/'
            '6tbIMBC5Anhl5bOlWT5ZFA', self.msg_https.uri('example.com'))

    def test_to_partial_json(self):
        self.assertEqual(self.jmsg_http, self.msg_http.to_partial_json())
        self.assertEqual(self.jmsg_https, self.msg_https.to_partial_json())

    def test_from_json(self):
        from acme.challenges import SimpleHTTPResponse
        self.assertEqual(self.msg_http,
                         SimpleHTTPResponse.from_json(self.jmsg_http))
        self.assertEqual(self.msg_https,
                         SimpleHTTPResponse.from_json(self.jmsg_https))

    def test_from_json_hashable(self):
        from acme.challenges import SimpleHTTPResponse
        hash(SimpleHTTPResponse.from_json(self.jmsg_http))
        hash(SimpleHTTPResponse.from_json(self.jmsg_https))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_good_token(self, mock_get):
        for resp in self.resp_http, self.resp_https:
            mock_get.reset_mock()
            mock_get.return_value = mock.MagicMock(text=self.chall.token,
                                                   headers=self.good_headers)
            self.assertTrue(resp.simple_verify(self.chall, "local"))
            mock_get.assert_called_once_with(resp.uri("local"), verify=False)

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_bad_token(self, mock_get):
        mock_get.return_value = mock.MagicMock(text=self.chall.token + "!",
                                               headers=self.good_headers)
        self.assertFalse(self.resp_http.simple_verify(self.chall, "local"))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_bad_content_type(self, mock_get):
        mock_get().text = self.chall.token
        self.assertFalse(self.resp_http.simple_verify(self.chall, "local"))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_connection_error(self, mock_get):
        mock_get.side_effect = requests.exceptions.RequestException
        self.assertFalse(self.resp_http.simple_verify(self.chall, "local"))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_port(self, mock_get):
        self.resp_http.simple_verify(self.chall, "local", 4430)
        self.assertEqual(
            "local:4430",
            urllib_parse.urlparse(mock_get.mock_calls[0][1][0]).netloc)
 def test_from_json_hashable(self):
     from acme.challenges import SimpleHTTPResponse
     hash(SimpleHTTPResponse.from_json(self.jmsg_http))
     hash(SimpleHTTPResponse.from_json(self.jmsg_https))
class SimpleHTTPResponseTest(unittest.TestCase):
    # pylint: disable=too-many-instance-attributes

    def setUp(self):
        from acme.challenges import SimpleHTTPResponse
        self.msg_http = SimpleHTTPResponse(tls=False)
        self.msg_https = SimpleHTTPResponse(tls=True)
        self.jmsg_http = {
            'resource': 'challenge',
            'type': 'simpleHttp',
            'tls': False,
        }
        self.jmsg_https = {
            'resource': 'challenge',
            'type': 'simpleHttp',
            'tls': True,
        }

        from acme.challenges import SimpleHTTP
        self.chall = SimpleHTTP(token=(b"x" * 16))
        self.resp_http = SimpleHTTPResponse(tls=False)
        self.resp_https = SimpleHTTPResponse(tls=True)
        self.good_headers = {'Content-Type': SimpleHTTPResponse.CONTENT_TYPE}

    def test_to_partial_json(self):
        self.assertEqual(self.jmsg_http, self.msg_http.to_partial_json())
        self.assertEqual(self.jmsg_https, self.msg_https.to_partial_json())

    def test_from_json(self):
        from acme.challenges import SimpleHTTPResponse
        self.assertEqual(
            self.msg_http, SimpleHTTPResponse.from_json(self.jmsg_http))
        self.assertEqual(
            self.msg_https, SimpleHTTPResponse.from_json(self.jmsg_https))

    def test_from_json_hashable(self):
        from acme.challenges import SimpleHTTPResponse
        hash(SimpleHTTPResponse.from_json(self.jmsg_http))
        hash(SimpleHTTPResponse.from_json(self.jmsg_https))

    def test_scheme(self):
        self.assertEqual('http', self.msg_http.scheme)
        self.assertEqual('https', self.msg_https.scheme)

    def test_port(self):
        self.assertEqual(80, self.msg_http.port)
        self.assertEqual(443, self.msg_https.port)

    def test_uri(self):
        self.assertEqual(
            'http://example.com/.well-known/acme-challenge/'
            'eHh4eHh4eHh4eHh4eHh4eA', self.msg_http.uri(
                'example.com', self.chall))
        self.assertEqual(
            'https://example.com/.well-known/acme-challenge/'
            'eHh4eHh4eHh4eHh4eHh4eA', self.msg_https.uri(
                'example.com', self.chall))

    def test_gen_check_validation(self):
        account_key = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem'))
        self.assertTrue(self.resp_http.check_validation(
            validation=self.resp_http.gen_validation(self.chall, account_key),
            chall=self.chall, account_public_key=account_key.public_key()))

    def test_gen_check_validation_wrong_key(self):
        key1 = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem'))
        key2 = jose.JWKRSA.load(test_util.load_vector('rsa1024_key.pem'))
        self.assertFalse(self.resp_http.check_validation(
            validation=self.resp_http.gen_validation(self.chall, key1),
            chall=self.chall, account_public_key=key2.public_key()))

    def test_check_validation_wrong_payload(self):
        account_key = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem'))
        validations = tuple(
            jose.JWS.sign(payload=payload, alg=jose.RS256, key=account_key)
            for payload in (b'', b'{}', self.chall.json_dumps().encode('utf-8'),
                            self.resp_http.json_dumps().encode('utf-8'))
        )
        for validation in validations:
            self.assertFalse(self.resp_http.check_validation(
                validation=validation, chall=self.chall,
                account_public_key=account_key.public_key()))

    def test_check_validation_wrong_fields(self):
        resource = self.resp_http.gen_resource(self.chall)
        account_key = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem'))
        validations = tuple(
            jose.JWS.sign(payload=bad_resource.json_dumps().encode('utf-8'),
                          alg=jose.RS256, key=account_key)
            for bad_resource in (resource.update(tls=True),
                                 resource.update(token=(b'x' * 20)))
        )
        for validation in validations:
            self.assertFalse(self.resp_http.check_validation(
                validation=validation, chall=self.chall,
                account_public_key=account_key.public_key()))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_good_validation(self, mock_get):
        account_key = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem'))
        for resp in self.resp_http, self.resp_https:
            mock_get.reset_mock()
            validation = resp.gen_validation(self.chall, account_key)
            mock_get.return_value = mock.MagicMock(
                text=validation.json_dumps(), headers=self.good_headers)
            self.assertTrue(resp.simple_verify(self.chall, "local", None))
            mock_get.assert_called_once_with(resp.uri(
                "local", self.chall), verify=False)

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_bad_validation(self, mock_get):
        mock_get.return_value = mock.MagicMock(
            text="!", headers=self.good_headers)
        self.assertFalse(self.resp_http.simple_verify(
            self.chall, "local", None))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_bad_content_type(self, mock_get):
        mock_get().text = self.chall.token
        self.assertFalse(self.resp_http.simple_verify(
            self.chall, "local", None))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_connection_error(self, mock_get):
        mock_get.side_effect = requests.exceptions.RequestException
        self.assertFalse(self.resp_http.simple_verify(
            self.chall, "local", None))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_port(self, mock_get):
        self.resp_http.simple_verify(
            self.chall, domain="local", account_public_key=None, port=4430)
        self.assertEqual("local:4430", urllib_parse.urlparse(
            mock_get.mock_calls[0][1][0]).netloc)
Exemple #20
0
class SimpleHTTPResponseTest(unittest.TestCase):
    # pylint: disable=too-many-instance-attributes

    def setUp(self):
        from acme.challenges import SimpleHTTPResponse
        self.msg_http = SimpleHTTPResponse(tls=False)
        self.msg_https = SimpleHTTPResponse(tls=True)
        self.jmsg_http = {
            'resource': 'challenge',
            'type': 'simpleHttp',
            'tls': False,
        }
        self.jmsg_https = {
            'resource': 'challenge',
            'type': 'simpleHttp',
            'tls': True,
        }

        from acme.challenges import SimpleHTTP
        self.chall = SimpleHTTP(token=(b"x" * 16))
        self.resp_http = SimpleHTTPResponse(tls=False)
        self.resp_https = SimpleHTTPResponse(tls=True)
        self.good_headers = {'Content-Type': SimpleHTTPResponse.CONTENT_TYPE}

    def test_to_partial_json(self):
        self.assertEqual(self.jmsg_http, self.msg_http.to_partial_json())
        self.assertEqual(self.jmsg_https, self.msg_https.to_partial_json())

    def test_from_json(self):
        from acme.challenges import SimpleHTTPResponse
        self.assertEqual(self.msg_http,
                         SimpleHTTPResponse.from_json(self.jmsg_http))
        self.assertEqual(self.msg_https,
                         SimpleHTTPResponse.from_json(self.jmsg_https))

    def test_from_json_hashable(self):
        from acme.challenges import SimpleHTTPResponse
        hash(SimpleHTTPResponse.from_json(self.jmsg_http))
        hash(SimpleHTTPResponse.from_json(self.jmsg_https))

    def test_scheme(self):
        self.assertEqual('http', self.msg_http.scheme)
        self.assertEqual('https', self.msg_https.scheme)

    def test_port(self):
        self.assertEqual(80, self.msg_http.port)
        self.assertEqual(443, self.msg_https.port)

    def test_uri(self):
        self.assertEqual(
            'http://example.com/.well-known/acme-challenge/'
            'eHh4eHh4eHh4eHh4eHh4eA',
            self.msg_http.uri('example.com', self.chall))
        self.assertEqual(
            'https://example.com/.well-known/acme-challenge/'
            'eHh4eHh4eHh4eHh4eHh4eA',
            self.msg_https.uri('example.com', self.chall))

    def test_gen_check_validation(self):
        account_key = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem'))
        self.assertTrue(
            self.resp_http.check_validation(
                validation=self.resp_http.gen_validation(
                    self.chall, account_key),
                chall=self.chall,
                account_public_key=account_key.public_key()))

    def test_gen_check_validation_wrong_key(self):
        key1 = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem'))
        key2 = jose.JWKRSA.load(test_util.load_vector('rsa1024_key.pem'))
        self.assertFalse(
            self.resp_http.check_validation(
                validation=self.resp_http.gen_validation(self.chall, key1),
                chall=self.chall,
                account_public_key=key2.public_key()))

    def test_check_validation_wrong_payload(self):
        account_key = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem'))
        validations = tuple(
            jose.JWS.sign(payload=payload, alg=jose.RS256, key=account_key)
            for payload in (b'', b'{}',
                            self.chall.json_dumps().encode('utf-8'),
                            self.resp_http.json_dumps().encode('utf-8')))
        for validation in validations:
            self.assertFalse(
                self.resp_http.check_validation(
                    validation=validation,
                    chall=self.chall,
                    account_public_key=account_key.public_key()))

    def test_check_validation_wrong_fields(self):
        resource = self.resp_http.gen_resource(self.chall)
        account_key = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem'))
        validations = tuple(
            jose.JWS.sign(payload=bad_resource.json_dumps().encode('utf-8'),
                          alg=jose.RS256,
                          key=account_key)
            for bad_resource in (resource.update(tls=True),
                                 resource.update(token=(b'x' * 20))))
        for validation in validations:
            self.assertFalse(
                self.resp_http.check_validation(
                    validation=validation,
                    chall=self.chall,
                    account_public_key=account_key.public_key()))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_good_validation(self, mock_get):
        account_key = jose.JWKRSA.load(test_util.load_vector('rsa512_key.pem'))
        for resp in self.resp_http, self.resp_https:
            mock_get.reset_mock()
            validation = resp.gen_validation(self.chall, account_key)
            mock_get.return_value = mock.MagicMock(
                text=validation.json_dumps(), headers=self.good_headers)
            self.assertTrue(resp.simple_verify(self.chall, "local", None))
            mock_get.assert_called_once_with(resp.uri("local", self.chall),
                                             verify=False)

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_bad_validation(self, mock_get):
        mock_get.return_value = mock.MagicMock(text="!",
                                               headers=self.good_headers)
        self.assertFalse(
            self.resp_http.simple_verify(self.chall, "local", None))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_bad_content_type(self, mock_get):
        mock_get().text = self.chall.token
        self.assertFalse(
            self.resp_http.simple_verify(self.chall, "local", None))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_connection_error(self, mock_get):
        mock_get.side_effect = requests.exceptions.RequestException
        self.assertFalse(
            self.resp_http.simple_verify(self.chall, "local", None))

    @mock.patch("acme.challenges.requests.get")
    def test_simple_verify_port(self, mock_get):
        self.resp_http.simple_verify(self.chall,
                                     domain="local",
                                     account_public_key=None,
                                     port=4430)
        self.assertEqual(
            "local:4430",
            urllib_parse.urlparse(mock_get.mock_calls[0][1][0]).netloc)