Exemplo n.º 1
0
def test_fread_reset():
    """Test the reset subindex (DOMAIN, writeonly)"""
    network, node = connect_bus()
    subindex = node.sdo[FREAD_INDEX][RESET_SUBINDEX]

    # writeonly
    with pytest.raises(SdoAbortedError):
        subindex.raw

    # reset deletes file
    fname = FREAD_TMP_DIR + "test.txt"
    with open(fname, "w") as fptr:
        fptr.write("test")
    subindex.raw = "0".encode("utf-8")
    assert not isfile(fname)

    # should never fail
    subindex.raw = "0".encode("utf-8")
    subindex.raw = "0".encode("utf-8")
    subindex.raw = "0".encode("utf-8")

    # reset clear name
    node.sdo[FREAD_INDEX][NAME_SUBINDEX].raw = FREAD_FILE.encode("utf-8")
    subindex.raw = "0".encode("utf-8")
    with pytest.raises(SdoAbortedError):
        node.sdo[FREAD_INDEX][NAME_SUBINDEX].raw

    network.disconnect()
Exemplo n.º 2
0
def test_fread_name():
    """Test the fread filename subindex (DOMAIN, readwrite)."""
    network, node = connect_bus()
    subindex = node.sdo[FREAD_INDEX][NAME_SUBINDEX]

    # read no data
    with pytest.raises(SdoAbortedError):
        print(subindex.phys)

    # read and write filename with invalid olm file format
    with pytest.raises(SdoAbortedError):
        subindex.raw = "test.txt".encode("utf-8")

    # readwrite with filename that does not end with '\0'
    subindex.raw = FREAD_FILE2.encode("utf-8")
    assert subindex.raw.decode("utf-8") == (FREAD_FILE2 + '\0')

    # read and write filename that ends with '\0'
    read_file_b = FREAD_FILE + '\0'
    subindex.raw = read_file_b.encode("utf-8")
    assert subindex.raw.decode("utf-8") == read_file_b

    # readwrite with filename that does not exist
    with pytest.raises(SdoAbortedError):
        subindex.raw = FREAD_FILE3.encode("utf-8")

    network.disconnect()
Exemplo n.º 3
0
def test_file_name():
    """Test the file name subindex (DOMAIN, readonly)."""
    network, node = connect_bus()
    len_subindex = node.sdo[FILE_CACHES_INDEX][CACHE_LEN_SUBINDEX]
    iter_subindex = node.sdo[FILE_CACHES_INDEX][ITERATOR_SUBINDEX]
    name_subindex = node.sdo[FILE_CACHES_INDEX][NAME_SUBINDEX]

    # test readonly
    with pytest.raises(SdoAbortedError):
        name_subindex.raw = "0".encode("utf-8")

    count = 0

    # check for all valid and invalid files
    # make sure to handle the posible for files from CO fread test
    for i in range(len_subindex.phys):
        iter_subindex.phys = i
        name = name_subindex.raw.decode("utf-8").replace('\0', '')

        assert name not in FCACHES_FREAD_INVALID_FILES or \
            name == FCACHES_FREAD_EMPTY_FILE

        if name in FCACHES_FREAD_VALID_FILES:
            count += 1

    assert count == len(FCACHES_FREAD_VALID_FILES)

    network.disconnect()
Exemplo n.º 4
0
def test_iterator():
    """Test the iterator subindex (uint32, readonly)."""
    network, node = connect_bus()
    selector_subindex = node.sdo[FILE_CACHES_INDEX][CACHE_SELECTOR_SUBINDEX]
    len_subindex = node.sdo[FILE_CACHES_INDEX][CACHE_LEN_SUBINDEX]
    iter_subindex = node.sdo[FILE_CACHES_INDEX][ITERATOR_SUBINDEX]

    selector_subindex.phys = FREAD_CACHE

    # valid iterator values
    for i in range(len_subindex.phys):
        iter_subindex.phys = i

    # invalid iterator values
    with pytest.raises(SdoAbortedError):
        iter_subindex.phys = len_subindex.phys
    with pytest.raises(SdoAbortedError):
        iter_subindex.phys = len_subindex.phys + 10

    selector_subindex.phys = FWRITE_CACHE

    # valid iterator values
    for i in range(len_subindex.phys):
        iter_subindex.phys = i

    # invalid iterator values
    with pytest.raises(SdoAbortedError):
        iter_subindex.phys = len_subindex.phys
    with pytest.raises(SdoAbortedError):
        iter_subindex.phys = len_subindex.phys + 10

    selector_subindex.phys = FREAD_CACHE
    network.disconnect()
