Blame nss/tst-field.c

Packit 6c4009
/* Test for invalid field handling in file-style NSS databases.  [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
/* This test needs to be statically linked because it access hidden
Packit 6c4009
   functions.  */
Packit 6c4009
Packit 6c4009
#include <nss.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
#include <support/support.h>
Packit 6c4009
Packit 6c4009
static bool errors;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
check (const char *what, bool expr)
Packit 6c4009
{
Packit 6c4009
  if (!expr)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: %s\n", what);
Packit 6c4009
      errors = true;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define CHECK(expr) check (#expr, (expr))
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
check_rewrite (const char *input, const char *expected)
Packit 6c4009
{
Packit 6c4009
  char *to_free;
Packit 6c4009
  const char *result = __nss_rewrite_field (input, &to_free);
Packit 6c4009
  CHECK (result != NULL);
Packit 6c4009
  if (result != NULL && strcmp (result, expected) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("FAIL: rewrite \"%s\" -> \"%s\", expected \"%s\"\n",
Packit 6c4009
	      input, result, expected);
Packit 6c4009
      errors = true;
Packit 6c4009
    }
Packit 6c4009
  free (to_free);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  CHECK (__nss_valid_field (NULL));
Packit 6c4009
  CHECK (__nss_valid_field (""));
Packit 6c4009
  CHECK (__nss_valid_field ("+"));
Packit 6c4009
  CHECK (__nss_valid_field ("-"));
Packit 6c4009
  CHECK (__nss_valid_field (" "));
Packit 6c4009
  CHECK (__nss_valid_field ("abcdef"));
Packit 6c4009
  CHECK (__nss_valid_field ("abc def"));
Packit 6c4009
  CHECK (__nss_valid_field ("abc\tdef"));
Packit 6c4009
  CHECK (!__nss_valid_field ("abcdef:"));
Packit 6c4009
  CHECK (!__nss_valid_field ("abcde:f"));
Packit 6c4009
  CHECK (!__nss_valid_field (":abcdef"));
Packit 6c4009
  CHECK (!__nss_valid_field ("abcdef\n"));
Packit 6c4009
  CHECK (!__nss_valid_field ("\nabcdef"));
Packit 6c4009
  CHECK (!__nss_valid_field (":"));
Packit 6c4009
  CHECK (!__nss_valid_field ("\n"));
Packit 6c4009
Packit 6c4009
  CHECK (__nss_valid_list_field (NULL));
Packit 6c4009
  CHECK (__nss_valid_list_field ((char *[]) {(char *) "good", NULL}));
Packit 6c4009
  CHECK (!__nss_valid_list_field ((char *[]) {(char *) "g,ood", NULL}));
Packit 6c4009
  CHECK (!__nss_valid_list_field ((char *[]) {(char *) "g\nood", NULL}));
Packit 6c4009
  CHECK (!__nss_valid_list_field ((char *[]) {(char *) "g:ood", NULL}));
Packit 6c4009
Packit 6c4009
  check_rewrite (NULL, "");
Packit 6c4009
  check_rewrite ("", "");
Packit 6c4009
  check_rewrite ("abc", "abc");
Packit 6c4009
  check_rewrite ("abc\n", "abc ");
Packit 6c4009
  check_rewrite ("abc:", "abc ");
Packit 6c4009
  check_rewrite ("\nabc", " abc");
Packit 6c4009
  check_rewrite (":abc", " abc");
Packit 6c4009
  check_rewrite (":", " ");
Packit 6c4009
  check_rewrite ("\n", " ");
Packit 6c4009
  check_rewrite ("a:b:c", "a b c");
Packit 6c4009
  check_rewrite ("a\nb\nc", "a b c");
Packit 6c4009
  check_rewrite ("a\nb:c", "a b c");
Packit 6c4009
  check_rewrite ("aa\nbb\ncc", "aa bb cc");
Packit 6c4009
  check_rewrite ("aa\nbb:cc", "aa bb cc");
Packit 6c4009
Packit 6c4009
  return errors;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>