Blame elf/tst-pldd.c

Packit Service 2725fa
/* Basic tests for pldd program.
Packit Service 2725fa
   Copyright (C) 2019 Free Software Foundation, Inc.
Packit Service 2725fa
   This file is part of the GNU C Library.
Packit Service 2725fa
Packit Service 2725fa
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 2725fa
   modify it under the terms of the GNU Lesser General Public
Packit Service 2725fa
   License as published by the Free Software Foundation; either
Packit Service 2725fa
   version 2.1 of the License, or (at your option) any later version.
Packit Service 2725fa
Packit Service 2725fa
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 2725fa
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 2725fa
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 2725fa
   Lesser General Public License for more details.
Packit Service 2725fa
Packit Service 2725fa
   You should have received a copy of the GNU Lesser General Public
Packit Service 2725fa
   License along with the GNU C Library; if not, see
Packit Service 2725fa
   <http://www.gnu.org/licenses/>.  */
Packit Service 2725fa
Packit Service 2725fa
#include <stdio.h>
Packit Service 2725fa
#include <string.h>
Packit Service 2725fa
#include <unistd.h>
Packit Service 2725fa
#include <stdint.h>
Packit Service 2725fa
#include <libgen.h>
Packit Service 2725fa
#include <stdbool.h>
Packit Service 2725fa
Packit Service 2725fa
#include <array_length.h>
Packit Service 2725fa
#include <gnu/lib-names.h>
Packit Service 2725fa
Packit Service 2725fa
#include <support/subprocess.h>
Packit Service 2725fa
#include <support/capture_subprocess.h>
Packit Service 2725fa
#include <support/check.h>
Packit Service 2725fa
Packit Service 2725fa
static void
Packit Service 2725fa
target_process (void *arg)
Packit Service 2725fa
{
Packit Service 2725fa
  pause ();
Packit Service 2725fa
}
Packit Service 2725fa
Packit Service 2725fa
/* The test runs in a container because pldd does not support tracing
Packit Service 2725fa
   a binary started by the loader iself (as with testrun.sh).  */
Packit Service 2725fa
Packit Service 2725fa
static int
Packit Service 2725fa
do_test (void)
Packit Service 2725fa
{
Packit Service 2725fa
  /* Create a copy of current test to check with pldd.  */
Packit Service 2725fa
  struct support_subprocess target = support_subprocess (target_process, NULL);
Packit Service 2725fa
Packit Service 2725fa
  /* Run 'pldd' on test subprocess.  */
Packit Service 2725fa
  struct support_capture_subprocess pldd;
Packit Service 2725fa
  {
Packit Service 2725fa
    /* Three digits per byte plus null terminator.  */
Packit Service 2725fa
    char pid[3 * sizeof (uint32_t) + 1];
Packit Service 2725fa
    snprintf (pid, array_length (pid), "%d", target.pid);
Packit Service 2725fa
Packit Service 2725fa
    const char prog[] = "/usr/bin/pldd";
Packit Service 2725fa
Packit Service 2725fa
    pldd = support_capture_subprogram (prog,
Packit Service 2725fa
      (char *const []) { (char *) prog, pid, NULL });
Packit Service 2725fa
Packit Service 2725fa
    support_capture_subprocess_check (&pldd, "pldd", 0, sc_allow_stdout);
Packit Service 2725fa
  }
Packit Service 2725fa
Packit Service 2725fa
  /* Check 'pldd' output.  The test is expected to be linked against only
Packit Service 2725fa
     loader and libc.  */
Packit Service 2725fa
  {
Packit Service 2725fa
    pid_t pid;
Packit Service 2725fa
    char buffer[512];
Packit Service 2725fa
#define STRINPUT(size) "%" # size "s"
Packit Service 2725fa
Packit Service 2725fa
    FILE *out = fmemopen (pldd.out.buffer, pldd.out.length, "r");
Packit Service 2725fa
    TEST_VERIFY (out != NULL);
Packit Service 2725fa
Packit Service 2725fa
    /* First line is in the form of <pid>: <full path of executable>  */
Packit Service 2725fa
    TEST_COMPARE (fscanf (out, "%u: " STRINPUT (512), &pid, buffer), 2);
Packit Service 2725fa
Packit Service 2725fa
    TEST_COMPARE (pid, target.pid);
Packit Service 2725fa
    TEST_COMPARE (strcmp (basename (buffer), "tst-pldd"), 0);
Packit Service 2725fa
Packit Service 2725fa
    /* It expects only one loader and libc loaded by the program.  */
Packit Service 2725fa
    bool interpreter_found = false, libc_found = false;
Packit Service 2725fa
    while (fgets (buffer, array_length (buffer), out) != NULL)
Packit Service 2725fa
      {
Packit Service 2725fa
       /* Ignore vDSO.  */
Packit Service 2725fa
        if (buffer[0] != '/')
Packit Service 2725fa
         continue;
Packit Service 2725fa
Packit Service 2725fa
       /* Remove newline so baseline (buffer) can compare against the
Packit Service 2725fa
          LD_SO and LIBC_SO macros unmodified.  */
Packit Service 2725fa
       if (buffer[strlen(buffer)-1] == '\n')
Packit Service 2725fa
         buffer[strlen(buffer)-1] = '\0';
Packit Service 2725fa
Packit Service 2725fa
       if (strcmp (basename (buffer), LD_SO) == 0)
Packit Service 2725fa
         {
Packit Service 2725fa
           TEST_COMPARE (interpreter_found, false);
Packit Service 2725fa
           interpreter_found = true;
Packit Service 2725fa
           continue;
Packit Service 2725fa
         }
Packit Service 2725fa
Packit Service 2725fa
       if (strcmp (basename (buffer), LIBC_SO) == 0)
Packit Service 2725fa
         {
Packit Service 2725fa
           TEST_COMPARE (libc_found, false);
Packit Service 2725fa
           libc_found = true;
Packit Service 2725fa
           continue;
Packit Service 2725fa
         }
Packit Service 2725fa
      }
Packit Service 2725fa
    TEST_COMPARE (interpreter_found, true);
Packit Service 2725fa
    TEST_COMPARE (libc_found, true);
Packit Service 2725fa
Packit Service 2725fa
    fclose (out);
Packit Service 2725fa
  }
Packit Service 2725fa
Packit Service 2725fa
  support_capture_subprocess_free (&pldd);
Packit Service 2725fa
  support_process_terminate (&target);
Packit Service 2725fa
Packit Service 2725fa
  return 0;
Packit Service 2725fa
}
Packit Service 2725fa
Packit Service 2725fa
#include <support/test-driver.c>