foreseen

A text-based gemini browser with prefetching.
git clone git://git.laack.co/foreseen.git
Log | Files | Refs | README | LICENSE

commit 552aa296e6cab9159d494d0962a28edea0a2d315
parent ca46a157cbfb8dcb3e4cf826cb819e256827bf63
Author: Andrew Laack <andrew@laack.co>
Date:   Mon, 28 Sep 2026 14:05:05 -0500

Added random walk test to try to find edge cases for browser usage. Found and fixed an issue with long urls as a result.

Diffstat:
M.gitignore | 1+
MMakefile | 5+++++
MTODO.txt | 4++++
Mconfig.mk | 2+-
Minclude/shared.hpp | 9++++++++-
Minclude/utils.hpp | 2++
Msrc/gemini-client.cpp | 6+++++-
Msrc/identity-manager.cpp | 11++++++++---
Msrc/site.cpp | 9+++++++--
Msrc/utils.cpp | 4++++
Mtests/mem-leak-test.cpp | 33+++++++++++++++++++++++++++------
Atests/run-crash-until-fail.sh | 7+++++++
12 files changed, 79 insertions(+), 14 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1 +1,2 @@ *.out +vgcore* diff --git a/Makefile b/Makefile @@ -33,4 +33,9 @@ mem-leak-test: valgrind --tool=memcheck ./test4.out rm test4.out +crash-test: + ${STCOMMAND_P} tests/mem-leak-test.cpp ${COMMAND_S} -o test5.out + ./test5.out + rm test5.out + test: pure-test browser-test fetch-test diff --git a/TODO.txt b/TODO.txt @@ -99,3 +99,7 @@ --- - finalize name + renaming + +--- + +- fix up the config.mk and makefile diff --git a/config.mk b/config.mk @@ -22,4 +22,4 @@ TCOMMAND_S = ${BASE_FILES} ${TLIBS} DCOMMAND_P = ${CC} ${DFLAGS} DCOMMAND_S = ${BASE_FILES} ${LIBS} -STCOMMAND_P = ${CC} ${CFLAGS} +STCOMMAND_P = ${CC} ${CFLAGS} -D DEBUG_MODE diff --git a/include/shared.hpp b/include/shared.hpp @@ -14,4 +14,11 @@ const int COLOR_PREFORMATTED = 201; // color used for preformatted text regions // We guarantee responses can be at least the size limit, and at most 4096 additional bytes const int RESPONSE_SIZE_LIMIT_MB = 5; -const char openUnknownScheme [] = "xdg-open"; +// since the xdg-open stuff is in a seperate process nothing will go to stdout +// even in the echo case. + +#ifdef DEBUG_MODE + const char openUnknownScheme[] = "echo"; +#else + const char openUnknownScheme[] = "xdg-open"; +#endif diff --git a/include/utils.hpp b/include/utils.hpp @@ -46,3 +46,5 @@ Destination handleDestinationResolution(std::string destination, bool isCli); void sanitizeCharactersToDraw(std::vector<std::pair<std::string, TextRender>>& strLs); + +bool isSendableIfGeminiUrl(const uri& u); diff --git a/src/gemini-client.cpp b/src/gemini-client.cpp @@ -15,6 +15,10 @@ Site* GeminiClient::getNetworkedSite(Link link, std::string crtPath, std::string keyPath) { + if (!isSendableIfGeminiUrl(link.getLinkDestination())) { + return nullptr; + } + std::string host = link.getLinkDestination().get_host(); std::string req = link.getLinkDestination().to_string() + "\r\n"; std::string conn = host + ":1965"; @@ -120,7 +124,7 @@ Site* GeminiClient::fetchSite(Link link, std::string crtPath, std::string keyPat try { return getNetworkedSite(link, crtPath, keyPath); } catch (...) { - auto* unreach = new Site{"", ""}; + auto* unreach = new Site{"41 server unreachable", ""}; unreach->setUnreachable(); return unreach; } diff --git a/src/identity-manager.cpp b/src/identity-manager.cpp @@ -24,9 +24,14 @@ Identity IdentityManager::getIdentityForURI(uri destination) { } fs::path dir = certDir / name; - if (fs::exists(dir / "client.crt") && fs::exists(dir / "client.key")) { - return Identity{(dir / "client.key").string(), (dir / "client.crt").string()}; + try { + if (fs::exists(dir / "client.crt") && fs::exists(dir / "client.key")) { + return Identity{(dir / "client.key").string(), (dir / "client.crt").string()}; + } + } + catch (const fs::filesystem_error& e){ + // this can happen if the domain is stupidly long. + // in such cases, there are bigger issues. } - return Identity{"", ""}; } diff --git a/src/site.cpp b/src/site.cpp @@ -1,5 +1,6 @@ #include "../include/site.hpp" #include <algorithm> +#include <cstdint> #include <string> Site::Site(std::string h, std::string b) : body(b), header(h) {} @@ -12,8 +13,12 @@ std::string Site::getBody() { return body; } uint32_t Site::getStatusCode() { - uint32_t statusCode = std::stoi(header.substr(0,2)); - return statusCode; + try { + uint32_t statusCode = std::stoi(header.substr(0,2)); + return statusCode; + } catch (...) { + return uint32_t {41}; + } } std::string Site::getMeta() { diff --git a/src/utils.cpp b/src/utils.cpp @@ -424,3 +424,7 @@ void sanitizeCharactersToDraw(std::vector<std::pair<std::string, TextRender>>& s } return; } + +bool isSendableIfGeminiUrl(const uri& u) { + return u.get_scheme() != "gemini" || u.to_string().size() <= 1024; +} diff --git a/tests/mem-leak-test.cpp b/tests/mem-leak-test.cpp @@ -2,27 +2,48 @@ #include <cstdlib> #include <thread> #include <vector> +#include <ctime> +#include <unistd.h> +std::string gen_random(const int len) { + static const char alphanum[] = + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + std::string tmp_s; + tmp_s.reserve(len); + + for (int i = 0; i < len; ++i) { + tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)]; + } + + return tmp_s; +} void traversal() { Browser b{}; b.goToSite("gemini://laack.co", true); - for(int i = 0; i < 100; ++i) { + for(int i = 0; i < 1000; ++i) { auto* lls = b.getLinkLines(); std::size_t count = lls->size(); delete lls; - if(count == 0) { + if(count == 0 && rand() % 5 == 0) { b.goBack(); } else if(rand() % 5 == 0) { b.goForward(); } else if(rand() % 5 == 0) { b.goBack(); - } else { - b.followLinkNumber(rand() % count + 1); + } else if (rand() % 5 == 0){ + b.goToSite(std::string{"gemini://"} + gen_random(rand() % 400) + ".com"); + } + else { + if(count == 0) { + count = 10; + } + b.followLinkNumber(((rand() % count) + 1) + (rand() % 5)); } } - } // NOTE: When running this you probably want to run something like: @@ -31,7 +52,7 @@ void traversal() { int main() { std::vector<std::thread*> vec {}; - for(int x = 0; x < 10; ++x) { + for(int x = 0; x < 100; ++x) { auto* t = new std::thread (&traversal); vec.push_back(t); } diff --git a/tests/run-crash-until-fail.sh b/tests/run-crash-until-fail.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +while make crash-test; do + echo $SECONDS +done + +echo "Failed"