Exemplo n.º 5
0
def test_os_command():
    """Test if the fwrite filename subindex (DOMAIN, readwrite)."""
    network, node = connect_bus()
    command_subindex = node.sdo[FWRITE_INDEX][COMMAND_SUBINDEX]
    status_subindex = node.sdo[FWRITE_INDEX][STATUS_SUBINDEX]
    reply_subindex = node.sdo[FWRITE_INDEX][REPLY_SUBINDEX]

    assert status_subindex.phys == OSCommandReply.no_error_no_reply.value

    # read no data
    with pytest.raises(SdoAbortedError):
        command_subindex.raw

    assert status_subindex.phys == OSCommandReply.no_error_no_reply.value

    # invalid message with reply
    command_subindex.raw = "echo \"abcd\"".encode("utf-8")
    while status_subindex.phys == OSCommandReply.excuting.value:
        sleep(0.1)
    assert status_subindex.phys == OSCommandReply.no_error_reply.value

    # message with reply
    message = random_string_generator(10)
    bash_message = "echo \"" + message + "\""
    command_subindex.raw = bash_message.encode("utf-8")
    assert command_subindex.raw.decode("utf-8") == bash_message + "\0"
    if status_subindex.phys == OSCommandReply.excuting.value:
        sleep(1)
    assert status_subindex.phys == OSCommandReply.no_error_reply.value
    assert len(reply_subindex.raw.decode("utf-8")) == len(message) + 1
    assert reply_subindex.raw.decode("utf-8") == message + "\n"

    # medium message with medium reply
    message = random_string_generator(100)
    bash_message = "echo \"" + message + "\""
    command_subindex.raw = bash_message.encode("utf-8")
    assert command_subindex.raw.decode("utf-8") == bash_message + "\0"
    if status_subindex.phys == OSCommandReply.excuting.value:
        sleep(1)
    assert status_subindex.phys == OSCommandReply.no_error_reply.value
    assert len(reply_subindex.raw.decode("utf-8")) == len(message) + 1
    assert reply_subindex.raw.decode("utf-8") == message + "\n"

    # large message with large reply
    message = random_string_generator(5000)
    bash_message = "echo \"" + message + "\""
    command_subindex.raw = bash_message.encode("utf-8")
    assert command_subindex.raw.decode("utf-8") == bash_message + "\0"
    if status_subindex.phys == OSCommandReply.excuting.value:
        sleep(1)
    assert status_subindex.phys == OSCommandReply.no_error_reply.value
    assert len(reply_subindex.raw.decode("utf-8")) == len(message) + 1
    assert reply_subindex.raw.decode("utf-8") == message + "\n"

    network.disconnect()
Exemplo n.º 6
0
def test_fwrite_cache_len():
    """Test the fwrite cache length subindex (uint8, readonly)."""
    network, node = connect_bus()
    subindex = node.sdo[FILE_CACHES_INDEX][FWRITE_CACHE_LEN_SUBINDEX]

    # test read
    subindex.phys

    # test readonly
    with pytest.raises(SdoAbortedError):
        subindex.phys = 0

    network.disconnect()
Exemplo n.º 7
0
def test_file_size():
    """Test the file size subindex (uint32, readonly)."""
    network, node = connect_bus()
    len_subindex = node.sdo[FILE_CACHES_INDEX][CACHE_LEN_SUBINDEX]
    iter_subindex = node.sdo[FILE_CACHES_INDEX][ITERATOR_SUBINDEX]
    size_subindex = node.sdo[FILE_CACHES_INDEX][SIZE_SUBINDEX]

    # test readonly
    with pytest.raises(SdoAbortedError):
        size_subindex.phys = 0

    # all files have their size set and there are not empty files
    for i in range(len_subindex.phys):
        iter_subindex.phys = i
        assert size_subindex.phys != 0

    network.disconnect()
