Beispiel #1
0
def test_save_hex():
    """
    Ensure the good case works.
    """
    # Ensure we have a temporary file to write to that doesn't already exist.
    path_to_hex = os.path.join(tempfile.gettempdir(), "microbit.hex")
    if os.path.exists(path_to_hex):
        os.remove(path_to_hex)
    assert not os.path.exists(path_to_hex)
    # Create the hex file we want to "flash"
    hex_file = uflash.embed_fs_uhex(uflash._RUNTIME, TEST_SCRIPT)
    # Save the hex.
    uflash.save_hex(hex_file, path_to_hex)
    # Ensure the hex has been written as expected.
    assert os.path.exists(path_to_hex)
    with open(path_to_hex) as written_file:
        assert written_file.read() == hex_file
Beispiel #2
0
def test_flash_has_python_no_path_to_microbit():
    """
    The good case with a path to a Python file. When it's possible to find a
    path to the micro:bit.

    The resulting payload should be a correctly created micropython.hex file.
    """
    with mock.patch("uflash.find_microbit", return_value="foo"):
        with mock.patch("uflash.save_hex") as mock_save:
            uflash.flash("tests/example.py")
            assert mock_save.call_count == 1
            # Create the hex we're expecting to flash onto the device.
            with open("tests/example.py", "rb") as py_file:
                py_code = py_file.read()
            assert py_code
            expected_hex = uflash.embed_fs_uhex(uflash._RUNTIME, py_code)
            assert mock_save.call_args[0][0] == expected_hex
            expected_path = os.path.join("foo", "micropython.hex")
            assert mock_save.call_args[0][1] == expected_path
Beispiel #3
0
def test_embed_fs_uhex():
    """
    Test embed_fs_uhex to add the filesystem into a Universal Hex with standard
    two sections, one for V1 and one for V2.
    """
    uhex = '\n'.join(TEST_UNIVERSAL_HEX_LIST)
    v1_fs_i = 11
    v2_fs_i = 20
    expected_uhex = '\n'.join(TEST_UNIVERSAL_HEX_LIST[:v1_fs_i] +
                              TEST_SCRIPT_FS_V1_HEX_LIST +
                              TEST_UNIVERSAL_HEX_LIST[v1_fs_i:v2_fs_i] +
                              TEST_SCRIPT_FS_V2_HEX_LIST +
                              TEST_UNIVERSAL_HEX_LIST[v2_fs_i:])

    with mock.patch('uflash._FS_START_ADDR_V1', 0x38C00), \
            mock.patch('uflash._FS_END_ADDR_V1', 0x3F800), \
            mock.patch('uflash._FS_START_ADDR_V2', 0x6D000), \
            mock.patch('uflash._FS_END_ADDR_V2', 0x72000):
        uhex_with_fs = uflash.embed_fs_uhex(uhex, TEST_SCRIPT_FS)

    assert expected_uhex == uhex_with_fs
Beispiel #4
0
def test_flash_with_path_to_multiple_microbits():
    """
    Flash the referenced paths to the micro:bit with a hex file generated from
    the MicroPython firmware and the referenced Python script.
    """
    with mock.patch("uflash.save_hex") as mock_save:
        uflash.flash("tests/example.py", ["test_path1", "test_path2"])
        assert mock_save.call_count == 2
        # Create the hex we're expecting to flash onto the device.
        with open("tests/example.py", "rb") as py_file:
            py_code = py_file.read()
        assert py_code
        expected_hex = uflash.embed_fs_uhex(uflash._RUNTIME, py_code)

        assert mock_save.call_args_list[0][0][0] == expected_hex
        expected_path = os.path.join("test_path1", "micropython.hex")
        assert mock_save.call_args_list[0][0][1] == expected_path

        assert mock_save.call_args_list[1][0][0] == expected_hex
        expected_path = os.path.join("test_path2", "micropython.hex")
        assert mock_save.call_args_list[1][0][1] == expected_path
Beispiel #5
0
def test_embed_fs_uhex_empty_code():
    uhex = '\n'.join(TEST_UNIVERSAL_HEX_LIST)

    identical_uhex = uflash.embed_fs_uhex(uhex, '')

    assert identical_uhex == uhex
