예제 #1
0
def test_script_to_fs_script_too_long():
    """
    Test script_to_fs when the script is too long and won't fit.
    """
    script = (b'shouldfit' * 3023)[:-1]
    _ = uflash.script_to_fs(script, uflash._MICROBIT_ID_V1)

    script += b'1'
    with pytest.raises(ValueError) as ex:
        _ = uflash.script_to_fs(script, uflash._MICROBIT_ID_V1)
    assert 'Python script must be less than' in ex.value.args[0]
예제 #2
0
def test_script_to_fs_two_chunks():
    """
    Test script_to_fs with a script that takes two chunks for V1 and V2.
    """
    expected_result_v1 = '\n'.join(TEST_SCRIPT_FS_V1_HEX_LIST + [''])
    expected_result_v2 = '\n'.join(TEST_SCRIPT_FS_V2_HEX_LIST + [''])

    with mock.patch('uflash._FS_START_ADDR_V1', 0x38C00), \
            mock.patch('uflash._FS_END_ADDR_V1', 0x3F800):
        result_v1 = uflash.script_to_fs(TEST_SCRIPT_FS, uflash._MICROBIT_ID_V1)
        result_v2 = uflash.script_to_fs(TEST_SCRIPT_FS, uflash._MICROBIT_ID_V2)

    assert result_v1 == expected_result_v1
    assert result_v2 == expected_result_v2
예제 #3
0
def test_script_to_fs_short():
    """
    Test script_to_fs with a script smaller than a fs chunk.
    """
    script = b"Very short example"
    expected_result = "\n".join(
        [
            ":020000040003F7",
            ":108C0000FE1B076D61696E2E70795665727920734F",
            ":108C1000686F7274206578616D706C65FFFFFFFF8F",
            ":108C2000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF54",
            ":108C3000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF44",
            ":108C4000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF34",
            ":108C5000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF24",
            ":108C6000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF14",
            ":108C7000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF04",
            ":01F80000FD0A",
            "",
        ]
    )

    with mock.patch("uflash._FS_START_ADDR_V1", 0x38C00), mock.patch(
        "uflash._FS_END_ADDR_V1", 0x3F800
    ):
        result = uflash.script_to_fs(script, uflash._MICROBIT_ID_V1)

    assert result == expected_result, script
예제 #4
0
def test_script_to_fs_unknown_microbit_id():
    """
    Test script_to_fs when the micro:bit ID is not recognised.
    """
    with pytest.raises(ValueError) as ex:
        _ = uflash.script_to_fs(TEST_SCRIPT_FS, '1234')

    assert 'Incompatible micro:bit ID found: 1234' in ex.value.args[0]
예제 #5
0
def test_script_to_fs_line_endings():
    """
    Test script_to_fs converts line endings before embedding script.
    """
    script_win_lines = TEST_SCRIPT_FS.replace(b'\n', b'\r\n')
    script_cr_lines = TEST_SCRIPT_FS.replace(b'\n', b'\r')
    expected_result = '\n'.join(TEST_SCRIPT_FS_V1_HEX_LIST + [''])

    with mock.patch('uflash._FS_START_ADDR_V1', 0x38C00), \
            mock.patch('uflash._FS_END_ADDR_V1', 0x3F800):
        result_win = uflash.script_to_fs(script_win_lines,
                                         uflash._MICROBIT_ID_V1)
        result_cr = uflash.script_to_fs(script_cr_lines,
                                        uflash._MICROBIT_ID_V1)

    assert result_win == expected_result
    assert result_cr == expected_result
예제 #6
0
def test_script_to_fs():
    """
    Test script_to_fs with a random example without anything special about it.
    """
    script = b"A" * 364
    expected_result = "\n".join(
        [
            ":020000040003F7",
            ":108C0000FE79076D61696E2E7079414141414141A4",
            ":108C10004141414141414141414141414141414144",
            ":108C20004141414141414141414141414141414134",
            ":108C30004141414141414141414141414141414124",
            ":108C40004141414141414141414141414141414114",
            ":108C50004141414141414141414141414141414104",
            ":108C600041414141414141414141414141414141F4",
            ":108C70004141414141414141414141414141410223",
            ":108C80000141414141414141414141414141414114",
            ":108C900041414141414141414141414141414141C4",
            ":108CA00041414141414141414141414141414141B4",
            ":108CB00041414141414141414141414141414141A4",
            ":108CC0004141414141414141414141414141414194",
            ":108CD0004141414141414141414141414141414184",
            ":108CE0004141414141414141414141414141414174",
            ":108CF00041414141414141414141414141414103A2",
            ":108D00000241414141414141414141414141414192",
            ":108D10004141414141414141414141414141414143",
            ":108D20004141414141414141414141414141414133",
            ":108D30004141414141414141414141414141414123",
            ":108D40004141414141414141414141414141414113",
            ":108D50004141414141414141414141414141414103",
            ":108D600041414141414141414141414141414141F3",
            ":108D700041414141414141414141FFFFFFFFFFFF6F",
            ":01F80000FD0A",
            "",
        ]
    )

    with mock.patch("uflash._FS_START_ADDR_V1", 0x38C00), mock.patch(
        "uflash._FS_END_ADDR_V1", 0x3F800
    ):
        result = uflash.script_to_fs(script, uflash._MICROBIT_ID_V1)

    assert result == expected_result, script
