aboutsummaryrefslogtreecommitdiff
path: root/tests/test__pycrypto_crypt.py
blob: 2f452912b91758ed666929b187daca992be93d2c (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Unit tests for oauth2client._pycrypto_crypt."""

import os

import unittest2

from oauth2client import crypt


class TestPyCryptoVerifier(unittest2.TestCase):

    PUBLIC_CERT_FILENAME = os.path.join(os.path.dirname(__file__),
                                        'data', 'public_cert.pem')
    PRIVATE_KEY_FILENAME = os.path.join(os.path.dirname(__file__),
                                        'data', 'privatekey.pem')

    def _load_public_cert_bytes(self):
        with open(self.PUBLIC_CERT_FILENAME, 'rb') as fh:
            return fh.read()

    def _load_private_key_bytes(self):
        with open(self.PRIVATE_KEY_FILENAME, 'rb') as fh:
            return fh.read()

    def test_verify_success(self):
        to_sign = b'foo'
        signer = crypt.PyCryptoSigner.from_string(
            self._load_private_key_bytes())
        actual_signature = signer.sign(to_sign)

        verifier = crypt.PyCryptoVerifier.from_string(
            self._load_public_cert_bytes(), is_x509_cert=True)
        self.assertTrue(verifier.verify(to_sign, actual_signature))

    def test_verify_failure(self):
        verifier = crypt.PyCryptoVerifier.from_string(
            self._load_public_cert_bytes(), is_x509_cert=True)
        bad_signature = b''
        self.assertFalse(verifier.verify(b'foo', bad_signature))

    def test_verify_bad_key(self):
        verifier = crypt.PyCryptoVerifier.from_string(
            self._load_public_cert_bytes(), is_x509_cert=True)
        bad_signature = b''
        self.assertFalse(verifier.verify(b'foo', bad_signature))

    def test_from_string_unicode_key(self):
        public_key = self._load_public_cert_bytes()
        public_key = public_key.decode('utf-8')
        verifier = crypt.PyCryptoVerifier.from_string(
            public_key, is_x509_cert=True)
        self.assertIsInstance(verifier, crypt.PyCryptoVerifier)


class TestPyCryptoSigner(unittest2.TestCase):

    def test_from_string_bad_key(self):
        key_bytes = 'definitely-not-pem-format'
        with self.assertRaises(NotImplementedError):
            crypt.PyCryptoSigner.from_string(key_bytes)