Blame gshadow/tst-putsgent.c

Packit 6c4009
/* Test for processing of invalid gshadow entries.  [BZ #18724]
Packit 6c4009
   Copyright (C) 2015-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
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 <gshadow.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
static bool errors;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
check (struct sgrp e, const char *expected)
Packit 6c4009
{
Packit 6c4009
  char *buf;
Packit 6c4009
  size_t buf_size;
Packit 6c4009
  FILE *f = open_memstream (&buf, &buf_size);
Packit 6c4009
Packit 6c4009
  if (f == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("open_memstream: %m\n");
Packit 6c4009
      errors = true;
Packit 6c4009
      return;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  int ret = putsgent (&e, f);
Packit 6c4009
Packit 6c4009
  if (expected == NULL)
Packit 6c4009
    {
Packit 6c4009
      if (ret == -1)
Packit 6c4009
	{
Packit 6c4009
	  if (errno != EINVAL)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("putsgent: unexpected error code: %m\n");
Packit 6c4009
	      errors = true;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  printf ("putsgent: unexpected success (\"%s\")\n", e.sg_namp);
Packit 6c4009
	  errors = true;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Expect success.  */
Packit 6c4009
      size_t expected_length = strlen (expected);
Packit 6c4009
      if (ret == 0)
Packit 6c4009
	{
Packit 6c4009
	  long written = ftell (f);
Packit 6c4009
Packit 6c4009
	  if (written <= 0 || fflush (f) < 0)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("stream error: %m\n");
Packit 6c4009
	      errors = true;
Packit 6c4009
	    }
Packit 6c4009
	  else if (buf[written - 1] != '\n')
Packit 6c4009
	    {
Packit 6c4009
	      printf ("FAILED: \"%s\" without newline\n", expected);
Packit 6c4009
	      errors = true;
Packit 6c4009
	    }
Packit 6c4009
	  else if (strncmp (buf, expected, written - 1) != 0
Packit 6c4009
		   || written - 1 != expected_length)
Packit 6c4009
	    {
Packit 6c4009
	      printf ("FAILED: \"%s\" (%ld), expected \"%s\" (%zu)\n",
Packit 6c4009
		      buf, written - 1, expected, expected_length);
Packit 6c4009
	      errors = true;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  printf ("FAILED: putsgent (expected \"%s\"): %m\n", expected);
Packit 6c4009
	  errors = true;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  fclose (f);
Packit 6c4009
  free (buf);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  check ((struct sgrp) {
Packit 6c4009
      .sg_namp = (char *) "root",
Packit 6c4009
    },
Packit 6c4009
    "root:::");
Packit 6c4009
  check ((struct sgrp) {
Packit 6c4009
      .sg_namp = (char *) "root",
Packit 6c4009
      .sg_passwd = (char *) "password",
Packit 6c4009
    },
Packit 6c4009
    "root:password::");
Packit 6c4009
  check ((struct sgrp) {
Packit 6c4009
      .sg_namp = (char *) "root",
Packit 6c4009
      .sg_passwd = (char *) "password",
Packit 6c4009
      .sg_adm = (char *[]) {(char *) "adm1", (char *) "adm2", NULL},
Packit 6c4009
      .sg_mem = (char *[]) {(char *) "mem1", (char *) "mem2", NULL},
Packit 6c4009
    },
Packit 6c4009
    "root:password:adm1,adm2:mem1,mem2");
Packit 6c4009
Packit 6c4009
  /* Bad values.  */
Packit 6c4009
  {
Packit 6c4009
    static const char *const bad_strings[] = {
Packit 6c4009
      ":",
Packit 6c4009
      "\n",
Packit 6c4009
      ":bad",
Packit 6c4009
      "\nbad",
Packit 6c4009
      "b:ad",
Packit 6c4009
      "b\nad",
Packit 6c4009
      "bad:",
Packit 6c4009
      "bad\n",
Packit 6c4009
      "b:a\nd",
Packit 6c4009
      ",",
Packit 6c4009
      "\n,",
Packit 6c4009
      ":,",
Packit 6c4009
      ",bad",
Packit 6c4009
      "b,ad",
Packit 6c4009
      "bad,",
Packit 6c4009
      NULL
Packit 6c4009
    };
Packit 6c4009
    for (const char *const *bad = bad_strings; *bad != NULL; ++bad)
Packit 6c4009
      {
Packit 6c4009
	char *members[]
Packit 6c4009
	  = {(char *) "first", (char *) *bad, (char *) "last", NULL};
Packit 6c4009
	if (strpbrk (*bad, ":\n") != NULL)
Packit 6c4009
	  {
Packit 6c4009
	    check ((struct sgrp) {
Packit 6c4009
		.sg_namp = (char *) *bad,
Packit 6c4009
	      }, NULL);
Packit 6c4009
	    check ((struct sgrp) {
Packit 6c4009
		.sg_namp = (char *) "root",
Packit 6c4009
		.sg_passwd = (char *) *bad,
Packit 6c4009
	      }, NULL);
Packit 6c4009
	  }
Packit 6c4009
	check ((struct sgrp) {
Packit 6c4009
	    .sg_namp = (char *) "root",
Packit 6c4009
	    .sg_passwd = (char *) "password",
Packit 6c4009
	    .sg_adm = members
Packit 6c4009
	  }, NULL);
Packit 6c4009
	check ((struct sgrp) {
Packit 6c4009
	    .sg_namp = (char *) "root",
Packit 6c4009
	    .sg_passwd = (char *) "password",
Packit 6c4009
	    .sg_mem = members
Packit 6c4009
	  }, NULL);
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  return errors;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"