huaweicloud-encryption-sdk-python 1.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- examples/file_encryption_example.py +57 -0
- examples/key_capsule_encryption_example.py +240 -0
- examples/kms_discovery_encryption_example.py +56 -0
- examples/local_aes_encryption_example.py +59 -0
- examples/test_rsa.py +42 -0
- examples/test_sm2.py +38 -0
- examples/test_sm4_cbc.py +37 -0
- examples/test_sm4_gcm.py +37 -0
- huaweicloud_encryption_sdk_python-1.2.1.dist-info/METADATA +18 -0
- huaweicloud_encryption_sdk_python-1.2.1.dist-info/RECORD +174 -0
- huaweicloud_encryption_sdk_python-1.2.1.dist-info/WHEEL +5 -0
- huaweicloud_encryption_sdk_python-1.2.1.dist-info/top_level.txt +3 -0
- huaweicloud_encryptionsdk/__init__.py +5 -0
- huaweicloud_encryptionsdk/_apig_sdk/__init__.py +1 -0
- huaweicloud_encryptionsdk/_apig_sdk/signer.py +278 -0
- huaweicloud_encryptionsdk/_apig_sdk/signer_v11.py +70 -0
- huaweicloud_encryptionsdk/_apig_sdk/sm3_hash.py +141 -0
- huaweicloud_encryptionsdk/_lts_sdk/auth/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/auth/signer.py +174 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/client/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/client/consumer_client_adapter.py +59 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/client/http_client.py +211 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/core/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/core/client_consumer_worker.py +106 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/core/consumer_heartbeat.py +73 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/core/log_shard_consumer.py +179 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/interface/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/interface/consumer_check_point_tracker.py +120 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/interface/consumer_processor.py +18 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/interface/consumer_processor_factory.py +3 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/model/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/model/check_point.py +20 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/model/consumer_config.py +27 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/model/log_data.py +18 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/model/task_result.py +40 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/task/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/task/consumer_fetch_task.py +53 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/task/initial_task.py +55 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/task/process_task.py +25 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer/task/shutdown_task.py +24 -0
- huaweicloud_encryptionsdk/_lts_sdk/consumer_main.py +88 -0
- huaweicloud_encryptionsdk/_lts_sdk/parallel_main.py +64 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/callback/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/callback/call_back.py +7 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/core/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/core/accumulator.py +135 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/core/io_thread_pool.py +107 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/core/io_worker.py +124 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/core/mover.py +69 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/core/producer.py +98 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/lts_http/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/lts_http/http_client.py +63 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/lts_http/simple_request.py +14 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/config.py +50 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/error.py +18 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/log.py +87 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/log_batch.py +154 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/lts_store.py +21 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/result.py +17 -0
- huaweicloud_encryptionsdk/_lts_sdk/producer/model/retry_queue.py +39 -0
- huaweicloud_encryptionsdk/_lts_sdk/serial_main.py +77 -0
- huaweicloud_encryptionsdk/_lts_sdk/utils/__init__.py +0 -0
- huaweicloud_encryptionsdk/_lts_sdk/utils/common.py +57 -0
- huaweicloud_encryptionsdk/_lts_sdk/utils/lts_threading.py +32 -0
- huaweicloud_encryptionsdk/cache/__init__.py +0 -0
- huaweicloud_encryptionsdk/cache/data_key_cache.py +79 -0
- huaweicloud_encryptionsdk/cache/local_data_key_cache.py +161 -0
- huaweicloud_encryptionsdk/common/__init__.py +0 -0
- huaweicloud_encryptionsdk/common/constants.py +15 -0
- huaweicloud_encryptionsdk/common/counting_output_stream.py +18 -0
- huaweicloud_encryptionsdk/common/crypto_factory.py +48 -0
- huaweicloud_encryptionsdk/common/ec_private_key_loader.py +43 -0
- huaweicloud_encryptionsdk/common/ecs_metadata_service.py +42 -0
- huaweicloud_encryptionsdk/common/file_path_for_example_constants.py +4 -0
- huaweicloud_encryptionsdk/common/utils.py +162 -0
- huaweicloud_encryptionsdk/exception/__init__.py +0 -0
- huaweicloud_encryptionsdk/exception/errors.py +109 -0
- huaweicloud_encryptionsdk/handler/__init__.py +0 -0
- huaweicloud_encryptionsdk/handler/cipher_handler.py +294 -0
- huaweicloud_encryptionsdk/handler/crypto_handler.py +24 -0
- huaweicloud_encryptionsdk/handler/default_crypto_handler.py +148 -0
- huaweicloud_encryptionsdk/handler/default_serialize_handler.py +45 -0
- huaweicloud_encryptionsdk/handler/file_decrypt_handler.py +101 -0
- huaweicloud_encryptionsdk/handler/file_encrypt_handler.py +30 -0
- huaweicloud_encryptionsdk/handler/serialize_handler.py +14 -0
- huaweicloud_encryptionsdk/huawei_config.py +49 -0
- huaweicloud_encryptionsdk/huawei_crypto.py +311 -0
- huaweicloud_encryptionsdk/keyrings/__init__.py +0 -0
- huaweicloud_encryptionsdk/keyrings/keyring.py +14 -0
- huaweicloud_encryptionsdk/keyrings/keyring_factory.py +10 -0
- huaweicloud_encryptionsdk/keyrings/kms_keyring_factory.py +21 -0
- huaweicloud_encryptionsdk/keyrings/kmskeyring/__init__.py +0 -0
- huaweicloud_encryptionsdk/keyrings/kmskeyring/kms_discovery_keyring.py +51 -0
- huaweicloud_encryptionsdk/keyrings/kmskeyring/kms_key_capsule_keyring.py +100 -0
- huaweicloud_encryptionsdk/keyrings/kmskeyring/kms_keyring.py +155 -0
- huaweicloud_encryptionsdk/keyrings/kmskeyring/kms_multi_region_keyring.py +8 -0
- huaweicloud_encryptionsdk/keyrings/raw_keyring_factory.py +27 -0
- huaweicloud_encryptionsdk/keyrings/rawkeyring/__init__.py +0 -0
- huaweicloud_encryptionsdk/keyrings/rawkeyring/raw_aes_keyring.py +63 -0
- huaweicloud_encryptionsdk/keyrings/rawkeyring/raw_keyring.py +112 -0
- huaweicloud_encryptionsdk/keyrings/rawkeyring/raw_rsa_keyring.py +55 -0
- huaweicloud_encryptionsdk/keyrings/rawkeyring/raw_sm2_keyring.py +75 -0
- huaweicloud_encryptionsdk/keyrings/rawkeyring/raw_sm4_cbc_keyring.py +66 -0
- huaweicloud_encryptionsdk/keyrings/rawkeyring/raw_sm4_keyring.py +70 -0
- huaweicloud_encryptionsdk/kms/__init__.py +0 -0
- huaweicloud_encryptionsdk/kms/create_key_capsule_request.py +35 -0
- huaweicloud_encryptionsdk/kms/create_key_capsule_response.py +19 -0
- huaweicloud_encryptionsdk/kms/decrypt_key_capsule_request.py +32 -0
- huaweicloud_encryptionsdk/kms/decrypt_key_capsule_response.py +19 -0
- huaweicloud_encryptionsdk/kms/kms_http_client.py +116 -0
- huaweicloud_encryptionsdk/log/__init__.py +0 -0
- huaweicloud_encryptionsdk/log/log_config.py +30 -0
- huaweicloud_encryptionsdk/log/log_file_backup.py +144 -0
- huaweicloud_encryptionsdk/log/log_metrics.py +132 -0
- huaweicloud_encryptionsdk/log/log_reporter_manager.py +163 -0
- huaweicloud_encryptionsdk/log/lts_sdk_producer.py +386 -0
- huaweicloud_encryptionsdk/log/lts_struct_log_builder.py +106 -0
- huaweicloud_encryptionsdk/meterialmanager/__init__.py +0 -0
- huaweicloud_encryptionsdk/meterialmanager/cache_crypto_meterial_manager.py +221 -0
- huaweicloud_encryptionsdk/meterialmanager/crypto_meterial_manager.py +19 -0
- huaweicloud_encryptionsdk/meterialmanager/data_key_generate.py +10 -0
- huaweicloud_encryptionsdk/meterialmanager/data_key_generate_factory.py +20 -0
- huaweicloud_encryptionsdk/meterialmanager/default_crypto_meterials_manager.py +71 -0
- huaweicloud_encryptionsdk/meterialmanager/key_capsule_data_key_generate.py +63 -0
- huaweicloud_encryptionsdk/meterialmanager/kms_data_key_generate.py +65 -0
- huaweicloud_encryptionsdk/meterialmanager/local_data_key_generate.py +13 -0
- huaweicloud_encryptionsdk/model/__init__.py +0 -0
- huaweicloud_encryptionsdk/model/access_point_config.py +9 -0
- huaweicloud_encryptionsdk/model/cipher_body.py +81 -0
- huaweicloud_encryptionsdk/model/cipher_header.py +405 -0
- huaweicloud_encryptionsdk/model/ciphertext_data_key.py +166 -0
- huaweicloud_encryptionsdk/model/crypto_result.py +11 -0
- huaweicloud_encryptionsdk/model/data_key_materials.py +17 -0
- huaweicloud_encryptionsdk/model/data_materials.py +11 -0
- huaweicloud_encryptionsdk/model/enums/__init__.py +0 -0
- huaweicloud_encryptionsdk/model/enums/access_point_type.py +28 -0
- huaweicloud_encryptionsdk/model/enums/crypto_algorithm.py +75 -0
- huaweicloud_encryptionsdk/model/enums/data_key_generate_type.py +20 -0
- huaweicloud_encryptionsdk/model/enums/key_provider_enum.py +19 -0
- huaweicloud_encryptionsdk/model/enums/keyring_type_enum.py +27 -0
- huaweicloud_encryptionsdk/model/key_capsule.py +10 -0
- huaweicloud_encryptionsdk/model/key_capsule_config.py +12 -0
- huaweicloud_encryptionsdk/model/kms_config.py +10 -0
- huaweicloud_encryptionsdk/model/request/__init__.py +0 -0
- huaweicloud_encryptionsdk/model/request/encrypt_request.py +10 -0
- huaweicloud_encryptionsdk/model/request/sign_request.py +2 -0
- huaweicloud_encryptionsdk/model/request/verify_request.py +2 -0
- huaweicloud_encryptionsdk/proof/__init__.py +0 -0
- huaweicloud_encryptionsdk/proof/attestation_document.py +36 -0
- huaweicloud_encryptionsdk/proof/collector/__init__.py +0 -0
- huaweicloud_encryptionsdk/proof/collector/cce_proof_collector.py +24 -0
- huaweicloud_encryptionsdk/proof/collector/ecs_proof_collector.py +11 -0
- huaweicloud_encryptionsdk/proof/collector/key_pair_proof_collector.py +55 -0
- huaweicloud_encryptionsdk/proof/collector/ma_proof_collector.py +51 -0
- huaweicloud_encryptionsdk/proof/private_key_decrypt_callback.py +7 -0
- huaweicloud_encryptionsdk/proof/proof_collector.py +11 -0
- huaweicloud_encryptionsdk/proof/proof_collector_factory.py +53 -0
- huaweicloud_encryptionsdk/proof/proof_info.py +10 -0
- huaweicloud_encryptionsdk/util/__init__.py +0 -0
- huaweicloud_encryptionsdk/util/common_utils.py +12 -0
- tests/__init__.py +0 -0
- tests/test_enums.py +84 -0
- tests/test_exceptions.py +38 -0
- tests/test_factories.py +71 -0
- tests/test_huawei_crypto.py +67 -0
- tests/test_imports.py +46 -0
- tests/test_kms_models.py +95 -0
- tests/test_log_file_backup.py +45 -0
- tests/test_lts_struct_log_builder.py +97 -0
- tests/test_models.py +142 -0
- tests/test_utils.py +111 -0
tests/test_factories.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from huaweicloud_encryptionsdk.keyrings.kms_keyring_factory import KmsKeyringFactory
|
|
3
|
+
from huaweicloud_encryptionsdk.keyrings.raw_keyring_factory import RawKeyringFactory
|
|
4
|
+
from huaweicloud_encryptionsdk.keyrings.rawkeyring.raw_aes_keyring import RawAesKeyring
|
|
5
|
+
from huaweicloud_encryptionsdk.keyrings.rawkeyring.raw_sm4_keyring import RawSM4Keyring
|
|
6
|
+
from huaweicloud_encryptionsdk.keyrings.rawkeyring.raw_sm4_cbc_keyring import RawSM4CBCKeyring
|
|
7
|
+
from huaweicloud_encryptionsdk.keyrings.rawkeyring.raw_rsa_keyring import RawRSAKeyring
|
|
8
|
+
from huaweicloud_encryptionsdk.keyrings.rawkeyring.raw_sm2_keyring import RawSM2Keyring
|
|
9
|
+
from huaweicloud_encryptionsdk.keyrings.kmskeyring.kms_multi_region_keyring import KMSMultiRegionKeyring
|
|
10
|
+
from huaweicloud_encryptionsdk.keyrings.kmskeyring.kms_discovery_keyring import KMSDiscoveryKeyring
|
|
11
|
+
from huaweicloud_encryptionsdk.keyrings.kmskeyring.kms_key_capsule_keyring import KMSKeyCapsuleKeyring
|
|
12
|
+
from huaweicloud_encryptionsdk.meterialmanager.data_key_generate_factory import DataKeyGenerateFactory
|
|
13
|
+
from huaweicloud_encryptionsdk.meterialmanager.local_data_key_generate import LocalDataKeyGenerate
|
|
14
|
+
from huaweicloud_encryptionsdk.meterialmanager.kms_data_key_generate import KmsDataKeyGenerate
|
|
15
|
+
from huaweicloud_encryptionsdk.meterialmanager.key_capsule_data_key_generate import KeyCapsuleDataKeyGenerate
|
|
16
|
+
from huaweicloud_encryptionsdk.proof.proof_collector_factory import ProofCollectorFactory
|
|
17
|
+
from huaweicloud_encryptionsdk.proof.proof_collector import ProofCollector
|
|
18
|
+
from huaweicloud_encryptionsdk.proof.proof_info import ProofInfo
|
|
19
|
+
from huaweicloud_encryptionsdk.proof.attestation_document import AttestationDocument
|
|
20
|
+
from huaweicloud_encryptionsdk.model.enums.keyring_type_enum import KeyringTypeEnum
|
|
21
|
+
from huaweicloud_encryptionsdk.model.enums.data_key_generate_type import DataKeyGenerateType
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class TestRawKeyringFactory:
|
|
25
|
+
def test_aes(self):
|
|
26
|
+
kr = RawKeyringFactory().get_keyring("AES")
|
|
27
|
+
assert isinstance(kr, RawAesKeyring)
|
|
28
|
+
|
|
29
|
+
def test_sm4(self):
|
|
30
|
+
kr = RawKeyringFactory().get_keyring("SM4_GCM")
|
|
31
|
+
assert isinstance(kr, RawSM4Keyring)
|
|
32
|
+
|
|
33
|
+
def test_sm4_cbc(self):
|
|
34
|
+
kr = RawKeyringFactory().get_keyring("SM4_CBC")
|
|
35
|
+
assert isinstance(kr, RawSM4CBCKeyring)
|
|
36
|
+
|
|
37
|
+
def test_rsa(self):
|
|
38
|
+
kr = RawKeyringFactory().get_keyring("RSA")
|
|
39
|
+
assert isinstance(kr, RawRSAKeyring)
|
|
40
|
+
|
|
41
|
+
def test_sm2(self):
|
|
42
|
+
kr = RawKeyringFactory().get_keyring("SM2")
|
|
43
|
+
assert isinstance(kr, RawSM2Keyring)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class TestKmsKeyringFactory:
|
|
47
|
+
def test_multi_region(self):
|
|
48
|
+
kr = KmsKeyringFactory().get_keyring("multi")
|
|
49
|
+
assert isinstance(kr, KMSMultiRegionKeyring)
|
|
50
|
+
|
|
51
|
+
def test_discovery(self):
|
|
52
|
+
kr = KmsKeyringFactory().get_keyring("discovery")
|
|
53
|
+
assert isinstance(kr, KMSDiscoveryKeyring)
|
|
54
|
+
|
|
55
|
+
def test_key_capsule(self):
|
|
56
|
+
kr = KmsKeyringFactory().get_keyring("key_capsule")
|
|
57
|
+
assert isinstance(kr, KMSKeyCapsuleKeyring)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class TestDataKeyGenerateFactory:
|
|
61
|
+
def test_local(self):
|
|
62
|
+
gen = DataKeyGenerateFactory.get_data_key_generate(DataKeyGenerateType.LOCAL_GENERATE)
|
|
63
|
+
assert isinstance(gen, LocalDataKeyGenerate)
|
|
64
|
+
|
|
65
|
+
def test_kms(self):
|
|
66
|
+
gen = DataKeyGenerateFactory.get_data_key_generate(DataKeyGenerateType.KMS_GENERATE)
|
|
67
|
+
assert isinstance(gen, KmsDataKeyGenerate)
|
|
68
|
+
|
|
69
|
+
def test_key_capsule(self):
|
|
70
|
+
gen = DataKeyGenerateFactory.get_data_key_generate(DataKeyGenerateType.KEY_CAPSULE_GENERATE)
|
|
71
|
+
assert isinstance(gen, KeyCapsuleDataKeyGenerate)
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from unittest.mock import patch, MagicMock
|
|
3
|
+
from huaweicloud_encryptionsdk import HuaweiCrypto, HuaweiConfig
|
|
4
|
+
from huaweicloud_encryptionsdk.exception.errors import KeyringNotFoundException
|
|
5
|
+
from huaweicloud_encryptionsdk.model.enums.crypto_algorithm import CryptoAlgorithm
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TestHuaweiCryptoNoKeyring:
|
|
9
|
+
def test_encrypt_without_keyring_raises(self):
|
|
10
|
+
config = HuaweiConfig(ak="ak", sk="sk", crypto_algorithm=CryptoAlgorithm.AES_256_GCM_NOPADDING)
|
|
11
|
+
crypto = HuaweiCrypto(config)
|
|
12
|
+
with pytest.raises(KeyringNotFoundException):
|
|
13
|
+
crypto.encrypt(b"test")
|
|
14
|
+
|
|
15
|
+
def test_decrypt_without_keyring_raises(self):
|
|
16
|
+
config = HuaweiConfig(ak="ak", sk="sk")
|
|
17
|
+
crypto = HuaweiCrypto(config)
|
|
18
|
+
with pytest.raises(KeyringNotFoundException):
|
|
19
|
+
crypto.decrypt(b"test")
|
|
20
|
+
|
|
21
|
+
def test_encrypt_stream_without_keyring_raises(self):
|
|
22
|
+
config = HuaweiConfig(ak="ak", sk="sk", crypto_algorithm=CryptoAlgorithm.AES_256_GCM_NOPADDING)
|
|
23
|
+
crypto = HuaweiCrypto(config)
|
|
24
|
+
with pytest.raises(KeyringNotFoundException):
|
|
25
|
+
crypto.encrypt_stream(MagicMock(), MagicMock())
|
|
26
|
+
|
|
27
|
+
def test_decrypt_stream_without_keyring_raises(self):
|
|
28
|
+
config = HuaweiConfig(ak="ak", sk="sk")
|
|
29
|
+
crypto = HuaweiCrypto(config)
|
|
30
|
+
with pytest.raises(KeyringNotFoundException):
|
|
31
|
+
crypto.decrypt_stream(MagicMock(), MagicMock())
|
|
32
|
+
|
|
33
|
+
def test_encrypt_file_without_keyring_raises(self):
|
|
34
|
+
config = HuaweiConfig(ak="ak", sk="sk", crypto_algorithm=CryptoAlgorithm.AES_256_GCM_NOPADDING)
|
|
35
|
+
crypto = HuaweiCrypto(config)
|
|
36
|
+
with pytest.raises(KeyringNotFoundException):
|
|
37
|
+
crypto.encrypt_file("src.txt", "dst.txt")
|
|
38
|
+
|
|
39
|
+
def test_decrypt_file_without_keyring_raises(self):
|
|
40
|
+
config = HuaweiConfig(ak="ak", sk="sk")
|
|
41
|
+
crypto = HuaweiCrypto(config)
|
|
42
|
+
with pytest.raises(KeyringNotFoundException):
|
|
43
|
+
crypto.decrypt_file("src.txt", "dst.txt")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class TestHuaweiCryptoBuilder:
|
|
47
|
+
def test_with_keyring(self):
|
|
48
|
+
config = HuaweiConfig(ak="ak", sk="sk")
|
|
49
|
+
crypto = HuaweiCrypto(config)
|
|
50
|
+
mock_keyring = MagicMock()
|
|
51
|
+
result = crypto.with_keyring(mock_keyring)
|
|
52
|
+
assert result is crypto
|
|
53
|
+
|
|
54
|
+
def test_with_crypto_material_manager(self):
|
|
55
|
+
config = HuaweiConfig(ak="ak", sk="sk")
|
|
56
|
+
crypto = HuaweiCrypto(config)
|
|
57
|
+
mock_manager = MagicMock()
|
|
58
|
+
result = crypto.with_crypto_material_manager(mock_manager)
|
|
59
|
+
assert result is crypto
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class TestHuaweiConfigLocalLogOnly:
|
|
63
|
+
def test_local_log_only_does_not_init_lts(self):
|
|
64
|
+
config = HuaweiConfig(ak="ak", sk="sk", local_log_only=True)
|
|
65
|
+
crypto = HuaweiCrypto(config)
|
|
66
|
+
from huaweicloud_encryptionsdk.log.log_reporter_manager import LogReporterManager
|
|
67
|
+
assert LogReporterManager.get_instance().is_initialized
|
tests/test_imports.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
def test_import_main():
|
|
2
|
+
from huaweicloud_encryptionsdk import HuaweiCrypto, HuaweiConfig
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def test_import_models():
|
|
6
|
+
from huaweicloud_encryptionsdk.model.crypto_result import CryptoResult
|
|
7
|
+
from huaweicloud_encryptionsdk.model.data_key_materials import DataKeyMaterials
|
|
8
|
+
from huaweicloud_encryptionsdk.model.cipher_header import CipherHeader
|
|
9
|
+
from huaweicloud_encryptionsdk.model.cipher_body import CipherBody
|
|
10
|
+
from huaweicloud_encryptionsdk.model.ciphertext_data_key import CiphertextDataKey
|
|
11
|
+
from huaweicloud_encryptionsdk.model.kms_config import KMSConfig
|
|
12
|
+
from huaweicloud_encryptionsdk.model.key_capsule import KeyCapsule
|
|
13
|
+
from huaweicloud_encryptionsdk.model.key_capsule_config import KeyCapsuleConfig
|
|
14
|
+
from huaweicloud_encryptionsdk.model.access_point_config import AccessPointConfig
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def test_import_enums():
|
|
18
|
+
from huaweicloud_encryptionsdk.model.enums.crypto_algorithm import CryptoAlgorithm
|
|
19
|
+
from huaweicloud_encryptionsdk.model.enums.keyring_type_enum import KeyringTypeEnum
|
|
20
|
+
from huaweicloud_encryptionsdk.model.enums.data_key_generate_type import DataKeyGenerateType
|
|
21
|
+
from huaweicloud_encryptionsdk.model.enums.key_provider_enum import KeyProviderEnum
|
|
22
|
+
from huaweicloud_encryptionsdk.model.enums.access_point_type import AccessPointType
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_import_exceptions():
|
|
26
|
+
from huaweicloud_encryptionsdk.exception.errors import (
|
|
27
|
+
HuaweicloudException,
|
|
28
|
+
KeyringNotFoundException,
|
|
29
|
+
KeyringNotMatchException,
|
|
30
|
+
KeyParseException,
|
|
31
|
+
EncryptException,
|
|
32
|
+
CipherTamperedException,
|
|
33
|
+
CipherDataException,
|
|
34
|
+
ErrorMessage,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def test_import_keyrings():
|
|
39
|
+
from huaweicloud_encryptionsdk.keyrings.keyring import Keyring
|
|
40
|
+
from huaweicloud_encryptionsdk.keyrings.raw_keyring_factory import RawKeyringFactory
|
|
41
|
+
from huaweicloud_encryptionsdk.keyrings.kms_keyring_factory import KmsKeyringFactory
|
|
42
|
+
from huaweicloud_encryptionsdk.keyrings.rawkeyring.raw_aes_keyring import RawAesKeyring
|
|
43
|
+
from huaweicloud_encryptionsdk.keyrings.rawkeyring.raw_rsa_keyring import RawRSAKeyring
|
|
44
|
+
from huaweicloud_encryptionsdk.keyrings.rawkeyring.raw_sm2_keyring import RawSM2Keyring
|
|
45
|
+
from huaweicloud_encryptionsdk.keyrings.rawkeyring.raw_sm4_keyring import RawSM4Keyring
|
|
46
|
+
from huaweicloud_encryptionsdk.keyrings.rawkeyring.raw_sm4_cbc_keyring import RawSM4CBCKeyring
|
tests/test_kms_models.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from huaweicloud_encryptionsdk.kms.create_key_capsule_request import CreateKeyCapsuleRequest
|
|
3
|
+
from huaweicloud_encryptionsdk.kms.create_key_capsule_response import CreateKeyCapsuleResponse
|
|
4
|
+
from huaweicloud_encryptionsdk.kms.decrypt_key_capsule_request import DecryptKeyCapsuleRequest
|
|
5
|
+
from huaweicloud_encryptionsdk.kms.decrypt_key_capsule_response import DecryptKeyCapsuleResponse
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TestCreateKeyCapsuleRequest:
|
|
9
|
+
def test_defaults(self):
|
|
10
|
+
r = CreateKeyCapsuleRequest()
|
|
11
|
+
assert r.key_id is None
|
|
12
|
+
assert r.datakey_length == 0
|
|
13
|
+
assert r.public_key is None
|
|
14
|
+
assert r.policy_id is None
|
|
15
|
+
assert r.key_policy is None
|
|
16
|
+
|
|
17
|
+
def test_to_dict(self):
|
|
18
|
+
r = CreateKeyCapsuleRequest(key_id="kid", datakey_length=256, public_key="pk", policy_id="pid")
|
|
19
|
+
d = r.to_dict()
|
|
20
|
+
assert d["key_id"] == "kid"
|
|
21
|
+
assert d["datakey_length"] == 256
|
|
22
|
+
assert d["public_key"] == "pk"
|
|
23
|
+
assert d["policy_id"] == "pid"
|
|
24
|
+
|
|
25
|
+
def test_from_dict(self):
|
|
26
|
+
d = {"key_id": "kid", "datakey_length": 128, "public_key": "pk", "policy_id": "pid", "key_policy": "kp"}
|
|
27
|
+
r = CreateKeyCapsuleRequest.from_dict(d)
|
|
28
|
+
assert r.key_id == "kid"
|
|
29
|
+
assert r.datakey_length == 128
|
|
30
|
+
assert r.key_policy == "kp"
|
|
31
|
+
|
|
32
|
+
def test_roundtrip(self):
|
|
33
|
+
r = CreateKeyCapsuleRequest(key_id="kid", datakey_length=256, public_key="pk")
|
|
34
|
+
d = r.to_dict()
|
|
35
|
+
r2 = CreateKeyCapsuleRequest.from_dict(d)
|
|
36
|
+
assert r2.key_id == r.key_id
|
|
37
|
+
assert r2.datakey_length == r.datakey_length
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class TestCreateKeyCapsuleResponse:
|
|
41
|
+
def test_defaults(self):
|
|
42
|
+
r = CreateKeyCapsuleResponse()
|
|
43
|
+
assert r.key_id is None
|
|
44
|
+
assert r.datakey_cipher is None
|
|
45
|
+
assert r.datakey is None
|
|
46
|
+
assert r.datakey_capsule is None
|
|
47
|
+
|
|
48
|
+
def test_from_dict(self):
|
|
49
|
+
d = {"key_id": "kid", "datakey_cipher": "cipher", "datakey": "dk", "datakey_capsule": "capsule"}
|
|
50
|
+
r = CreateKeyCapsuleResponse.from_dict(d)
|
|
51
|
+
assert r.key_id == "kid"
|
|
52
|
+
assert r.datakey_cipher == "cipher"
|
|
53
|
+
assert r.datakey == "dk"
|
|
54
|
+
assert r.datakey_capsule == "capsule"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class TestDecryptKeyCapsuleRequest:
|
|
58
|
+
def test_defaults(self):
|
|
59
|
+
r = DecryptKeyCapsuleRequest()
|
|
60
|
+
assert r.key_id is None
|
|
61
|
+
assert r.datakey_capsule is None
|
|
62
|
+
assert r.attestation_document is None
|
|
63
|
+
|
|
64
|
+
def test_to_dict(self):
|
|
65
|
+
r = DecryptKeyCapsuleRequest(key_id="kid", datakey_capsule="capsule")
|
|
66
|
+
d = r.to_dict()
|
|
67
|
+
assert d["key_id"] == "kid"
|
|
68
|
+
assert d["datakey_capsule"] == "capsule"
|
|
69
|
+
|
|
70
|
+
def test_from_dict(self):
|
|
71
|
+
d = {"key_id": "kid", "datakey_capsule": "capsule"}
|
|
72
|
+
r = DecryptKeyCapsuleRequest.from_dict(d)
|
|
73
|
+
assert r.key_id == "kid"
|
|
74
|
+
assert r.datakey_capsule == "capsule"
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class TestDecryptKeyCapsuleResponse:
|
|
78
|
+
def test_defaults(self):
|
|
79
|
+
r = DecryptKeyCapsuleResponse()
|
|
80
|
+
assert r.key_id is None
|
|
81
|
+
assert r.datakey is None
|
|
82
|
+
assert r.datakey_cipher is None
|
|
83
|
+
assert r.instance_id is None
|
|
84
|
+
|
|
85
|
+
def test_from_dict(self):
|
|
86
|
+
d = {"key_id": "kid", "datakey": "dk", "datakey_cipher": "cipher", "instanceId": "inst-123"}
|
|
87
|
+
r = DecryptKeyCapsuleResponse.from_dict(d)
|
|
88
|
+
assert r.key_id == "kid"
|
|
89
|
+
assert r.datakey == "dk"
|
|
90
|
+
assert r.instance_id == "inst-123"
|
|
91
|
+
|
|
92
|
+
def test_from_dict_without_instance_id(self):
|
|
93
|
+
d = {"key_id": "kid", "datakey": "dk"}
|
|
94
|
+
r = DecryptKeyCapsuleResponse.from_dict(d)
|
|
95
|
+
assert r.instance_id is None
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import json
|
|
3
|
+
import time
|
|
4
|
+
import pytest
|
|
5
|
+
from huaweicloud_encryptionsdk.log.log_file_backup import LogFileBackup
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TestLogFileBackup:
|
|
9
|
+
def test_save_and_recover(self, tmp_path):
|
|
10
|
+
backup = LogFileBackup(backup_dir_path=str(tmp_path))
|
|
11
|
+
logs = [{"operation": "encrypt", "keyId": "kid-123"}]
|
|
12
|
+
filepath = backup.save_failed_logs(logs, int(time.time() * 1000))
|
|
13
|
+
assert filepath is not None
|
|
14
|
+
assert os.path.exists(filepath)
|
|
15
|
+
|
|
16
|
+
recovered = backup.recover_failed_logs()
|
|
17
|
+
assert len(recovered) == 1
|
|
18
|
+
assert recovered[0]["operation"] == "encrypt"
|
|
19
|
+
assert recovered[0]["keyId"] == "kid-123"
|
|
20
|
+
|
|
21
|
+
def test_save_multiple_batches(self, tmp_path):
|
|
22
|
+
backup = LogFileBackup(backup_dir_path=str(tmp_path))
|
|
23
|
+
backup.save_failed_logs([{"op": "a"}], int(time.time() * 1000))
|
|
24
|
+
time.sleep(0.01)
|
|
25
|
+
backup.save_failed_logs([{"op": "b"}], int(time.time() * 1000))
|
|
26
|
+
|
|
27
|
+
recovered = backup.recover_failed_logs()
|
|
28
|
+
assert len(recovered) == 2
|
|
29
|
+
|
|
30
|
+
def test_clear_all(self, tmp_path):
|
|
31
|
+
backup = LogFileBackup(backup_dir_path=str(tmp_path))
|
|
32
|
+
backup.save_failed_logs([{"op": "a"}], int(time.time() * 1000))
|
|
33
|
+
backup.clear_all_backup_files()
|
|
34
|
+
|
|
35
|
+
recovered = backup.recover_failed_logs()
|
|
36
|
+
assert len(recovered) == 0
|
|
37
|
+
|
|
38
|
+
def test_recover_from_empty_dir(self, tmp_path):
|
|
39
|
+
backup = LogFileBackup(backup_dir_path=str(tmp_path))
|
|
40
|
+
recovered = backup.recover_failed_logs()
|
|
41
|
+
assert recovered == []
|
|
42
|
+
|
|
43
|
+
def test_backup_dir_property(self, tmp_path):
|
|
44
|
+
backup = LogFileBackup(backup_dir_path=str(tmp_path))
|
|
45
|
+
assert backup.backup_dir_path == str(tmp_path)
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import pytest
|
|
3
|
+
from huaweicloud_encryptionsdk.log.lts_struct_log_builder import LtsStructLogBuilder
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class TestBuildEncryptStructLog:
|
|
7
|
+
def test_basic_fields(self):
|
|
8
|
+
log = LtsStructLogBuilder.build_encrypt_struct_log(
|
|
9
|
+
encrypt_file_name=None,
|
|
10
|
+
encrypt_data_bytes=100,
|
|
11
|
+
encrypt_output_file_name=None,
|
|
12
|
+
encrypt_output_bytes=200,
|
|
13
|
+
key_id="kid-123",
|
|
14
|
+
access_point_type="ECS",
|
|
15
|
+
instance_id="inst-456",
|
|
16
|
+
)
|
|
17
|
+
assert log["operation"] == "encrypt"
|
|
18
|
+
assert "timestamp" in log
|
|
19
|
+
assert "utcTime" in log
|
|
20
|
+
assert log["encryptDataBytes"] == "100"
|
|
21
|
+
assert log["encryptOutputBytes"] == "200"
|
|
22
|
+
assert log["keyId"] == "kid-123"
|
|
23
|
+
assert log["accessPointType"] == "ECS"
|
|
24
|
+
assert log["instanceId"] == "inst-456"
|
|
25
|
+
|
|
26
|
+
def test_optional_fields_omitted(self):
|
|
27
|
+
log = LtsStructLogBuilder.build_encrypt_struct_log(
|
|
28
|
+
encrypt_file_name=None,
|
|
29
|
+
encrypt_data_bytes=0,
|
|
30
|
+
encrypt_output_file_name=None,
|
|
31
|
+
encrypt_output_bytes=0,
|
|
32
|
+
key_id=None,
|
|
33
|
+
access_point_type=None,
|
|
34
|
+
instance_id=None,
|
|
35
|
+
)
|
|
36
|
+
assert "encryptDataBytes" not in log
|
|
37
|
+
assert "encryptOutputBytes" not in log
|
|
38
|
+
assert "keyId" not in log
|
|
39
|
+
assert "accessPointType" not in log
|
|
40
|
+
assert "instanceId" not in log
|
|
41
|
+
|
|
42
|
+
def test_with_file_names(self):
|
|
43
|
+
log = LtsStructLogBuilder.build_encrypt_struct_log(
|
|
44
|
+
encrypt_file_name="test.txt",
|
|
45
|
+
encrypt_data_bytes=100,
|
|
46
|
+
encrypt_output_file_name="test.enc",
|
|
47
|
+
encrypt_output_bytes=200,
|
|
48
|
+
key_id="kid",
|
|
49
|
+
access_point_type="Custom",
|
|
50
|
+
instance_id=None,
|
|
51
|
+
)
|
|
52
|
+
assert log["encryptFileName"] == "test.txt"
|
|
53
|
+
assert log["encryptOutputFileName"] == "test.enc"
|
|
54
|
+
assert "instanceId" not in log
|
|
55
|
+
|
|
56
|
+
def test_utc_time_format(self):
|
|
57
|
+
log = LtsStructLogBuilder.build_encrypt_struct_log(
|
|
58
|
+
encrypt_file_name=None, encrypt_data_bytes=None,
|
|
59
|
+
encrypt_output_file_name=None, encrypt_output_bytes=None,
|
|
60
|
+
key_id=None, access_point_type=None, instance_id=None,
|
|
61
|
+
)
|
|
62
|
+
assert log["utcTime"].endswith("Z")
|
|
63
|
+
assert "T" in log["utcTime"]
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class TestBuildDecryptStructLog:
|
|
67
|
+
def test_basic_fields(self):
|
|
68
|
+
log = LtsStructLogBuilder.build_decrypt_struct_log(
|
|
69
|
+
decrypt_file_name=None,
|
|
70
|
+
decrypt_data_bytes=200,
|
|
71
|
+
decrypt_output_file_name=None,
|
|
72
|
+
decrypt_output_bytes=100,
|
|
73
|
+
key_id="kid-123",
|
|
74
|
+
access_point_type="CCE",
|
|
75
|
+
instance_id="inst-789",
|
|
76
|
+
)
|
|
77
|
+
assert log["operation"] == "decrypt"
|
|
78
|
+
assert log["decryptDataBytes"] == "200"
|
|
79
|
+
assert log["decryptOutputBytes"] == "100"
|
|
80
|
+
assert log["keyId"] == "kid-123"
|
|
81
|
+
assert log["accessPointType"] == "CCE"
|
|
82
|
+
assert log["instanceId"] == "inst-789"
|
|
83
|
+
|
|
84
|
+
def test_instance_id_from_decrypt_response(self):
|
|
85
|
+
log = LtsStructLogBuilder.build_decrypt_struct_log(
|
|
86
|
+
decrypt_file_name=None, decrypt_data_bytes=None,
|
|
87
|
+
decrypt_output_file_name=None, decrypt_output_bytes=None,
|
|
88
|
+
key_id="kid", access_point_type="Custom",
|
|
89
|
+
instance_id="from-kms-response",
|
|
90
|
+
)
|
|
91
|
+
assert log["instanceId"] == "from-kms-response"
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
class TestGetLocalIp:
|
|
95
|
+
def test_returns_string_or_none(self):
|
|
96
|
+
ip = LtsStructLogBuilder.get_local_ip()
|
|
97
|
+
assert ip is None or isinstance(ip, str)
|
tests/test_models.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from huaweicloud_encryptionsdk.model.key_capsule import KeyCapsule
|
|
3
|
+
from huaweicloud_encryptionsdk.model.kms_config import KMSConfig
|
|
4
|
+
from huaweicloud_encryptionsdk.model.access_point_config import AccessPointConfig
|
|
5
|
+
from huaweicloud_encryptionsdk.model.key_capsule_config import KeyCapsuleConfig
|
|
6
|
+
from huaweicloud_encryptionsdk.model.data_key_materials import DataKeyMaterials
|
|
7
|
+
from huaweicloud_encryptionsdk.model.crypto_result import CryptoResult
|
|
8
|
+
from huaweicloud_encryptionsdk.model.request.encrypt_request import EncryptRequest
|
|
9
|
+
from huaweicloud_encryptionsdk.huawei_config import HuaweiConfig
|
|
10
|
+
from huaweicloud_encryptionsdk.model.enums.crypto_algorithm import CryptoAlgorithm
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class TestKeyCapsule:
|
|
14
|
+
def test_defaults(self):
|
|
15
|
+
kc = KeyCapsule()
|
|
16
|
+
assert kc.key_id is None
|
|
17
|
+
assert kc.datakey_capsule is None
|
|
18
|
+
assert kc.access_point_type == 0
|
|
19
|
+
assert kc.instance_id is None
|
|
20
|
+
|
|
21
|
+
def test_with_values(self):
|
|
22
|
+
kc = KeyCapsule(key_id="kid", datakey_capsule="capsule", access_point_type=1, instance_id="inst-123")
|
|
23
|
+
assert kc.key_id == "kid"
|
|
24
|
+
assert kc.datakey_capsule == "capsule"
|
|
25
|
+
assert kc.access_point_type == 1
|
|
26
|
+
assert kc.instance_id == "inst-123"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class TestKMSConfig:
|
|
30
|
+
def test_defaults(self):
|
|
31
|
+
c = KMSConfig()
|
|
32
|
+
assert c.region == ""
|
|
33
|
+
assert c.key_id == ""
|
|
34
|
+
assert c.project_id == ""
|
|
35
|
+
assert c.end_point is None
|
|
36
|
+
|
|
37
|
+
def test_with_values(self):
|
|
38
|
+
c = KMSConfig(region="cn-north-7", key_id="kid", project_id="pid", end_point="https://kms.example.com")
|
|
39
|
+
assert c.region == "cn-north-7"
|
|
40
|
+
assert c.key_id == "kid"
|
|
41
|
+
assert c.project_id == "pid"
|
|
42
|
+
assert c.end_point == "https://kms.example.com"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class TestAccessPointConfig:
|
|
46
|
+
def test_defaults(self):
|
|
47
|
+
ap = AccessPointConfig()
|
|
48
|
+
assert ap.type == 0
|
|
49
|
+
assert ap.encrypted_private_key is None
|
|
50
|
+
assert ap.private_key_decrypt_callback is None
|
|
51
|
+
assert ap.service_account is None
|
|
52
|
+
|
|
53
|
+
def test_with_callback(self):
|
|
54
|
+
cb = lambda x: x
|
|
55
|
+
ap = AccessPointConfig(type=2, encrypted_private_key="enc", private_key_decrypt_callback=cb)
|
|
56
|
+
assert ap.type == 2
|
|
57
|
+
assert ap.private_key_decrypt_callback("test") == "test"
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class TestKeyCapsuleConfig:
|
|
61
|
+
def test_defaults(self):
|
|
62
|
+
kcc = KeyCapsuleConfig()
|
|
63
|
+
assert kcc.region == ""
|
|
64
|
+
assert kcc.default_policy_id is None
|
|
65
|
+
assert kcc.default_key_policy is None
|
|
66
|
+
assert kcc.access_point_config is None
|
|
67
|
+
|
|
68
|
+
def test_with_access_point(self):
|
|
69
|
+
ap = AccessPointConfig(type=1)
|
|
70
|
+
kcc = KeyCapsuleConfig(region="cn-north-7", access_point_config=ap)
|
|
71
|
+
assert kcc.region == "cn-north-7"
|
|
72
|
+
assert kcc.access_point_config.type == 1
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class TestEncryptRequest:
|
|
76
|
+
def test_defaults(self):
|
|
77
|
+
r = EncryptRequest()
|
|
78
|
+
assert r.encryption_context == {}
|
|
79
|
+
assert r.plain_text == b""
|
|
80
|
+
assert r.policy_id is None
|
|
81
|
+
assert r.key_policy is None
|
|
82
|
+
|
|
83
|
+
def test_with_values(self):
|
|
84
|
+
r = EncryptRequest(plain_text=b"hello", policy_id="pid")
|
|
85
|
+
assert r.plain_text == b"hello"
|
|
86
|
+
assert r.policy_id == "pid"
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class TestDataKeyMaterials:
|
|
90
|
+
def test_defaults(self):
|
|
91
|
+
dkm = DataKeyMaterials()
|
|
92
|
+
assert dkm.plaintext_data_key == b""
|
|
93
|
+
assert dkm.ciphertext_data_keys == []
|
|
94
|
+
assert dkm.encryption_contexts == {}
|
|
95
|
+
assert dkm.crypto_algorithm is None
|
|
96
|
+
assert dkm.key_capsule is None
|
|
97
|
+
|
|
98
|
+
def test_with_values(self):
|
|
99
|
+
kc = KeyCapsule(key_id="kid", instance_id="inst-1")
|
|
100
|
+
dkm = DataKeyMaterials(plaintext_data_key=b"\x01\x02", key_capsule=kc)
|
|
101
|
+
assert dkm.plaintext_data_key == b"\x01\x02"
|
|
102
|
+
assert dkm.key_capsule.key_id == "kid"
|
|
103
|
+
assert dkm.key_capsule.instance_id == "inst-1"
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class TestCryptoResult:
|
|
107
|
+
def test_defaults(self):
|
|
108
|
+
r = CryptoResult()
|
|
109
|
+
assert r.result is None
|
|
110
|
+
assert r.headers is None
|
|
111
|
+
assert r.data_length == 0
|
|
112
|
+
|
|
113
|
+
def test_with_values(self):
|
|
114
|
+
r = CryptoResult(result=b"decrypted", data_length=10)
|
|
115
|
+
assert r.result == b"decrypted"
|
|
116
|
+
assert r.data_length == 10
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class TestHuaweiConfig:
|
|
120
|
+
def test_defaults(self):
|
|
121
|
+
c = HuaweiConfig()
|
|
122
|
+
assert c.ak == ""
|
|
123
|
+
assert c.sk == ""
|
|
124
|
+
assert c.is_discovery is False
|
|
125
|
+
assert c.kms_config_list is None
|
|
126
|
+
assert c.crypto_algorithm is None
|
|
127
|
+
assert c.key_capsule_config is None
|
|
128
|
+
assert c.log_config is None
|
|
129
|
+
assert c.local_log_only is False
|
|
130
|
+
|
|
131
|
+
def test_set_ak_sk(self):
|
|
132
|
+
c = HuaweiConfig()
|
|
133
|
+
c.set_ak("myak")
|
|
134
|
+
c.set_sk("mysk")
|
|
135
|
+
assert c.get_ak() == "myak"
|
|
136
|
+
assert c.get_sk() == "mysk"
|
|
137
|
+
|
|
138
|
+
def test_with_key_capsule(self):
|
|
139
|
+
kcc = KeyCapsuleConfig(region="cn-north-7")
|
|
140
|
+
c = HuaweiConfig(ak="ak", sk="sk", key_capsule_config=kcc, crypto_algorithm=CryptoAlgorithm.AES_256_GCM_NOPADDING)
|
|
141
|
+
assert c.key_capsule_config.region == "cn-north-7"
|
|
142
|
+
assert c.crypto_algorithm == CryptoAlgorithm.AES_256_GCM_NOPADDING
|
tests/test_utils.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from huaweicloud_encryptionsdk.common.utils import (
|
|
3
|
+
is_empty, hex_to_bytes, bytes_to_hex, long_to_bytes, bytes_to_long,
|
|
4
|
+
bytes_to_int, add_prevent_overflow, serialize_context, deserialize_context,
|
|
5
|
+
get_capsule_kms_config,
|
|
6
|
+
)
|
|
7
|
+
from huaweicloud_encryptionsdk.model.kms_config import KMSConfig
|
|
8
|
+
from huaweicloud_encryptionsdk.model.key_capsule_config import KeyCapsuleConfig
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class TestIsEmpty:
|
|
12
|
+
def test_none(self):
|
|
13
|
+
assert is_empty(None) is True
|
|
14
|
+
|
|
15
|
+
def test_empty_string(self):
|
|
16
|
+
assert is_empty("") is True
|
|
17
|
+
|
|
18
|
+
def test_empty_list(self):
|
|
19
|
+
assert is_empty([]) is True
|
|
20
|
+
|
|
21
|
+
def test_non_empty_string(self):
|
|
22
|
+
assert is_empty("abc") is False
|
|
23
|
+
|
|
24
|
+
def test_non_empty_list(self):
|
|
25
|
+
assert is_empty([1]) is False
|
|
26
|
+
|
|
27
|
+
def test_zero(self):
|
|
28
|
+
assert is_empty(0) is True
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class TestHexConversion:
|
|
32
|
+
def test_hex_to_bytes(self):
|
|
33
|
+
assert hex_to_bytes("48656c6c6f") == b"Hello"
|
|
34
|
+
|
|
35
|
+
def test_bytes_to_hex(self):
|
|
36
|
+
assert bytes_to_hex(b"Hello") == "48656c6c6f"
|
|
37
|
+
|
|
38
|
+
def test_roundtrip(self):
|
|
39
|
+
original = b"\x01\x02\xff\x00"
|
|
40
|
+
assert hex_to_bytes(bytes_to_hex(original)) == original
|
|
41
|
+
|
|
42
|
+
def test_empty(self):
|
|
43
|
+
with pytest.raises(ValueError):
|
|
44
|
+
hex_to_bytes("")
|
|
45
|
+
assert bytes_to_hex(b"") == ""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class TestLongBytesConversion:
|
|
49
|
+
def test_long_to_bytes(self):
|
|
50
|
+
result = long_to_bytes(256)
|
|
51
|
+
assert len(result) == 8
|
|
52
|
+
assert result[-2:] == b"\x01\x00"
|
|
53
|
+
|
|
54
|
+
def test_bytes_to_long(self):
|
|
55
|
+
assert bytes_to_long(long_to_bytes(12345)) == 12345
|
|
56
|
+
|
|
57
|
+
def test_bytes_to_int(self):
|
|
58
|
+
b = b"\x00\x00\x00\x2a"
|
|
59
|
+
assert bytes_to_int(b) == 42
|
|
60
|
+
|
|
61
|
+
def test_roundtrip(self):
|
|
62
|
+
for val in [0, 1, 255, 256, 65535, 2**31 - 1]:
|
|
63
|
+
assert bytes_to_long(long_to_bytes(val)) == val
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class TestAddPreventOverflow:
|
|
67
|
+
def test_normal(self):
|
|
68
|
+
assert add_prevent_overflow(100, 200) == 300
|
|
69
|
+
|
|
70
|
+
def test_overflow(self):
|
|
71
|
+
result = add_prevent_overflow(2**63 - 1, 1)
|
|
72
|
+
assert result == 2**63
|
|
73
|
+
|
|
74
|
+
def test_zero(self):
|
|
75
|
+
assert add_prevent_overflow(0, 0) == 0
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class TestSerializeContext:
|
|
79
|
+
def test_roundtrip(self):
|
|
80
|
+
ctx = {"key1": "value1", "key2": "value2"}
|
|
81
|
+
serialized = serialize_context(ctx)
|
|
82
|
+
deserialized = deserialize_context(serialized)
|
|
83
|
+
assert deserialized == ctx
|
|
84
|
+
|
|
85
|
+
def test_empty_context(self):
|
|
86
|
+
ctx = {}
|
|
87
|
+
serialized = serialize_context(ctx)
|
|
88
|
+
deserialized = deserialize_context(serialized)
|
|
89
|
+
assert deserialized == ctx
|
|
90
|
+
|
|
91
|
+
def test_single_entry(self):
|
|
92
|
+
ctx = {"a": "b"}
|
|
93
|
+
serialized = serialize_context(ctx)
|
|
94
|
+
deserialized = deserialize_context(serialized)
|
|
95
|
+
assert deserialized == ctx
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class TestGetCapsuleKmsConfig:
|
|
99
|
+
def test_found(self):
|
|
100
|
+
kms1 = KMSConfig(region="cn-north-7", key_id="kid1", project_id="pid1")
|
|
101
|
+
kms2 = KMSConfig(region="cn-south-1", key_id="kid2", project_id="pid2")
|
|
102
|
+
kcc = KeyCapsuleConfig(region="cn-north-7")
|
|
103
|
+
result = get_capsule_kms_config([kms1, kms2], kcc)
|
|
104
|
+
assert result.region == "cn-north-7"
|
|
105
|
+
assert result.key_id == "kid1"
|
|
106
|
+
|
|
107
|
+
def test_not_found(self):
|
|
108
|
+
kms1 = KMSConfig(region="cn-north-7", key_id="kid1", project_id="pid1")
|
|
109
|
+
kcc = KeyCapsuleConfig(region="cn-south-1")
|
|
110
|
+
with pytest.raises(Exception):
|
|
111
|
+
get_capsule_kms_config([kms1], kcc)
|