├── MANIFEST.in ├── _meta.py ├── .gitignore ├── AUTHORS.rst ├── tox.ini ├── docs ├── index.rst ├── make.bat ├── Makefile └── conf.py ├── setup.py ├── LICENSE ├── integration_tests.py ├── README.rst ├── sandboxie.py └── unit_tests.py /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README.rst AUTHORS.rst LICENSE 2 | -------------------------------------------------------------------------------- /_meta.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __version__ = '0.1.0' 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ 2 | build/* 3 | *.pyc 4 | .tox 5 | MANIFEST 6 | docs/_build 7 | -------------------------------------------------------------------------------- /AUTHORS.rst: -------------------------------------------------------------------------------- 1 | Development Lead 2 | ---------------- 3 | - Gregg Gajic 4 | 5 | Contributors 6 | ------------ 7 | -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = py27, py32, pep8, docs 3 | 4 | [testenv] 5 | deps = mock 6 | commands = python unit_tests.py 7 | python integration_tests.py 8 | 9 | [testenv:pep8] 10 | deps = pep8 11 | commands = pep8 --repeat sandboxie.py unit_tests.py integration_tests.py 12 | 13 | [testenv:docs] 14 | changedir = {toxinidir}/docs 15 | deps = sphinx 16 | commands = sphinx-build -WEab html -d _build/doctrees . _build/html 17 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | .. sandboxie documentation master file, created by 2 | sphinx-quickstart on Thu Apr 5 08:48:09 2012. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | 7 | sandboxie |version| 8 | =================== 9 | 10 | .. include:: ../README.rst 11 | 12 | 13 | API Documentation 14 | ----------------- 15 | 16 | .. automodule:: sandboxie 17 | :members: 18 | :inherited-members: 19 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | from distutils.core import setup 4 | 5 | import _meta 6 | 7 | requirements = [] 8 | 9 | try: 10 | import configparser 11 | except ImportError: 12 | requirements.append('configparser') 13 | 14 | setup( 15 | name='sandboxie', 16 | version=_meta.__version__, 17 | author='Gregg Gajic', 18 | author_email='gregg.gajic@gmail.com', 19 | py_modules=['sandboxie', '_meta'], 20 | install_requires=requirements, 21 | classifiers=['Programming Language :: Python', 22 | 'Programming Language :: Python :: 2.7', 23 | 'Programming Language :: Python :: 3', 24 | 'Programming Language :: Python :: 3.2', 25 | 'Natural Language :: English', 26 | 'Operating System :: Microsoft :: Windows', 27 | 'License :: OSI Approved :: MIT License', 28 | 'Development Status :: 4 - Beta'], 29 | packages=[], 30 | scripts=[], 31 | license=open('LICENSE').read(), 32 | description='Python interface to Sandboxie.', 33 | long_description=open('README.rst').read(), 34 | url='https://github.com/gg/sandboxie-py', 35 | ) 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Gregg Gajic and contributors. See 2 | AUTHORS.rst for more details. 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of 5 | this software and associated documentation files (the "Software"), to deal in 6 | the Software without restriction, including without limitation the rights to 7 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 8 | of the Software, and to permit persons to whom the Software is furnished to do 9 | so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 | SOFTWARE. 21 | -------------------------------------------------------------------------------- /integration_tests.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | from __future__ import unicode_literals 4 | 5 | import sandboxie 6 | import subprocess 7 | import time 8 | import unittest 9 | 10 | 11 | class SandboxieIntegrationTests(unittest.TestCase): 12 | def setUp(self): 13 | self.sbie = sandboxie.Sandboxie() 14 | self.sbie.create_sandbox(box='foo', options={'Enabled': 'yes'}) 15 | 16 | def tearDown(self): 17 | self.sbie.delete_contents(box='foo') 18 | self.sbie.destroy_sandbox(box='foo') 19 | 20 | def test_start_command_fails_due_to_non_existent_sandbox(self): 21 | try: 22 | self.sbie.start('notepad.exe', box='DOES_NOT_EXIST', wait=False) 23 | except subprocess.CalledProcessError: 24 | pass 25 | else: 26 | self.fail() 27 | 28 | def test_start_command_fails_due_to_invalid_command(self): 29 | try: 30 | self.sbie.start('asdaklwjWLAL.asjd', box='foo', wait=False) 31 | except subprocess.CalledProcessError: 32 | pass 33 | else: 34 | self.fail() 35 | 36 | def test_launch_notepad(self): 37 | self.sbie.start('notepad.exe', box='foo', wait=False) 38 | assert(len(list(self.sbie.running_processes(box='foo'))) > 0) 39 | self.sbie.terminate_processes(box='foo') 40 | 41 | 42 | if __name__ == '__main__': 43 | unittest.main() 44 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | sandboxie is a Python interface to `Sandboxie `_. 2 | 3 | 4 | Quickstart 5 | ---------- 6 | 7 | :: 8 | 9 | >>> import sandboxie 10 | >>> sbie = sandboxie.Sandboxie() 11 | 12 | Create a sandbox:: 13 | 14 | >>> sbie.create_sandbox(box='foo', options={'Enabled': 'yes'}) 15 | 16 | Start a sandboxed process:: 17 | 18 | >>> sbie.start('notepad.exe', box='foo', wait=False) 19 | 20 | Get sandboxed processes:: 21 | 22 | >>> for pid in sbie.running_processes(box='foo'): 23 | >>> print(pid) 24 | 3 25 | 15688 26 | 5716 27 | 26916 28 | 29 | Terminate sandboxed processes:: 30 | 31 | >>> sbie.terminate_processes(box='foo') 32 | 33 | Delete the contents of a sandbox:: 34 | 35 | >>> sbie.delete_contents(box='foo') 36 | 37 | Destroy a sandbox:: 38 | 39 | >>> sbie.destroy_sandbox(box='foo') 40 | 41 | 42 | Installation 43 | ------------ 44 | 45 | The preferred way is to use pip_:: 46 | 47 | $ pip install sandboxie 48 | 49 | You can also use ``easy_install``, but it's discouraged. 50 | 51 | .. _pip: http://pip-installer.org 52 | 53 | 54 | Supported Python versions 55 | ~~~~~~~~~~~~~~~~~~~~~~~~~ 56 | 57 | Python 2.7 and 3.2 are currently supported from a single codebase, without 2to3 58 | translation. 59 | 60 | 61 | Contribute 62 | ---------- 63 | 64 | The code repository is on GitHub: https://github.com/gg/sandboxie-py. 65 | 66 | To contribute: 67 | 68 | #. Work on an `open issue`_ or submit a new issue to start a discussion around 69 | a bug or feature request. 70 | 71 | * When submitting a bug, ensure your description includes the following: 72 | - the version of ``sandboxie`` used 73 | - any relevant system information, such as your operating system 74 | - steps to produce the bug (so others could reproduce it) 75 | 76 | #. Fork `the repository`_ and add the bug fix or feature to the **develop** 77 | branch. 78 | #. Write tests that demonstrate the bug was fixed or the feature works as 79 | expected. 80 | #. Submit a pull request and bug the maintainer until your contribution gets 81 | merged and published :-) You should also add yourself to AUTHORS_. 82 | 83 | .. _the repository: https://github.com/gg/sandboxie-py 84 | .. _open issue: https://github.com/gg/sandboxie-py/issues 85 | .. _AUTHORS: https://github.com/gg/sandboxie-py/blob/develop/AUTHORS.rst 86 | 87 | 88 | Running the Tests 89 | ~~~~~~~~~~~~~~~~~ 90 | 91 | tox_ is used to run unit and integration tests in each of the supported Python 92 | environments. 93 | 94 | First install tox:: 95 | 96 | $ pip install tox 97 | 98 | Then run tox from the project root directory:: 99 | 100 | $ tox 101 | 102 | *Note: the integration tests require Sandboxie to be installed on your 103 | machine.* 104 | 105 | .. _tox: http://tox.testrun.org/ 106 | 107 | 108 | Coding Style 109 | ~~~~~~~~~~~~ 110 | 111 | Ensure that your contributed code complies with `PEP 8`_. The test runner 112 | ``tox`` also checks for PEP 8 compliance. 113 | 114 | .. _PEP 8: http://www.python.org/dev/peps/pep-0008/ 115 | -------------------------------------------------------------------------------- /docs/make.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | 3 | REM Command file for Sphinx documentation 4 | 5 | if "%SPHINXBUILD%" == "" ( 6 | set SPHINXBUILD=sphinx-build 7 | ) 8 | set BUILDDIR=_build 9 | set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . 10 | set I18NSPHINXOPTS=%SPHINXOPTS% . 11 | if NOT "%PAPER%" == "" ( 12 | set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% 13 | set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% 14 | ) 15 | 16 | if "%1" == "" goto help 17 | 18 | if "%1" == "help" ( 19 | :help 20 | echo.Please use `make ^` where ^ is one of 21 | echo. html to make standalone HTML files 22 | echo. dirhtml to make HTML files named index.html in directories 23 | echo. singlehtml to make a single large HTML file 24 | echo. pickle to make pickle files 25 | echo. json to make JSON files 26 | echo. htmlhelp to make HTML files and a HTML help project 27 | echo. qthelp to make HTML files and a qthelp project 28 | echo. devhelp to make HTML files and a Devhelp project 29 | echo. epub to make an epub 30 | echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter 31 | echo. text to make text files 32 | echo. man to make manual pages 33 | echo. texinfo to make Texinfo files 34 | echo. gettext to make PO message catalogs 35 | echo. changes to make an overview over all changed/added/deprecated items 36 | echo. linkcheck to check all external links for integrity 37 | echo. doctest to run all doctests embedded in the documentation if enabled 38 | goto end 39 | ) 40 | 41 | if "%1" == "clean" ( 42 | for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i 43 | del /q /s %BUILDDIR%\* 44 | goto end 45 | ) 46 | 47 | if "%1" == "html" ( 48 | %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html 49 | if errorlevel 1 exit /b 1 50 | echo. 51 | echo.Build finished. The HTML pages are in %BUILDDIR%/html. 52 | goto end 53 | ) 54 | 55 | if "%1" == "dirhtml" ( 56 | %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml 57 | if errorlevel 1 exit /b 1 58 | echo. 59 | echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. 60 | goto end 61 | ) 62 | 63 | if "%1" == "singlehtml" ( 64 | %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml 65 | if errorlevel 1 exit /b 1 66 | echo. 67 | echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. 68 | goto end 69 | ) 70 | 71 | if "%1" == "pickle" ( 72 | %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle 73 | if errorlevel 1 exit /b 1 74 | echo. 75 | echo.Build finished; now you can process the pickle files. 76 | goto end 77 | ) 78 | 79 | if "%1" == "json" ( 80 | %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json 81 | if errorlevel 1 exit /b 1 82 | echo. 83 | echo.Build finished; now you can process the JSON files. 84 | goto end 85 | ) 86 | 87 | if "%1" == "htmlhelp" ( 88 | %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp 89 | if errorlevel 1 exit /b 1 90 | echo. 91 | echo.Build finished; now you can run HTML Help Workshop with the ^ 92 | .hhp project file in %BUILDDIR%/htmlhelp. 93 | goto end 94 | ) 95 | 96 | if "%1" == "qthelp" ( 97 | %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp 98 | if errorlevel 1 exit /b 1 99 | echo. 100 | echo.Build finished; now you can run "qcollectiongenerator" with the ^ 101 | .qhcp project file in %BUILDDIR%/qthelp, like this: 102 | echo.^> qcollectiongenerator %BUILDDIR%\qthelp\sandboxie.qhcp 103 | echo.To view the help file: 104 | echo.^> assistant -collectionFile %BUILDDIR%\qthelp\sandboxie.ghc 105 | goto end 106 | ) 107 | 108 | if "%1" == "devhelp" ( 109 | %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp 110 | if errorlevel 1 exit /b 1 111 | echo. 112 | echo.Build finished. 113 | goto end 114 | ) 115 | 116 | if "%1" == "epub" ( 117 | %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub 118 | if errorlevel 1 exit /b 1 119 | echo. 120 | echo.Build finished. The epub file is in %BUILDDIR%/epub. 121 | goto end 122 | ) 123 | 124 | if "%1" == "latex" ( 125 | %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex 126 | if errorlevel 1 exit /b 1 127 | echo. 128 | echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. 129 | goto end 130 | ) 131 | 132 | if "%1" == "text" ( 133 | %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text 134 | if errorlevel 1 exit /b 1 135 | echo. 136 | echo.Build finished. The text files are in %BUILDDIR%/text. 137 | goto end 138 | ) 139 | 140 | if "%1" == "man" ( 141 | %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man 142 | if errorlevel 1 exit /b 1 143 | echo. 144 | echo.Build finished. The manual pages are in %BUILDDIR%/man. 145 | goto end 146 | ) 147 | 148 | if "%1" == "texinfo" ( 149 | %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo 150 | if errorlevel 1 exit /b 1 151 | echo. 152 | echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. 153 | goto end 154 | ) 155 | 156 | if "%1" == "gettext" ( 157 | %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale 158 | if errorlevel 1 exit /b 1 159 | echo. 160 | echo.Build finished. The message catalogs are in %BUILDDIR%/locale. 161 | goto end 162 | ) 163 | 164 | if "%1" == "changes" ( 165 | %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes 166 | if errorlevel 1 exit /b 1 167 | echo. 168 | echo.The overview file is in %BUILDDIR%/changes. 169 | goto end 170 | ) 171 | 172 | if "%1" == "linkcheck" ( 173 | %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck 174 | if errorlevel 1 exit /b 1 175 | echo. 176 | echo.Link check complete; look for any errors in the above output ^ 177 | or in %BUILDDIR%/linkcheck/output.txt. 178 | goto end 179 | ) 180 | 181 | if "%1" == "doctest" ( 182 | %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest 183 | if errorlevel 1 exit /b 1 184 | echo. 185 | echo.Testing of doctests in the sources finished, look at the ^ 186 | results in %BUILDDIR%/doctest/output.txt. 187 | goto end 188 | ) 189 | 190 | :end 191 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # Internal variables. 11 | PAPEROPT_a4 = -D latex_paper_size=a4 12 | PAPEROPT_letter = -D latex_paper_size=letter 13 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 14 | # the i18n builder cannot share the environment and doctrees with the others 15 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 16 | 17 | .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext 18 | 19 | help: 20 | @echo "Please use \`make ' where is one of" 21 | @echo " html to make standalone HTML files" 22 | @echo " dirhtml to make HTML files named index.html in directories" 23 | @echo " singlehtml to make a single large HTML file" 24 | @echo " pickle to make pickle files" 25 | @echo " json to make JSON files" 26 | @echo " htmlhelp to make HTML files and a HTML help project" 27 | @echo " qthelp to make HTML files and a qthelp project" 28 | @echo " devhelp to make HTML files and a Devhelp project" 29 | @echo " epub to make an epub" 30 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 31 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 32 | @echo " text to make text files" 33 | @echo " man to make manual pages" 34 | @echo " texinfo to make Texinfo files" 35 | @echo " info to make Texinfo files and run them through makeinfo" 36 | @echo " gettext to make PO message catalogs" 37 | @echo " changes to make an overview of all changed/added/deprecated items" 38 | @echo " linkcheck to check all external links for integrity" 39 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 40 | 41 | clean: 42 | -rm -rf $(BUILDDIR)/* 43 | 44 | html: 45 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 46 | @echo 47 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 48 | 49 | dirhtml: 50 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 51 | @echo 52 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 53 | 54 | singlehtml: 55 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 56 | @echo 57 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 58 | 59 | pickle: 60 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 61 | @echo 62 | @echo "Build finished; now you can process the pickle files." 63 | 64 | json: 65 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 66 | @echo 67 | @echo "Build finished; now you can process the JSON files." 68 | 69 | htmlhelp: 70 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 71 | @echo 72 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 73 | ".hhp project file in $(BUILDDIR)/htmlhelp." 74 | 75 | qthelp: 76 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 77 | @echo 78 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 79 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 80 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/sandboxie.qhcp" 81 | @echo "To view the help file:" 82 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/sandboxie.qhc" 83 | 84 | devhelp: 85 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 86 | @echo 87 | @echo "Build finished." 88 | @echo "To view the help file:" 89 | @echo "# mkdir -p $$HOME/.local/share/devhelp/sandboxie" 90 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/sandboxie" 91 | @echo "# devhelp" 92 | 93 | epub: 94 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 95 | @echo 96 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 97 | 98 | latex: 99 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 100 | @echo 101 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 102 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 103 | "(use \`make latexpdf' here to do that automatically)." 104 | 105 | latexpdf: 106 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 107 | @echo "Running LaTeX files through pdflatex..." 108 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 109 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 110 | 111 | text: 112 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 113 | @echo 114 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 115 | 116 | man: 117 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 118 | @echo 119 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 120 | 121 | texinfo: 122 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 123 | @echo 124 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 125 | @echo "Run \`make' in that directory to run these through makeinfo" \ 126 | "(use \`make info' here to do that automatically)." 127 | 128 | info: 129 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 130 | @echo "Running Texinfo files through makeinfo..." 131 | make -C $(BUILDDIR)/texinfo info 132 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 133 | 134 | gettext: 135 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 136 | @echo 137 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 138 | 139 | changes: 140 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 141 | @echo 142 | @echo "The overview file is in $(BUILDDIR)/changes." 143 | 144 | linkcheck: 145 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 146 | @echo 147 | @echo "Link check complete; look for any errors in the above output " \ 148 | "or in $(BUILDDIR)/linkcheck/output.txt." 149 | 150 | doctest: 151 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 152 | @echo "Testing of doctests in the sources finished, look at the " \ 153 | "results in $(BUILDDIR)/doctest/output.txt." 154 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # 4 | # sandboxie documentation build configuration file, created by 5 | # sphinx-quickstart on Thu Apr 5 08:48:09 2012. 6 | # 7 | # This file is execfile()d with the current directory set to its containing dir. 8 | # 9 | # Note that not all possible configuration values are present in this 10 | # autogenerated file. 11 | # 12 | # All configuration values have a default; values that are commented out 13 | # serve to show the default. 14 | 15 | import sys, os 16 | 17 | import _meta 18 | 19 | # If extensions (or modules to document with autodoc) are in another directory, 20 | # add these directories to sys.path here. If the directory is relative to the 21 | # documentation root, use os.path.abspath to make it absolute, like shown here. 22 | #sys.path.insert(0, os.path.abspath('.')) 23 | 24 | # -- General configuration ----------------------------------------------------- 25 | 26 | # If your documentation needs a minimal Sphinx version, state it here. 27 | #needs_sphinx = '1.0' 28 | 29 | # Add any Sphinx extension module names here, as strings. They can be extensions 30 | # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. 31 | extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.intersphinx', 'sphinx.ext.todo', 'sphinx.ext.coverage', 'sphinx.ext.pngmath', 'sphinx.ext.mathjax', 'sphinx.ext.ifconfig', 'sphinx.ext.viewcode'] 32 | 33 | # Add any paths that contain templates here, relative to this directory. 34 | templates_path = ['_templates'] 35 | 36 | # The suffix of source filenames. 37 | source_suffix = '.rst' 38 | 39 | # The encoding of source files. 40 | #source_encoding = 'utf-8-sig' 41 | 42 | # The master toctree document. 43 | master_doc = 'index' 44 | 45 | # General information about the project. 46 | project = 'sandboxie' 47 | copyright = '2012, Gregg Gajic and contributors.' 48 | 49 | # The version info for the project you're documenting, acts as replacement for 50 | # |version| and |release|, also used in various other places throughout the 51 | # built documents. 52 | # 53 | # The short X.Y version. 54 | version = _meta.__version__ 55 | # The full version, including alpha/beta/rc tags. 56 | release = version 57 | 58 | # The language for content autogenerated by Sphinx. Refer to documentation 59 | # for a list of supported languages. 60 | #language = None 61 | 62 | # There are two options for replacing |today|: either, you set today to some 63 | # non-false value, then it is used: 64 | #today = '' 65 | # Else, today_fmt is used as the format for a strftime call. 66 | #today_fmt = '%B %d, %Y' 67 | 68 | # List of patterns, relative to source directory, that match files and 69 | # directories to ignore when looking for source files. 70 | exclude_patterns = ['_build', 'api.rst'] 71 | 72 | # The reST default role (used for this markup: `text`) to use for all documents. 73 | #default_role = None 74 | 75 | # If true, '()' will be appended to :func: etc. cross-reference text. 76 | #add_function_parentheses = True 77 | 78 | # If true, the current module name will be prepended to all description 79 | # unit titles (such as .. function::). 80 | #add_module_names = True 81 | 82 | # If true, sectionauthor and moduleauthor directives will be shown in the 83 | # output. They are ignored by default. 84 | #show_authors = False 85 | 86 | # The name of the Pygments (syntax highlighting) style to use. 87 | pygments_style = 'sphinx' 88 | 89 | # A list of ignored prefixes for module index sorting. 90 | #modindex_common_prefix = [] 91 | 92 | 93 | # -- Options for HTML output --------------------------------------------------- 94 | 95 | # The theme to use for HTML and HTML Help pages. See the documentation for 96 | # a list of builtin themes. 97 | html_theme = 'default' 98 | 99 | # Theme options are theme-specific and customize the look and feel of a theme 100 | # further. For a list of options available for each theme, see the 101 | # documentation. 102 | #html_theme_options = {} 103 | 104 | # Add any paths that contain custom themes here, relative to this directory. 105 | #html_theme_path = [] 106 | 107 | # The name for this set of Sphinx documents. If None, it defaults to 108 | # " v documentation". 109 | #html_title = None 110 | 111 | # A shorter title for the navigation bar. Default is the same as html_title. 112 | #html_short_title = None 113 | 114 | # The name of an image file (relative to this directory) to place at the top 115 | # of the sidebar. 116 | #html_logo = None 117 | 118 | # The name of an image file (within the static path) to use as favicon of the 119 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 120 | # pixels large. 121 | #html_favicon = None 122 | 123 | # Add any paths that contain custom static files (such as style sheets) here, 124 | # relative to this directory. They are copied after the builtin static files, 125 | # so a file named "default.css" will overwrite the builtin "default.css". 126 | html_static_path = ['_static'] 127 | 128 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, 129 | # using the given strftime format. 130 | #html_last_updated_fmt = '%b %d, %Y' 131 | 132 | # If true, SmartyPants will be used to convert quotes and dashes to 133 | # typographically correct entities. 134 | #html_use_smartypants = True 135 | 136 | # Custom sidebar templates, maps document names to template names. 137 | #html_sidebars = {} 138 | 139 | # Additional templates that should be rendered to pages, maps page names to 140 | # template names. 141 | #html_additional_pages = {} 142 | 143 | # If false, no module index is generated. 144 | #html_domain_indices = True 145 | 146 | # If false, no index is generated. 147 | #html_use_index = True 148 | 149 | # If true, the index is split into individual pages for each letter. 150 | #html_split_index = False 151 | 152 | # If true, links to the reST sources are added to the pages. 153 | #html_show_sourcelink = True 154 | 155 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 156 | #html_show_sphinx = True 157 | 158 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 159 | #html_show_copyright = True 160 | 161 | # If true, an OpenSearch description file will be output, and all pages will 162 | # contain a tag referring to it. The value of this option must be the 163 | # base URL from which the finished HTML is served. 164 | #html_use_opensearch = '' 165 | 166 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 167 | #html_file_suffix = None 168 | 169 | # Output file base name for HTML help builder. 170 | htmlhelp_basename = 'sandboxiedoc' 171 | 172 | 173 | # -- Options for LaTeX output -------------------------------------------------- 174 | 175 | latex_elements = { 176 | # The paper size ('letterpaper' or 'a4paper'). 177 | #'papersize': 'letterpaper', 178 | 179 | # The font size ('10pt', '11pt' or '12pt'). 180 | #'pointsize': '10pt', 181 | 182 | # Additional stuff for the LaTeX preamble. 183 | #'preamble': '', 184 | } 185 | 186 | # Grouping the document tree into LaTeX files. List of tuples 187 | # (source start file, target name, title, author, documentclass [howto/manual]). 188 | latex_documents = [ 189 | ('index', 'sandboxie.tex', 'sandboxie Documentation', 190 | 'Gregg Gajic', 'manual'), 191 | ] 192 | 193 | # The name of an image file (relative to this directory) to place at the top of 194 | # the title page. 195 | #latex_logo = None 196 | 197 | # For "manual" documents, if this is true, then toplevel headings are parts, 198 | # not chapters. 199 | #latex_use_parts = False 200 | 201 | # If true, show page references after internal links. 202 | #latex_show_pagerefs = False 203 | 204 | # If true, show URL addresses after external links. 205 | #latex_show_urls = False 206 | 207 | # Documents to append as an appendix to all manuals. 208 | #latex_appendices = [] 209 | 210 | # If false, no module index is generated. 211 | #latex_domain_indices = True 212 | 213 | 214 | # -- Options for manual page output -------------------------------------------- 215 | 216 | # One entry per manual page. List of tuples 217 | # (source start file, name, description, authors, manual section). 218 | man_pages = [ 219 | ('index', 'sandboxie', 'sandboxie Documentation', 220 | ['Gregg Gajic'], 1) 221 | ] 222 | 223 | # If true, show URL addresses after external links. 224 | #man_show_urls = False 225 | 226 | 227 | # -- Options for Texinfo output ------------------------------------------------ 228 | 229 | # Grouping the document tree into Texinfo files. List of tuples 230 | # (source start file, target name, title, author, 231 | # dir menu entry, description, category) 232 | texinfo_documents = [ 233 | ('index', 'sandboxie', 'sandboxie Documentation', 234 | 'Gregg Gajic', 'sandboxie', 'One line description of project.', 235 | 'Miscellaneous'), 236 | ] 237 | 238 | # Documents to append as an appendix to all manuals. 239 | #texinfo_appendices = [] 240 | 241 | # If false, no module index is generated. 242 | #texinfo_domain_indices = True 243 | 244 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 245 | #texinfo_show_urls = 'footnote' 246 | 247 | # http://sphinx.pocoo.org/ext/autodoc.html#confval-autoclass_content 248 | # Include both the class' and the __init__ methods docstring. 249 | autoclass_content = 'both' 250 | 251 | 252 | # Example configuration for intersphinx: refer to the Python standard library. 253 | intersphinx_mapping = {'http://docs.python.org/': None} 254 | -------------------------------------------------------------------------------- /sandboxie.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | # Copyright (c) 2012 Gregg Gajic and contributors. See 4 | # AUTHORS.rst for more details. 5 | # 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy 7 | # of this software and associated documentation files (the "Software"), to deal 8 | # in the Software without restriction, including without limitation the rights 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | # copies of the Software, and to permit persons to whom the Software is 11 | # furnished to do so, subject to the following conditions: 12 | # 13 | # The above copyright notice and this permission notice shall be included in 14 | # all copies or substantial portions of the Software. 15 | # 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | # SOFTWARE. 23 | 24 | from __future__ import unicode_literals 25 | 26 | import configparser 27 | import contextlib 28 | import io 29 | import os 30 | import subprocess 31 | 32 | import _meta 33 | 34 | 35 | __version__ = _meta.__version__ 36 | 37 | 38 | class SandboxieError(Exception): 39 | pass 40 | 41 | 42 | class Sandboxie(object): 43 | """An interface to `Sandboxie `_.""" 44 | 45 | def __init__(self, defaultbox='DefaultBox', install_dir=None): 46 | """ 47 | :param defaultbox: The default sandbox in which sandboxed commands are 48 | executed. 49 | :param install_dir: The absolute path to the Sandboxie installation 50 | directory. If ``None``, it will be set to the 51 | value of the ``SANDBOXIE_INSTALL_DIR`` environment 52 | variable, or ``C:\Program Files\Sandboxie``, 53 | if the environment variable is not set. 54 | 55 | Raises :class:`SandboxieError` if the Sandboxie.ini config file could 56 | not be located in the following directories: 57 | 58 | #. In the Windows folder: ``C:\WINDOWS`` on most Windows 59 | installations; ``C:\WINNT`` on Windows 2000 60 | #. In the Sandboxie installation folder, typically 61 | ``C:\Program Files\Sandboxie``. 62 | """ 63 | self.install_dir = install_dir 64 | if install_dir is None: 65 | self.install_dir = os.environ.get('SANDBOXIE_INSTALL_DIR', 66 | r'C:\Program Files\Sandboxie') 67 | self.defaultbox = defaultbox 68 | self.config_path = self._find_config_path() 69 | if self.config_path is None: 70 | raise SandboxieError(('Could not find Sandboxie.ini config. Is ' 71 | 'Sandboxie installed?')) 72 | 73 | def _find_config_path(self): 74 | """Returns the absolute path to the Sandboxie.ini config, or ``None`` 75 | if it could not be found. 76 | """ 77 | for _dir in (os.environ['WinDir'], self.install_dir): 78 | path = os.path.join(_dir, 'Sandboxie.ini') 79 | if os.path.exists(path): 80 | return path 81 | return None 82 | 83 | def _open_config_file(self, mode='r', encoding='utf-16-le'): 84 | return io.open(self.config_path, mode, encoding=encoding) 85 | 86 | def _shell_output(self, *args, **kwargs): 87 | return subprocess.check_output(*args, **kwargs) 88 | 89 | def _write_config(self, config): 90 | """Writes *config* to ``self.config_path``. 91 | 92 | :param config: a :class:`configparser.ConfigParser` instance of a 93 | Sandboxie.ini config. 94 | """ 95 | with self._open_config_file(mode='w') as config_file: 96 | config.write(config_file) 97 | 98 | @contextlib.contextmanager 99 | def _modify_config(self): 100 | """A context manager that yields a :class:`configparser.ConfigParser` 101 | instance of the parsed Sandboxie.ini config (to allow for 102 | modifications), then writes the config to ``self.config_path`` upon 103 | completion of the block. 104 | """ 105 | config = self.get_config() 106 | yield config 107 | self._write_config(config) 108 | 109 | def get_config(self): 110 | """Returns a :class:`configparser.ConfigParser` instance of the parsed 111 | Sandboxie.ini config.""" 112 | config = configparser.ConfigParser(strict=False) 113 | with self._open_config_file(mode='r') as config_file: 114 | config.read_file(config_file) 115 | return config 116 | 117 | def create_sandbox(self, box, options): 118 | """Creates a sandbox named *box*, with a ``dict`` of sandbox 119 | *options*.""" 120 | with self._modify_config() as config: 121 | config[box] = options 122 | self.reload_config() 123 | 124 | def destroy_sandbox(self, box): 125 | """Destroys the sandbox named *box*. Counterpart to 126 | :func:`create_sandbox`.""" 127 | with self._modify_config() as config: 128 | config.remove_section(box) 129 | self.reload_config() 130 | 131 | def start(self, command=None, box=None, silent=True, wait=False, 132 | nosbiectrl=True, elevate=False, disable_forced=False, 133 | reload=False, terminate=False, terminate_all=False, 134 | listpids=False): 135 | """Executes *command* under the supervision of Sandboxie by invoking 136 | `Sandboxie's Start Command Line`_. 137 | 138 | Returns the output of Start.exe on success. Raises 139 | :class:`subprocess.CalledProcessError` or :class:`WindowsError` on 140 | failure. 141 | 142 | :param box: The name of the sandbox sandbox to execute the command in. 143 | If ``None``, the command will be executed in the default sandbox, 144 | ``self.defaultbox``. 145 | :param silent: If ``True``, eliminates some pop-up error messages. 146 | :param wait: If ``True``, wait for the command to finish executing 147 | before returning. 148 | :param nosbiectrl: If ``True``, ensures that Sandboxie Control is not 149 | run before executing *command*. 150 | :param elevate: If ``True``, allows you to run a program with 151 | Administrator privileges on a system where User Account Control 152 | (UAC) is enabled. 153 | :param disable_forced: If ``True``, runs a program outside the sandbox, 154 | even if the program is forced. 155 | :param reload: If ``True``, reloads the Sandboxie config. Only applies 156 | when *command* is ``None``. 157 | :param terminate: If ``True``, terminates all sandboxed processes in 158 | *box*. Only applies when *command* is ``None``. 159 | :param terminate_all: If ``True``, terminates all processes in **all** 160 | sandboxes. Only applies when *command* is ``None``. 161 | :param listpids: If ``True``, return string containing line-separated 162 | process ids of all sandboxed processes in sandbox *box* Only 163 | applies when *command* is ``None``. 164 | 165 | .. _Sandboxie's Start Command Line: 166 | http://www.sandboxie.com/index.php?StartCommandLine 167 | """ 168 | if box is None: 169 | box = self.defaultbox 170 | options = ['/box:{0}'.format(box)] 171 | if silent: 172 | options.append('/silent') 173 | if wait: 174 | options.append('/wait') 175 | if nosbiectrl: 176 | options.append('/nosbiectrl') 177 | if elevate: 178 | options.append('/elevate') 179 | if disable_forced: 180 | options.append('/dfp') 181 | 182 | if command is None: 183 | if reload: 184 | options.append('/reload') 185 | if terminate: 186 | options.append('/terminate') 187 | if terminate_all: 188 | options.append('/terminate_all') 189 | if listpids: 190 | options.append('/listpids') 191 | # Since Start.exe is not a command-line application, its output 192 | # does not appear on the command-line unless it is piped into 193 | # more. 194 | command = '| more' 195 | 196 | start_exe = os.path.join(self.install_dir, 'Start.exe') 197 | command = command or '' 198 | return self._shell_output([start_exe] + options + [command]) 199 | 200 | def reload_config(self, **kwargs): 201 | """Reloads the Sandboxie.ini config.""" 202 | self.start(reload=True, **kwargs) 203 | 204 | def delete_contents(self, box=None, **kwargs): 205 | """Deletes the contents of sandbox *box*. If *box* is ``None``, 206 | ``self.defaultbox`` is used. 207 | """ 208 | self.start('delete_sandbox_silent', box=None, **kwargs) 209 | 210 | def terminate_processes(self, box=None, **kwargs): 211 | """Terminates all processes running in sandbox *box* If *box* is 212 | ``None``, ``self.defaultbox`` is used.""" 213 | self.start(terminate=True, box=box, **kwargs) 214 | 215 | def terminate_all_processes(self, **kwargs): 216 | """Terminates all processes running in **all** sandboxes.""" 217 | self.start(terminate_all=True, **kwargs) 218 | 219 | def running_processes(self, box=None, **kwargs): 220 | """Returns a generator of integer process ids for each process running 221 | in sandbox *box*. If *box* is ``None``, ``self.defaultbox`` is used. 222 | """ 223 | output = self.start(listpids=True, box=box, wait=True, **kwargs) 224 | return (int(pid) for pid in output.split()) 225 | -------------------------------------------------------------------------------- /unit_tests.py: -------------------------------------------------------------------------------- 1 | from __future__ import unicode_literals 2 | 3 | import configparser 4 | import contextlib 5 | import io 6 | import os 7 | import shutil 8 | import subprocess 9 | import tempfile 10 | import types 11 | import unittest 12 | 13 | import mock 14 | 15 | from sandboxie import Sandboxie, SandboxieError 16 | 17 | 18 | class SandboxieUnitTests(unittest.TestCase): 19 | def setUp(self): 20 | os.environ = {'WinDir': 'does_not_exist'} 21 | self.config_dir = tempfile.mkdtemp() 22 | self.config_path = os.path.join(self.config_dir, 'Sandboxie.ini') 23 | with io.open(self.config_path, 'w'): 24 | pass 25 | self.sbie = Sandboxie(install_dir=self.config_dir) 26 | 27 | def tearDown(self): 28 | shutil.rmtree(self.config_dir) 29 | 30 | @property 31 | def default_start_options(self): 32 | return set(['/box:{0}'.format(self.sbie.defaultbox), 33 | '/silent', '/nosbiectrl']) 34 | 35 | def _build_command_matcher(self, command, options): 36 | start_exe = os.path.join(self.sbie.install_dir, 'Start.exe') 37 | return SandboxieStartCommandMatcher(start_exe, command, options) 38 | 39 | def _test_start(self, expected_options, command='test.exe', **startkwargs): 40 | self.sbie._shell_output = mock.Mock() 41 | self.sbie.start(command, **startkwargs) 42 | matcher = self._build_command_matcher(command, expected_options) 43 | self.sbie._shell_output.assert_called_once(matcher) 44 | 45 | def test_init_raises_when_sandboxie_config_not_found(self): 46 | try: 47 | Sandboxie() 48 | except SandboxieError: 49 | pass 50 | else: 51 | self.fail() 52 | 53 | def test_init_finds_config_in_windows_dir(self): 54 | os.environ['WinDir'] = self.config_dir 55 | sbie = Sandboxie() 56 | self.assertEqual(sbie.config_path, self.config_path) 57 | 58 | def test_init_finds_config_in_install_dir(self): 59 | sbie = Sandboxie(install_dir=self.config_dir) 60 | self.assertEqual(sbie.install_dir, self.config_dir) 61 | self.assertEqual(sbie.config_path, self.config_path) 62 | 63 | def test_init_default_install_dir(self): 64 | os.environ['WinDir'] = self.config_dir 65 | sbie = Sandboxie() 66 | self.assertEqual(sbie.install_dir, r'C:\Program Files\Sandboxie') 67 | 68 | def test_init_install_dir_from_env_var(self): 69 | os.environ['SANDBOXIE_INSTALL_DIR'] = 'some_dir' 70 | os.environ['WinDir'] = self.config_dir 71 | sbie = Sandboxie() 72 | self.assertEqual(sbie.install_dir, 'some_dir') 73 | self.assertEqual(sbie.config_path, self.config_path) 74 | 75 | def test_create_sandbox(self): 76 | os.environ['WinDir'] = self.config_dir 77 | sbie = Sandboxie() 78 | sbie.start = mock.Mock() 79 | sbie.create_sandbox('foo', {'Enabled': 'yes'}) 80 | 81 | with io.open(self.config_path, 'r', encoding='utf-16-le') as f: 82 | config = configparser.ConfigParser() 83 | config.read_file(f) 84 | self.assertEqual(config['foo']['Enabled'], 'yes') 85 | 86 | sbie.start.assert_called_once(reload=True) 87 | 88 | def test_destroy_sandbox(self): 89 | config = configparser.ConfigParser() 90 | config['sandbox1'] = {'Enabled': 'yes'} 91 | config['sandbox2'] = {'Enabled': 'no'} 92 | 93 | with io.open(self.config_path, 'w', encoding='utf-16-le') as f: 94 | config.write(f) 95 | 96 | os.environ['WinDir'] = self.config_dir 97 | sbie = Sandboxie() 98 | sbie.start = mock.Mock() 99 | sbie.destroy_sandbox('sandbox1') 100 | 101 | with io.open(self.config_path, 'r', encoding='utf-16-le') as f: 102 | config = configparser.ConfigParser() 103 | config.read_file(f) 104 | self.assertEqual(config['sandbox2']['Enabled'], 'no') 105 | try: 106 | config.get('sandbox1', 'Enabled') 107 | except configparser.NoSectionError: 108 | pass 109 | else: 110 | self.fail() 111 | 112 | sbie.start.assert_called_once(reload=True) 113 | 114 | def test_reload_delegates_to_start(self): 115 | self.sbie.start = mock.Mock() 116 | self.sbie.reload_config() 117 | self.sbie.start.assert_called_once(reload=True) 118 | 119 | def test_delete_contents_delegates_to_start(self): 120 | self.sbie = Sandboxie(install_dir=self.config_dir) 121 | self.sbie.start = mock.Mock() 122 | self.sbie.delete_contents() 123 | self.sbie.start.assert_called_once('delete_sandbox_silent', box=None) 124 | 125 | def test_terminate_processes_delegates_to_start(self): 126 | self.sbie.start = mock.Mock() 127 | self.sbie.terminate_processes() 128 | self.sbie.start.assert_called_once(terminate=True, box=None) 129 | 130 | def test_terminate_all_processes_delegates_to_start(self): 131 | self.sbie.start = mock.Mock() 132 | self.sbie.terminate_all_processes() 133 | self.sbie.start.assert_called_once(terminate_all=True) 134 | 135 | def test_running_processes_delegates_to_start(self): 136 | self.sbie.start = mock.Mock() 137 | pidlist = '\r\n'.join(('13', '2705', '1336', '2914')) 138 | self.sbie.start.return_value = pidlist 139 | self.sbie.running_processes() 140 | self.sbie.start.assert_called_once(listpids=True, box=None) 141 | 142 | def test_running_processes_returns_pidlist_generator(self): 143 | self.sbie._shell_output = mock.Mock() 144 | pidlist = '\r\n'.join(('13', '2705', '1336', '2914')) 145 | self.sbie._shell_output.return_value = pidlist 146 | pid_generator = self.sbie.running_processes() 147 | self.assertEqual(type(pid_generator), types.GeneratorType) 148 | self.assertEqual(tuple(pid_generator), (13, 2705, 1336, 2914)) 149 | 150 | def test_start_command_with_spaces(self): 151 | self._test_start(self.default_start_options, 152 | 'ping www.google.com -c 5') 153 | 154 | def test_start_custom_defaultbox(self): 155 | self.sbie.defaultbox = 'somebox' 156 | self._test_start(self.default_start_options, 'test.exe') 157 | 158 | def test_start_box_param_overrides_defaultbox(self): 159 | self.sbie.defaultbox = 'somebox' 160 | expected_options = self.default_start_options 161 | expected_options.remove('/box:somebox') 162 | expected_options.add('/box:anotherbox') 163 | self._test_start(expected_options, 'test.exe', box='anotherbox') 164 | 165 | def test_start_silent_true(self): 166 | self._test_start(self.default_start_options, silent=True) 167 | 168 | def test_start_silent_false(self): 169 | expected_options = self.default_start_options - set(['/silent']) 170 | self._test_start(expected_options, silent=False) 171 | 172 | def test_start_wait_true(self): 173 | expected_options = self.default_start_options | set(['/wait']) 174 | self._test_start(self.default_start_options, wait=True) 175 | 176 | def test_start_wait_false(self): 177 | self._test_start(self.default_start_options, wait=False) 178 | 179 | def test_start_nosbiectrl_true(self): 180 | self._test_start(self.default_start_options, nosbiectrl=True) 181 | 182 | def test_start_nosbiectrl_false(self): 183 | expected_options = self.default_start_options - set(['/nosbiectrl']) 184 | self._test_start(expected_options, nosbiectrl=False) 185 | 186 | def test_start_elevate_true(self): 187 | expected_options = self.default_start_options | set(['/elevate']) 188 | self._test_start(expected_options, elevate=True) 189 | 190 | def test_start_elevate_false(self): 191 | self._test_start(self.default_start_options, elevate=False) 192 | 193 | def test_start_disable_forced_true(self): 194 | expected_options = self.default_start_options | set(['/dfp']) 195 | self._test_start(expected_options, disable_forced=True) 196 | 197 | def test_start_disable_forced_false(self): 198 | self._test_start(self.default_start_options, disable_forced=False) 199 | 200 | def test_start_reload_ignored_when_command_not_None(self): 201 | self._test_start(self.default_start_options, reload=True) 202 | 203 | def test_start_reload_not_ignored_when_command_is_none(self): 204 | expected_options = self.default_start_options | set(['/reload']) 205 | self._test_start(expected_options, command=None, reload=True) 206 | 207 | def test_start_terminate_ignored_when_command_not_none(self): 208 | self._test_start(self.default_start_options, terminate=True) 209 | 210 | def test_start_terminate_not_ignored_when_command_is_none(self): 211 | expected_options = self.default_start_options | set(['/terminate']) 212 | self._test_start(expected_options, command=None, terminate=True) 213 | 214 | def test_start_terminate_all_ignored_when_command_not_none(self): 215 | self._test_start(self.default_start_options, terminate_all=True) 216 | 217 | def test_start_terminate_all_not_ignored_when_command_is_none(self): 218 | expected_options = self.default_start_options | set(['/terminate_all']) 219 | self._test_start(expected_options, command=None, terminate_all=True) 220 | 221 | 222 | class SandboxieStartCommandMatcher(object): 223 | def __init__(self, start_exe, command, options): 224 | self.start_exe = start_exe 225 | self.command = command 226 | self.options = options 227 | 228 | def __eq__(self, command_string): 229 | import re 230 | # "(/path/to/Start.exe)" (/option1 /option2 /option3 ...) (command) 231 | command_string_regex = r'"(.+)" ((?:/[^\s]+\s?)*) (.+)?' 232 | groups = re.match(command_string_regex, command_string).groups() 233 | start_exe, options, command = groups[0], groups[1].split(), groups[2] 234 | return (self.start_exe == start_exe 235 | and set(self.options) == set(options) 236 | and self.command == command) 237 | 238 | def __repr__(self): 239 | return '"{0}" {1} {2}'.format(self.start_exe, self.options, 240 | self.command) 241 | 242 | 243 | if __name__ == '__main__': 244 | unittest.main() 245 | --------------------------------------------------------------------------------