diff options
| author | Sergio Cazzolato <sergiocazzolato@gmail.com> | 2018-02-21 17:48:55 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-02-21 17:48:55 -0300 |
| commit | e9992e9d9e1b4925c919df080d99eeb964d2f277 (patch) | |
| tree | 2b2b5d713605f0a5d387074f114d301abfac64af | |
| parent | 3663066bee53ea8314d45d6f88efcce11b3af6d9 (diff) | |
| parent | 21c847b93d0bac76228c9faf94439571bae851ec (diff) | |
Merge pull request #4563 from sergiocazzolato/tests-interfaces-gpio-memory-control
tests: new spread test for gpio-memory-control interface
4 files changed, 129 insertions, 0 deletions
diff --git a/tests/lib/snaps/test-snapd-gpio-memory-control/Makefile b/tests/lib/snaps/test-snapd-gpio-memory-control/Makefile new file mode 100644 index 0000000000..55766b8af2 --- /dev/null +++ b/tests/lib/snaps/test-snapd-gpio-memory-control/Makefile @@ -0,0 +1,5 @@ +CFLAGS += -Wall -Werror +.PHONY: all +all: gpiomem + +gpiomem: gpiomem.c \ No newline at end of file diff --git a/tests/lib/snaps/test-snapd-gpio-memory-control/gpiomem.c b/tests/lib/snaps/test-snapd-gpio-memory-control/gpiomem.c new file mode 100644 index 0000000000..804dca141e --- /dev/null +++ b/tests/lib/snaps/test-snapd-gpio-memory-control/gpiomem.c @@ -0,0 +1,59 @@ +#include <fcntl.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/types.h> +#include <unistd.h> + +#define MAP_SIZE 4096UL +#define MAP_MASK (MAP_SIZE - 1) + +int main(int argc, char** argv) +{ + int fd; + void *map_base, *virt_addr; + uint32_t read_result, write_result; + + uint32_t writeval = 't'; + off_t address = 0x00000001; + int retval = -1; + + /* Open character device */ + if ((fd = open("/dev/gpiomem", O_RDWR)) == -1) { + perror("cannot open /dev/gpiomem"); + goto close; + } + + /* Map one page */ + map_base = mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, address & ~MAP_MASK); + if (map_base == MAP_FAILED) { + perror("cannot map gpio memory"); + goto unmap; + } + printf("Memory mapped at address %p.\n", map_base); + + /* Read memory map */ + virt_addr = (char*)map_base + (address & MAP_MASK); + read_result = *((uint32_t*)virt_addr); + printf("Read value: %#010x\n", read_result); + + /* Write memory map */ + *((uint32_t*)virt_addr) = writeval; + write_result = *((uint32_t*)virt_addr); + printf("Written %#010x; readback %#010x\n", writeval, write_result); + + retval = 0; + +unmap: + /* Unmap the page */ + if (munmap(map_base, MAP_SIZE) == -1) { + perror("cannot unmap gpio memory"); + retval = -1; + } + +close: + close(fd); + return retval; +} diff --git a/tests/lib/snaps/test-snapd-gpio-memory-control/snapcraft.yaml b/tests/lib/snaps/test-snapd-gpio-memory-control/snapcraft.yaml new file mode 100644 index 0000000000..69dd6c07b2 --- /dev/null +++ b/tests/lib/snaps/test-snapd-gpio-memory-control/snapcraft.yaml @@ -0,0 +1,17 @@ +name: test-snapd-gpio-memory-control +version: 1.0 +summary: Basic gpio-memory-control snap +description: A basic snap which access to public gpiomem device +grade: stable +confinement: strict + +apps: + gpiomem: + command: gpiomem + plugs: [gpio-memory-control] + +parts: + test: + source: . + plugin: make + artifacts: [gpiomem] diff --git a/tests/main/interfaces-gpio-memory-control/task.yaml b/tests/main/interfaces-gpio-memory-control/task.yaml new file mode 100644 index 0000000000..94400c4f0b --- /dev/null +++ b/tests/main/interfaces-gpio-memory-control/task.yaml @@ -0,0 +1,48 @@ +summary: Ensure that the gpio physical memory control interface works. + +details: | + The gpio-memory-control interface allows read/write access to all gpio memory. + +systems: [ubuntu-core-16-arm-*] + +environment: + CONNECTED_PATTERN: ':gpio-memory-control +test-snapd-gpio-memory-control' + DISCONNECTED_PATTERN: '\- +test-snapd-gpio-memory-control:gpio-memory-control' + +prepare: | + echo "Given the test-snapd-gpio-memory-control snap is installed" + snap install test-snapd-gpio-memory-control + +restore: | + rm -f call.error + +execute: | + if ! [ -c /dev/gpiomem ]; then + echo "The /dev/gpiomem device does not exist in current system" + exit 0 + fi + + echo "The interface is not connected by default" + snap interfaces | MATCH "$DISCONNECTED_PATTERN" + + echo "When the interface is connected" + snap connect test-snapd-gpio-memory-control:gpio-memory-control + snap interfaces | MATCH "$CONNECTED_PATTERN" + + echo "Then the snap is able read and write the physical memory" + test-snapd-gpio-memory-control.gpiomem + + if [ "$(snap debug confinement)" = partial ] ; then + exit 0 + fi + + echo "When the plug is disconnected" + snap disconnect test-snapd-gpio-memory-control:gpio-memory-control + snap interfaces | MATCH "$DISCONNECTED_PATTERN" + + echo "Then the snap is not able to access the gpio physical memory" + if test-snapd-gpio-memory-control.gpiomem 2>${PWD}/call.error; then + echo "Expected permission error reading the gpio physical memory with disconnected plug" + exit 1 + fi + MATCH "Permission denied" < call.error |
