/usr/lib/python3/dist-packages/plainbox/impl/secure/test_launcher1.py is in python3-plainbox 0.25-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | # This file is part of Checkbox.
#
# Copyright 2013 Canonical Ltd.
# Written by:
# Sylvain Pineau <sylvain.pineau@canonical.com>
# Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
#
# Checkbox 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.
#
# Checkbox 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 Checkbox. If not, see <http://www.gnu.org/licenses/>.
"""
plainbox.impl.secure.test_launcher1
===================================
Test definitions for plainbox.impl.secure.launcher1 module
"""
from inspect import cleandoc
from unittest import TestCase
import os
from plainbox.impl.job import JobDefinition
from plainbox.impl.secure.launcher1 import TrustedLauncher
from plainbox.impl.secure.launcher1 import main
from plainbox.impl.secure.origin import JobOutputTextSource
from plainbox.impl.secure.providers.v1 import Provider1
from plainbox.impl.secure.providers.v1 import Provider1PlugIn
from plainbox.impl.secure.providers.v1 import all_providers
from plainbox.impl.secure.providers.v1 import get_secure_PROVIDERPATH_list
from plainbox.impl.secure.rfc822 import RFC822Record
from plainbox.testing_utils.io import TestIO
from plainbox.vendor import mock
class TrustedLauncherTests(TestCase):
"""
Unit tests for the TrustedLauncher class that implements much of
plainbox-trusted-launcher-1
"""
def setUp(self):
self.launcher = TrustedLauncher()
def test_init(self):
self.assertEqual(self.launcher._job_list, [])
def test_add_job_list(self):
job = mock.Mock(spec=JobDefinition, name='job')
self.launcher.add_job_list([job])
# Ensure that the job was added correctly
self.assertEqual(self.launcher._job_list, [job])
def test_find_job_when_it_doesnt_work(self):
job = mock.Mock(spec=JobDefinition, name='job')
self.launcher.add_job_list([job])
with self.assertRaises(LookupError) as boom:
self.launcher.find_job('foo')
# Ensure that LookupError is raised if a job cannot be found
self.assertIsInstance(boom.exception, LookupError)
self.assertEqual(boom.exception.args, (
'Cannot find job with checksum foo',))
def test_find_job_when_it_works(self):
job = mock.Mock(spec=JobDefinition, name='job')
self.launcher.add_job_list([job])
# Ensure that the job was found correctly
self.assertIs(self.launcher.find_job(job.checksum), job)
@mock.patch.dict('os.environ', clear=True)
@mock.patch('subprocess.call')
def test_run_shell_from_job(self, mock_call):
# Create a mock job and add it to the launcher
job = mock.Mock(spec=JobDefinition, name='job')
self.launcher.add_job_list([job])
# Create a environment we'll pass (empty)
env = {'key': 'value'}
# Run the tested method
retval = self.launcher.run_shell_from_job(job.checksum, env)
# Ensure that we run the job command via job.shell
mock_call.assert_called_once_with(
[job.shell, '-c', job.command], env=env)
# Ensure that the return value of subprocess.call() is returned
self.assertEqual(retval, mock_call())
@mock.patch.dict('os.environ', clear=True, DISPLAY='foo')
@mock.patch('subprocess.call')
def test_run_shell_from_job_with_env_preserved(self, mock_call):
# Create a mock job and add it to the launcher
job = mock.Mock(spec=JobDefinition, name='job')
self.launcher.add_job_list([job])
# Create a environment we'll pass (empty)
env = {'key': 'value'}
# Run the tested method
retval = self.launcher.run_shell_from_job(job.checksum, env)
# Ensure that we run the job command via job.shell with a preserved env
expected_env = dict(os.environ)
expected_env.update(env)
mock_call.assert_called_once_with(
[job.shell, '-c', job.command], env=expected_env)
# Ensure that the return value of subprocess.call() is returned
self.assertEqual(retval, mock_call())
@mock.patch.dict('os.environ', clear=True)
@mock.patch('plainbox.impl.job.JobDefinition.from_rfc822_record')
@mock.patch('plainbox.impl.secure.launcher1.load_rfc822_records')
@mock.patch('subprocess.check_output')
def test_run_local_job(self, mock_check_output, mock_load_rfc822_records,
mock_from_rfc822_record):
# Create a mock job and add it to the launcher
job = mock.Mock(spec=JobDefinition, name='job', plugin='local')
self.launcher.add_job_list([job])
# Create two mock rfc822 records
record1 = mock.Mock(spec=RFC822Record, name='record')
record2 = mock.Mock(spec=RFC822Record, name='record')
# Ensure that load_rfc822_records() returns some mocked records
mock_load_rfc822_records.return_value = [record1, record2]
# Run the tested method
job_list = self.launcher.run_generator_job(job.checksum, None)
# Ensure that we run the job command via job.shell
mock_check_output.assert_called_with(
[job.shell, '-c', job.command], env={}, universal_newlines=True)
# Ensure that we parse all of the output
mock_load_rfc822_records.assert_called_with(
mock_check_output(), source=JobOutputTextSource(job))
# Ensure that we return the jobs back
self.assertEqual(len(job_list), 2)
self.assertEqual(job_list[0], mock_from_rfc822_record(record1))
self.assertEqual(job_list[1], mock_from_rfc822_record(record2))
class MainTests(TestCase):
"""
Unit tests for the main() function that implements
plainbox-trusted-launcher-1
"""
def setUp(self):
self.provider = mock.Mock(name='provider', spec=Provider1)
all_providers.fake_plugins([
mock.Mock(
name='plugin',
spec=Provider1PlugIn,
plugin_name='{}/fake.provider'.format(
get_secure_PROVIDERPATH_list()[0]),
plugin_object=self.provider)
])
def test_help(self):
"""
verify how `plainbox-trusted-launcher-1 --help` looks like
"""
# Run the program with io intercept
with TestIO(combined=True) as io:
with self.assertRaises(SystemExit) as call:
main(['--help'])
self.assertEqual(call.exception.args, (0,))
self.maxDiff = None
expected = """
usage: plainbox-trusted-launcher-1 [-h] (-w | -t CHECKSUM)
[-T NAME=VALUE [NAME=VALUE ...]]
[-g CHECKSUM]
[-G NAME=VALUE [NAME=VALUE ...]]
Security elevation mechanism for plainbox
optional arguments:
-h, --help show this help message and exit
-w, --warmup return immediately, only useful when used with
pkexec(1)
-t CHECKSUM, --target CHECKSUM
run a job with this checksum
target job specification:
-T NAME=VALUE [NAME=VALUE ...], --target-environment NAME=VALUE [NAME=VALUE ...]
environment passed to the target job
generator job specification:
-g CHECKSUM, --generator CHECKSUM
also run a job with this checksum (assuming it is a
local job)
-G NAME=VALUE [NAME=VALUE ...], --generator-environment NAME=VALUE [NAME=VALUE ...]
environment passed to the generator job
"""
self.assertEqual(io.combined, cleandoc(expected) + "\n")
def test_warmup(self):
"""
verify what `plainbox-trusted-launcher-1 --warmup` does
"""
# Run the program with io intercept
with TestIO(combined=True) as io:
retval = main(['--warmup'])
# Ensure that it just returns 0
self.assertEqual(retval, 0)
# Without printing anything
self.assertEqual(io.combined, '')
def test_run_without_args(self):
"""
verify what `plainbox-trusted-launcher-1` does
"""
# Run the program with io intercept
with TestIO(combined=True) as io:
with self.assertRaises(SystemExit) as call:
main([])
self.assertEqual(call.exception.args, (2,))
expected = """
usage: plainbox-trusted-launcher-1 [-h] (-w | -t CHECKSUM)
[-T NAME=VALUE [NAME=VALUE ...]]
[-g CHECKSUM]
[-G NAME=VALUE [NAME=VALUE ...]]
plainbox-trusted-launcher-1: error: one of the arguments -w/--warmup -t/--target is required
"""
self.assertEqual(io.combined, cleandoc(expected) + "\n")
@mock.patch('plainbox.impl.secure.launcher1.TrustedLauncher')
def test_run_valid_hash(self, mock_launcher):
"""
verify what happens when `plainbox-trusted-launcher-1` is called with
--hash that designates an existing job.
"""
# Create a mock job, give it a predictable checksum
job = mock.Mock(name='job', spec=JobDefinition, checksum='1234')
# Ensure this job is enumerated by the provider
self.provider.job_list = [job]
# Run the program with io intercept
with TestIO(combined=True) as io:
retval = main([
'--target=1234', '-T', 'key=value', '-T', 'other=value'])
# Ensure that the job command was invoked
# and that environment was properly parsed and provided
mock_launcher().run_shell_from_job.assert_called_with(
job.checksum, {'key': 'value', 'other': 'value'})
# Ensure that the return code is propagated
self.assertEqual(retval, mock_launcher().run_shell_from_job())
# Ensure that we didn't print anything (we normally do but this is not
# tested here since we mock that part away)
self.assertEqual(io.combined, '')
@mock.patch('plainbox.impl.secure.launcher1.TrustedLauncher')
def test_run_valid_hash_and_via(self, mock_launcher):
"""
verify what happens when `plainbox-trusted-launcher-1` is called with
both --hash and --via that both are okay and designate existing jobs.
"""
# Create a mock (local) job, give it a predictable checksum
local_job = mock.Mock(
name='local_job',
spec=JobDefinition,
checksum='5678')
# Create a mock (target) job, give it a predictable checksum
target_job = mock.Mock(
name='target_job',
spec=JobDefinition,
checksum='1234')
# Ensure this local job is enumerated by the provider
self.provider.job_list = [local_job]
# Ensure that the target job is generated by the local job
mock_launcher.run_local_job.return_value = [target_job]
# Run the program with io intercept
with TestIO(combined=True) as io:
retval = main(['--target=1234', '--generator=5678'])
# Ensure that the local job command was invoked
mock_launcher().run_generator_job.assert_called_with(local_job.checksum, None)
# Ensure that the target job command was invoked
mock_launcher().run_shell_from_job.assert_called_with(
target_job.checksum, None)
# Ensure that the return code is propagated
self.assertEqual(retval, mock_launcher().run_shell_from_job())
# Ensure that we didn't print anything (we normally do but this is not
# tested here since we mock that part away)
self.assertEqual(io.combined, '')
def test_run_invalid_target_checksum(self):
"""
verify what happens when `plainbox-trusted-launcher-1` is called with a
target job checksum that cannot be found in any of the providers.
"""
# Ensure this there are no jobs that the launcher knows about
self.provider.job_list = []
# Run the program with io intercept
with TestIO(combined=True) as io:
with self.assertRaises(SystemExit) as call:
main(['--target=1234'])
# Ensure that the error message contains the checksum of the target job
self.assertEqual(call.exception.args, (
'Cannot find job with checksum 1234',))
self.assertEqual(io.combined, '')
def test_run_invalid_generator_checksum(self):
"""
verify what happens when `plainbox-trusted-launcher-1` is called with a
generator job checksum that cannot be found in any of the providers.
"""
# Ensure this there are no jobs that the launcher knows about
self.provider.job_list = []
# Run the program with io intercept
with TestIO(combined=True) as io:
with self.assertRaises(SystemExit) as call:
main(['--target=1234', '--generator=4567'])
# Ensure that the error message contains the checksum of the via job
self.assertEqual(call.exception.args, (
'Cannot find job with checksum 4567',))
# Ensure that we didn't print anything (we normally do but this is not
# tested here since we mock that part away)
self.assertEqual(io.combined, '')
def test_run_invalid_env(self):
"""
verify what happens when `plainbox-trusted-launcher-1` is called with a
checksum that cannot be found in any of the providers.
"""
# Run the program with io intercept
with TestIO(combined=True) as io:
with self.assertRaises(SystemExit) as call:
main(['--target=1234', '-T', 'blarg'])
# Ensure that we exit with an error code
self.assertEqual(call.exception.args, (2,))
# Ensure that we print a meaningful error message
expected = """
usage: plainbox-trusted-launcher-1 [-h] (-w | -t CHECKSUM)
[-T NAME=VALUE [NAME=VALUE ...]]
[-g CHECKSUM]
[-G NAME=VALUE [NAME=VALUE ...]]
plainbox-trusted-launcher-1: error: argument -T/--target-environment: expected NAME=VALUE
"""
self.assertEqual(io.combined, cleandoc(expected) + "\n")
|