1
+ import pytest
2
+ import tempfile
1
3
2
- def test_io_read_ssh_file ():
3
- ...
4
+ from cryptography .hazmat .primitives .asymmetric import ed25519
5
+ from cryptography .hazmat .primitives import serialization
6
+
7
+ from synlink .crypto .io import load_open_ssh_private_key
8
+ from synlink .crypto .ed25519 import PrivateKey
9
+
10
+
11
+
12
+ def _create_tempfile (buffer : bytes ) -> tempfile ._TemporaryFileWrapper :
13
+ """create a test ssh-keygen tempory file."""
14
+ file = tempfile .NamedTemporaryFile ()
15
+ try :
16
+ _ = file .write (buffer )
17
+ _ = file .flush ()
18
+ except Exception as e :
19
+ file .close ()
20
+ raise
21
+
22
+ return file
23
+
24
+
25
+ @pytest .fixture
26
+ def file_buffer () -> tempfile ._TemporaryFileWrapper :
27
+ """Generate a OpenSSH key."""
28
+ # Generate ed25519 private key.
29
+ private_key = ed25519 .Ed25519PrivateKey .generate ()
30
+ buffer = private_key .private_bytes (
31
+ encoding = serialization .Encoding .PEM ,
32
+ format = serialization .PrivateFormat .OpenSSH ,
33
+ encryption_algorithm = serialization .BestAvailableEncryption (
34
+ b"hello-world"
35
+ )
36
+ )
37
+
38
+ return _create_tempfile (buffer )
39
+
40
+ @pytest .fixture
41
+ def invalid_file_buffer () -> tempfile ._TemporaryFileWrapper :
42
+ # Generate ed25519 private key.
43
+ private_key = ed25519 .Ed25519PrivateKey .generate ()
44
+ buffer = private_key .private_bytes (
45
+ encoding = serialization .Encoding .Raw ,
46
+ format = serialization .PrivateFormat .Raw ,
47
+ encryption_algorithm = serialization .NoEncryption ()
48
+ )
49
+
50
+ return _create_tempfile (buffer )
51
+
52
+
53
+
54
+ def test_io_read_ssh_file (file_buffer : tempfile ._TemporaryFileWrapper ):
55
+ try :
56
+ _ = file_buffer
57
+ _keypair = load_open_ssh_private_key (
58
+ _ .name ,
59
+ password = "hello-world"
60
+ )
61
+ # finally close the file
62
+ finally :
63
+ _ .close ()
64
+
65
+
66
+
67
+ def test_io_invalid_read_ssh_file (invalid_file_buffer : tempfile ._TemporaryFileWrapper ):
68
+ _ = invalid_file_buffer
69
+
70
+ with pytest .raises (Exception ):
71
+ _keypair = load_open_ssh_private_key (
72
+ _ .name ,
73
+ password = "hello-world"
74
+ )
75
+
76
+ _ .close ()
77
+
78
+
79
+ def test_io_invalid_key_read_ssh_file (file_buffer : tempfile ._TemporaryFileWrapper ):
80
+ _ = file_buffer
81
+
82
+ with pytest .raises (Exception ):
83
+ _keypair = load_open_ssh_private_key (
84
+ _ .name ,
85
+ password = "hello"
86
+ )
87
+
88
+ _ .close ()
0 commit comments