2023-11-01 04:57:08 +00:00
|
|
|
"""Test for utility functions"""
|
|
|
|
|
2017-08-05 10:24:46 +00:00
|
|
|
from base64 import b64decode
|
|
|
|
from unittest.mock import mock_open, patch
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2023-11-01 06:16:17 +00:00
|
|
|
from ssb.util import ConfigException, load_ssb_secret
|
2017-08-05 10:24:46 +00:00
|
|
|
|
|
|
|
CONFIG_FILE = """
|
|
|
|
## Comments should be supported too
|
|
|
|
{
|
|
|
|
"curve": "ed25519",
|
|
|
|
"public": "rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=ed25519",
|
|
|
|
"private": "/bqDBI/vGLD5qy3GxMsgHFgYIrrY08JfTzUaCYT6x0GuxikEhxezGNAB/Qk16z4wepPYMv4R+ilYoCnisZ4Q9A==",
|
|
|
|
"id": "@rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=.ed25519"
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
CONFIG_FILE_INVALID = CONFIG_FILE.replace("ed25519", "foo")
|
2017-08-05 10:24:46 +00:00
|
|
|
|
|
|
|
|
2023-11-01 06:22:29 +00:00
|
|
|
def test_load_secret() -> None:
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Test loading the SSB secret from a file"""
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
with patch("ssb.util.open", mock_open(read_data=CONFIG_FILE), create=True):
|
2017-08-05 10:24:46 +00:00
|
|
|
secret = load_ssb_secret()
|
|
|
|
|
|
|
|
priv_key = b'\xfd\xba\x83\x04\x8f\xef\x18\xb0\xf9\xab-\xc6\xc4\xcb \x1cX\x18"\xba\xd8\xd3\xc2_O5\x1a\t\x84\xfa\xc7A'
|
|
|
|
|
2023-11-01 04:04:43 +00:00
|
|
|
assert secret["id"] == "@rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=.ed25519"
|
|
|
|
assert bytes(secret["keypair"]) == priv_key
|
|
|
|
assert bytes(secret["keypair"].verify_key) == b64decode("rsYpBIcXsxjQAf0JNes+MHqT2DL+EfopWKAp4rGeEPQ=")
|
2017-08-05 10:24:46 +00:00
|
|
|
|
|
|
|
|
2023-11-01 06:22:29 +00:00
|
|
|
def test_load_exception() -> None:
|
2023-11-01 04:57:08 +00:00
|
|
|
"""Test configuration loading if there is a problem with the file"""
|
2023-11-01 06:22:29 +00:00
|
|
|
|
2017-08-05 10:24:46 +00:00
|
|
|
with pytest.raises(ConfigException):
|
2023-11-01 04:04:43 +00:00
|
|
|
with patch("ssb.util.open", mock_open(read_data=CONFIG_FILE_INVALID), create=True):
|
2017-08-05 10:24:46 +00:00
|
|
|
load_ssb_secret()
|