8
8
from __future__ import annotations
9
9
10
10
import base64
11
+ import pathlib
11
12
import sys
12
13
import typing
13
14
from datetime import datetime , timedelta , timezone
@@ -2477,9 +2478,11 @@ def test_load_locations_fails_when_all_args_are_none(self) -> None:
2477
2478
with pytest .raises (Error ):
2478
2479
store .load_locations (None , None )
2479
2480
2480
- def test_load_locations_raises_error_on_failure (self , tmpdir ) -> None :
2481
- invalid_ca_file = tmpdir .join ("invalid.pem" )
2482
- invalid_ca_file .write ("This is not a certificate" )
2481
+ def test_load_locations_raises_error_on_failure (
2482
+ self , tmp_path : pathlib .Path
2483
+ ) -> None :
2484
+ invalid_ca_file = tmp_path / "invalid.pem"
2485
+ invalid_ca_file .write_text ("This is not a certificate" )
2483
2486
2484
2487
store = X509Store ()
2485
2488
with pytest .raises (Error ):
@@ -3369,22 +3372,25 @@ def test_get_verified_chain_invalid_chain_no_root(self) -> None:
3369
3372
assert exc .value .certificate .get_subject ().CN == "intermediate"
3370
3373
3371
3374
@pytest .fixture
3372
- def root_ca_file (self , tmpdir ):
3373
- return self ._create_ca_file (tmpdir , "root_ca_hash_dir" , self .root_cert )
3375
+ def root_ca_file (self , tmp_path : pathlib .Path ) -> pathlib .Path :
3376
+ return self ._create_ca_file (
3377
+ tmp_path , "root_ca_hash_dir" , self .root_cert
3378
+ )
3374
3379
3375
3380
@pytest .fixture
3376
- def intermediate_ca_file (self , tmpdir ) :
3381
+ def intermediate_ca_file (self , tmp_path : pathlib . Path ) -> pathlib . Path :
3377
3382
return self ._create_ca_file (
3378
- tmpdir , "intermediate_ca_hash_dir" , self .intermediate_cert
3383
+ tmp_path , "intermediate_ca_hash_dir" , self .intermediate_cert
3379
3384
)
3380
3385
3381
3386
@staticmethod
3382
- def _create_ca_file (base_path , hash_directory : str , cacert : X509 ):
3387
+ def _create_ca_file (
3388
+ base_path : pathlib .Path , hash_directory : str , cacert : X509
3389
+ ) -> pathlib .Path :
3383
3390
ca_hash = f"{ cacert .subject_name_hash ():08x} .0"
3384
- cafile = base_path .join (hash_directory , ca_hash )
3385
- cafile .write_binary (
3386
- dump_certificate (FILETYPE_PEM , cacert ), ensure = True
3387
- )
3391
+ cafile = base_path / hash_directory / ca_hash
3392
+ cafile .parent .mkdir (parents = True , exist_ok = True )
3393
+ cafile .write_bytes (dump_certificate (FILETYPE_PEM , cacert ))
3388
3394
return cafile
3389
3395
3390
3396
def test_verify_with_ca_file_location (self , root_ca_file ) -> None :
@@ -3396,7 +3402,7 @@ def test_verify_with_ca_file_location(self, root_ca_file) -> None:
3396
3402
3397
3403
def test_verify_with_ca_path_location (self , root_ca_file ) -> None :
3398
3404
store = X509Store ()
3399
- store .load_locations (None , str (root_ca_file .dirname ))
3405
+ store .load_locations (None , str (root_ca_file .parent ))
3400
3406
3401
3407
store_ctx = X509StoreContext (store , self .intermediate_cert )
3402
3408
store_ctx .verify_certificate ()
@@ -3406,7 +3412,7 @@ def test_verify_with_cafile_and_capath(
3406
3412
):
3407
3413
store = X509Store ()
3408
3414
store .load_locations (
3409
- cafile = str (root_ca_file ), capath = str (intermediate_ca_file .dirname )
3415
+ cafile = str (root_ca_file ), capath = str (intermediate_ca_file .parent )
3410
3416
)
3411
3417
3412
3418
store_ctx = X509StoreContext (store , self .intermediate_server_cert )
@@ -3422,9 +3428,11 @@ def test_verify_with_multiple_ca_files(
3422
3428
store_ctx = X509StoreContext (store , self .intermediate_server_cert )
3423
3429
store_ctx .verify_certificate ()
3424
3430
3425
- def test_verify_failure_with_empty_ca_directory (self , tmpdir ) -> None :
3431
+ def test_verify_failure_with_empty_ca_directory (
3432
+ self , tmp_path : pathlib .Path
3433
+ ) -> None :
3426
3434
store = X509Store ()
3427
- store .load_locations (None , str (tmpdir ))
3435
+ store .load_locations (None , str (tmp_path ))
3428
3436
3429
3437
store_ctx = X509StoreContext (store , self .intermediate_cert )
3430
3438
with pytest .raises (X509StoreContextError ) as exc :
0 commit comments