Skip to content

Commit

Permalink
imgtool: Fix test paths
Browse files Browse the repository at this point in the history
Move tests from imgtool/keys to test/keys
Move pre-generated files to test/assets folder
Fix paths for tests to run from root directory
Replace hardcoded paths with constants in tests

Signed-off-by: Rustam Ismayilov <[email protected]>
Change-Id: I100e65609f31cefa1c17f20143bceb165862fa14
  • Loading branch information
rustammendel committed Jun 21, 2024
1 parent f035441 commit 6388c78
Show file tree
Hide file tree
Showing 62 changed files with 46 additions and 48 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions scripts/tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
PUB_KEY_EXT = ".pub"
PUB_KEY_HASH_EXT = ".pubhash"

images_dir = "./images"
images_dir = "./tests/assets/images"
signed_images_dir = images_dir + "/signed"

keys_dir = "./tests/assets/keys/"

def tmp_name(tmp_path, key_type, suffix=""):
return tmp_path / (key_type + suffix)
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions scripts/tests/test_dumpinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from click.testing import CliRunner

from imgtool.main import imgtool
from tests.constants import tmp_name, KEY_TYPES, signed_images_dir
from tests.constants import tmp_name, KEY_TYPES, signed_images_dir, images_dir

DUMPINFO_SUCCESS_LITERAL = "dumpinfo has run successfully"

Expand Down Expand Up @@ -63,7 +63,7 @@ def setup(self, tmp_path_persistent, key_type="rsa-2048"):
imgtool, ["keygen", "--key", str(self.key), "--type", key_type]
)

self.image = "./images/zero.bin"
self.image = images_dir + "/zero.bin"
self.image_signed = tmp_name(tmp_path_persistent, "image", ".signed")

@pytest.mark.parametrize("key_type", ("rsa-2048",))
Expand Down
36 changes: 18 additions & 18 deletions scripts/tests/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from imgtool.main import imgtool
from tests.constants import KEY_TYPES, GEN_KEY_EXT, tmp_name, OPENSSL_KEY_TYPES, KEY_ENCODINGS, PUB_KEY_EXT, \
PVT_KEY_FORMATS, KEY_LANGS, PUB_HASH_ENCODINGS, PUB_KEY_HASH_EXT
PVT_KEY_FORMATS, KEY_LANGS, PUB_HASH_ENCODINGS, PUB_KEY_HASH_EXT, keys_dir

