|
11 | 11 | PKCS11Error, |
12 | 12 | ) |
13 | 13 | from pkcs11.attributes import AttributeMapper, handle_bool, handle_str |
| 14 | +from pkcs11.exceptions import PinIncorrect, PinLenRange |
14 | 15 |
|
15 | 16 | from . import TOKEN_PIN, TOKEN_SO_PIN, Not, Only, TestCase, requires |
16 | 17 |
|
@@ -291,3 +292,52 @@ def _handler(self, key): |
291 | 292 | bool_read = key[Attribute.ID] |
292 | 293 | self.assertIsInstance(bool_read, bool) |
293 | 294 | self.assertFalse(bool_read, False) |
| 295 | + |
| 296 | + @Only.softhsm2 |
| 297 | + def test_set_pin(self): |
| 298 | + old_token_pin = TOKEN_PIN |
| 299 | + new_token_pin = f"{TOKEN_PIN}56" |
| 300 | + |
| 301 | + with self.token.open(rw=True, user_pin=old_token_pin) as session: |
| 302 | + session.set_pin(old_token_pin, new_token_pin) |
| 303 | + |
| 304 | + with self.token.open(user_pin=new_token_pin) as session: |
| 305 | + self.assertIsInstance(session, pkcs11.Session) |
| 306 | + |
| 307 | + with self.token.open(rw=True, user_pin=new_token_pin) as session: |
| 308 | + session.set_pin(new_token_pin, old_token_pin) |
| 309 | + |
| 310 | + with self.token.open(user_pin=old_token_pin) as session: |
| 311 | + self.assertIsInstance(session, pkcs11.Session) |
| 312 | + |
| 313 | + with self.token.open(rw=True, user_pin=old_token_pin) as session: |
| 314 | + with self.assertRaises(AttributeError): |
| 315 | + session.set_pin(None, new_token_pin) |
| 316 | + with self.assertRaises(AttributeError): |
| 317 | + session.set_pin(old_token_pin, None) |
| 318 | + with self.assertRaises(PinLenRange): |
| 319 | + session.set_pin(old_token_pin, "") |
| 320 | + with self.assertRaises(PinIncorrect): |
| 321 | + session.set_pin("", new_token_pin) |
| 322 | + |
| 323 | + @Only.softhsm2 |
| 324 | + def test_init_pin(self): |
| 325 | + new_token_pin = f"{TOKEN_PIN}56" |
| 326 | + |
| 327 | + with self.token.open(rw=True, so_pin=TOKEN_SO_PIN) as session: |
| 328 | + session.init_pin(new_token_pin) |
| 329 | + |
| 330 | + with self.token.open(rw=True, user_pin=new_token_pin) as session: |
| 331 | + self.assertIsInstance(session, pkcs11.Session) |
| 332 | + |
| 333 | + with self.token.open(rw=True, so_pin=TOKEN_SO_PIN) as session: |
| 334 | + session.init_pin(TOKEN_PIN) |
| 335 | + |
| 336 | + with self.token.open(rw=True, user_pin=TOKEN_PIN) as session: |
| 337 | + self.assertIsInstance(session, pkcs11.Session) |
| 338 | + |
| 339 | + with self.token.open(rw=True, so_pin=TOKEN_SO_PIN) as session: |
| 340 | + with self.assertRaises(AttributeError): |
| 341 | + session.init_pin(None) |
| 342 | + with self.assertRaises(PinLenRange): |
| 343 | + session.init_pin("") |
0 commit comments