Skip to content

Commit

Permalink
Merge pull request #11 from pszem0c/user-mode-args
Browse files Browse the repository at this point in the history
fix: Ommit append option when running in user mode with arguments
  • Loading branch information
Novakov authored Dec 5, 2023
2 parents 9d2bd75 + dff5253 commit 1ca2b57
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/qemu_runner/layer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os.path
import os.path
import re
from configparser import ConfigParser
from dataclasses import dataclass, replace
from pathlib import Path
Expand Down Expand Up @@ -234,7 +235,13 @@ def _yield_args():
yield layer.general.kernel

if layer.general.kernel_cmdline:
yield '-append'
yield layer.general.kernel_cmdline
if not layer.general.mode or layer.general.mode == Mode.System:
yield '-append'
yield layer.general.kernel_cmdline
else:
pattern = re.compile(r"\"[^\"]*\"|\'[^\']*\'|\S+")
args = pattern.findall(layer.general.kernel_cmdline)
for arg in args:
yield arg.strip('"\'')

return list(_yield_args())
12 changes: 12 additions & 0 deletions tests/test_layer_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@
Layer(GeneralSettings(engine='my-engine', kernel='abc.elf', kernel_cmdline='a b c')),
['my-engine', '-kernel', 'abc.elf', '-append', 'a b c']
),
(
Layer(GeneralSettings(engine='my-engine', kernel='abc.elf', mode=Mode.User, kernel_cmdline='a b c')),
['my-engine', 'abc.elf', 'a', 'b', 'c']
),
(
Layer(GeneralSettings(engine='my-engine', kernel='abc.elf', mode=Mode.User, kernel_cmdline='a "b c d" e')),
['my-engine', 'abc.elf', 'a', 'b c d', 'e']
),
(
Layer(GeneralSettings(engine='my-engine', kernel='abc.elf', mode=Mode.User, kernel_cmdline='a \'b c d\' e')),
['my-engine', 'abc.elf', 'a', 'b c d', 'e']
),
(
Layer(
GeneralSettings(engine='my-engine', halted=True, gdb=True, kernel='abc.elf', kernel_cmdline='a b c'),
Expand Down
70 changes: 70 additions & 0 deletions tests/test_runner_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,76 @@ def test_runner_flow_no_args(tmp_path: Path, test_layer: Path):
'-kernel', str(tmp_path / 'abc.elf'),
]

@pytest.fixture()
def test_user_mode_layer(tmp_path: Path) -> Path:
with open(tmp_path / 'test-layer', 'w') as f:
f.write("""
[general]
engine = qemu-aarch64
mode = user
[L]
@ = /some/path
""")

return tmp_path / 'test-layer'


def test_runner_flow_user_mode(tmp_path: Path, test_user_mode_layer: Path):
engine = place_echo_args(tmp_path / 'qemu' / 'qemu-aarch64')

run_make_runner('-l', test_user_mode_layer, '-o', tmp_path / 'test.pyz', cwd=tmp_path)
with with_cwd(tmp_path):
cmdline = capture_runner_cmdline(tmp_path / 'test.pyz', 'abc.elf', 'arg1', 'arg2')

assert cmdline == [
engine,
'-L', '/some/path',
str(tmp_path / 'abc.elf'),
'arg1', 'arg2'
]

def test_runner_flow_user_mode_double_quoted_args(tmp_path: Path, test_user_mode_layer: Path):
engine = place_echo_args(tmp_path / 'qemu' / 'qemu-aarch64')

run_make_runner('-l', test_user_mode_layer, '-o', tmp_path / 'test.pyz', cwd=tmp_path)
with with_cwd(tmp_path):
cmdline = capture_runner_cmdline(tmp_path / 'test.pyz', 'abc.elf', 'arg1', '"arg2 arg3"', 'arg4')

assert cmdline == [
engine,
'-L', '/some/path',
str(tmp_path / 'abc.elf'),
'arg1', 'arg2 arg3', 'arg4'
]

def test_runner_flow_user_mode_single_quoted_args(tmp_path: Path, test_user_mode_layer: Path):
engine = place_echo_args(tmp_path / 'qemu' / 'qemu-aarch64')

run_make_runner('-l', test_user_mode_layer, '-o', tmp_path / 'test.pyz', cwd=tmp_path)
with with_cwd(tmp_path):
cmdline = capture_runner_cmdline(tmp_path / 'test.pyz', 'abc.elf', 'arg1', '\'arg2 arg3\'', 'arg4')

assert cmdline == [
engine,
'-L', '/some/path',
str(tmp_path / 'abc.elf'),
'arg1', 'arg2 arg3', 'arg4'
]

def test_runner_flow_no_args(tmp_path: Path, test_user_mode_layer: Path):
engine = place_echo_args(tmp_path / 'qemu' / 'qemu-aarch64')

run_make_runner('-l', test_user_mode_layer, '-o', tmp_path / 'test.pyz', cwd=tmp_path)
with with_cwd(tmp_path):
cmdline = capture_runner_cmdline(tmp_path / 'test.pyz', 'abc.elf')

assert cmdline == [
engine,
'-L', '/some/path',
str(tmp_path / 'abc.elf')
]


def assert_arg_set_in_cmdline(arg_set: List[str], cmdline: List[str]):
if len(arg_set) == 1:
Expand Down

0 comments on commit 1ca2b57

Please sign in to comment.