(root)/
libxcrypt-4.4.36/
test/
short-outbuf.c
       1  /* Copyright (C) 2018 Björn Esser <besser82@fedoraproject.org>
       2   *
       3   * Redistribution and use in source and binary forms, with or without
       4   * modification, are permitted.
       5   *
       6   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
       7   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       8   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       9   * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
      10   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      11   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      12   * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      13   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      14   * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      15   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      16   * SUCH DAMAGE.
      17   */
      18  
      19  #include "crypt-port.h"
      20  #include <crypt.h>
      21  #include <stdio.h>
      22  #include <stdlib.h>
      23  #include <string.h>
      24  
      25  struct testcase
      26  {
      27    const char *exp_rn;
      28    const char *exp_ra;
      29  };
      30  
      31  static const struct testcase testcases[] =
      32  {
      33    { "",   "*0" },
      34    { "*",  "*0" },
      35    { "*0", "*0" },
      36  };
      37  
      38  int
      39  main (void)
      40  {
      41    bool ok = true;
      42    char result[5];
      43  
      44    for (size_t i = 0; i < ARRAY_SIZE (testcases); i++)
      45      {
      46        size_t s = i + 1;
      47        int j = (int) s;
      48        char *outbuf = malloc (sizeof (char) * s);
      49  
      50        crypt_rn ("@@", "@@", outbuf, j);
      51  
      52        if (!strncmp (testcases[i].exp_rn, outbuf, s))
      53          {
      54            strcpy (result, "PASS");
      55          }
      56        else
      57          {
      58            strcpy (result, "FAIL");
      59            ok = false;
      60          }
      61  
      62        printf ("Test %zu.0: %s, expected: \"%-2s\", got: \"%-2s\"\n",
      63                s, result, testcases[i].exp_rn, outbuf);
      64  
      65        crypt_ra ("@@", "@@", (void **) &outbuf, &j);
      66  
      67        if (!strncmp (testcases[i].exp_ra, outbuf, strlen(outbuf)))
      68          {
      69            strcpy (result, "PASS");
      70          }
      71        else
      72          {
      73            strcpy (result, "FAIL");
      74            ok = false;
      75          }
      76  
      77        printf ("Test %zu.1: %s, expected: \"%-2s\", got: \"%-2s\"\n",
      78                s, result, testcases[i].exp_ra, outbuf);
      79  
      80        free (outbuf);
      81      }
      82  
      83    return ok ? 0 : 1;
      84  }