regress/unit-tests/url_parse/test_url_parse.c
changeset 0 1d0ce1ebbc72
equal deleted inserted replaced
-1:000000000000 0:1d0ce1ebbc72
       
     1 /*
       
     2  * Copyright (c) 2020 Sunil Nimmagadda <sunil@openbsd.org>
       
     3  *
       
     4  * Permission to use, copy, modify, and distribute this software for any
       
     5  * purpose with or without fee is hereby granted, provided that the above
       
     6  * copyright notice and this permission notice appear in all copies.
       
     7  *
       
     8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
       
     9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
       
    10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
       
    11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
       
    12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
       
    13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
       
    14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
       
    15  */
       
    16 
       
    17 #include <err.h>
       
    18 #include <signal.h>
       
    19 #include <stdio.h>
       
    20 #include <string.h>
       
    21 
       
    22 #include "ftp.h"
       
    23 
       
    24 struct url *ftp_proxy, *http_proxy;
       
    25 volatile sig_atomic_t interrupted;
       
    26 const char *useragent;
       
    27 char *oarg;
       
    28 int activemode, family, io_debug, verbose, progressmeter;
       
    29 
       
    30 int
       
    31 fd_request(char *path, int flags, off_t *offset)
       
    32 {
       
    33 	/* dummy */
       
    34 	return 0;
       
    35 }
       
    36 
       
    37 static struct {
       
    38 	const char	*str;
       
    39 	struct url	 url;
       
    40 	int		 noparse;
       
    41 } testcases[] = {
       
    42 	{ "http://google.com/index.html", {
       
    43 	    S_HTTP, 0, "google.com", "80", "/index.html" } },
       
    44 	{ "https://google.com:", {
       
    45 	    S_HTTPS, 0, "google.com", "443" } },
       
    46 	{ "file:.", {
       
    47 	    S_FILE, 0, NULL, NULL, "." } },
       
    48 	{ "http://[::1]:/index.html", {
       
    49 	    S_HTTP, 1, "::1", "80", "/index.html" } },
       
    50 	{ "http://[::1]:1234/", {
       
    51 	    S_HTTP, 1, "::1", "1234", "/" } },
       
    52 	{ "foo.bar", {}, 1 },
       
    53 	{ "http://[::1:1234", {}, 1 },
       
    54 	{ "http://[1::2::3]:1234", {
       
    55 	    S_HTTP, 0, "1::2::3", "1234" } },
       
    56 	{ "http://foo.com:bar", {
       
    57 	    S_HTTP, 0, "foo.com", "bar" } },
       
    58 	{ "http:/foo.com", {}, 1 },
       
    59 	{ "http://foo:bar@baz.com", {
       
    60 	    S_HTTP, 0, "baz.com", "80" } },
       
    61 	{ "http://[::1]abcd/", {}, 1 },
       
    62 	{ "    http://localhost:8080", {
       
    63 	    S_HTTP, 0, "localhost", "8080" } },
       
    64 	{ "ftps://localhost:21", {}, 1 },
       
    65 	{ "http://marc.info/?l=openbsd-tech&m=151790635206581&q=raw", {
       
    66 	    S_HTTP, 0, "marc.info", "80", "/?l=openbsd-tech&m=151790635206581&q=raw" } },
       
    67 	{ "file://disklabel.template", {
       
    68 	    S_FILE, 0, NULL, NULL, "/disklabel.template" } },
       
    69 	{ "file:/disklabel.template", {
       
    70 	    S_FILE, 0, NULL, NULL, "/disklabel.template" } },
       
    71 	{ "file:///disklabel.template", {
       
    72 	    S_FILE, 0, NULL, NULL, "/disklabel.template" } },
       
    73 };
       
    74 
       
    75 static int
       
    76 ptr_null_cmp(void *a, void *b)
       
    77 {
       
    78 	if ((a && b == NULL) || (a == NULL && b))
       
    79 		return 1;
       
    80 
       
    81 	return 0;
       
    82 }
       
    83 
       
    84 static int
       
    85 url_cmp(struct url *a, struct url *b)
       
    86 {
       
    87 	if (ptr_null_cmp(a, b) ||
       
    88 	    ptr_null_cmp(a->host, b->host) ||
       
    89 	    ptr_null_cmp(a->port, b->port) ||
       
    90 	    ptr_null_cmp(a->path, b->path))
       
    91 		return 1;
       
    92 
       
    93 	if (a->scheme != b->scheme ||
       
    94 	    (a->host && strcmp(a->host, b->host)) ||
       
    95 	    (a->port && strcmp(a->port, b->port)) ||
       
    96 	    (a->path && strcmp(a->path, b->path)))
       
    97 		return 1;
       
    98 
       
    99 	return 0;
       
   100 }
       
   101 
       
   102 int
       
   103 main(void)
       
   104 {
       
   105 	struct url	*url, *eurl;
       
   106 	size_t		 i;
       
   107 
       
   108 	if (freopen("/dev/null", "w", stderr) == NULL)
       
   109 		err(1, "freopen");
       
   110 
       
   111 	for (i = 0; i < nitems(testcases); i++) {
       
   112 		url = url_parse(testcases[i].str);
       
   113 		if (testcases[i].noparse) {
       
   114 			if (url != NULL)
       
   115 				goto bad;
       
   116 
       
   117 			continue;
       
   118 		}
       
   119 
       
   120 		if (url_cmp(url, &testcases[i].url) != 0)
       
   121 			goto bad;
       
   122 	}
       
   123 
       
   124 	return 0;
       
   125 
       
   126  bad:
       
   127 	printf("%s\n", testcases[i].str);
       
   128 	eurl = &testcases[i].url;
       
   129 	printf("Expected: scheme = %s, host = %s, port = %s, path = %s\n",
       
   130 	    url_scheme_str(eurl->scheme), eurl->host, eurl->port, eurl->path);
       
   131 	printf("Got: scheme = %s, host = %s, port = %s, path = %s\n",
       
   132 	    url_scheme_str(url->scheme), url->host, url->port, url->path);
       
   133 	return 1;
       
   134 }