All of lore.kernel.org
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v2] add new testcase for bind
Date: Fri, 27 Jul 2018 11:31:50 +0200	[thread overview]
Message-ID: <20180727093150.GA914@rei> (raw)
In-Reply-To: <20180727073057.22798-1-mmoese@suse.de>

Hi!
> +#include <errno.h>
> +#include <limits.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +
> +#include "tst_test.h"
> +#include "tst_safe_net.h"
> +
> +#define SOCK_NAME "socket.1"
> +
> +static int sock1, sock2;
> +
> +static char dir_path[PATH_MAX];
> +static char sockpath[PATH_MAX];
> +
> +void run(void)
> +{
> +	struct sockaddr_un sun;
> +
> +	sock1 = SAFE_SOCKET(PF_UNIX, SOCK_STREAM, 0);
> +
> +	memset(&sun, 0, sizeof(sun));
> +	sun.sun_family = AF_UNIX;
> +	if (snprintf(sockpath, sizeof(sockpath), "%s/%s", dir_path, SOCK_NAME)
> +	    >= PATH_MAX) {
> +		tst_res(TBROK, "bind_test: snprintf(sockpath)");
> +		return;
> +	}
> +	if (snprintf(sun.sun_path, sizeof(sun.sun_path), "%s", sockpath)
> +	    >= (int) sizeof(sun.sun_path)) {
> +		tst_res(TBROK, "bind_test: snprintf(sun.sun_path)");
> +		return;
> +	}
> +
> +	SAFE_BIND(sock1, (struct sockaddr *)&sun, sizeof(sun));
> +
> +	/*
> +	 * Once a STREAM UNIX domain socket has been bound, it can't be
> +	 * rebound. Expected error is EINVAL.
> +	 */
> +	if (bind(sock1, (struct sockaddr *)&sun, sizeof(sun)) == 0) {
> +		tst_res(TFAIL, "re-binding of socket succeeded");
> +		return;
> +	}
> +
> +	if (errno != EINVAL) {
> +		tst_res(TFAIL | TERRNO, "expected EINVAL");
> +		return;
> +	}
> +
> +	sock2 = SAFE_SOCKET(PF_UNIX, SOCK_STREAM, 0);
> +
> +	/*
> +	 * Since a socket is already bound to the pathname, it can't be bound
> +	 * to a second socket. Expected error is EADDRINUSE.
> +	 */
> +	if (bind(sock2, (struct sockaddr *)&sun, sizeof(sun)) == 0) {
> +		tst_res(TFAIL, "bind() succeeded with already bound pathname!");
> +		return;
> +	}
> +
> +	if (errno != EADDRINUSE) {
> +		tst_res(TFAIL | TERRNO, "expected to fail with EADDRINUSE");
> +		return;
> +	}
> +
> +	tst_res(TPASS, "bind() failed with EADDRINUSE as expected");
> +}
> +
> +
> +static void setup(void)
> +{
> +	strncpy(dir_path, "/tmp/unix_bind.XXXXXXX", PATH_MAX);
> +	if (mkdtemp(dir_path) == NULL)
> +		tst_res(TBROK, "cannot create temporary directory");

We should use the test temporary directory here instead. I.e. set the
needs_tmpdir flag in the tst_test structure and use a relative paths in
the testcase.

Other than that this is fine.

> +}
> +
> +static void cleanup(void)
> +{
> +	close(sock1);
> +	close(sock2);
> +	unlink(sockpath);
> +	rmdir(dir_path);
> +}
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test_all = run,
> +};
> -- 
> 2.18.0
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz

      reply	other threads:[~2018-07-27  9:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-27  7:30 [LTP] [PATCH v2] add new testcase for bind Michael Moese
2018-07-27  9:31 ` Cyril Hrubis [this message]

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=20180727093150.GA914@rei \
    --to=chrubis@suse.cz \
    --cc=ltp@lists.linux.it \
    /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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.