Blame login/tst-updwtmpx.c

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