UNSUPPORTED_TYPES = [("openssl", "ed25519"),
("openssl", "x25519"),
Expand Down Expand Up @@ -168,7 +168,7 @@ class TestGetPriv(TestKeys):
def test_getpriv(self, key_type, key_format, tmp_path_persistent):
"""Get private key"""

gen_key = "./keys/" + key_type + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + GEN_KEY_EXT

result = self.runner.invoke(
imgtool,
Expand All @@ -187,7 +187,7 @@ def test_getpriv(self, key_type, key_format, tmp_path_persistent):
def test_getpriv_with_password(self, key_type, key_format, tmp_path_persistent):
"""Get private key with password"""

gen_key = "./keys/" + key_type + "_passwd" + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + "_passwd" + GEN_KEY_EXT

result = self.runner.invoke(
imgtool,
Expand All @@ -207,7 +207,7 @@ def test_getpriv_with_password(self, key_type, key_format, tmp_path_persistent):
def test_getpriv_unsupported(self, key_format, key_type, tmp_path_persistent):
"""Get private key for unsupported combinations should print error message"""

gen_key = "./keys/" + key_type + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + GEN_KEY_EXT

result = self.runner.invoke(
imgtool,
Expand All @@ -228,7 +228,7 @@ def test_getpriv_unsupported(self, key_format, key_type, tmp_path_persistent):
def test_getpriv_with_invalid_pass(self, key_type, key_format, tmp_path_persistent):
"""Get private key with invalid password should print 'Invalid passphrase' in stdout"""

gen_key = "./keys/" + key_type + "_passwd" + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + "_passwd" + GEN_KEY_EXT

result = self.runner.invoke(
imgtool,
Expand All @@ -252,7 +252,7 @@ class TestGetPub(TestKeys):
def test_getpub(self, key_type, tmp_path_persistent):
"""Get public key - Default lang is c """

gen_key = "./keys/" + key_type + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + GEN_KEY_EXT
pub_key = tmp_name(tmp_path_persistent, key_type, "." + "default" + PUB_KEY_EXT)

assert not pub_key.exists()
Expand All @@ -276,7 +276,7 @@ def test_getpub(self, key_type, tmp_path_persistent):
def test_getpub_with_encoding(self, key_type, encoding, tmp_path_persistent):
"""Get public key with encoding"""

gen_key = "./keys/" + key_type + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + GEN_KEY_EXT
pub_key = tmp_name(tmp_path_persistent, key_type, "." + encoding + PUB_KEY_EXT)

assert not pub_key.exists()
Expand All @@ -302,7 +302,7 @@ def test_getpub_with_encoding(self, key_type, encoding, tmp_path_persistent):
def test_getpub_with_lang(self, key_type, lang, tmp_path_persistent):
"""Get public key with specified lang"""

gen_key = "./keys/" + key_type + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + GEN_KEY_EXT
pub_key = tmp_name(tmp_path_persistent, key_type, PUB_KEY_EXT + "." + lang)

assert not pub_key.exists()
Expand All @@ -328,7 +328,7 @@ def test_getpub_with_lang(self, key_type, lang, tmp_path_persistent):
def test_getpub_no_outfile(self, key_type, encoding, tmp_path_persistent):
"""Get public key without output file should dump only to stdout"""

gen_key = "./keys/" + key_type + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + GEN_KEY_EXT
pub_key = tmp_name(tmp_path_persistent, key_type, "." + encoding + "-null" + PUB_KEY_EXT)

assert not pub_key.exists()
Expand All @@ -350,7 +350,7 @@ def test_getpub_no_outfile(self, key_type, encoding, tmp_path_persistent):
def test_getpub_invalid_encoding(self, key_type, tmp_path_persistent):
"""Get public key with invalid encoding should error message in stdout"""

gen_key = "./keys/" + key_type + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + GEN_KEY_EXT
pub_key = tmp_name(tmp_path_persistent, key_type, "." + PUB_KEY_EXT)

assert not pub_key.exists()
Expand All @@ -373,7 +373,7 @@ def test_getpub_invalid_encoding(self, key_type, tmp_path_persistent):
def test_getpub_invalid_lang(self, key_type, tmp_path_persistent):
"""Get public key with invalid lang should print error message in stdout"""

gen_key = "./keys/" + key_type + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + GEN_KEY_EXT
pub_key = tmp_name(tmp_path_persistent, key_type, "." + PUB_KEY_EXT)

assert not pub_key.exists()
Expand All @@ -398,7 +398,7 @@ def test_getpub_invalid_lang(self, key_type, tmp_path_persistent):
def test_getpub_with_encoding_and_lang(self, key_type, encoding, lang, tmp_path_persistent):
"""Get public key with both encoding and lang should print error message"""

gen_key = "./keys/" + key_type + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + GEN_KEY_EXT
pub_key = tmp_name(tmp_path_persistent, key_type, "." + lang + encoding + PUB_KEY_EXT)

assert not pub_key.exists()
Expand All @@ -425,7 +425,7 @@ def test_getpub_with_encoding_and_lang(self, key_type, encoding, lang, tmp_path_
def test_getpub_invalid_pass(self, key_type, encoding, tmp_path_persistent):
"""Get public key with invalid password should print 'Invalid passphrase' in stdout"""

gen_key = "./keys/" + key_type + "_passwd" + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + "_passwd" + GEN_KEY_EXT
pub_key = tmp_name(tmp_path_persistent, key_type, "." + encoding + PUB_KEY_EXT)

result = self.runner.invoke(
Expand All @@ -451,7 +451,7 @@ class TestGetPubHash(TestKeys):
def test_getpubhash(self, key_type, tmp_path_persistent):
"""Get the hash of the public key - Default encoding is lang-c"""

gen_key = "./keys/" + key_type + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + GEN_KEY_EXT
pub_key_hash = tmp_name(
tmp_path_persistent, key_type, "." + "default" + PUB_KEY_HASH_EXT
)
Expand All @@ -476,7 +476,7 @@ def test_getpubhash(self, key_type, tmp_path_persistent):
def test_getpubhash_with_encoding(self, key_type, encoding, tmp_path_persistent):
"""Get the hash of the public key with encoding"""

gen_key = "./keys/" + key_type + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + GEN_KEY_EXT
pub_key_hash = tmp_name(
tmp_path_persistent, key_type, "." + encoding + PUB_KEY_HASH_EXT
)
Expand All @@ -503,7 +503,7 @@ def test_getpubhash_with_encoding(self, key_type, encoding, tmp_path_persistent)
def test_getpubhash_with_no_output(self, key_type, encoding, tmp_path_persistent):
"""Get the hash of the public key without outfile should dump only to stdout"""

gen_key = "./keys/" + key_type + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + GEN_KEY_EXT
pub_key_hash = tmp_name(
tmp_path_persistent, key_type, "." + encoding + "-null" + PUB_KEY_HASH_EXT
)
Expand All @@ -527,7 +527,7 @@ def test_getpubhash_with_no_output(self, key_type, encoding, tmp_path_persistent
def test_getpubhash_with_invalid_encoding(self, key_type, tmp_path_persistent):
"""Get the hash of the public key with encoding"""

gen_key = "./keys/" + key_type + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + GEN_KEY_EXT
pub_key_hash = tmp_name(
tmp_path_persistent, key_type, "." + "invalid" + PUB_KEY_HASH_EXT
)
Expand Down Expand Up @@ -555,7 +555,7 @@ def test_getpubhash_with_invalid_encoding(self, key_type, tmp_path_persistent):
def test_getpubhash_with_invalid_pass(self, key_type, encoding, tmp_path_persistent):
"""Get the hash of the public key with invalid password should print 'Invalid passphrase' in stdout"""

gen_key = "./keys/" + key_type + "_passwd" + GEN_KEY_EXT
gen_key = keys_dir + "/" + key_type + "_passwd" + GEN_KEY_EXT
pub_key_hash = tmp_name(
tmp_path_persistent, key_type, "." + encoding + "-null" + PUB_KEY_HASH_EXT
)
Expand Down
32 changes: 15 additions & 17 deletions scripts/tests/test_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@

from imgtool.dumpinfo import dump_imginfo
from imgtool.main import imgtool
from tests.constants import KEY_TYPES, GEN_KEY_EXT, PUB_KEY_EXT, tmp_name
from tests.constants import KEY_TYPES, GEN_KEY_EXT, PUB_KEY_EXT, tmp_name, images_dir, keys_dir

try:
KEY_TYPES.remove("x25519") # x25519 is not used for signing, so directory does not contain any such image
except ValueError:
pass

KEY_DIR = "./keys/"


def assert_image_signed(result, image_signed, verify=True):
assert result.exit_code == 0
Expand Down Expand Up @@ -63,7 +61,7 @@ def tmp_path_persistent(self, tmp_path_factory):
def setup(self, tmp_path_persistent):
"""Generate keys and images for testing"""

self.image = Path("./images/zero.bin")
self.image = Path(images_dir + "/zero.bin")
self.image_signed = tmp_name(tmp_path_persistent, "image", ".signed")

@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -252,7 +250,7 @@ def test_sign_dependencies(self, tmp_path_persistent, dependencies, expected):
def test_sign_header_not_padded(self, tmp_path_persistent):
"""Test with un-padded header with non-zero start should fail"""

self.image = Path("./images/one.bin")
self.image = Path(images_dir + "/one.bin")

result = self.runner.invoke(
imgtool,
Expand Down Expand Up @@ -545,7 +543,7 @@ def test_sign_missing_args(self, key_type, tmp_path_persistent):
def test_sign_pub_key_format(self, key_type, tmp_path_persistent, key_format):
"""Test with pub key format"""

loaded_key = KEY_DIR + key_type + ".key"
loaded_key = keys_dir + key_type + ".key"

result = self.runner.invoke(
imgtool,
Expand Down Expand Up @@ -683,7 +681,7 @@ def test_sign_sig_out(self, tmp_path_persistent, key_type):
def test_sign_load_addr(self, key_type, tmp_path_persistent):
"""Test sign with load addr"""

loaded_key = KEY_DIR + key_type + ".key"
loaded_key = keys_dir + key_type + ".key"

result = self.runner.invoke(
imgtool,
Expand Down Expand Up @@ -712,7 +710,7 @@ def test_sign_load_addr(self, key_type, tmp_path_persistent):
def test_sign_load_addr_rom_fixed(self, key_type, tmp_path_persistent):
"""Test sign with both load addr and rom fixed should fail"""

loaded_key = KEY_DIR + key_type + ".key"
loaded_key = keys_dir + key_type + ".key"

result = self.runner.invoke(
imgtool,
Expand Down Expand Up @@ -1207,8 +1205,8 @@ class TestSignHex(TestSign):
def test_sign_basic_hex(self, key_type, tmp_path_persistent):
"""Test basic sign and verify hex file"""

loaded_key = KEY_DIR + key_type + ".key"
self.image = "./images/zero.hex"
loaded_key = keys_dir + key_type + ".key"
self.image = images_dir + "/zero.hex"
self.image_signed = tmp_name(tmp_path_persistent, "image", ".hex")

result = self.runner.invoke(
Expand Down Expand Up @@ -1238,7 +1236,7 @@ def test_sign_basic_hex(self, key_type, tmp_path_persistent):
def test_sign_hex_file_not_exists(self, key_type, tmp_path_persistent):
"""Test sign hex file with non-existent file"""

loaded_key = KEY_DIR + key_type + ".key"
loaded_key = keys_dir + key_type + ".key"
self.image = "./images/invalid/path/file.hex"
self.image_signed = tmp_name(tmp_path_persistent, "image", ".hex")

Expand Down Expand Up @@ -1270,8 +1268,8 @@ def test_sign_hex_file_not_exists(self, key_type, tmp_path_persistent):
def test_sign_hex_padded(self, key_type, tmp_path_persistent):
"""Test hex file with pad"""

loaded_key = KEY_DIR + key_type + ".key"
self.image = "./images/one_offset.hex"
loaded_key = keys_dir + key_type + ".key"
self.image = images_dir + "/one_offset.hex"
# image above generated as below:
# ih = IntelHex()
# ih.puts(10, '1' * 1024)
Expand Down Expand Up @@ -1307,8 +1305,8 @@ def test_sign_hex_padded(self, key_type, tmp_path_persistent):
def test_sign_hex_confirm(self, key_type, tmp_path_persistent):
"""Test hex file with confirm"""

loaded_key = KEY_DIR + key_type + ".key"
self.image = "./images/zero.hex"
loaded_key = keys_dir + key_type + ".key"
self.image = images_dir + "/zero.hex"
self.image_signed = tmp_name(tmp_path_persistent, "image", ".hex")

result = self.runner.invoke(
Expand Down Expand Up @@ -1339,8 +1337,8 @@ def test_sign_hex_confirm(self, key_type, tmp_path_persistent):
def test_sign_basic_hex_bad_image_size(self, key_type, tmp_path_persistent):
"""Test basic sign hex file with insufficient size should fail"""

loaded_key = KEY_DIR + key_type + ".key"
self.image = "./images/zero.hex"
loaded_key = keys_dir + key_type + ".key"
self.image = images_dir + "/zero.hex"
self.image_signed = tmp_name(tmp_path_persistent, "image", ".hex")

result = self.runner.invoke(
Expand Down
18 changes: 9 additions & 9 deletions scripts/tests/test_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from click.testing import CliRunner

from imgtool.main import imgtool
from tests.constants import KEY_TYPES, GEN_KEY_EXT, tmp_name, signed_images_dir
from tests.constants import KEY_TYPES, GEN_KEY_EXT, tmp_name, signed_images_dir, keys_dir

KEY_TYPE_MISMATCH_TLV = "Key type does not match TLV record"
NO_SIG_FOR_KEY = "No signature found for the given key"
Expand Down Expand Up @@ -51,7 +51,7 @@ class TestVerifyBasic(TestVerify):

@pytest.fixture(autouse=True)
def setup(self, request, tmp_path_persistent, key_type):
self.key = "./keys/" + key_type + ".key"
self.key = keys_dir + key_type + ".key"
self.gen_key = tmp_name(tmp_path_persistent, key_type, GEN_KEY_EXT)
self.runner.invoke(
imgtool, ["keygen", "--key", str(self.gen_key), "--type", key_type]
Expand Down Expand Up @@ -191,7 +191,7 @@ class TestVerifyEncryptedClear(TestVerify):

@pytest.fixture(autouse=True)
def setup(self, request, tmp_path_persistent, key_type):
self.key = "./keys/" + key_type + ".key"
self.key = keys_dir + key_type + ".key"
self.gen_key = tmp_name(tmp_path_persistent, key_type, GEN_KEY_EXT)
self.runner.invoke(
imgtool, ["keygen", "--key", str(self.gen_key), "--type", key_type]
Expand Down Expand Up @@ -240,7 +240,7 @@ class TestVerifyCustomTLV(TestVerify):

@pytest.fixture(autouse=True)
def setup(self, request, tmp_path_persistent, key_type):
self.key = "./keys/" + key_type + ".key"
self.key = keys_dir + key_type + ".key"
self.gen_key = tmp_name(tmp_path_persistent, key_type, GEN_KEY_EXT)
self.runner.invoke(
imgtool, ["keygen", "--key", str(self.gen_key), "--type", key_type]
Expand Down Expand Up @@ -301,7 +301,7 @@ def test_verify_no_key(self):
def test_verify_no_key_image_with_key(self):
"""Test verify image signed without key, attempt to verify with a key should fail on signature check"""

self.key = "./keys/" + "rsa-2048" + ".key"
self.key = keys_dir + "rsa-2048" + ".key"
self.image_signed = self.test_signed_images_dir + "noKey256" + ".bin"

result = self.runner.invoke(
Expand All @@ -319,7 +319,7 @@ def test_verify_no_key_image_with_key(self):
def test_verify_no_key_image_with_wrong_key(self):
"""Test verify image signed without key, attempt to verify with wrong key should fail on hash check"""

self.key = "./keys/" + "ecdsa-p384" + ".key"
self.key = keys_dir + "ecdsa-p384" + ".key"
self.image_signed = self.test_signed_images_dir + "noKey256" + ".bin"

result = self.runner.invoke(
Expand All @@ -343,7 +343,7 @@ class TestVerifyPubKey(TestVerify):

@pytest.fixture(autouse=True)
def setup(self, request, tmp_path_persistent, key_type):
self.key = "./keys/" + key_type + ".key"
self.key = keys_dir + key_type + ".key"
self.gen_key = tmp_name(tmp_path_persistent, key_type, GEN_KEY_EXT)
self.runner.invoke(
imgtool, ["keygen", "--key", str(self.gen_key), "--type", key_type]
Expand Down Expand Up @@ -383,7 +383,7 @@ def test_verify_384_key(self, key_type):
def test_verify_key_not_matching(self, key_type, tmp_path_persistent):
"""Test verify image with mismatching key """

self.key = "./keys/" + "ecdsa-p384" + ".key"
self.key = keys_dir + "ecdsa-p384" + ".key"
self.image_signed = self.test_signed_images_dir + key_type + ".bin"

result = self.runner.invoke(
Expand Down Expand Up @@ -423,7 +423,7 @@ class TestVerifyHex(TestVerify):

@pytest.fixture(autouse=True)
def setup(self, request, tmp_path_persistent, key_type="rsa-2048"):
self.key = "./keys/" + key_type + ".key"
self.key = keys_dir + key_type + ".key"

@pytest.mark.parametrize("hex_addr", ("0", "16", "35"))
def test_verify_basic(self, hex_addr, tmp_path_persistent):
Expand Down

0 comments on commit 6388c78

Please sign in to comment.