Esempio n. 1
0
def test_simple_expect3(ch: channel.Channel) -> None:
    ch.sendline("echo Lo1337rem")
    res = ch.expect(["Dolor", "roloD", tbot.Re(r"Lo(\d{1,20})"), "rem"])
    assert res.i == 2
    assert isinstance(res.match, Match), "Not a match object"
    assert res.match.group(1) == b"1337"
Esempio n. 2
0
def selftest_machine_channel(lab: typing.Optional[linux.Lab] = None, ) -> None:
    with channel.SubprocessChannel() as ch:
        ch.read()
        # Test a simple command
        ch.sendline("echo Hello World", read_back=True)
        out = ch.read()
        assert out.startswith(b"Hello World"), repr(out)

    with channel.SubprocessChannel() as ch:
        ch.read()
        # Test reading
        ch.write(b"1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ")
        out = ch.read(10)
        assert out == b"1234567890", repr(out)

        out = ch.read()
        assert out == b"ABCDEFGHIJKLMNOPQRSTUVWXYZ", repr(out)

    with channel.SubprocessChannel() as ch:
        ch.read()
        # Test read iter
        ch.write(b"12345678901234567890")
        final = bytearray()
        for new in ch.read_iter(10):
            final.extend(new)
        assert final == b"1234567890", repr(final)
        for i in range(1, 10):
            c = ch.read(1)
            assert c == str(i).encode("utf-8"), repr(c)

    with channel.SubprocessChannel() as ch:
        ch.read()
        # Test readline
        ch.sendline("echo Hello; echo World", read_back=True)
        out_s = ch.readline()
        assert out_s == "Hello\n", repr(out)
        out_s = ch.readline()
        assert out_s == "World\n", repr(out)

    # Test expect
    with channel.SubprocessChannel() as ch:
        ch.read()
        ch.sendline("echo Lorem Ipsum")
        res = ch.expect(["Lol", "Ip"])
        assert res.i == 1, repr(res)
        assert res.match == "Ip", repr(res)

    with channel.SubprocessChannel() as ch:
        ch.read()
        ch.sendline("echo Lorem Ipsum Dolor Sit")
        res = ch.expect(["Lol", "Dolor", "Dol"])
        assert res.i == 1, repr(res)
        assert res.match == "Dolor", repr(res)

    with channel.SubprocessChannel() as ch:
        ch.read()
        ch.sendline("echo Lo1337rem")
        res = ch.expect(["Dolor", "roloD", tbot.Re(r"Lo(\d{1,20})"), "rem"])
        assert res.i == 2, repr(res)
        assert isinstance(res.match, typing.Match), "Not a match object"
        assert res.match.group(1) == b"1337", repr(res)

    with channel.SubprocessChannel() as ch:
        # Test Borrow
        ch.sendline("echo Hello")

        with ch.borrow() as ch2:
            ch2.sendline("echo World")

            raised = False
            try:
                ch.sendline("echo Illegal")
            except channel.ChannelBorrowedException:
                raised = True

            assert raised, "Borrow was unsuccessful"

        ch.sendline("echo back again")

    with channel.SubprocessChannel() as ch:
        # Test Move
        ch.sendline("echo Hello")

        ch2 = ch.take()
        ch2.sendline("echo World")

        raised = False
        try:
            ch.sendline("echo Illegal")
        except channel.ChannelTakenException:
            raised = True

        assert raised, "Take was unsuccessful"
Esempio n. 3
0
class MockhwBoardUBoot(board.Connector, board.UBootAutobootIntercept,
                       board.UBootShell, tbot.role.Role):
    name = "mockhw-uboot"
    autoboot_prompt = tbot.Re(r"Autoboot: \d{0,10}")
    prompt = "=> "