Exemplo n.º 8
0
def test_delete_file():
    """Test the delete file subindex (DOMAIN, readonly)."""
    network, node = connect_bus()
    selector_subindex = node.sdo[FILE_CACHES_INDEX][CACHE_SELECTOR_SUBINDEX]
    len_subindex = node.sdo[FILE_CACHES_INDEX][CACHE_LEN_SUBINDEX]
    iter_subindex = node.sdo[FILE_CACHES_INDEX][ITERATOR_SUBINDEX]
    name_subindex = node.sdo[FILE_CACHES_INDEX][NAME_SUBINDEX]
    delete_subindex = node.sdo[FILE_CACHES_INDEX][DELETE_SUBINDEX]

    # test writeonly
    with pytest.raises(SdoAbortedError):
        delete_subindex.phys

    # Delete all FCACHES_FREAD_VALID_FILES cache files but don't delete any
    # fread files
    for i in range(len(FCACHES_FREAD_VALID_FILES)):
        found = False
        length = len_subindex.phys

        for i in range(length):
            iter_subindex.phys = i
            name = name_subindex.raw.decode("utf-8").replace('\0', '')

            if name in FCACHES_FREAD_VALID_FILES:
                delete_subindex.raw = "0".encode("utf-8")
                assert len_subindex.phys == length - 1
                found = True
                break

        assert found

    selector_subindex.phys = FWRITE_CACHE

    for i in range(len_subindex.phys - 1, 0, -1):
        iter_subindex.phys = i
        delete_subindex.raw = "0".encode("utf-8")
        assert len_subindex.phys == i
        assert iter_subindex.phys == 0  # should be 0 after delete is called

    selector_subindex.phys = FREAD_CACHE

    network.disconnect()
Exemplo n.º 9
0
def test_fread_delete():
    """Test the delete file subindex (DOMAIN, writeonly)"""
    network, node = connect_bus()
    subindex = node.sdo[FREAD_INDEX][DELETE_SUBINDEX]

    # writeonly
    with pytest.raises(SdoAbortedError):
        subindex.raw

    # delete file
    node.sdo[FREAD_INDEX][NAME_SUBINDEX].raw = FREAD_FILE.encode("utf-8")
    subindex.raw = "0".encode("utf-8")
    assert not isfile(FREAD_CACHE_DIR + FREAD_FILE)

    # delete another file
    node.sdo[FREAD_INDEX][NAME_SUBINDEX].raw = FREAD_FILE2.encode("utf-8")
    subindex.raw = "0".encode("utf-8")
    assert not isfile(FREAD_CACHE_DIR + FREAD_FILE2)

    network.disconnect()
Exemplo n.º 10
0
def test_filer():
    """Test the cache filter subindex (DOMAIN, readonly)."""
    network, node = connect_bus()
    node.sdo[FILE_CACHES_INDEX][CACHE_SELECTOR_SUBINDEX].phys = FREAD_CACHE
    subindex = node.sdo[FILE_CACHES_INDEX][FILTER_SUBINDEX]

    # test read with empty filter
    with pytest.raises(SdoAbortedError):
        subindex.raw

    # test write with valid inputs
    subindex.raw = "foo".encode("utf-8")
    assert "foo\0" == subindex.raw.decode("utf-8")
    subindex.raw = "bar\0".encode("utf-8")
    assert "bar\0" == subindex.raw.decode("utf-8")
    subindex.raw = b'\00'  # clear filter
    with pytest.raises(SdoAbortedError):
        subindex.raw

    network.disconnect()
Exemplo n.º 11
0
def test_cache_selector():
    """Test the cache selector subindex (uint8, readwrite)."""
    network, node = connect_bus()
    subindex = node.sdo[FILE_CACHES_INDEX][CACHE_SELECTOR_SUBINDEX]

    # test read
    subindex.phys

    # test write with valid inputs
    subindex.phys = FREAD_CACHE
    assert FREAD_CACHE == subindex.phys
    subindex.phys = FWRITE_CACHE
    assert FWRITE_CACHE == subindex.phys
    subindex.phys = FREAD_CACHE
    assert FREAD_CACHE == subindex.phys
    subindex.phys = FWRITE_CACHE
    assert FWRITE_CACHE == subindex.phys

    # test with invalid input
    with pytest.raises(SdoAbortedError):
        subindex.phys = 12

    network.disconnect()
