diff options
author | Dmitry Antipov <dmitry.antipov@linaro.org> | 2012-11-30 15:01:08 +0400 |
---|---|---|
committer | Dmitry Antipov <dmitry.antipov@linaro.org> | 2012-11-30 15:01:08 +0400 |
commit | edd476b70e7b43957ce00508b7d694f85daa2eb1 (patch) | |
tree | 0e955c7a6ee5a317bc67caa5c08e4e64b37b8221 | |
parent | 5c0cbc0d2102b16a266dfaf66fedf3ec9e3634f1 (diff) | |
download | linaro-android-kernel-test-edd476b70e7b43957ce00508b7d694f85daa2eb1.tar.gz |
Add simple mmap() test.
-rw-r--r-- | ashmemtest-basic/ashmemtest.c | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/ashmemtest-basic/ashmemtest.c b/ashmemtest-basic/ashmemtest.c index 390b2b4..e60b36b 100644 --- a/ashmemtest-basic/ashmemtest.c +++ b/ashmemtest-basic/ashmemtest.c @@ -2,6 +2,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <sys/ioctl.h> +#include <sys/mman.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> @@ -329,6 +330,89 @@ void ashmem_size (char *ashmemdev, size_t pagesize) printf ("%d: test passed\n", testno); } +void ashmem_mmap (char *ashmemdev, size_t pagesize) +{ + void *mem; + int i, fd, ret; + size_t size = pagesize * 4; + char *pattern = alloca (size); + + testno++; + + /* fill the random pattern */ + for (i = 0; i < size; i++) + pattern[i] = 'a' + rand () % ('z' - 'a'); + + /* write the pattern to ashmem region, then compare with origin */ + fd = test_open (ashmemdev, O_RDWR); + ret = ioctl (fd, ASHMEM_SET_SIZE, size); + if (ret < 0) + fatal (errno, "can't allocate %ld-bytes region"); + mem = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (mem == MAP_FAILED) + fatal (errno, "can't map ashmem region"); + memcpy (mem, pattern, size); + if (memcmp (mem, pattern, size)) + fatal (0, "pattern vs. ashmem mismatch"); + if (munmap (mem, size)) + fatal (errno, "can't unmap ashmem region"); + test_close (fd); + + /* make sure we can mmap a half in the middle */ + fd = test_open (ashmemdev, O_RDWR); + ret = ioctl (fd, ASHMEM_SET_SIZE, size); + if (ret < 0) + fatal (errno, "can't allocate %ld-bytes region"); + mem = mmap (NULL, size / 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, pagesize); + if (mem == MAP_FAILED) + fatal (errno, "can't map ashmem region in the middle"); + if (munmap (mem, size / 2)) + fatal (errno, "can't unmap ashmem region"); + test_close (fd); + + /* make sure we can't mmap more than allocated */ + fd = test_open (ashmemdev, O_RDWR); + ret = ioctl (fd, ASHMEM_SET_SIZE, size); + if (ret < 0) + fatal (errno, "can't allocate %ld-bytes region"); + mem = mmap (NULL, size + pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (mem != MAP_FAILED || errno != EINVAL) + fatal (errno, "should not allow to map more than allocated"); + test_close (fd); + + /* make sure we can't mmap beyond allocated */ + fd = test_open (ashmemdev, O_RDWR); + ret = ioctl (fd, ASHMEM_SET_SIZE, size); + if (ret < 0) + fatal (errno, "can't allocate %ld-bytes region"); + mem = mmap (NULL, pagesize * 2, PROT_READ | PROT_WRITE, MAP_SHARED, fd, pagesize * 3); + if (mem != MAP_FAILED || errno != EINVAL) + fatal (errno, "should not allow to map beyond allocated"); + test_close (fd); + + /* make sure we can't mmap read-only region for read/write */ + fd = test_open (ashmemdev, O_RDONLY); + ret = ioctl (fd, ASHMEM_SET_SIZE, size); + if (ret < 0) + fatal (errno, "can't allocate %ld-bytes read-only region"); + mem = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (mem != MAP_FAILED || errno != EINVAL) + fatal (errno, "should not allow read-write map for read-only region"); + test_close (fd); + + /* make sure we can't mmap write-only region for read/write */ + fd = test_open (ashmemdev, O_WRONLY); + ret = ioctl (fd, ASHMEM_SET_SIZE, size); + if (ret < 0) + fatal (errno, "can't allocate %ld-bytes read-only region"); + mem = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (mem != MAP_FAILED || errno != EINVAL) + fatal (errno, "should not allow read-write map for write-only region"); + test_close (fd); + + printf ("%d: test passed\n", testno); +} + int main(int argc, char *argv[]) { char ashmemdev[64]; @@ -342,6 +426,7 @@ int main(int argc, char *argv[]) ashmem_pin_unpin (ashmemdev, pagesize); ashmem_pin_status (ashmemdev, pagesize); ashmem_size (ashmemdev, pagesize); + ashmem_mmap (ashmemdev, pagesize); return 0; } |