| 1 |
# Copyright 2015 Rackspace Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
from unittest import mock |
| 16 |
|
| 17 |
from oslotest import base |
| 18 |
|
| 19 |
from oslo_privsep import capabilities |
| 20 |
|
| 21 |
|
| 22 |
class TestCapabilities(base.BaseTestCase): |
| 23 |
@mock.patch('oslo_privsep.capabilities._prctl') |
| 24 |
def test_set_keepcaps_error(self, mock_prctl): |
| 25 |
mock_prctl.return_value = -1 |
| 26 |
self.assertRaises(OSError, capabilities.set_keepcaps, True) |
| 27 |
|
| 28 |
@mock.patch('oslo_privsep.capabilities._prctl') |
| 29 |
def test_set_keepcaps(self, mock_prctl): |
| 30 |
mock_prctl.return_value = 0 |
| 31 |
capabilities.set_keepcaps(True) |
| 32 |
|
| 33 |
# Disappointingly, ffi.cast(type, 1) != ffi.cast(type, 1) |
| 34 |
# so can't just use assert_called_once_with :-( |
| 35 |
self.assertEqual(1, mock_prctl.call_count) |
| 36 |
self.assertCountEqual( |
| 37 |
[8, 1], # [PR_SET_KEEPCAPS, true] |
| 38 |
[int(x) for x in mock_prctl.call_args[0]], |
| 39 |
) |
| 40 |
|
| 41 |
@mock.patch('oslo_privsep.capabilities._capset') |
| 42 |
def test_drop_all_caps_except_error(self, mock_capset): |
| 43 |
mock_capset.return_value = -1 |
| 44 |
self.assertRaises( |
| 45 |
OSError, capabilities.drop_all_caps_except, [0], [0], [0] |
| 46 |
) |
| 47 |
|
| 48 |
@mock.patch('oslo_privsep.capabilities._capset') |
| 49 |
def test_drop_all_caps_except(self, mock_capset): |
| 50 |
mock_capset.return_value = 0 |
| 51 |
|
| 52 |
# Somewhat arbitrary bit patterns to exercise _caps_to_mask |
| 53 |
capabilities.drop_all_caps_except( |
| 54 |
(17, 24, 49), (8, 10, 35, 56), (24, 31, 40) |
| 55 |
) |
| 56 |
|
| 57 |
self.assertEqual(1, mock_capset.call_count) |
| 58 |
hdr, data = mock_capset.call_args[0] |
| 59 |
self.assertEqual( |
| 60 |
0x20071026, # _LINUX_CAPABILITY_VERSION_2 |
| 61 |
hdr.version, |
| 62 |
) |
| 63 |
self.assertEqual(0x01020000, data[0].effective) |
| 64 |
self.assertEqual(0x00020000, data[1].effective) |
| 65 |
self.assertEqual(0x00000500, data[0].permitted) |
| 66 |
self.assertEqual(0x01000008, data[1].permitted) |
| 67 |
self.assertEqual(0x81000000, data[0].inheritable) |
| 68 |
self.assertEqual(0x00000100, data[1].inheritable) |
| 69 |
|
| 70 |
@mock.patch('oslo_privsep.capabilities._capget') |
| 71 |
def test_get_caps_error(self, mock_capget): |
| 72 |
mock_capget.return_value = -1 |
| 73 |
self.assertRaises(OSError, capabilities.get_caps) |
| 74 |
|
| 75 |
@mock.patch('oslo_privsep.capabilities._capget') |
| 76 |
def test_get_caps(self, mock_capget): |
| 77 |
def impl(hdr, data): |
| 78 |
# Somewhat arbitrary bit patterns to exercise _mask_to_caps |
| 79 |
data[0].effective = 0x01020000 |
| 80 |
data[1].effective = 0x00020000 |
| 81 |
data[0].permitted = 0x00000500 |
| 82 |
data[1].permitted = 0x01000008 |
| 83 |
data[0].inheritable = 0x81000000 |
| 84 |
data[1].inheritable = 0x00000100 |
| 85 |
return 0 |
| 86 |
|
| 87 |
mock_capget.side_effect = impl |
| 88 |
|
| 89 |
self.assertCountEqual( |
| 90 |
([17, 24, 49], [8, 10, 35, 56], [24, 31, 40]), |
| 91 |
capabilities.get_caps(), |
| 92 |
) |