49 lines
2.2 KiB
Python
49 lines
2.2 KiB
Python
"""Pure framing tests; actual UID/descriptor execution lives in regression fixture."""
|
|
import pathlib
|
|
import struct
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
|
import runtime_owner as owner
|
|
|
|
|
|
def frame(argv, env=(), cwd='/'):
|
|
def string(value):
|
|
raw = value.encode()
|
|
return struct.pack('!I', len(raw)) + raw
|
|
return struct.pack('!IIII', owner.MAGIC, 1, len(argv), len(env)) + string(cwd) + b''.join(map(string, [*argv, *env]))
|
|
|
|
|
|
class FrameTests(unittest.TestCase):
|
|
def test_exact_arguments_environment_and_unicode(self):
|
|
raw = frame(['python3', '-c', 'literal $value; nothing interpreted', 'video ü name'],
|
|
['CC_aarch64-linux-android=/ndk/clang', 'TOKEN=a=b'], '/work folder')
|
|
self.assertEqual(owner.decode(raw), (['python3', '-c', 'literal $value; nothing interpreted', 'video ü name'],
|
|
{'CC_aarch64-linux-android': '/ndk/clang', 'TOKEN': 'a=b'}, '/work folder'))
|
|
|
|
def test_truncations_extras_invalid_utf8_and_duplicate_env_refuse(self):
|
|
raw = frame(['true'])
|
|
for end in range(len(raw)):
|
|
with self.subTest(end=end), self.assertRaises(ValueError):
|
|
owner.decode(raw[:end])
|
|
for bad in (raw + b'x', frame(['']), frame(['true'], ['A=1', 'A=2']),
|
|
frame(['true'], ['=x']), frame(['true'], ['missing']),
|
|
frame(['true'], cwd='relative'), frame(['x\0y']), raw[:-1] + b'\xff'):
|
|
with self.subTest(bad=bad), self.assertRaises(ValueError):
|
|
owner.decode(bad)
|
|
|
|
def test_protocol_and_allocation_bounds(self):
|
|
raw = frame(['true'])
|
|
for bad in (b'x' * (owner.MAX_PACKET + 1), struct.pack('!IIII', owner.MAGIC, 1, 257, 0),
|
|
struct.pack('!IIII', owner.MAGIC, 1, 0, 0),
|
|
struct.pack('!IIII', owner.MAGIC, 1, 1, 129),
|
|
struct.pack('!I', 0) + raw[4:], raw[:4] + struct.pack('!I', 2) + raw[8:],
|
|
raw[:16] + struct.pack('!I', 2**32-1) + raw[20:]):
|
|
with self.subTest(size=len(bad)), self.assertRaises(ValueError):
|
|
owner.decode(bad)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|