puppet-test.cpp (11728B)
1 #include <ncurses.h> 2 #include <rapidcheck.h> 3 #include <rapidcheck/Check.h> 4 #include <rapidcheck/Log.h> 5 6 #include <catch2/catch_test_macros.hpp> 7 #include <cstdlib> 8 #include <filesystem> 9 #include <string> 10 11 #include "../include/render.hpp" 12 #ifndef CTRL 13 #define CTRL(c) ((c) & 037) 14 #endif 15 16 std::string genRandom(const int len) { 17 static const char alpha[] = 18 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 19 "abcdefghijklmnopqrstuvwxyz"; 20 std::string tmp_s; 21 tmp_s.reserve(len); 22 23 for (int i = 0; i < len; ++i) { 24 tmp_s += alpha[rand() % (sizeof(alpha) - 1)]; 25 } 26 27 return tmp_s; 28 } 29 30 std::string genAlNumNlSp(const int len) { 31 static const char alphanum[] = 32 "0123456789" 33 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 34 "abcdefghijklmnopqrstuvwxyz\n "; 35 std::string tmp_s; 36 tmp_s.reserve(len); 37 38 for (int i = 0; i < len; ++i) { 39 tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)]; 40 } 41 42 return tmp_s; 43 } 44 45 // Puppet entire browser 46 47 TEST_CASE("q quits session immediately") { 48 Browser* bPtr = new Browser{}; 49 Browser& b = *bPtr; 50 DrawState ds{}; 51 ds.bPtr = bPtr; 52 REQUIRE_FALSE(mainLoop(ds, b, 'q', 0, 0)); 53 delete bPtr; 54 } 55 56 TEST_CASE("RESIZE falls through and returns") { 57 Browser* bPtr = new Browser{}; 58 Browser& b = *bPtr; 59 DrawState ds{}; 60 ds.bPtr = bPtr; 61 REQUIRE(mainLoop(ds, b, KEY_RESIZE, 0, 0)); 62 delete bPtr; 63 } 64 65 TEST_CASE("Redirect to broken doesn't deadlock") { 66 Browser* bPtr = new Browser{}; 67 Browser& b = *bPtr; 68 DrawState ds{}; 69 ds.bPtr = bPtr; 70 std::string dest = "ogemini://localhost/cgi-bin/redirect-to-broken.py"; 71 for (auto& ch : dest) { 72 REQUIRE(mainLoop(ds, b, ch, 100, 100)); 73 } 74 75 for (int i = 0; i < 100; ++i) { 76 REQUIRE(mainLoop(ds, b, 'y', 100, 100)); 77 } 78 79 REQUIRE(mainLoop(ds, b, 'n', 100, 100)); 80 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 81 "about://newtab"); 82 delete bPtr; 83 } 84 85 TEST_CASE("Open page handler returns") { 86 Browser* bPtr = new Browser{}; 87 Browser& b = *bPtr; 88 DrawState ds{}; 89 ds.bPtr = bPtr; 90 REQUIRE(mainLoop(ds, b, 'o', 0, 0)); 91 delete bPtr; 92 } 93 94 TEST_CASE("Cancel 'o' menu by pressing escape") { 95 Browser* bPtr = new Browser{}; 96 Browser& b = *bPtr; 97 DrawState ds{}; 98 ds.bPtr = bPtr; 99 REQUIRE(mainLoop(ds, b, 'o', 0, 0)); 100 REQUIRE(mainLoop(ds, b, 'l', 0, 0)); 101 REQUIRE(mainLoop(ds, b, 'a', 0, 0)); 102 REQUIRE(mainLoop(ds, b, 'a', 0, 0)); 103 REQUIRE(mainLoop(ds, b, 'a', 0, 0)); 104 REQUIRE(mainLoop(ds, b, KEY_BACKSPACE, 0, 0)); 105 REQUIRE(mainLoop(ds, b, 'c', 0, 0)); 106 REQUIRE(mainLoop(ds, b, 'k', 0, 0)); 107 REQUIRE(mainLoop(ds, b, '.', 0, 0)); 108 REQUIRE(mainLoop(ds, b, 'c', 0, 0)); 109 REQUIRE(mainLoop(ds, b, 'o', 0, 0)); 110 REQUIRE(mainLoop(ds, b, 27, 0, 0)); // escape 111 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 112 "about://newtab"); 113 delete bPtr; 114 } 115 116 TEST_CASE("Handle redirects") { 117 Browser* bPtr = new Browser{}; 118 Browser& b = *bPtr; 119 DrawState ds{}; 120 ds.bPtr = bPtr; 121 std::string st = "ogemini://localhost/cgi-bin/redirect.py"; 122 for (auto& ch : st) { 123 REQUIRE(mainLoop(ds, b, ch, 0, 0)); 124 } 125 126 REQUIRE(mainLoop(ds, b, '\n', 0, 0)); 127 128 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 129 "gemini://localhost/cgi-bin/redirect.py"); 130 131 REQUIRE(mainLoop(ds, b, 'y', 0, 0)); // follow redirect 132 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 133 "gemini://localhost"); 134 delete bPtr; 135 } 136 137 TEST_CASE("Cancel input") { 138 Browser* bPtr = new Browser{}; 139 Browser& b = *bPtr; 140 DrawState ds{}; 141 ds.bPtr = bPtr; 142 143 std::string st = "ogemini://localhost/cgi-bin/search.py"; 144 for (auto& ch : st) { 145 REQUIRE(mainLoop(ds, b, ch, 0, 0)); 146 } 147 148 REQUIRE(mainLoop(ds, b, '\n', 0, 0)); 149 150 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 151 "gemini://localhost/cgi-bin/search.py"); 152 153 std::string s2 = "test search query"; 154 for (auto& ch : s2) { 155 REQUIRE(mainLoop(ds, b, ch, 0, 0)); 156 } 157 158 REQUIRE(mainLoop(ds, b, 27, 0, 0)); // escape 159 160 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 161 "about://newtab"); 162 delete bPtr; 163 } 164 165 TEST_CASE("Handle random status codes") { 166 for (int i = 0; i < 100; ++i) { 167 Browser* bPtr = new Browser{}; 168 Browser& b = *bPtr; 169 DrawState ds{}; 170 ds.bPtr = bPtr; 171 172 std::string st = "ogemini://localhost/cgi-bin/rnd.py"; 173 for (auto& ch : st) { 174 REQUIRE(mainLoop(ds, b, ch, 0, 0)); 175 } 176 REQUIRE(mainLoop(ds, b, '\n', 0, 0)); 177 delete bPtr; 178 } 179 } 180 181 TEST_CASE("Follow non-existent link") { 182 Browser* bPtr = new Browser{}; 183 Browser& b = *bPtr; 184 DrawState ds{}; 185 ds.bPtr = bPtr; 186 187 std::string st = "ogemini://localhost/cgi-bin/"; 188 for (auto& ch : st) { 189 REQUIRE(mainLoop(ds, b, ch, 0, 0)); 190 } 191 REQUIRE(mainLoop(ds, b, '\n', 0, 0)); 192 193 REQUIRE(mainLoop(ds, b, 'o', 0, 0)); 194 REQUIRE(mainLoop(ds, b, '9', 0, 0)); 195 REQUIRE(mainLoop(ds, b, '9', 0, 0)); 196 REQUIRE(mainLoop(ds, b, '9', 0, 0)); 197 REQUIRE(mainLoop(ds, b, '\n', 0, 0)); 198 199 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 200 "gemini://localhost/cgi-bin/"); 201 202 delete bPtr; 203 } 204 205 TEST_CASE("Handle input status codes") { 206 Browser* bPtr = new Browser{}; 207 Browser& b = *bPtr; 208 DrawState ds{}; 209 ds.bPtr = bPtr; 210 211 std::string st = "ogemini://localhost/cgi-bin/search.py"; 212 for (auto& ch : st) { 213 REQUIRE(mainLoop(ds, b, ch, 0, 0)); 214 } 215 216 REQUIRE(mainLoop(ds, b, '\n', 0, 0)); 217 218 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 219 "gemini://localhost/cgi-bin/search.py"); 220 221 std::string s2 = "test search query"; 222 for (auto& ch : s2) { 223 REQUIRE(mainLoop(ds, b, ch, 0, 0)); 224 } 225 226 REQUIRE(mainLoop(ds, b, 127, 0, 0)); 227 REQUIRE(mainLoop(ds, b, 'y', 0, 0)); 228 229 REQUIRE(mainLoop(ds, b, '\n', 0, 0)); 230 231 REQUIRE(mainLoop(ds, b, 'n', 0, 0)); // don't follow redirect 232 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 233 "gemini://localhost/cgi-bin/search.py?" + urlEncode(s2)); 234 delete bPtr; 235 } 236 237 TEST_CASE("Handle redirect rejection") { 238 Browser* bPtr = new Browser{}; 239 Browser& b = *bPtr; 240 DrawState ds{}; 241 ds.bPtr = bPtr; 242 243 std::string st = "ogemini://localhost/cgi-bin/redirect.py"; 244 for (auto& ch : st) { 245 REQUIRE(mainLoop(ds, b, ch, 0, 0)); 246 } 247 248 REQUIRE(mainLoop(ds, b, '\n', 0, 0)); 249 250 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 251 "gemini://localhost/cgi-bin/redirect.py"); 252 253 REQUIRE(mainLoop(ds, b, 'n', 0, 0)); // don't follow redirect 254 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 255 "about://newtab"); 256 delete bPtr; 257 } 258 259 TEST_CASE("Handle g and G navigation") { 260 Browser* bPtr = new Browser{}; 261 Browser& b = *bPtr; 262 DrawState ds{}; 263 ds.bPtr = bPtr; 264 265 std::string st = "ogemini://localhost/cgi-bin/redirect.py"; 266 for (auto& ch : st) { 267 REQUIRE(mainLoop(ds, b, ch, 100, 100)); 268 } 269 270 REQUIRE(mainLoop(ds, b, '\n', 100, 100)); 271 272 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 273 "gemini://localhost/cgi-bin/redirect.py"); 274 275 REQUIRE(mainLoop(ds, b, 'n', 100, 100)); // don't follow redirect 276 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 277 "about://newtab"); 278 279 REQUIRE(mainLoop(ds, b, 'g', 0, 0)); 280 REQUIRE(mainLoop(ds, b, 'G', 0, 0)); 281 282 REQUIRE(mainLoop(ds, b, 'g', 100, 0)); 283 REQUIRE(mainLoop(ds, b, 'G', 100, 0)); 284 285 REQUIRE(mainLoop(ds, b, 'g', 100, 100)); 286 REQUIRE(mainLoop(ds, b, 'G', 100, 100)); 287 288 REQUIRE(mainLoop(ds, b, 'g', 100, 0)); 289 REQUIRE(mainLoop(ds, b, 'G', 100, 0)); 290 291 delete bPtr; 292 } 293 294 TEST_CASE("Show issue text on non-existent resource request") { 295 Browser* bPtr = new Browser{}; 296 Browser& b = *bPtr; 297 DrawState ds{}; 298 ds.bPtr = bPtr; 299 300 std::string st = 301 "ogemini://localhost/cgi-bin/this-straight-up-cant-exist.py"; 302 for (auto& ch : st) { 303 REQUIRE(mainLoop(ds, b, ch, 100, 100)); 304 } 305 306 REQUIRE(mainLoop(ds, b, '\n', 100, 100)); 307 308 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 309 "about://newtab"); 310 311 REQUIRE(ds.issueText != ""); 312 313 REQUIRE(mainLoop(ds, b, 'o', 100, 100)); 314 315 delete bPtr; 316 } 317 318 TEST_CASE("Redirect on navigation by link number") { 319 Browser* bPtr = new Browser{}; 320 Browser& b = *bPtr; 321 DrawState ds{}; 322 ds.bPtr = bPtr; 323 324 std::string st = "ogemini://localhost/cgi-bin/"; 325 for (auto& ch : st) { 326 REQUIRE(mainLoop(ds, b, ch, 100, 100)); 327 } 328 329 REQUIRE(mainLoop(ds, b, '\n', 100, 100)); 330 331 REQUIRE(mainLoop(ds, b, 'o', 100, 100)); 332 REQUIRE(mainLoop(ds, b, '2', 100, 100)); 333 REQUIRE(mainLoop(ds, b, '\n', 100, 100)); 334 335 REQUIRE(mainLoop(ds, b, 'y', 100, 100)); 336 REQUIRE(ds.issueText == ""); 337 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == 338 "gemini://localhost"); 339 340 delete bPtr; 341 } 342 343 TEST_CASE( 344 "Only return false on 'q' entry when not using input boxes, never crash, " 345 "junk inputs") { 346 setenv("EDITOR", "test", 1); 347 348 rc::check("Never return false / fail except with input 'q'", 349 [](std::string st) { 350 Browser* bPtr = new Browser{}; 351 Browser& b = *bPtr; 352 DrawState ds{}; 353 ds.bPtr = bPtr; 354 RC_LOG(st); 355 for (auto& cur : st) { 356 if (cur == 'q') { 357 if (!ds.handleInput && !ds.handleOpenOther && 358 !ds.handleRedirect) { 359 REQUIRE_FALSE(mainLoop(ds, b, cur, 0, 0)); 360 } else { 361 REQUIRE(mainLoop(ds, b, cur, 0, 0)); 362 } 363 } else { 364 REQUIRE(mainLoop(ds, b, cur, 0, 0)); 365 } 366 } 367 delete bPtr; 368 }); 369 } 370 371 TEST_CASE( 372 "Only return false on 'q' entry when not using input boxes, alphanumeric " 373 "inputs") { 374 setenv("EDITOR", "test", 1); 375 376 for (int i = 0; i < 10; ++i) { 377 Browser* bPtr = new Browser{}; 378 Browser& b = *bPtr; 379 380 DrawState ds{}; 381 ds.bPtr = bPtr; 382 std::string st = genAlNumNlSp(rand() % 100); 383 INFO(st); 384 for (auto& cur : st) { 385 if (cur == 'q') { 386 if (!ds.handleInput && !ds.handleOpenOther && 387 !ds.handleRedirect) { 388 REQUIRE_FALSE(mainLoop(ds, b, cur, 0, 0)); 389 } else { 390 REQUIRE(mainLoop(ds, b, cur, 0, 0)); 391 } 392 } else { 393 REQUIRE(mainLoop(ds, b, cur, 0, 0)); 394 } 395 } 396 delete bPtr; 397 } 398 } 399 400 TEST_CASE("Basic usage with small screen sizes") { 401 setenv("EDITOR", "test", 1); 402 403 for (int i = 0; i < 10; ++i) { 404 int x = rand() % 25; 405 int y = rand() % 10; 406 Browser* bPtr = new Browser{}; 407 Browser& b = *bPtr; 408 DrawState ds{}; 409 ds.bPtr = bPtr; 410 411 std::string cwd = std::filesystem::current_path(); 412 std::string st = "file://" + cwd + "/tests/sites/all_line_types.gmi"; 413 414 REQUIRE(mainLoop(ds, b, 'o', x, y)); 415 416 for (auto& ch : st) { 417 REQUIRE(mainLoop(ds, b, ch, x, y)); 418 } 419 420 REQUIRE(mainLoop(ds, b, '\n', x, y)); 421 422 REQUIRE(b.getCurrentLink()->getLinkDestination().to_string() == st); 423 delete bPtr; 424 } 425 }