c7170abb9371e254b78977e5aff382b3ad11b59e
[keymanager.git] / src / leap / keymanager / tests / test_validation.py
1 # -*- coding: utf-8 -*-
2 # __init__.py
3 # Copyright (C) 2014 LEAP
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 """
18 Tests for the Validation Levels
19 """
20
21 from leap.keymanager.openpgp import OpenPGPKey
22 from leap.keymanager.errors import (
23     KeyNotValidUpgrade
24 )
25 from leap.keymanager.tests import (
26     KeyManagerWithSoledadTestCase,
27     ADDRESS,
28     PUBLIC_KEY,
29     KEY_FINGERPRINT
30 )
31 from leap.keymanager.validation import ValidationLevel
32
33
34 class ValidationLevelTestCase(KeyManagerWithSoledadTestCase):
35
36     def test_none_old_key(self):
37         km = self._key_manager()
38         km.put_raw_key(PUBLIC_KEY, OpenPGPKey)
39         key = km.get_key(ADDRESS, OpenPGPKey, fetch_remote=False)
40         self.assertEqual(key.fingerprint, KEY_FINGERPRINT)
41
42     def test_cant_upgrade(self):
43         km = self._key_manager()
44         km.put_raw_key(PUBLIC_KEY, OpenPGPKey,
45                        validation=ValidationLevel.Provider_Trust)
46         self.assertRaises(KeyNotValidUpgrade, km.put_raw_key, UNRELATED_KEY,
47                           OpenPGPKey)
48
49     def test_fingerprint_level(self):
50         km = self._key_manager()
51         km.put_raw_key(PUBLIC_KEY, OpenPGPKey)
52         km.put_raw_key(UNRELATED_KEY, OpenPGPKey,
53                        validation=ValidationLevel.Fingerprint)
54         key = km.get_key(ADDRESS, OpenPGPKey, fetch_remote=False)
55         self.assertEqual(key.fingerprint, UNRELATED_FINGERPRINT)
56
57     def test_expired_key(self):
58         km = self._key_manager()
59         km.put_raw_key(EXPIRED_KEY, OpenPGPKey)
60         km.put_raw_key(UNRELATED_KEY, OpenPGPKey)
61         key = km.get_key(ADDRESS, OpenPGPKey, fetch_remote=False)
62         self.assertEqual(key.fingerprint, UNRELATED_FINGERPRINT)
63
64     def test_expired_fail_lower_level(self):
65         km = self._key_manager()
66         km.put_raw_key(EXPIRED_KEY, OpenPGPKey,
67                        validation=ValidationLevel.Third_Party_Endorsement)
68         self.assertRaises(
69             KeyNotValidUpgrade,
70             km.put_raw_key,
71             UNRELATED_KEY,
72             OpenPGPKey,
73             validation=ValidationLevel.Provider_Trust)
74
75
76 # Key material for testing
77
78 # key 901FBCA5: public key "Leap Test Key <leap@leap.se>"
79 UNRELATED_FINGERPRINT = "ABCCD9C8270B6A8D5633FAC9D04DB2E4901FBCA5"
80 UNRELATED_KEY = """
81 -----BEGIN PGP PUBLIC KEY BLOCK-----
82 Version: GnuPG v1
83
84 mQENBFQ9VDoBCACbKflcEhUXZULOT4Fwc2ifRUllJpusd2uX5oeDlZdZ15uLY2eF
85 LcxnAdIWkI/PsXimh0ev/Pf4oCynfmt02I3c2d9F0N6JXWnRiP+p098oPOcqeEqL
86 N3CrkH1RVnEXNeJ/Fu7tkD61SBXl1MytMfcHyhN5arg8OcVAjcmghX53+92jFhC9
87 8ss87H/qEe5vEX/ahP3tiL5ULvaS4GIX+XB0O3yCVdRoRG9lqMIBP/ZqCkKrNll8
88 dT12a6ByG/rWharZUeUETiM4Y+JjDUUaEC2YhNF9k52JNGanLH9LTTtlKy5WTT+E
89 C6T6VMAtkwcBDpkXr5sBB/N+Y1z0Fp359lIXABEBAAG0HExlYXAgVGVzdCBLZXkg
90 PGxlYXBAbGVhcC5zZT6JATgEEwECACIFAlQ9VDoCGwMGCwkIBwMCBhUIAgkKCwQW
91 AgMBAh4BAheAAAoJENBNsuSQH7ylsSUIAIxUFbkeTdHbCF/LVA2U+ktnR1iVikAY
92 vFK+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DJE7
109 =FO7G
110 -----END PGP PUBLIC KEY BLOCK-----
111 """
112
113 # key A1885A7C: public key "Leap Test Key <leap@leap.se>"
114 EXPIRED_FINGERPRINT = "7C1F68B0E14157B09B5F4ADE6F15F004A1885A7C"
115 EXPIRED_KEY = """
116 -----BEGIN PGP PUBLIC KEY BLOCK-----
117 Version: GnuPG v1.4.12 (GNU/Linux)
118
119 mQENBBvrfd0BCADGNpspaNhsbhSjKioCWrE2MTTYC+Sdpes22RabdhQyOCWvlSbj
120 b8p0y3kmnMOtVBT+c22/w7eu2YBfIpS4RswgE5ypr/1kZLFQueVe/cp29GjPvLwJ
121 82A3EOHcmXs8rSJ76h2bnkySvbJawz9rwCcaXhpdAwC+sjWvbqiwZYEL+90I4Xp3
122 acDh9vNtPxDCg5RdI0bfdIEBGgHTfsda3kWGvo1wH5SgrTRq0+EcTI7aJgkMmM/A
123 IhnpACE52NvGdG9eB3x7xyQFsQqK8F0XvEev2UJH4SR7vb+Z7FNTJKCy6likYbSV
124 wGGFuowFSESnzXuUI6PcjyuO6FUbMgeM5euFABEBAAG0HExlYXAgVGVzdCBLZXkg
125 PGxlYXBAbGVhcC5zZT6JAT4EEwECACgFAhvrfd0CGwMFCQABUYAGCwkIBwMCBhUI
126 AgkKCwQWAgMBAh4BAheAAAoJEG8V8AShiFp8VNkH/iCQcXkTfMOVlL2rQRyZtJEO
127 Lr5uTyyY8O6ubeNCHqZzlIopiPAsv4hIYjjMDvOfZ9R53YgmbacUm0rvh1B4MSUf
128 k+sa9/tequ3y44LUKp7AB6NyyLgVOU5ngl2w+bi7CgXAep3oP4joYKcU0mmSAc2S
129 2Gj85DVqP0kdzNs47esvyj7g1TOfdBwmLsTx/219H+w3dNBeyCQWkYCYNh7MX/Ba
130 SZ+P0xr4FetcOVPM3wAzUtDG7hKsgccoIXt0FWhG/nn8cETfGH+o3W/ky7Jktatx
131 DGDHoZJvAaG2B2ey1pAQlezr8p/O+ZVABiigHk1S+myBHyhlXzUcjhQnEG7aHZ65
132 AQ0EG+t93QEIAKqRq/2sBDW4g3FU+11LhixT+GosrfVvnitz3S9k2tBXok/wYpI1
133 XeA+kTHiF0LaqoaciDRvkA9DvhDbSrNM1yeuYRyZiHlTmoPZ/Fkl60oA2cyLd1L5
134 sXbuipY3TEiakugdSU4rzgi0hFycm6Go6yq2G6eC6UALvD9CTMdZHw40TadG9xpm
135 4thYPuJ1kPH8/bkbTi9sLHoApYgL+7ssje8w4epr0qD4IGxeKwJPf/tbTRpnd8w3
136 leldixHHKAutNt49p0pkXlORAHRpUmp+KMZhFvCvIPwe9o5mYtMR7sDRxjY61ZEQ
137 KLyKoh5wsJsaPXBjdG7cf6G/cBcwvnQVUHcAEQEAAYkBJQQYAQIADwUCG+t93QIb
138 DAUJAAFRgAAKCRBvFfAEoYhafOPgB/9z4YCyT/N0262HtegHykhsyykuqEeNb1LV
139 D9INcP+RbCX/0IjFgP4DTMPP7qqF1OBwR276maALT321Gqxc5HN5YrwxGdmoyBLm
140 unaQJJlD+7B1C+jnO6r4m44obvJ/NMERxVyzkXap3J2VgRIO1wNLI9I0sH6Kj5/j
141 Mgy06OwXDcqIc+jB4sIJ3Tnm8LZ3phJzNEm9mI8Ak0oJ7IEcMndR6DzmRt1rJQcq
142 K/D7hOG02zvyRhxF27U1qR1MxeU/gNnOx8q4dnVyWB+EiV1sFl4iTOyYHEsoyd7W
143 Osuse7+NkyUHgMXMVW7cz+nU7iO+ht2rkBtv+Z5LGlzgHTeFjKci
144 =WhX+
145 -----END PGP PUBLIC KEY BLOCK-----
146 """
147
148 import unittest
149 if __name__ == "__main__":
150     unittest.main()