예제 #7
0
def test_script_to_fs():
    """
    Test script_to_fs with a random example without anything special about it.
    """
    script = b'A' * 364
    expected_result = '\n'.join([
        ':020000040003F7',
        ':108C0000FE79076D61696E2E7079414141414141A4',
        ':108C10004141414141414141414141414141414144',
        ':108C20004141414141414141414141414141414134',
        ':108C30004141414141414141414141414141414124',
        ':108C40004141414141414141414141414141414114',
        ':108C50004141414141414141414141414141414104',
        ':108C600041414141414141414141414141414141F4',
        ':108C70004141414141414141414141414141410223',
        ':108C80000141414141414141414141414141414114',
        ':108C900041414141414141414141414141414141C4',
        ':108CA00041414141414141414141414141414141B4',
        ':108CB00041414141414141414141414141414141A4',
        ':108CC0004141414141414141414141414141414194',
        ':108CD0004141414141414141414141414141414184',
        ':108CE0004141414141414141414141414141414174',
        ':108CF00041414141414141414141414141414103A2',
        ':108D00000241414141414141414141414141414192',
        ':108D10004141414141414141414141414141414143',
        ':108D20004141414141414141414141414141414133',
        ':108D30004141414141414141414141414141414123',
        ':108D40004141414141414141414141414141414113',
        ':108D50004141414141414141414141414141414103',
        ':108D600041414141414141414141414141414141F3',
        ':108D700041414141414141414141FFFFFFFFFFFF6F',
        ':01F80000FD0A',
        '',
    ])

    with mock.patch('uflash._FS_START_ADDR_V1', 0x38C00), \
            mock.patch('uflash._FS_END_ADDR_V1', 0x3F800):
        result = uflash.script_to_fs(script, uflash._MICROBIT_ID_V1)

    assert result == expected_result, script
예제 #8
0
def test_script_to_fs_short():
    """
    Test script_to_fs with a script smaller than a fs chunk.
    """
    script = b'Very short example'
    expected_result = '\n'.join([
        ':020000040003F7',
        ':108C0000FE1B076D61696E2E70795665727920734F',
        ':108C1000686F7274206578616D706C65FFFFFFFF8F',
        ':108C2000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF54',
        ':108C3000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF44',
        ':108C4000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF34',
        ':108C5000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF24',
        ':108C6000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF14',
        ':108C7000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF04',
        ':01F80000FD0A',
        '',
    ])

    with mock.patch('uflash._FS_START_ADDR_V1', 0x38C00), \
            mock.patch('uflash._FS_END_ADDR_V1', 0x3F800):
        result = uflash.script_to_fs(script, uflash._MICROBIT_ID_V1)

    assert result == expected_result, script
예제 #9
0
def test_script_to_fs_empty_code():
    """
    Test script_to_fs results an empty string if the input code is empty.
    """
    result = uflash.script_to_fs('', uflash._MICROBIT_ID_V1)
    assert result == ''
예제 #10
0
def test_script_to_fs_chunk_boundary():
    """
    Test script_to_fs edge case with the taking exactly one chunk.
    """
    script_short = b'This is an edge case test to fill the last byte of ' \
                   b'the first chunk.\n' + (b'A' * 48)
    expected_result_short = '\n'.join([
        ':020000040003F7',
        ':108C0000FE7D076D61696E2E707954686973206905',
        ':108C10007320616E206564676520636173652074ED',
        ':108C200065737420746F2066696C6C2074686520AD',
        ':108C30006C6173742062797465206F662074686556',
        ':108C4000206669727374206368756E6B2E0A4141E9',
        ':108C50004141414141414141414141414141414104',
        ':108C600041414141414141414141414141414141F4',
        ':108C70004141414141414141414141414141FFFF68',
        ':01F80000FD0A',
        '',
    ])
    script_exact = b'This is an edge case test to fill the last byte of ' \
                   b'the first chunk.\n' + (b'A' * 49)
    expected_result_exact = '\n'.join([
        ':020000040003F7',
        ':108C0000FE00076D61696E2E707954686973206982',
        ':108C10007320616E206564676520636173652074ED',
        ':108C200065737420746F2066696C6C2074686520AD',
        ':108C30006C6173742062797465206F662074686556',
        ':108C4000206669727374206368756E6B2E0A4141E9',
        ':108C50004141414141414141414141414141414104',
        ':108C600041414141414141414141414141414141F4',
        ':108C70004141414141414141414141414141410223',
        ':108C800001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2',
        ':108C9000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE4',
        ':108CA000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD4',
        ':108CB000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC4',
        ':108CC000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB4',
        ':108CD000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA4',
        ':108CE000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF94',
        ':108CF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF84',
        ':01F80000FD0A',
        '',
    ])
    script_large = b'This is an edge case test to fill the last byte of ' \
                   b'the first chunk.\n' + (b'A' * 50)
    expected_result_large = '\n'.join([
        ':020000040003F7',
        ':108C0000FE01076D61696E2E707954686973206981',
        ':108C10007320616E206564676520636173652074ED',
        ':108C200065737420746F2066696C6C2074686520AD',
        ':108C30006C6173742062797465206F662074686556',
        ':108C4000206669727374206368756E6B2E0A4141E9',
        ':108C50004141414141414141414141414141414104',
        ':108C600041414141414141414141414141414141F4',
        ':108C70004141414141414141414141414141410223',
        ':108C80000141FFFFFFFFFFFFFFFFFFFFFFFFFFFFB0',
        ':108C9000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE4',
        ':108CA000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD4',
        ':108CB000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC4',
        ':108CC000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB4',
        ':108CD000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA4',
        ':108CE000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF94',
        ':108CF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF84',
        ':01F80000FD0A',
        '',
    ])

    with mock.patch('uflash._FS_START_ADDR_V1', 0x38C00), \
            mock.patch('uflash._FS_END_ADDR_V1', 0x3F800):
        result_short = uflash.script_to_fs(script_short,
                                           uflash._MICROBIT_ID_V1)
        result_exact = uflash.script_to_fs(script_exact,
                                           uflash._MICROBIT_ID_V1)
        result_large = uflash.script_to_fs(script_large,
                                           uflash._MICROBIT_ID_V1)

    assert result_short == expected_result_short, script_short
    assert result_exact == expected_result_exact, script_exact
    assert result_large == expected_result_large, script_large
