99"""
1010
1111import os
12+ from pathlib import Path
1213import stat
1314import tempfile
14- from pathlib import Path
1515
1616import pytest
1717
1818from libp2p .crypto .ed25519 import create_new_key_pair
1919from libp2p .identity_utils import load_identity , save_identity
2020
2121
22- def test_save_identity_sets_restrictive_permissions ():
22+ def test_save_identity_sets_restrictive_permissions () -> None :
2323 """
2424 Verify that saved identity files have restrictive permissions (0600).
25-
25+
2626 This is a security requirement to prevent other users from reading
2727 the private key file.
2828 """
2929 with tempfile .TemporaryDirectory () as tmpdir :
3030 filepath = Path (tmpdir ) / "test_identity.key"
3131 key_pair = create_new_key_pair ()
3232 save_identity (key_pair , filepath )
33-
33+
3434 # Check file permissions (Unix-like systems only)
3535 if os .name != "nt" : # Skip on Windows
3636 mode = filepath .stat ().st_mode
@@ -41,102 +41,97 @@ def test_save_identity_sets_restrictive_permissions():
4141 )
4242
4343
44- def test_load_corrupted_identity_raises_error ():
44+ def test_load_corrupted_identity_raises_error () -> None :
4545 """
4646 Verify that loading a corrupted identity file raises ValueError.
47-
47+
4848 This ensures we don't silently accept invalid key data.
4949 """
5050 with tempfile .TemporaryDirectory () as tmpdir :
5151 filepath = Path (tmpdir ) / "corrupted.key"
5252 # Write invalid data
5353 filepath .write_bytes (b"not a valid private key" )
54-
54+
5555 with pytest .raises (ValueError , match = "Invalid or corrupted" ):
5656 load_identity (filepath )
5757
5858
59- def test_load_truncated_file_raises_error ():
59+ def test_load_truncated_file_raises_error () -> None :
6060 """
6161 Verify that loading a truncated file raises ValueError.
62-
62+
6363 Truncated files could result from interrupted writes.
6464 """
6565 with tempfile .TemporaryDirectory () as tmpdir :
6666 filepath = Path (tmpdir ) / "truncated.key"
6767 # Write only a few bytes (truncated protobuf)
6868 filepath .write_bytes (b"\x00 \x01 \x02 " )
69-
69+
7070 with pytest .raises (ValueError , match = "Invalid or corrupted" ):
7171 load_identity (filepath )
7272
7373
74- def test_load_empty_file_raises_error ():
74+ def test_load_empty_file_raises_error () -> None :
7575 """
7676 Verify that loading an empty file raises ValueError.
7777 """
7878 with tempfile .TemporaryDirectory () as tmpdir :
7979 filepath = Path (tmpdir ) / "empty.key"
8080 # Write empty file
8181 filepath .write_bytes (b"" )
82-
82+
8383 with pytest .raises (ValueError , match = "Invalid or corrupted" ):
8484 load_identity (filepath )
8585
8686
87- def test_save_and_load_rsa_identity () :
87+ def test_overwrite_existing_identity () -> None :
8888 """
8989 Test that saving to an existing file overwrites it correctly.
90-
90+
9191 This ensures we can update identity files without errors.
9292 """
9393 with tempfile .TemporaryDirectory () as tmpdir :
9494 filepath = Path (tmpdir ) / "identity.key"
95-
95+
9696 # Save first identity
9797 key_pair_1 = create_new_key_pair ()
9898 save_identity (key_pair_1 , filepath )
99-
99+
100100 # Overwrite with second identity
101101 key_pair_2 = create_new_key_pair ()
102102 save_identity (key_pair_2 , filepath )
103-
103+
104104 # Load and verify it's the second identity
105105 loaded_key_pair = load_identity (filepath )
106106 assert (
107- loaded_key_pair .private_key .to_bytes ()
108- == key_pair_2 .private_key .to_bytes ()
107+ loaded_key_pair .private_key .to_bytes () == key_pair_2 .private_key .to_bytes ()
109108 )
110109 assert (
111- loaded_key_pair .private_key .to_bytes ()
112- != key_pair_1 .private_key .to_bytes ()
110+ loaded_key_pair .private_key .to_bytes () != key_pair_1 .private_key .to_bytes ()
113111 )
114112
115113
116- def test_save_identity_creates_parent_directories ():
114+ def test_save_identity_creates_parent_directories () -> None :
117115 """
118116 Test that save_identity creates parent directories if they don't exist.
119-
117+
120118 This prevents FileNotFoundError when saving to nested paths.
121119 """
122120 with tempfile .TemporaryDirectory () as tmpdir :
123- # Use a nested path that doesn't exist
121+ # Use a nested path that doesn't exis
124122 filepath = Path (tmpdir ) / "nested" / "dir" / "identity.key"
125-
126- # Parent directories don't exist yet
123+
124+ # Parent directories don't exist ye
127125 assert not filepath .parent .exists ()
128-
126+
129127 # Save should create them
130128 key_pair = create_new_key_pair ()
131129 save_identity (key_pair , filepath )
132-
130+
133131 # Verify file was created
134132 assert filepath .exists ()
135133 assert filepath .parent .exists ()
136-
137- # Verify we can load it
134+
135+ # Verify we can load i
138136 loaded_key_pair = load_identity (filepath )
139- assert (
140- loaded_key_pair .private_key .to_bytes ()
141- == key_pair .private_key .to_bytes ()
142- )
137+ assert loaded_key_pair .private_key .to_bytes () == key_pair .private_key .to_bytes ()
0 commit comments