mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Alyssa Ross <hi@alyssa.is>
To: musl@lists.openwall.com
Cc: "Érico Nogueira" <ericonr@disroot.org>, "Rich Felker" <dalias@libc.org>
Subject: [musl] [PATCH libc-test v2 1/3] functional: add mntent test
Date: Wed, 15 Sep 2021 22:11:53 +0000	[thread overview]
Message-ID: <20210915221155.3977763-2-hi@alyssa.is> (raw)
In-Reply-To: <20210915221155.3977763-1-hi@alyssa.is>

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


  reply	other threads:[~2021-09-15 23:39 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-15 22:11 [musl] [PATCH v2 0/3] Alyssa Ross
2021-09-15 22:11 ` Alyssa Ross [this message]
2021-09-15 22:11 ` [musl] [PATCH libc-test v2 2/3] functional: add mntent test for single-field line Alyssa Ross
2021-09-15 22:11 ` [musl] [PATCH musl v2 3/3] mntent: fix parsing lines with optional fields Alyssa Ross
2021-09-20  4:21   ` Rich Felker
2022-01-09  3:18     ` Rich Felker
2022-01-13 16:30       ` Alyssa Ross
2022-01-13 17:40         ` Rich Felker
2022-01-13 18:53           ` Alyssa Ross
2022-05-12 14:08             ` Rich Felker
2022-05-12 18:02               ` Alyssa Ross
2022-05-12 18:15                 ` Rich Felker
2022-05-15 18:38                   ` Alyssa Ross
2022-05-15 23:19                     ` Rich Felker
2022-05-16 10:19                       ` Alyssa Ross
2022-05-12 20:58               ` Oliver Ford
2022-05-12 21:10                 ` Rich Felker
2022-05-13 21:39                   ` Oliver Ford
2022-05-14  4:24                     ` Rich Felker
2022-05-14 22:16                       ` Oliver Ford
2022-05-15 23:31               ` Rich Felker
2022-05-16 10:07                 ` Alyssa Ross

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210915221155.3977763-2-hi@alyssa.is \
    --to=hi@alyssa.is \
    --cc=dalias@libc.org \
    --cc=ericonr@disroot.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).