[PATCH 2/2] Add test case for character device mmap handler

Kevin Kirspel kevin-kirspel at idexx.com
Tue May 23 01:42:25 UTC 2017


---
 testsuite/cdev01/test_cdev.c   | 20 +++++++++++++++++++-
 testsuite/cdev01/test_cdev01.h |  2 ++
 testsuite/cdev01/test_main.c   |  7 +++++++
 3 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/testsuite/cdev01/test_cdev.c b/testsuite/cdev01/test_cdev.c
index 19fb1a5..9b0c46f 100644
--- a/testsuite/cdev01/test_cdev.c
+++ b/testsuite/cdev01/test_cdev.c
@@ -39,6 +39,8 @@
 
 #include "test_cdev01.h"
 
+static char mmap_buffer[256] = TEST_MMAP_DATA;
+
 static	d_open_t	testopen;
 static	d_close_t	testclose;
 static	d_read_t	testread;
@@ -46,6 +48,7 @@ static	d_write_t	testwrite;
 static	d_ioctl_t	testioctl;
 static	d_poll_t	testpoll;
 static	d_kqfilter_t	testkqfilter;
+static	d_mmap_t	testmmap;
 
 static struct cdevsw test_cdevsw = {
 	.d_version =	D_VERSION,
@@ -59,6 +62,7 @@ static struct cdevsw test_cdevsw = {
 	.d_ioctl =	testioctl,
 	.d_poll =	testpoll,
 	.d_kqfilter =	testkqfilter,
+	.d_mmap =	testmmap,
 };
 
 static	int
@@ -77,7 +81,7 @@ testclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
 {
 	test_state *state = dev->si_drv1;
 
-	assert(*state == TEST_KQFILTER);
+	assert(*state == TEST_MMAP);
 	*state = TEST_CLOSED;
 
 	return 0;
@@ -148,6 +152,20 @@ testkqfilter(struct cdev *dev, struct knote *kn)
 	return TEST_KQ_ERRNO;
 }
 
+static	int
+testmmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
+    int nprot, vm_memattr_t *memattr)
+{
+	test_state *state = dev->si_drv1;
+
+	assert(*state == TEST_KQFILTER);
+	*state = TEST_MMAP;
+
+	*paddr = (vm_paddr_t)&mmap_buffer[0];
+
+	return 0;
+}
+
 void
 test_make_dev(test_state *state, const char *name)
 {
diff --git a/testsuite/cdev01/test_cdev01.h b/testsuite/cdev01/test_cdev01.h
index a83001b..dcb0529 100644
--- a/testsuite/cdev01/test_cdev01.h
+++ b/testsuite/cdev01/test_cdev01.h
@@ -42,6 +42,7 @@ typedef enum {
 	TEST_WRITEV,
 	TEST_POLL,
 	TEST_KQFILTER,
+	TEST_MMAP,
 	TEST_CLOSED
 } test_state;
 
@@ -50,5 +51,6 @@ void test_make_dev(test_state *state, const char *name);
 #define TEST_IOCTL_CMD 0x12344321
 #define TEST_KQ_ERRNO ENOTTY /* This should never happen in normal kq */
 #define TEST_UDATA ((void *) 0xcafe)
+#define TEST_MMAP_DATA "MMAP DATA"
 
 #endif /* TEST_CDEV01_H */
diff --git a/testsuite/cdev01/test_main.c b/testsuite/cdev01/test_main.c
index cbff913..b051838 100644
--- a/testsuite/cdev01/test_main.c
+++ b/testsuite/cdev01/test_main.c
@@ -35,6 +35,7 @@
 #include <sys/time.h>
 #include <sys/uio.h>
 #include <sys/ioctl.h>
+#include <sys/mman.h>
 
 #include <assert.h>
 #include <errno.h>
@@ -72,6 +73,7 @@ static void test_cdev(const char *path)
 	struct pollfd fds[1];
 	int kq;
 	struct kevent change;
+	char *mmap_addr;
 
 	test_make_dev(&state, name);
 
@@ -125,6 +127,11 @@ static void test_cdev(const char *path)
 	assert(rv == -1);
 	assert(errno == TEST_KQ_ERRNO);
 
+	mmap_addr = (char *)mmap(NULL, sizeof(TEST_MMAP_DATA) + 1,
+	    PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+	assert(mmap_addr != MAP_FAILED);
+	assert(strcmp(mmap_addr, TEST_MMAP_DATA) == 0);
+
 	rv = close(fd);
 	assert(rv == 0);
 
-- 
1.9.1



More information about the devel mailing list