/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ │vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ ╞══════════════════════════════════════════════════════════════════════════════╡ │ Copyright 2020 Justine Alexandra Roberts Tunney │ │ │ │ This program 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; version 2 of the License. │ │ │ │ This program 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 this program; if not, write to the Free Software │ │ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA │ │ 02110-1301 USA │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/bits/bits.h" #include "libc/bits/initializer.h" #include "libc/errno.h" #include "libc/log/log.h" #include "libc/macros.h" #include "libc/mem/mem.h" #include "libc/runtime/gc.h" #include "libc/stdio/stdio.h" #include "libc/str/str.h" #include "libc/testlib/ezbench.h" #include "libc/testlib/testlib.h" #include "libc/x/x.h" #include "net/http/uri.h" #define URIPARSE(URI) uriparse(&uri, (p = URI), (size = sizeof(URI) - 1)) static const char kHttpCosmopolitanVideoUrl[] = "http://cosmopolitan.storage.googleapis.com/pub/vid/blankspace.mpg"; static const char kSipPriceIsTortureUri[] = "sip:bob%20barker:priceisright@[dead:beef::666]:5060;isup-oli=00"; static const char kWinsockIcoPngBase64[] = "\ base64,iVBORw0KGgoAAAANSUhEUgAAAJcAAACXCAYAAAAYn8l5AAAABmJLR0QA/\ wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4woLByMP6uwgW\ QAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAMeSURBVHja7\ d1BcuIwEAVQPMW94GbAzeBkZJepUXpw01ixDO+tE0PML+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UVORK5CYII="; static size_t size; static const char *p; static struct Uri uri; static struct UriMem { struct UriSlice segs[8]; struct UriRef paramsegs[8]; struct UriKeyval params[4], queries[4]; } urimem_; static textstartup void init() { uri.segs.n = ARRAYLEN(urimem_.segs); uri.segs.p = urimem_.segs; uri.params.n = ARRAYLEN(urimem_.params); uri.params.p = urimem_.params; uri.queries.n = ARRAYLEN(urimem_.queries); uri.queries.p = urimem_.queries; uri.paramsegs.n = ARRAYLEN(urimem_.paramsegs); uri.paramsegs.p = urimem_.paramsegs; } const void *const g_name_ctor[] initarray = {init}; TEST(uriparse, sipPstnUri) { EXPECT_NE(-1, URIPARSE("sip:+12125650666")); EXPECT_STREQ("sip", gc(strndup(p + uri.scheme.i, uri.scheme.n))); EXPECT_STREQ("+12125650666", gc(strndup(p + uri.host.i, uri.host.n))); EXPECT_STREQ("", gc(strndup(p + uri.opaque.i, uri.opaque.n))); } TEST(uriparse, printVideoUrl) { EXPECT_NE(-1, URIPARSE(kHttpCosmopolitanVideoUrl)); EXPECT_STREQ("http", gc(strndup(p + uri.scheme.i, uri.scheme.n))); EXPECT_STREQ("cosmopolitan.storage.googleapis.com", gc(strndup(p + uri.host.i, uri.host.n))); EXPECT_STREQ("", gc(strndup(p + uri.port.i, uri.port.n))); EXPECT_STREQ("/pub/vid/blankspace.mpg", gc(strndup(p + uri.segs.p[0].i, (uri.segs.p[uri.segs.i - 1].n + (uri.segs.p[uri.segs.i - 1].i - uri.segs.p[0].i))))); } TEST(uriparse, localRelativeFile) { EXPECT_NE(-1, URIPARSE("blankspace.mpg")); EXPECT_STREQ("", gc(strndup(p + uri.scheme.i, uri.scheme.n))); EXPECT_STREQ("", gc(strndup(p + uri.host.i, uri.host.n))); EXPECT_STREQ("", gc(strndup(p + uri.port.i, uri.port.n))); EXPECT_STREQ("blankspace.mpg", gc(strndup(p + uri.segs.p[0].i, (uri.segs.p[uri.segs.i - 1].n + (uri.segs.p[uri.segs.i - 1].i - uri.segs.p[0].i))))); } TEST(uriparse, badPort_einval) { EXPECT_EQ(-1, URIPARSE("http://hello.example:http/")); EXPECT_EQ(EINVAL, errno); } TEST(uriparse, datauri) { size = strlen((p = gc(xstrcat("data:image/png;", kWinsockIcoPngBase64)))); EXPECT_NE(-1, uriparse(&uri, p, size)); EXPECT_EQ(5, uri.opaque.i); EXPECT_EQ(size - 5, uri.opaque.n); } //////////////////////////////////////////////////////////////////////////////// BENCH(uriparse, bench) { EZBENCH(donothing, URIPARSE("sip:+12125650666")); EZBENCH(donothing, URIPARSE("http://hello.example")); EZBENCH(donothing, URIPARSE(kHttpCosmopolitanVideoUrl)); EZBENCH(donothing, URIPARSE(kSipPriceIsTortureUri)); } BENCH(uriparse, bigWinsockIcoPngUri) { const char *BigDataIconUri; BigDataIconUri = gc(xstrcat("data:image/png;", kWinsockIcoPngBase64)); size = strlen(kWinsockIcoPngBase64); EZBENCH(donothing, uriparse(&uri, BigDataIconUri, size)); }