Пример #1
0
def LicenseData_invalid_timestamps():
    """Test LicenseData() for invalid timestamps"""
    with assert_exception(ValueError):
        LicenseData('invalid', '2014-01-01T00:00:01', '2014-01-01T00:00:03')
    with assert_exception(ValueError):
        LicenseData('2014-01-01T00:00:00', 'invalid', '2014-01-01T00:00:03')
    with assert_exception(ValueError):
        LicenseData('2014-01-01T00:00:00', '2014-01-01T00:00:01', 'invalid')
Пример #2
0
 def test_invalid_names(self):
     """Test LicenseData() for invalid names"""
     with self.assertRaises(ValueError):
         LicenseData('2014-01-01T00:00:00',
                     '2014-01-01T00:00:01',
                     issuer='invalid')
     with self.assertRaises(ValueError):
         LicenseData('2014-01-01T00:00:00',
                     '2014-01-01T00:00:01',
                     holder='invalid')
Пример #3
0
def LicenseData_invalid_names():
    """Test LicenseData() for invalid names"""
    with assert_exception(ValueError):
        license = LicenseData('2014-01-01T00:00:00',
                              '2014-01-01T00:00:01',
                              issuer='invalid')
    with assert_exception(ValueError):
        license = LicenseData('2014-01-01T00:00:00',
                              '2014-01-01T00:00:01',
                              holder='invalid')
Пример #4
0
 def test_invalid_timestamps(self):
     """Test LicenseData() for invalid timestamps"""
     with self.assertRaises(ValueError):
         LicenseData('invalid', '2014-01-01T00:00:01',
                     '2014-01-01T00:00:03')
     with self.assertRaises(ValueError):
         LicenseData('2014-01-01T00:00:00', 'invalid',
                     '2014-01-01T00:00:03')
     with self.assertRaises(ValueError):
         LicenseData('2014-01-01T00:00:00', '2014-01-01T00:00:01',
                     'invalid')
Пример #5
0
 def test_signature_algorithm_valid_non_default_signature_algo(self):
     """Tests License() for valid, non-default signature_algorithm"""
     License(to_document(
         serialize(LicenseData('2014-01-01T00:00:00',
                               '2014-01-01T00:00:01'))),
             '<signature>',
             signature_algorithm='HELLOwithWORLD')
Пример #6
0
    def IssueLicense(self,
                     key_folder,
                     licence_input,
                     password='******',
                     licence_file='license.key',
                     validate_days=365):
        with open(os.path.join(key_folder, 'certificate.pem'), 'rb') as f:
            certificate = f.read()

        # Load the private key
        with open(os.path.join(key_folder, 'key.pem'), 'rb') as f:
            key = serialization.load_pem_private_key(
                f.read(),
                password=str.encode(password, encoding='UTF-8'),
                backend=backends.default_backend())

        # Issue the license
        license = License.issue(
            certificate,
            key,
            license_data=LicenseData(
                datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S'),
                (datetime.datetime.now() + datetime.timedelta(
                    days=validate_days)).strftime('%Y-%m-%dT%H:%M:%S')))

        # Store the license
        with open(licence_file, 'wb') as f:
            license.store(f, str.encode(licence_input, encoding='UTF-8'))
Пример #7
0
    def test_issue_valid(self):
        """Tests that the signature is correctly constructed"""
        # Generated with command below:
        '''
        python -c 'import tests.license_test; \
            open("key.pem", "w").write(tests.license_test.KEY)'

        python -c 'import sys, truepy; sys.stdout.write( \
            truepy._bean.to_document( \
                truepy._bean.serialize( \
                    truepy.LicenseData( \
                        not_before = "2014-01-01T00:00:00", \
                        not_after = "2014-01-01T00:00:01"))))' \
        | openssl sha -sign key.pem -sha1 \
        | base64
        '''
        expected = (
            'BwaJUYJTcY22EiC5x/qZQVMKGeIxAwTiIejRjrch2Q/uVoUrB1ptKRn1ffGgYs5zc'
            'agsj+7YTi8bB8nim+W+ANy93WttNrgz5hl2k75D4hGmR3EGV+f45l91RYMdTHukuK'
            'ZkA+agc/At5ByHC8Qaw4+4Jdz2e0XJMJaR3aEAYIJ/5NDVKSHD2OjpGDLEc70Qwdo'
            'rUU10B4X2URasRKHuRZTv9jVz2t4Mgk4wrJHiT/gw6sHbR1U7u7PbbnbQ8Xx/c37U'
            'L54jy9ZqM+j7yEhEXqPGW5rXvj0IQmYrODLdyVzNMUa/ReC66oERy2JZA0aaFyY8l'
            'Fr5V1yC1xT4r6yMnw==')

        self.assertEqual(
            expected,
            License.issue(
                CERTIFICATE,
                key(),
                license_data=LicenseData('2014-01-01T00:00:00',
                                         '2014-01-01T00:00:01')).signature)
