Skip to contentSkip to conversation

selftests: add binderfs selftests

PATCH Superseded Christian Brauner 2 participants 3 messages Jan 17, 2019 Kselftest

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selftests: add binderfs selftests

This adds the promised selftest for binderfs. It will verify the following things:
- binderfs mounting works
- binder device allocation works
- performing a binder ioctl() request through a binderfs device works
- binder device removal works
- binder-control removal fails
- binderfs unmounting works

The tests are performed both privileged and unprivileged. The latter verifies that binderfs behaves correctly in user namespaces.

Cc Todd Kjos <tkjos at google.com>
Signed-off-by Christian Brauner <christian.brauner at ubuntu.com>

---
/* Changelog */
v2:
- make failure to create /dev/binderfs directory fatal in all circumstances
- make tests run in user namespace to test whether binderfs can be mounted in user namespaces and so that unprivileged users can run the tests
- use ksft_exit_skip()

v1:
- check for ENODEV on mount failure to detect whether binderfs is available
If it is not, skip the test and exit with success.

---
 tools/testing/selftests/Makefile              |   1 +
 .../selftests/filesystems/binderfs/.gitignore |   1 +
 .../selftests/filesystems/binderfs/Makefile   |   6 +
 .../filesystems/binderfs/binderfs_test.c      | 270 ++++++++++++++++++
 .../selftests/filesystems/binderfs/config     |   3 +
 5 files changed, 281 insertions(+)
 create mode 100644 tools/testing/selftests/filesystems/binderfs/.gitignore
 create mode 100644 tools/testing/selftests/filesystems/binderfs/Makefile
 create mode 100644 tools/testing/selftests/filesystems/binderfs/binderfs_test.c
 create mode 100644 tools/testing/selftests/filesystems/binderfs/config
tools/testing/selftests/Makefile +1 -0
index 1a2bd15c5b6e..400ee81a3043 100644
@@ -10,6 +10,7 @@ TARGETS += drivers/dma-buf
 TARGETS += efivarfs
 TARGETS += exec
 TARGETS += filesystems
+TARGETS += filesystems/binderfs
 TARGETS += firmware
 TARGETS += ftrace
 TARGETS += futex
tools/testing/selftests/filesystems/binderfs/.gitignore new file +1 -0
new file mode 100644
index 000000000000..8a5d9bf63dd4
@@ -0,0 +1 @@
+binderfs_test
tools/testing/selftests/filesystems/binderfs/Makefile new file +6 -0
new file mode 100644
index 000000000000..58cb659b56b4
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+
+CFLAGS += -I../../../../../usr/include/
+TEST_GEN_PROGS := binderfs_test
+
+include ../../lib.mk
tools/testing/selftests/filesystems/binderfs/binderfs_test.c new file +270 -0

270 lines not on this page · raw message

tools/testing/selftests/filesystems/binderfs/config new file +3 -0
new file mode 100644
index 000000000000..02dd6cc9cf99
@@ -0,0 +1,3 @@
+CONFIG_ANDROID=y
+CONFIG_ANDROID_BINDERFS=y
+CONFIG_ANDROID_BINDER_IPC=y
Signature · 1 line
2.19.1
#1 lore raw
Quoted 9 lines · Christian Brauner

This adds the promised selftest for binderfs. It will verify the following things:
- binderfs mounting works
- binder device allocation works
- performing a binder ioctl() request through a binderfs device works
- binder device removal works
- binder-control removal fails
- binderfs unmounting works

Quoted 5 lines

The tests are performed both privileged and unprivileged. The latter verifies that binderfs behaves correctly in user namespaces.

Cc Todd Kjos <tkjos at google.com>
Signed-off-by Christian Brauner <christian.brauner at ubuntu.com>

Now I am just nit-picking:

