summary: Check that plugged and unplugged device nodes are available in devmode

details: |
    This tests that a framebuffer device is accessible in devmode and makes
    sure that other devices not included in the snap's plugged interfaces are
    still accessible (ie, the cgroup is not in effect).

prepare: |
    # Create framebuffer device node and give it some content we can verify
    # the test snap can read.
    if [ ! -e /dev/fb0 ]; then
        mknod /dev/fb0 c 29 0
        touch /dev/fb0.spread
    fi

    # Create a test char device so we can verify the test snap cannot read.
    if [ ! -e /dev/test1 ]; then
        mknod /dev/test1 c 29 1
        touch /dev/test1.spread
    fi

    echo "Given a snap declaring a plug on framebuffer is installed in devmode"
    "$TESTSTOOLS"/snaps-state install-local test-devmode-cgroup --devmode

restore: |
    if [ -e /dev/fb0.spread ]; then
        rm -f /dev/fb0 /dev/fb0.spread
    fi
    if [ -e /dev/test1.spread ]; then
        rm -f /dev/test1 /dev/test1.spread
    fi

execute: |
    echo "And the framebuffer plug is connected"
    snap connect test-devmode-cgroup:framebuffer

    echo "the devmode snap can access the framebuffer"
    test-devmode-cgroup.read-dev fb0 2>&1 | NOMATCH '(Permission denied|Operation not permitted)'

    echo "the devmode snap can access other devices"
    test-devmode-cgroup.read-dev test1 | NOMATCH '(Permission denied|Operation not permitted)'

    echo "And the framebuffer plug is disconnected"
    snap disconnect test-devmode-cgroup:framebuffer

    echo "the devmode snap can access the framebuffer"
    test-devmode-cgroup.read-dev fb0 2>&1 | NOMATCH '(Permission denied|Operation not permitted)'

    echo "the devmode snap can access other devices"
    test-devmode-cgroup.read-dev test1 | NOMATCH '(Permission denied|Operation not permitted)'