Пример #8
0
 def test_encoded_valid(self):
     """Tests that License() with valid encoded data has correct encoded
     value"""
     License(
         to_document(
             serialize(
                 LicenseData('2014-01-01T00:00:00',
                             '2014-01-01T00:00:01'))), '<signature>')
Пример #9
0
 def test_verify_valid(self):
     """Tests that License.verify does not raise exception for valid
     certificate"""
     License.issue(CERTIFICATE,
                   key(),
                   license_data=LicenseData(
                       '2014-01-01T00:00:00',
                       '2014-01-01T00:00:01')).verify(CERTIFICATE)
Пример #10
0
 def test_verify_invalid(self):
     """Tests that License.verify raises exception for invalid signatures"""
     with self.assertRaises(License.InvalidSignatureException):
         License.issue(CERTIFICATE,
                       key(),
                       license_data=LicenseData(
                           '2014-01-01T00:00:00',
                           '2014-01-01T00:00:01')).verify(OTHER_CERTIFICATE)
Пример #11
0
def License_verify_valid():
    """Tests that License.verify does not raise exception for valid
    certificate"""
    License.issue(certificate(),
                  key(),
                  license_data=LicenseData('2014-01-01T00:00:00',
                                           '2014-01-01T00:00:01')).verify(
                                               certificate())
Пример #12
0
def License_verify_invalid():
    """Tests that License.verify raises exception for invalid signatures"""
    with assert_exception(License.InvalidSignatureException):
        License.issue(certificate(),
                      key(),
                      license_data=LicenseData('2014-01-01T00:00:00',
                                               '2014-01-01T00:00:01')).verify(
                                                   other_certificate())
Пример #13
0
def LicenseData_int_subject():
    """Test LicenseData() for string subject"""
    expected = '42'

    license = LicenseData('2014-01-01T00:00:00',
                          '2014-01-01T00:00:01',
                          subject=int(expected))
    assert_eq(expected, license.subject)
Пример #14
0
    def test_string_subject(self):
        """Test LicenseData() for string subject"""
        expected = 'subject'

        license = LicenseData('2014-01-01T00:00:00',
                              '2014-01-01T00:00:01',
                              subject=expected)
        self.assertEqual(expected, license.subject)
Пример #15
0
def License_signature_algorithm0():
    """Tests License() for invalid signature_algorithm"""
    with assert_exception(ValueError):
        License(to_document(
            serialize(LicenseData('2014-01-01T00:00:00',
                                  '2014-01-01T00:00:01'))),
                '<signature>',
                signature_algorithm='invalid')
Пример #16
0
    def test_string_extra(self):
        """Test LicenseData() for string extra data"""
        expected = 'hello world'

        license = LicenseData('2014-01-01T00:00:00',
                              '2014-01-01T00:00:01',
                              extra=expected)
        self.assertEqual(expected, license.extra)
Пример #17
0
    def test_int_consumer_type(self):
        """Test LicenseData() for string consumer_type"""
        expected = '42'

        license = LicenseData('2014-01-01T00:00:00',
                              '2014-01-01T00:00:01',
                              consumer_type=int(expected))
        self.assertEqual(expected, license.consumer_type)
Пример #18
0
    def test_int_info(self):
        """Test LicenseData() for string info"""
        expected = '42'

        license = LicenseData('2014-01-01T00:00:00',
                              '2014-01-01T00:00:01',
                              info=int(expected))
        self.assertEqual(expected, license.info)
Пример #19
0
def LicenseData_string_extra():
    """Test LicenseData() for string extra data"""
    expected = 'hello world'

    license = LicenseData('2014-01-01T00:00:00',
                          '2014-01-01T00:00:01',
                          extra=expected)
    assert_eq(expected, license.extra)
Пример #20
0
def LicenseData_int_info():
    """Test LicenseData() for string info"""
    expected = '42'

    license = LicenseData('2014-01-01T00:00:00',
                          '2014-01-01T00:00:01',
                          info=int(expected))
    assert_eq(expected, license.info)
