Adjust nacl import in tests (#119392)

pull/119415/head
epenet 2024-06-11 18:06:30 +02:00 committed by GitHub
parent 9e8f9abbf7
commit a0abd537c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 71 deletions

View File

@ -5,7 +5,8 @@ from http import HTTPStatus
import json import json
from unittest.mock import patch from unittest.mock import patch
import pytest from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
from homeassistant.components.mobile_app.const import CONF_SECRET, DOMAIN from homeassistant.components.mobile_app.const import CONF_SECRET, DOMAIN
from homeassistant.const import CONF_WEBHOOK_ID from homeassistant.const import CONF_WEBHOOK_ID
@ -66,13 +67,6 @@ async def test_registration_encryption(
hass: HomeAssistant, hass_client: ClientSessionGenerator hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> None: ) -> None:
"""Test that registrations happen.""" """Test that registrations happen."""
try:
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
except (ImportError, OSError):
pytest.skip("libnacl/libsodium is not installed")
return
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
api_client = await hass_client() api_client = await hass_client()
@ -111,13 +105,6 @@ async def test_registration_encryption_legacy(
hass: HomeAssistant, hass_client: ClientSessionGenerator hass: HomeAssistant, hass_client: ClientSessionGenerator
) -> None: ) -> None:
"""Test that registrations happen.""" """Test that registrations happen."""
try:
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
except (ImportError, OSError):
pytest.skip("libnacl/libsodium is not installed")
return
await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
api_client = await hass_client() api_client = await hass_client()

View File

@ -2,8 +2,11 @@
from binascii import unhexlify from binascii import unhexlify
from http import HTTPStatus from http import HTTPStatus
import json
from unittest.mock import ANY, patch from unittest.mock import ANY, patch
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
import pytest import pytest
from homeassistant.components.camera import CameraEntityFeature from homeassistant.components.camera import CameraEntityFeature
@ -35,14 +38,6 @@ async def homeassistant(hass):
def encrypt_payload(secret_key, payload, encode_json=True): def encrypt_payload(secret_key, payload, encode_json=True):
"""Return a encrypted payload given a key and dictionary of data.""" """Return a encrypted payload given a key and dictionary of data."""
try:
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
except (ImportError, OSError):
pytest.skip("libnacl/libsodium is not installed")
import json
prepped_key = unhexlify(secret_key) prepped_key = unhexlify(secret_key)
if encode_json: if encode_json:
@ -56,14 +51,6 @@ def encrypt_payload(secret_key, payload, encode_json=True):
def encrypt_payload_legacy(secret_key, payload, encode_json=True): def encrypt_payload_legacy(secret_key, payload, encode_json=True):
"""Return a encrypted payload given a key and dictionary of data.""" """Return a encrypted payload given a key and dictionary of data."""
try:
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
except (ImportError, OSError):
pytest.skip("libnacl/libsodium is not installed")
import json
keylen = SecretBox.KEY_SIZE keylen = SecretBox.KEY_SIZE
prepped_key = secret_key.encode("utf-8") prepped_key = secret_key.encode("utf-8")
prepped_key = prepped_key[:keylen] prepped_key = prepped_key[:keylen]
@ -80,14 +67,6 @@ def encrypt_payload_legacy(secret_key, payload, encode_json=True):
def decrypt_payload(secret_key, encrypted_data): def decrypt_payload(secret_key, encrypted_data):
"""Return a decrypted payload given a key and a string of encrypted data.""" """Return a decrypted payload given a key and a string of encrypted data."""
try:
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
except (ImportError, OSError):
pytest.skip("libnacl/libsodium is not installed")
import json
prepped_key = unhexlify(secret_key) prepped_key = unhexlify(secret_key)
decrypted_data = SecretBox(prepped_key).decrypt( decrypted_data = SecretBox(prepped_key).decrypt(
@ -100,14 +79,6 @@ def decrypt_payload(secret_key, encrypted_data):
def decrypt_payload_legacy(secret_key, encrypted_data): def decrypt_payload_legacy(secret_key, encrypted_data):
"""Return a decrypted payload given a key and a string of encrypted data.""" """Return a decrypted payload given a key and a string of encrypted data."""
try:
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
except (ImportError, OSError):
pytest.skip("libnacl/libsodium is not installed")
import json
keylen = SecretBox.KEY_SIZE keylen = SecretBox.KEY_SIZE
prepped_key = secret_key.encode("utf-8") prepped_key = secret_key.encode("utf-8")
prepped_key = prepped_key[:keylen] prepped_key = prepped_key[:keylen]

View File

@ -1,8 +1,12 @@
"""The tests for the Owntracks device tracker.""" """The tests for the Owntracks device tracker."""
import base64
import json import json
import pickle
from unittest.mock import patch from unittest.mock import patch
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
import pytest import pytest
from homeassistant.components import owntracks from homeassistant.components import owntracks
@ -1330,23 +1334,14 @@ def generate_ciphers(secret):
# PyNaCl ciphertext generation will fail if the module # PyNaCl ciphertext generation will fail if the module
# cannot be imported. However, the test for decryption # cannot be imported. However, the test for decryption
# also relies on this library and won't be run without it. # also relies on this library and won't be run without it.
import base64 keylen = SecretBox.KEY_SIZE
import pickle key = secret.encode("utf-8")
key = key[:keylen]
key = key.ljust(keylen, b"\0")
try: msg = json.dumps(DEFAULT_LOCATION_MESSAGE).encode("utf-8")
from nacl.encoding import Base64Encoder
from nacl.secret import SecretBox
keylen = SecretBox.KEY_SIZE ctxt = SecretBox(key).encrypt(msg, encoder=Base64Encoder).decode("utf-8")
key = secret.encode("utf-8")
key = key[:keylen]
key = key.ljust(keylen, b"\0")
msg = json.dumps(DEFAULT_LOCATION_MESSAGE).encode("utf-8")
ctxt = SecretBox(key).encrypt(msg, encoder=Base64Encoder).decode("utf-8")
except (ImportError, OSError):
ctxt = ""
mctxt = base64.b64encode( mctxt = base64.b64encode(
pickle.dumps( pickle.dumps(
@ -1381,9 +1376,6 @@ def mock_cipher():
def mock_decrypt(ciphertext, key): def mock_decrypt(ciphertext, key):
"""Decrypt/unpickle.""" """Decrypt/unpickle."""
import base64
import pickle
(mkey, plaintext) = pickle.loads(base64.b64decode(ciphertext)) (mkey, plaintext) = pickle.loads(base64.b64decode(ciphertext))
if key != mkey: if key != mkey:
raise ValueError raise ValueError
@ -1504,12 +1496,6 @@ async def test_encrypted_payload_no_topic_key(hass: HomeAssistant, setup_comp) -
async def test_encrypted_payload_libsodium(hass: HomeAssistant, setup_comp) -> None: async def test_encrypted_payload_libsodium(hass: HomeAssistant, setup_comp) -> None:
"""Test sending encrypted message payload.""" """Test sending encrypted message payload."""
try:
import nacl # noqa: F401
except (ImportError, OSError):
pytest.skip("PyNaCl/libsodium is not installed")
return
await setup_owntracks(hass, {CONF_SECRET: TEST_SECRET_KEY}) await setup_owntracks(hass, {CONF_SECRET: TEST_SECRET_KEY})
await send_message(hass, LOCATION_TOPIC, ENCRYPTED_LOCATION_MESSAGE) await send_message(hass, LOCATION_TOPIC, ENCRYPTED_LOCATION_MESSAGE)