logo

badwolf

Minimalist and privacy-oriented WebKitGTK+ browser
commit: 06b34a53c2c7af02fbc673d53b5c13f40c2933a6
parent: 4e144dd01af4855e02ed394c75e8c04c4dae710b
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Mon, 16 Dec 2019 01:47:12 +0100

Add tests on badwolf_ensure_uri_scheme

Diffstat:

MMakefile19++++++++++++++-----
Mbadwolf.c26+-------------------------
Mbadwolf.h12------------
Auri.c39+++++++++++++++++++++++++++++++++++++++
Auri.h17+++++++++++++++++
Auri_test.c54++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 125 insertions(+), 42 deletions(-)

diff --git a/Makefile b/Makefile @@ -12,13 +12,15 @@ APPSDIR = $(PREFIX)/share/applications PACKAGE = Badwolf DEPS = gtk+-3.0 webkit2gtk-4.0 libsoup-2.4 -OBJS = keybindings.o badwolf.o +OBJS = uri.o keybindings.o badwolf.o +OBJS_test = uri_test.o EXE = badwolf +EXE_test = uri_test TRANS = fr.mo DOCS = usr.bin.badwolf README.md KnowledgeBase.md CC = cc -CFLAGS = -g -Wall -Wextra -Wconversion -Wsign-conversion +CFLAGS = -g -Wall -Wextra -Wconversion -Wsign-conversion -O2 CDEPS = `pkg-config --cflags $(DEPS)` -DDATADIR=\"$(DATADIR)\" -DPACKAGE=\"$(PACKAGE)\" -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION_FULL)\" LIBS = `pkg-config --libs $(DEPS)` @@ -43,6 +45,14 @@ badwolf: $(OBJS) .c.o: $(CC) -std=c11 $(CFLAGS) $(CDEPS) -c -o $@ $< +uri_test: uri.o uri_test.o + $(CC) -std=c11 -o $@ uri.o uri_test.o $(LDFLAGS) $(LIBS) + ./$@ + +.PHONY: test +test: $(EXE_test) + +.PHONY: install install: all mkdir -p $(DESTDIR)$(BINDIR) cp -p badwolf $(DESTDIR)$(BINDIR)/badwolf @@ -56,10 +66,9 @@ install: all cp -p $(DOCS) $(DESTDIR)$(DOCDIR)/ @printf '\nNote: An example AppArmor profile has been installed at '$(DOCDIR)/usr.bin.badwolf'\n' +.PHONY: clean clean: - rm -fr locale $(OBJS) $(EXE) + rm -fr locale $(OBJS) $(OBJS_test) $(EXE) $(EXE_test) format: *.c *.h clang-format -style=file -assume-filename=.clang-format -i *.c *.h - -.PHONY: clean install diff --git a/badwolf.c b/badwolf.c @@ -6,13 +6,13 @@ #include "config.h" #include "keybindings.h" +#include "uri.h" #include <glib/gi18n.h> /* _() and other internationalization/localization helpers */ #include <glib/gprintf.h> /* g_fprintf() */ #include <libsoup/soup.h> /* soup* */ #include <locale.h> /* LC_* */ #include <stdlib.h> /* malloc() */ -#include <unistd.h> /* access() */ const gchar *homepage = "https://hacktivis.me/projects/badwolf"; const gchar *version = VERSION; @@ -63,30 +63,6 @@ static void closeCb_clicked(GtkButton *close, gpointer user_data); static void notebookCb_switch__page(GtkNotebook *notebook, GtkWidget *page, guint page_num, gpointer user_data); -const gchar * -badwolf_ensure_uri_scheme(const gchar *text, gboolean try_file) -{ - SoupURI *uri = soup_uri_new(text); - - if(uri->path == NULL || text == NULL) - { - soup_uri_free(uri); - return "about:blank"; - } - - if(uri->scheme != NULL) - { - soup_uri_free(uri); - return text; - } - - soup_uri_free(uri); - - if(try_file && (access(text, R_OK) == 0)) return g_strdup_printf("file://%s", text); - - return g_strdup_printf("http://%s", text); -} - static gboolean WebViewCb_close(WebKitWebView *webView, gpointer user_data) { diff --git a/badwolf.h b/badwolf.h @@ -30,18 +30,6 @@ struct Client GtkWidget *search; }; -/* badwolf_ensure_uri_scheme: tries to add a scheme on a pseudo-URL missing a scheme - * - gchar text: pseudo-URL missing a scheme - * - gboolean try_file: when TRUE check try first if it can be a file:// path - * - * When `text` isn't exploitable (ie. NULL), returns "about:blank", - * when the URL seems to be valid, return it, - * if try_file is TRUE, check if it can be file:// path, - * some other checks might be added. - * In the end use the fallback (`http://` for now, might get configuration), - * might get some safeguard. - */ -const gchar *badwolf_ensure_uri_scheme(const gchar *text, gboolean try_file); GtkWidget *badwolf_new_tab_box(const gchar *title, struct Client *browser); void webView_tab_label_change(struct Client *browser, const gchar *title); struct Client * diff --git a/uri.c b/uri.c @@ -0,0 +1,39 @@ +// BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser +// Copyright © 2019 Haelwenn (lanodan) Monnier <contact@hacktivis.me> +// SPDX-License-Identifier: BSD-3-Clause + +#include "uri.h" + +#include <glib/gprintf.h> /* g_fprintf() */ +#include <libsoup/soup.h> /* soup* */ +#include <unistd.h> /* access() */ + +const gchar * +badwolf_ensure_uri_scheme(const gchar *text, gboolean try_file) +{ + const gchar *fallback = "about:blank"; + SoupURI *uri; + + uri = soup_uri_new(text); + if(!uri) return fallback; + + //g_fprintf(stderr, "uri->scheme = %s, uri->path = %s\n", uri->scheme, uri->path); + + if(!uri->path) + { + soup_uri_free(uri); + return fallback; + } + + if(uri->scheme) + { + soup_uri_free(uri); + return text; + } + + soup_uri_free(uri); + + if(try_file && (access(text, R_OK) == 0)) return g_strdup_printf("file://%s", text); + + return g_strdup_printf("http://%s", text); +} diff --git a/uri.h b/uri.h @@ -0,0 +1,17 @@ +#ifndef URI_H_INCLUDED +#define URI_H_INCLUDED +#include <glib.h> + +/* badwolf_ensure_uri_scheme: tries to add a scheme on a pseudo-URL missing a scheme + * - gchar text: pseudo-URL missing a scheme + * - gboolean try_file: when TRUE check try first if it can be a file:// path + * + * When `text` isn't exploitable (ie. NULL), returns "about:blank", + * when the URL seems to be valid, return it, + * if try_file is TRUE, check if it can be file:// path, + * some other checks might be added. + * In the end use the fallback (`http://` for now, might get configuration), + * might get some safeguard. + */ +const gchar *badwolf_ensure_uri_scheme(const gchar *text, gboolean try_file); +#endif /* URI_H_INCLUDED */ diff --git a/uri_test.c b/uri_test.c @@ -0,0 +1,54 @@ +// BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser +// Copyright © 2019 Haelwenn (lanodan) Monnier <contact@hacktivis.me> +// SPDX-License-Identifier: BSD-3-Clause + +#include "uri.h" + +#include <glib.h> +#include <glib/gi18n.h> + +static void +badwolf_ensure_uri_scheme_test(void) +{ + const gchar *fallback = "about:blank"; + + struct + { + const gchar *expect; + const gchar *text; + gboolean try_file; + } cases[] = { + // clang formatting hack + {fallback, NULL, FALSE}, + {fallback, NULL, TRUE}, + {fallback, "", FALSE}, + {fallback, "", TRUE}, + {fallback, "/dev/null", FALSE}, + //{"file://dev/null", "/dev/null", TRUE}, + //{"http://example.org", "example.org", FALSE}, + //{"http://example.org", "example.org", TRUE}, + //{fallback, "http://", FALSE}, + //{fallback, "http://", TRUE} + }; + + for(size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) + { + g_info("badwolf_ensure_uri_scheme(\"%s\", %s)", + cases[i].text, + cases[i].try_file ? "TRUE" : "FALSE"); + + const gchar *got = badwolf_ensure_uri_scheme(cases[i].text, cases[i].try_file); + + if(got != cases[i].expect) g_error("expected: \"%s\", got: \"%s\"", cases[i].expect, got); + } +} + +int +main(int argc, char *argv[]) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/badwolf_ensure_uri_scheme/test", badwolf_ensure_uri_scheme_test); + + return g_test_run(); +}