Beispiel #6
0
def test_embed_fs_uhex_extra_uicr_jump_record():
    uhex_list = [
        # Section for V1 starts
        ':020000040000FA',
        ':0400000A9900C0DEBB',
        ':1000000000400020218E01005D8E01005F8E010006',
        ':1000100000000000000000000000000000000000E0',
        ':10002000000000000000000000000000618E0100E0',
        ':10003000040007609F4203D1042302791343037134',
        ':0888B00095880100C1000000E1',
        # V1 UICR
        ':020000041000EA',
        ':1010C0007CB0EE17FFFFFFFF0A0000000000E30006',
        ':0C10D000FFFFFFFF2D6D0300000000007B',
        # Section for V2 starts
        ':020000040000FA',
        ':0400000A9903C0DEB8',
        ':1000000D00040020810A000015070000610A0000AD',
        ':020000040001F9',
        ':1000000D03D13000F8BD4010F3E7331D0122180082',
        ':1000100DF8F7B2FD4460EFE7E4B30200F0B5070083',
        ':1000200D89B000201E000D00019215F0ECFB0E4B74',
        # V2 UICR with an extra extended linear address record
        ':020000040000FA',
        ':020000041000EA',
        ':0810140D0070070000E0070069',
        # V2 Regions table (this in flash again)
        ':020000040006F4',
        ':102FC00D0100010000B00100000000000000000041',
        ':102FD00D02021C00E46504009CA105000000000035',
        ':102FE00D03006D0000600000000000000000000004',
        ':102FF00DFE307F590100300003000C009DD7B1C198',
        ':00000001FF',
        ''
    ]
    uhex_ela_record = '\n'.join(uhex_list)
    v1_fs_i = 7
    v2_fs_i = 17
    expected_uhex_ela_record = '\n'.join(uhex_list[:v1_fs_i] +
                                         TEST_SCRIPT_FS_V1_HEX_LIST +
                                         uhex_list[v1_fs_i:v2_fs_i] +
                                         TEST_SCRIPT_FS_V2_HEX_LIST +
                                         uhex_list[v2_fs_i:])
    # Replace Extended linear Address with Segmented record
    uhex_list[v2_fs_i] = ':020000020000FC'
    uhex_esa_record = '\n'.join(uhex_list)
    expected_uhex_esa_record = '\n'.join(uhex_list[:v1_fs_i] +
                                         TEST_SCRIPT_FS_V1_HEX_LIST +
                                         uhex_list[v1_fs_i:v2_fs_i] +
                                         TEST_SCRIPT_FS_V2_HEX_LIST +
                                         uhex_list[v2_fs_i:])

    with mock.patch('uflash._FS_START_ADDR_V1', 0x38C00), \
            mock.patch('uflash._FS_END_ADDR_V1', 0x3F800), \
            mock.patch('uflash._FS_START_ADDR_V2', 0x6D000), \
            mock.patch('uflash._FS_END_ADDR_V2', 0x72000):
        uhex_ela_with_fs = uflash.embed_fs_uhex(uhex_ela_record,
                                                TEST_SCRIPT_FS)
        uhex_esa_with_fs = uflash.embed_fs_uhex(uhex_esa_record,
                                                TEST_SCRIPT_FS)

    assert expected_uhex_ela_record == uhex_ela_with_fs
    assert expected_uhex_esa_record == uhex_esa_with_fs
Beispiel #7
0
def test_embed_fs_uhex_extra_uicr_jump_record():
    uhex_list = [
        # Section for V1 starts
        ":020000040000FA",
        ":0400000A9900C0DEBB",
        ":1000000000400020218E01005D8E01005F8E010006",
        ":1000100000000000000000000000000000000000E0",
        ":10002000000000000000000000000000618E0100E0",
        ":10003000040007609F4203D1042302791343037134",
        ":0888B00095880100C1000000E1",
        # V1 UICR
        ":020000041000EA",
        ":1010C0007CB0EE17FFFFFFFF0A0000000000E30006",
        ":0C10D000FFFFFFFF2D6D0300000000007B",
        # Section for V2 starts
        ":020000040000FA",
        ":0400000A9903C0DEB8",
        ":1000000D00040020810A000015070000610A0000AD",
        ":020000040001F9",
        ":1000000D03D13000F8BD4010F3E7331D0122180082",
        ":1000100DF8F7B2FD4460EFE7E4B30200F0B5070083",
        ":1000200D89B000201E000D00019215F0ECFB0E4B74",
        # V2 UICR with an extra extended linear address record
        ":020000040000FA",
        ":020000041000EA",
        ":0810140D0070070000E0070069",
        # V2 Regions table (this in flash again)
        ":020000040006F4",
        ":102FC00D0100010000B00100000000000000000041",
        ":102FD00D02021C00E46504009CA105000000000035",
        ":102FE00D03006D0000600000000000000000000004",
        ":102FF00DFE307F590100300003000C009DD7B1C198",
        ":00000001FF",
        "",
    ]
    uhex_ela_record = "\n".join(uhex_list)
    uhex_ela_record_alignment = len(uhex_ela_record) % 512
    v1_fs_i = 7
    v2_fs_i = 17
    expected_uhex_ela_record = "\n".join(
        uhex_list[:v1_fs_i]
        + TEST_SCRIPT_FS_V1_HEX_LIST
        + TEST_SCRIPT_FS_V1_HEX_PADDING_LIST
        + uhex_list[v1_fs_i:v2_fs_i]
        + TEST_SCRIPT_FS_V2_HEX_LIST
        + TEST_SCRIPT_FS_V2_HEX_PADDING_LIST
        + uhex_list[v2_fs_i:]
    )
    # Replace Extended linear Address with Segmented record
    uhex_list[v2_fs_i] = ":020000020000FC"
    uhex_esa_record = "\n".join(uhex_list)
    uhex_esa_record_alignment = len(uhex_esa_record) % 512
    expected_uhex_esa_record = "\n".join(
        uhex_list[:v1_fs_i]
        + TEST_SCRIPT_FS_V1_HEX_LIST
        + TEST_SCRIPT_FS_V1_HEX_PADDING_LIST
        + uhex_list[v1_fs_i:v2_fs_i]
        + TEST_SCRIPT_FS_V2_HEX_LIST
        + TEST_SCRIPT_FS_V2_HEX_PADDING_LIST
        + uhex_list[v2_fs_i:]
    )

    with mock.patch("uflash._FS_START_ADDR_V1", 0x38C00), mock.patch(
        "uflash._FS_END_ADDR_V1", 0x3F800
    ), mock.patch("uflash._FS_START_ADDR_V2", 0x6D000), mock.patch(
        "uflash._FS_END_ADDR_V2", 0x72000
    ):
        uhex_ela_with_fs = uflash.embed_fs_uhex(
            uhex_ela_record, TEST_SCRIPT_FS
        )
        uhex_esa_with_fs = uflash.embed_fs_uhex(
            uhex_esa_record, TEST_SCRIPT_FS
        )

    assert expected_uhex_ela_record == uhex_ela_with_fs
    assert uhex_ela_record_alignment == (len(uhex_ela_with_fs) % 512)
    assert expected_uhex_esa_record == uhex_esa_with_fs
    assert uhex_esa_record_alignment == (len(uhex_esa_with_fs) % 512)