
Security Fundamentals
Turtles, Clams, and Cyber Threat Actors: Shell Usage
The Socket Threat Research Team uncovers how threat actors weaponize shell techniques across npm, PyPI, and Go ecosystems to maintain persistence and exfiltrate data.
This library contains object, which can be used during mocking files in tests. Features:
You can register different actions (provide different objects) for different paths
You can add text, which will be returned on read
You can select behavior for paths, which are not registered. Supported behaviors:
It can be installed with following command:
pip install open-mock-file
Examples
Mock open first file with custom object, second one with string data and
raise FileNotFoundError for not registered path:
.. code:: python
from unittest.mock import patch
from open_file_mock import MockOpen
class FileObject:
def __init__(self, data):
self.data = data
def read(self, *args, **kwargs):
return self.data
def write(self, data, *args, **kwargs):
self.data = data
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
pass
with patch('builtins.open', new_callable=MockOpen) as open_mock:
open_mock.register_object_for_path(path='/tmp/f1', obj=FileObject('Some data'))
open_mock.set_read_data_for(path='/tmp/f2', data='file2 data')
with open('/tmp/f1') as f1:
print(f1.read())
f1.write('Other data')
print(f1.read())
print('----------------------')
with open('/tmp/f2') as f2:
print(f2.read())
print('----------------------')
open('/etc/hostname')
**output:**
::
Some data
Other data
----------------------
file2 data
----------------------
Traceback (most recent call last):
...
FileNotFoundError: File /etc/hostname not found in mock function
Set default behavior (for not registered paths) to return original file:
.. code:: python
from unittest.mock import patch, MagicMock
from open_file_mock import MockOpen, DEFAULTS_ORIGINAL
with patch('builtins.open', new_callable=MockOpen) as open_mock:
open_mock.default_behavior = DEFAULTS_ORIGINAL
open_mock.register_object_for_path(path='/tmp/f1', obj=MagicMock())
with open('/tmp/f1') as f1:
print(f1.read())
print('----------------------')
with open('/etc/hostname') as f2:
print(f2.read())
print('----------------------')
**output:**
::
<MagicMock name='mock.__enter__().read()' id='...'>
----------------------
myhost
----------------------
Set default behavior to return new mock:
.. code:: python
from unittest.mock import patch
from open_file_mock import MockOpen, DEFAULTS_MOCK
with patch('builtins.open', new_callable=MockOpen) as open_mock:
open_mock.default_behavior = DEFAULTS_MOCK
open_mock.set_read_data_for('/tmp/f1', 'QWERTY')
with open('/tmp/f1') as f1:
print(f1.read())
print('----------------------')
with open('/etc/hostname') as f2:
print(f2.read())
print('----------------------')
**output:**
::
QWERTY
----------------------
<MagicMock name='mock.__enter__().read()' id='...'>
----------------------
Yoy can get registered object with *get\_object\_for\_path* method:
.. code:: python
from unittest.mock import patch
from open_file_mock import MockOpen
class FileObject:
def __init__(self, data):
self.data = data
def read(self, *args, **kwargs):
return self.data
def write(self, data, *args, **kwargs):
self.data = data
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
pass
with patch('builtins.open', new_callable=MockOpen) as open_mock:
open_mock.register_object_for_path(path='/tmp/f1', obj=FileObject('Some data'))
open_mock.set_read_data_for(path='/tmp/f2', data='file2 data')
with open('/tmp/f1') as f1:
print(f1.read())
print('----------------------')
print(open_mock.get_object_for_path('/tmp/f1'))
**output:**
::
Some data
----------------------
<__main__.FileObject object at ...>
Object methods:
FAQs
Smart object for mocking open file calls, depends on file path
We found that open-mock-file demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security Fundamentals
The Socket Threat Research Team uncovers how threat actors weaponize shell techniques across npm, PyPI, and Go ecosystems to maintain persistence and exfiltrate data.
Security News
At VulnCon 2025, NIST scrapped its NVD consortium plans, admitted it can't keep up with CVEs, and outlined automation efforts amid a mounting backlog.
Product
We redesigned our GitHub PR comments to deliver clear, actionable security insights without adding noise to your workflow.