|
|
Message-Id: <20210915221155.3977763-2-hi@alyssa.is>
Date: Wed, 15 Sep 2021 22:11:53 +0000
From: Alyssa Ross <hi@...ssa.is>
To: musl@...ts.openwall.com
Cc: Érico Nogueira <ericonr@...root.org>,
Rich Felker <dalias@...c.org>
Subject: [PATCH libc-test v2 1/3] functional: add mntent test
This only checks reading an fstab from an stream. I haven't written
tests for either setmntent(), addmntent(), or hasmntnent().
test_getmntent exposes a bug in Musl where lines omitting the final
two fields, which are supposed to be optional according to fstab(5),
are not accepted. The tests all pass on Glibc.
---
AUTHORS | 1 +
src/functional/mntent.c | 76 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 77 insertions(+)
create mode 100644 src/functional/mntent.c
diff --git a/AUTHORS b/AUTHORS
index ff99471..cf2a394 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -5,3 +5,4 @@ John Spencer
Jens Gustedt
Alexander Monakov
Julien Ramseier
+Alyssa Ross
diff --git a/src/functional/mntent.c b/src/functional/mntent.c
new file mode 100644
index 0000000..59d816a
--- /dev/null
+++ b/src/functional/mntent.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: MIT
+
+#define _DEFAULT_SOURCE // for getmntent_r
+
+#include <mntent.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include "test.h"
+
+#define ASSERT(x) do { \
+ if (!(x)) { \
+ t_error(#x " failed\n"); \
+ exit(EXIT_FAILURE); \
+ } \
+ } while (0);
+
+#define ERR(fmt, ...) do { \
+ t_error(fmt ": %s\n", ##__VA_ARGS__, strerror(errno)); \
+ exit(EXIT_FAILURE); \
+ } while (0)
+
+void test_getmntent_empty(void)
+{
+ char fstab[] = "\n";
+ FILE *f = fmemopen((void *)fstab, sizeof fstab - 1, "r");
+ if (!f) ERR("fmemopen");
+ ASSERT(!getmntent(f));
+ ASSERT(endmntent(f) == 1);
+}
+
+void test_getmntent(void)
+{
+ // Checks that the fifth and sixth fields default to 0.
+ char fstab[] = "none /proc proc defaults\n";
+ FILE *f = fmemopen((void *)fstab, sizeof fstab - 1, "r");
+ if (!f) ERR("fmemopen");
+ struct mntent *m = getmntent(f);
+ ASSERT(m);
+ ASSERT(!strcmp(m->mnt_fsname, "none"));
+ ASSERT(!strcmp(m->mnt_dir, "/proc"));
+ ASSERT(!strcmp(m->mnt_type, "proc"));
+ ASSERT(!strcmp(m->mnt_opts, "defaults"));
+ ASSERT(m->mnt_freq == 0);
+ ASSERT(m->mnt_passno == 0);
+ ASSERT(endmntent(f) == 1);
+}
+
+void test_getmntent_r(void)
+{
+ struct mntent m, *r;
+ char fstab[] = "/dev/sda\t/\text4\trw,nosuid\t2\t1\n";
+ char buf[sizeof(fstab)];
+
+ FILE *f = fmemopen((void *)fstab, sizeof fstab - 1, "r");
+ if (!f) ERR("fmemopen");
+
+ r = getmntent_r(f, &m, buf, sizeof buf);
+ ASSERT(r == &m);
+ ASSERT(!strcmp(m.mnt_fsname, "/dev/sda"));
+ ASSERT(!strcmp(m.mnt_dir, "/"));
+ ASSERT(!strcmp(m.mnt_type, "ext4"));
+ ASSERT(!strcmp(m.mnt_opts, "rw,nosuid"));
+ ASSERT(m.mnt_freq == 2);
+ ASSERT(m.mnt_passno == 1);
+ ASSERT(endmntent(f) == 1);
+}
+
+int main(void)
+{
+ test_getmntent_empty();
+ test_getmntent();
+ test_getmntent_r();
+}
--
2.32.0
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.