#!/usr/bin/python3 # # test-python-virtualenv.py quality assurance test script for python-virtualenv # Copyright (C) 2025 Canonical Ltd. # Author: Octavio Galland # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3, # as published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # QRT-Packages: virtualenv # QRT-Packages-Dependencies: bash python3-pil from __future__ import print_function import os import subprocess import sys import unittest import testlib import shutil try: from private.qrt.PyhonVirtualenv import PrivatePyhonVirtualenvTest except ImportError: class PrivatePyhonVirtualenvTest(object): '''Empty class''' print("Skipping private tests", file=sys.stdout) class PyhonVirtualenvTest(testlib.TestlibCase, PrivatePyhonVirtualenvTest): '''Test virtualenv package.''' def setUp(self): '''Tests will require chaging directories, so keep track of starting directory''' self._original_directory = os.getcwd() def tearDown(self): '''Go back to starting directory after each test, in case it failed/crashed before performing cleanup''' os.chdir(self._original_directory) def runInsideVirtualenv(self, environment_name, venv_cmd, before_cmd=':'): '''Helper function to assert that a given command runs sucessfully inside a virtual environment''' # delete old virtual env, if any try: shutil.rmtree(environment_name) except FileNotFoundError: pass # create virtual env previous_dir = os.getcwd() os.mkdir(environment_name) os.chdir(environment_name) self.assertShellExitSuccess(['virtualenv', '.']) # activate the environment, run the provided command, and deactivate activate_path = os.path.join('bin', 'activate') bash_cmd = '%s && source %s && %s && deactivate' % (before_cmd, activate_path, venv_cmd) self.assertShellExitSuccess(['bash', '-ic', bash_cmd]) # clean up os.chdir(previous_dir) shutil.rmtree(environment_name) def test_interpreter_location(self): '''Test python interpreter points into virtual environment''' environment_name = 'test' environment_python = os.path.join(os.getcwd(), environment_name, 'bin', 'python') environment_cmd = 'which python | grep -q "%s"' % (environment_python) self.runInsideVirtualenv(environment_name, environment_cmd) def test_interpreter_works(self): '''Test python interpreter points into virtual environment''' self.runInsideVirtualenv('test', 'python -c "import os"') def test_prompt(self): '''Test that virtual environment has the right prompt''' environment_name = 'test' before_cmd = 'OLD_PS1="$PS1"' environment_cmd = '[ "$PS1" = "(%s) $OLD_PS1" ]' % (environment_name) self.runInsideVirtualenv(environment_name, environment_cmd, before_cmd=before_cmd) def test_cve_2024_53899(self): '''Test for command-line injection: CVE-2024-53899''' # the path should contain a command line injection, in this case creating a file, # once inside the virtualenv we can test if this command was executed environment_name = "';touch pwnd;':" self.runInsideVirtualenv(environment_name, '[ ! -f "pwnd" ]') if __name__ == '__main__': unittest.main()