Quoted 13 lines
tools/testing/selftests/filesystems/binderfs/binderfs_test.c new file +13 -0
+static void write_to_file(const char *filename, const void *buf, size_t count,
+			  int allowed_errno)
+{
+	int fd, saved_errno;
+	ssize_t ret;
+
+	fd = open(filename, O_WRONLY | O_CLOEXEC);
+	if (fd < 0)
+		ksft_exit_fail_msg("%s - Failed to open file %s\n",
+				   strerror(errno), filename);
+
+	ret = write_nointr(fd, buf, count);
+	if (ret < 0) {

tools/testing/selftests/filesystems/binderfs/binderfs_test.c line 53

Quoted 10 lines
tools/testing/selftests/filesystems/binderfs/binderfs_test.c new file +10 -0
+		if (allowed_errno && (errno == allowed_errno)) {
+			close(fd);
+			return;
+		}
+
+		goto on_error;
+	}
+
+	if ((size_t)ret != count)
+		goto on_error;

if ret < count, you are supposed to try again with the remaining data, right? A write() implementation can just take one byte at a time.

Yes, for your example here that isn't going to happen as the kernel should be handling a larger buffer than that, but note that if you use this code elsewhere, it's not really correct because:

tools/testing/selftests/filesystems/binderfs/binderfs_test.c line 59

Quoted 6 lines
tools/testing/selftests/filesystems/binderfs/binderfs_test.c new file +6 -0
+
+	close(fd);
+	return;
+
+on_error:
+	saved_errno = errno;

If you do a short write, there is no error, so who knows what errno you end up with here.

Anyway, just one other minor question that might be relevant:

tools/testing/selftests/filesystems/binderfs/binderfs_test.c line 187

Quoted 2 lines
tools/testing/selftests/filesystems/binderfs/binderfs_test.c new file +2 -0
+	printf("Allocated new binder device with major %d, minor %d, and name %s\n",
+	       device.major, device.minor, device.name);

Aren't tests supposed to print their output in some sort of normal format? I thought you were supposed to use ksft_print_msg() so that tools can properly parse the output.

thanks,

greg k-h

#2 lore raw
Quoted 10 lines · Greg KH
Quoted 9 lines · Christian Brauner

This adds the promised selftest for binderfs. It will verify the following things:
- binderfs mounting works
- binder device allocation works
- performing a binder ioctl() request through a binderfs device works
- binder device removal works
- binder-control removal fails
- binderfs unmounting works

Quoted 7 lines
Quoted 5 lines

The tests are performed both privileged and unprivileged. The latter verifies that binderfs behaves correctly in user namespaces.

Cc Todd Kjos <tkjos at google.com>
Signed-off-by Christian Brauner <christian.brauner at ubuntu.com>

Now I am just nit-picking:

I would've been surprised if someone wouldn't have. :)

Quoted 28 lines
Quoted 14 lines
tools/testing/selftests/filesystems/binderfs/binderfs_test.c new file +13 -0
+static void write_to_file(const char *filename, const void *buf, size_t count,
+			  int allowed_errno)
+{
+	int fd, saved_errno;
+	ssize_t ret;
+
+	fd = open(filename, O_WRONLY | O_CLOEXEC);
+	if (fd < 0)
+		ksft_exit_fail_msg("%s - Failed to open file %s\n",
+				   strerror(errno), filename);
+
+	ret = write_nointr(fd, buf, count);
+	if (ret < 0) {

tools/testing/selftests/filesystems/binderfs/binderfs_test.c line 53

Quoted 10 lines
tools/testing/selftests/filesystems/binderfs/binderfs_test.c new file +10 -0
+		if (allowed_errno && (errno == allowed_errno)) {
+			close(fd);
+			return;
+		}
+
+		goto on_error;
+	}
+
+	if ((size_t)ret != count)
+		goto on_error;

if ret < count, you are supposed to try again with the remaining data, right? A write() implementation can just take one byte at a time.

Quoted 3 lines

Yes, for your example here that isn't going to happen as the kernel should be handling a larger buffer than that, but note that if you use this code elsewhere, it's not really correct because:

Yeah, I know you should retry but for the test I'm not really willing to keep track of where I was in the buffer and so on. If the test fails because of that I'd say to count it as failed and move on.

Quoted 11 lines

tools/testing/selftests/filesystems/binderfs/binderfs_test.c line 59

Quoted 7 lines
tools/testing/selftests/filesystems/binderfs/binderfs_test.c new file +6 -0
+
+	close(fd);
+	return;
+
+on_error:
+	saved_errno = errno;

If you do a short write, there is no error, so who knows what errno you end up with here.

Quoted 8 lines

Anyway, just one other minor question that might be relevant:

tools/testing/selftests/filesystems/binderfs/binderfs_test.c line 187

Quoted 2 lines
tools/testing/selftests/filesystems/binderfs/binderfs_test.c new file +2 -0
+	printf("Allocated new binder device with major %d, minor %d, and name %s\n",
+	       device.major, device.minor, device.name);

Aren't tests supposed to print their output in some sort of normal format? I thought you were supposed to use ksft_print_msg() so that tools can properly parse the output.

I can switch the printf()s over to ksft_print_msg().

Thanks!
Christian