-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee53bba
commit b9685e2
Showing
6 changed files
with
201 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Upgraded to pytest 8. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
###################################################################### | ||
# | ||
# File: test/unit/_cli/test_pickle.py | ||
# | ||
# Copyright 2023 Backblaze Inc. All Rights Reserved. | ||
# | ||
# License https://www.backblaze.com/using_b2_code.html | ||
# | ||
###################################################################### | ||
import pickle | ||
|
||
import pytest | ||
|
||
from b2._internal._cli import autocomplete_cache | ||
|
||
from .unpickle import unpickle | ||
|
||
|
||
def test_pickle_store(tmp_path): | ||
dir = tmp_path | ||
store = autocomplete_cache.HomeCachePickleStore(dir) | ||
|
||
store.set_pickle('test_1', b'test_data_1') | ||
assert store.get_pickle('test_1') == b'test_data_1' | ||
assert store.get_pickle('test_2') is None | ||
assert len(list(dir.rglob('*.pickle'))) == 1 | ||
|
||
store.set_pickle('test_2', b'test_data_2') | ||
assert store.get_pickle('test_2') == b'test_data_2' | ||
assert store.get_pickle('test_1') is None | ||
assert len(list(dir.rglob('*.pickle'))) == 1 | ||
|
||
|
||
def test_unpickle(): | ||
"""This tests ensures that Unpickler works as expected: | ||
prevents successful unpickling of objects that depend on loading | ||
modules from b2sdk.""" | ||
from .fixtures.module_loading_b2sdk import function | ||
pickled = pickle.dumps(function) | ||
with pytest.raises(RuntimeError): | ||
unpickle(pickled) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
###################################################################### | ||
# | ||
# File: test/unit/_cli/unpickle.py | ||
# | ||
# Copyright 2023 Backblaze Inc. All Rights Reserved. | ||
# | ||
# License https://www.backblaze.com/using_b2_code.html | ||
# | ||
###################################################################### | ||
import importlib | ||
import io | ||
import pickle | ||
import sys | ||
from typing import Any | ||
|
||
|
||
class Unpickler(pickle.Unpickler): | ||
"""This Unpickler will raise an exception if loading the pickled object | ||
imports any b2sdk module.""" | ||
|
||
_modules_to_load: set[str] | ||
|
||
def load(self): | ||
self._modules_to_load = set() | ||
|
||
b2_modules = [module for module in sys.modules if 'b2sdk' in module] | ||
for key in b2_modules: | ||
del sys.modules[key] | ||
|
||
result = super().load() | ||
|
||
for module in self._modules_to_load: | ||
importlib.import_module(module) | ||
importlib.reload(sys.modules[module]) | ||
|
||
if any('b2sdk' in module for module in sys.modules): | ||
raise RuntimeError("Loading the pickled object imported b2sdk module") | ||
return result | ||
|
||
def find_class(self, module: str, name: str) -> Any: | ||
self._modules_to_load.add(module) | ||
return super().find_class(module, name) | ||
|
||
|
||
def unpickle(data: bytes) -> Any: | ||
"""Unpickling function that raises RuntimeError if unpickled | ||
object depends on b2sdk.""" | ||
return Unpickler(io.BytesIO(data)).load() |