Blame login/tst-ptsname.c

Packit 6c4009
/* Test for ptsname/ptsname_r.
Packit 6c4009
   Copyright (C) 2014-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Aurelien Jarno <aurelien@aurel32.net>, 2014.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#define DEV_TTY		"/dev/tty"
Packit 6c4009
#define PTSNAME_EINVAL	"./ptsname-einval"
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_single_test (int fd, char *buf, size_t buflen, int expected_err)
Packit 6c4009
{
Packit 6c4009
Packit 6c4009
  int ret = ptsname_r (fd, buf, buflen);
Packit 6c4009
  int err = errno;
Packit 6c4009
Packit 6c4009
  if (expected_err == 0)
Packit 6c4009
    {
Packit 6c4009
      if (ret != 0)
Packit 6c4009
	{
Packit 6c4009
	  printf ("ptsname_r: expected: return = 0\n");
Packit 6c4009
	  printf ("           got: return = %d, errno = %d (%s)\n",
Packit 6c4009
	          ret, err, strerror (err));
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (ret == 0 || errno != expected_err)
Packit 6c4009
	{
Packit 6c4009
	  printf ("ptsname_r: expected: return = %d, errno = %d (%s)\n",
Packit 6c4009
	          -1, expected_err, strerror (expected_err));
Packit 6c4009
	  printf ("           got: return = %d, errno = %d (%s)\n",
Packit 6c4009
	          ret, err, strerror (err));
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  char buf[512];
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  /* Tests with a real PTS master.  */
Packit 6c4009
  int fd = posix_openpt (O_RDWR);
Packit 6c4009
  if (fd != -1)
Packit 6c4009
    {
Packit 6c4009
      result |= do_single_test (fd, buf, sizeof (buf), 0);
Packit 6c4009
      result |= do_single_test (fd, buf, 1, ERANGE);
Packit 6c4009
      close (fd);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    printf ("posix_openpt (O_RDWR) failed\nerrno %d (%s)\n",
Packit 6c4009
	    errno, strerror (errno));
Packit 6c4009
Packit 6c4009
  /* Test with a terminal device which is not a PTS master.  */
Packit 6c4009
  fd = open (DEV_TTY, O_RDONLY);
Packit 6c4009
  if (fd != -1)
Packit 6c4009
    {
Packit 6c4009
      result |= do_single_test (fd, buf, sizeof (buf), ENOTTY);
Packit 6c4009
      close (fd);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    printf ("open (\"%s\", O_RDWR) failed\nerrno %d (%s)\n",
Packit 6c4009
	    DEV_TTY, errno, strerror (errno));
Packit 6c4009
Packit 6c4009
  /* Test with a file.  */
Packit 6c4009
  fd = open (PTSNAME_EINVAL, O_RDWR | O_CREAT, 0600);
Packit 6c4009
  if (fd != -1)
Packit 6c4009
    {
Packit 6c4009
      result |= do_single_test (fd, buf, sizeof (buf), ENOTTY);
Packit 6c4009
      close (fd);
Packit 6c4009
      unlink (PTSNAME_EINVAL);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    printf ("open (\"%s\", O_RDWR | OCREAT) failed\nerrno %d (%s)\n",
Packit 6c4009
	    PTSNAME_EINVAL, errno, strerror (errno));
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"