foreseen

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

browser-test.cpp (8057B)


      1 #include "../include/browser.hpp"
      2 
      3 #include <rapidcheck.h>
      4 #include <rapidcheck/Assertions.h>
      5 
      6 #include <catch2/catch_test_macros.hpp>
      7 #include <cstdlib>
      8 #include <filesystem>
      9 #include <utility>
     10 
     11 #include "../include/utils.hpp"
     12 
     13 std::string genRandom(const int len) {
     14     static const char alphanum[] =
     15         "0123456789"
     16         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     17         "abcdefghijklmnopqrstuvwxyz";
     18     std::string tmp_s;
     19     tmp_s.reserve(len);
     20 
     21     for (int i = 0; i < len; ++i) {
     22         tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
     23     }
     24 
     25     return tmp_s;
     26 }
     27 
     28 TEST_CASE("Basic rendering of all line types") {
     29     Browser b{};
     30     std::string cwd = std::filesystem::current_path();
     31     std::string st = "file://" + cwd + "/tests/sites/all_line_types.gmi";
     32 
     33     b.goToSite(st, true);
     34     REQUIRE(b.getCurrentSite()->getBody() ==
     35             readFileToString("tests/sites/all_line_types.gmi"));
     36     auto strLs = b.renderSite();
     37     auto res = breakLines(strLs, 80, 200);
     38     for (auto& st : res) {
     39         if (st.second.shouldFold) {
     40             REQUIRE(st.first.size() <= 200);
     41         }
     42     }
     43 }
     44 
     45 TEST_CASE("Basic navigation") {
     46     Browser b{};
     47     std::string cwd = std::filesystem::current_path();
     48     std::string st = "file://" + cwd + "/tests/sites/basic.gmi";
     49     std::string st2 = "file://" + cwd + "/tests/sites/basic_2.gmi";
     50 
     51     b.goToSite(st, true);
     52     REQUIRE(b.getCurrentSite()->getBody() ==
     53             readFileToString("tests/sites/basic.gmi"));
     54     b.goToSite(st2, true);
     55     REQUIRE(b.getCurrentSite()->getBody() ==
     56             readFileToString("tests/sites/basic_2.gmi"));
     57 }
     58 
     59 TEST_CASE("Local filesystem relative navigation") {
     60     Browser b{};
     61     std::string cwd = std::filesystem::current_path();
     62     std::string st = "file://" + cwd + "/tests/sites/basic.gmi";
     63 
     64     b.goToSite(st, true);
     65     REQUIRE(b.getCurrentSite()->getBody() ==
     66             readFileToString("tests/sites/basic.gmi"));
     67     b.goToSite("basic_2.gmi", true);
     68     REQUIRE(b.getCurrentSite()->getBody() ==
     69             readFileToString("tests/sites/basic_2.gmi"));
     70 }
     71 
     72 TEST_CASE("Local filesystems navigation via links") {
     73     Browser b{};
     74     std::string cwd = std::filesystem::current_path();
     75     std::string st = "file://" + cwd + "/tests/sites/basic.gmi";
     76 
     77     b.goToSite(st, true);
     78     REQUIRE(b.getCurrentSite()->getBody() ==
     79             readFileToString("tests/sites/basic.gmi"));
     80     b.followLinkNumber(1);
     81     REQUIRE(b.getCurrentSite()->getBody() ==
     82             readFileToString("tests/sites/basic_2.gmi"));
     83 }
     84 
     85 TEST_CASE("Test browser doesn't crash on invalid sites.") {
     86     Browser b{};
     87     b.goToSite("gemini://this_site_doesn_t_exsist");
     88     REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() ==
     89             "about://newtab");
     90     b.goBack();
     91     REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() ==
     92             "about://newtab");
     93 }
     94 
     95 TEST_CASE(
     96     "Test browser doesn't crash when accessing sites with specified port "
     97     "number that is not responsive.") {
     98     Browser b{};
     99     SiteLoadPair res = b.goToSite("gemini://localhost:3847");
    100     REQUIRE(res.result == SITE_PERMANENT_FAILURE);
    101     b.goForward();
    102     b.goBack();
    103     REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() ==
    104             "about://newtab");
    105 }
    106 
    107 TEST_CASE(
    108     "Test browser can access sites with specified (default) port number.") {
    109     Browser b{};
    110     b.goToSite("gemini://localhost:1965");
    111     REQUIRE(b.getCurrentSite()->getMeta() == "text/gemini");
    112 }
    113 
    114 // TODO:  Test invalid meta lines (like the case where weird stuff is sent from
    115 // server)
    116 TEST_CASE("Test browser returns proper meta for visited sites.") {
    117     Browser b{};
    118     b.goToSite("gemini://localhost");
    119     // meta strips status.
    120     REQUIRE(b.getCurrentSite()->getMeta() == "text/gemini");
    121 }
    122 
    123 TEST_CASE(
    124     "Test browser doesn't crash on invalid link number following, excessive "
    125     "back, and excessive forwards.") {
    126     Browser b{};
    127     b.goToSite("gemini://localhost/sites/feed.xml");
    128     REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() ==
    129             "gemini://localhost/sites/feed.xml");
    130     for (int i = 0; i < 1000; ++i) {
    131         b.followLinkNumber(i);
    132     }
    133     REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() ==
    134             "gemini://localhost/sites/feed.xml");
    135     b.goBack();
    136     REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() ==
    137             "about://newtab");
    138     b.goForward();
    139     REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() ==
    140             "gemini://localhost/sites/feed.xml");
    141     b.goBack();
    142     b.goBack();
    143     REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() ==
    144             "about://newtab");
    145     b.goForward();
    146     b.goForward();
    147     REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() ==
    148             "gemini://localhost/sites/feed.xml");
    149 }
    150 
    151 TEST_CASE("Test page downloading.") {
    152     Browser b{};
    153     b.goToSite("gemini://localhost/sites/");
    154     REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() ==
    155             "gemini://localhost/sites/");
    156     std::string destination = b.tryDownloadPage();
    157     std::string out = readFileToString(destination);
    158     REQUIRE(out == b.getCurrentSite()->getBody());
    159     REQUIRE(std::filesystem::remove(destination));
    160 }
    161 
    162 TEST_CASE("Test downloading to specific directory") {
    163     std::filesystem::path cwd = std::filesystem::current_path();
    164 
    165     Browser b{};
    166     b.goToSite("gemini://localhost/sites/");
    167     REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() ==
    168             "gemini://localhost/sites/");
    169     std::string destination = b.tryDownloadPage(cwd);
    170     std::string out = readFileToString(destination);
    171     REQUIRE(out == b.getCurrentSite()->getBody());
    172     REQUIRE(destination == cwd / "localhost_sites_");
    173     REQUIRE(std::filesystem::remove(destination));
    174 }
    175 
    176 TEST_CASE(
    177     "Test going forwards doesn't break when accessing an invalid site prior.") {
    178     Browser b{};
    179     SiteLoadPair res = b.goToSite("gemini://aroisetnatsr.aoirseaorstie");
    180     REQUIRE(res.result == SITE_PERMANENT_FAILURE);
    181     b.goForward();
    182 }
    183 
    184 TEST_CASE(
    185     "Test going backwards doesn't break when accessing an invalid site "
    186     "prior.") {
    187     Browser b{};
    188     SiteLoadPair res = b.goToSite("gemini://aroisetnatsr.aoirseaorstie");
    189     REQUIRE(res.result == SITE_PERMANENT_FAILURE);
    190     b.goBack();
    191 }
    192 
    193 TEST_CASE("Test encoding allows relative linking with : in parameter") {
    194     Browser b{};
    195     b.goToSite("gemini://localhost/cgi-bin/search.py");
    196     REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() ==
    197             "gemini://localhost/cgi-bin/search.py");
    198     b.goToSite("?https://test.com");
    199     REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() ==
    200             "gemini://localhost/cgi-bin/search.py?https%3A%2F%2Ftest.com");
    201 }
    202 
    203 TEST_CASE("Sanitize characters to draw  tests") {
    204     Browser b{};
    205     std::string cwd = std::filesystem::current_path();
    206     std::string st = "file://" + cwd + "/tests/sites/line-return.gmi";
    207     SiteLoadPair res = b.goToSite(st);
    208 
    209     REQUIRE(res.result == SITE_LOADED);
    210     REQUIRE(b.getCurrentSite()->getStatusCode() >= 20);
    211     REQUIRE(b.getCurrentSite()->getStatusCode() <= 29);
    212 
    213     auto ls = b.renderSite();
    214     sanitizeCharactersToDraw(ls);
    215     for (auto& line : ls) {
    216         REQUIRE(line.first.find('\r') == std::string::npos);
    217     }
    218 }
    219 
    220 TEST_CASE("Never crash from weird user inputs") {
    221     for (int i = 0; i < 5; ++i) {
    222         Browser b{};
    223 
    224         std::vector<std::string> ls{};
    225         for (int x = 0; x < 10; ++x) {
    226             ls.push_back(genRandom(rand() % 5000));
    227         }
    228 
    229         for (auto& str : ls) {
    230             auto res = handleDestinationResolution("?" + str, false);
    231             if (res.t == STRING_DESTINATION) {
    232                 b.goToSite(res.destination);
    233             } else if (res.t == NUMBER_DESTINATION) {
    234                 b.followLinkNumber(res.linkNumber);
    235             }
    236             b.refresh();
    237             b.goBack();
    238             REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() ==
    239                     "about://newtab");
    240         }
    241     }
    242 }