(root)/
glibc-2.38/
shadow/
tst-putspent.c
       1  /* Test for processing of invalid shadow entries.  [BZ #18724]
       2     Copyright (C) 2015-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #include <errno.h>
      20  #include <shadow.h>
      21  #include <stdbool.h>
      22  #include <stdio.h>
      23  #include <stdlib.h>
      24  #include <string.h>
      25  
      26  static bool errors;
      27  
      28  static void
      29  check (struct spwd p, const char *expected)
      30  {
      31    char *buf;
      32    size_t buf_size;
      33    FILE *f = open_memstream (&buf, &buf_size);
      34  
      35    if (f == NULL)
      36      {
      37        printf ("open_memstream: %m\n");
      38        errors = true;
      39        return;
      40      }
      41  
      42    int ret = putspent (&p, f);
      43  
      44    if (expected == NULL)
      45      {
      46        if (ret == -1)
      47  	{
      48  	  if (errno != EINVAL)
      49  	    {
      50  	      printf ("putspent: unexpected error code: %m\n");
      51  	      errors = true;
      52  	    }
      53  	}
      54        else
      55  	{
      56  	  printf ("putspent: unexpected success (\"%s\")\n", p.sp_namp);
      57  	  errors = true;
      58  	}
      59      }
      60    else
      61      {
      62        /* Expect success.  */
      63        size_t expected_length = strlen (expected);
      64        if (ret == 0)
      65  	{
      66  	  long written = ftell (f);
      67  
      68  	  if (written <= 0 || fflush (f) < 0)
      69  	    {
      70  	      printf ("stream error: %m\n");
      71  	      errors = true;
      72  	    }
      73  	  else if (buf[written - 1] != '\n')
      74  	    {
      75  	      printf ("FAILED: \"%s\" without newline\n", expected);
      76  	      errors = true;
      77  	    }
      78  	  else if (strncmp (buf, expected, written - 1) != 0
      79  		   || written - 1 != expected_length)
      80  	    {
      81  	      printf ("FAILED: \"%s\" (%ld), expected \"%s\" (%zu)\n",
      82  		      buf, written - 1, expected, expected_length);
      83  	      errors = true;
      84  	    }
      85  	}
      86        else
      87  	{
      88  	  printf ("FAILED: putspent (expected \"%s\"): %m\n", expected);
      89  	  errors = true;
      90  	}
      91      }
      92  
      93    fclose (f);
      94    free (buf);
      95  }
      96  
      97  static int
      98  do_test (void)
      99  {
     100    check ((struct spwd) {
     101        .sp_namp = (char *) "root",
     102      },
     103      "root::0:0:0:0:0:0:0");
     104    check ((struct spwd) {
     105        .sp_namp = (char *) "root",
     106        .sp_pwdp = (char *) "password",
     107      },
     108      "root:password:0:0:0:0:0:0:0");
     109    check ((struct spwd) {
     110        .sp_namp = (char *) "root",
     111        .sp_pwdp = (char *) "password",
     112        .sp_lstchg = -1,
     113        .sp_min = -1,
     114        .sp_max = -1,
     115        .sp_warn = -1,
     116        .sp_inact = -1,
     117        .sp_expire = -1,
     118        .sp_flag = -1
     119      },
     120      "root:password:::::::");
     121    check ((struct spwd) {
     122        .sp_namp = (char *) "root",
     123        .sp_pwdp = (char *) "password",
     124        .sp_lstchg = 1,
     125        .sp_min = 2,
     126        .sp_max = 3,
     127        .sp_warn = 4,
     128        .sp_inact = 5,
     129        .sp_expire = 6,
     130        .sp_flag = 7
     131      },
     132      "root:password:1:2:3:4:5:6:7");
     133  
     134    /* Bad values.  */
     135    {
     136      static const char *const bad_strings[] = {
     137        ":",
     138        "\n",
     139        ":bad",
     140        "\nbad",
     141        "b:ad",
     142        "b\nad",
     143        "bad:",
     144        "bad\n",
     145        "b:a\nd",
     146        NULL
     147      };
     148      for (const char *const *bad = bad_strings; *bad != NULL; ++bad)
     149        {
     150  	check ((struct spwd) {
     151  	    .sp_namp = (char *) *bad,
     152  	  }, NULL);
     153  	check ((struct spwd) {
     154  	    .sp_namp = (char *) "root",
     155  	    .sp_pwdp = (char *) *bad,
     156  	  }, NULL);
     157        }
     158    }
     159  
     160    return errors;
     161  }
     162  
     163  #define TEST_FUNCTION do_test ()
     164  #include "../test-skeleton.c"