/* testurl.c */

/* Copyright 1997 by Eberhard Mattes <mattes@azu.informatik.uni-stuttgart.de>
   Donated to the public domain.  No warranty.

   1997-07-29 Initial version
   1997-09-06 Add -p and -c switches
   1997-09-13 url_check_path() removed */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "libemfw.h"
#include "emio.h"
#include "squid-gw.h"

static void usage (void) ATTR_NORETURN;

static void usage (void)
{
  fputs ("Usage:\n", stderr);
  fputs (" testurl -p          Test url_parse()\n", stderr);
  fputs (" testurl -c          Test url_compare()\n", stderr);
  exit (1);
}

static void test_parse (void)
{
  static char line[1024];
  struct url u;
  const char *s;
  char *p;

  while (fgets (line, sizeof (line), stdin) != NULL)
    {
      p = strchr (line, '\n');
      if (p == NULL)
	{
	  fputs ("Missing linefeed\n", stderr);
	  exit (1);
	}
      /* Test if url_parse() honors the input length. */
      *p = 'x';
      if (url_parse (&u, (octet*) line, p - line, 0) != 0)
	puts ("error");
      else
	{
	  switch (u.scheme)
	    {
	    case URL_CACHE_OBJECT:
	      s = "cache_object";
	      break;
	    case URL_CLSID:
	      s = "clsid";
	      break;
	    case URL_FILE:
	      s = "file";
	      break;
	    case URL_FTP:
	      s = "ftp";
	      break;
	    case URL_GOPHER:
	      s = "gopher";
	      break;
	    case URL_HTTP:
	      s = "http";
	      break;
	    case URL_HTTPS:
	      s = "https";
	      break;
	    case URL_JAVA:
	      s = "java";
	      break;
	    case URL_JAVASCRIPT:
	      s = "javascript";
	      break;
	    case URL_MAILTO:
	      s = "mailto";
	      break;
	    case URL_NEWS:
	      s = "news";
	      break;
	    case URL_NNTP:
	      s = "nntp";
	      break;
	    case URL_PROSPERO:
	      s = "prospero";
	      break;
	    case URL_TELNET:
	      s = "telnet";
	      break;
	    case URL_WAIS:
	      s = "wais";
	      break;
	    case URL_OTHER_SCHEME:
	      s = "OTHER";
	      break;
	    case URL_EMPTY:
	      s = "EMPTY";
	      break;
	    default:
	      abort ();
	    }
	  printf ("%s|%.*s|%.*s|%.*s|%.*s|%.*s|%.*s|%.*s|%.*s|%.*s|%ld\n",
		  s,
		  u.scheme_length, line + u.scheme_start,
		  u.user_length, line + u.user_start,
		  u.password_length, line + u.password_start,
		  u.host_length, line + u.host_start,
		  u.port_length, line + u.port_start,
		  u.path_length, line + u.path_start,
		  u.query_length, line + u.query_start,
		  u.fragment_length, line + u.fragment_start,
		  u.type_length, line + u.type_start,
		  u.port);
	}
    }
}

static void test_compare (void)
{
  static char line[1024];
  struct url u1, u2;
  const char *s, *s2;
  size_t len1, len2;
  int r, lineno;

  lineno = 0;
  while (fgets (line, sizeof (line), stdin) != NULL)
    {
      ++lineno;
      s = strchr (line, '|');
      if (s == NULL)
	goto syntax;
      len1 = s - line;
      s2 = s + 1;
      s = strchr (s2, '|');
      if (s == NULL || (s[1] != '-' && s[1] != '+'))
	goto syntax;
      len2 = s - s2;
      if (url_parse (&u1, (octet*)line, len1, UPF_WILDCARD | UPF_NODEFPORT) != 0
	  || url_parse (&u2, (octet*)s2, len2, 0) != 0)
	{
	  fprintf (stderr, "url_parse() failed in line %d\n", lineno);
	  exit (1);
	}
      r = url_compare ((octet*)line, &u1, (octet*)s2, &u2, 
		 	UCF_IGNORE_CASE | UCF_WILDCARD);
      if (s[1] != (r == 0 ? '+' : '-'))
	{
	  fprintf (stderr, "Error in line %d\n", lineno);
	  exit (1);
	}
    }
  return;

syntax:
  fprintf (stderr, "Syntax error in line %d\n", lineno);
  exit (1);
}

int main (int argc, char *argv[])
{
  if (argc == 2 && strcmp (argv[1], "-p") == 0)
    test_parse ();
  else if (argc == 2 && strcmp (argv[1], "-c") == 0)
    test_compare ();
  else
    usage ();
  return 0;
}
