Blame login/tst-updwtmpx.c

Packit Service c57519
/* Basic test coverage for updwtmpx.
Packit Service c57519
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit Service c57519
   This file is part of the GNU C Library.
Packit Service c57519
Packit Service c57519
   The GNU C Library is free software; you can redistribute it and/or
Packit Service c57519
   modify it under the terms of the GNU Lesser General Public License as
Packit Service c57519
   published by the Free Software Foundation; either version 2.1 of the
Packit Service c57519
   License, or (at your option) any later version.
Packit Service c57519
Packit Service c57519
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service c57519
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service c57519
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service c57519
   Lesser General Public License for more details.
Packit Service c57519
Packit Service c57519
   You should have received a copy of the GNU Lesser General Public
Packit Service c57519
   License along with the GNU C Library; see the file COPYING.LIB.  If
Packit Service c57519
   not, see <http://www.gnu.org/licenses/>.  */
Packit Service c57519
Packit Service c57519
/* This program runs a series of tests.  Each one calls updwtmpx
Packit Service c57519
   twice, to write two records, optionally with misalignment in the
Packit Service c57519
   file, and reads back the results.  */
Packit Service c57519
Packit Service c57519
#include <errno.h>
Packit Service c57519
#include <stdlib.h>
Packit Service c57519
#include <support/check.h>
Packit Service c57519
#include <support/descriptors.h>
Packit Service c57519
#include <support/support.h>
Packit Service c57519
#include <support/temp_file.h>
Packit Service c57519
#include <support/test-driver.h>
Packit Service c57519
#include <support/xunistd.h>
Packit Service c57519
#include <unistd.h>
Packit Service c57519
#include <utmpx.h>
Packit Service c57519
Packit Service c57519
static int
Packit Service c57519
do_test (void)
Packit Service c57519
{
Packit Service c57519
  /* Two entries filled with an arbitrary bit pattern.  */
Packit Service c57519
  struct utmpx entries[2];
Packit Service c57519
  unsigned char pad;
Packit Service c57519
  {
Packit Service c57519
    unsigned char *p = (unsigned char *) &entries[0];
Packit Service c57519
    for (size_t i = 0; i < sizeof (entries); ++i)
Packit Service c57519
      {
Packit Service c57519
        p[i] = i;
Packit Service c57519
      }
Packit Service c57519
    /* Make sure that the first and second entry and the padding are
Packit Service c57519
       different.  */
Packit Service c57519
    p[sizeof (struct utmpx)] = p[0] + 1;
Packit Service c57519
    pad = p[0] + 2;
Packit Service c57519
  }
Packit Service c57519
Packit Service c57519
  char *path;
Packit Service c57519
  int fd = create_temp_file ("tst-updwtmpx-", &path);
Packit Service c57519
Packit Service c57519
  /* Used to check that updwtmpx does not leave an open file
Packit Service c57519
     descriptor around.  */
Packit Service c57519
  struct support_descriptors *descriptors = support_descriptors_list ();
Packit Service c57519
Packit Service c57519
  /* updwtmpx is expected to remove misalignment.  Optionally insert
Packit Service c57519
     one byte of misalignment at the start and in the middle (after
Packit Service c57519
     the first entry).  */
Packit Service c57519
  for (int misaligned_start = 0; misaligned_start < 2; ++misaligned_start)
Packit Service c57519
    for (int misaligned_middle = 0; misaligned_middle < 2; ++misaligned_middle)
Packit Service c57519
      {
Packit Service c57519
        if (test_verbose > 0)
Packit Service c57519
          printf ("info: misaligned_start=%d misaligned_middle=%d\n",
Packit Service c57519
                  misaligned_start, misaligned_middle);
Packit Service c57519
Packit Service c57519
        xftruncate (fd, 0);
Packit Service c57519
        TEST_COMPARE (pwrite64 (fd, &pad, misaligned_start, 0),
Packit Service c57519
                      misaligned_start);
Packit Service c57519
Packit Service c57519
        /* Write first entry and check it.  */
Packit Service c57519
        errno = 0;
Packit Service c57519
        updwtmpx (path, &entries[0]);
Packit Service c57519
        TEST_COMPARE (errno, 0);
Packit Service c57519
        support_descriptors_check (descriptors);
Packit Service c57519
        TEST_COMPARE (xlseek (fd, 0, SEEK_END), sizeof (struct utmpx));
Packit Service c57519
        struct utmpx buffer;
Packit Service c57519
        TEST_COMPARE (pread64 (fd, &buffer, sizeof (buffer), 0),
Packit Service c57519
                      sizeof (buffer));
Packit Service c57519
        TEST_COMPARE_BLOB (&entries[0], sizeof (entries[0]),
Packit Service c57519
                           &buffer, sizeof (buffer));
Packit Service c57519
Packit Service c57519
        /* Middle mis-alignmet.  */
Packit Service c57519
        TEST_COMPARE (pwrite64 (fd, &pad, misaligned_middle,
Packit Service c57519
                                sizeof (struct utmpx)), misaligned_middle);
Packit Service c57519
Packit Service c57519
        /* Write second entry and check both entries.  */
Packit Service c57519
        errno = 0;
Packit Service c57519
        updwtmpx (path, &entries[1]);
Packit Service c57519
        TEST_COMPARE (errno, 0);
Packit Service c57519
        support_descriptors_check (descriptors);
Packit Service c57519
        TEST_COMPARE (xlseek (fd, 0, SEEK_END), 2 * sizeof (struct utmpx));
Packit Service c57519
        TEST_COMPARE (pread64 (fd, &buffer, sizeof (buffer), 0),
Packit Service c57519
                      sizeof (buffer));
Packit Service c57519
        TEST_COMPARE_BLOB (&entries[0], sizeof (entries[0]),
Packit Service c57519
                           &buffer, sizeof (buffer));
Packit Service c57519
        TEST_COMPARE (pread64 (fd, &buffer, sizeof (buffer), sizeof (buffer)),
Packit Service c57519
                      sizeof (buffer));
Packit Service c57519
        TEST_COMPARE_BLOB (&entries[1], sizeof (entries[1]),
Packit Service c57519
                           &buffer, sizeof (buffer));
Packit Service c57519
      }
Packit Service c57519
Packit Service c57519
  support_descriptors_free (descriptors);
Packit Service c57519
  free (path);
Packit Service c57519
  xclose (fd);
Packit Service c57519
Packit Service c57519
  return 0;
Packit Service c57519
}
Packit Service c57519
Packit Service c57519
#include <support/test-driver.c>