-
Notifications
You must be signed in to change notification settings - Fork 70
Added functional tests for SSL/TLS #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Davidson-Souza
merged 1 commit into
vinteumorg:master
from
qlrd:test-functional-florestad-ssl
Mar 24, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| """ | ||
| florestad/ssl-fail-test.py | ||
|
|
||
| This functional test checks the failure on connect to florestad's TLS port. | ||
| """ | ||
|
|
||
| import errno | ||
|
|
||
| from test_framework.electrum_client import ElectrumClient | ||
| from test_framework.floresta_rpc import REGTEST_RPC_SERVER | ||
| from test_framework.test_framework import FlorestaTestFramework | ||
|
|
||
|
|
||
| class TestSslFailInitialization(FlorestaTestFramework): | ||
| """ | ||
| Test the initialization of florestad without --ssl-key-path and --ssl-cert-path | ||
| (and without proper keys), a request from Electrum client to TLS port and its failure. | ||
| """ | ||
|
|
||
| nodes = [-1] | ||
| electrum = None | ||
|
|
||
| def set_test_params(self): | ||
| """ | ||
| Setup a single node without SSL | ||
| """ | ||
| TestSslFailInitialization.nodes[0] = self.add_node_settings( | ||
| chain="regtest", extra_args=[], rpcserver=REGTEST_RPC_SERVER, ssl=False | ||
| ) | ||
|
|
||
| def run_test(self): | ||
| """ | ||
| Run the no-ssl node, create an electrum client that will try to connect to port 50002, | ||
| and assert a connection refused failure. | ||
| """ | ||
| self.run_node(TestSslFailInitialization.nodes[0]) | ||
| self.wait_for_rpc_connection(TestSslFailInitialization.nodes[0]) | ||
|
|
||
| # now try create a connection with an electrum client at default port | ||
| # it must fail, since the TLS port isnt opened | ||
| try: | ||
| TestSslFailInitialization.electrum = ElectrumClient("0.0.0.0", 50002) | ||
|
|
||
| except ConnectionRefusedError as exc: | ||
| assert exc.errno == errno.ECONNREFUSED | ||
|
|
||
| # Shutdown node | ||
| self.stop_node(TestSslFailInitialization.nodes[0]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| TestSslFailInitialization().main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| """ | ||
| florestad/ssl-test.py | ||
|
|
||
| This functional test tests the proper creatiion of a TLS port on florestad. | ||
| """ | ||
|
|
||
| from test_framework.electrum_client import ElectrumClient | ||
| from test_framework.floresta_rpc import REGTEST_RPC_SERVER | ||
| from test_framework.test_framework import FlorestaTestFramework | ||
|
|
||
|
|
||
| class TestSslInitialization(FlorestaTestFramework): | ||
| """ | ||
| Test the initialization of florestad with --ssl-key-path and --ssl-cert-path and | ||
| a request from Electrum client to TLS port and its success. | ||
| """ | ||
|
|
||
| nodes = [-1] | ||
| electrum = None | ||
|
|
||
| def set_test_params(self): | ||
| """ | ||
| Setup a single node and a electrum client at port 50002 | ||
| """ | ||
| TestSslInitialization.nodes[0] = self.add_node_settings( | ||
| chain="regtest", extra_args=[], rpcserver=REGTEST_RPC_SERVER, ssl=True | ||
| ) | ||
|
|
||
| def run_test(self): | ||
| """ | ||
| Run the ssl node, create a electrum client that will try to connect to port 50002. | ||
| Send a ping to make sure everything is working. | ||
| """ | ||
| self.run_node(TestSslInitialization.nodes[0]) | ||
| self.wait_for_rpc_connection(TestSslInitialization.nodes[0]) | ||
|
|
||
| # now create a connection with an electrum client at default port | ||
| TestSslInitialization.electrum = ElectrumClient("0.0.0.0", 50002) | ||
|
|
||
| # request something to TLS port | ||
| result = TestSslInitialization.electrum.ping() | ||
| self.log(result) | ||
|
|
||
| assert result is not None | ||
|
|
||
| # Shutdown node | ||
| self.stop_node(TestSslInitialization.nodes[0]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| TestSslInitialization().main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| """ | ||
| tests/test_framework/crypto/pkcs8.py | ||
|
|
||
| This module generate proper PKCS#8 private keys and certificate | ||
| """ | ||
|
|
||
| import os | ||
| from datetime import datetime, timedelta | ||
| from typing import Tuple | ||
|
|
||
| from cryptography import x509 | ||
| from cryptography.hazmat.primitives import hashes, serialization | ||
| from cryptography.hazmat.primitives.asymmetric import rsa | ||
| from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey | ||
| from cryptography.x509.oid import NameOID | ||
|
|
||
| DEFAULT_PUBLIC_EXPONENT = 65537 | ||
| DEFAULT_KEY_SIZE = 2048 | ||
| DEFAULT_CN = "vinteumorg" | ||
| DEFAULT_DAYS = 1 | ||
|
|
||
|
|
||
| def create_pkcs8_private_key( | ||
| path: str, | ||
| public_exponent: int = DEFAULT_PUBLIC_EXPONENT, | ||
| key_size: int = DEFAULT_KEY_SIZE, | ||
| ) -> Tuple[str, RSAPrivateKey]: | ||
| """ | ||
| Generate private key in a proper format PKCS#8 | ||
| """ | ||
| pk = rsa.generate_private_key(public_exponent=public_exponent, key_size=key_size) | ||
|
|
||
| # Serialize and save key | ||
| pem = pk.private_bytes( | ||
| encoding=serialization.Encoding.PEM, | ||
| format=serialization.PrivateFormat.PKCS8, | ||
| encryption_algorithm=serialization.NoEncryption(), | ||
| ) | ||
| pk_path = os.path.join(path, "key.pem") | ||
| with open(pk_path, "wb") as f: | ||
| f.write(pem) | ||
|
|
||
| return (pk_path, pk) | ||
|
|
||
|
|
||
| def create_pkcs8_self_signed_certificate( | ||
| path: str, | ||
| pk: RSAPrivateKey, | ||
| common_name: str = DEFAULT_CN, | ||
| validity_days: int = DEFAULT_DAYS, | ||
| ) -> str: | ||
| """ | ||
| Generate a self signed certificate in a proper format PKCS#8 | ||
| """ | ||
| # Create subject/issuer name | ||
| subject = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, common_name)]) | ||
|
|
||
| # Certificate validity period | ||
| now = datetime.utcnow() | ||
| validity = timedelta(days=validity_days) | ||
|
|
||
| # Build and sign certificate | ||
| cert = ( | ||
| x509.CertificateBuilder() | ||
| .subject_name(subject) | ||
| .issuer_name(subject) | ||
| .public_key(pk.public_key()) | ||
| .serial_number(x509.random_serial_number()) | ||
| .not_valid_before(now) | ||
| .not_valid_after(now + validity) | ||
| .sign(pk, hashes.SHA256()) | ||
| ) | ||
|
|
||
| # Save certificate | ||
| cert_path = os.path.join(path, "cert.pem") | ||
| with open(cert_path, "wb") as f: | ||
| f.write(cert.public_bytes(serialization.Encoding.PEM)) | ||
|
|
||
| return cert_path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.