Exemplo n.º 12
0
def test_fread_data():
    """Test the fread filedata subindex (DOMAIN, readonly)."""
    network, node = connect_bus()
    name_subindex = node.sdo[FREAD_INDEX][NAME_SUBINDEX]
    data_subindex = node.sdo[FREAD_INDEX][DATA_SUBINDEX]

    # readonly
    with pytest.raises(SdoAbortedError):
        data_subindex.raw = "0".encode("utf-8")

    # read a file over CANbus
    name_subindex.raw = FREAD_FILE.encode("utf-8")
    file_data = data_subindex.raw.decode("utf-8")
    with open(FREAD_CACHE_DIR + FREAD_FILE, "r") as fptr:
        assert fptr.read() == file_data

    # read another file over CANbus that require multiple blocks
    name_subindex.raw = FREAD_FILE2.encode("utf-8")
    file2_data = data_subindex.raw.decode("utf-8")
    assert isfile(FREAD_CACHE_DIR + FREAD_FILE2)
    with open(FREAD_CACHE_DIR + FREAD_FILE2, "r") as fptr:
        assert fptr.read() == file2_data

    network.disconnect()
Exemplo n.º 13
0
def test_cache_len():
    """Test the selected cache length subindex (uint32, readonly)."""
    network, node = connect_bus()
    selector_subindex = node.sdo[FILE_CACHES_INDEX][CACHE_SELECTOR_SUBINDEX]
    len_subindex = node.sdo[FILE_CACHES_INDEX][CACHE_LEN_SUBINDEX]

    selector_subindex.phys = FREAD_CACHE

    # test readonly fro fread cache
    len = len_subindex.phys
    with pytest.raises(SdoAbortedError):
        len_subindex.phys = 42
    assert len == len_subindex.phys

    selector_subindex.phys = FWRITE_CACHE

    # test readonly fro fwrite cache
    len = len_subindex.phys
    with pytest.raises(SdoAbortedError):
        len_subindex.phys = 42
    assert len == len_subindex.phys

    selector_subindex.phys = FREAD_CACHE
    network.disconnect()
Exemplo n.º 14
0
def test_fwrite_data():
    """Test if the fwrite filedata subindex (DOMAIN, writeonly)."""
    network, node = connect_bus()
    name_subindex = node.sdo[FWRITE_INDEX][NAME_SUBINDEX]
    data_subindex = node.sdo[FWRITE_INDEX][DATA_SUBINDEX]

    with pytest.raises(SdoAbortedError):
        data_subindex.phys

    # write a file over CANbus
    new_file = "test_write_123.txt"
    name_subindex.raw = new_file.encode("utf-8")
    file_data = random_string_generator(10)
    data_subindex.raw = file_data.encode("utf-8")
    assert isfile(FWRITE_CACHE_DIR + new_file)
    with open(FWRITE_CACHE_DIR + new_file, "r") as fptr:
        assert fptr.read() == file_data

    # write another file over CANbus that require multiple blocks
    new_file2 = "test_write_456.txt"
    name_subindex.raw = new_file2.encode("utf-8")
    file2_data = random_string_generator(2000)
    data_subindex.raw = file2_data.encode("utf-8")
    assert isfile(FWRITE_CACHE_DIR + new_file2)
    with open(FWRITE_CACHE_DIR + new_file2, "r") as fptr:
        assert fptr.read() == file2_data

    # write another file over CANbus with same name as last
    name_subindex.raw = new_file2.encode("utf-8")
    file2b_data = random_string_generator(50)
    data_subindex.raw = file2b_data.encode("utf-8")
    assert isfile(FWRITE_CACHE_DIR + new_file2)
    with open(FWRITE_CACHE_DIR + new_file2, "r") as fptr:
        assert fptr.read() == file2b_data

    network.disconnect()
Exemplo n.º 15
0
def test_fwrite_name():
    """Test if the fwrite filename subindex (DOMAIN, readwrite)."""
    network, node = connect_bus()
    subindex = node.sdo[FWRITE_INDEX][NAME_SUBINDEX]

    # read no data
    with pytest.raises(SdoAbortedError):
        subindex.phys

    # read and write filename with invalid olm file format
    with pytest.raises(SdoAbortedError):
        subindex.raw = "test.txt".encode("utf-8")

    # readwrite with filename that does not end with '\0'
    new_file = "test_write_123.txt"
    node.sdo[FWRITE_INDEX][NAME_SUBINDEX].raw = new_file.encode("utf-8")
    assert subindex.raw.decode("utf-8") == (new_file + '\0')

    # read and write filename that ends with '\0'
    new_file = "test_write_123.txt\0"
    subindex.raw = new_file.encode("utf-8")
    assert subindex.raw.decode("utf-8") == new_file

    network.disconnect()