]> git.ipfire.org Git - pakfire.git/blame - tests/python/keys.py
tests: Add functions to make opening files in the test env easier
[pakfire.git] / tests / python / keys.py
CommitLineData
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
e1b05e21 22import pakfire
e1b05e21 23
7d6ca8df
MT
24import tests
25
26class KeysTests(tests.TestCase):
e1b05e21
MT
27 """
28 This tests the keys
29 """
30 def setUp(self):
7d6ca8df 31 self.pakfire = self.setup_pakfire()
e1b05e21
MT
32
33 def test_generate(self):
34 """
35 Generate a new key
36 """
391eeca0
MT
37 key = self.pakfire.generate_key(
38 algorithm=pakfire.PAKFIRE_KEY_ALGO_ED25519, comment="Key 1")
e1b05e21
MT
39
40 # Check if we got the correct type
41 self.assertIsInstance(key, pakfire.Key)
42
43 # Check that the ID is in integer
44 self.assertIsInstance(key.id, int)
45
46 # Check that the algorithm matches
47 self.assertEqual(key.algorithm, "Ed255919")
48
44cbdc0a 49 def _import(self, path):
c7c92efb 50 with self.open(path) as f:
6b88067b
MT
51 payload = f.read()
52
53 return self.pakfire.import_key(payload)
44cbdc0a 54
5d30390b 55 def test_import_public_key(self):
44cbdc0a
MT
56 # Import a public key
57 key = self._import("keys/key1.pub")
58
5d30390b
MT
59 # Check for the correct key ID
60 self.assertEqual(key.id, 13863674484496905947)
61
62 def test_import_secret_key(self):
44cbdc0a
MT
63 # Import a secret key
64 key = self._import("keys/key1.sec")
65
5d30390b
MT
66 # Check for the correct key ID
67 self.assertEqual(key.id, 13863674484496905947)
68
28ff41a8
MT
69 def test_sign(self):
70 """
71 Generate a new key
72 """
391eeca0 73 key = self.pakfire.generate_key(
6b88067b 74 algorithm=pakfire.PAKFIRE_KEY_ALGO_ED25519, comment="Key 1")
28ff41a8
MT
75
76 data = b"Pakfire"
77
78 # Sign!
79 signature = key.sign(data, comment="This is a comment")
80
81 # Verify!
82 self.assertTrue(key.verify(signature, data))
83
e1b05e21
MT
84
85if __name__ == "__main__":
7d6ca8df 86 tests.main()