From dbe13153f83e0f99caa99835b1231ec51b8128ae Mon Sep 17 00:00:00 2001 From: Taanviir <66136914+Taanviir@users.noreply.github.com> Date: Tue, 11 Jun 2024 23:49:25 +0400 Subject: [PATCH] chore: removed broken testers --- Makefile | 21 ++-- sources/CGI/.tests/test_cgi.cpp | 99 ------------------- sources/CGI/module.mk | 4 - sources/http/.tests/test_http.cpp | 39 -------- sources/http/Message.hpp | 68 ------------- sources/http/handler/DeleteRequestHandler.cpp | 2 +- sources/http/handler/GetRequestHandler.cpp | 2 +- sources/http/module.mk | 4 - sources/server/.tests/ClientTest.cpp | 23 ++++- sources/server/.tests/SOCKET_main.cpp | 69 ++----------- 10 files changed, 39 insertions(+), 292 deletions(-) delete mode 100644 sources/CGI/.tests/test_cgi.cpp delete mode 100644 sources/http/.tests/test_http.cpp diff --git a/Makefile b/Makefile index 0b56cdc..62de072 100644 --- a/Makefile +++ b/Makefile @@ -75,8 +75,8 @@ clean: $(RM) $(OBJS_DIR); \ echo "$(RED)$(BOLD)[ DELETE ]$(RESET) Removed object files."; \ fi - @if [ -f $(TEST_PARSER) ] || [ -f $(TEST_HTTP) ] || [ -f $(TEST_SOCKET) ] || [ -f $(TEST_CGI) ] || [ -f client ]; then \ - $(RM) $(TEST_PARSER) $(TEST_HTTP) $(TEST_SOCKET) $(TEST_CGI) client; \ + @if [ -f $(TEST_PARSER) ] || [ -f $(TEST_SOCKET) ] [ -f client ]; then \ + $(RM) $(TEST_PARSER) $(TEST_SOCKET) client; \ echo "$(RED)$(BOLD)[ DELETE ]$(RESET) Removed testers."; \ fi @@ -92,23 +92,14 @@ re: fclean test_parser: @cd ./$(PARSER_DIR)/$(TESTS_DIR) && ./run_tests.sh && cd - -test_http: - @$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEBUGFLAGS) $(CGI_SRCS) $(HTTP_SRCS) $(TEST_HTTP_SRC) -o $(TEST_HTTP) - @echo "$(BLUE)$(BOLD)[ TEST ]$(RESET) HTTP ready for testing." - -#! SOCKET_main.cpp has a compile error so this test has been commented out -# test_socket: -# @$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEBUGFLAGS) $(SOCKET_SRCS) $(TEST_CLIENT_SRC) -o $(SERVER_DIR)/client @$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEBUGFLAGS) $(SOCKET_SRCS) $(TEST_SOCKET_SRC) -o $(TEST_SOCKET) -# @echo "$(BLUE)$(BOLD)[ TEST ]$(RESET) SOCKET ready for testing." +test_socket: + @$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEBUGFLAGS) $(SOCKET_SRCS) $(TEST_CLIENT_SRC) -o $(SERVER_DIR)/client @$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEBUGFLAGS) $(SOCKET_SRCS) $(TEST_SOCKET_SRC) -o $(TEST_SOCKET) + @echo "$(BLUE)$(BOLD)[ TEST ]$(RESET) SOCKET ready for testing." client: $(TEST_CLIENT_SRC) @$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEBUGFLAGS) $(TEST_CLIENT_SRC) $(SERVER_DIR)/socket/Socket.cpp $(SERVER_DIR)/socket/TCPSocket.cpp -o $@ @echo "$(BLUE)$(BOLD)[ TEST ]$(RESET) Client is ready." -test_cgi: - @$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEBUGFLAGS) $(SANITIZE) $(HTTP_SRCS) $(CGI_SRCS) $(TEST_CGI_SRC) -o $(TEST_CGI) - @echo "$(BLUE)$(BOLD)[ TEST ]$(RESET) CGI ready for testing." - format: @echo "$(BLUE)$(BOLD)[ FORMAT ]$(RESET) Formatting code..." @find . -name "*.cpp" -exec clang-format -i {} + @@ -117,4 +108,4 @@ format: -include $(OBJS:.o=.d) -.PHONY: all run log debug clean fclean re test_parser test_http test_socket client test_cgi format +.PHONY: all run log debug clean fclean re test_parser test_socket client format diff --git a/sources/CGI/.tests/test_cgi.cpp b/sources/CGI/.tests/test_cgi.cpp deleted file mode 100644 index 3fad4b7..0000000 --- a/sources/CGI/.tests/test_cgi.cpp +++ /dev/null @@ -1,99 +0,0 @@ - -// #include -// # include -// # include -// # include -// # include -// # include -// # include -// # include -#include "CGI.hpp" - - -// using namespace webserv::http; - - -// int main(int ac, char **av, char **env) -// { -// (void)ac; -// //(void)av; -// int fd[2]; -// int id; - -// pipe(fd); -// id = fork(); -// if (id == 0) -// { -// //dup2(fd[0], STDIN_FILENO); -// close(fd[0]); -// close(fd[1]); -// if (execve("/bin/ls", ++av, env)) -// write(2, "error\n", 6); -// } -// close(fd[0]); -// close(fd[1]); - -// return (0); -// } - - -int main(int argc, char** argv, char** envp) -{ - (void) argc; - (void) argv; - (void) envp; - vsp headers; - - // for (int i = 0; envp[i] != NULL; ++i) - // { - // cout << envp[i] << endl; - // } - // cout<< endl<first << ": " << it->second << endl; - // } - - CGI cgi(request); - - res = cgi.execute(); - cout << "this return exce CGI : " << res << endl; - - // cgi.execute("stds"); - } catch (runtime_error& e) { - cerr << e.what() << endl; - } - - // const char *pythonScriptPath = - // "/Users/hashim/Desktop/42curses/webserv/CGI/tester/file.sh"; const char - // *pythonInterpreterPath = - // "/Users/hashim/Desktop/42curses/webserv/CGI/tester/file.sh"; - - // // Check if the Python script exists - // if (access(pythonScriptPath, X_OK) == -1) - // { - // cerr << "Error: Python script not found or does not have execution permission." - // << endl; return 1; - // } - - // // Create an instance of the CGI class - // char* scriptArguments[] = { const_cast(pythonScriptPath), nullptr }; - // CGI cgi(const_cast(pythonInterpreterPath), scriptArguments, envp); - - // // Execute the Python script - // cgi.execute("std"); - return 0; -} diff --git a/sources/CGI/module.mk b/sources/CGI/module.mk index 026f360..35f5ec4 100644 --- a/sources/CGI/module.mk +++ b/sources/CGI/module.mk @@ -2,7 +2,3 @@ CGI_SRCS:= $(addprefix $(CGI_DIR)/, CGI.cpp) SRCS += $(CGI_SRCS) - -# tester mains -TEST_CGI:= $(CGI_DIR)/test_cgi -TEST_CGI_SRC:= $(CGI_DIR)/$(TESTS_DIR)/test_cgi.cpp diff --git a/sources/http/.tests/test_http.cpp b/sources/http/.tests/test_http.cpp deleted file mode 100644 index a35d56f..0000000 --- a/sources/http/.tests/test_http.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "Request.hpp" -#include "Response.hpp" -#include "webserv.hpp" - -int main() -{ -#if 0 // parsing test - try - { - Request request(sample_request); - cout << request; - } - catch (runtime_error &e) - { - cerr << e.what() << endl; - } -#endif // parsing test - -#if 0 // basic test - string m = GET; - string path; - string version; - - stringstream line(sample_message); - line.exceptions(std::ios::failbit | std::ios::badbit); - - cout << "GET: " << enumToString(m) << endl; - - line >> enumFromString(m) >> path >> version; - if (line.fail()) - cout << "Failed to parse" << endl; - else { - cout << "METHOD is : " << m << endl; - cout << "PATH is : " << path << endl; - cout << "VERSION is : " << version << endl; - } -#endif // basic test - return 0; -} diff --git a/sources/http/Message.hpp b/sources/http/Message.hpp index 680f426..8d12f25 100644 --- a/sources/http/Message.hpp +++ b/sources/http/Message.hpp @@ -160,72 +160,4 @@ static StatusCodeMap createStatusCodeMap() const StatusCodeMap status_codes_map = createStatusCodeMap(); -/* ------------------------------ HTTP SAMPLES ------------------------------ */ - -static const string sample_response = - "HTTP/1.1 200 OK\r\n" // status line - "Content-Type: text/html; charset=UTF-8\r\n" // headers - "Content-Length: 23\r\n" - "\r\n" - "\r\n" - "Sample Page\r\n" - "

