summary: Ensure that the framebuffer interface works.

details: |
    The framebuffer interface allows to access the /dev/fb* buffer files.

    The test uses the test-snapd-framebuffer snap to write/read from /dev/fb0.

    The test also checks the interface connection and disconnection works properly.

prepare: |
    "$TESTSTOOLS"/snaps-state install-local test-snapd-framebuffer

execute: |
    echo "The plug is not connected by default"
    snap interfaces -i framebuffer | MATCH '^- +test-snapd-framebuffer:framebuffer'

    if [ ! -e /dev/fb0 ]; then
        # ensure this test runs on at least one system
        if [[ "$SPREAD_SYSTEM" = ubuntu-core-16-arm-* ]]; then
            echo "ubuntu-core-16-arm-32 must have a framebuffer"
            exit 1
        fi
        echo "SKIP: Framebuffer not available on /dev/fb0"
        exit 0
    fi

    echo "When the interface is connected"
    snap connect test-snapd-framebuffer:framebuffer

    echo "Then the snap is able to write in the framebuffer"
    test-snapd-framebuffer.write "123"
    MATCH "123" < /dev/fb0

    echo "Then the snap is able to read from the framebuffer"
    test-snapd-framebuffer.read

    if [ "$(snap debug confinement)" = partial ] ; then
        exit 0
    fi

    echo "When the plug is disconnected"
    snap disconnect test-snapd-framebuffer:framebuffer

    echo "Then the snap is not able to access the framebuffer"
    if test-snapd-framebuffer.write "123" 2> call.error; then
        echo "Expected permission error trying to write the framebuffer"
        exit 1
    fi
    MATCH "Permission denied" < call.error