예제 #11
0
def test_script_to_fs_chunk_boundary():
    """
    Test script_to_fs edge case with the taking exactly one chunk.
    """
    script_short = (
        b"This is an edge case test to fill the last byte of "
        b"the first chunk.\n" + (b"A" * 48)
    )
    expected_result_short = "\n".join(
        [
            ":020000040003F7",
            ":108C0000FE7D076D61696E2E707954686973206905",
            ":108C10007320616E206564676520636173652074ED",
            ":108C200065737420746F2066696C6C2074686520AD",
            ":108C30006C6173742062797465206F662074686556",
            ":108C4000206669727374206368756E6B2E0A4141E9",
            ":108C50004141414141414141414141414141414104",
            ":108C600041414141414141414141414141414141F4",
            ":108C70004141414141414141414141414141FFFF68",
            ":01F80000FD0A",
            "",
        ]
    )
    script_exact = (
        b"This is an edge case test to fill the last byte of "
        b"the first chunk.\n" + (b"A" * 49)
    )
    expected_result_exact = "\n".join(
        [
            ":020000040003F7",
            ":108C0000FE00076D61696E2E707954686973206982",
            ":108C10007320616E206564676520636173652074ED",
            ":108C200065737420746F2066696C6C2074686520AD",
            ":108C30006C6173742062797465206F662074686556",
            ":108C4000206669727374206368756E6B2E0A4141E9",
            ":108C50004141414141414141414141414141414104",
            ":108C600041414141414141414141414141414141F4",
            ":108C70004141414141414141414141414141410223",
            ":108C800001FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2",
            ":108C9000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE4",
            ":108CA000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD4",
            ":108CB000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC4",
            ":108CC000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB4",
            ":108CD000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA4",
            ":108CE000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF94",
            ":108CF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF84",
            ":01F80000FD0A",
            "",
        ]
    )
    script_large = (
        b"This is an edge case test to fill the last byte of "
        b"the first chunk.\n" + (b"A" * 50)
    )
    expected_result_large = "\n".join(
        [
            ":020000040003F7",
            ":108C0000FE01076D61696E2E707954686973206981",
            ":108C10007320616E206564676520636173652074ED",
            ":108C200065737420746F2066696C6C2074686520AD",
            ":108C30006C6173742062797465206F662074686556",
            ":108C4000206669727374206368756E6B2E0A4141E9",
            ":108C50004141414141414141414141414141414104",
            ":108C600041414141414141414141414141414141F4",
            ":108C70004141414141414141414141414141410223",
            ":108C80000141FFFFFFFFFFFFFFFFFFFFFFFFFFFFB0",
            ":108C9000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE4",
            ":108CA000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD4",
            ":108CB000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC4",
            ":108CC000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB4",
            ":108CD000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA4",
            ":108CE000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF94",
            ":108CF000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF84",
            ":01F80000FD0A",
            "",
        ]
    )

    with mock.patch("uflash._FS_START_ADDR_V1", 0x38C00), mock.patch(
        "uflash._FS_END_ADDR_V1", 0x3F800
    ):
        result_short = uflash.script_to_fs(
            script_short, uflash._MICROBIT_ID_V1
        )
        result_exact = uflash.script_to_fs(
            script_exact, uflash._MICROBIT_ID_V1
        )
        result_large = uflash.script_to_fs(
            script_large, uflash._MICROBIT_ID_V1
        )

    assert result_short == expected_result_short, script_short
    assert result_exact == expected_result_exact, script_exact
    assert result_large == expected_result_large, script_large