[PATCH cgi-io 12/12] tests: add cram based unit tests

Petr Štetiar ynezz at true.cz
Mon Oct 12 08:37:18 EDT 2020


For the start with cgi-exec testing.

Signed-off-by: Petr Štetiar <ynezz at true.cz>
---
 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 efad20642dd6..5306b913c0d9 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 000000000000..d8b317c40b47
--- /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=$<TARGET_FILE_DIR:cgi-io>")
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 000000000000..0b03e8b008a1
--- /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 000000000000..a4969b8148e0
--- /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 000000000000..3561f1099a45
--- /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 000000000000..e4a33565f84a
--- /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 000000000000..d5cffb45e871
--- /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



More information about the openwrt-devel mailing list