Skip to content

Commit 1c6c318

Browse files
committed
add tests
1 parent 1c34618 commit 1c6c318

File tree

3 files changed

+124
-2
lines changed

3 files changed

+124
-2
lines changed

poetry.lock

Lines changed: 56 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ filelock = "^3.0.12"
1919
pytest-xdist = "^2.1.0"
2020
pytest_httpserver = "^0.3.5"
2121
GitPython = "^3.1.12"
22+
ecdsa = "^0.17.0"
2223

2324
[tool.poetry.dev-dependencies]
2425
mkdocs = "^1.2.1"

test/test_keys.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# This file is part of arduino-cli.
2+
#
3+
# Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
#
5+
# This software is released under the GNU General Public License version 3,
6+
# which covers the main part of arduino-cli.
7+
# The terms of this license can be found at:
8+
# https://www.gnu.org/licenses/gpl-3.0.en.html
9+
#
10+
# You can be released from the requirements of the above licenses by purchasing
11+
# a commercial license. Buying such a license is mandatory if you want to modify or
12+
# otherwise use the software for commercial activities involving the Arduino
13+
# software without disclosing the source code of your own applications. To purchase
14+
# a commercial license, send an email to [email protected].
15+
16+
from ecdsa import VerifyingKey, SigningKey
17+
from pathlib import Path
18+
19+
20+
def test_keys_generate(run_command, working_dir):
21+
# Create security keys without specifying the keychain dir (by default in the working directory)
22+
sign_key_name = "ecdsa-p256-signing-key.pem"
23+
sign_header_name = "ecdsa-p256-signing-key.h"
24+
result = run_command(["keys", "generate", "--key-name", sign_key_name])
25+
assert result.ok
26+
assert f"Keys created in: {working_dir}" in result.stdout
27+
assert Path(working_dir, f"pub_{sign_key_name}").is_file()
28+
assert Path(working_dir, f"priv_{sign_key_name}").is_file()
29+
assert Path(working_dir, f"pub_{sign_header_name}").is_file()
30+
assert Path(working_dir, f"priv_{sign_header_name}").is_file()
31+
32+
# Overwrite security keys
33+
result = run_command(["keys", "generate", "--key-name", sign_key_name])
34+
assert result.failed
35+
assert f"Error during Generate: Cannot create file: File already exists: {working_dir}" in result.stderr
36+
37+
# Create security keys in specified directory
38+
keychain_name = "keychain"
39+
keychain_path = Path(working_dir, keychain_name)
40+
result = run_command(["keys", "generate", "--key-name", sign_key_name, "--keys-keychain", keychain_path])
41+
assert result.ok
42+
assert f"Keys created in: {keychain_path}" in result.stdout
43+
assert Path(keychain_path, f"pub_{sign_key_name}").is_file()
44+
assert Path(keychain_path, f"priv_{sign_key_name}").is_file()
45+
assert Path(keychain_path, f"pub_{sign_header_name}").is_file()
46+
assert Path(keychain_path, f"priv_{sign_header_name}").is_file()
47+
48+
# verify that keypar is valid by signing a message and then verify it
49+
with open(f"{keychain_path}/pub_{sign_key_name}") as f:
50+
vk = VerifyingKey.from_pem(f.read())
51+
with open(f"{keychain_path}/priv_{sign_key_name}") as f1:
52+
sk = SigningKey.from_pem(f1.read())
53+
54+
signature = sk.sign(b"message")
55+
assert vk.verify(signature, b"message")
56+
57+
# Create security keys without specifying --key-name
58+
result = run_command(["keys", "generate", "--keys-keychain", keychain_path])
59+
assert result.failed
60+
assert 'Error: required flag(s) "key-name" not set' in result.stderr
61+
62+
# Create security keys with unsupported algorithm
63+
result = run_command(
64+
["keys", "generate", "--key-name", sign_key_name, "--keys-keychain", keychain_path, "-t", "rsa"]
65+
)
66+
assert result.failed
67+
assert "Error during Generate: Cannot create file: Unsupported algorithm: rsa" in result.stderr

0 commit comments

Comments
 (0)