From ab4c3471b26179b6e1decfb6ca27c4a87df9a0a4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20=C5=A0tetiar?= Date: Mon, 12 Oct 2020 14:06:11 +0200 Subject: [PATCH] tests: add cram based unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit For the start with cgi-exec testing. Signed-off-by: Petr Å tetiar --- tests/CMakeLists.txt | 2 ++ tests/cram/CMakeLists.txt | 25 ++++++++++++++++++++++ tests/cram/test-cases/cgi-exec-01.txt | 1 + tests/cram/test-cases/cgi-exec-02.txt | 1 + tests/cram/test-cases/cgi-exec-03.txt | 1 + tests/cram/test-san_cgi-exec.t | 30 +++++++++++++++++++++++++++ tests/cram/test_cgi-exec.t | 30 +++++++++++++++++++++++++++ 7 files changed, 90 insertions(+) create mode 100644 tests/cram/CMakeLists.txt create mode 100644 tests/cram/test-cases/cgi-exec-01.txt create mode 100644 tests/cram/test-cases/cgi-exec-02.txt create mode 100644 tests/cram/test-cases/cgi-exec-03.txt create mode 100644 tests/cram/test-san_cgi-exec.t create mode 100644 tests/cram/test_cgi-exec.t diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index efad206..5306b91 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +1,5 @@ +ADD_SUBDIRECTORY(cram) + IF(CMAKE_C_COMPILER_ID STREQUAL "Clang") ADD_SUBDIRECTORY(fuzz) ADD_SUBDIRECTORY(fuzz-multipart-parser) diff --git a/tests/cram/CMakeLists.txt b/tests/cram/CMakeLists.txt new file mode 100644 index 0000000..d8b317c --- /dev/null +++ b/tests/cram/CMakeLists.txt @@ -0,0 +1,25 @@ +FIND_PACKAGE(PythonInterp 3 REQUIRED) +FILE(GLOB test_cases "test_*.t") + +IF(CMAKE_C_COMPILER_ID STREQUAL "Clang") + FILE(GLOB test_cases_san "test-san_*.t") +ENDIF() + +SET(PYTHON_VENV_DIR "${CMAKE_CURRENT_BINARY_DIR}/.venv") +SET(PYTHON_VENV_PIP "${PYTHON_VENV_DIR}/bin/pip") +SET(PYTHON_VENV_CRAM "${PYTHON_VENV_DIR}/bin/cram") + +ADD_CUSTOM_COMMAND( + OUTPUT ${PYTHON_VENV_CRAM} + COMMAND ${PYTHON_EXECUTABLE} -m venv ${PYTHON_VENV_DIR} + COMMAND ${PYTHON_VENV_PIP} install cram +) +ADD_CUSTOM_TARGET(prepare-cram-venv ALL DEPENDS ${PYTHON_VENV_CRAM}) + +ADD_TEST( + NAME cram + COMMAND ${PYTHON_VENV_CRAM} ${test_cases} ${test_cases_san} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) + +SET_PROPERTY(TEST cram APPEND PROPERTY ENVIRONMENT "BUILD_BIN_DIR=$") diff --git a/tests/cram/test-cases/cgi-exec-01.txt b/tests/cram/test-cases/cgi-exec-01.txt new file mode 100644 index 0000000..0b03e8b --- /dev/null +++ b/tests/cram/test-cases/cgi-exec-01.txt @@ -0,0 +1 @@ +sessionid=0& diff --git a/tests/cram/test-cases/cgi-exec-02.txt b/tests/cram/test-cases/cgi-exec-02.txt new file mode 100644 index 0000000..a4969b8 --- /dev/null +++ b/tests/cram/test-cases/cgi-exec-02.txt @@ -0,0 +1 @@ +sessionid=0&command=basename /tmp/foo& diff --git a/tests/cram/test-cases/cgi-exec-03.txt b/tests/cram/test-cases/cgi-exec-03.txt new file mode 100644 index 0000000..3561f10 --- /dev/null +++ b/tests/cram/test-cases/cgi-exec-03.txt @@ -0,0 +1 @@ +sessionid=0&command=basename /king/banik/1922&filename=output.txt&mimetype=0& diff --git a/tests/cram/test-san_cgi-exec.t b/tests/cram/test-san_cgi-exec.t new file mode 100644 index 0000000..e4a3356 --- /dev/null +++ b/tests/cram/test-san_cgi-exec.t @@ -0,0 +1,30 @@ +check that cgi-exec is producing expected results: + + $ [ -n "$BUILD_BIN_DIR" ] && export PATH="$BUILD_BIN_DIR:$PATH" + $ ln -sf $BUILD_BIN_DIR/cgi-io-san $BUILD_BIN_DIR/cgi-exec + + $ for file in $(LC_ALL=C find "$TESTDIR/test-cases" -type f | sort); do + > export CONTENT_TYPE="application/x-www-form-urlencoded"; \ + > export CONTENT_LENGTH="$(wc -c < $file)"; \ + > printf "\n[-] testing: $(basename $file)\n"; \ + > cgi-exec < $file; \ + > done + + [-] testing: cgi-exec-01.txt + Status: 400 Invalid command parameter\r (esc) + Content-Type: text/plain\r (esc) + \r (esc) + Invalid command parameter + + [-] testing: cgi-exec-02.txt + Status: 200 OK\r (esc) + Content-Type: application/octet-stream\r (esc) + \r (esc) + foo + + [-] testing: cgi-exec-03.txt + Status: 200 OK\r (esc) + Content-Type: 0\r (esc) + Content-Disposition: attachment; filename="output.txt"\r (esc) + \r (esc) + 1922 diff --git a/tests/cram/test_cgi-exec.t b/tests/cram/test_cgi-exec.t new file mode 100644 index 0000000..d5cffb4 --- /dev/null +++ b/tests/cram/test_cgi-exec.t @@ -0,0 +1,30 @@ +check that cgi-exec is producing expected results: + + $ [ -n "$BUILD_BIN_DIR" ] && export PATH="$BUILD_BIN_DIR:$PATH" + $ ln -sf $BUILD_BIN_DIR/cgi-io $BUILD_BIN_DIR/cgi-exec + + $ for file in $(LC_ALL=C find "$TESTDIR/test-cases" -type f | sort); do + > export CONTENT_TYPE="application/x-www-form-urlencoded"; \ + > export CONTENT_LENGTH="$(wc -c < $file)"; \ + > printf "\n[-] testing: $(basename $file)\n"; \ + > valgrind --quiet --leak-check=full cgi-exec < $file; \ + > done + + [-] testing: cgi-exec-01.txt + Status: 400 Invalid command parameter\r (esc) + Content-Type: text/plain\r (esc) + \r (esc) + Invalid command parameter + + [-] testing: cgi-exec-02.txt + Status: 200 OK\r (esc) + Content-Type: application/octet-stream\r (esc) + \r (esc) + foo + + [-] testing: cgi-exec-03.txt + Status: 200 OK\r (esc) + Content-Type: 0\r (esc) + Content-Disposition: attachment; filename="output.txt"\r (esc) + \r (esc) + 1922 -- 2.30.2