logo

badwolf

minimalist and privacy-oriented web browser based on WebKitGTK git clone https://hacktivis.me/git/badwolf.git
commit: 58af594c9d9cfe7070742ff09ef8528c11d65bef
parent eceb1437726a1fae5280ec57bb35a8065996e21d
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sat, 26 Feb 2022 13:41:55 +0100

fmt: Grab fmt_context_id for tests

Diffstat:

MMakefile12++++++++----
Mbadwolf.c27+++------------------------
Afmt.c31+++++++++++++++++++++++++++++++
Afmt.h4++++
Afmt_test.c56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 102 insertions(+), 28 deletions(-)

diff --git a/Makefile b/Makefile @@ -5,11 +5,11 @@ include config.mk -SRCS = bookmarks.c uri.c uri_test.c keybindings.c downloads.c badwolf.c -OBJS = bookmarks.o uri.o keybindings.o downloads.o badwolf.o -OBJS_test = uri_test.o +SRCS = bookmarks.c fmt.c fmt_test.c uri.c uri_test.c keybindings.c downloads.c badwolf.c +OBJS = bookmarks.o fmt.o uri.o keybindings.o downloads.o badwolf.o +OBJS_test = fmt_test.o uri_test.o EXE = badwolf -EXE_test = uri_test +EXE_test = fmt_test uri_test TRANS = fr.mo pt_BR.mo tr.mo de.mo vi.mo DOCS = usr.bin.badwolf README.md KnowledgeBase.md interface.txt @@ -51,9 +51,13 @@ badwolf: $(OBJS) uri_test: uri.o uri_test.o $(CC) -std=c11 -o $@ uri.o uri_test.o $(LDFLAGS) $(LIBS) +fmt_test: fmt.o fmt_test.o + $(CC) -std=c11 -o $@ fmt.o fmt_test.o $(LDFLAGS) $(LIBS) + .PHONY: test test: $(EXE_test) $(DBG) ./uri_test + $(DBG) ./fmt_test .PHONY: lint lint: diff --git a/badwolf.c b/badwolf.c @@ -7,6 +7,7 @@ #include "bookmarks.h" #include "config.h" #include "downloads.h" +#include "fmt.h" #include "keybindings.h" #include "uri.h" @@ -132,34 +133,12 @@ WebViewCb_notify__uri(WebKitWebView *webView, GParamSpec *pspec, gpointer user_d return TRUE; } -static char * -fmt_context_id(uint64_t num, char *out) -{ - const char alpha_digits[27] = {" ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; - char buf[66] = {'\0'}; - int i, len = 0; - - // Get 0 = 'A' instead of 0 = '' and 1 = 'A' - num++; - - buf[len++] = ' '; - buf[len++] = ':'; - do - { - buf[len++] = alpha_digits[num % 26]; - } while(num /= 26); - - for(i = 0; len > 0; i++) - out[i] = buf[--len]; - - return out; -} - GtkWidget * badwolf_new_tab_box(const gchar *title, struct Client *browser) { (void)browser; - char context_id_str[7] = ""; + /* flawfinder: ignore. bound checks are done */ + char context_id_str[BADWOLF_CTX_SIZ] = {0, 0, 0, 0, 0, 0, 0}; fmt_context_id(browser->context_id, context_id_str); diff --git a/fmt.c b/fmt.c @@ -0,0 +1,31 @@ +// BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser +// Copyright © 2019-2021 Badwolf Authors <https://hacktivis.me/projects/badwolf> +// SPDX-License-Identifier: BSD-3-Clause + +#include "fmt.h" + +/* flawfinder: ignore. `alpha_digits` is never modified */ +static const char alpha_digits[26] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; +void +fmt_context_id(uint64_t num, char *out) +{ + /* flawfinder: ignore. bound checks are done */ + char buf[BADWOLF_CTX_SIZ] = {0, 0, 0, 0, 0, 0, 0}; + int len = 0; + + buf[++len] = ' '; + buf[++len] = ':'; + + buf[++len] = alpha_digits[num % 26]; + num /= 26; + while(num > 0 && len < (BADWOLF_CTX_SIZ - 1)) + { + buf[++len] = alpha_digits[(num - 1) % 26]; + num /= 26; + } + + for(int i = 0; i < len; i++) + { + out[i] = buf[len - i]; + } +} diff --git a/fmt.h b/fmt.h @@ -0,0 +1,4 @@ +#include <stdint.h> // uint64_t + +#define BADWOLF_CTX_SIZ 7 +void fmt_context_id(uint64_t num, char *out); diff --git a/fmt_test.c b/fmt_test.c @@ -0,0 +1,56 @@ +// BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser +// Copyright © 2019-2021 Badwolf Authors <https://hacktivis.me/projects/badwolf> +// SPDX-License-Identifier: BSD-3-Clause + +#include "fmt.h" + +#include <glib.h> + +static void +fmt_context_id_test(void) +{ + struct + { + const char *expect; + uint64_t context_id; + } cases[] = { + // + {"A: ", 0}, + {"B: ", 1}, + {"Y: ", 24}, + {"Z: ", 25}, + {"AA: ", 26}, + {"AB: ", 27}, + {"AZ: ", 51}, + {"BA: ", 52}, + {"BB: ", 53}, + {"QKWW: ", 4294967296}, // 2^32 + {"JFHI: ", 9223372036854775808}, // 2^63 + {"TLPO: ", 18446744073709551614}, // 2^64 -2 + {"TLPP: ", 18446744073709551615}, // 2^64 -1 (aka UINT64_MAX) + }; + + for(size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) + { + g_info("fmt_context_id(%lu)", cases[i].context_id); + /* flawfinder: ignore. bound checks are done */ + char got[BADWOLF_CTX_SIZ] = {0, 0, 0, 0, 0, 0, 0}; + + fmt_context_id(cases[i].context_id, got); + + if(strncmp(got, cases[i].expect, BADWOLF_CTX_SIZ) != 0) + { + 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("/fmt_context_id/test", fmt_context_id_test); + + return g_test_run(); +}