Пример #21
0
def LicenseData_int_consumer_type():
    """Test LicenseData() for string consumer_type"""
    expected = '42'

    license = LicenseData('2014-01-01T00:00:00',
                          '2014-01-01T00:00:01',
                          consumer_type=int(expected))
    assert_eq(expected, license.consumer_type)
Пример #22
0
def License_signature_encoding0():
    """Tests License() for invalid signature_encoding"""
    with assert_exception(ValueError):
        License(to_document(
            serialize(LicenseData('2014-01-01T00:00:00',
                                  '2014-01-01T00:00:01'))),
                '<signature>',
                signature_encoding='UTF-8/Base64')
Пример #23
0
def License_issue_invalid_license_data():
    """Tests that License.issue with invalid license_data fails"""
    with assert_exception(ValueError):
        License.issue(certificate(), key(), license_data=None)
    with assert_exception(ValueError):
        License.issue(certificate(),
                      key(),
                      license_data=LicenseData('2014-01-01T00:00:00',
                                               '2014-01-01T00:00:01'),
                      not_before='2014-01-01T00:00:00')
    with assert_exception(ValueError):
        License.issue(certificate(),
                      key(),
                      license_data=LicenseData('2014-01-01T00:00:00',
                                               '2014-01-01T00:00:01'),
                      issuer='CN=must not be passed')
    with assert_exception(ValueError):
        License.issue(certificate(), key(), not_before='2014-01-01T00:00:00')
Пример #24
0
def License_store():
    """Tests that a license can be loaded from the stored data"""
    f = io.BytesIO()
    License.issue(certificate(),
                  key(),
                  license_data=LicenseData('2014-01-01T00:00:00',
                                           '2014-01-01T00:00:01')).store(
                                               f, b'valid password')
    License.load(io.BytesIO(f.getvalue()), b'valid password')
Пример #25
0
    def test_list_extra(self):
        """Test LicenseData() for list extra data"""
        extra = [1, 2, True, 'end']
        expected = '[1, 2, true, "end"]'

        license = LicenseData('2014-01-01T00:00:00',
                              '2014-01-01T00:00:01',
                              extra=extra)
        self.assertEqual(expected, license.extra)
Пример #26
0
 def test_store(self):
     """Tests that a license can be loaded from the stored data"""
     f = io.BytesIO()
     License.issue(CERTIFICATE,
                   key(),
                   license_data=LicenseData('2014-01-01T00:00:00',
                                            '2014-01-01T00:00:01')).store(
                                                f, b'valid password')
     License.load(io.BytesIO(f.getvalue()), b'valid password')
Пример #27
0
 def test_signature_algorithm_invalid(self):
     """Tests License() for invalid signature_algorithm"""
     with self.assertRaises(ValueError):
         License(to_document(
             serialize(
                 LicenseData('2014-01-01T00:00:00',
                             '2014-01-01T00:00:01'))),
                 '<signature>',
                 signature_algorithm='invalid')
Пример #28
0
 def test_signature_encoding_invalid(self):
     """Tests License() for invalid signature_encoding"""
     with self.assertRaises(ValueError):
         License(to_document(
             serialize(
                 LicenseData('2014-01-01T00:00:00',
                             '2014-01-01T00:00:01'))),
                 '<signature>',
                 signature_encoding='UTF-8/Base64')
Пример #29
0
def LicenseData_list_extra():
    """Test LicenseData() for list extra data"""
    extra = [1, 2, True, 'end']
    expected = '[1, 2, true, "end"]'

    license = LicenseData('2014-01-01T00:00:00',
                          '2014-01-01T00:00:01',
                          extra=extra)
    assert_eq(expected, license.extra)
Пример #30
0
 def test_issue_invalid_license_data(self):
     """Tests that License.issue with invalid license_data fails"""
     with self.assertRaises(ValueError):
         License.issue(CERTIFICATE, key(), license_data=None)
     with self.assertRaises(ValueError):
         License.issue(CERTIFICATE,
                       key(),
                       license_data=LicenseData('2014-01-01T00:00:00',
                                                '2014-01-01T00:00:01'),
                       not_before='2014-01-01T00:00:00')
     with self.assertRaises(ValueError):
         License.issue(CERTIFICATE,
                       key(),
                       license_data=LicenseData('2014-01-01T00:00:00',
                                                '2014-01-01T00:00:01'),
                       issuer='CN=must not be passed')
     with self.assertRaises(ValueError):
         License.issue(CERTIFICATE, key(), not_before='2014-01-01T00:00:00')