Hello, World!

This is a sample webpage.

\r\n" - "\r\n"; - - -static const string sample_request = - "GET resource/resource HTTP/1.1\r\n" // request-line - "Host: Linode.com\r\n" // headers fields - "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.8) " - "Gecko/20091102 Firefox/3.5.5 \r\n" - "Content-Length: 69\r\n" - "Transfer-Encoding: chunked\r\n" - "Accept-Charset: ISO-8859-1,utf-8\r\n" - "Cache-Control: no-cache\r\n" - "\r\n" // - "17\r\n" - "Wiki aldkjflakdjf w kew\r\n" - "17\r\n" - "Wiki aldkjflakdjf w kew\r\n" - "17\r\n" - "Wiki aldkjflakdjf w kew\r\n" - "0\r\n" - "\r\n" - "Lorem ipsum dolor sit amet, officia excepteur ex fugiat reprehenderit\r\n" - "enim labore culpa sint ad nisi Lorem pariatur mollit ex esse exercitation amet.\r\n" - "Nisi anim cupidatat excepteur officia. Reprehenderit nostrud nostrud ipsum Lorem\r\n" - "est aliquip amet voluptate voluptate dolor minim nulla est proident. Nostrud\r\n" - "officia pariatur ut officia. Sit irure elit esse ea nulla sunt ex occaecat " - "reprehenderit\r\n" - "commodo officia dolor Lorem duis laboris cupidatat officia voluptate.\r\n" - "Culpa proident adipisicing id nulla nisi laboris ex in Lorem sunt duis officia " - "eiusmod.\r\n" - "Aliqua reprehenderit commodo ex non excepteur duis sunt velit enim.\r\n" - "Voluptate laboris sint cupidatat ullamco ut ea consectetur et est culpa et culpa " - "duis.\r\n" - "\r\n"; - -static const string sample_request_cgi = - "GET /Users/hashim/Desktop/42curses/webserv/sources/CGI/.tests/file.sh?name=hashim&lastname=mohamed HTTP/1.1\r\n" // request-line - "Host: Linode.com\r\n" // headers fields - "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.8) " - "Gecko/20091102 Firefox/3.5.5 \r\n" - "Accept-Encoding: gzip,deflate\r\n" - "Accept-Charset: ISO-8859-1,utf-8\r\n" - "Cache-Control: no-cache\r\n" - "\r\n" // - "Lorem ipsum dolor sit amet, officia excepteur ex fugiat reprehenderit\r\n" - "enim labore culpa sint ad nisi Lorem pariatur mollit ex esse exercitation amet.\r\n" - "Nisi anim cupidatat excepteur officia. Reprehenderit nostrud nostrud ipsum Lorem\r\n" - "est aliquip amet voluptate voluptate dolor minim nulla est proident. Nostrud\r\n" - "officia pariatur ut officia. Sit irure elit esse ea nulla sunt ex occaecat " - "reprehenderit\r\n" - "commodo officia dolor Lorem duis laboris cupidatat officia voluptate.\r\n" - "Culpa proident adipisicing id nulla nisi laboris ex in Lorem sunt duis officia " - "eiusmod.\r\n" - "Aliqua reprehenderit commodo ex non excepteur duis sunt velit enim.\r\n" - "Voluptate laboris sint cupidatat ullamco ut ea consectetur et est culpa et culpa " - "duis.\r\n" - "\r\n"; - #endif // MESSAGE_HPP diff --git a/sources/http/handler/DeleteRequestHandler.cpp b/sources/http/handler/DeleteRequestHandler.cpp index e2050b6..88560be 100644 --- a/sources/http/handler/DeleteRequestHandler.cpp +++ b/sources/http/handler/DeleteRequestHandler.cpp @@ -1,6 +1,6 @@ #include "DeleteRequestHandler.hpp" -#include "CachedPages.hpp" #include "CGI.hpp" +#include "CachedPages.hpp" Response DeleteRequestHandler::handle_request(const Request& r) { diff --git a/sources/http/handler/GetRequestHandler.cpp b/sources/http/handler/GetRequestHandler.cpp index 34eff45..7d55891 100644 --- a/sources/http/handler/GetRequestHandler.cpp +++ b/sources/http/handler/GetRequestHandler.cpp @@ -1,6 +1,6 @@ #include "GetRequestHandler.hpp" -#include "CachedPages.hpp" #include "CGI.hpp" +#include "CachedPages.hpp" #include "webserv.hpp" Response GetRequestHandler::handle_request(const Request& r) diff --git a/sources/http/module.mk b/sources/http/module.mk index 57a4cf3..6438aee 100644 --- a/sources/http/module.mk +++ b/sources/http/module.mk @@ -10,7 +10,3 @@ HANDLER_DIR:=$(HTTP_DIR)/handler HANDLER_SRCS:= $(wildcard $(HANDLER_DIR)/*.cpp) SRCS += $(REQUEST_SRCS) $(RESPONSE_SRCS) $(HANDLER_SRCS) $(wildcard ./*cpp) - -# tester mains -TEST_HTTP:= $(HTTP_DIR)/test_http -TEST_HTTP_SRC:= $(HTTP_DIR)/$(TESTS_DIR)/test_http.cpp diff --git a/sources/server/.tests/ClientTest.cpp b/sources/server/.tests/ClientTest.cpp index 11b46df..86d96c9 100644 --- a/sources/server/.tests/ClientTest.cpp +++ b/sources/server/.tests/ClientTest.cpp @@ -4,10 +4,29 @@ #include #include -const string crlf("\r\n"); -const string request("GET /index.html " + HTTP_VER + crlf + crlf); #define BUFFER_SIZE 4096 +static const string sample_request = + "GET resource/resource HTTP/1.1\r\n" // request-line + "Host: Linode.com\r\n" // headers fields + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.8) " + "Gecko/20091102 Firefox/3.5.5 \r\n" + "Content-Length: 69\r\n" + "Transfer-Encoding: chunked\r\n" + "Accept-Charset: ISO-8859-1,utf-8\r\n" + "Cache-Control: no-cache\r\n" + "\r\n" // + "17\r\n" + "Wiki aldkjflakdjf w kew\r\n" + "17\r\n" + "Wiki aldkjflakdjf w kew\r\n" + "17\r\n" + "Wiki aldkjflakdjf w kew\r\n" + "0\r\n" + "\r\n" + "Lorem ipsum dolor sit amet, officia excepteur ex fugiat reprehenderit\r\n" + "\r\n"; + int main() { int sockfd = socket(AF_INET, SOCK_STREAM, 0); diff --git a/sources/server/.tests/SOCKET_main.cpp b/sources/server/.tests/SOCKET_main.cpp index 33c9046..11a6703 100644 --- a/sources/server/.tests/SOCKET_main.cpp +++ b/sources/server/.tests/SOCKET_main.cpp @@ -6,49 +6,39 @@ // Socket tester main add test cases here to test the behaviour of the -// class - - // simple server -#if 0 int main() { // make a listening socket - TCPSocket a; + TCPSocket a(8080, 16, htonl(INADDR_ANY)); // prep it to accept connections on port 8080 a.set_port(8080); // oh-o !!! a.bind(); // set it to listen with backlog 25 - a.listen(25); + a.listen(25); fd_set read_fds; int fdmax = a.get_fd(); - while (true) - { - - cout << "---------- Server is up and waiting!!! ----------------" - << endl; + while (true) { + cout << "---------- Server is up and waiting!!! ----------------" << endl; FD_ZERO(&read_fds); FD_SET(a.get_fd(), &read_fds); - if (select(fdmax + 1, &read_fds, NULL, NULL, NULL) == -1) - { + if (select(fdmax + 1, &read_fds, NULL, NULL, NULL) == -1) { cerr << "Error with select" << endl; continue; } - if (FD_ISSET(a.get_fd(), &read_fds)) - { + if (FD_ISSET(a.get_fd(), &read_fds)) { // accept if a connection happens - file_descriptor peer = a.accept(); + fd peer = a.accept(); cout << "peer is connected on port " << peer << "\n"; // receive data from the connected peer char buffer[1024]; ssize_t bytesRead = recv(peer, buffer, sizeof(buffer), 0); - if (bytesRead == -1) - { + if (bytesRead == -1) { cerr << "Error receiving data from peer" << endl; continue; } @@ -58,49 +48,10 @@ int main() cout << "Received data from peer: " << receivedData << endl; // send a response back to the peer - string response = "Hello from the server!"; - ssize_t bytesSent = send(peer, response.c_str(), response.length(), 0); + string response = "Hello from the server!"; + ssize_t bytesSent = send(peer, response.c_str(), response.length(), 0); if (bytesSent == -1) - { cerr << "Error sending"; - } } } } -#endif // TCPSocket - - -// set_port -#if 1 - -// Define the X macro for test cases -#define TEST_CASES \ - X(0) \ - X(-1) \ - X(-INT_MAX) \ - X(INT_MAX) \ - X(INT_MIN) \ - X('a') \ - X(8080) \ - X(99) - -int main() -{ -#define X(test_case) \ - do { \ - cout << "---- Case[" << test_case << "] -----\n"; \ - TCPSocket socket; \ - try { \ - socket.set_port(test_case); \ - socket.bind(); \ - } catch (Socket::Exception & se) { \ - cerr << "Error in port number\n"; \ - } \ - } while (false); - TEST_CASES -#undef X // Clean up the macro definition - - return 0; -} - -#endif