// test_demo_smoke.cpp - ยง13.6 tier 4: PTY smoke of the real demo binary (TTY smoke) #include "../tttest.hpp" #include "../ttgui.hpp" #include #include using namespace ttgui; // This case drives ./demo under a PTY harness, feeds Ctrl+Q, asserts clean exit + restore seq. // Tagged [pty][slow] so default tiers 1-3 skip it; run: ./tests/run_tests --tags [pty] TEST_CASE("demo-smoke: demo under PTY boots, Ctrl+Q exits clean, terminal restored", "[pty][slow][demo]"){ std::string demoPath; for(auto p: {"./demo","../demo","../../demo","demo"}) if(std::filesystem::exists(p)) { demoPath=p; break; } if(demoPath.empty()) SKIP_TEST("demo binary not built (build demo.cpp first)"); int master=-1, slave=-1; winsize ws{}; ws.ws_col=100; ws.ws_row=35; if(openpty(&master,&slave,nullptr,nullptr,&ws)!=0) SKIP_TEST("PTY unavailable"); pid_t pid=fork(); if(pid==0){ close(master); setsid(); ioctl(slave,TIOCSCTTY,0); dup2(slave,0); dup2(slave,1); dup2(slave,2); if(slave>2) close(slave); setenv("TERM","xterm-256color",1); execl(demoPath.c_str(),"demo",(char*)nullptr); _exit(127); } close(slave); std::string out; char buf[4096]; // read boot output for a while for(int i=0;i<60;i++){ fd_set rf; FD_ZERO(&rf); FD_SET(master,&rf); timeval tv{0,100000}; if(select(master+1,&rf,nullptr,nullptr,&tv)>0){ ssize_t n=read(master,buf,sizeof(buf)); if(n>0) out.append(buf,(size_t)n); else break; } if(out.find("Menu")!=std::string::npos && out.size()>2000) break; } // Ctrl+Q (0x11) char q=0x11; (void)!write(master,&q,1); int status=0; bool exited=false; for(int i=0;i<50;i++){ fd_set rf; FD_ZERO(&rf); FD_SET(master,&rf); timeval tv{0,100000}; if(select(master+1,&rf,nullptr,nullptr,&tv)>0){ ssize_t n=read(master,buf,sizeof(buf)); if(n>0) out.append(buf,(size_t)n); } pid_t r=waitpid(pid,&status,WNOHANG); if(r==pid){ exited=true; break; } } if(!exited){ kill(pid,SIGKILL); waitpid(pid,&status,0); } close(master); REQUIRE(exited); CHECK(WIFEXITED(status)); CHECK_EQ(WEXITSTATUS(status),0); CHECK(out.find("\033[?1049h")!=std::string::npos); // alt screen enter CHECK(out.find("\033[?1049l")!=std::string::npos); // restored on exit CHECK(out.find("Menu")!=std::string::npos); } TEST_CASE("demo-smoke: console typing a command does not crash (dangling-this regression)", "[pty][slow][demo]"){ // Regression (screenshot + ASan): ConsoleApp's onEnter captured `this`, // but the app instance dies when createWindow() returns, so the first // console command (typing "ls" + Enter in the screenshot) segfaulted. std::string demoPath; for(auto p: {"./demo","../demo","../../demo","demo"}) if(std::filesystem::exists(p)) { demoPath=p; break; } if(demoPath.empty()) SKIP_TEST("demo binary not built (build demo.cpp first)"); int master=-1, slave=-1; winsize ws{}; ws.ws_col=100; ws.ws_row=35; if(openpty(&master,&slave,nullptr,nullptr,&ws)!=0) SKIP_TEST("PTY unavailable"); pid_t pid=fork(); if(pid==0){ close(master); setsid(); ioctl(slave,TIOCSCTTY,0); dup2(slave,0); dup2(slave,1); dup2(slave,2); if(slave>2) close(slave); setenv("TERM","xterm-256color",1); execl(demoPath.c_str(),"demo",(char*)nullptr); _exit(127); } close(slave); std::string out; char buf[4096]; auto pump=[&](int rounds){ for(int i=0;i0){ ssize_t n=read(master,buf,sizeof(buf)); if(n>0) out.append(buf,(size_t)n); else break; } } }; // Time-based pump: packet counts are not time (the demo renders in small diff chunks). auto pumpFor=[&](int ms){ auto end=std::chrono::steady_clock::now()+std::chrono::milliseconds(ms); while(std::chrono::steady_clock::now()0){ ssize_t n=read(master,buf,sizeof(buf)); if(n>0) out.append(buf,(size_t)n); else break; } } }; auto alive=[&](){ return waitpid(pid,nullptr,WNOHANG)==0; }; pump(60); REQUIRE(out.find("Menu")!=std::string::npos); REQUIRE(alive()); (void)!write(master,"\033[24~",5); pumpFor(1500); // F12 opens the console (void)!write(master,"ls",2); pumpFor(800); // type the reported command (void)!write(master,"\r",1); pumpFor(1500); // Enter -> onEnter (used to segfault here) CHECK(alive()); // Raw output is ANSI diff fragments (words split by cursor moves), so // assert on escape-stripped text, never on contiguous raw substrings. auto stripped=[&](){ std::string s; for(size_t i=0;i='@'&&out[i]<='~')) i++; if(i")!=std::string::npos); char q=0x11; (void)!write(master,&q,1); int status=0; bool exited=false; for(int i=0;i<50;i++){ pump(1); if(waitpid(pid,&status,WNOHANG)==pid){ exited=true; break; } } if(!exited){ kill(pid,SIGKILL); waitpid(pid,&status,0); } close(master); REQUIRE(exited); CHECK(WIFEXITED(status)); CHECK_EQ(WEXITSTATUS(status),0); }