Beispiel #1
0
def test_bad_exe():
    """
    Tests error reporting on bad executable.
    """
    with pytest.raises(ir.Errors) as exc_info:
        ir.run1({
            "argv": ["/usr/bin/bogus"],
        })
    assert any("No such file or directory" in e for e in exc_info.value.errors)
Beispiel #2
0
def test_stdout_stderr_merge(tmp_path):
    stderr_path = tmp_path / "stderr"
    res = ir.run1({
        "argv": [str(ir.TEST_EXE), "--exit", "42"],
        "fds": [
            ["stderr", {
                "file": {
                    "path": str(stderr_path)
                }
            }],
            ["stdout", {
                "dup": {
                    "fd": 2
                }
            }],
        ]
    })

    assert res["status"] == 42 << 8
    assert res["exit_code"] == 42
    assert res["signum"] is None
    assert res["core_dump"] is False

    assert stderr_path.read_text() == ("message 0 to stdout\n"
                                       "message 1 to stderr\n"
                                       "message 2 to stdout\n")
Beispiel #3
0
def test_echo(mode, format):
    """
    Tests basic capture of stdout.
    """
    res = ir.run1({
        "argv": ["/bin/echo", "Hello, world.", "How are you?"],
        "fds": [
            ["stdout", {
                "capture": {
                    "mode": mode,
                    "format": format,
                }
            }],
        ]
    })

    assert res["status"] == 0

    stdout = res["fds"]["stdout"]
    text = "Hello, world. How are you?\n"
    if mode == "text":
        assert stdout["text"] == text
    elif mode == "base64":
        assert stdout["encoding"] == "base64"
        assert stdout["text"] == base64.b64encode(text.encode())
Beispiel #4
0
def test_bad_capture_path():
    """
    Tests error reporting for a bad capture file path.
    """
    with pytest.raises(ir.Errors) as exc_info:
        ir.run1({
            "argv": ["/bin/echo", "Hello, world!"],
            "fds": [
                ["stdout", {
                    "file": {
                        "path": "/not/a/valid/path",
                    }
                }],
                ["stderr", {
                    "file": {
                        "path": "/not/a/valid/path/either",
                    }
                }],
            ]
        })
    assert any("failed to set up fd 1" in e for e in exc_info.value.errors)
    assert any("failed to set up fd 2" in e for e in exc_info.value.errors)
Beispiel #5
0
def test_utf8_sanitize(mode):
    """
    Tests capturing invalid UTF-8 as text.
    """
    res = ir.run1({
        "argv": [
            "/usr/bin/printf",
            "abc\200\200def",
        ],
        "fds": [
            ["stdout", {
                "capture": {
                    "mode": mode
                }
            }],
        ],
    })

    assert res["status"] == 0

    out = res["fds"]["stdout"]["text"]
    assert len(out) == 8
    assert out[:3] == "abc"
    assert out[-3:] == "def"
Beispiel #6
0
def test_interleaved(mode):
    """
    Tests interleaved stdout and stderr.
    """
    exe = TEST_DIR / "interleaved.py"
    assert exe.exists()

    res = ir.run1({
        "argv": [
            str(exe),
        ],
        "fds": [[
            "stdout",
            {
                "capture": {
                    "mode": mode,
                    "format": "base64",
                }
            },
        ], [
            "stderr",
            {
                "capture": {
                    "mode": mode,
                    "format": "base64",
                }
            },
        ]]
    })

    assert res["status"] == 0

    out = base64.standard_b64decode(res["fds"]["stdout"]["data"])
    err = base64.standard_b64decode(res["fds"]["stderr"]["data"])
    assert out == b"".join(bytes([i]) * i for i in range(256) if i % 3 != 0)
    assert err == b"".join(bytes([i]) * i for i in range(256) if i % 3 == 0)