/*
  This file is part of TALER
  (C) 2026 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file util/test_slug.c
 * @brief Tests for slug and session ID validation
 * @author Christian Grothoff
 */
#include "taler/taler_util.h"


static unsigned int
check_slug (void)
{
  struct
  {
    const char *slug;
    bool expected;
  } tests[] = {
    /* Valid slugs */
    {"a", true},
    {"instance-1", true},
    {"2026.216-EV3XB88000000", true},
    {"A.b_c:d~e", true},
    {"0123456789", true},
    {"...", true},

    /* Invalid slugs */
    {"", false},                                 /* empty */
    {".", false},                                /* special path component */
    {"..", false},                               /* special path component */
    {"a/b", false},                              /* not a single component */
    {"a%2Fb", false},                            /* percent-encoding */
    {"a b", false},
    {"a+b", false},
    {"a=b", false},                              /* '=' is only for session IDs */
    {"a?b", false},
    {"a#b", false},
    {"a&b", false},
    {"a@b", false},
  };
  unsigned int failed = 0;

  for (unsigned int i = 0; i<sizeof (tests) / sizeof (tests[0]); i++)
  {
    bool result = TALER_is_slug (tests[i].slug);

    if (result == tests[i].expected)
      continue;
    failed++;
    fprintf (stderr,
             "FAIL: slug \"%s\" -> %s (expected %s)\n",
             tests[i].slug,
             result ? "VALID" : "INVALID",
             tests[i].expected ? "VALID" : "INVALID");
  }
  return failed;
}


static unsigned int
check_session_id (void)
{
  struct
  {
    const char *session_id;
    bool expected;
  } tests[] = {
    /* Valid session IDs */
    {"", true},                                  /* not session-bound */
    {"session-1", true},
    {"4322-6hvIP7UnmNDVjp5Intuf9jFK7MzW0ycEyxf5Mszl3xs=", true},  /* Paivana ID */
    {"a", true},
    {"A.b_c:d~e=", true},
    {"0123456789", true},
    {"...", true},

    /* Invalid session IDs */
    {".", false},                                /* special path component */
    {"..", false},                               /* special path component */
    {"a/b", false},                              /* not a single component */
    {"a%2Fb", false},                            /* percent-encoding */
    {"a b", false},
    {"a+b", false},
    {"a?b", false},
    {"a#b", false},
    {"a&b", false},
    {"a@b", false},
  };
  unsigned int failed = 0;

  for (unsigned int i = 0; i<sizeof (tests) / sizeof (tests[0]); i++)
  {
    bool result = TALER_is_session_id (tests[i].session_id);

    if (result == tests[i].expected)
      continue;
    failed++;
    fprintf (stderr,
             "FAIL: session ID \"%s\" -> %s (expected %s)\n",
             tests[i].session_id,
             result ? "VALID" : "INVALID",
             tests[i].expected ? "VALID" : "INVALID");
  }
  return failed;
}


int
main (int argc,
      const char *const argv[])
{
  (void) argc;
  (void) argv;
  if (0 != check_slug ())
    return 1;
  if (0 != check_session_id ())
    return 1;
  return 0;
}
