]>
Commit | Line | Data |
---|---|---|
e1b05e21 MT |
1 | #!/usr/bin/python3 |
2 | ############################################################################### | |
3 | # # | |
4 | # Pakfire - The IPFire package management system # | |
5 | # Copyright (C) 2023 Pakfire development team # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
22 | import os | |
23 | import pakfire | |
24 | import unittest | |
25 | ||
26 | class KeysTests(unittest.TestCase): | |
27 | """ | |
28 | This tests the keys | |
29 | """ | |
30 | def setUp(self): | |
31 | path = os.environ.get("TEST_STUB_ROOT") | |
32 | if not path: | |
33 | raise RuntimeError("TEST_STUB_ROOT is not defined") | |
34 | ||
35 | self.pakfire = pakfire.Pakfire(path) | |
36 | ||
37 | def test_generate(self): | |
38 | """ | |
39 | Generate a new key | |
40 | """ | |
391eeca0 MT |
41 | key = self.pakfire.generate_key( |
42 | algorithm=pakfire.PAKFIRE_KEY_ALGO_ED25519, comment="Key 1") | |
e1b05e21 MT |
43 | |
44 | # Check if we got the correct type | |
45 | self.assertIsInstance(key, pakfire.Key) | |
46 | ||
47 | # Check that the ID is in integer | |
48 | self.assertIsInstance(key.id, int) | |
49 | ||
50 | # Check that the algorithm matches | |
51 | self.assertEqual(key.algorithm, "Ed255919") | |
52 | ||
44cbdc0a MT |
53 | def _import(self, path): |
54 | path = os.path.join( | |
55 | os.environ.get("TEST_DATA_DIR"), path, | |
56 | ) | |
57 | ||
58 | with open(path, "rb") as f: | |
6b88067b MT |
59 | payload = f.read() |
60 | ||
61 | return self.pakfire.import_key(payload) | |
44cbdc0a | 62 | |
5d30390b | 63 | def test_import_public_key(self): |
44cbdc0a MT |
64 | # Import a public key |
65 | key = self._import("keys/key1.pub") | |
66 | ||
5d30390b MT |
67 | # Check for the correct key ID |
68 | self.assertEqual(key.id, 13863674484496905947) | |
69 | ||
70 | def test_import_secret_key(self): | |
44cbdc0a MT |
71 | # Import a secret key |
72 | key = self._import("keys/key1.sec") | |
73 | ||
5d30390b MT |
74 | # Check for the correct key ID |
75 | self.assertEqual(key.id, 13863674484496905947) | |
76 | ||
28ff41a8 MT |
77 | def test_sign(self): |
78 | """ | |
79 | Generate a new key | |
80 | """ | |
391eeca0 | 81 | key = self.pakfire.generate_key( |
6b88067b | 82 | algorithm=pakfire.PAKFIRE_KEY_ALGO_ED25519, comment="Key 1") |
28ff41a8 MT |
83 | |
84 | data = b"Pakfire" | |
85 | ||
86 | # Sign! | |
87 | signature = key.sign(data, comment="This is a comment") | |
88 | ||
89 | # Verify! | |
90 | self.assertTrue(key.verify(signature, data)) | |
91 | ||
e1b05e21 MT |
92 | |
93 | if __name__ == "__main__": | |
94 | unittest.main() |